Keep native printer font around checkbox bit-images.
Only the checkbox uses ESC *; surrounding text is no longer rasterized with the bitmap font. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -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 # Raster text + checkbox glyphs for mixed lines
30
├── bitmap_font.rs # Checkbox glyphs + wrap for mixed checkbox/text lines
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 AST27
├── dsl/ # Parser and AST28
├── params.rs # Parameter collect / substitute / preview samples28
├── params.rs # Parameter collect / substitute / preview samples29
├── 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 lines30
├── bitmap_font.rs # Checkbox glyphs + wrap for mixed checkbox/text lines31
├── preview.rs # PreviewModel for UI canvas31
├── preview.rs # PreviewModel for UI canvas32
├── text_wrap.rs # Shared wrap (preview + print)32
├── text_wrap.rs # Shared wrap (preview + print)33
├── datetime.rs # @date / @time formatting33
├── datetime.rs # @date / @time formatting@@ -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 are encoded as GS v 0 bitmaps so the box/tick is reliable on CP437 printers. Preview uses Unicode ☐ / ☑.
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 ☐ / ☑.
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]` | GS v 0 raster checkbox (+ tick when checked) |
272
| `[]` / `[ ]` / `[x]` | ESC `*` bit-image checkbox (+ tick when checked); 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
87
Lines that contain checkboxes are encoded as GS v 0 bitmaps so the box/tick is reliable on CP437 printers. Preview uses Unicode ☐ / ☑.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 ☐ / ☑.88
88
89
Parameters work inside any text, and inside barcode/QR/image directives (below).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
| `# 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]` | GS v 0 raster checkbox (+ tick when checked) |272
| `[]` / `[ ]` / `[x]` | ESC `*` bit-image checkbox (+ tick when checked); 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 |@@ -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 (raster). Preview shows ☐ / ☑.
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 ☐ / ☑.
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
### Checkboxes83
### Checkboxes84
84
85
Inline checkboxes print as a real empty box or a box with a checkmark (raster). Preview shows ☐ / ☑.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 ☐ / ☑.86
86
87
| Syntax | Result |87
| Syntax | Result |88
|--------|--------|88
|--------|--------|@@ -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 | GS v 0 raster (box / box+tick) | `[]` / `[ ]` / `[x]` lines |
32
| Inline checkbox | ESC `*` 24-dot bit image (box / box+tick) | `[]` / `[ ]` / `[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 | GS v 0 raster (box / box+tick) | `[]` / `[ ]` / `[x]` lines |32
| Inline checkbox | ESC `*` 24-dot bit image (box / box+tick) | `[]` / `[ ]` / `[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 |@@ -98,8 +98,17 @@ impl EscPosEncoder {
98
98
};
99
99
let cols = text_wrap::wrap_cols(&self.metadata, wrap_style);
100
100
if bitmap_font::inlines_have_checkbox(inlines) {
101
// Raster path already draws glyphs; skip size/bold toggles.
101
if *level == 1 {
102
self.buffer.extend_from_slice(&[0x1D, b'!', 0x11]);
103
} else {
104
self.buffer.extend_from_slice(&[0x1B, b'E', 1]);
105
}
102
106
self.encode_mixed_inlines(inlines, cols, "", "")?;
107
if *level == 1 {
108
self.buffer.extend_from_slice(&[0x1D, b'!', 0x00]);
109
} else {
110
self.buffer.extend_from_slice(&[0x1B, b'E', 0]);
111
}
103
112
} else {
104
113
let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
105
114
if *level == 1 {
@@ -221,9 +230,36 @@ impl EscPosEncoder {
221
230
}
222
231
223
232
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))
233
// Native printer font for text; only the checkbox is a bit-image glyph.
234
for seg in segs {
235
match seg {
236
MixedSeg::Text(t) => self.buffer.extend_from_slice(t.as_bytes()),
237
MixedSeg::Checkbox { checked } => self.write_checkbox_bit_image(*checked),
238
}
239
}
240
self.buffer.push(b'\n');
241
Ok(())
242
}
243
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);
262
}
227
263
}
228
264
229
265
fn feed(&mut self, lines: u8) {
@@ -443,19 +479,21 @@ mod tests {
443
479
}
444
480
445
481
#[test]
446
fn encodes_inline_checkboxes_as_raster() {
447
let doc = parse("[] [ ] [x]").unwrap();
482
fn encodes_inline_checkboxes_as_bit_image() {
483
let doc = parse("[] hello [x]").unwrap();
448
484
let mut enc = EscPosEncoder::new(PosMetadata::default());
449
485
let bytes = enc.encode_document(&doc).unwrap();
450
486
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()
487
bytes.windows(3).any(|w| w == [0x1B, b'*', 33]),
488
"checkbox should use ESC * 24-dot bit image"
489
);
490
assert!(
491
bytes.windows(5).any(|w| w == b"hello"),
492
"surrounding text should stay as native font bytes"
454
493
);
455
let text = String::from_utf8_lossy(bytes);
456
494
assert!(
457
!text.contains("[ ]") && !text.contains("[x]"),
458
"should not fall back to ASCII checkbox glyphs"
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"
459
497
);
460
498
}
461
499
@@ -98,8 +98,17 @@ impl EscPosEncoder {98
};
98
};
99
let cols = text_wrap::wrap_cols(&self.metadata, wrap_style);
99
let cols = text_wrap::wrap_cols(&self.metadata, wrap_style);
100
if bitmap_font::inlines_have_checkbox(inlines) {
100
if bitmap_font::inlines_have_checkbox(inlines) {
101
// Raster path already draws glyphs; skip size/bold toggles.
101
if *level == 1 {
102
self.buffer.extend_from_slice(&[0x1D, b'!', 0x11]);
103
} else {
104
self.buffer.extend_from_slice(&[0x1B, b'E', 1]);
105
}
102
self.encode_mixed_inlines(inlines, cols, "", "")?;
106
self.encode_mixed_inlines(inlines, cols, "", "")?;
107
if *level == 1 {
108
self.buffer.extend_from_slice(&[0x1D, b'!', 0x00]);
109
} else {
110
self.buffer.extend_from_slice(&[0x1B, b'E', 0]);
111
}
103
} else {
112
} else {
104
let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
113
let wrapped = text_wrap::wrap_text(&inlines_to_plain(inlines), cols);
105
if *level == 1 {
114
if *level == 1 {
@@ -221,9 +230,36 @@ impl EscPosEncoder {221
}
230
}
222
231
223
fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
232
fn encode_mixed_line(&mut self, segs: &[MixedSeg]) -> Result<(), EscPosError> {
224
let img = bitmap_font::render_mixed_line(segs);
233
// Native printer font for text; only the checkbox is a bit-image glyph.
225
let w = img.width().max(1) as u16;
234
for seg in segs {
226
self.encode_gray_raster(&img, ImageWidth::Dots(w))
235
match seg {
236
MixedSeg::Text(t) => self.buffer.extend_from_slice(t.as_bytes()),
237
MixedSeg::Checkbox { checked } => self.write_checkbox_bit_image(*checked),
238
}
239
}
240
self.buffer.push(b'\n');
241
Ok(())
242
}
243
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);
262
}
227
}
263
}
228
264
229
fn feed(&mut self, lines: u8) {
265
fn feed(&mut self, lines: u8) {
@@ -443,19 +479,21 @@ mod tests {443
}
479
}
444
480
445
#[test]
481
#[test]
446
fn encodes_inline_checkboxes_as_raster() {
482
fn encodes_inline_checkboxes_as_bit_image() {
447
let doc = parse("[] [ ] [x]").unwrap();
483
let doc = parse("[] hello [x]").unwrap();
448
let mut enc = EscPosEncoder::new(PosMetadata::default());
484
let mut enc = EscPosEncoder::new(PosMetadata::default());
449
let bytes = enc.encode_document(&doc).unwrap();
485
let bytes = enc.encode_document(&doc).unwrap();
450
assert!(
486
assert!(
451
bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
487
bytes.windows(3).any(|w| w == [0x1B, b'*', 33]),
452
"checkbox lines should be GS v 0 rasters, got {} bytes",
488
"checkbox should use ESC * 24-dot bit image"
453
bytes.len()
489
);
490
assert!(
491
bytes.windows(5).any(|w| w == b"hello"),
492
"surrounding text should stay as native font bytes"
454
);
493
);
455
let text = String::from_utf8_lossy(bytes);
456
assert!(
494
assert!(
457
!text.contains("[ ]") && !text.contains("[x]"),
495
!bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
458
"should not fall back to ASCII checkbox glyphs"
496
"checkbox lines must not rasterize the whole line with GS v 0"
459
);
497
);
460
}
498
}
461
499