BlueGit

timfa posprint

Files

posprint / src / bitmap_font.rs

bitmap_font.rs

c3fd0b10 History
bitmap_font.rs 18.7 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.
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/// Rendered character cell width after nearest-neighbor scale from 8×8.
12pub const CHAR_WIDTH: u32 = 12;
13const CHAR_SRC: u32 = 8;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum MixedSeg {
17 Text(String),
18 Checkbox { checked: bool },
19}
20
21pub fn segments_from_inlines(inlines: &[crate::dsl::Inline]) -> Vec<MixedSeg> {
22 let mut out = Vec::new();
23 for inline in inlines {
24 match inline {
25 crate::dsl::Inline::Text(t) | crate::dsl::Inline::Param(t) => {
26 if t.is_empty() {
27 continue;
28 }
29 if let Some(MixedSeg::Text(existing)) = out.last_mut() {
30 existing.push_str(t);
31 } else {
32 out.push(MixedSeg::Text(t.clone()));
33 }
34 }
35 crate::dsl::Inline::Checkbox { checked } => {
36 out.push(MixedSeg::Checkbox {
37 checked: *checked,
38 });
39 }
40 }
41 }
42 out
43}
44
45pub fn inlines_have_checkbox(inlines: &[crate::dsl::Inline]) -> bool {
46 inlines
47 .iter()
48 .any(|i| matches!(i, crate::dsl::Inline::Checkbox { .. }))
49}
50
51/// Word-wrap mixed segments to `max_cols` character columns.
52pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec<Vec<MixedSeg>> {
53 wrap_segments_hanging(segs, max_cols, "", "")
54}
55
56/// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `).
57pub fn wrap_segments_hanging(
58 segs: &[MixedSeg],
59 max_cols: usize,
60 first_prefix: &str,
61 next_prefix: &str,
62) -> Vec<Vec<MixedSeg>> {
63 let first_budget = max_cols.saturating_sub(first_prefix.chars().count()).max(1);
64 let next_budget = max_cols.saturating_sub(next_prefix.chars().count()).max(1);
65
66 let atoms = flatten_atoms(segs);
67 if atoms.is_empty() {
68 return vec![prefix_line(first_prefix, Vec::new())];
69 }
70
71 let mut lines: Vec<Vec<MixedSeg>> = Vec::new();
72 let mut current: Vec<Atom> = Vec::new();
73 let mut budget = first_budget;
74 let mut is_first = true;
75
76 for atom in atoms {
77 let atom_w = atom.width();
78 let cur_w = atoms_width(&current);
79 let need = if current.is_empty() {
80 atom_w
81 } else {
82 cur_w + 1 + atom_w // space between atoms
83 };
84
85 if !current.is_empty() && need > budget {
86 let prefix = if is_first { first_prefix } else { next_prefix };
87 lines.push(prefix_line(prefix, atoms_to_segs(&current)));
88 current.clear();
89 budget = next_budget;
90 is_first = false;
91 current.push(atom);
92 } else if current.is_empty() && atom_w > budget {
93 // Split overlong text atoms.
94 match atom {
95 Atom::Word(word) => {
96 let mut rest = word;
97 while !rest.is_empty() {
98 let chunk: String = rest.chars().take(budget).collect();
99 let n = chunk.chars().count();
100 let prefix = if is_first { first_prefix } else { next_prefix };
101 lines.push(prefix_line(prefix, vec![MixedSeg::Text(chunk)]));
102 rest = rest.chars().skip(n).collect();
103 budget = next_budget;
104 is_first = false;
105 }
106 }
107 Atom::Checkbox { checked } => {
108 let prefix = if is_first { first_prefix } else { next_prefix };
109 lines.push(prefix_line(
110 prefix,
111 vec![MixedSeg::Checkbox { checked }],
112 ));
113 budget = next_budget;
114 is_first = false;
115 }
116 }
117 } else {
118 current.push(atom);
119 }
120 }
121
122 if !current.is_empty() || lines.is_empty() {
123 let prefix = if is_first { first_prefix } else { next_prefix };
124 lines.push(prefix_line(prefix, atoms_to_segs(&current)));
125 }
126 lines
127}
128
129fn prefix_line(prefix: &str, mut segs: Vec<MixedSeg>) -> Vec<MixedSeg> {
130 if prefix.is_empty() {
131 return segs;
132 }
133 if let Some(MixedSeg::Text(t)) = segs.first_mut() {
134 t.insert_str(0, prefix);
135 segs
136 } else {
137 let mut out = vec![MixedSeg::Text(prefix.to_string())];
138 out.append(&mut segs);
139 out
140 }
141}
142
143#[derive(Clone)]
144enum Atom {
145 Word(String),
146 Checkbox { checked: bool },
147}
148
149impl Atom {
150 fn width(&self) -> usize {
151 match self {
152 Atom::Word(w) => w.chars().count(),
153 Atom::Checkbox { .. } => CHECKBOX_WRAP_COLS,
154 }
155 }
156}
157
158fn flatten_atoms(segs: &[MixedSeg]) -> Vec<Atom> {
159 let mut atoms = Vec::new();
160 for seg in segs {
161 match seg {
162 MixedSeg::Checkbox { checked } => {
163 atoms.push(Atom::Checkbox {
164 checked: *checked,
165 });
166 }
167 MixedSeg::Text(t) => {
168 for word in t.split_whitespace() {
169 atoms.push(Atom::Word(word.to_string()));
170 }
171 }
172 }
173 }
174 atoms
175}
176
177fn atoms_width(atoms: &[Atom]) -> usize {
178 if atoms.is_empty() {
179 return 0;
180 }
181 let words: usize = atoms.iter().map(Atom::width).sum();
182 words + atoms.len().saturating_sub(1)
183}
184
185fn atoms_to_segs(atoms: &[Atom]) -> Vec<MixedSeg> {
186 let mut segs = Vec::new();
187 for (i, atom) in atoms.iter().enumerate() {
188 if i > 0 {
189 if let Some(MixedSeg::Text(t)) = segs.last_mut() {
190 t.push(' ');
191 } else {
192 segs.push(MixedSeg::Text(" ".into()));
193 }
194 }
195 match atom {
196 Atom::Word(w) => {
197 if let Some(MixedSeg::Text(t)) = segs.last_mut() {
198 t.push_str(w);
199 } else {
200 segs.push(MixedSeg::Text(w.clone()));
201 }
202 }
203 Atom::Checkbox { checked } => {
204 segs.push(MixedSeg::Checkbox {
205 checked: *checked,
206 });
207 }
208 }
209 }
210 segs
211}
212
213/// Rasterize a mixed text/checkbox line to a gray bitmap (black ink on white).
214pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
215 let width = line_width_dots(segs).max(1);
216 let mut img = GrayImage::from_pixel(width, LINE_HEIGHT, Luma([255]));
217 let mut x = 0u32;
218 for seg in segs {
219 match seg {
220 MixedSeg::Text(t) => {
221 for ch in t.chars() {
222 draw_char(&mut img, x, 0, ch);
223 x += CHAR_WIDTH;
224 }
225 }
226 MixedSeg::Checkbox { checked } => {
227 let box_img = render_checkbox(*checked);
228 blit(&mut img, x, 0, &box_img);
229 x += checkbox_width_dots();
230 }
231 }
232 }
233 img
234}
235
236pub fn checkbox_width_dots() -> u32 {
237 CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32
238}
239
240fn line_width_dots(segs: &[MixedSeg]) -> u32 {
241 let mut w = 0u32;
242 for seg in segs {
243 match seg {
244 MixedSeg::Text(t) => w += t.chars().count() as u32 * CHAR_WIDTH,
245 MixedSeg::Checkbox { .. } => w += checkbox_width_dots(),
246 }
247 }
248 w
249}
250
251/// Empty or checked checkbox glyph (box + optional checkmark), vertically centered.
252pub fn render_checkbox(checked: bool) -> GrayImage {
253 let w = checkbox_width_dots();
254 let mut img = GrayImage::from_pixel(w, LINE_HEIGHT, Luma([255]));
255 // 14×14 box, centered in the 24-dot row and within the cell.
256 let side = 14u32;
257 let x0 = (w.saturating_sub(side)) / 2;
258 let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2;
259
260 for i in 0..side {
261 put_black(&mut img, x0 + i, y0);
262 put_black(&mut img, x0 + i, y0 + side - 1);
263 put_black(&mut img, x0, y0 + i);
264 put_black(&mut img, x0 + side - 1, y0 + i);
265 // 2px border
266 if i + 1 < side {
267 put_black(&mut img, x0 + i, y0 + 1);
268 put_black(&mut img, x0 + i, y0 + side - 2);
269 put_black(&mut img, x0 + 1, y0 + i);
270 put_black(&mut img, x0 + side - 2, y0 + i);
271 }
272 }
273
274 if checked {
275 // Checkmark: short stroke then long stroke.
276 draw_thick_line(&mut img, x0 + 3, y0 + 7, x0 + 6, y0 + 10);
277 draw_thick_line(&mut img, x0 + 6, y0 + 10, x0 + 11, y0 + 3);
278 }
279 img
280}
281
282fn put_black(img: &mut GrayImage, x: u32, y: u32) {
283 if x < img.width() && y < img.height() {
284 img.put_pixel(x, y, Luma([0]));
285 }
286}
287
288fn draw_thick_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) {
289 for dx in 0..=1u32 {
290 for dy in 0..=1u32 {
291 draw_line(img, x0 + dx, y0 + dy, x1 + dx, y1 + dy);
292 }
293 }
294}
295
296fn draw_line(img: &mut GrayImage, x0: u32, y0: u32, x1: u32, y1: u32) {
297 let x0i = x0 as i32;
298 let y0i = y0 as i32;
299 let x1i = x1 as i32;
300 let y1i = y1 as i32;
301 let dx = (x1i - x0i).abs();
302 let sx = if x0i < x1i { 1 } else { -1 };
303 let dy = -(y1i - y0i).abs();
304 let sy = if y0i < y1i { 1 } else { -1 };
305 let mut err = dx + dy;
306 let mut x = x0i;
307 let mut y = y0i;
308 loop {
309 if x >= 0 && y >= 0 {
310 put_black(img, x as u32, y as u32);
311 }
312 if x == x1i && y == y1i {
313 break;
314 }
315 let e2 = 2 * err;
316 if e2 >= dy {
317 err += dy;
318 x += sx;
319 }
320 if e2 <= dx {
321 err += dx;
322 y += sy;
323 }
324 }
325}
326
327fn blit(dst: &mut GrayImage, x0: u32, y0: u32, src: &GrayImage) {
328 for y in 0..src.height() {
329 for x in 0..src.width() {
330 let p = src.get_pixel(x, y).0[0];
331 if p < 128 {
332 put_black(dst, x0 + x, y0 + y);
333 }
334 }
335 }
336}
337
338fn draw_char(img: &mut GrayImage, x0: u32, y0: u32, ch: char) {
339 let glyph = glyph_bitmap(ch);
340 // Scale 8×8 → 12×24 with nearest neighbor.
341 for sy in 0..LINE_HEIGHT {
342 for sx in 0..CHAR_WIDTH {
343 let src_x = sx * CHAR_SRC / CHAR_WIDTH;
344 let src_y = sy * CHAR_SRC / LINE_HEIGHT;
345 let row = glyph[src_y as usize];
346 let on = (row >> (7 - src_x)) & 1 == 1;
347 if on {
348 put_black(img, x0 + sx, y0 + sy);
349 }
350 }
351 }
352}
353
354fn glyph_bitmap(ch: char) -> [u8; 8] {
355 let idx = if ch.is_ascii() && (ch as u8) >= 32 {
356 (ch as u8 - 32) as usize
357 } else {
358 // '?' for unsupported
359 (b'?' - 32) as usize
360 };
361 FONT8X8_BASIC.get(idx).copied().unwrap_or(FONT8X8_BASIC[b'?' as usize - 32])
362}
363
364// 8×8 font for ASCII 32..126 (95 glyphs). Row-major, MSB = left.
365// Derived from the common public-domain font8x8_basic set.
366const FONT8X8_BASIC: [[u8; 8]; 95] = [
367 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], //
368 [0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00], // !
369 [0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // "
370 [0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00], // #
371 [0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00], // $
372 [0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00], // %
373 [0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00], // &
374 [0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00], // '
375 [0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00], // (
376 [0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00], // )
377 [0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00], // *
378 [0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00], // +
379 [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ,
380 [0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00], // -
381 [0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00], // .
382 [0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00], // /
383 [0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00], // 0
384 [0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00], // 1
385 [0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00], // 2
386 [0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00], // 3
387 [0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00], // 4
388 [0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00], // 5
389 [0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00], // 6
390 [0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00], // 7
391 [0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00], // 8
392 [0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00], // 9
393 [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00], // :
394 [0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06], // ;
395 [0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00], // <
396 [0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00], // =
397 [0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00], // >
398 [0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00], // ?
399 [0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00], // @
400 [0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00], // A
401 [0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00], // B
402 [0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00], // C
403 [0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00], // D
404 [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00], // E
405 [0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00], // F
406 [0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00], // G
407 [0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00], // H
408 [0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // I
409 [0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00], // J
410 [0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00], // K
411 [0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00], // L
412 [0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00], // M
413 [0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00], // N
414 [0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00], // O
415 [0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00], // P
416 [0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00], // Q
417 [0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00], // R
418 [0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00], // S
419 [0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // T
420 [0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00], // U
421 [0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // V
422 [0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00], // W
423 [0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00], // X
424 [0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00], // Y
425 [0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00], // Z
426 [0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00], // [
427 [0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00], // \
428 [0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00], // ]
429 [0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00], // ^
430 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF], // _
431 [0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00], // `
432 [0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00], // a
433 [0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00], // b
434 [0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00], // c
435 [0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00], // d
436 [0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00], // e
437 [0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00], // f
438 [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F], // g
439 [0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00], // h
440 [0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // i
441 [0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E], // j
442 [0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00], // k
443 [0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00], // l
444 [0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00], // m
445 [0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00], // n
446 [0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00], // o
447 [0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F], // p
448 [0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78], // q
449 [0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00], // r
450 [0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00], // s
451 [0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00], // t
452 [0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00], // u
453 [0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00], // v
454 [0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00], // w
455 [0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00], // x
456 [0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F], // y
457 [0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00], // z
458 [0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00], // {
459 [0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00], // |
460 [0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00], // }
461 [0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // ~
462];
463
464#[cfg(test)]
465mod tests {
466 use super::*;
467 use crate::dsl::parse;
468
469 #[test]
470 fn checkbox_has_ink() {
471 let empty = render_checkbox(false);
472 let checked = render_checkbox(true);
473 assert_eq!(empty.dimensions(), checked.dimensions());
474 let empty_ink: usize = empty.pixels().filter(|p| p.0[0] < 128).count();
475 let checked_ink: usize = checked.pixels().filter(|p| p.0[0] < 128).count();
476 assert!(empty_ink > 40, "empty box should draw a border");
477 assert!(
478 checked_ink > empty_ink,
479 "checked box should have more ink than empty"
480 );
481 }
482
483 #[test]
484 fn wraps_checkbox_segments() {
485 let segs = vec![
486 MixedSeg::Checkbox { checked: false },
487 MixedSeg::Text("hello world".into()),
488 ];
489 let lines = wrap_segments(&segs, 10);
490 assert!(lines.len() >= 1);
491 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
492 }
493
494 #[test]
495 fn segments_from_parsed_checkboxes() {
496 let doc = parse("[] hi [x]").unwrap();
497 let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
498 panic!("paragraph");
499 };
500 assert!(inlines_have_checkbox(inlines));
501 let segs = segments_from_inlines(inlines);
502 assert_eq!(
503 segs.iter()
504 .filter(|s| matches!(s, MixedSeg::Checkbox { .. }))
505 .count(),
506 2
507 );
508 }
509
510 #[test]
511 fn render_mixed_line_wider_than_checkbox() {
512 let segs = vec![
513 MixedSeg::Checkbox { checked: true },
514 MixedSeg::Text(" Done".into()),
515 ];
516 let img = render_mixed_line(&segs);
517 assert!(img.width() > checkbox_width_dots());
518 assert_eq!(img.height(), LINE_HEIGHT);
519 }
520}