BlueGit

timfa posprint

Fix checkbox line breaks and mirrored bitmap text.

Preserve hard newlines when wrapping mixed segments; read font8x8 bits LSB-first; enlarge checkbox boxes ~20%.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-07-17 · 4 months ago

Hash
d5fd69e1e379177ae950b584b0682aea5216054d
Parent
c3fd0b10
1 file changed +86 −9
src/bitmap_font.rs +86 −9
@@ -4,13 +4,15 @@
4 4 use image::{GrayImage, Luma};
5 5
6 6 /// Columns reserved for one checkbox when wrapping text.
7 pub const CHECKBOX_WRAP_COLS: usize = 2;
7 pub const CHECKBOX_WRAP_COLS: usize = 3;
8 8
9 9 /// Render height of a normal text line (matches Font A row height on ESC/POS).
10 10 pub const LINE_HEIGHT: u32 = 24;
11 11 /// Rendered character cell width after nearest-neighbor scale from 8×8.
12 12 pub const CHAR_WIDTH: u32 = 12;
13 13 const CHAR_SRC: u32 = 8;
14 /// Checkbox box size (~20% larger than the original 14px).
15 const CHECKBOX_SIDE: u32 = 17;
14 16
15 17 #[derive(Debug, Clone, PartialEq, Eq)]
16 18 pub enum MixedSeg {
@@ -49,16 +51,71 @@ pub fn inlines_have_checkbox(inlines: &[crate::dsl::Inline]) -> bool {
49 51 }
50 52
51 53 /// Word-wrap mixed segments to `max_cols` character columns.
54 /// Explicit `\n` inside text segments start a new line (same as [`crate::text_wrap::wrap_text`]).
52 55 pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec<Vec<MixedSeg>> {
53 56 wrap_segments_hanging(segs, max_cols, "", "")
54 57 }
55 58
59 /// Split segments into hard lines on `\n` inside text (checkboxes never contain newlines).
60 fn split_on_newlines(segs: &[MixedSeg]) -> Vec<Vec<MixedSeg>> {
61 let mut lines: Vec<Vec<MixedSeg>> = vec![Vec::new()];
62 for seg in segs {
63 match seg {
64 MixedSeg::Checkbox { checked } => {
65 lines.last_mut().unwrap().push(MixedSeg::Checkbox {
66 checked: *checked,
67 });
68 }
69 MixedSeg::Text(t) => {
70 let parts: Vec<&str> = t.split('\n').collect();
71 for (i, part) in parts.iter().enumerate() {
72 if i > 0 {
73 lines.push(Vec::new());
74 }
75 if part.is_empty() {
76 continue;
77 }
78 let line = lines.last_mut().unwrap();
79 if let Some(MixedSeg::Text(existing)) = line.last_mut() {
80 existing.push_str(part);
81 } else {
82 line.push(MixedSeg::Text((*part).to_string()));
83 }
84 }
85 }
86 }
87 }
88 lines
89 }
90
56 91 /// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `).
92 /// Explicit `\n` inside text start a new hanging-wrapped group.
57 93 pub fn wrap_segments_hanging(
58 94 segs: &[MixedSeg],
59 95 max_cols: usize,
60 96 first_prefix: &str,
61 97 next_prefix: &str,
98 ) -> Vec<Vec<MixedSeg>> {
99 let mut out = Vec::new();
100 for paragraph in split_on_newlines(segs) {
101 out.extend(wrap_segments_hanging_one(
102 &paragraph,
103 max_cols,
104 first_prefix,
105 next_prefix,
106 ));
107 }
108 if out.is_empty() {
109 out.push(prefix_line(first_prefix, Vec::new()));
110 }
111 out
112 }
113
114 fn wrap_segments_hanging_one(
115 segs: &[MixedSeg],
116 max_cols: usize,
117 first_prefix: &str,
118 next_prefix: &str,
62 119 ) -> Vec<Vec<MixedSeg>> {
63 120 let first_budget = max_cols.saturating_sub(first_prefix.chars().count()).max(1);
64 121 let next_budget = max_cols.saturating_sub(next_prefix.chars().count()).max(1);
@@ -234,7 +291,8 @@ pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
234 291 }
235 292
236 293 pub fn checkbox_width_dots() -> u32 {
237 CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32
294 // Side + padding; ~20% larger visual than the original 24-dot cell.
295 CHECKBOX_SIDE + 10
238 296 }
239 297
240 298 fn line_width_dots(segs: &[MixedSeg]) -> u32 {
@@ -252,8 +310,7 @@ fn line_width_dots(segs: &[MixedSeg]) -> u32 {
252 310 pub fn render_checkbox(checked: bool) -> GrayImage {
253 311 let w = checkbox_width_dots();
254 312 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;
313 let side = CHECKBOX_SIDE;
257 314 let x0 = (w.saturating_sub(side)) / 2;
258 315 let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2;
259 316
@@ -272,9 +329,15 @@ pub fn render_checkbox(checked: bool) -> GrayImage {
272 329 }
273 330
274 331 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);
332 // Checkmark proportions scaled with side (was 14px: 3,7 → 6,10 → 11,3).
333 let x1 = x0 + side * 3 / 14;
334 let y1 = y0 + side * 7 / 14;
335 let x2 = x0 + side * 6 / 14;
336 let y2 = y0 + side * 10 / 14;
337 let x3 = x0 + side * 11 / 14;
338 let y3 = y0 + side * 3 / 14;
339 draw_thick_line(&mut img, x1, y1, x2, y2);
340 draw_thick_line(&mut img, x2, y2, x3, y3);
278 341 }
279 342 img
280 343 }
@@ -343,7 +406,8 @@ fn draw_char(img: &mut GrayImage, x0: u32, y0: u32, ch: char) {
343 406 let src_x = sx * CHAR_SRC / CHAR_WIDTH;
344 407 let src_y = sy * CHAR_SRC / LINE_HEIGHT;
345 408 let row = glyph[src_y as usize];
346 let on = (row >> (7 - src_x)) & 1 == 1;
409 // font8x8_basic: least significant bit is the leftmost pixel.
410 let on = (row >> src_x) & 1 == 1;
347 411 if on {
348 412 put_black(img, x0 + sx, y0 + sy);
349 413 }
@@ -487,8 +551,21 @@ mod tests {
487 551 MixedSeg::Text("hello world".into()),
488 552 ];
489 553 let lines = wrap_segments(&segs, 10);
490 assert!(lines.len() >= 1);
554 assert!(!lines.is_empty());
555 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
556 }
557
558 #[test]
559 fn hard_newlines_keep_checkboxes_on_separate_lines() {
560 let doc = parse("[] empty\n[x] done").unwrap();
561 let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
562 panic!("paragraph");
563 };
564 let segs = segments_from_inlines(inlines);
565 let lines = wrap_segments(&segs, 32);
566 assert_eq!(lines.len(), 2, "expected 2 hard lines, got {lines:?}");
491 567 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
568 assert!(matches!(lines[1][0], MixedSeg::Checkbox { checked: true }));
492 569 }
493 570
494 571 #[test]
@@ -4,13 +4,15 @@
4use image::{GrayImage, Luma};
4use image::{GrayImage, Luma};
5
5
6/// Columns reserved for one checkbox when wrapping text.
6/// Columns reserved for one checkbox when wrapping text.
7pub const CHECKBOX_WRAP_COLS: usize = 2;
7pub const CHECKBOX_WRAP_COLS: usize = 3;
8
8
9/// Render height of a normal text line (matches Font A row height on ESC/POS).
9/// Render height of a normal text line (matches Font A row height on ESC/POS).
10pub const LINE_HEIGHT: u32 = 24;
10pub const LINE_HEIGHT: u32 = 24;
11/// Rendered character cell width after nearest-neighbor scale from 8×8.
11/// Rendered character cell width after nearest-neighbor scale from 8×8.
12pub const CHAR_WIDTH: u32 = 12;
12pub const CHAR_WIDTH: u32 = 12;
13const CHAR_SRC: u32 = 8;
13const CHAR_SRC: u32 = 8;
14/// Checkbox box size (~20% larger than the original 14px).
15const CHECKBOX_SIDE: u32 = 17;
14
16
15#[derive(Debug, Clone, PartialEq, Eq)]
17#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum MixedSeg {
18pub enum MixedSeg {
@@ -49,16 +51,71 @@ pub fn inlines_have_checkbox(inlines: &[crate::dsl::Inline]) -> bool {
49}
51}
50
52
51/// Word-wrap mixed segments to `max_cols` character columns.
53/// Word-wrap mixed segments to `max_cols` character columns.
54/// Explicit `\n` inside text segments start a new line (same as [`crate::text_wrap::wrap_text`]).
52pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec<Vec<MixedSeg>> {
55pub fn wrap_segments(segs: &[MixedSeg], max_cols: usize) -> Vec<Vec<MixedSeg>> {
53 wrap_segments_hanging(segs, max_cols, "", "")
56 wrap_segments_hanging(segs, max_cols, "", "")
54}
57}
55
58
59/// Split segments into hard lines on `\n` inside text (checkboxes never contain newlines).
60fn split_on_newlines(segs: &[MixedSeg]) -> Vec<Vec<MixedSeg>> {
61 let mut lines: Vec<Vec<MixedSeg>> = vec![Vec::new()];
62 for seg in segs {
63 match seg {
64 MixedSeg::Checkbox { checked } => {
65 lines.last_mut().unwrap().push(MixedSeg::Checkbox {
66 checked: *checked,
67 });
68 }
69 MixedSeg::Text(t) => {
70 let parts: Vec<&str> = t.split('\n').collect();
71 for (i, part) in parts.iter().enumerate() {
72 if i > 0 {
73 lines.push(Vec::new());
74 }
75 if part.is_empty() {
76 continue;
77 }
78 let line = lines.last_mut().unwrap();
79 if let Some(MixedSeg::Text(existing)) = line.last_mut() {
80 existing.push_str(part);
81 } else {
82 line.push(MixedSeg::Text((*part).to_string()));
83 }
84 }
85 }
86 }
87 }
88 lines
89}
90
56/// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `).
91/// Word-wrap with hanging prefixes (e.g. bullet `* ` / ` `).
92/// Explicit `\n` inside text start a new hanging-wrapped group.
57pub fn wrap_segments_hanging(
93pub fn wrap_segments_hanging(
58 segs: &[MixedSeg],
94 segs: &[MixedSeg],
59 max_cols: usize,
95 max_cols: usize,
60 first_prefix: &str,
96 first_prefix: &str,
61 next_prefix: &str,
97 next_prefix: &str,
98) -> Vec<Vec<MixedSeg>> {
99 let mut out = Vec::new();
100 for paragraph in split_on_newlines(segs) {
101 out.extend(wrap_segments_hanging_one(
102 &paragraph,
103 max_cols,
104 first_prefix,
105 next_prefix,
106 ));
107 }
108 if out.is_empty() {
109 out.push(prefix_line(first_prefix, Vec::new()));
110 }
111 out
112}
113
114fn wrap_segments_hanging_one(
115 segs: &[MixedSeg],
116 max_cols: usize,
117 first_prefix: &str,
118 next_prefix: &str,
62) -> Vec<Vec<MixedSeg>> {
119) -> Vec<Vec<MixedSeg>> {
63 let first_budget = max_cols.saturating_sub(first_prefix.chars().count()).max(1);
120 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);
121 let next_budget = max_cols.saturating_sub(next_prefix.chars().count()).max(1);
@@ -234,7 +291,8 @@ pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
234}
291}
235
292
236pub fn checkbox_width_dots() -> u32 {
293pub fn checkbox_width_dots() -> u32 {
237 CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32
294 // Side + padding; ~20% larger visual than the original 24-dot cell.
295 CHECKBOX_SIDE + 10
238}
296}
239
297
240fn line_width_dots(segs: &[MixedSeg]) -> u32 {
298fn line_width_dots(segs: &[MixedSeg]) -> u32 {
@@ -252,8 +310,7 @@ fn line_width_dots(segs: &[MixedSeg]) -> u32 {
252pub fn render_checkbox(checked: bool) -> GrayImage {
310pub fn render_checkbox(checked: bool) -> GrayImage {
253 let w = checkbox_width_dots();
311 let w = checkbox_width_dots();
254 let mut img = GrayImage::from_pixel(w, LINE_HEIGHT, Luma([255]));
312 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.
313 let side = CHECKBOX_SIDE;
256 let side = 14u32;
257 let x0 = (w.saturating_sub(side)) / 2;
314 let x0 = (w.saturating_sub(side)) / 2;
258 let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2;
315 let y0 = (LINE_HEIGHT.saturating_sub(side)) / 2;
259
316
@@ -272,9 +329,15 @@ pub fn render_checkbox(checked: bool) -> GrayImage {
272 }
329 }
273
330
274 if checked {
331 if checked {
275 // Checkmark: short stroke then long stroke.
332 // Checkmark proportions scaled with side (was 14px: 3,7 → 6,10 → 11,3).
276 draw_thick_line(&mut img, x0 + 3, y0 + 7, x0 + 6, y0 + 10);
333 let x1 = x0 + side * 3 / 14;
277 draw_thick_line(&mut img, x0 + 6, y0 + 10, x0 + 11, y0 + 3);
334 let y1 = y0 + side * 7 / 14;
335 let x2 = x0 + side * 6 / 14;
336 let y2 = y0 + side * 10 / 14;
337 let x3 = x0 + side * 11 / 14;
338 let y3 = y0 + side * 3 / 14;
339 draw_thick_line(&mut img, x1, y1, x2, y2);
340 draw_thick_line(&mut img, x2, y2, x3, y3);
278 }
341 }
279 img
342 img
280}
343}
@@ -343,7 +406,8 @@ fn draw_char(img: &mut GrayImage, x0: u32, y0: u32, ch: char) {
343 let src_x = sx * CHAR_SRC / CHAR_WIDTH;
406 let src_x = sx * CHAR_SRC / CHAR_WIDTH;
344 let src_y = sy * CHAR_SRC / LINE_HEIGHT;
407 let src_y = sy * CHAR_SRC / LINE_HEIGHT;
345 let row = glyph[src_y as usize];
408 let row = glyph[src_y as usize];
346 let on = (row >> (7 - src_x)) & 1 == 1;
409 // font8x8_basic: least significant bit is the leftmost pixel.
410 let on = (row >> src_x) & 1 == 1;
347 if on {
411 if on {
348 put_black(img, x0 + sx, y0 + sy);
412 put_black(img, x0 + sx, y0 + sy);
349 }
413 }
@@ -487,8 +551,21 @@ mod tests {
487 MixedSeg::Text("hello world".into()),
551 MixedSeg::Text("hello world".into()),
488 ];
552 ];
489 let lines = wrap_segments(&segs, 10);
553 let lines = wrap_segments(&segs, 10);
490 assert!(lines.len() >= 1);
554 assert!(!lines.is_empty());
555 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
556 }
557
558 #[test]
559 fn hard_newlines_keep_checkboxes_on_separate_lines() {
560 let doc = parse("[] empty\n[x] done").unwrap();
561 let crate::dsl::Block::Paragraph { inlines, .. } = &doc.blocks[0] else {
562 panic!("paragraph");
563 };
564 let segs = segments_from_inlines(inlines);
565 let lines = wrap_segments(&segs, 32);
566 assert_eq!(lines.len(), 2, "expected 2 hard lines, got {lines:?}");
491 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
567 assert!(matches!(lines[0][0], MixedSeg::Checkbox { checked: false }));
568 assert!(matches!(lines[1][0], MixedSeg::Checkbox { checked: true }));
492 }
569 }
493
570
494 #[test]
571 #[test]