Align preview to printer dots and fix blank-line feeds on print.
Preview uses one pixel per dot with column-based fonts; spacers match line spacing. Feed uses LF like print-bridge so blank lines work after rasters. Warn when the document does not end with @cut. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -31,7 +31,12 @@ Built-in reference for the template editor and POS markup language.
31
31
32
32
| Setting | Description |
33
33
|---------|-------------|
34
| **Page width (mm)** | Paper width, typically `58` or `80` |
34
| **Page width (mm)** | Paper width — set this to match your physical roll (e.g. `53`, `58`, `80`) |
35
| **Preview zoom** | Screen zoom only; layout uses printer dots at 100% |
36
37
Preview width matches printable dots (203 DPI). Blank lines in the editor feed paper the same as `@feed 1`.
38
39
If the document does not end with `@cut` (ignoring blank lines after it), a red warning appears next to **Help**.
35
40
36
41
---
37
42
@@ -31,7 +31,12 @@ Built-in reference for the template editor and POS markup language.31
31
32
| Setting | Description |32
| Setting | Description |33
|---------|-------------|33
|---------|-------------|34
| **Page width (mm)** | Paper width, typically `58` or `80` |34
| **Page width (mm)** | Paper width — set this to match your physical roll (e.g. `53`, `58`, `80`) |35
| **Preview zoom** | Screen zoom only; layout uses printer dots at 100% |36
37
Preview width matches printable dots (203 DPI). Blank lines in the editor feed paper the same as `@feed 1`.38
39
If the document does not end with `@cut` (ignoring blank lines after it), a red warning appears next to **Help**.35
40
36
---41
---37
42
@@ -11,7 +11,8 @@ use posprint::editor_assist::{
11
11
use posprint::help_view;
12
12
use posprint::io::{PosFile, PosMetadata};
13
13
use posprint::preview::{
14
preview_content_width_px, preview_zoom_scale, render_preview, PreviewLine, TextStyle,
14
preview_char_width_px, preview_content_width_px, preview_line_height_estimate,
15
preview_line_spacing_px, preview_zoom_scale, render_preview, PreviewLine, TextStyle,
15
16
};
16
17
use posprint::undo::TextUndo;
17
18
use posprint::HELP_MARKDOWN;
@@ -337,6 +338,14 @@ impl eframe::App for PosPrintApp {
337
338
ui.horizontal(|ui| {
338
339
ui.selectable_value(&mut self.view_mode, ViewMode::Design, "Design");
339
340
ui.selectable_value(&mut self.view_mode, ViewMode::Help, "Help");
341
if let Ok(doc) = parse(&self.body) {
342
if !dsl::document_ends_with_cut(&doc) {
343
ui.colored_label(
344
egui::Color32::RED,
345
"Warning: No @cut directive at end of file.",
346
);
347
}
348
}
340
349
});
341
350
});
342
351
@@ -507,6 +516,8 @@ impl PosPrintApp {
507
516
);
508
517
let visual_scale = preview_zoom_scale(self.preview_zoom);
509
518
let content_width = preview_content_width_px(&self.metadata) * visual_scale;
519
let char_width = preview_char_width_px(&self.metadata, self.preview_zoom);
520
let line_spacing = preview_line_spacing_px(self.preview_zoom);
510
521
let padding = 8.0 * visual_scale;
511
522
512
523
egui::ScrollArea::both()
@@ -515,7 +526,9 @@ impl PosPrintApp {
515
526
egui::Frame::canvas(ui.style()).show(ui, |ui| {
516
527
let mut total_height = padding * 2.0;
517
528
for line in &model.lines {
518
total_height += line_height(line, visual_scale) + 2.0;
529
total_height +=
530
preview_line_height_estimate(line, &self.metadata, self.preview_zoom)
531
+ 2.0;
519
532
}
520
533
total_height = total_height.max(100.0);
521
534
@@ -539,7 +552,7 @@ impl PosPrintApp {
539
552
align,
540
553
style,
541
554
} => {
542
let font_id = text_font(*style, visual_scale);
555
let font_id = preview_text_font(*style, char_width);
543
556
let galley = painter.layout_no_wrap(
544
557
content.clone(),
545
558
font_id,
@@ -548,7 +561,7 @@ impl PosPrintApp {
548
561
let x = align_x(*align, rect.min.x + padding, inner_width, galley.size().x);
549
562
let line_h = galley.size().y;
550
563
painter.galley(egui::pos2(x, y), galley, egui::Color32::BLACK);
551
y += line_h + 4.0 * visual_scale;
564
y += line_h + 2.0 * visual_scale;
552
565
}
553
566
PreviewLine::Rule => {
554
567
painter.line_segment(
@@ -577,7 +590,7 @@ impl PosPrintApp {
577
590
egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
578
591
egui::Color32::WHITE,
579
592
);
580
y += size.y + 4.0 * visual_scale;
593
y += size.y + 2.0 * visual_scale;
581
594
}
582
595
PreviewLine::Error { message, align } => {
583
596
let galley = painter.layout(
@@ -592,7 +605,7 @@ impl PosPrintApp {
592
605
y += line_h + 4.0 * visual_scale;
593
606
}
594
607
PreviewLine::Spacer(n) => {
595
y += 18.0 * visual_scale * (*n as f32);
608
y += line_spacing * (*n as f32);
596
609
}
597
610
}
598
611
}
@@ -601,14 +614,15 @@ impl PosPrintApp {
601
614
}
602
615
}
603
616
604
fn text_font(style: TextStyle, scale: f32) -> egui::FontId {
617
fn preview_text_font(style: TextStyle, char_width: f32) -> egui::FontId {
618
let base = char_width * 0.9;
605
619
match style {
606
TextStyle::Heading1 => egui::FontId::proportional(22.0 * scale),
607
TextStyle::Heading2 => egui::FontId::proportional(18.0 * scale),
608
TextStyle::Bold => egui::FontId::proportional(16.0 * scale),
609
TextStyle::Italic => egui::FontId::proportional(15.0 * scale),
610
TextStyle::Code => egui::FontId::monospace(14.0 * scale),
611
TextStyle::Normal => egui::FontId::monospace(14.0 * scale),
620
TextStyle::Heading1 => egui::FontId::monospace(base * 2.0),
621
TextStyle::Heading2 => egui::FontId::monospace(base * 1.5),
622
TextStyle::Bold => egui::FontId::monospace(base * 1.05),
623
TextStyle::Italic => egui::FontId::monospace(base),
624
TextStyle::Code => egui::FontId::monospace(base),
625
TextStyle::Normal => egui::FontId::monospace(base),
612
626
}
613
627
}
614
628
@@ -620,20 +634,6 @@ fn align_x(align: dsl::Align, left: f32, inner_width: f32, item_width: f32) -> f
620
634
}
621
635
}
622
636
623
fn line_height(line: &PreviewLine, scale: f32) -> f32 {
624
match line {
625
PreviewLine::Text { style, .. } => match style {
626
TextStyle::Heading1 => 28.0 * scale,
627
TextStyle::Heading2 => 24.0 * scale,
628
_ => 18.0 * scale,
629
},
630
PreviewLine::Rule => 12.0 * scale,
631
PreviewLine::Graphic { image, .. } => image.height() as f32 * scale + 4.0,
632
PreviewLine::Error { .. } => 18.0 * scale,
633
PreviewLine::Spacer(n) => 18.0 * scale * (*n as f32),
634
}
635
}
636
637
637
fn gray_to_texture(
638
638
ctx: &egui::Context,
639
639
id: &str,
@@ -11,7 +11,8 @@ use posprint::editor_assist::{11
use posprint::help_view;
11
use posprint::help_view;
12
use posprint::io::{PosFile, PosMetadata};
12
use posprint::io::{PosFile, PosMetadata};
13
use posprint::preview::{
13
use posprint::preview::{
14
preview_content_width_px, preview_zoom_scale, render_preview, PreviewLine, TextStyle,
14
preview_char_width_px, preview_content_width_px, preview_line_height_estimate,
15
preview_line_spacing_px, preview_zoom_scale, render_preview, PreviewLine, TextStyle,
15
};
16
};
16
use posprint::undo::TextUndo;
17
use posprint::undo::TextUndo;
17
use posprint::HELP_MARKDOWN;
18
use posprint::HELP_MARKDOWN;
@@ -337,6 +338,14 @@ impl eframe::App for PosPrintApp {337
ui.horizontal(|ui| {
338
ui.horizontal(|ui| {
338
ui.selectable_value(&mut self.view_mode, ViewMode::Design, "Design");
339
ui.selectable_value(&mut self.view_mode, ViewMode::Design, "Design");
339
ui.selectable_value(&mut self.view_mode, ViewMode::Help, "Help");
340
ui.selectable_value(&mut self.view_mode, ViewMode::Help, "Help");
341
if let Ok(doc) = parse(&self.body) {
342
if !dsl::document_ends_with_cut(&doc) {
343
ui.colored_label(
344
egui::Color32::RED,
345
"Warning: No @cut directive at end of file.",
346
);
347
}
348
}
340
});
349
});
341
});
350
});
342
351
@@ -507,6 +516,8 @@ impl PosPrintApp {507
);
516
);
508
let visual_scale = preview_zoom_scale(self.preview_zoom);
517
let visual_scale = preview_zoom_scale(self.preview_zoom);
509
let content_width = preview_content_width_px(&self.metadata) * visual_scale;
518
let content_width = preview_content_width_px(&self.metadata) * visual_scale;
519
let char_width = preview_char_width_px(&self.metadata, self.preview_zoom);
520
let line_spacing = preview_line_spacing_px(self.preview_zoom);
510
let padding = 8.0 * visual_scale;
521
let padding = 8.0 * visual_scale;
511
522
512
egui::ScrollArea::both()
523
egui::ScrollArea::both()
@@ -515,7 +526,9 @@ impl PosPrintApp {515
egui::Frame::canvas(ui.style()).show(ui, |ui| {
526
egui::Frame::canvas(ui.style()).show(ui, |ui| {
516
let mut total_height = padding * 2.0;
527
let mut total_height = padding * 2.0;
517
for line in &model.lines {
528
for line in &model.lines {
518
total_height += line_height(line, visual_scale) + 2.0;
529
total_height +=
530
preview_line_height_estimate(line, &self.metadata, self.preview_zoom)
531
+ 2.0;
519
}
532
}
520
total_height = total_height.max(100.0);
533
total_height = total_height.max(100.0);
521
534
@@ -539,7 +552,7 @@ impl PosPrintApp {539
align,
552
align,
540
style,
553
style,
541
} => {
554
} => {
542
let font_id = text_font(*style, visual_scale);
555
let font_id = preview_text_font(*style, char_width);
543
let galley = painter.layout_no_wrap(
556
let galley = painter.layout_no_wrap(
544
content.clone(),
557
content.clone(),
545
font_id,
558
font_id,
@@ -548,7 +561,7 @@ impl PosPrintApp {548
let x = align_x(*align, rect.min.x + padding, inner_width, galley.size().x);
561
let x = align_x(*align, rect.min.x + padding, inner_width, galley.size().x);
549
let line_h = galley.size().y;
562
let line_h = galley.size().y;
550
painter.galley(egui::pos2(x, y), galley, egui::Color32::BLACK);
563
painter.galley(egui::pos2(x, y), galley, egui::Color32::BLACK);
551
y += line_h + 4.0 * visual_scale;
564
y += line_h + 2.0 * visual_scale;
552
}
565
}
553
PreviewLine::Rule => {
566
PreviewLine::Rule => {
554
painter.line_segment(
567
painter.line_segment(
@@ -577,7 +590,7 @@ impl PosPrintApp {577
egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
590
egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
578
egui::Color32::WHITE,
591
egui::Color32::WHITE,
579
);
592
);
580
y += size.y + 4.0 * visual_scale;
593
y += size.y + 2.0 * visual_scale;
581
}
594
}
582
PreviewLine::Error { message, align } => {
595
PreviewLine::Error { message, align } => {
583
let galley = painter.layout(
596
let galley = painter.layout(
@@ -592,7 +605,7 @@ impl PosPrintApp {592
y += line_h + 4.0 * visual_scale;
605
y += line_h + 4.0 * visual_scale;
593
}
606
}
594
PreviewLine::Spacer(n) => {
607
PreviewLine::Spacer(n) => {
595
y += 18.0 * visual_scale * (*n as f32);
608
y += line_spacing * (*n as f32);
596
}
609
}
597
}
610
}
598
}
611
}
@@ -601,14 +614,15 @@ impl PosPrintApp {601
}
614
}
602
}
615
}
603
616
604
fn text_font(style: TextStyle, scale: f32) -> egui::FontId {
617
fn preview_text_font(style: TextStyle, char_width: f32) -> egui::FontId {
618
let base = char_width * 0.9;
605
match style {
619
match style {
606
TextStyle::Heading1 => egui::FontId::proportional(22.0 * scale),
620
TextStyle::Heading1 => egui::FontId::monospace(base * 2.0),
607
TextStyle::Heading2 => egui::FontId::proportional(18.0 * scale),
621
TextStyle::Heading2 => egui::FontId::monospace(base * 1.5),
608
TextStyle::Bold => egui::FontId::proportional(16.0 * scale),
622
TextStyle::Bold => egui::FontId::monospace(base * 1.05),
609
TextStyle::Italic => egui::FontId::proportional(15.0 * scale),
623
TextStyle::Italic => egui::FontId::monospace(base),
610
TextStyle::Code => egui::FontId::monospace(14.0 * scale),
624
TextStyle::Code => egui::FontId::monospace(base),
611
TextStyle::Normal => egui::FontId::monospace(14.0 * scale),
625
TextStyle::Normal => egui::FontId::monospace(base),
612
}
626
}
613
}
627
}
614
628
@@ -620,20 +634,6 @@ fn align_x(align: dsl::Align, left: f32, inner_width: f32, item_width: f32) -> f620
}
634
}
621
}
635
}
622
636
623
fn line_height(line: &PreviewLine, scale: f32) -> f32 {
624
match line {
625
PreviewLine::Text { style, .. } => match style {
626
TextStyle::Heading1 => 28.0 * scale,
627
TextStyle::Heading2 => 24.0 * scale,
628
_ => 18.0 * scale,
629
},
630
PreviewLine::Rule => 12.0 * scale,
631
PreviewLine::Graphic { image, .. } => image.height() as f32 * scale + 4.0,
632
PreviewLine::Error { .. } => 18.0 * scale,
633
PreviewLine::Spacer(n) => 18.0 * scale * (*n as f32),
634
}
635
}
636
637
fn gray_to_texture(
637
fn gray_to_texture(
638
ctx: &egui::Context,
638
ctx: &egui::Context,
639
id: &str,
639
id: &str,
@@ -200,4 +200,4 @@ impl QrEcc {
200
200
201
201
mod parser;
202
202
203
pub use parser::parse;
203
pub use parser::{document_ends_with_cut, parse};
@@ -200,4 +200,4 @@ impl QrEcc {200
200
201
mod parser;
201
mod parser;
202
202
203
pub use parser::parse;
203
pub use parser::{document_ends_with_cut, parse};
@@ -92,6 +92,18 @@ pub fn parse(source: &str) -> Result<Document, ParseError> {
92
92
Ok(Document { blocks })
93
93
}
94
94
95
/// True when the last substantive block is `@cut` (trailing blank-line feeds are ignored).
96
pub fn document_ends_with_cut(document: &Document) -> bool {
97
for block in document.blocks.iter().rev() {
98
match block {
99
Block::Feed(_) => continue,
100
Block::Cut => return true,
101
_ => return false,
102
}
103
}
104
false
105
}
106
95
107
fn strip_comments(source: &str) -> String {
96
108
let mut out = String::with_capacity(source.len());
97
109
let mut chars = source.chars().peekable();
@@ -802,6 +814,24 @@ mod tests {
802
814
assert!(matches!(doc.blocks[1], Block::Feed(1)));
803
815
}
804
816
817
#[test]
818
fn document_ends_with_cut_ignores_trailing_blank_lines() {
819
let doc = parse("Line\n@cut\n\n").unwrap();
820
assert!(document_ends_with_cut(&doc));
821
}
822
823
#[test]
824
fn document_without_cut_at_end() {
825
let doc = parse("Line\n@feed 1").unwrap();
826
assert!(!document_ends_with_cut(&doc));
827
}
828
829
#[test]
830
fn blank_line_between_barcode_and_qr_is_feed() {
831
let doc = parse("@barcode RASTER CODE128 X\n\n:center:@qr RASTER Y scale=4").unwrap();
832
assert!(doc.blocks.iter().any(|b| matches!(b, Block::Feed(1))));
833
}
834
805
835
#[test]
806
836
fn dsl_marker_lines_are_not_feed() {
807
837
for src in [
@@ -92,6 +92,18 @@ pub fn parse(source: &str) -> Result<Document, ParseError> {92
Ok(Document { blocks })
92
Ok(Document { blocks })
93
}
93
}
94
94
95
/// True when the last substantive block is `@cut` (trailing blank-line feeds are ignored).
96
pub fn document_ends_with_cut(document: &Document) -> bool {
97
for block in document.blocks.iter().rev() {
98
match block {
99
Block::Feed(_) => continue,
100
Block::Cut => return true,
101
_ => return false,
102
}
103
}
104
false
105
}
106
95
fn strip_comments(source: &str) -> String {
107
fn strip_comments(source: &str) -> String {
96
let mut out = String::with_capacity(source.len());
108
let mut out = String::with_capacity(source.len());
97
let mut chars = source.chars().peekable();
109
let mut chars = source.chars().peekable();
@@ -802,6 +814,24 @@ mod tests {802
assert!(matches!(doc.blocks[1], Block::Feed(1)));
814
assert!(matches!(doc.blocks[1], Block::Feed(1)));
803
}
815
}
804
816
817
#[test]
818
fn document_ends_with_cut_ignores_trailing_blank_lines() {
819
let doc = parse("Line\n@cut\n\n").unwrap();
820
assert!(document_ends_with_cut(&doc));
821
}
822
823
#[test]
824
fn document_without_cut_at_end() {
825
let doc = parse("Line\n@feed 1").unwrap();
826
assert!(!document_ends_with_cut(&doc));
827
}
828
829
#[test]
830
fn blank_line_between_barcode_and_qr_is_feed() {
831
let doc = parse("@barcode RASTER CODE128 X\n\n:center:@qr RASTER Y scale=4").unwrap();
832
assert!(doc.blocks.iter().any(|b| matches!(b, Block::Feed(1))));
833
}
834
805
#[test]
835
#[test]
806
fn dsl_marker_lines_are_not_feed() {
836
fn dsl_marker_lines_are_not_feed() {
807
for src in [
837
for src in [
@@ -173,7 +173,10 @@ impl EscPosEncoder {
173
173
}
174
174
175
175
fn feed(&mut self, lines: u8) {
176
self.buffer.extend_from_slice(&[0x1B, b'd', lines]);
176
// LF feeds match print-bridge / node-escpos; ESC d is unreliable after raster graphics.
177
for _ in 0..lines {
178
self.buffer.push(b'\n');
179
}
177
180
}
178
181
179
182
fn cut(&mut self) {
@@ -433,11 +436,41 @@ mod tests {
433
436
#[test]
434
437
fn encodes_cut_directive() {
435
438
let doc = parse("@cut").unwrap();
439
let mut enc = EscPosEncoder::new(PosMetadata::default());
440
let bytes = enc.encode_document(&doc).unwrap();
441
assert!(bytes.windows(3).any(|w| w == [0x1D, b'V', 66]));
442
}
443
444
#[test]
445
fn feed_uses_line_feed_not_esc_d() {
446
let doc = parse("A\n\nB").unwrap();
447
let mut enc = EscPosEncoder::new(PosMetadata::default());
448
let bytes = enc.encode_document(&doc).unwrap();
449
assert!(bytes.contains(&b'\n'));
450
assert!(!bytes.windows(2).any(|w| w == [0x1B, b'd']));
451
}
452
453
#[test]
454
fn blank_line_between_graphics_feeds_on_print() {
455
let doc = parse("@barcode RASTER CODE128 X\n\n@qr RASTER Y scale=4").unwrap();
456
assert!(doc.blocks.iter().any(|b| matches!(b, Block::Feed(1))));
436
457
let mut enc = EscPosEncoder::new(PosMetadata {
458
page_width_mm: 53.0,
437
459
..Default::default()
438
460
});
439
461
let bytes = enc.encode_document(&doc).unwrap();
440
assert!(bytes.windows(3).any(|w| w == [0x1D, b'V', 66]));
462
let raster_positions: Vec<_> = bytes
463
.windows(4)
464
.enumerate()
465
.filter(|(_, w)| *w == [0x1D, b'v', 0x30, 0x00])
466
.map(|(i, _)| i)
467
.collect();
468
assert_eq!(raster_positions.len(), 2);
469
let between = &bytes[raster_positions[0]..raster_positions[1]];
470
assert!(
471
between.contains(&b'\n'),
472
"expected line feed between raster graphics"
473
);
441
474
}
442
475
443
476
#[test]
@@ -173,7 +173,10 @@ impl EscPosEncoder {173
}
173
}
174
174
175
fn feed(&mut self, lines: u8) {
175
fn feed(&mut self, lines: u8) {
176
self.buffer.extend_from_slice(&[0x1B, b'd', lines]);
176
// LF feeds match print-bridge / node-escpos; ESC d is unreliable after raster graphics.
177
for _ in 0..lines {
178
self.buffer.push(b'\n');
179
}
177
}
180
}
178
181
179
fn cut(&mut self) {
182
fn cut(&mut self) {
@@ -433,11 +436,41 @@ mod tests {433
#[test]
436
#[test]
434
fn encodes_cut_directive() {
437
fn encodes_cut_directive() {
435
let doc = parse("@cut").unwrap();
438
let doc = parse("@cut").unwrap();
439
let mut enc = EscPosEncoder::new(PosMetadata::default());
440
let bytes = enc.encode_document(&doc).unwrap();
441
assert!(bytes.windows(3).any(|w| w == [0x1D, b'V', 66]));
442
}
443
444
#[test]
445
fn feed_uses_line_feed_not_esc_d() {
446
let doc = parse("A\n\nB").unwrap();
447
let mut enc = EscPosEncoder::new(PosMetadata::default());
448
let bytes = enc.encode_document(&doc).unwrap();
449
assert!(bytes.contains(&b'\n'));
450
assert!(!bytes.windows(2).any(|w| w == [0x1B, b'd']));
451
}
452
453
#[test]
454
fn blank_line_between_graphics_feeds_on_print() {
455
let doc = parse("@barcode RASTER CODE128 X\n\n@qr RASTER Y scale=4").unwrap();
456
assert!(doc.blocks.iter().any(|b| matches!(b, Block::Feed(1))));
436
let mut enc = EscPosEncoder::new(PosMetadata {
457
let mut enc = EscPosEncoder::new(PosMetadata {
458
page_width_mm: 53.0,
437
..Default::default()
459
..Default::default()
438
});
460
});
439
let bytes = enc.encode_document(&doc).unwrap();
461
let bytes = enc.encode_document(&doc).unwrap();
440
assert!(bytes.windows(3).any(|w| w == [0x1D, b'V', 66]));
462
let raster_positions: Vec<_> = bytes
463
.windows(4)
464
.enumerate()
465
.filter(|(_, w)| *w == [0x1D, b'v', 0x30, 0x00])
466
.map(|(i, _)| i)
467
.collect();
468
assert_eq!(raster_positions.len(), 2);
469
let between = &bytes[raster_positions[0]..raster_positions[1]];
470
assert!(
471
between.contains(&b'\n'),
472
"expected line feed between raster graphics"
473
);
441
}
474
}
442
475
443
#[test]
476
#[test]
@@ -305,18 +305,52 @@ fn resolve_image_path(path: &str, base_dir: Option<&Path>) -> PathBuf {
305
305
}
306
306
}
307
307
308
/// Pixels per millimetre on screen (preview uses configured page width).
309
const PREVIEW_PX_PER_MM: f32 = 3.0;
308
/// Default ESC/POS line spacing in dots (1/6 inch at 203 DPI).
309
pub const PREVIEW_LINE_SPACING_DOTS: f32 = 30.0;
310
310
311
/// Preview canvas width: one screen pixel per printer dot at zoom 1.0.
311
312
pub fn preview_content_width_px(metadata: &PosMetadata) -> f32 {
312
metadata.page_width_mm * PREVIEW_PX_PER_MM
313
metadata.printable_dots() as f32
313
314
}
314
315
315
/// User-controlled zoom factor applied on top of the mm-based layout.
316
/// Character cell width in preview pixels (matches print column width).
317
pub fn preview_char_width_px(metadata: &PosMetadata, zoom: f32) -> f32 {
318
preview_content_width_px(metadata) * preview_zoom_scale(zoom)
319
/ metadata.chars_per_line().max(1) as f32
320
}
321
322
/// Blank-line / `@feed` vertical space in preview pixels.
323
pub fn preview_line_spacing_px(zoom: f32) -> f32 {
324
PREVIEW_LINE_SPACING_DOTS * preview_zoom_scale(zoom)
325
}
326
327
/// User-controlled zoom factor applied on top of the dot-based layout.
316
328
pub fn preview_zoom_scale(zoom: f32) -> f32 {
317
329
zoom.clamp(0.25, 4.0)
318
330
}
319
331
332
/// Estimated line height for layout (text styles use galley at draw time).
333
pub fn preview_line_height_estimate(
334
line: &PreviewLine,
335
metadata: &PosMetadata,
336
zoom: f32,
337
) -> f32 {
338
let scale = preview_zoom_scale(zoom);
339
let char_w = preview_char_width_px(metadata, zoom);
340
let line_sp = preview_line_spacing_px(zoom);
341
match line {
342
PreviewLine::Text { style, .. } => match style {
343
TextStyle::Heading1 => char_w * 2.2,
344
TextStyle::Heading2 => char_w * 1.8,
345
_ => line_sp,
346
},
347
PreviewLine::Rule => 4.0 * scale,
348
PreviewLine::Graphic { image, .. } => image.height() as f32 * scale,
349
PreviewLine::Error { .. } => line_sp,
350
PreviewLine::Spacer(n) => line_sp * (*n as f32),
351
}
352
}
353
320
354
#[cfg(test)]
321
355
mod tests {
322
356
use super::*;
@@ -454,11 +488,22 @@ mod tests {
454
488
}
455
489
456
490
#[test]
457
fn preview_width_follows_page_mm() {
491
fn preview_width_matches_printable_dots() {
458
492
let meta = PosMetadata {
459
page_width_mm: 58.0,
493
page_width_mm: 53.0,
460
494
..Default::default()
461
495
};
462
assert!((preview_content_width_px(&meta) - 58.0 * PREVIEW_PX_PER_MM).abs() < 0.01);
496
assert_eq!(
497
preview_content_width_px(&meta) as u16,
498
meta.printable_dots()
499
);
500
}
501
502
#[test]
503
fn spacer_height_matches_line_spacing() {
504
let meta = PosMetadata::default();
505
let line = PreviewLine::Spacer(1);
506
let h = preview_line_height_estimate(&line, &meta, 1.0);
507
assert!((h - PREVIEW_LINE_SPACING_DOTS).abs() < 0.01);
463
508
}
464
509
}
@@ -305,18 +305,52 @@ fn resolve_image_path(path: &str, base_dir: Option<&Path>) -> PathBuf {305
}
305
}
306
}
306
}
307
307
308
/// Pixels per millimetre on screen (preview uses configured page width).
308
/// Default ESC/POS line spacing in dots (1/6 inch at 203 DPI).
309
const PREVIEW_PX_PER_MM: f32 = 3.0;
309
pub const PREVIEW_LINE_SPACING_DOTS: f32 = 30.0;
310
310
311
/// Preview canvas width: one screen pixel per printer dot at zoom 1.0.
311
pub fn preview_content_width_px(metadata: &PosMetadata) -> f32 {
312
pub fn preview_content_width_px(metadata: &PosMetadata) -> f32 {
312
metadata.page_width_mm * PREVIEW_PX_PER_MM
313
metadata.printable_dots() as f32
313
}
314
}
314
315
315
/// User-controlled zoom factor applied on top of the mm-based layout.
316
/// Character cell width in preview pixels (matches print column width).
317
pub fn preview_char_width_px(metadata: &PosMetadata, zoom: f32) -> f32 {
318
preview_content_width_px(metadata) * preview_zoom_scale(zoom)
319
/ metadata.chars_per_line().max(1) as f32
320
}
321
322
/// Blank-line / `@feed` vertical space in preview pixels.
323
pub fn preview_line_spacing_px(zoom: f32) -> f32 {
324
PREVIEW_LINE_SPACING_DOTS * preview_zoom_scale(zoom)
325
}
326
327
/// User-controlled zoom factor applied on top of the dot-based layout.
316
pub fn preview_zoom_scale(zoom: f32) -> f32 {
328
pub fn preview_zoom_scale(zoom: f32) -> f32 {
317
zoom.clamp(0.25, 4.0)
329
zoom.clamp(0.25, 4.0)
318
}
330
}
319
331
332
/// Estimated line height for layout (text styles use galley at draw time).
333
pub fn preview_line_height_estimate(
334
line: &PreviewLine,
335
metadata: &PosMetadata,
336
zoom: f32,
337
) -> f32 {
338
let scale = preview_zoom_scale(zoom);
339
let char_w = preview_char_width_px(metadata, zoom);
340
let line_sp = preview_line_spacing_px(zoom);
341
match line {
342
PreviewLine::Text { style, .. } => match style {
343
TextStyle::Heading1 => char_w * 2.2,
344
TextStyle::Heading2 => char_w * 1.8,
345
_ => line_sp,
346
},
347
PreviewLine::Rule => 4.0 * scale,
348
PreviewLine::Graphic { image, .. } => image.height() as f32 * scale,
349
PreviewLine::Error { .. } => line_sp,
350
PreviewLine::Spacer(n) => line_sp * (*n as f32),
351
}
352
}
353
320
#[cfg(test)]
354
#[cfg(test)]
321
mod tests {
355
mod tests {
322
use super::*;
356
use super::*;
@@ -454,11 +488,22 @@ mod tests {454
}
488
}
455
489
456
#[test]
490
#[test]
457
fn preview_width_follows_page_mm() {
491
fn preview_width_matches_printable_dots() {
458
let meta = PosMetadata {
492
let meta = PosMetadata {
459
page_width_mm: 58.0,
493
page_width_mm: 53.0,
460
..Default::default()
494
..Default::default()
461
};
495
};
462
assert!((preview_content_width_px(&meta) - 58.0 * PREVIEW_PX_PER_MM).abs() < 0.01);
496
assert_eq!(
497
preview_content_width_px(&meta) as u16,
498
meta.printable_dots()
499
);
500
}
501
502
#[test]
503
fn spacer_height_matches_line_spacing() {
504
let meta = PosMetadata::default();
505
let line = PreviewLine::Spacer(1);
506
let h = preview_line_height_estimate(&line, &meta, 1.0);
507
assert!((h - PREVIEW_LINE_SPACING_DOTS).abs() < 0.01);
463
}
508
}
464
}
509
}