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