From c77e8a9c264d72bbfa5b899fcb1230a545190e06 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 2 Dec 2026 21:09:53 +0100 Subject: [PATCH] Use shared Windows paper discovery and retain distinct custom forms --- apps/vectorcraft/src/printing.rs | 12 ++------ crates/ui-egui/src/dialogs/tests_print.rs | 18 +++++++++++ crates/ui-egui/src/print.rs | 37 ++++++++++++++++------- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/apps/vectorcraft/src/printing.rs b/apps/vectorcraft/src/printing.rs index f81ca95306..860439b147 100644 --- a/apps/vectorcraft/src/printing.rs +++ b/apps/vectorcraft/src/printing.rs @@ -152,15 +152,8 @@ mod system { pub fn papers(printer: &str) -> Vec { #[cfg(windows)] { - // The name goes in as an environment variable, never into the script's text. - const SCRIPT: &str = "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Add-Type -AssemblyName System.Drawing; $s = New-Object System.Drawing.Printing.PrinterSettings; \ - $s.PrinterName = $env:VC_PRINTER; foreach ($p in $s.PaperSizes) { \"$($p.RawKind)`t$($p.PaperName)`t$($p.Width)`t$($p.Height)\" }"; - use std::os::windows::process::CommandExt as _; - // CREATE_NO_WINDOW: no console window opens over the app. - const CREATE_NO_WINDOW: u32 = 0x0800_0000; - let mut command = Command::new("powershell.exe"); - command.args(["-NoProfile", "-NonInteractive", "-Command", SCRIPT]).env("VC_PRINTER", printer).creation_flags(CREATE_NO_WINDOW); - paper_output(&mut command).map(|out| vectorcraft_ui_egui::print::windows_papers(&String::from_utf8_lossy(&out))).unwrap_or_default() + let forms = craft::print::printer_papers(printer); + vectorcraft_ui_egui::print::windows_driver_papers(forms.into_iter().map(|form| (form.name, form.width, form.height))) } #[cfg(not(windows))] { @@ -258,6 +251,7 @@ mod system { }) } /// Drain output while polling the child, and stop discovery after ten seconds. + #[cfg(not(windows))] fn paper_output(command: &mut Command) -> Option> { use std::io::Read as _; use std::process::Stdio; diff --git a/crates/ui-egui/src/dialogs/tests_print.rs b/crates/ui-egui/src/dialogs/tests_print.rs index 0db4091949..b7b0f165c1 100644 --- a/crates/ui-egui/src/dialogs/tests_print.rs +++ b/crates/ui-egui/src/dialogs/tests_print.rs @@ -770,6 +770,24 @@ fn windows_paper_sizes_that_share_a_name_are_all_listed() { struct WindowsForms(&'static str); +#[test] +fn shared_driver_forms_keep_preset_keys_and_distinguish_precise_sizes() { + let rows = [("Custom", 288.0, 432.0), ("Custom", 361.15, 648.0), ("Card", 288.11, 432.0), ("Card", 288.32, 432.0)]; + let choices = + |rows: &[(&str, f64, f64)]| crate::print::windows_driver_papers(rows.iter().map(|&(label, width, height)| (label.to_owned(), width, height))); + let papers = choices(&rows); + assert_eq!(papers.len(), 4); + assert_eq!(papers[0].key, "Custom\t400x600"); + assert_eq!(papers[1].key, "Custom\t502x900"); + assert_ne!(papers[2].key, papers[3].key); + let mut reversed = rows; + reversed.reverse(); + let reordered = choices(&reversed); + for (a, b) in papers.iter().zip(reordered.iter().rev()) { + assert_eq!(a.key, b.key); + } +} + impl PrintService for WindowsForms { fn printers(&mut self) -> Vec { vec![Printer::new("Forward").is_default(true), Printer::new("Reverse")] diff --git a/crates/ui-egui/src/print.rs b/crates/ui-egui/src/print.rs index d1c2c5b803..21bf3d8a43 100644 --- a/crates/ui-egui/src/print.rs +++ b/crates/ui-egui/src/print.rs @@ -153,27 +153,42 @@ pub fn ppd_papers(ppd: &str) -> Vec { /// listed again with another size stays in the list, keyed by its name and size, and every size of that name /// shows its size in inches after the name; one listed again with the same size is left out. pub fn windows_papers(out: &str) -> Vec { - let mut papers: Vec = Vec::new(); - for line in out.lines() { + let forms = out.lines().filter_map(|line| { let fields: Vec<&str> = line.trim_end_matches('\r').split('\t').map(str::trim).collect(); - let [key, label, w, h] = fields[..] else { continue }; - let (Ok(w), Ok(h)) = (w.parse::(), h.parse::()) else { continue }; - if !key.is_empty() { - let label = if label.is_empty() { key } else { label }; - let (width, height) = (w * 0.72, h * 0.72); - let key = format!("{label}\t{w}x{h}"); - push_paper(&mut papers, Paper { key, label: label.into(), width, height }); - } + let [kind, label, width, height] = fields[..] else { return None }; + let width = width.parse::().ok()? * 0.72; + let height = height.parse::().ok()? * 0.72; + let label = if label.is_empty() { kind } else { label }; + (!kind.is_empty()).then(|| (label.to_owned(), width, height)) + }); + windows_driver_papers(forms) +} + +/// Convert named driver forms in points into the choices shown by the Windows print dialog. +pub fn windows_driver_papers(forms: impl IntoIterator) -> Vec { + let mut papers = Vec::new(); + for (label, width, height) in forms { + let key = format!("{label}\t{width}x{height}"); + push_paper(&mut papers, Paper { key, label, width, height }); } // Sizes that share a name show their size in inches after it. // A shown name that is still another paper's gets a number after it. - let names: Vec = papers.iter().map(|p| p.key.split('\t').next().unwrap_or_default().to_string()).collect(); + let names: Vec = papers.iter().map(|p| p.key.rsplit_once('\t').map_or(p.label.as_str(), |(name, _)| name).to_owned()).collect(); + // Older presets identify same-name forms by dimensions in hundredths of an inch. + let legacy_keys: Vec = + papers.iter().zip(&names).map(|(p, name)| format!("{name}\t{}x{}", (p.width / 0.72).round(), (p.height / 0.72).round())).collect(); let inches = |pt: f64| (pt / 0.72).round() / 100.0; for i in 0..papers.len() { if names.iter().filter(|n| **n == names[i]).count() < 2 { papers[i].key = names[i].clone(); continue; } + if legacy_keys.iter().filter(|key| **key == legacy_keys[i]).count() == 1 { + papers[i].key = legacy_keys[i].clone(); + } else { + // Distinct forms can round to the same legacy size. Keep their precise point dimensions. + papers[i].key.push_str("pt"); + } let p = &papers[i]; let suffix = format!(" ({} x {} {})", inches(p.width), inches(p.height), tl!("in")); let with_suffix = |suffix: &str| {