| 1 | use std::collections::HashMap;
|
| 2 | use std::path::Path;
|
| 3 |
|
| 4 | use image::GrayImage;
|
| 5 |
|
| 6 | use crate::align_row::{columns_from_parts, layout_aligned_row_or_lines};
|
| 7 | use crate::dsl::{
|
| 8 | Align, BarcodeMode, BarcodeSymbology, Block, Document, ImageWidth, QrMode,
|
| 9 | };
|
| 10 | use crate::io::PosMetadata;
|
| 11 | use crate::datetime;
|
| 12 | use crate::params::{preview_barcode_sample, substitute_for_preview, substitute_inlines_for_preview};
|
| 13 | use crate::raster::{render_barcode, render_qr, scale_gray_to_print_width};
|
| 14 | use crate::text_wrap::{self, TextWrapStyle};
|
| 15 |
|
| 16 | #[derive(Debug, Clone, PartialEq)]
|
| 17 | pub struct PreviewModel {
|
| 18 | pub lines: Vec<PreviewLine>,
|
| 19 | pub width_dots: u32,
|
| 20 | pub width_mm: f32,
|
| 21 | }
|
| 22 |
|
| 23 | #[derive(Debug, Clone, PartialEq)]
|
| 24 | pub enum PreviewLine {
|
| 25 | Text {
|
| 26 | content: String,
|
| 27 | align: Align,
|
| 28 | style: TextStyle,
|
| 29 | },
|
| 30 | Rule,
|
| 31 | Graphic {
|
| 32 | image: GrayImage,
|
| 33 | align: Align,
|
| 34 | },
|
| 35 | Error {
|
| 36 | message: String,
|
| 37 | align: Align,
|
| 38 | },
|
| 39 | Spacer(u8),
|
| 40 | }
|
| 41 |
|
| 42 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
| 43 | pub enum TextStyle {
|
| 44 | #[default]
|
| 45 | Normal,
|
| 46 | Heading1,
|
| 47 | Heading2,
|
| 48 | }
|
| 49 |
|
| 50 | pub fn render_preview(
|
| 51 | document: &Document,
|
| 52 | metadata: &PosMetadata,
|
| 53 | base_dir: Option<&Path>,
|
| 54 | preview_params: &HashMap<String, String>,
|
| 55 | ) -> PreviewModel {
|
| 56 | let width_dots = metadata.printable_dots() as u32;
|
| 57 | let mut lines = Vec::new();
|
| 58 |
|
| 59 | for block in &document.blocks {
|
| 60 | match block {
|
| 61 | Block::Paragraph { align, inlines } => {
|
| 62 | let text = substitute_inlines_for_preview(inlines, preview_params);
|
| 63 | let cols = text_wrap::wrap_cols(metadata, TextWrapStyle::Normal);
|
| 64 | for line in text_wrap::wrap_text(&text, cols) {
|
| 65 | lines.push(PreviewLine::Text {
|
| 66 | content: line,
|
| 67 | align: *align,
|
| 68 | style: TextStyle::Normal,
|
| 69 | });
|
| 70 | }
|
| 71 | }
|
| 72 | Block::AlignedRow { parts } => {
|
| 73 | let cols = text_wrap::wrap_cols(metadata, TextWrapStyle::Normal);
|
| 74 | let plain_parts: Vec<(Align, String)> = parts
|
| 75 | .iter()
|
| 76 | .map(|(align, inlines)| {
|
| 77 | (
|
| 78 | *align,
|
| 79 | substitute_inlines_for_preview(inlines, preview_params),
|
| 80 | )
|
| 81 | })
|
| 82 | .collect();
|
| 83 | let columns = columns_from_parts(&plain_parts);
|
| 84 | for (align, line) in layout_aligned_row_or_lines(&columns, cols) {
|
| 85 | for wrapped in text_wrap::wrap_text(&line, cols) {
|
| 86 | lines.push(PreviewLine::Text {
|
| 87 | content: wrapped,
|
| 88 | align,
|
| 89 | style: TextStyle::Normal,
|
| 90 | });
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 | Block::List { align, items } => {
|
| 95 | let cols = text_wrap::wrap_cols(metadata, TextWrapStyle::Normal);
|
| 96 | for item in items {
|
| 97 | let text = substitute_inlines_for_preview(&item.inlines, preview_params);
|
| 98 | for line in text_wrap::wrap_bullet_item_at_depth(&text, cols, item.depth) {
|
| 99 | lines.push(PreviewLine::Text {
|
| 100 | content: line,
|
| 101 | align: *align,
|
| 102 | style: TextStyle::Normal,
|
| 103 | });
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 | Block::Heading { level, align, inlines } => {
|
| 108 | let style = if *level == 1 {
|
| 109 | TextStyle::Heading1
|
| 110 | } else {
|
| 111 | TextStyle::Heading2
|
| 112 | };
|
| 113 | let wrap_style = if *level == 1 {
|
| 114 | TextWrapStyle::Heading1
|
| 115 | } else {
|
| 116 | TextWrapStyle::Heading2
|
| 117 | };
|
| 118 | let text = substitute_inlines_for_preview(inlines, preview_params);
|
| 119 | let cols = text_wrap::wrap_cols(metadata, wrap_style);
|
| 120 | for line in text_wrap::wrap_text(&text, cols) {
|
| 121 | lines.push(PreviewLine::Text {
|
| 122 | content: line,
|
| 123 | align: *align,
|
| 124 | style,
|
| 125 | });
|
| 126 | }
|
| 127 | }
|
| 128 | Block::Rule => lines.push(PreviewLine::Rule),
|
| 129 | Block::Feed(n) => lines.push(PreviewLine::Spacer(*n)),
|
| 130 | Block::Cut => lines.push(PreviewLine::Text {
|
| 131 | content: "--- cut ---".into(),
|
| 132 | align: Align::Center,
|
| 133 | style: TextStyle::Normal,
|
| 134 | }),
|
| 135 | Block::Image { path, width, align } => {
|
| 136 | let path = substitute_for_preview(path, preview_params);
|
| 137 | lines.push(render_image_preview(&path, *width, *align, metadata, base_dir));
|
| 138 | }
|
| 139 | Block::Barcode {
|
| 140 | mode,
|
| 141 | symbology,
|
| 142 | data,
|
| 143 | height,
|
| 144 | align,
|
| 145 | ..
|
| 146 | } => {
|
| 147 | let resolved = substitute_for_preview(data, preview_params);
|
| 148 | lines.push(render_barcode_preview(
|
| 149 | *mode,
|
| 150 | *symbology,
|
| 151 | data,
|
| 152 | &resolved,
|
| 153 | *height,
|
| 154 | *align,
|
| 155 | metadata,
|
| 156 | ));
|
| 157 | }
|
| 158 | Block::Qr {
|
| 159 | mode,
|
| 160 | data,
|
| 161 | size,
|
| 162 | ecc,
|
| 163 | version,
|
| 164 | align,
|
| 165 | } => {
|
| 166 | let data = substitute_for_preview(data, preview_params);
|
| 167 | lines.push(render_qr_preview(
|
| 168 | *mode, &data, *size, *ecc, *version, *align, metadata,
|
| 169 | ));
|
| 170 | }
|
| 171 | Block::Date { format, align } => {
|
| 172 | let text = datetime::format_date(format.as_deref());
|
| 173 | let cols = text_wrap::wrap_cols(metadata, TextWrapStyle::Normal);
|
| 174 | for line in text_wrap::wrap_text(&text, cols) {
|
| 175 | lines.push(PreviewLine::Text {
|
| 176 | content: line,
|
| 177 | align: *align,
|
| 178 | style: TextStyle::Normal,
|
| 179 | });
|
| 180 | }
|
| 181 | }
|
| 182 | Block::Time { format, align } => {
|
| 183 | let text = datetime::format_time(*format);
|
| 184 | let cols = text_wrap::wrap_cols(metadata, TextWrapStyle::Normal);
|
| 185 | for line in text_wrap::wrap_text(&text, cols) {
|
| 186 | lines.push(PreviewLine::Text {
|
| 187 | content: line,
|
| 188 | align: *align,
|
| 189 | style: TextStyle::Normal,
|
| 190 | });
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | PreviewModel {
|
| 197 | lines,
|
| 198 | width_dots,
|
| 199 | width_mm: metadata.page_width_mm,
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | fn render_barcode_preview(
|
| 204 | mode: BarcodeMode,
|
| 205 | symbology: BarcodeSymbology,
|
| 206 | template_data: &str,
|
| 207 | resolved_data: &str,
|
| 208 | height: u8,
|
| 209 | align: Align,
|
| 210 | metadata: &PosMetadata,
|
| 211 | ) -> PreviewLine {
|
| 212 | let height_px = u32::from(height.max(20));
|
| 213 | let resolved_data = resolved_data.trim();
|
| 214 | let print_width = metadata.printable_dots() as u32;
|
| 215 | match mode {
|
| 216 | BarcodeMode::Raster | BarcodeMode::Native => {
|
| 217 | match render_barcode(symbology, resolved_data, height_px) {
|
| 218 | Ok(img) => PreviewLine::Graphic {
|
| 219 | image: scale_gray_to_print_width(img, print_width),
|
| 220 | align,
|
| 221 | },
|
| 222 | Err(e) => {
|
| 223 | if template_data.contains("${") {
|
| 224 | if let Ok(img) =
|
| 225 | render_barcode(symbology, preview_barcode_sample(symbology), height_px)
|
| 226 | {
|
| 227 | return PreviewLine::Graphic {
|
| 228 | image: scale_gray_to_print_width(img, print_width),
|
| 229 | align,
|
| 230 | };
|
| 231 | }
|
| 232 | }
|
| 233 | PreviewLine::Error {
|
| 234 | message: format!(
|
| 235 | "barcode: {e} ({} supports alphanumeric data; URLs and punctuation often fail)",
|
| 236 | symbology.as_str()
|
| 237 | ),
|
| 238 | align,
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | fn render_qr_preview(
|
| 247 | mode: QrMode,
|
| 248 | data: &str,
|
| 249 | size: u8,
|
| 250 | ecc: crate::dsl::QrEcc,
|
| 251 | version: Option<u8>,
|
| 252 | align: Align,
|
| 253 | metadata: &PosMetadata,
|
| 254 | ) -> PreviewLine {
|
| 255 | let module_pixels = u32::from(size.max(2));
|
| 256 | match mode {
|
| 257 | QrMode::Raster | QrMode::Native => match render_qr(data, version, ecc, module_pixels) {
|
| 258 | Ok(img) => {
|
| 259 | let max_w = (metadata.printable_dots() as u32).min(img.width());
|
| 260 | scale_graphic_to_width(img, max_w, align)
|
| 261 | }
|
| 262 | Err(e) => PreviewLine::Error {
|
| 263 | message: format!("qr: {e}"),
|
| 264 | align,
|
| 265 | },
|
| 266 | },
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | fn render_image_preview(
|
| 271 | path: &str,
|
| 272 | width: ImageWidth,
|
| 273 | align: Align,
|
| 274 | metadata: &PosMetadata,
|
| 275 | base_dir: Option<&Path>,
|
| 276 | ) -> PreviewLine {
|
| 277 | match crate::image_load::load_image(path, base_dir) {
|
| 278 | Ok(img) => {
|
| 279 | let gray = img.to_luma8();
|
| 280 | let target = target_image_width(width, metadata);
|
| 281 | let scaled = scale_gray_to_width(gray, target);
|
| 282 | PreviewLine::Graphic {
|
| 283 | image: scaled,
|
| 284 | align,
|
| 285 | }
|
| 286 | }
|
| 287 | Err(e) => PreviewLine::Error {
|
| 288 | message: e.to_string(),
|
| 289 | align,
|
| 290 | },
|
| 291 | }
|
| 292 | }
|
| 293 |
|
| 294 | fn scale_graphic_to_width(img: GrayImage, target_width: u32, align: Align) -> PreviewLine {
|
| 295 | PreviewLine::Graphic {
|
| 296 | image: scale_gray_to_width(img, target_width),
|
| 297 | align,
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | fn scale_gray_to_width(gray: GrayImage, target_width: u32) -> GrayImage {
|
| 302 | let (w, h) = gray.dimensions();
|
| 303 | if w == 0 || h == 0 {
|
| 304 | return gray;
|
| 305 | }
|
| 306 | let new_w = target_width.min(w).max(1);
|
| 307 | if new_w == w {
|
| 308 | return gray;
|
| 309 | }
|
| 310 | let new_h = ((h as f32) * (new_w as f32 / w as f32)).max(1.0) as u32;
|
| 311 | image::imageops::resize(
|
| 312 | &gray,
|
| 313 | new_w,
|
| 314 | new_h,
|
| 315 | image::imageops::FilterType::Nearest,
|
| 316 | )
|
| 317 | }
|
| 318 |
|
| 319 | fn target_image_width(width: ImageWidth, metadata: &PosMetadata) -> u32 {
|
| 320 | match width {
|
| 321 | ImageWidth::Percent(pct) => {
|
| 322 | (metadata.printable_dots() as u32 * pct as u32 / 100).max(1)
|
| 323 | }
|
| 324 | ImageWidth::Dots(d) => d as u32,
|
| 325 | ImageWidth::Default => metadata.printable_dots() as u32,
|
| 326 | }
|
| 327 | }
|
| 328 |
|
| 329 | /// Default ESC/POS line spacing in dots (1/6 inch at 203 DPI).
|
| 330 | pub const PREVIEW_LINE_SPACING_DOTS: f32 = 30.0;
|
| 331 |
|
| 332 | /// Screen calibration so zoom `1.0` matches a typical receipt width on screen.
|
| 333 | pub const PREVIEW_SCREEN_CALIBRATION: f32 = 0.52;
|
| 334 |
|
| 335 | /// Preview canvas width in printer dots (before screen calibration).
|
| 336 | pub fn preview_content_width_px(metadata: &PosMetadata) -> f32 {
|
| 337 | metadata.printable_dots() as f32
|
| 338 | }
|
| 339 |
|
| 340 | /// User-controlled zoom factor (1.0 = 100%).
|
| 341 | pub fn preview_zoom_scale(zoom: f32) -> f32 {
|
| 342 | zoom.clamp(0.25, 4.0)
|
| 343 | }
|
| 344 |
|
| 345 | /// Effective on-screen scale for page width, text, and raster graphics.
|
| 346 | pub fn preview_visual_scale(zoom: f32) -> f32 {
|
| 347 | preview_zoom_scale(zoom) * PREVIEW_SCREEN_CALIBRATION
|
| 348 | }
|
| 349 |
|
| 350 | /// Character cell width in preview pixels (matches print column width).
|
| 351 | pub fn preview_char_width_px(metadata: &PosMetadata, zoom: f32) -> f32 {
|
| 352 | preview_content_width_px(metadata) * preview_visual_scale(zoom)
|
| 353 | / metadata.chars_per_line().max(1) as f32
|
| 354 | }
|
| 355 |
|
| 356 | /// egui monospace sized 1:1 to column width reads smaller than printed Font A.
|
| 357 | /// Empirically ~60% of print; scale up so preview matches the receipt.
|
| 358 | pub const PREVIEW_FONT_PRINT_MATCH: f32 = 1.0 / 0.6;
|
| 359 |
|
| 360 | /// Font size in preview pixels for normal / `##` text.
|
| 361 | pub fn preview_font_size_px(char_width: f32) -> f32 {
|
| 362 | char_width * PREVIEW_FONT_PRINT_MATCH
|
| 363 | }
|
| 364 |
|
| 365 | /// Blank-line / `@feed` vertical space in preview pixels (printer line spacing).
|
| 366 | pub fn preview_line_spacing_px(zoom: f32) -> f32 {
|
| 367 | PREVIEW_LINE_SPACING_DOTS * preview_visual_scale(zoom)
|
| 368 | }
|
| 369 |
|
| 370 | /// Vertical advance for a text style (matches printer LF / ESC d line spacing).
|
| 371 | pub fn preview_text_advance_px(style: TextStyle, zoom: f32) -> f32 {
|
| 372 | let line_sp = preview_line_spacing_px(zoom);
|
| 373 | match style {
|
| 374 | // GS ! 0x11: double height → two line advances
|
| 375 | TextStyle::Heading1 => line_sp * 2.0,
|
| 376 | TextStyle::Heading2 | TextStyle::Normal => line_sp,
|
| 377 | }
|
| 378 | }
|
| 379 |
|
| 380 | /// Estimated line height for layout (text styles use galley at draw time).
|
| 381 | pub fn preview_line_height_estimate(
|
| 382 | line: &PreviewLine,
|
| 383 | _metadata: &PosMetadata,
|
| 384 | zoom: f32,
|
| 385 | ) -> f32 {
|
| 386 | let scale = preview_visual_scale(zoom);
|
| 387 | match line {
|
| 388 | PreviewLine::Text { style, .. } => preview_text_advance_px(*style, zoom),
|
| 389 | PreviewLine::Rule => 4.0 * scale,
|
| 390 | PreviewLine::Graphic { image, .. } => image.height() as f32 * scale,
|
| 391 | PreviewLine::Error { .. } => preview_line_spacing_px(zoom),
|
| 392 | PreviewLine::Spacer(n) => preview_line_spacing_px(zoom) * (*n as f32),
|
| 393 | }
|
| 394 | }
|
| 395 |
|
| 396 | #[cfg(test)]
|
| 397 | mod tests {
|
| 398 | use super::*;
|
| 399 | use crate::dsl::parse;
|
| 400 | use std::collections::HashMap;
|
| 401 |
|
| 402 | #[test]
|
| 403 | fn renders_paragraph_lines() {
|
| 404 | let doc = parse("Line1\nLine2").unwrap();
|
| 405 | let model = render_preview(&doc, &PosMetadata::default(), None, &HashMap::new());
|
| 406 | assert!(model.lines.len() >= 2);
|
| 407 | }
|
| 408 |
|
| 409 | #[test]
|
| 410 | fn uses_placeholder_when_param_not_set() {
|
| 411 | let doc = parse("${X}").unwrap();
|
| 412 | let model = render_preview(&doc, &PosMetadata::default(), None, &HashMap::new());
|
| 413 | let PreviewLine::Text { content, .. } = &model.lines[0] else {
|
| 414 | panic!();
|
| 415 | };
|
| 416 | assert_eq!(content, "1234567890");
|
| 417 | }
|
| 418 |
|
| 419 | #[test]
|
| 420 | fn uses_dialog_params_in_preview() {
|
| 421 | let doc = parse("Hello ${Name}").unwrap();
|
| 422 | let mut params = HashMap::new();
|
| 423 | params.insert("Name".into(), "Ada".into());
|
| 424 | let model = render_preview(&doc, &PosMetadata::default(), None, ¶ms);
|
| 425 | let PreviewLine::Text { content, .. } = &model.lines[0] else {
|
| 426 | panic!();
|
| 427 | };
|
| 428 | assert_eq!(content, "Hello Ada");
|
| 429 | }
|
| 430 |
|
| 431 | #[test]
|
| 432 | fn renders_barcode_graphic() {
|
| 433 | let doc = parse("@barcode CODE128 TEST").unwrap();
|
| 434 | let model = render_preview(&doc, &PosMetadata::default(), None, &HashMap::new());
|
| 435 | assert!(model
|
| 436 | .lines
|
| 437 | .iter()
|
| 438 | .any(|l| matches!(l, PreviewLine::Graphic { .. })));
|
| 439 | }
|
| 440 |
|
| 441 | #[test]
|
| 442 | fn renders_barcode_with_unresolved_params() {
|
| 443 | let doc = parse("@barcode CODE128 ${DocumentNumber}").unwrap();
|
| 444 | let model = render_preview(&doc, &PosMetadata::default(), None, &HashMap::new());
|
| 445 | assert!(model
|
| 446 | .lines
|
| 447 | .iter()
|
| 448 | .any(|l| matches!(l, PreviewLine::Graphic { .. })));
|
| 449 | }
|
| 450 |
|
| 451 | #[test]
|
| 452 | fn renders_raster_code128_with_data_param() {
|
| 453 | let doc = parse(
|
| 454 | ":center:\n# Hello\n@qr ${Data}\n@barcode RASTER CODE128 ${Data}\n",
|
| 455 | )
|
| 456 | .unwrap();
|
| 457 | let model = render_preview(&doc, &PosMetadata::default(), None, &HashMap::new());
|
| 458 | let graphics = model
|
| 459 | .lines
|
| 460 | .iter()
|
| 461 | .filter(|l| matches!(l, PreviewLine::Graphic { .. }))
|
| 462 | .count();
|
| 463 | let errors: Vec<_> = model
|
| 464 | .lines
|
| 465 | .iter()
|
| 466 | .filter_map(|l| match l {
|
| 467 | PreviewLine::Error { message, .. } => Some(message.as_str()),
|
| 468 | _ => None,
|
| 469 | })
|
| 470 | .collect();
|
| 471 | assert_eq!(graphics, 2, "expected QR and barcode graphics, errors: {errors:?}");
|
| 472 | }
|
| 473 |
|
| 474 | #[test]
|
| 475 | fn empty_param_value_uses_barcode_placeholder() {
|
| 476 | let doc = parse("@barcode RASTER CODE128 ${Data}\n").unwrap();
|
| 477 | let mut params = HashMap::new();
|
| 478 | params.insert("Data".into(), String::new());
|
| 479 | let model = render_preview(&doc, &PosMetadata::default(), None, ¶ms);
|
| 480 | assert!(model
|
| 481 | .lines
|
| 482 | .iter()
|
| 483 | .any(|l| matches!(l, PreviewLine::Graphic { .. })));
|
| 484 | }
|
| 485 |
|
| 486 | #[test]
|
| 487 | fn shared_data_param_url_still_renders_barcode_preview() {
|
| 488 | let doc = parse("@qr ${Data}\n@barcode RASTER CODE128 ${Data}\n").unwrap();
|
| 489 | let mut params = HashMap::new();
|
| 490 | params.insert("Data".into(), "https://example.com".into());
|
| 491 | let model = render_preview(&doc, &PosMetadata::default(), None, ¶ms);
|
| 492 | let graphics = model
|
| 493 | .lines
|
| 494 | .iter()
|
| 495 | .filter(|l| matches!(l, PreviewLine::Graphic { .. }))
|
| 496 | .count();
|
| 497 | assert_eq!(graphics, 2, "QR and barcode preview should both render");
|
| 498 | }
|
| 499 |
|
| 500 | #[test]
|
| 501 | fn persistent_center_heading_uses_center_in_preview() {
|
| 502 | let doc = parse(":center:\n# Hello").unwrap();
|
| 503 | let model = render_preview(&doc, &PosMetadata::default(), None, &HashMap::new());
|
| 504 | let PreviewLine::Text { align, content, .. } = &model.lines[0] else {
|
| 505 | panic!();
|
| 506 | };
|
| 507 | assert_eq!(content, "Hello");
|
| 508 | assert_eq!(*align, Align::Center);
|
| 509 | }
|
| 510 |
|
| 511 | #[test]
|
| 512 | fn inline_left_right_is_one_preview_line() {
|
| 513 | let doc = parse(":left:appel:right:10").unwrap();
|
| 514 | let meta = PosMetadata {
|
| 515 | chars_per_line: Some(32),
|
| 516 | ..Default::default()
|
| 517 | };
|
| 518 | let model = render_preview(&doc, &meta, None, &HashMap::new());
|
| 519 | let text_lines: Vec<_> = model
|
| 520 | .lines
|
| 521 | .iter()
|
| 522 | .filter_map(|l| match l {
|
| 523 | PreviewLine::Text { content, align, .. } => Some((content.as_str(), *align)),
|
| 524 | _ => None,
|
| 525 | })
|
| 526 | .collect();
|
| 527 | assert_eq!(text_lines.len(), 1);
|
| 528 | assert_eq!(text_lines[0].1, Align::Left);
|
| 529 | assert_eq!(
|
| 530 | text_lines[0].0,
|
| 531 | format!("appel{}10", " ".repeat(25))
|
| 532 | );
|
| 533 | }
|
| 534 |
|
| 535 | #[test]
|
| 536 | fn heading_wraps_in_preview_on_narrow_paper() {
|
| 537 | let doc = parse("# Proof of Transfer").unwrap();
|
| 538 | let meta = PosMetadata {
|
| 539 | page_width_mm: 53.0,
|
| 540 | ..Default::default()
|
| 541 | };
|
| 542 | let model = render_preview(&doc, &meta, None, &HashMap::new());
|
| 543 | let text_lines: Vec<_> = model
|
| 544 | .lines
|
| 545 | .iter()
|
| 546 | .filter_map(|l| match l {
|
| 547 | PreviewLine::Text { content, style, .. } if *style == TextStyle::Heading1 => {
|
| 548 | Some(content.as_str())
|
| 549 | }
|
| 550 | _ => None,
|
| 551 | })
|
| 552 | .collect();
|
| 553 | assert_eq!(text_lines, vec!["Proof of", "Transfer"]);
|
| 554 | }
|
| 555 |
|
| 556 | #[test]
|
| 557 | fn preview_width_matches_printable_dots() {
|
| 558 | let meta = PosMetadata {
|
| 559 | page_width_mm: 53.0,
|
| 560 | ..Default::default()
|
| 561 | };
|
| 562 | assert_eq!(
|
| 563 | preview_content_width_px(&meta) as u16,
|
| 564 | meta.printable_dots()
|
| 565 | );
|
| 566 | }
|
| 567 |
|
| 568 | #[test]
|
| 569 | fn spacer_height_matches_line_spacing() {
|
| 570 | let meta = PosMetadata::default();
|
| 571 | let line = PreviewLine::Spacer(1);
|
| 572 | let h = preview_line_height_estimate(&line, &meta, 1.0);
|
| 573 | assert!((h - PREVIEW_LINE_SPACING_DOTS * PREVIEW_SCREEN_CALIBRATION).abs() < 0.01);
|
| 574 | }
|
| 575 |
|
| 576 | #[test]
|
| 577 | fn zoom_100_percent_uses_screen_calibration() {
|
| 578 | assert!((preview_visual_scale(1.0) - PREVIEW_SCREEN_CALIBRATION).abs() < 0.001);
|
| 579 | }
|
| 580 |
|
| 581 | #[test]
|
| 582 | fn font_size_matches_print_calibration() {
|
| 583 | let char_w = 6.0;
|
| 584 | assert!((preview_font_size_px(char_w) - char_w / 0.6).abs() < 0.001);
|
| 585 | }
|
| 586 |
|
| 587 | #[test]
|
| 588 | fn char_width_fits_columns_on_paper() {
|
| 589 | let meta = PosMetadata::default();
|
| 590 | let width = preview_content_width_px(&meta) * preview_visual_scale(1.0);
|
| 591 | let char_w = preview_char_width_px(&meta, 1.0);
|
| 592 | assert!((char_w * meta.chars_per_line() as f32 - width).abs() < 0.01);
|
| 593 | }
|
| 594 |
|
| 595 | #[test]
|
| 596 | fn heading1_is_double_normal_text_height() {
|
| 597 | let meta = PosMetadata::default();
|
| 598 | let normal = preview_line_height_estimate(
|
| 599 | &PreviewLine::Text {
|
| 600 | content: "x".into(),
|
| 601 | align: crate::dsl::Align::Left,
|
| 602 | style: TextStyle::Normal,
|
| 603 | },
|
| 604 | &meta,
|
| 605 | 1.0,
|
| 606 | );
|
| 607 | let heading = preview_line_height_estimate(
|
| 608 | &PreviewLine::Text {
|
| 609 | content: "x".into(),
|
| 610 | align: crate::dsl::Align::Left,
|
| 611 | style: TextStyle::Heading1,
|
| 612 | },
|
| 613 | &meta,
|
| 614 | 1.0,
|
| 615 | );
|
| 616 | assert!((heading - normal * 2.0).abs() < 0.01);
|
| 617 | }
|
| 618 |
|
| 619 | #[test]
|
| 620 | fn spacer_matches_normal_text_advance() {
|
| 621 | let meta = PosMetadata::default();
|
| 622 | let spacer = preview_line_height_estimate(&PreviewLine::Spacer(1), &meta, 1.0);
|
| 623 | let text = preview_text_advance_px(TextStyle::Normal, 1.0);
|
| 624 | assert!((spacer - text).abs() < 0.01);
|
| 625 | }
|
| 626 |
|
| 627 | #[test]
|
| 628 | fn renders_bullet_list_with_hanging_indent() {
|
| 629 | let doc = parse("- this is a long line").unwrap();
|
| 630 | let meta = PosMetadata {
|
| 631 | chars_per_line: Some(15),
|
| 632 | ..Default::default()
|
| 633 | };
|
| 634 | let model = render_preview(&doc, &meta, None, &HashMap::new());
|
| 635 | assert_eq!(model.lines.len(), 2);
|
| 636 | let PreviewLine::Text { content: first, .. } = &model.lines[0] else {
|
| 637 | panic!();
|
| 638 | };
|
| 639 | let PreviewLine::Text { content: second, .. } = &model.lines[1] else {
|
| 640 | panic!();
|
| 641 | };
|
| 642 | assert!(first.starts_with("* "));
|
| 643 | assert!(second.starts_with(" "));
|
| 644 | }
|
| 645 | }
|