BlueGit

timfa posprint

Files

posprint / src / bitmap_font.rs

bitmap_font.rs

bitmap_font.rs 22.6 KiB Download Raw
1//! Minimal 8×8 ASCII font for rasterizing lines that contain checkbox glyphs.
2//! Glyphs are public-domain style bitmaps (MSB = leftmost pixel).
3
4use image::{GrayImage, Luma};
5
6/// Columns reserved for one checkbox when wrapping text (two Font A cells).
7pub const CHECKBOX_WRAP_COLS: usize = 2;
8
9/// Render height of a normal text line (matches Font A row height on ESC/POS).
10pub const LINE_HEIGHT: u32 = 24;
11/// Font A cell width in dots — checkbox spans two cells via user-defined chars.
12pub const CHAR_WIDTH: u32 = 12;
13const CHAR_SRC: u32 = 8;
14/// Checkbox box size (~20% larger than the original 14px).
15const CHECKBOX_SIDE: u32 = 17;
16
17/// User-defined character codes (ESC &): empty L/R, checked L/R.
18pub const CHAR_EMPTY_L: u8 = 0x01;
19pub const CHAR_EMPTY_R: u8 = 0x02;
20pub const CHAR_CHECKED_L: u8 = 0x03;
21pub const CHAR_CHECKED_R: u8 = 0x04;
22
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum MixedSeg {
25 Text(String),
26 Checkbox { checked: bool },
27}
28
29pub fn segments_from_inlines(inlines: &[crate::dsl::Inline]) -> Vec<MixedSeg> {
30 let mut out = Vec::new();
31 for inline in inlines {
32 match inline {
33 crate::dsl::Inline::Text(t) | crate::dsl::Inline::Param(t) => {
34 if t.is_empty() {
35 continue;
36 }
37 if let Some(MixedSeg::Text(existing)) = out.last_mut() {
38 existing.push_str(t);
39 } else {
40 out.push(MixedSeg::Text(t.clone()));
41 }
42 }
43 crate::dsl::Inline::Checkbox { checked } => {
44 out.push(MixedSeg::Checkbox {
45 checked: *checked,
46 });
47 }
48 }
49 }
50 out
51}
52
53pub fn inlines_have_checkbox(inlines: &[crate::dsl::Inline]) -> bool {
54 inlines
55 .iter()
56 .any(|i| matches!(i, crate::dsl::Inline::Checkbox { .. }))
57}
58
59/// Word-wrap mixed segments to `max_cols` character columns.
60/// Explicit `\n` inside text segments start a new line (same as [`crate::text_wrap::wrap_text`]).
61pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec<Vec<MixedSeg>> {
62 wrap_segments_hanging(segs, max_cols, "", "")
63}
64
65/// Split segments into hard lines on `\n` inside text (checkboxes never contain newlines).
66fn split_on_newlines(segs: &[MixedSeg]) -> Vec<Vec<MixedSeg>> {
67 let mut lines: Vec<Vec<MixedSeg>> = vec![Vec::new()];
68 for seg in segs {
69 match seg {
70 MixedSeg::Checkbox { checked } => {
71 lines.last_mut().unwrap().push(MixedSeg::Checkbox {
72 checked: *checked,
73 });
74 }
75 MixedSeg::Text(t) => {
76 let parts: Vec<&str> = t.split('\n').collect();
77 for (i, part) in parts.iter().enumerate() {
78 if i > 0 {
79 lines.push(Vec::new());
80 }
81 if part.is_empty() {
82 continue;
83 }
84 let line = lines.last_mut().unwrap();
85 if let Some(MixedSeg::Text(existing)) = line.last_mut() {
86 existing.push_str(part);
87 } else {
88 line.push(MixedSeg::Text((*part).to_string()));
89 }
90 }
91 }
92 }
93 }
94 lines
95}
96
97/// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `).
98/// Explicit `\n` inside text start a new hanging-wrapped group.
99pub fn wrap_segments_hanging(
100 segs: &[MixedSeg],
101 max_cols: usize,
102 first_prefix: &str,
103 next_prefix: &str,
104) -> Vec<Vec<MixedSeg>> {
105 let mut out = Vec::new();
106 for paragraph in split_on_newlines(segs) {
107 out.extend(wrap_segments_hanging_one(
108 &paragraph,
109 max_cols,
110 first_prefix,
111 next_prefix,
112 ));
113 }
114 if out.is_empty() {
115 out.push(prefix_line(first_prefix, Vec::new()));
116 }
117 out
118}
119
120fn wrap_segments_hanging_one(
121 segs: &[MixedSeg],
122 max_cols: usize,
123 first_prefix: &str,
124 next_prefix: &str,
125) -> Vec<Vec<MixedSeg>> {
126 let first_budget = max_cols.saturating_sub(first_prefix.chars().count()).max(1);
127 let next_budget = max_cols.saturating_sub(next_prefix.chars().count()).max(1);
128
129 let atoms = flatten_atoms(segs);
130 if atoms.is_empty() {
131 return vec![prefix_line(first_prefix, Vec::new())];
132 }
133
134 let mut lines: Vec<Vec<MixedSeg>> = Vec::new();
135 let mut current: Vec<Atom> = Vec::new();
136 let mut budget = first_budget;
137 let mut is_first = true;
138
139 for atom in atoms {
140 let atom_w = atom.width();
141 let cur_w = atoms_width(&current);
142 let need = if current.is_empty() {
143 atom_w
144 } else {
145 cur_w + 1 + atom_w // space between atoms
146 };
147
148 if !current.is_empty() && need > budget {
149 let prefix = if is_first { first_prefix } else { next_prefix };
150 lines.push(prefix_line(prefix, atoms_to_segs(&current)));
151 current.clear();
152 budget = next_budget;
153 is_first = false;
154 current.push(atom);
155 } else if current.is_empty() && atom_w > budget {
156 // Split overlong text atoms.
157 match atom {
158 Atom::Word(word) => {
159 let mut rest = word;
160 while !rest.is_empty() {
161 let chunk: String = rest.chars().take(budget).collect();
162 let n = chunk.chars().count();
163 let prefix = if is_first { first_prefix } else { next_prefix };
164 lines.push(prefix_line(prefix, vec![MixedSeg::Text(chunk)]));
165 rest = rest.chars().skip(n).collect();
166 budget = next_budget;
167 is_first = false;
168 }
169 }
170 Atom::Checkbox { checked } => {
171 let prefix = if is_first { first_prefix } else { next_prefix };
172 lines.push(prefix_line(
173 prefix,
174 vec![MixedSeg::Checkbox { checked }],
175 ));
176 budget = next_budget;
177 is_first = false;
178 }
179 }
180 } else {
181 current.push(atom);
182 }
183 }
184
185 if !current.is_empty() || lines.is_empty() {
186 let prefix = if is_first { first_prefix } else { next_prefix };
187 lines.push(prefix_line(prefix, atoms_to_segs(&current)));
188 }
189 lines
190}
191
192fn prefix_line(prefix: &str, mut segs: Vec<MixedSeg>) -> Vec<MixedSeg> {
193 if prefix.is_empty() {
194 return segs;
195 }
196 if let Some(MixedSeg::Text(t)) = segs.first_mut() {
197 t.insert_str(0, prefix);
198 segs
199 } else {
200 let mut out = vec![MixedSeg::Text(prefix.to_string())];
201 out.append(&mut segs);
202 out
203 }
204}
205
206#[derive(Clone)]
207enum Atom {
208 Word(String),
209 Checkbox { checked: bool },
210}
211
212impl Atom {
213 fn width(&self) -> usize {
214 match self {
215 Atom::Word(w) => w.chars().count(),
216 Atom::Checkbox { .. } => CHECKBOX_WRAP_COLS,
217 }
218 }
219}
220
221fn flatten_atoms(segs: &[MixedSeg]) -> Vec<Atom> {
222 let mut atoms = Vec::new();
223 for seg in segs {
224 match seg {
225 MixedSeg::Checkbox { checked } => {
226 atoms.push(Atom::Checkbox {
227 checked: *checked,
228 });
229 }
230 MixedSeg::Text(t) => {
231 for word in t.split_whitespace() {
232 atoms.push(Atom::Word(word.to_string()));
233 }
234 }
235 }
236 }
237 atoms
238}
239
240fn atoms_width(atoms: &[Atom]) -> usize {
241 if atoms.is_empty() {
242 return 0;
243 }
244 let words: usize = atoms.iter().map(Atom::width).sum();
245 words + atoms.len().saturating_sub(1)
246}
247
248fn atoms_to_segs(atoms: &[Atom]) -> Vec<MixedSeg> {
249 let mut segs = Vec::new();
250 for (i, atom) in atoms.iter().enumerate() {
251 if i > 0 {
252 if let Some(MixedSeg::Text(t)) = segs.last_mut() {
253 t.push(' ');
254 } else {
255 segs.push(MixedSeg::Text(" ".into()));
256 }
257 }
258 match atom {
259 Atom::Word(w) => {
260 if let Some(MixedSeg::Text(t)) = segs.last_mut() {
261 t.push_str(w);
262 } else {
263 segs.push(MixedSeg::Text(w.clone()));
264 }
265 }
266 Atom::Checkbox { checked } => {
267 segs.push(MixedSeg::Checkbox {
268 checked: *checked,
269 });
270 }
271 }
272 }
273 segs
274}
275
276/// Rasterize a mixed text/checkbox line to a gray bitmap (black ink on white).
277pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
278 let width = line_width_dots(segs).max(1);
279 let mut img = GrayImage::from_pixel(width, LINE_HEIGHT, Luma([255]));
280 let mut x = 0u32;
281 for seg in segs {
282 match seg {
283 MixedSeg::Text(t) => {
284 for ch in t.chars() {
285 draw_char(&mut img, x, 0, ch);
286 x += CHAR_WIDTH;
287 }
288 }
289 MixedSeg::Checkbox { checked } => {
290 let box_img = render_checkbox(*checked);
291 blit(&mut img, x, 0, &box_img);
292 x += checkbox_width_dots();
293 }
294 }
295 }
296 img
297}
298
299pub fn checkbox_width_dots() -> u32 {
300 CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32
301}
302
303/// Column-format data for Font A user-defined characters (ESC & y=3).
304/// Returns (left half, right half), each ≤ 12 columns of 3 bytes.
305pub fn checkbox_font_a_halves(checked: bool) -> (Vec<[u8; 3]>, Vec<[u8; 3]>) {
306 let img = render_checkbox(checked);
307 let cols = gray_to_font_a_columns(&img);
308 let mid = (cols.len() / 2).min(12);
309 let right_len = (cols.len() - mid).min(12);
310 (
311 cols[..mid].to_vec(),
312 cols[mid..mid + right_len].to_vec(),
313 )
314}
315
316fn gray_to_font_a_columns(img: &GrayImage) -> Vec<[u8; 3]> {
317 let w = img.width().min(24);
318 let h = img.height().min(24);
319 let mut cols = Vec::with_capacity(w as usize);
320 for x in 0..w {
321 let mut bytes = [0u8; 3];
322 for y in 0..h {
323 if img.get_pixel(x, y).0[0] < 128 {
324 let byte_idx = (y / 8) as usize;
325 let bit = 7 - (y % 8);
326 bytes[byte_idx] |= 1 << bit;
327 }
328 }
329 cols.push(bytes);
330 }
331 cols
332}
333
334fn line_width_dots(segs: &[MixedSeg]) -> u32 {
335 let mut w = 0u32;
336 for seg in segs {
337 match seg {
338 MixedSeg::Text(t) => w += t.chars().count() as u32 * CHAR_WIDTH,
339 MixedSeg::Checkbox { .. } => w += checkbox_width_dots(),
340 }
341 }
342 w
343}
344
345/// Empty or checked checkbox glyph (box + optional checkmark), vertically centered.
346pub fn render_checkbox(checked: bool) -> GrayImage {
347 let w = checkbox_width_dots();
348 let mut img = GrayImage::from_pixel(w, LINE_HEIGHT, Luma([255]));
349 let side = CHECKBOX_SIDE;
350 let x0 = (w.saturating_sub(side)) / 2;
351 let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2;
352
353 for i in 0..side {
354 put_black(&mut img, x0 + i, y0);
355 put_black(&mut img, x0 + i, y0 + side - 1);
356 put_black(&mut img, x0, y0 + i);
357 put_black(&mut img, x0 + side - 1, y0 + i);
358 // 2px border
359 if i + 1 < side {
360 put_black(&mut img, x0 + i, y0 + 1);
361 put_black(&mut img, x0 + i, y0 + side - 2);
362 put_black(&mut img, x0 + 1, y0 + i);
363 put_black(&mut img, x0 + side - 2, y0 + i);
364 }
365 }
366
367 if checked {
368 // Checkmark proportions scaled with side (was 14px: 3,7 → 6,10 → 11,3).
369 let x1 = x0 + side * 3 / 14;
370 let y1 = y0 + side * 7 / 14;
371 let x2 = x0 + side * 6 / 14;
372 let y2 = y0 + side * 10 / 14;
373 let x3 = x0 + side * 11 / 14;
374 let y3 = y0 + side * 3 / 14;
375 draw_thick_line(&mut img, x1, y1, x2, y2);
376 draw_thick_line(&mut img, x2, y2, x3, y3);
377 }
378 img
379}
380
381fn put_black(img: &mut GrayImage, x: u32, y: u32) {
382 if x < img.width() && y < img.height() {
383 img.put_pixel(x, y, Luma([0]));
384 }
385}
386
387fn draw_thick_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) {
388 for dx in 0..=1u32 {
389 for dy in 0..=1u32 {
390 draw_line(img, x0 + dx, y0 + dy, x1 + dx, y1 + dy);
391 }
392 }
393}
394
395fn draw_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) {
396 let x0i = x0 as i32;
397 let y0i = y0 as i32;
398 let x1i = x1 as i32;
399 let y1i = y1 as i32;
400 let dx = (x1i - x0i).abs();
401 let sx = if x0i < x1i { 1 } else { -1 };
402 let dy = -(y1i - y0i).abs();
403 let sy = if y0i < y1i { 1 } else { -1 };
404 let mut err = dx + dy;
405 let mut x = x0i;
406 let mut y = y0i;
407 loop {
408 if x >= 0 && y >= 0 {
409 put_black(img, x as u32, y as u32);
410 }
411 if x == x1i && y == y1i {
412 break;
413 }
414 let e2 = 2 * err;
415 if e2 >= dy {
416 err += dy;
417 x += sx;
418 }
419 if e2 <= dx {
420 err += dx;
421 y += sy;
422 }
423 }
424}
425
426fn blit(dst: &mut GrayImage, x0: u32, y0: u32, src: &GrayImage) {
427 for y in 0..src.height() {
428 for x in 0..src.width() {
429 let p = src.get_pixel(x, y).0[0];
430 if p < 128 {
431 put_black(dst, x0 + x, y0 + y);
432 }
433 }
434 }
435}
436
437fn draw_char(img: &mut GrayImage, x0: u32, y0: u32, ch: char) {
438 let glyph = glyph_bitmap(ch);
439 // Scale 8×8 → 12×24 with nearest neighbor.
440 for sy in 0..LINE_HEIGHT {
441 for sx in 0..CHAR_WIDTH {
442 let src_x = sx * CHAR_SRC / CHAR_WIDTH;
443 let src_y = sy * CHAR_SRC / LINE_HEIGHT;
444 let row = glyph[src_y as usize];
445 // font8x8_basic: least significant bit is the leftmost pixel.
446 let on = (row >> src_x) & 1 == 1;
447 if on {
448 put_black(img, x0 + sx, y0 + sy);
449 }
450 }
451 }
452}
453
454fn glyph_bitmap(ch: char) -> [u8; 8] {
455 let idx = if ch.is_ascii() && (ch as u8) >= 32 {
456 (ch as u8 - 32) as usize
457 } else {
458 // '?' for unsupported
459 (b'?' - 32) as usize
460 };
461 FONT8X8_BASIC.get(idx).copied().unwrap_or(FONT8X8_BASIC[b'?' as usize - 32])
462}
463
464// 8×8 font for ASCII 32..126 (95 glyphs). Row-major, MSB = left.
465// Derived from the common public-domain font8x8_basic set.
466const FONT8X8_BASIC: [[u8; 8]; 95] = [
467 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], //
468 [0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00], // !
469 [0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // "
470 [0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00], // #
471 [0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00], // $
472 [0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00], // %
473 [0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00], // &
474 [0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], // '
475 [0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00], // (
476 [0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00], // )
477 [0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00], // *
478 [0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00], // +
479 [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ,
480 [0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00], // -
481 [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00], // .
482 [0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00], // /
483 [0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00], // 0
484 [0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00], // 1
485 [0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00], // 2
486 [0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00], // 3
487 [0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00], // 4
488 [0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00], // 5
489 [0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00], // 6
490 [0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00], // 7
491 [0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00], // 8
492 [0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00], // 9
493 [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00], // :
494 [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ;
495 [0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00], // <
496 [0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00], // =
497 [0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00], // >
498 [0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00], // ?
499 [0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00], // @
500 [0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00], // A
501 [0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00], // B
502 [0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00], // C
503 [0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00], // D
504 [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00], // E
505 [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00], // F
506 [0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00], // G
507 [0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00], // H
508 [0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // I
509 [0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00], // J
510 [0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00], // K
511 [0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00], // L
512 [0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00], // M
513 [0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00], // N
514 [0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00], // O
515 [0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00], // P
516 [0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00], // Q
517 [0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00], // R
518 [0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00], // S
519 [0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // T
520 [0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00], // U
521 [0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // V
522 [0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00], // W
523 [0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00], // X
524 [0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00], // Y
525 [0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00], // Z
526 [0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00], // [
527 [0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00], // \
528 [0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00], // ]
529 [0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // ^
530 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF], // _
531 [0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00], // `
532 [0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00], // a
533 [0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00], // b
534 [0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00], // c
535 [0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00], // d
536 [0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00], // e
537 [0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00], // f
538 [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F], // g
539 [0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00], // h
540 [0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // i
541 [0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E], // j
542 [0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00], // k
543 [0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // l
544 [0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00], // m
545 [0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00], // n
546 [0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00], // o
547 [0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F], // p
548 [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78], // q
549 [0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00], // r
550 [0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00], // s
551 [0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00], // t
552 [0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00], // u
553 [0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // v
554 [0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00], // w
555 [0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00], // x
556 [0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F], // y
557 [0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00], // z
558 [0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00], // {
559 [0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00], // |
560 [0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00], // }
561 [0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // ~
562];
563
564#[cfg(test)]
565mod tests {
566 use super::*;
567 use crate::dsl::parse;
568
569 #[test]
570 fn checkbox_has_ink() {
571 let empty = render_checkbox(false);
572 let checked = render_checkbox(true);
573 assert_eq!(empty.dimensions(), checked.dimensions());
574 let empty_ink: usize = empty.pixels().filter(|p| p.0[0] < 128).count();
575 let checked_ink: usize = checked.pixels().filter(|p| p.0[0] < 128).count();
576 assert!(empty_ink > 40, "empty box should draw a border");
577 assert!(
578 checked_ink > empty_ink,
579 "checked box should have more ink than empty"
580 );
581 }
582
583 #[test]
584 fn wraps_checkbox_segments() {
585 let segs = vec![
586 MixedSeg::Checkbox { checked: false },
587 MixedSeg::Text("hello world".into()),
588 ];
589 let lines = wrap_segments(&segs, 10);
590 assert!(!lines.is_empty());
591 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
592 }
593
594 #[test]
595 fn hard_newlines_keep_checkboxes_on_separate_lines() {
596 let doc = parse("[] empty\n[x] done").unwrap();
597 let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
598 panic!("paragraph");
599 };
600 let segs = segments_from_inlines(inlines);
601 let lines = wrap_segments(&segs, 32);
602 assert_eq!(lines.len(), 2, "expected 2 hard lines, got {lines:?}");
603 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
604 assert!(matches!(lines[1][0], MixedSeg::Checkbox { checked: true }));
605 }
606
607 #[test]
608 fn segments_from_parsed_checkboxes() {
609 let doc = parse("[] hi [x]").unwrap();
610 let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
611 panic!("paragraph");
612 };
613 assert!(inlines_have_checkbox(inlines));
614 let segs = segments_from_inlines(inlines);
615 assert_eq!(
616 segs.iter()
617 .filter(|s| matches!(s, MixedSeg::Checkbox { .. }))
618 .count(),
619 2
620 );
621 }
622
623 #[test]
624 fn render_mixed_line_wider_than_checkbox() {
625 let segs = vec![
626 MixedSeg::Checkbox { checked: true },
627 MixedSeg::Text(" Done".into()),
628 ];
629 let img = render_mixed_line(&segs);
630 assert!(img.width() > checkbox_width_dots());
631 assert_eq!(img.height(), LINE_HEIGHT);
632 }
633}