//! Minimal 8×8 ASCII font for rasterizing lines that contain checkbox glyphs. //! Glyphs are public-domain style bitmaps (MSB = leftmost pixel). use image::{GrayImage, Luma}; /// Columns reserved for one checkbox when wrapping text. pub const CHECKBOX_WRAP_COLS: usize = 2; /// Render height of a normal text line (matches Font A row height on ESC/POS). pub const LINE_HEIGHT: u32 = 24; /// Rendered character cell width after nearest-neighbor scale from 8×8. pub const CHAR_WIDTH: u32 = 12; const CHAR_SRC: u32 = 8; #[derive(Debug, Clone, PartialEq, Eq)] pub enum MixedSeg { Text(String), Checkbox { checked: bool }, } pub fn segments_from_inlines(inlines: &[crate::dsl::Inline]) -> Vec { let mut out = Vec::new(); for inline in inlines { match inline { crate::dsl::Inline::Text(t) | crate::dsl::Inline::Param(t) => { if t.is_empty() { continue; } if let Some(MixedSeg::Text(existing)) = out.last_mut() { existing.push_str(t); } else { out.push(MixedSeg::Text(t.clone())); } } crate::dsl::Inline::Checkbox { checked } => { out.push(MixedSeg::Checkbox { checked: *checked, }); } } } out } pub fn inlines_have_checkbox(inlines: &[crate::dsl::Inline]) -> bool { inlines .iter() .any(|i| matches!(i, crate::dsl::Inline::Checkbox { .. })) } /// Word-wrap mixed segments to `max_cols` character columns. pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec> { wrap_segments_hanging(segs, max_cols, "", "") } /// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `). pub fn wrap_segments_hanging( segs: &[MixedSeg], max_cols: usize, first_prefix: &str, next_prefix: &str, ) -> Vec> { let first_budget = max_cols.saturating_sub(first_prefix.chars().count()).max(1); let next_budget = max_cols.saturating_sub(next_prefix.chars().count()).max(1); let atoms = flatten_atoms(segs); if atoms.is_empty() { return vec![prefix_line(first_prefix, Vec::new())]; } let mut lines: Vec> = Vec::new(); let mut current: Vec = Vec::new(); let mut budget = first_budget; let mut is_first = true; for atom in atoms { let atom_w = atom.width(); let cur_w = atoms_width(¤t); let need = if current.is_empty() { atom_w } else { cur_w + 1 + atom_w // space between atoms }; if !current.is_empty() && need > budget { let prefix = if is_first { first_prefix } else { next_prefix }; lines.push(prefix_line(prefix, atoms_to_segs(¤t))); current.clear(); budget = next_budget; is_first = false; current.push(atom); } else if current.is_empty() && atom_w > budget { // Split overlong text atoms. match atom { Atom::Word(word) => { let mut rest = word; while !rest.is_empty() { let chunk: String = rest.chars().take(budget).collect(); let n = chunk.chars().count(); let prefix = if is_first { first_prefix } else { next_prefix }; lines.push(prefix_line(prefix, vec![MixedSeg::Text(chunk)])); rest = rest.chars().skip(n).collect(); budget = next_budget; is_first = false; } } Atom::Checkbox { checked } => { let prefix = if is_first { first_prefix } else { next_prefix }; lines.push(prefix_line( prefix, vec![MixedSeg::Checkbox { checked }], )); budget = next_budget; is_first = false; } } } else { current.push(atom); } } if !current.is_empty() || lines.is_empty() { let prefix = if is_first { first_prefix } else { next_prefix }; lines.push(prefix_line(prefix, atoms_to_segs(¤t))); } lines } fn prefix_line(prefix: &str, mut segs: Vec) -> Vec { if prefix.is_empty() { return segs; } if let Some(MixedSeg::Text(t)) = segs.first_mut() { t.insert_str(0, prefix); segs } else { let mut out = vec![MixedSeg::Text(prefix.to_string())]; out.append(&mut segs); out } } #[derive(Clone)] enum Atom { Word(String), Checkbox { checked: bool }, } impl Atom { fn width(&self) -> usize { match self { Atom::Word(w) => w.chars().count(), Atom::Checkbox { .. } => CHECKBOX_WRAP_COLS, } } } fn flatten_atoms(segs: &[MixedSeg]) -> Vec { let mut atoms = Vec::new(); for seg in segs { match seg { MixedSeg::Checkbox { checked } => { atoms.push(Atom::Checkbox { checked: *checked, }); } MixedSeg::Text(t) => { for word in t.split_whitespace() { atoms.push(Atom::Word(word.to_string())); } } } } atoms } fn atoms_width(atoms: &[Atom]) -> usize { if atoms.is_empty() { return 0; } let words: usize = atoms.iter().map(Atom::width).sum(); words + atoms.len().saturating_sub(1) } fn atoms_to_segs(atoms: &[Atom]) -> Vec { let mut segs = Vec::new(); for (i, atom) in atoms.iter().enumerate() { if i > 0 { if let Some(MixedSeg::Text(t)) = segs.last_mut() { t.push(' '); } else { segs.push(MixedSeg::Text(" ".into())); } } match atom { Atom::Word(w) => { if let Some(MixedSeg::Text(t)) = segs.last_mut() { t.push_str(w); } else { segs.push(MixedSeg::Text(w.clone())); } } Atom::Checkbox { checked } => { segs.push(MixedSeg::Checkbox { checked: *checked, }); } } } segs } /// Rasterize a mixed text/checkbox line to a gray bitmap (black ink on white). pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage { let width = line_width_dots(segs).max(1); let mut img = GrayImage::from_pixel(width, LINE_HEIGHT, Luma([255])); let mut x = 0u32; for seg in segs { match seg { MixedSeg::Text(t) => { for ch in t.chars() { draw_char(&mut img, x, 0, ch); x += CHAR_WIDTH; } } MixedSeg::Checkbox { checked } => { let box_img = render_checkbox(*checked); blit(&mut img, x, 0, &box_img); x += checkbox_width_dots(); } } } img } pub fn checkbox_width_dots() -> u32 { CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32 } fn line_width_dots(segs: &[MixedSeg]) -> u32 { let mut w = 0u32; for seg in segs { match seg { MixedSeg::Text(t) => w += t.chars().count() as u32 * CHAR_WIDTH, MixedSeg::Checkbox { .. } => w += checkbox_width_dots(), } } w } /// Empty or checked checkbox glyph (box + optional checkmark), vertically centered. pub fn render_checkbox(checked: bool) -> GrayImage { let w = checkbox_width_dots(); let mut img = GrayImage::from_pixel(w, LINE_HEIGHT, Luma([255])); // 14×14 box, centered in the 24-dot row and within the cell. let side = 14u32; let x0 = (w.saturating_sub(side)) / 2; let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2; for i in 0..side { put_black(&mut img, x0 + i, y0); put_black(&mut img, x0 + i, y0 + side - 1); put_black(&mut img, x0, y0 + i); put_black(&mut img, x0 + side - 1, y0 + i); // 2px border if i + 1 < side { put_black(&mut img, x0 + i, y0 + 1); put_black(&mut img, x0 + i, y0 + side - 2); put_black(&mut img, x0 + 1, y0 + i); put_black(&mut img, x0 + side - 2, y0 + i); } } if checked { // Checkmark: short stroke then long stroke. draw_thick_line(&mut img, x0 + 3, y0 + 7, x0 + 6, y0 + 10); draw_thick_line(&mut img, x0 + 6, y0 + 10, x0 + 11, y0 + 3); } img } fn put_black(img: &mut GrayImage, x: u32, y: u32) { if x < img.width() && y < img.height() { img.put_pixel(x, y, Luma([0])); } } fn draw_thick_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) { for dx in 0..=1u32 { for dy in 0..=1u32 { draw_line(img, x0 + dx, y0 + dy, x1 + dx, y1 + dy); } } } fn draw_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) { let x0i = x0 as i32; let y0i = y0 as i32; let x1i = x1 as i32; let y1i = y1 as i32; let dx = (x1i - x0i).abs(); let sx = if x0i < x1i { 1 } else { -1 }; let dy = -(y1i - y0i).abs(); let sy = if y0i < y1i { 1 } else { -1 }; let mut err = dx + dy; let mut x = x0i; let mut y = y0i; loop { if x >= 0 && y >= 0 { put_black(img, x as u32, y as u32); } if x == x1i && y == y1i { break; } let e2 = 2 * err; if e2 >= dy { err += dy; x += sx; } if e2 <= dx { err += dx; y += sy; } } } fn blit(dst: &mut GrayImage, x0: u32, y0: u32, src: &GrayImage) { for y in 0..src.height() { for x in 0..src.width() { let p = src.get_pixel(x, y).0[0]; if p < 128 { put_black(dst, x0 + x, y0 + y); } } } } fn draw_char(img: &mut GrayImage, x0: u32, y0: u32, ch: char) { let glyph = glyph_bitmap(ch); // Scale 8×8 → 12×24 with nearest neighbor. for sy in 0..LINE_HEIGHT { for sx in 0..CHAR_WIDTH { let src_x = sx * CHAR_SRC / CHAR_WIDTH; let src_y = sy * CHAR_SRC / LINE_HEIGHT; let row = glyph[src_y as usize]; let on = (row >> (7 - src_x)) & 1 == 1; if on { put_black(img, x0 + sx, y0 + sy); } } } } fn glyph_bitmap(ch: char) -> [u8; 8] { let idx = if ch.is_ascii() && (ch as u8) >= 32 { (ch as u8 - 32) as usize } else { // '?' for unsupported (b'?' - 32) as usize }; FONT8X8_BASIC.get(idx).copied().unwrap_or(FONT8X8_BASIC[b'?' as usize - 32]) } // 8×8 font for ASCII 32..126 (95 glyphs). Row-major, MSB = left. // Derived from the common public-domain font8x8_basic set. const FONT8X8_BASIC: [[u8; 8]; 95] = [ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // [0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00], // ! [0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // " [0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00], // # [0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00], // $ [0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00], // % [0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00], // & [0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], // ' [0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00], // ( [0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00], // ) [0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00], // * [0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00], // + [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06], // , [0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00], // - [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00], // . [0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00], // / [0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00], // 0 [0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00], // 1 [0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00], // 2 [0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00], // 3 [0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00], // 4 [0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00], // 5 [0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00], // 6 [0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00], // 7 [0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00], // 8 [0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00], // 9 [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00], // : [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ; [0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00], // < [0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00], // = [0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00], // > [0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00], // ? [0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00], // @ [0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00], // A [0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00], // B [0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00], // C [0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00], // D [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00], // E [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00], // F [0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00], // G [0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00], // H [0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // I [0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00], // J [0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00], // K [0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00], // L [0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00], // M [0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00], // N [0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00], // O [0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00], // P [0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00], // Q [0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00], // R [0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00], // S [0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // T [0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00], // U [0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // V [0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00], // W [0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00], // X [0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00], // Y [0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00], // Z [0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00], // [ [0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00], // \ [0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00], // ] [0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // ^ [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF], // _ [0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00], // ` [0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00], // a [0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00], // b [0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00], // c [0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00], // d [0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00], // e [0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00], // f [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F], // g [0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00], // h [0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // i [0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E], // j [0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00], // k [0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // l [0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00], // m [0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00], // n [0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00], // o [0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F], // p [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78], // q [0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00], // r [0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00], // s [0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00], // t [0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00], // u [0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // v [0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00], // w [0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00], // x [0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F], // y [0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00], // z [0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00], // { [0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00], // | [0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00], // } [0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // ~ ]; #[cfg(test)] mod tests { use super::*; use crate::dsl::parse; #[test] fn checkbox_has_ink() { let empty = render_checkbox(false); let checked = render_checkbox(true); assert_eq!(empty.dimensions(), checked.dimensions()); let empty_ink: usize = empty.pixels().filter(|p| p.0[0] < 128).count(); let checked_ink: usize = checked.pixels().filter(|p| p.0[0] < 128).count(); assert!(empty_ink > 40, "empty box should draw a border"); assert!( checked_ink > empty_ink, "checked box should have more ink than empty" ); } #[test] fn wraps_checkbox_segments() { let segs = vec![ MixedSeg::Checkbox { checked: false }, MixedSeg::Text("hello world".into()), ]; let lines = wrap_segments(&segs, 10); assert!(lines.len() >= 1); assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false })); } #[test] fn segments_from_parsed_checkboxes() { let doc = parse("[] hi [x]").unwrap(); let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else { panic!("paragraph"); }; assert!(inlines_have_checkbox(inlines)); let segs = segments_from_inlines(inlines); assert_eq!( segs.iter() .filter(|s| matches!(s, MixedSeg::Checkbox { .. })) .count(), 2 ); } #[test] fn render_mixed_line_wider_than_checkbox() { let segs = vec![ MixedSeg::Checkbox { checked: true }, MixedSeg::Text(" Done".into()), ]; let img = render_mixed_line(&segs); assert!(img.width() > checkbox_width_dots()); assert_eq!(img.height(), LINE_HEIGHT); } }