| 1 | //! Shared receipt preview canvas for Design and print-only views.
|
| 2 |
|
| 3 | use std::collections::HashMap;
|
| 4 | use std::path::Path;
|
| 5 |
|
| 6 | use eframe::egui;
|
| 7 |
|
| 8 | use posprint::dsl::{self, parse};
|
| 9 | use posprint::io::PosMetadata;
|
| 10 | use posprint::preview::{
|
| 11 | preview_char_width_px, preview_content_width_px, preview_font_size_px,
|
| 12 | preview_line_height_estimate, preview_line_spacing_px, preview_text_advance_px,
|
| 13 | preview_visual_scale, render_preview, PreviewLine, TextStyle,
|
| 14 | };
|
| 15 |
|
| 16 | /// Draw a live receipt preview using current parameter values.
|
| 17 | pub fn show_preview_panel(
|
| 18 | ui: &mut egui::Ui,
|
| 19 | ctx: &egui::Context,
|
| 20 | body: &str,
|
| 21 | metadata: &PosMetadata,
|
| 22 | base_dir: Option<&Path>,
|
| 23 | params: &HashMap<String, String>,
|
| 24 | preview_zoom: f32,
|
| 25 | texture_id_prefix: &str,
|
| 26 | ) {
|
| 27 | let Ok(doc) = parse(body) else {
|
| 28 | ui.label("Fix parse errors to preview.");
|
| 29 | return;
|
| 30 | };
|
| 31 |
|
| 32 | let model = render_preview(&doc, metadata, base_dir, params);
|
| 33 | let visual_scale = preview_visual_scale(preview_zoom);
|
| 34 | let content_width = preview_content_width_px(metadata) * visual_scale;
|
| 35 | let char_width = preview_char_width_px(metadata, preview_zoom);
|
| 36 | let line_spacing = preview_line_spacing_px(preview_zoom);
|
| 37 | let pad_y = 4.0 * visual_scale;
|
| 38 |
|
| 39 | egui::ScrollArea::both()
|
| 40 | .auto_shrink([false; 2])
|
| 41 | .show(ui, |ui| {
|
| 42 | egui::Frame::canvas(ui.style()).show(ui, |ui| {
|
| 43 | let mut total_height = pad_y * 2.0;
|
| 44 | for line in &model.lines {
|
| 45 | total_height += preview_line_height_estimate(line, metadata, preview_zoom);
|
| 46 | }
|
| 47 | total_height = total_height.max(100.0);
|
| 48 |
|
| 49 | let (_id, rect) = ui.allocate_space(egui::vec2(content_width, total_height));
|
| 50 | let painter = ui.painter_at(rect);
|
| 51 | painter.rect_filled(rect, 4.0, egui::Color32::WHITE);
|
| 52 | painter.rect_stroke(
|
| 53 | rect,
|
| 54 | 4.0,
|
| 55 | egui::Stroke::new(1.0_f32, egui::Color32::GRAY),
|
| 56 | egui::StrokeKind::Inside,
|
| 57 | );
|
| 58 |
|
| 59 | let mut y = rect.min.y + pad_y;
|
| 60 | let inner_width = content_width;
|
| 61 |
|
| 62 | for (idx, line) in model.lines.iter().enumerate() {
|
| 63 | match line {
|
| 64 | PreviewLine::Text {
|
| 65 | content,
|
| 66 | align,
|
| 67 | style,
|
| 68 | } => {
|
| 69 | let font_id = preview_text_font(*style, char_width);
|
| 70 | let galley =
|
| 71 | painter.layout_no_wrap(content.clone(), font_id, egui::Color32::BLACK);
|
| 72 | let x = align_x(*align, rect.min.x, inner_width, galley.size().x);
|
| 73 | painter.galley(egui::pos2(x, y), galley, egui::Color32::BLACK);
|
| 74 | y += preview_text_advance_px(*style, preview_zoom);
|
| 75 | }
|
| 76 | PreviewLine::Rule => {
|
| 77 | painter.line_segment(
|
| 78 | [egui::pos2(rect.min.x, y), egui::pos2(rect.max.x, y)],
|
| 79 | egui::Stroke::new(1.0_f32, egui::Color32::DARK_GRAY),
|
| 80 | );
|
| 81 | y += preview_line_spacing_px(preview_zoom);
|
| 82 | }
|
| 83 | PreviewLine::Graphic { image, align } => {
|
| 84 | let tex = gray_to_texture(
|
| 85 | ctx,
|
| 86 | &format!("{texture_id_prefix}{idx}"),
|
| 87 | image,
|
| 88 | );
|
| 89 | let size = egui::vec2(
|
| 90 | tex.size()[0] as f32 * visual_scale,
|
| 91 | tex.size()[1] as f32 * visual_scale,
|
| 92 | );
|
| 93 | let x = align_x(*align, rect.min.x, inner_width, size.x);
|
| 94 | painter.image(
|
| 95 | tex.id(),
|
| 96 | egui::Rect::from_min_size(egui::pos2(x, y), size),
|
| 97 | egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)),
|
| 98 | egui::Color32::WHITE,
|
| 99 | );
|
| 100 | y += size.y;
|
| 101 | }
|
| 102 | PreviewLine::Error { message, align } => {
|
| 103 | let galley = painter.layout(
|
| 104 | message.clone(),
|
| 105 | egui::FontId::proportional(12.0 * visual_scale),
|
| 106 | egui::Color32::RED,
|
| 107 | inner_width,
|
| 108 | );
|
| 109 | let x = align_x(*align, rect.min.x, inner_width, galley.size().x);
|
| 110 | painter.galley(egui::pos2(x, y), galley, egui::Color32::RED);
|
| 111 | y += preview_line_spacing_px(preview_zoom);
|
| 112 | }
|
| 113 | PreviewLine::Spacer(n) => {
|
| 114 | y += line_spacing * (*n as f32);
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 | });
|
| 119 | });
|
| 120 | }
|
| 121 |
|
| 122 | fn preview_text_font(style: TextStyle, char_width: f32) -> egui::FontId {
|
| 123 | let normal = preview_font_size_px(char_width);
|
| 124 | match style {
|
| 125 | TextStyle::Heading1 => egui::FontId::monospace(normal * 2.0),
|
| 126 | TextStyle::Heading2 | TextStyle::Normal => egui::FontId::monospace(normal),
|
| 127 | }
|
| 128 | }
|
| 129 |
|
| 130 | fn align_x(align: dsl::Align, left: f32, inner_width: f32, item_width: f32) -> f32 {
|
| 131 | match align {
|
| 132 | dsl::Align::Left => left,
|
| 133 | dsl::Align::Center => left + (inner_width - item_width) / 2.0,
|
| 134 | dsl::Align::Right => left + inner_width - item_width,
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | fn gray_to_texture(
|
| 139 | ctx: &egui::Context,
|
| 140 | id: &str,
|
| 141 | image: &image::GrayImage,
|
| 142 | ) -> egui::TextureHandle {
|
| 143 | let (w, h) = image.dimensions();
|
| 144 | let pixels: Vec<egui::Color32> = image
|
| 145 | .pixels()
|
| 146 | .map(|p| egui::Color32::from_gray(p.0[0]))
|
| 147 | .collect();
|
| 148 | let color_image = egui::ColorImage {
|
| 149 | size: [w as usize, h as usize],
|
| 150 | pixels,
|
| 151 | };
|
| 152 | ctx.load_texture(id, color_image, egui::TextureOptions::NEAREST)
|
| 153 | }
|