BlueGit

timfa posprint

Print checkboxes as raster boxes with ticks.

ASCII [ ]/[x] is unreliable on CP437; encode checkbox lines as GS v 0 bitmaps instead.

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

timfa Tim Falken · pushed by timfa

Authored 2026-07-17 · 4 months ago

Hash
c3fd0b1055be2192d6db8f525032ae3ebe4dc738
Parent
00d5035f
9 files changed +609 −34
docs/ARCHITECTURE.md +1 −0
@@ -27,6 +27,7 @@ src/
27 27 ├── dsl/ # Parser and AST
28 28 ├── params.rs # Parameter collect / substitute / preview samples
29 29 ├── escpos.rs # ESC/POS byte encoder (in-tree)
30 ├── bitmap_font.rs # Raster text + checkbox glyphs for mixed lines
30 31 ├── preview.rs # PreviewModel for UI canvas
31 32 ├── text_wrap.rs # Shared wrap (preview + print)
32 33 ├── datetime.rs # @date / @time formatting
@@ -27,6 +27,7 @@ src/
27├── dsl/ # Parser and AST
27├── dsl/ # Parser and AST
28├── params.rs # Parameter collect / substitute / preview samples
28├── params.rs # Parameter collect / substitute / preview samples
29├── escpos.rs # ESC/POS byte encoder (in-tree)
29├── escpos.rs # ESC/POS byte encoder (in-tree)
30├── bitmap_font.rs # Raster text + checkbox glyphs for mixed lines
30├── preview.rs # PreviewModel for UI canvas
31├── preview.rs # PreviewModel for UI canvas
31├── text_wrap.rs # Shared wrap (preview + print)
32├── text_wrap.rs # Shared wrap (preview + print)
32├── datetime.rs # @date / @time formatting
33├── datetime.rs # @date / @time formatting
docs/DSL-SPEC.md +6 −6
@@ -79,12 +79,12 @@ Wrapped lines use a hanging indent:
79 79
80 80 ### Checkboxes
81 81
82 | Syntax | Printed glyph |
83 |--------|---------------|
84 | `[]` or `[ ]` | `[ ]` (empty) |
85 | `[x]` or `[X]` | `[x]` (checked) |
82 | Syntax | Printed result |
83 |--------|----------------|
84 | `[]` or `[ ]` | Empty checkbox (raster) |
85 | `[x]` or `[X]` | Checked checkbox with tick (raster) |
86 86
87 ASCII glyphs keep CP437 printers reliable; raster boxes are a future option if needed.
87 Lines that contain checkboxes are encoded as GS v 0 bitmaps so the box/tick is reliable on CP437 printers. Preview uses Unicode ☐ / ☑.
88 88
89 89 Parameters work inside any text, and inside barcode/QR/image directives (below).
90 90
@@ -269,7 +269,7 @@ Parse errors include 1-based line and column. Typical causes:
269 269 | `# heading` | Double width/height ON → text → OFF |
270 270 | `## heading` | Bold ON → text → OFF |
271 271 | `- item` | ASCII `* ` bullet + hanging wrap |
272 | `[]` / `[ ]` / `[x]` | ASCII checkbox glyphs |
272 | `[]` / `[ ]` / `[x]` | GS v 0 raster checkbox (+ tick when checked) |
273 273 | `:center:` | Justification center |
274 274 | `@feed n` / blank line | ESC d n |
275 275 | `@cut` | GS V partial cut |
@@ -79,12 +79,12 @@ Wrapped lines use a hanging indent:
79
79
80### Checkboxes
80### Checkboxes
81
81
82| Syntax | Printed glyph |
82| Syntax | Printed result |
83|--------|---------------|
83|--------|----------------|
84| `[]` or `[ ]` | `[ ]` (empty) |
84| `[]` or `[ ]` | Empty checkbox (raster) |
85| `[x]` or `[X]` | `[x]` (checked) |
85| `[x]` or `[X]` | Checked checkbox with tick (raster) |
86
86
87ASCII glyphs keep CP437 printers reliable; raster boxes are a future option if needed.
87Lines that contain checkboxes are encoded as GS v 0 bitmaps so the box/tick is reliable on CP437 printers. Preview uses Unicode ☐ / ☑.
88
88
89Parameters work inside any text, and inside barcode/QR/image directives (below).
89Parameters work inside any text, and inside barcode/QR/image directives (below).
90
90
@@ -269,7 +269,7 @@ Parse errors include 1-based line and column. Typical causes:
269| `# heading` | Double width/height ON → text → OFF |
269| `# heading` | Double width/height ON → text → OFF |
270| `## heading` | Bold ON → text → OFF |
270| `## heading` | Bold ON → text → OFF |
271| `- item` | ASCII `* ` bullet + hanging wrap |
271| `- item` | ASCII `* ` bullet + hanging wrap |
272| `[]` / `[ ]` / `[x]` | ASCII checkbox glyphs |
272| `[]` / `[ ]` / `[x]` | GS v 0 raster checkbox (+ tick when checked) |
273| `:center:` | Justification center |
273| `:center:` | Justification center |
274| `@feed n` / blank line | ESC d n |
274| `@feed n` / blank line | ESC d n |
275| `@cut` | GS V partial cut |
275| `@cut` | GS V partial cut |
docs/HELP.md +3 −3
@@ -82,12 +82,12 @@ Long items wrap with a hanging indent (printed/previewed as ASCII `*`):
82 82
83 83 ### Checkboxes
84 84
85 Inline checkboxes (ASCII on print/preview):
85 Inline checkboxes print as a real empty box or a box with a checkmark (raster). Preview shows ☐ / ☑.
86 86
87 87 | Syntax | Result |
88 88 |--------|--------|
89 | `[]` or `[ ]` | Empty box `[ ]` |
90 | `[x]` or `[X]` | Checked box `[x]` |
89 | `[]` or `[ ]` | Empty checkbox |
90 | `[x]` or `[X]` | Checked checkbox |
91 91
92 92 ```markdown
93 93 [] Unchecked
@@ -82,12 +82,12 @@ Long items wrap with a hanging indent (printed/previewed as ASCII `*`):
82
82
83### Checkboxes
83### Checkboxes
84
84
85Inline checkboxes (ASCII on print/preview):
85Inline checkboxes print as a real empty box or a box with a checkmark (raster). Preview shows ☐ / ☑.
86
86
87| Syntax | Result |
87| Syntax | Result |
88|--------|--------|
88|--------|--------|
89| `[]` or `[ ]` | Empty box `[ ]` |
89| `[]` or `[ ]` | Empty checkbox |
90| `[x]` or `[X]` | Checked box `[x]` |
90| `[x]` or `[X]` | Checked checkbox |
91
91
92```markdown
92```markdown
93[] Unchecked
93[] Unchecked
docs/PRINTING.md +1 −0
@@ -29,6 +29,7 @@ Target **Epson ESC/POS** compatible printers (58 mm / 80 mm thermal).
29 29 | Line feed | LF (text lines), ESC d (`@feed` / blank lines) | Text ends with LF; blank/`@feed` use ESC d |
30 30 | Raster flush | LF after GS v 0 | Ensures spacing after images/barcodes/QR rasters |
31 31 | List bullet | ASCII `*` | `- item` lines in templates |
32 | Inline checkbox | GS v 0 raster (box / box+tick) | `[]` / `[ ]` / `[x]` lines |
32 33 | Partial cut | GS V 66 0 | `@cut` in template only |
33 34 | Barcode | GS k | Native symbologies; raster via GS v 0 |
34 35 | QR Code | GS ( k | Model 2; or raster GS v 0 |
@@ -29,6 +29,7 @@ Target **Epson ESC/POS** compatible printers (58 mm / 80 mm thermal).
29| Line feed | LF (text lines), ESC d (`@feed` / blank lines) | Text ends with LF; blank/`@feed` use ESC d |
29| Line feed | LF (text lines), ESC d (`@feed` / blank lines) | Text ends with LF; blank/`@feed` use ESC d |
30| Raster flush | LF after GS v 0 | Ensures spacing after images/barcodes/QR rasters |
30| Raster flush | LF after GS v 0 | Ensures spacing after images/barcodes/QR rasters |
31| List bullet | ASCII `*` | `- item` lines in templates |
31| List bullet | ASCII `*` | `- item` lines in templates |
32| Inline checkbox | GS v 0 raster (box / box+tick) | `[]` / `[ ]` / `[x]` lines |
32| Partial cut | GS V 66 0 | `@cut` in template only |
33| Partial cut | GS V 66 0 | `@cut` in template only |
33| Barcode | GS k | Native symbologies; raster via GS v 0 |
34| Barcode | GS k | Native symbologies; raster via GS v 0 |
34| QR Code | GS ( k | Model 2; or raster GS v 0 |
35| QR Code | GS ( k | Model 2; or raster GS v 0 |
examples/print_checkbox_test.rs +2 −2
@@ -17,7 +17,7 @@ fn main() {
17 17 std::process::exit(1);
18 18 }
19 19
20 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("checkbox-test.pos");
20 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/checkbox-test.pos");
21 21 let file = PosFile::load_from_path(&path).expect("load checkbox-test.pos");
22 22 let bytes = encode_pos_file(&file, path.parent(), &HashMap::new()).expect("encode");
23 23
@@ -30,5 +30,5 @@ fn main() {
30 30 println!("Printing checkbox test to: {}", device.display_name);
31 31 let mut transport = open_printer(&device.target).expect("open printer");
32 32 print_bytes(&mut transport, &bytes).expect("print");
33 println!("OK — check paper for: [] / [ ] / [x] rendered as [ ] [ ] [x]");
33 println!("OK — check paper for real checkbox boxes (empty / empty / checked with tick)");
34 34 }
@@ -17,7 +17,7 @@ fn main() {
17 std::process::exit(1);
17 std::process::exit(1);
18 }
18 }
19
19
20 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("checkbox-test.pos");
20 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples/checkbox-test.pos");
21 let file = PosFile::load_from_path(&path).expect("load checkbox-test.pos");
21 let file = PosFile::load_from_path(&path).expect("load checkbox-test.pos");
22 let bytes = encode_pos_file(&file, path.parent(), &HashMap::new()).expect("encode");
22 let bytes = encode_pos_file(&file, path.parent(), &HashMap::new()).expect("encode");
23
23
@@ -30,5 +30,5 @@ fn main() {
30 println!("Printing checkbox test to: {}", device.display_name);
30 println!("Printing checkbox test to: {}", device.display_name);
31 let mut transport = open_printer(&device.target).expect("open printer");
31 let mut transport = open_printer(&device.target).expect("open printer");
32 print_bytes(&mut transport, &bytes).expect("print");
32 print_bytes(&mut transport, &bytes).expect("print");
33 println!("OK — check paper for: [] / [ ] / [x] rendered as [ ] [ ] [x]");
33 println!("OK — check paper for real checkbox boxes (empty / empty / checked with tick)");
34}
34}
src/bitmap_font.rs added +520 −0
@@ -0,0 +1,520 @@
1 //! Minimal 8×8 ASCII font for rasterizing lines that contain checkbox glyphs.
2 //! Glyphs are public-domain style bitmaps (MSB = leftmost pixel).
3
4 use image::{GrayImage, Luma};
5
6 /// Columns reserved for one checkbox when wrapping text.
7 pub const CHECKBOX_WRAP_COLS: usize = 2;
8
9 /// Render height of a normal text line (matches Font A row height on ESC/POS).
10 pub const LINE_HEIGHT: u32 = 24;
11 /// Rendered character cell width after nearest-neighbor scale from 8×8.
12 pub const CHAR_WIDTH: u32 = 12;
13 const CHAR_SRC: u32 = 8;
14
15 #[derive(Debug, Clone, PartialEq, Eq)]
16 pub enum MixedSeg {
17 Text(String),
18 Checkbox { checked: bool },
19 }
20
21 pub 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
45 pub 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.
52 pub 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 `* ` / ` `).
57 pub 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
129 fn 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)]
144 enum Atom {
145 Word(String),
146 Checkbox { checked: bool },
147 }
148
149 impl 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
158 fn 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
177 fn 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
185 fn 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).
214 pub 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
236 pub fn checkbox_width_dots() -> u32 {
237 CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32
238 }
239
240 fn 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.
252 pub 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
282 fn 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
288 fn 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
296 fn 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
327 fn 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
338 fn 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
354 fn 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.
366 const 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)]
465 mod 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 }
@@ -0,0 +1,520 @@
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}
src/dsl/mod.rs +3 −3
@@ -141,12 +141,12 @@ pub enum Inline {
141 141 },
142 142 }
143 143
144 /// ASCII glyphs for checkboxes (CP437-safe on ESC/POS printers).
144 /// Preview glyphs for checkboxes (Unicode ballot boxes; print uses rasters).
145 145 pub fn checkbox_glyph(checked: bool) -> &'static str {
146 146 if checked {
147 "[x]"
147 "☑"
148 148 } else {
149 "[ ]"
149 "☐"
150 150 }
151 151 }
152 152
@@ -141,12 +141,12 @@ pub enum Inline {
141 },
141 },
142}
142}
143
143
144/// ASCII glyphs for checkboxes (CP437-safe on ESC/POS printers).
144/// Preview glyphs for checkboxes (Unicode ballot boxes; print uses rasters).
145pub fn checkbox_glyph(checked: bool) -> &'static str {
145pub fn checkbox_glyph(checked: bool) -> &'static str {
146 if checked {
146 if checked {
147 "[x]"
147 "☑"
148 } else {
148 } else {
149 "[ ]"
149 "☐"
150 }
150 }
151}
151}
152
152
src/escpos.rs +72 −20
@@ -2,6 +2,7 @@ use std::path::Path;
2 2
3 3 use image::GrayImage;
4 4
5 use crate::bitmap_font::{self, MixedSeg};
5 6 use crate::dsl::{
6 7 Align, BarcodeMode, BarcodeSymbology, Block, Document, ImageWidth, Inline, QrEcc, QrMode,
7 8 };
@@ -59,19 +60,32 @@ impl EscPosEncoder {
59 60 match block {
60 61 Block::Paragraph { align, inlines } => {
61 62 self.set_align(*align);
62 let line = inlines_to_plain(inlines);
63 63 let cols = text_wrap::wrap_cols(&self.metadata, TextWrapStyle::Normal);
64 for part in text_wrap::wrap_text(&line, cols) {
65 self.writeln(&part);
64 if bitmap_font::inlines_have_checkbox(inlines) {
65 self.encode_mixed_inlines(inlines, cols, "", "")?;
66 } else {
67 let line = inlines_to_plain(inlines);
68 for part in text_wrap::wrap_text(&line, cols) {
69 self.writeln(&part);
70 }
66 71 }
67 72 }
68 73 Block::List { align, items } => {
69 74 self.set_align(*align);
70 75 let cols = text_wrap::wrap_cols(&self.metadata, TextWrapStyle::Normal);
71 76 for item in items {
72 let line = inlines_to_plain(item);
73 for part in text_wrap::wrap_bullet_item(&line, cols) {
74 self.writeln(&part);
77 if bitmap_font::inlines_have_checkbox(item) {
78 self.encode_mixed_inlines(
79 item,
80 cols,
81 text_wrap::BULLET_FIRST_PREFIX,
82 text_wrap::BULLET_NEXT_PREFIX,
83 )?;
84 } else {
85 let line = inlines_to_plain(item);
86 for part in text_wrap::wrap_bullet_item(&line, cols) {
87 self.writeln(&part);
88 }
75 89 }
76 90 }
77 91 }
@@ -83,19 +97,24 @@ impl EscPosEncoder {
83 97 TextWrapStyle::Heading2
84 98 };
85 99 let cols = text_wrap::wrap_cols(&self.metadata, wrap_style);
86 let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
87 if *level == 1 {
88 self.buffer.extend_from_slice(&[0x1D, b'!', 0x11]);
100 if bitmap_font::inlines_have_checkbox(inlines) {
101 // Raster path already draws glyphs; skip size/bold toggles.
102 self.encode_mixed_inlines(inlines, cols, "", "")?;
89 103 } else {
90 self.buffer.extend_from_slice(&[0x1B, b'E', 1]);
91 }
92 for part in &wrapped {
93 self.writeln(part);
94 }
95 if *level == 1 {
96 self.buffer.extend_from_slice(&[0x1D, b'!', 0x00]);
97 } else {
98 self.buffer.extend_from_slice(&[0x1B, b'E', 0]);
104 let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
105 if *level == 1 {
106 self.buffer.extend_from_slice(&[0x1D, b'!', 0x11]);
107 } else {
108 self.buffer.extend_from_slice(&[0x1B, b'E', 1]);
109 }
110 for part in &wrapped {
111 self.writeln(part);
112 }
113 if *level == 1 {
114 self.buffer.extend_from_slice(&[0x1D, b'!', 0x00]);
115 } else {
116 self.buffer.extend_from_slice(&[0x1B, b'E', 0]);
117 }
99 118 }
100 119 }
101 120 Block::Rule => {
@@ -182,6 +201,31 @@ impl EscPosEncoder {
182 201 self.buffer.push(b'\n');
183 202 }
184 203
204 fn encode_mixed_inlines(
205 &mut self,
206 inlines: &[Inline],
207 cols: usize,
208 first_prefix: &str,
209 next_prefix: &str,
210 ) -> Result<(), EscPosError> {
211 let segs = bitmap_font::segments_from_inlines(inlines);
212 let lines = if first_prefix.is_empty() && next_prefix.is_empty() {
213 bitmap_font::wrap_segments(&segs, cols)
214 } else {
215 bitmap_font::wrap_segments_hanging(&segs, cols, first_prefix, next_prefix)
216 };
217 for line in lines {
218 self.encode_mixed_line(&line)?;
219 }
220 Ok(())
221 }
222
223 fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
224 let img = bitmap_font::render_mixed_line(segs);
225 let w = img.width().max(1) as u16;
226 self.encode_gray_raster(&img, ImageWidth::Dots(w))
227 }
228
185 229 fn feed(&mut self, lines: u8) {
186 230 if lines == 0 {
187 231 return;
@@ -399,12 +443,20 @@ mod tests {
399 443 }
400 444
401 445 #[test]
402 fn encodes_inline_checkboxes_as_ascii() {
446 fn encodes_inline_checkboxes_as_raster() {
403 447 let doc = parse("[] [ ] [x]").unwrap();
404 448 let mut enc = EscPosEncoder::new(PosMetadata::default());
405 449 let bytes = enc.encode_document(&doc).unwrap();
450 assert!(
451 bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
452 "checkbox lines should be GS v 0 rasters, got {} bytes",
453 bytes.len()
454 );
406 455 let text = String::from_utf8_lossy(bytes);
407 assert!(text.contains("[ ] [ ] [x]\n"));
456 assert!(
457 !text.contains("[ ]") && !text.contains("[x]"),
458 "should not fall back to ASCII checkbox glyphs"
459 );
408 460 }
409 461
410 462 #[test]
@@ -2,6 +2,7 @@ use std::path::Path;
2
2
3use image::GrayImage;
3use image::GrayImage;
4
4
5use crate::bitmap_font::{self, MixedSeg};
5use crate::dsl::{
6use crate::dsl::{
6 Align, BarcodeMode, BarcodeSymbology, Block, Document, ImageWidth, Inline, QrEcc, QrMode,
7 Align, BarcodeMode, BarcodeSymbology, Block, Document, ImageWidth, Inline, QrEcc, QrMode,
7};
8};
@@ -59,19 +60,32 @@ impl EscPosEncoder {
59 match block {
60 match block {
60 Block::Paragraph { align, inlines } => {
61 Block::Paragraph { align, inlines } => {
61 self.set_align(*align);
62 self.set_align(*align);
62 let line = inlines_to_plain(inlines);
63 let cols = text_wrap::wrap_cols(&self.metadata, TextWrapStyle::Normal);
63 let cols = text_wrap::wrap_cols(&self.metadata, TextWrapStyle::Normal);
64 for part in text_wrap::wrap_text(&line, cols) {
64 if bitmap_font::inlines_have_checkbox(inlines) {
65 self.writeln(&part);
65 self.encode_mixed_inlines(inlines, cols, "", "")?;
66 } else {
67 let line = inlines_to_plain(inlines);
68 for part in text_wrap::wrap_text(&line, cols) {
69 self.writeln(&part);
70 }
66 }
71 }
67 }
72 }
68 Block::List { align, items } => {
73 Block::List { align, items } => {
69 self.set_align(*align);
74 self.set_align(*align);
70 let cols = text_wrap::wrap_cols(&self.metadata, TextWrapStyle::Normal);
75 let cols = text_wrap::wrap_cols(&self.metadata, TextWrapStyle::Normal);
71 for item in items {
76 for item in items {
72 let line = inlines_to_plain(item);
77 if bitmap_font::inlines_have_checkbox(item) {
73 for part in text_wrap::wrap_bullet_item(&line, cols) {
78 self.encode_mixed_inlines(
74 self.writeln(&part);
79 item,
80 cols,
81 text_wrap::BULLET_FIRST_PREFIX,
82 text_wrap::BULLET_NEXT_PREFIX,
83 )?;
84 } else {
85 let line = inlines_to_plain(item);
86 for part in text_wrap::wrap_bullet_item(&line, cols) {
87 self.writeln(&part);
88 }
75 }
89 }
76 }
90 }
77 }
91 }
@@ -83,19 +97,24 @@ impl EscPosEncoder {
83 TextWrapStyle::Heading2
97 TextWrapStyle::Heading2
84 };
98 };
85 let cols = text_wrap::wrap_cols(&self.metadata, wrap_style);
99 let cols = text_wrap::wrap_cols(&self.metadata, wrap_style);
86 let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
100 if bitmap_font::inlines_have_checkbox(inlines) {
87 if *level == 1 {
101 // Raster path already draws glyphs; skip size/bold toggles.
88 self.buffer.extend_from_slice(&[0x1D, b'!', 0x11]);
102 self.encode_mixed_inlines(inlines, cols, "", "")?;
89 } else {
103 } else {
90 self.buffer.extend_from_slice(&[0x1B, b'E', 1]);
104 let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
91 }
105 if *level == 1 {
92 for part in &wrapped {
106 self.buffer.extend_from_slice(&[0x1D, b'!', 0x11]);
93 self.writeln(part);
107 } else {
94 }
108 self.buffer.extend_from_slice(&[0x1B, b'E', 1]);
95 if *level == 1 {
109 }
96 self.buffer.extend_from_slice(&[0x1D, b'!', 0x00]);
110 for part in &wrapped {
97 } else {
111 self.writeln(part);
98 self.buffer.extend_from_slice(&[0x1B, b'E', 0]);
112 }
113 if *level == 1 {
114 self.buffer.extend_from_slice(&[0x1D, b'!', 0x00]);
115 } else {
116 self.buffer.extend_from_slice(&[0x1B, b'E', 0]);
117 }
99 }
118 }
100 }
119 }
101 Block::Rule => {
120 Block::Rule => {
@@ -182,6 +201,31 @@ impl EscPosEncoder {
182 self.buffer.push(b'\n');
201 self.buffer.push(b'\n');
183 }
202 }
184
203
204 fn encode_mixed_inlines(
205 &mut self,
206 inlines: &[Inline],
207 cols: usize,
208 first_prefix: &str,
209 next_prefix: &str,
210 ) -> Result<(), EscPosError> {
211 let segs = bitmap_font::segments_from_inlines(inlines);
212 let lines = if first_prefix.is_empty() && next_prefix.is_empty() {
213 bitmap_font::wrap_segments(&segs, cols)
214 } else {
215 bitmap_font::wrap_segments_hanging(&segs, cols, first_prefix, next_prefix)
216 };
217 for line in lines {
218 self.encode_mixed_line(&line)?;
219 }
220 Ok(())
221 }
222
223 fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
224 let img = bitmap_font::render_mixed_line(segs);
225 let w = img.width().max(1) as u16;
226 self.encode_gray_raster(&img, ImageWidth::Dots(w))
227 }
228
185 fn feed(&mut self, lines: u8) {
229 fn feed(&mut self, lines: u8) {
186 if lines == 0 {
230 if lines == 0 {
187 return;
231 return;
@@ -399,12 +443,20 @@ mod tests {
399 }
443 }
400
444
401 #[test]
445 #[test]
402 fn encodes_inline_checkboxes_as_ascii() {
446 fn encodes_inline_checkboxes_as_raster() {
403 let doc = parse("[] [ ] [x]").unwrap();
447 let doc = parse("[] [ ] [x]").unwrap();
404 let mut enc = EscPosEncoder::new(PosMetadata::default());
448 let mut enc = EscPosEncoder::new(PosMetadata::default());
405 let bytes = enc.encode_document(&doc).unwrap();
449 let bytes = enc.encode_document(&doc).unwrap();
450 assert!(
451 bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
452 "checkbox lines should be GS v 0 rasters, got {} bytes",
453 bytes.len()
454 );
406 let text = String::from_utf8_lossy(bytes);
455 let text = String::from_utf8_lossy(bytes);
407 assert!(text.contains("[ ] [ ] [x]\n"));
456 assert!(
457 !text.contains("[ ]") && !text.contains("[x]"),
458 "should not fall back to ASCII checkbox glyphs"
459 );
408 }
460 }
409
461
410 #[test]
462 #[test]
src/lib.rs +1 −0
@@ -1,3 +1,4 @@
1 pub mod bitmap_font;
1 2 pub mod cli;
2 3 pub mod datetime;
3 4 pub mod dsl;
@@ -1,3 +1,4 @@
1pub mod bitmap_font;
1pub mod cli;
2pub mod cli;
2pub mod datetime;
3pub mod datetime;
3pub mod dsl;
4pub mod dsl;