pen.rs

2.0 kB · rust · 62 lines

1use mrlyfont::letters::{digits, extras, lowers, specials, uppers};2use mrlyfont::{draft, floor, glyph, strokes, trim, Glyph};34fn key(c: char) -> String {5    if c.is_ascii() {6        format!("{c:?}")7    } else {8        format!("'\\u{{{:04x}}}'", c as u32)9    }10}1112fn tokens(stroke: &[(usize, usize)]) -> String {13    let cells: Vec<String> = stroke.iter().map(|(r, c)| format!("{r}{c}")).collect();14    cells.join(" ")15}1617fn entry(c: char, strokes: &[Vec<(usize, usize)>]) -> String {18    if strokes.is_empty() {19        return format!("    ({}, &[]),\n", key(c));20    }21    let mut out = format!("    ({}, &[\n", key(c));22    for stroke in strokes {23        out.push_str(&format!("        \"{}\",\n", tokens(stroke)));24    }25    out.push_str("    ]),\n");26    out27}2829fn table(name: &str, doc: &str, glyphs: &[Glyph]) -> String {30    let mut out =31        format!("/// The pens of the {doc}.\n#[rustfmt::skip]\npub const {name}: &[Pen] = &[\n");32    for g in glyphs {33        out.push_str(&entry(g.char, &strokes(g.char)));34    }35    out.push_str("];\n");36    out37}3839fn main() {40    match std::env::args().nth(1).and_then(|a| a.chars().next()) {41        Some(c) => {42            let Some(g) = glyph(c) else {43                eprintln!("{c} is not in the font");44                std::process::exit(1);45            };46            let rows = trim(&g.rows);47            print!("{}", entry(c, &draft(&rows)));48            println!("floor {}", floor(&rows));49        }50        None => {51            let tables = [52                ("UPPERS", "twenty-six uppers", uppers()),53                ("LOWERS", "twenty-six lowers", lowers()),54                ("DIGITS", "ten digits", digits()),55                ("EXTRAS", "punctuation, symbol and arrow glyphs", extras()),56                ("SPECIALS", "four seven-row specials", specials()),57            ];58            let out: Vec<String> = tables.iter().map(|(n, d, g)| table(n, d, g)).collect();59            print!("{}", out.join("\n"));60        }61    }62}