Use shared Windows paper discovery and retain distinct custom forms

This commit is contained in:
Codex committed 2026-12-02 21:09:53 +01:00
1 parent 9f51978c94
commit c77e8a9c26
3 files changed
+47 -20

No files matched your search

+3 -9
View File
@@ -152,15 +152,8 @@ mod system {
pub fn papers(printer: &str) -> Vec<Paper> {
#[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<Vec<u8>> {
use std::io::Read as _;
use std::process::Stdio;
+18
View File
@@ -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<Printer> {
vec![Printer::new("Forward").is_default(true), Printer::new("Reverse")]
+26 -11
View File
@@ -153,27 +153,42 @@ pub fn ppd_papers(ppd: &str) -> Vec<Paper> {
/// 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<Paper> {
let mut papers: Vec<Paper> = 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::<f64>(), h.parse::<f64>()) 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::<f64>().ok()? * 0.72;
let height = height.parse::<f64>().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<Item = (String, f64, f64)>) -> Vec<Paper> {
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<String> = papers.iter().map(|p| p.key.split('\t').next().unwrap_or_default().to_string()).collect();
let names: Vec<String> = 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<String> =
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| {