BlueGit

timfa posprint

Print checkboxes as inline user-defined Font A characters.

ESC * was not buffered on the micro-printer; ESC & glyphs stay in the text line with the native font.

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

timfa Tim Falken · pushed by timfa

Authored 2026-07-17 · 4 months ago

Hash
118ec91aad607ec3e4e65b7162d8a275d8d3170d
Parent
5fc54f96
6 files changed +99 −35
docs/ARCHITECTURE.md +1 −1
@@ -27,7 +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 # Checkbox glyphs + wrap for mixed checkbox/text lines
30 ├── bitmap_font.rs # Checkbox glyphs (user-defined chars) + mixed-line wrap
31 31 ├── preview.rs # PreviewModel for UI canvas
32 32 ├── text_wrap.rs # Shared wrap (preview + print)
33 33 ├── datetime.rs # @date / @time formatting
@@ -27,7 +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 # Checkbox glyphs + wrap for mixed checkbox/text lines
30├── bitmap_font.rs # Checkbox glyphs (user-defined chars) + mixed-line wrap
31├── preview.rs # PreviewModel for UI canvas
31├── preview.rs # PreviewModel for UI canvas
32├── text_wrap.rs # Shared wrap (preview + print)
32├── text_wrap.rs # Shared wrap (preview + print)
33├── datetime.rs # @date / @time formatting
33├── datetime.rs # @date / @time formatting
docs/DSL-SPEC.md +2 −2
@@ -84,7 +84,7 @@ Wrapped lines use a hanging indent:
84 84 | `[]` or `[ ]` | Empty checkbox (raster) |
85 85 | `[x]` or `[X]` | Checked checkbox with tick (raster) |
86 86
87 Lines that contain checkboxes keep the printer’s normal text font; only the box/tick is an ESC `*` bit-image glyph. Preview uses Unicode ☐ / ☑.
87 Lines that contain checkboxes keep the printer’s normal text font; the box/tick is two Font A user-defined characters (ESC `&`). 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]` | ESC `*` bit-image checkbox (+ tick when checked); text stays native |
272 | `[]` / `[ ]` / `[x]` | User-defined Font A chars (ESC `&`) for checkbox; text stays native |
273 273 | `:center:` | Justification center |
274 274 | `@feed n` / blank line | ESC d n |
275 275 | `@cut` | GS V partial cut |
@@ -84,7 +84,7 @@ Wrapped lines use a hanging indent:
84| `[]` or `[ ]` | Empty checkbox (raster) |
84| `[]` or `[ ]` | Empty checkbox (raster) |
85| `[x]` or `[X]` | Checked checkbox with tick (raster) |
85| `[x]` or `[X]` | Checked checkbox with tick (raster) |
86
86
87Lines that contain checkboxes keep the printer’s normal text font; only the box/tick is an ESC `*` bit-image glyph. Preview uses Unicode ☐ / ☑.
87Lines that contain checkboxes keep the printer’s normal text font; the box/tick is two Font A user-defined characters (ESC `&`). 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]` | ESC `*` bit-image checkbox (+ tick when checked); text stays native |
272| `[]` / `[ ]` / `[x]` | User-defined Font A chars (ESC `&`) for checkbox; text stays native |
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 +1 −1
@@ -82,7 +82,7 @@ Long items wrap with a hanging indent (printed/previewed as ASCII `*`):
82 82
83 83 ### Checkboxes
84 84
85 Inline checkboxes print as a real empty box or a box with a checkmark (bit-image glyph). Surrounding text uses the normal printer font. Preview shows ☐ / ☑.
85 Inline checkboxes print as a real empty box or a box with a checkmark (user-defined printer characters). Surrounding text uses the normal printer font. Preview shows ☐ / ☑.
86 86
87 87 | Syntax | Result |
88 88 |--------|--------|
@@ -82,7 +82,7 @@ Long items wrap with a hanging indent (printed/previewed as ASCII `*`):
82
82
83### Checkboxes
83### Checkboxes
84
84
85Inline checkboxes print as a real empty box or a box with a checkmark (bit-image glyph). Surrounding text uses the normal printer font. Preview shows ☐ / ☑.
85Inline checkboxes print as a real empty box or a box with a checkmark (user-defined printer characters). Surrounding text uses the normal printer font. Preview shows ☐ / ☑.
86
86
87| Syntax | Result |
87| Syntax | Result |
88|--------|--------|
88|--------|--------|
docs/PRINTING.md +1 −1
@@ -29,7 +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 | ESC `*` 24-dot bit image (box / box+tick) | `[]` / `[ ]` / `[x]`; surrounding text uses native font |
32 | Inline checkbox | ESC `&` user-defined Font A chars (2 cells) | `[]` / `[ ]` / `[x]`; surrounding text uses native font |
33 33 | Partial cut | GS V 66 0 | `@cut` in template only |
34 34 | Barcode | GS k | Native symbologies; raster via GS v 0 |
35 35 | QR Code | GS ( k | Model 2; or raster GS v 0 |
@@ -29,7 +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 | ESC `*` 24-dot bit image (box / box+tick) | `[]` / `[ ]` / `[x]`; surrounding text uses native font |
32| Inline checkbox | ESC `&` user-defined Font A chars (2 cells) | `[]` / `[ ]` / `[x]`; surrounding text uses native font |
33| Partial cut | GS V 66 0 | `@cut` in template only |
33| Partial cut | GS V 66 0 | `@cut` in template only |
34| Barcode | GS k | Native symbologies; raster via GS v 0 |
34| Barcode | GS k | Native symbologies; raster via GS v 0 |
35| QR Code | GS ( k | Model 2; or raster GS v 0 |
35| QR Code | GS ( k | Model 2; or raster GS v 0 |
src/bitmap_font.rs +41 −5
@@ -3,17 +3,23 @@
3 3
4 4 use image::{GrayImage, Luma};
5 5
6 /// Columns reserved for one checkbox when wrapping text.
7 pub const CHECKBOX_WRAP_COLS: usize = 3;
6 /// Columns reserved for one checkbox when wrapping text (two Font A cells).
7 pub const CHECKBOX_WRAP_COLS: usize = 2;
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 /// Rendered character cell width after nearest-neighbor scale from 8×8.
11 /// Font A cell width in dots — checkbox spans two cells via user-defined chars.
12 12 pub const CHAR_WIDTH: u32 = 12;
13 13 const CHAR_SRC: u32 = 8;
14 14 /// Checkbox box size (~20% larger than the original 14px).
15 15 const CHECKBOX_SIDE: u32 = 17;
16 16
17 /// User-defined character codes (ESC &): empty L/R, checked L/R.
18 pub const CHAR_EMPTY_L: u8 = 0x01;
19 pub const CHAR_EMPTY_R: u8 = 0x02;
20 pub const CHAR_CHECKED_L: u8 = 0x03;
21 pub const CHAR_CHECKED_R: u8 = 0x04;
22
17 23 #[derive(Debug, Clone, PartialEq, Eq)]
18 24 pub enum MixedSeg {
19 25 Text(String),
@@ -291,8 +297,38 @@ pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
291 297 }
292 298
293 299 pub fn checkbox_width_dots() -> u32 {
294 // Side + padding; ~20% larger visual than the original 24-dot cell.
295 CHECKBOX_SIDE + 10
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.
305 pub 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
316 fn 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
296 332 }
297 333
298 334 fn line_width_dots(segs: &[MixedSeg]) -> u32 {
@@ -3,17 +3,23 @@
3
3
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 (two Font A cells).
7pub const CHECKBOX_WRAP_COLS: usize = 3;
7pub const CHECKBOX_WRAP_COLS: usize = 2;
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/// Font A cell width in dots — checkbox spans two cells via user-defined chars.
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).
14/// Checkbox box size (~20% larger than the original 14px).
15const CHECKBOX_SIDE: u32 = 17;
15const CHECKBOX_SIDE: u32 = 17;
16
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
17#[derive(Debug, Clone, PartialEq, Eq)]
23#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum MixedSeg {
24pub enum MixedSeg {
19 Text(String),
25 Text(String),
@@ -291,8 +297,38 @@ pub fn render_mixed_line(segs: &[MixedSeg]) -> GrayImage {
291}
297}
292
298
293pub fn checkbox_width_dots() -> u32 {
299pub fn checkbox_width_dots() -> u32 {
294 // Side + padding; ~20% larger visual than the original 24-dot cell.
300 CHAR_WIDTH * CHECKBOX_WRAP_COLS as u32
295 CHECKBOX_SIDE + 10
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
296}
332}
297
333
298fn line_width_dots(segs: &[MixedSeg]) -> u32 {
334fn line_width_dots(segs: &[MixedSeg]) -> u32 {
src/escpos.rs +53 −25
@@ -54,6 +54,33 @@ impl EscPosEncoder {
54 54 fn init(&mut self) {
55 55 self.buffer.extend_from_slice(&[0x1B, b'@']);
56 56 self.buffer.extend_from_slice(&[0x1B, b't', 0]);
57 // Checkbox glyphs as Font A user-defined characters so they stay inline
58 // with the native text font (ESC * is not buffered on many micro-printers).
59 self.define_checkbox_user_chars();
60 }
61
62 fn define_checkbox_user_chars(&mut self) {
63 let (el, er) = bitmap_font::checkbox_font_a_halves(false);
64 let (cl, cr) = bitmap_font::checkbox_font_a_halves(true);
65 self.define_font_a_char(bitmap_font::CHAR_EMPTY_L, &el);
66 self.define_font_a_char(bitmap_font::CHAR_EMPTY_R, &er);
67 self.define_font_a_char(bitmap_font::CHAR_CHECKED_L, &cl);
68 self.define_font_a_char(bitmap_font::CHAR_CHECKED_R, &cr);
69 // Select user-defined character set; undefined codes use the resident font.
70 self.buffer.extend_from_slice(&[0x1B, b'%', 1]);
71 }
72
73 /// ESC & y=3 — define one Font A (12×24) user character from column data.
74 fn define_font_a_char(&mut self, code: u8, columns: &[[u8; 3]]) {
75 let x = columns.len().min(12) as u8;
76 if x == 0 {
77 return;
78 }
79 self.buffer
80 .extend_from_slice(&[0x1B, b'&', 3, code, code, x]);
81 for col in columns.iter().take(x as usize) {
82 self.buffer.extend_from_slice(col);
83 }
57 84 }
58 85
59 86 fn encode_block(&mut self, block: &Block) -> Result<(), EscPosError> {
@@ -230,35 +257,27 @@ impl EscPosEncoder {
230 257 }
231 258
232 259 fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
233 // Native printer font for text; only the checkbox is a bit-image glyph.
234 260 for seg in segs {
235 261 match seg {
236 262 MixedSeg::Text(t) => self.buffer.extend_from_slice(t.as_bytes()),
237 MixedSeg::Checkbox { checked } => self.write_checkbox_bit_image(*checked),
263 MixedSeg::Checkbox { checked } => self.write_checkbox_chars(*checked),
238 264 }
239 265 }
240 266 self.buffer.push(b'\n');
241 267 Ok(())
242 268 }
243 269
244 /// ESC * m=33 — 24-dot double-density bit image, mixes with text in the print buffer.
245 fn write_checkbox_bit_image(&mut self, checked: bool) {
246 let img = bitmap_font::render_checkbox(checked);
247 let w = img.width();
248 let h = img.height().min(24);
249 let n_l = (w & 0xFF) as u8;
250 let n_h = ((w >> 8) & 0xFF) as u8;
251 self.buffer.extend_from_slice(&[0x1B, b'*', 33, n_l, n_h]);
252 for x in 0..w {
253 let mut bytes = [0u8; 3];
254 for y in 0..h {
255 if img.get_pixel(x, y).0[0] < 128 {
256 let byte_idx = (y / 8) as usize;
257 let bit = 7 - (y % 8);
258 bytes[byte_idx] |= 1 << bit;
259 }
260 }
261 self.buffer.extend_from_slice(&bytes);
270 fn write_checkbox_chars(&mut self, checked: bool) {
271 if checked {
272 self.buffer.extend_from_slice(&[
273 bitmap_font::CHAR_CHECKED_L,
274 bitmap_font::CHAR_CHECKED_R,
275 ]);
276 } else {
277 self.buffer.extend_from_slice(&[
278 bitmap_font::CHAR_EMPTY_L,
279 bitmap_font::CHAR_EMPTY_R,
280 ]);
262 281 }
263 282 }
264 283
@@ -479,22 +498,31 @@ mod tests {
479 498 }
480 499
481 500 #[test]
482 fn encodes_inline_checkboxes_as_bit_image() {
501 fn encodes_inline_checkboxes_as_user_chars() {
483 502 let doc = parse("[] hello [x]").unwrap();
484 503 let mut enc = EscPosEncoder::new(PosMetadata::default());
485 504 let bytes = enc.encode_document(&doc).unwrap();
486 505 assert!(
487 bytes.windows(3).any(|w| w == [0x1B, b'*', 33]),
488 "checkbox should use ESC * 24-dot bit image"
506 bytes.windows(2).any(|w| w == [0x1B, b'&']),
507 "should define user-defined checkbox characters with ESC &"
508 );
509 assert!(
510 bytes.windows(2).any(|w| w == [0x1B, b'%']),
511 "should select user-defined character set"
489 512 );
490 513 assert!(
491 514 bytes.windows(5).any(|w| w == b"hello"),
492 515 "surrounding text should stay as native font bytes"
493 516 );
494 517 assert!(
495 !bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
496 "checkbox lines must not rasterize the whole line with GS v 0"
518 !bytes.windows(3).any(|w| w == [0x1B, b'*', 33]),
519 "should not use ESC * bit images for checkboxes"
497 520 );
521 // `[] hello` → empty chars, space, then "hello"
522 let hello = bytes.windows(5).position(|w| w == b"hello").unwrap();
523 assert_eq!(bytes[hello - 1], b' ');
524 assert_eq!(bytes[hello - 2], bitmap_font::CHAR_EMPTY_R);
525 assert_eq!(bytes[hello - 3], bitmap_font::CHAR_EMPTY_L);
498 526 }
499 527
500 528 #[test]
@@ -54,6 +54,33 @@ impl EscPosEncoder {
54 fn init(&mut self) {
54 fn init(&mut self) {
55 self.buffer.extend_from_slice(&[0x1B, b'@']);
55 self.buffer.extend_from_slice(&[0x1B, b'@']);
56 self.buffer.extend_from_slice(&[0x1B, b't', 0]);
56 self.buffer.extend_from_slice(&[0x1B, b't', 0]);
57 // Checkbox glyphs as Font A user-defined characters so they stay inline
58 // with the native text font (ESC * is not buffered on many micro-printers).
59 self.define_checkbox_user_chars();
60 }
61
62 fn define_checkbox_user_chars(&mut self) {
63 let (el, er) = bitmap_font::checkbox_font_a_halves(false);
64 let (cl, cr) = bitmap_font::checkbox_font_a_halves(true);
65 self.define_font_a_char(bitmap_font::CHAR_EMPTY_L, &el);
66 self.define_font_a_char(bitmap_font::CHAR_EMPTY_R, &er);
67 self.define_font_a_char(bitmap_font::CHAR_CHECKED_L, &cl);
68 self.define_font_a_char(bitmap_font::CHAR_CHECKED_R, &cr);
69 // Select user-defined character set; undefined codes use the resident font.
70 self.buffer.extend_from_slice(&[0x1B, b'%', 1]);
71 }
72
73 /// ESC & y=3 — define one Font A (12×24) user character from column data.
74 fn define_font_a_char(&mut self, code: u8, columns: &[[u8; 3]]) {
75 let x = columns.len().min(12) as u8;
76 if x == 0 {
77 return;
78 }
79 self.buffer
80 .extend_from_slice(&[0x1B, b'&', 3, code, code, x]);
81 for col in columns.iter().take(x as usize) {
82 self.buffer.extend_from_slice(col);
83 }
57 }
84 }
58
85
59 fn encode_block(&mut self, block: &Block) -> Result<(), EscPosError> {
86 fn encode_block(&mut self, block: &Block) -> Result<(), EscPosError> {
@@ -230,35 +257,27 @@ impl EscPosEncoder {
230 }
257 }
231
258
232 fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
259 fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
233 // Native printer font for text; only the checkbox is a bit-image glyph.
234 for seg in segs {
260 for seg in segs {
235 match seg {
261 match seg {
236 MixedSeg::Text(t) => self.buffer.extend_from_slice(t.as_bytes()),
262 MixedSeg::Text(t) => self.buffer.extend_from_slice(t.as_bytes()),
237 MixedSeg::Checkbox { checked } => self.write_checkbox_bit_image(*checked),
263 MixedSeg::Checkbox { checked } => self.write_checkbox_chars(*checked),
238 }
264 }
239 }
265 }
240 self.buffer.push(b'\n');
266 self.buffer.push(b'\n');
241 Ok(())
267 Ok(())
242 }
268 }
243
269
244 /// ESC * m=33 — 24-dot double-density bit image, mixes with text in the print buffer.
270 fn write_checkbox_chars(&mut self, checked: bool) {
245 fn write_checkbox_bit_image(&mut self, checked: bool) {
271 if checked {
246 let img = bitmap_font::render_checkbox(checked);
272 self.buffer.extend_from_slice(&[
247 let w = img.width();
273 bitmap_font::CHAR_CHECKED_L,
248 let h = img.height().min(24);
274 bitmap_font::CHAR_CHECKED_R,
249 let n_l = (w & 0xFF) as u8;
275 ]);
250 let n_h = ((w >> 8) & 0xFF) as u8;
276 } else {
251 self.buffer.extend_from_slice(&[0x1B, b'*', 33, n_l, n_h]);
277 self.buffer.extend_from_slice(&[
252 for x in 0..w {
278 bitmap_font::CHAR_EMPTY_L,
253 let mut bytes = [0u8; 3];
279 bitmap_font::CHAR_EMPTY_R,
254 for y in 0..h {
280 ]);
255 if img.get_pixel(x, y).0[0] < 128 {
256 let byte_idx = (y / 8) as usize;
257 let bit = 7 - (y % 8);
258 bytes[byte_idx] |= 1 << bit;
259 }
260 }
261 self.buffer.extend_from_slice(&bytes);
262 }
281 }
263 }
282 }
264
283
@@ -479,22 +498,31 @@ mod tests {
479 }
498 }
480
499
481 #[test]
500 #[test]
482 fn encodes_inline_checkboxes_as_bit_image() {
501 fn encodes_inline_checkboxes_as_user_chars() {
483 let doc = parse("[] hello [x]").unwrap();
502 let doc = parse("[] hello [x]").unwrap();
484 let mut enc = EscPosEncoder::new(PosMetadata::default());
503 let mut enc = EscPosEncoder::new(PosMetadata::default());
485 let bytes = enc.encode_document(&doc).unwrap();
504 let bytes = enc.encode_document(&doc).unwrap();
486 assert!(
505 assert!(
487 bytes.windows(3).any(|w| w == [0x1B, b'*', 33]),
506 bytes.windows(2).any(|w| w == [0x1B, b'&']),
488 "checkbox should use ESC * 24-dot bit image"
507 "should define user-defined checkbox characters with ESC &"
508 );
509 assert!(
510 bytes.windows(2).any(|w| w == [0x1B, b'%']),
511 "should select user-defined character set"
489 );
512 );
490 assert!(
513 assert!(
491 bytes.windows(5).any(|w| w == b"hello"),
514 bytes.windows(5).any(|w| w == b"hello"),
492 "surrounding text should stay as native font bytes"
515 "surrounding text should stay as native font bytes"
493 );
516 );
494 assert!(
517 assert!(
495 !bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
518 !bytes.windows(3).any(|w| w == [0x1B, b'*', 33]),
496 "checkbox lines must not rasterize the whole line with GS v 0"
519 "should not use ESC * bit images for checkboxes"
497 );
520 );
521 // `[] hello` → empty chars, space, then "hello"
522 let hello = bytes.windows(5).position(|w| w == b"hello").unwrap();
523 assert_eq!(bytes[hello - 1], b' ');
524 assert_eq!(bytes[hello - 2], bitmap_font::CHAR_EMPTY_R);
525 assert_eq!(bytes[hello - 3], bitmap_font::CHAR_EMPTY_L);
498 }
526 }
499
527
500 #[test]
528 #[test]