BlueGit

timfa posprint

Fix GS v 0 raster header to match print-bridge format.

Use 1D 76 30 00 instead of 1D 76 00 00 so printers interpret bitmap data
as raster rather than printing raw bytes as text.

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

timfa Tim Falken · pushed by timfa

Authored 2026-07-15 · 4 months ago

Hash
c073ddac7d6e140bb1b05160ddcbb59d55719fac
Parent
7b9b44e0
3 files changed +30 −5
docs/PRINTING.md +1 −1
@@ -149,7 +149,7 @@ Status polling (paper out, cover open) is **not** implemented.
149 149 2. Decode with `image` crate.
150 150 3. Convert to luma8, resize to target width (%, dots, or full printable width).
151 151 4. Threshold to 1-bit raster.
152 5. Emit GS v 0.
152 5. Emit `GS v 0` raster (`1D 76 30 00`, width/height LE, bitmap) — same format as print-bridge.
153 153
154 154 Preview shows `[image: path]` only — no bitmap preview yet.
155 155
@@ -149,7 +149,7 @@ Status polling (paper out, cover open) is **not** implemented.
1492. Decode with `image` crate.
1492. Decode with `image` crate.
1503. Convert to luma8, resize to target width (%, dots, or full printable width).
1503. Convert to luma8, resize to target width (%, dots, or full printable width).
1514. Threshold to 1-bit raster.
1514. Threshold to 1-bit raster.
1525. Emit GS v 0.
1525. Emit `GS v 0` raster (`1D 76 30 00`, width/height LE, bitmap) — same format as print-bridge.
153
153
154Preview shows `[image: path]` only — no bitmap preview yet.
154Preview shows `[image: path]` only — no bitmap preview yet.
155
155
src/escpos.rs +28 −3
@@ -311,9 +311,11 @@ impl EscPosEncoder {
311 311 let x_h = ((bytes_per_row >> 8) & 0xFF) as u8;
312 312 let y_l = (new_h & 0xFF) as u8;
313 313 let y_h = ((new_h >> 8) & 0xFF) as u8;
314 self.buffer.extend_from_slice(&[0x1D, b'v', 0, 0, x_l, x_h, y_l, y_h]);
314 // GS v 0 normal raster — same header as print-bridge / node-escpos:
315 // 1D 76 30 00, xL xH (bytes/row), yL yH (dots), then bitmap bytes.
316 self.buffer
317 .extend_from_slice(&[0x1D, b'v', 0x30, 0x00, x_l, x_h, y_l, y_h]);
315 318 self.buffer.extend_from_slice(&raster);
316 self.buffer.push(b'\n');
317 319 Ok(())
318 320 }
319 321 }
@@ -379,7 +381,30 @@ mod tests {
379 381 let doc = parse("@barcode RASTER CODE128 TEST").unwrap();
380 382 let mut enc = EscPosEncoder::new(PosMetadata::default());
381 383 let bytes = enc.encode_document(&doc).unwrap();
382 assert!(bytes.windows(4).any(|w| w == [0x1D, b'v', 0, 0]));
384 assert!(
385 bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
386 "expected GS v 0 raster header"
387 );
388 }
389
390 #[test]
391 fn raster_header_matches_print_bridge_format() {
392 let doc = parse("@barcode RASTER CODE128 X").unwrap();
393 let mut enc = EscPosEncoder::new(PosMetadata {
394 page_width_mm: 58.0,
395 ..Default::default()
396 });
397 let bytes = enc.encode_document(&doc).unwrap();
398 let pos = bytes
399 .windows(4)
400 .position(|w| w == [0x1D, b'v', 0x30, 0x00])
401 .expect("raster header");
402 assert_eq!(&bytes[pos..pos + 4], &[0x1D, b'v', 0x30, 0x00]);
403 // x = bytes per row (u16 LE), y = height in dots (u16 LE)
404 let x_bytes = u16::from_le_bytes([bytes[pos + 4], bytes[pos + 5]]);
405 assert!(x_bytes > 0);
406 let y_dots = u16::from_le_bytes([bytes[pos + 6], bytes[pos + 7]]);
407 assert!(y_dots > 0);
383 408 }
384 409
385 410 #[test]
@@ -311,9 +311,11 @@ impl EscPosEncoder {
311 let x_h = ((bytes_per_row >> 8) & 0xFF) as u8;
311 let x_h = ((bytes_per_row >> 8) & 0xFF) as u8;
312 let y_l = (new_h & 0xFF) as u8;
312 let y_l = (new_h & 0xFF) as u8;
313 let y_h = ((new_h >> 8) & 0xFF) as u8;
313 let y_h = ((new_h >> 8) & 0xFF) as u8;
314 self.buffer.extend_from_slice(&[0x1D, b'v', 0, 0, x_l, x_h, y_l, y_h]);
314 // GS v 0 normal raster — same header as print-bridge / node-escpos:
315 // 1D 76 30 00, xL xH (bytes/row), yL yH (dots), then bitmap bytes.
316 self.buffer
317 .extend_from_slice(&[0x1D, b'v', 0x30, 0x00, x_l, x_h, y_l, y_h]);
315 self.buffer.extend_from_slice(&raster);
318 self.buffer.extend_from_slice(&raster);
316 self.buffer.push(b'\n');
317 Ok(())
319 Ok(())
318 }
320 }
319}
321}
@@ -379,7 +381,30 @@ mod tests {
379 let doc = parse("@barcode RASTER CODE128 TEST").unwrap();
381 let doc = parse("@barcode RASTER CODE128 TEST").unwrap();
380 let mut enc = EscPosEncoder::new(PosMetadata::default());
382 let mut enc = EscPosEncoder::new(PosMetadata::default());
381 let bytes = enc.encode_document(&doc).unwrap();
383 let bytes = enc.encode_document(&doc).unwrap();
382 assert!(bytes.windows(4).any(|w| w == [0x1D, b'v', 0, 0]));
384 assert!(
385 bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]),
386 "expected GS v 0 raster header"
387 );
388 }
389
390 #[test]
391 fn raster_header_matches_print_bridge_format() {
392 let doc = parse("@barcode RASTER CODE128 X").unwrap();
393 let mut enc = EscPosEncoder::new(PosMetadata {
394 page_width_mm: 58.0,
395 ..Default::default()
396 });
397 let bytes = enc.encode_document(&doc).unwrap();
398 let pos = bytes
399 .windows(4)
400 .position(|w| w == [0x1D, b'v', 0x30, 0x00])
401 .expect("raster header");
402 assert_eq!(&bytes[pos..pos + 4], &[0x1D, b'v', 0x30, 0x00]);
403 // x = bytes per row (u16 LE), y = height in dots (u16 LE)
404 let x_bytes = u16::from_le_bytes([bytes[pos + 4], bytes[pos + 5]]);
405 assert!(x_bytes > 0);
406 let y_dots = u16::from_le_bytes([bytes[pos + 6], bytes[pos + 7]]);
407 assert!(y_dots > 0);
383 }
408 }
384
409
385 #[test]
410 #[test]
src/pipeline.rs +1 −1
@@ -96,7 +96,7 @@ mod tests {
96 96 let mut params = HashMap::new();
97 97 params.insert("Data".into(), "Testcode".into());
98 98 let bytes = encode_pos_file(&file, None, &params).unwrap();
99 assert!(bytes.windows(4).any(|w| w == [0x1D, b'v', 0, 0]));
99 assert!(bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]));
100 100 }
101 101
102 102 #[test]
@@ -96,7 +96,7 @@ mod tests {
96 let mut params = HashMap::new();
96 let mut params = HashMap::new();
97 params.insert("Data".into(), "Testcode".into());
97 params.insert("Data".into(), "Testcode".into());
98 let bytes = encode_pos_file(&file, None, &params).unwrap();
98 let bytes = encode_pos_file(&file, None, &params).unwrap();
99 assert!(bytes.windows(4).any(|w| w == [0x1D, b'v', 0, 0]));
99 assert!(bytes.windows(4).any(|w| w == [0x1D, b'v', 0x30, 0x00]));
100 }
100 }
101
101
102 #[test]
102 #[test]