rustimal 0.2.4

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Procedural box-drawing and block characters, so lines connect seamlessly
//! regardless of font metrics or line height.

/// (up, right, down, left) arm weights for U+250C..=U+254B. 1 = light, 2 = heavy.
const ARMS_250C: [[u8; 4]; 64] = [
    [0, 1, 1, 0],
    [0, 2, 1, 0],
    [0, 1, 2, 0],
    [0, 2, 2, 0], // ┌┍┎┏
    [0, 0, 1, 1],
    [0, 0, 1, 2],
    [0, 0, 2, 1],
    [0, 0, 2, 2], // ┐┑┒┓
    [1, 1, 0, 0],
    [1, 2, 0, 0],
    [2, 1, 0, 0],
    [2, 2, 0, 0], // └┕┖┗
    [1, 0, 0, 1],
    [1, 0, 0, 2],
    [2, 0, 0, 1],
    [2, 0, 0, 2], // ┘┙┚┛
    [1, 1, 1, 0],
    [1, 2, 1, 0],
    [2, 1, 1, 0],
    [1, 1, 2, 0], // ├┝┞┟
    [2, 1, 2, 0],
    [2, 2, 1, 0],
    [1, 2, 2, 0],
    [2, 2, 2, 0], // ┠┡┢┣
    [1, 0, 1, 1],
    [1, 0, 1, 2],
    [2, 0, 1, 1],
    [1, 0, 2, 1], // ┤┥┦┧
    [2, 0, 2, 1],
    [2, 0, 1, 2],
    [1, 0, 2, 2],
    [2, 0, 2, 2], // ┨┩┪┫
    [0, 1, 1, 1],
    [0, 1, 1, 2],
    [0, 2, 1, 1],
    [0, 2, 1, 2], // ┬┭┮┯
    [0, 1, 2, 1],
    [0, 1, 2, 2],
    [0, 2, 2, 1],
    [0, 2, 2, 2], // ┰┱┲┳
    [1, 1, 0, 1],
    [1, 1, 0, 2],
    [1, 2, 0, 1],
    [1, 2, 0, 2], // ┴┵┶┷
    [2, 1, 0, 1],
    [2, 1, 0, 2],
    [2, 2, 0, 1],
    [2, 2, 0, 2], // ┸┹┺┻
    [1, 1, 1, 1],
    [1, 1, 1, 2],
    [1, 2, 1, 1],
    [1, 2, 1, 2], // ┼┽┾┿
    [2, 1, 1, 1],
    [1, 1, 2, 1],
    [2, 1, 2, 1],
    [2, 1, 1, 2], // ╀╁╂╃
    [2, 2, 1, 1],
    [1, 1, 2, 2],
    [1, 2, 2, 1],
    [2, 2, 1, 2], // ╄╅╆╇
    [1, 2, 2, 2],
    [2, 1, 2, 2],
    [2, 2, 2, 1],
    [2, 2, 2, 2], // ╈╉╊╋
];

struct Canvas {
    w: i32,
    h: i32,
    data: Vec<u8>,
}

impl Canvas {
    fn new(w: u32, h: u32) -> Self {
        Self { w: w as i32, h: h as i32, data: vec![0; (w * h) as usize] }
    }

    fn fill(&mut self, x0: i32, y0: i32, x1: i32, y1: i32, a: u8) {
        for y in y0.max(0)..y1.min(self.h) {
            for x in x0.max(0)..x1.min(self.w) {
                let p = &mut self.data[(y * self.w + x) as usize];
                *p = (*p).max(a);
            }
        }
    }

    /// Fill a rect given in fractions of the cell, snapped to pixels.
    fn fill_frac(&mut self, fx0: f32, fy0: f32, fx1: f32, fy1: f32, a: u8) {
        let (w, h) = (self.w as f32, self.h as f32);
        self.fill(
            (fx0 * w).round() as i32,
            (fy0 * h).round() as i32,
            (fx1 * w).round() as i32,
            (fy1 * h).round() as i32,
            a,
        );
    }

    /// Antialiased shape from a distance function (distance from pixel center to the stroke centerline).
    fn stroke(&mut self, thickness: f32, dist: impl Fn(f32, f32) -> f32) {
        for y in 0..self.h {
            for x in 0..self.w {
                let d = dist(x as f32 + 0.5, y as f32 + 0.5);
                let cov = (thickness * 0.5 + 0.5 - d).clamp(0.0, 1.0);
                let p = &mut self.data[(y * self.w + x) as usize];
                *p = (*p).max((cov * 255.0) as u8);
            }
        }
    }
}

pub fn draw(c: char, w: u32, h: u32, font_px: f32) -> Option<Vec<u8>> {
    let cp = c as u32;
    if !(0x2500..=0x259F).contains(&cp) || w == 0 || h == 0 {
        return None;
    }
    let mut cv = Canvas::new(w, h);
    let light = (font_px / 12.0).round().max(1.0) as i32;
    let heavy = ((light as f32) * 2.2).round() as i32;

    match cp {
        0x2500..=0x2503 => {
            let arms = [[0, 1, 0, 1], [0, 2, 0, 2], [1, 0, 1, 0], [2, 0, 2, 0]][(cp - 0x2500) as usize];
            arms_draw(&mut cv, arms, light, heavy);
        }
        // Dashed lines: (horizontal?, weight, segments)
        0x2504..=0x250B | 0x254C..=0x254F => {
            let (horiz, weight, n) = match cp {
                0x2504 => (true, 1, 3),
                0x2505 => (true, 2, 3),
                0x2506 => (false, 1, 3),
                0x2507 => (false, 2, 3),
                0x2508 => (true, 1, 4),
                0x2509 => (true, 2, 4),
                0x250A => (false, 1, 4),
                0x250B => (false, 2, 4),
                0x254C => (true, 1, 2),
                0x254D => (true, 2, 2),
                0x254E => (false, 1, 2),
                _ => (false, 2, 2),
            };
            let t = if weight == 1 { light } else { heavy };
            for i in 0..n {
                let s = i as f32 / n as f32;
                let e = s + 0.6 / n as f32;
                if horiz {
                    let y = (cv.h - t) / 2;
                    let (x0, x1) = ((s * w as f32).round() as i32, (e * w as f32).round() as i32);
                    cv.fill(x0, y, x1, y + t, 255);
                } else {
                    let x = (cv.w - t) / 2;
                    let (y0, y1) = ((s * h as f32).round() as i32, (e * h as f32).round() as i32);
                    cv.fill(x, y0, x + t, y1, 255);
                }
            }
        }
        0x250C..=0x254B => arms_draw(&mut cv, ARMS_250C[(cp - 0x250C) as usize], light, heavy),
        0x2550 | 0x2551 | 0x2554 | 0x2557 | 0x255A | 0x255D => double_draw(&mut cv, cp, light),
        0x256D..=0x2570 => {
            let t = light as f32;
            let lx = ((cv.w - light) / 2) as f32 + t * 0.5;
            let ly = ((cv.h - light) / 2) as f32 + t * 0.5;
            let r = (w as f32 * 0.5).min(h as f32 * 0.5).max(1.0);
            // (sx, sy): direction of the horizontal and vertical arms.
            let (sx, sy) = match cp {
                0x256D => (1.0, 1.0),   // ╭
                0x256E => (-1.0, 1.0),  // ╮
                0x256F => (-1.0, -1.0), // ╯
                _ => (1.0, -1.0),       // ╰
            };
            let (acx, acy) = (lx + sx * r, ly + sy * r);
            cv.stroke(t, |px, py| {
                let mut d = f32::MAX;
                if (px - acx) * sx >= 0.0 {
                    d = d.min((py - ly).abs());
                }
                if (py - acy) * sy >= 0.0 {
                    d = d.min((px - lx).abs());
                }
                if (px - acx) * sx <= 0.0 && (py - acy) * sy <= 0.0 {
                    d = d.min((((px - acx).powi(2) + (py - acy).powi(2)).sqrt() - r).abs());
                }
                d
            });
        }
        0x2571..=0x2573 => {
            let (wf, hf) = (w as f32, h as f32);
            let len = (wf * wf + hf * hf).sqrt();
            let diag_a = move |px: f32, py: f32| (px * hf - py * wf).abs() / len; // ╲
            let diag_b = move |px: f32, py: f32| (px * hf + py * wf - wf * hf).abs() / len; // ╱
            let t = light as f32;
            match cp {
                0x2571 => cv.stroke(t, diag_b),
                0x2572 => cv.stroke(t, diag_a),
                _ => cv.stroke(t, |x, y| diag_a(x, y).min(diag_b(x, y))),
            }
        }
        0x2574..=0x257F => {
            let arms = [
                [0, 0, 0, 1],
                [1, 0, 0, 0],
                [0, 1, 0, 0],
                [0, 0, 1, 0],
                [0, 0, 0, 2],
                [2, 0, 0, 0],
                [0, 2, 0, 0],
                [0, 0, 2, 0],
                [0, 2, 0, 1],
                [1, 0, 2, 0],
                [0, 1, 0, 2],
                [2, 0, 1, 0],
            ][(cp - 0x2574) as usize];
            arms_draw(&mut cv, arms, light, heavy);
        }
        0x2580..=0x259F => blocks(&mut cv, cp),
        _ => return None,
    }
    Some(cv.data)
}

fn arms_draw(cv: &mut Canvas, arms: [u8; 4], light: i32, heavy: i32) {
    let thick = |wgt: u8| if wgt == 2 { heavy } else { light };
    // The widest crossing arm determines how far arms extend into the center.
    let vmax = thick(arms[0].max(arms[2]));
    let hmax = thick(arms[1].max(arms[3]));
    let (w, h) = (cv.w, cv.h);
    let vx = |t: i32| (w - t) / 2;
    let hy = |t: i32| (h - t) / 2;
    if arms[0] > 0 {
        let t = thick(arms[0]);
        cv.fill(vx(t), 0, vx(t) + t, hy(hmax) + hmax, 255);
    }
    if arms[2] > 0 {
        let t = thick(arms[2]);
        cv.fill(vx(t), hy(hmax), vx(t) + t, h, 255);
    }
    if arms[1] > 0 {
        let t = thick(arms[1]);
        cv.fill(vx(vmax), hy(t), w, hy(t) + t, 255);
    }
    if arms[3] > 0 {
        let t = thick(arms[3]);
        cv.fill(0, hy(t), vx(vmax) + vmax, hy(t) + t, 255);
    }
}

fn double_draw(cv: &mut Canvas, cp: u32, t: i32) {
    let (w, h) = (cv.w, cv.h);
    let cx = (w - t) / 2;
    let cy = (h - t) / 2;
    let (x_out, x_in, y_out, y_in);
    match cp {
        0x2550 => {
            cv.fill(0, cy - t, w, cy, 255);
            cv.fill(0, cy + t, w, cy + 2 * t, 255);
            return;
        }
        0x2551 => {
            cv.fill(cx - t, 0, cx, h, 255);
            cv.fill(cx + t, 0, cx + 2 * t, h, 255);
            return;
        }
        _ => {}
    }
    // Corners: the outer line hugs the outside of the turn.
    let right = matches!(cp, 0x2554 | 0x255A);
    let down = matches!(cp, 0x2554 | 0x2557);
    if right {
        x_out = cx - t;
        x_in = cx + t;
    } else {
        x_out = cx + t;
        x_in = cx - t;
    }
    if down {
        y_out = cy - t;
        y_in = cy + t;
    } else {
        y_out = cy + t;
        y_in = cy - t;
    }
    for (x, y) in [(x_out, y_out), (x_in, y_in)] {
        if right {
            cv.fill(x, y, w, y + t, 255);
        } else {
            cv.fill(0, y, x + t, y + t, 255);
        }
        if down {
            cv.fill(x, y, x + t, h, 255);
        } else {
            cv.fill(x, 0, x + t, y + t, 255);
        }
    }
}

fn blocks(cv: &mut Canvas, cp: u32) {
    match cp {
        0x2580 => cv.fill_frac(0.0, 0.0, 1.0, 0.5, 255),
        0x2581..=0x2588 => {
            let f = (cp - 0x2580) as f32 / 8.0;
            cv.fill_frac(0.0, 1.0 - f, 1.0, 1.0, 255);
        }
        0x2589..=0x258F => {
            let f = (0x2590 - cp) as f32 / 8.0;
            cv.fill_frac(0.0, 0.0, f, 1.0, 255);
        }
        0x2590 => cv.fill_frac(0.5, 0.0, 1.0, 1.0, 255),
        0x2591 => cv.fill_frac(0.0, 0.0, 1.0, 1.0, 64),
        0x2592 => cv.fill_frac(0.0, 0.0, 1.0, 1.0, 128),
        0x2593 => cv.fill_frac(0.0, 0.0, 1.0, 1.0, 192),
        0x2594 => cv.fill_frac(0.0, 0.0, 1.0, 0.125, 255),
        0x2595 => cv.fill_frac(0.875, 0.0, 1.0, 1.0, 255),
        _ => {
            // Quadrants: bits = UL, UR, LL, LR
            let bits: u8 = match cp {
                0x2596 => 0b0010,
                0x2597 => 0b0001,
                0x2598 => 0b1000,
                0x2599 => 0b1011,
                0x259A => 0b1001,
                0x259B => 0b1110,
                0x259C => 0b1101,
                0x259D => 0b0100,
                0x259E => 0b0110,
                _ => 0b0111,
            };
            if bits & 0b1000 != 0 {
                cv.fill_frac(0.0, 0.0, 0.5, 0.5, 255);
            }
            if bits & 0b0100 != 0 {
                cv.fill_frac(0.5, 0.0, 1.0, 0.5, 255);
            }
            if bits & 0b0010 != 0 {
                cv.fill_frac(0.0, 0.5, 0.5, 1.0, 255);
            }
            if bits & 0b0001 != 0 {
                cv.fill_frac(0.5, 0.5, 1.0, 1.0, 255);
            }
        }
    }
}

/// Private-use code points for the title bar icons.
pub const ICON_MIN: char = '\u{10FFF0}';
pub const ICON_MAX: char = '\u{10FFF1}';
pub const ICON_RESTORE: char = '\u{10FFF2}';
pub const ICON_CLOSE: char = '\u{10FFF3}';

/// Window-control icons drawn as thin antialiased strokes in a `size` x `size` square.
pub fn icon(c: char, size: u32, scale: f32) -> Option<Vec<u8>> {
    if !(ICON_MIN..=ICON_CLOSE).contains(&c) {
        return None;
    }
    let mut cv = Canvas::new(size, size);
    let t = scale.max(1.0);
    let f = size as f32;
    let m = t * 0.5 + 0.5;
    let seg = |px: f32, py: f32, ax: f32, ay: f32, bx: f32, by: f32| {
        let (vx, vy) = (bx - ax, by - ay);
        let k = (((px - ax) * vx + (py - ay) * vy) / (vx * vx + vy * vy)).clamp(0.0, 1.0);
        ((px - ax - vx * k).powi(2) + (py - ay - vy * k).powi(2)).sqrt()
    };
    let rect_outline = |px: f32, py: f32, x0: f32, y0: f32, x1: f32, y1: f32| {
        seg(px, py, x0, y0, x1, y0)
            .min(seg(px, py, x1, y0, x1, y1))
            .min(seg(px, py, x1, y1, x0, y1))
            .min(seg(px, py, x0, y1, x0, y0))
    };
    match c {
        ICON_MIN => cv.stroke(t, |x, y| seg(x, y, m, f * 0.5, f - m, f * 0.5)),
        ICON_MAX => cv.stroke(t, |x, y| rect_outline(x, y, m, m, f - m, f - m)),
        ICON_RESTORE => {
            let o = (f * 0.22).round();
            cv.stroke(t, |x, y| {
                let front = rect_outline(x, y, m, m + o, f - m - o, f - m);
                // Back square: only the parts not hidden behind the front one.
                let back_top = seg(x, y, m + o, m, f - m, m);
                let back_right = seg(x, y, f - m, m, f - m, f - m - o);
                let back_l = seg(x, y, m + o, m, m + o, m + o);
                let back_b = seg(x, y, f - m - o, f - m - o, f - m, f - m - o);
                front.min(back_top).min(back_right).min(back_l).min(back_b)
            })
        }
        _ => cv.stroke(t, |x, y| seg(x, y, m, m, f - m, f - m).min(seg(x, y, f - m, m, m, f - m))),
    }
    Some(cv.data)
}