From 628a54b5646ab92e70b1ddafd3000a2367d7bb94 Mon Sep 17 00:00:00 2001 From: Moiztermind Date: Sat, 10 Oct 2026 23:33:35 +0100 Subject: [PATCH 01/39] Print on Windows: printer list, driver Print Settings, GDI printing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - photocraft-winprint: an isolated unsafe crate (like photocraft-tablet) for Windows printing: printer list, the driver's Print Settings dialog, page geometry, and GDI printing with ICM off. - file.print prints on Windows (explicit "send": true only); new file.print.printers and file.print.page commands; printerSettings param. - Print dialog on Windows: Printer menu, Print Settings…, paper from the driver. - panic_hunt fuzzes print commands as dry runs. Closes #2697. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 9 + Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/print_cmds.rs | 110 +++++++- crates/engine/src/print_cmds/tests.rs | 35 +++ crates/engine/tests/panic_hunt.rs | 9 + crates/ui-egui/Cargo.toml | 1 + crates/ui-egui/src/file_ui.rs | 113 +++++++- crates/winprint/Cargo.toml | 26 ++ crates/winprint/src/lib.rs | 263 ++++++++++++++++++ crates/winprint/src/windows.rs | 378 ++++++++++++++++++++++++++ xtask/src/layers.rs | 2 + 12 files changed, 933 insertions(+), 15 deletions(-) create mode 100644 crates/winprint/Cargo.toml create mode 100644 crates/winprint/src/lib.rs create mode 100644 crates/winprint/src/windows.rs diff --git a/Cargo.lock b/Cargo.lock index fde4ac01b8..80cfe99225 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4368,6 +4368,7 @@ dependencies = [ "photocraft-raw", "photocraft-text", "photocraft-vector", + "photocraft-winprint", "rayon", "roxmltree", "serde", @@ -4592,6 +4593,7 @@ dependencies = [ "photocraft-testkit", "photocraft-text", "photocraft-vector", + "photocraft-winprint", "rayon", "serde", "serde_json", @@ -4632,6 +4634,13 @@ dependencies = [ "web-sys", ] +[[package]] +name = "photocraft-winprint" +version = "0.7.0" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "pico-args" version = "0.5.0" diff --git a/Cargo.toml b/Cargo.toml index 7502ff4fd5..ea82e615c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ photocraft-engine = { path = "crates/engine" } photocraft-ui-egui = { path = "crates/ui-egui" } photocraft-testkit = { path = "crates/testkit" } photocraft-tablet = { path = "crates/tablet" } +photocraft-winprint = { path = "crates/winprint" } # external thiserror = "2" diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index fa15c30986..986ce8fc5c 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -19,6 +19,7 @@ photocraft-raster = { workspace = true } photocraft-doc = { workspace = true } photocraft-ops = { workspace = true } photocraft-compose = { workspace = true } +photocraft-winprint = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 59d6b3e1e8..e3e520ff9e 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -353,6 +353,20 @@ fn do_print(s: &mut Session, p: &Value, cmd: &str) -> Result { }; let copies = crate::commands::int(p, "copies").unwrap_or(1).clamp(1, 999); let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false); + // Windows has no `lp`: print through the driver (GDI, ICM off) instead of spooling a PDF. + // Only with an explicit `"send": true` (the dialog's Print button): a bare call, such as a + // fuzzer's or a script's `{}`, never puts paper through a printer. A dry run still renders + // the PDF and reports the `lp` line, as on every platform. + let explicit_send = p.get("send").and_then(Value::as_bool) == Some(true); + if cfg!(windows) && explicit_send && !dry && output.is_none() { + let printed = print_windows(&page, p, &doc.name, copies)?; + remember_print(s, p); + crate::automate_cmds::fire_event(s, "print"); + return Ok(json!({ + "pdf": Value::Null, "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0, + "copies": copies, "command": Value::Null, "sent": true, "spooler": printed, "color": color, + })); + } let pdf = print_pdf(&page); write_file(&pdf_path, &pdf)?; let mut argv: Vec = vec!["lp".into()]; @@ -386,6 +400,80 @@ fn remember_print(s: &mut Session, p: &Value) { s.file_menu.last_print = Some(remembered); } +/// The `printer` parameter, or the system's default printer. +fn printer_name(p: &Value) -> Result { + if let Some(name) = p.get("printer").and_then(Value::as_str).filter(|v| !v.trim().is_empty()) { + return Ok(name.to_string()); + } + photocraft_winprint::default_printer().map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set")) +} + +/// The driver settings chosen with Print Settings… (`printerSettings`, hex), when any. +fn printer_settings(p: &Value) -> Result> { + match p.get("printerSettings").and_then(Value::as_str).filter(|v| !v.is_empty()) { + Some(hex) => photocraft_winprint::DevMode::from_hex(hex).map(Some).map_err(|e| bad("file.print", e.to_string())), + None => Ok(None), + } +} + +/// The page's image as 8-bit RGB rows (gray expanded), mirrored for Emulsion Down. +pub fn page_rgb(page: &PrintPage) -> Result> { + let img = &page.image; + let (w, h) = (img.width as usize, img.height as usize); + let mut rgb = match img.channels { + 3 => img.data.clone(), + 1 => img.data.iter().flat_map(|g| [*g, *g, *g]).collect(), + _ => return Err(other("this printer profile is CMYK; Windows printers take RGB, so choose the printer's own (RGB) profile")), + }; + if rgb.len() != w * h * 3 { + return Err(other("nothing to print")); + } + if page.emulsion_down { + for row in rgb.chunks_exact_mut(w * 3) { + row.as_chunks_mut::<3>().0.reverse(); + } + } + Ok(rgb) +} + +/// The image box with its origin at the sheet's top-left (GDI's), mirrored for Emulsion Down. +pub fn page_rect_top_left(page: &PrintPage) -> (f64, f64, f64, f64) { + let (pw, ph) = page.paper; + let (x, y, w, h) = page.rect; + let x = if page.emulsion_down { pw - x - w } else { x }; + (x, ph - y - h, w, h) +} + +/// Prints `page` on a Windows printer. Printer marks, description and labels are PDF-only. +fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: i64) -> Result { + let printer = printer_name(p)?; + let settings = printer_settings(p)?; + let rgb = page_rgb(page)?; + let image = photocraft_winprint::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; + let job = photocraft_winprint::Job { printer: &printer, settings: settings.as_ref(), title, output_file: None, copies: copies.clamp(1, 999) as u32 }; + let done = photocraft_winprint::print(&job, &image, page_rect_top_left(page)).map_err(other)?; + let marks = page.marks.corner_crop || page.marks.center_crop || page.marks.registration || page.description.is_some() || page.label.is_some(); + Ok( + json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3], "marksSkipped": marks}), + ) +} + +fn printers(_: &mut Session, _: &Value) -> Result { + match photocraft_winprint::printers() { + Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::>()})), + Err(photocraft_winprint::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})), + Err(e) => Err(other(e)), + } +} + +fn printer_page(_: &mut Session, p: &Value) -> Result { + let printer = printer_name(p)?; + let pg = photocraft_winprint::page(&printer, printer_settings(p)?.as_ref()).map_err(other)?; + Ok( + json!({"printer": printer, "paper": [pg.paper.0, pg.paper.1], "printable": [pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3], "dpi": [pg.dpi.0, pg.dpi.1]}), + ) +} + #[cfg(not(target_arch = "wasm32"))] fn spool_pdf(argv: &[String]) -> Result { let out = std::process::Command::new(&argv[0]).args(&argv[1..]).output().map_err(|e| other(format!("could not run `lp` (CUPS): {e}")))?; @@ -567,9 +655,29 @@ pub fn specs() -> Vec { CommandSpec { id: $id, label: $label, menu: $menu, shortcut: $sc, params: $params, enabled: $enabled, journal: true, run: $run } }; } - const PRINT_PARAMS: &str = r##"{"printer":name? (default printer),"copies":1..999=1,"paper":"letter|legal|tabloid|a3|a4|a5|4x6|5x7"|[w,h] pt="letter","orientation":"portrait|landscape"="portrait","center":bool=true,"top":in?,"left":in?,"scale":%=100,"scaleToFit":bool=false,"colorHandling":"printerManages|photocraftManages|noColorManagement"="printerManages","printerProfile":profile? (photocraftManages),"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true,"cornerCropMarks":bool,"centerCropMarks":bool,"registrationMarks":bool,"description":bool,"labels":bool,"emulsionDown":bool=false (mirror the page left to right, for sublimation and other transfer printing),"output":pdf path? (print to PDF; then "send" defaults to false),"send":bool?,"dryRun":bool=false (render the PDF, report the lp command, don't spool)} → {pdf, imageRect, command, sent}"##; + const PRINT_PARAMS: &str = r##"{"printer":name? (default printer),"printerSettings":hex? (the driver settings from Print Settings…, Windows),"copies":1..999=1,"paper":"letter|legal|tabloid|a3|a4|a5|4x6|5x7"|[w,h] pt="letter","orientation":"portrait|landscape"="portrait","center":bool=true,"top":in?,"left":in?,"scale":%=100,"scaleToFit":bool=false,"colorHandling":"printerManages|photocraftManages|noColorManagement"="printerManages","printerProfile":profile? (photocraftManages),"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true,"cornerCropMarks":bool,"centerCropMarks":bool,"registrationMarks":bool,"description":bool,"labels":bool,"emulsionDown":bool=false (mirror the page left to right, for sublimation and other transfer printing),"output":pdf path? (print to PDF; then "send" defaults to false),"send":bool? (Windows prints to the printer only with an explicit true),"dryRun":bool=false (render the PDF, report the lp command, don't spool)} → {pdf, imageRect, command, sent}"##; vec![ spec!("file.print", "Print…", &["File"], Some("Cmd+P"), PRINT_PARAMS, native_doc, print), + CommandSpec { + id: "file.print.printers", + label: "Printers", + menu: &[], + shortcut: None, + params: r##"{} → {supported, printers: [{name, default}]} (supported: Windows; elsewhere Print spools with lp)"##, + enabled: |_| Ok(()), + journal: false, + run: printers, + }, + CommandSpec { + id: "file.print.page", + label: "Printer Page", + menu: &[], + shortcut: None, + params: r##"{"printer":name? (default printer),"printerSettings":hex?} → {printer, paper: [w,h] pt, printable: [x,y,w,h] pt from the top-left, dpi: [x,y]} (Windows)"##, + enabled: |_| Ok(()), + journal: false, + run: printer_page, + }, spec!( "file.printOneCopy", "Print One Copy", diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 877fb945ff..04102eb78c 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -133,6 +133,41 @@ fn emulsion_down_mirrors_the_whole_page() { } } +fn page_of(channels: usize, data: Vec, emulsion_down: bool) -> PrintPage { + PrintPage { + paper: (600.0, 800.0), + image: PrintImage { width: 2, height: 1, channels, data, icc: None }, + rect: (100.0, 200.0, 144.0, 72.0), + marks: Marks::default(), + description: None, + label: None, + emulsion_down, + } +} + +#[test] +fn windows_print_image_is_rgb_mirrored_for_emulsion_down() { + assert_eq!(page_rgb(&page_of(3, vec![1, 2, 3, 4, 5, 6], false)).unwrap(), [1, 2, 3, 4, 5, 6]); + assert_eq!(page_rgb(&page_of(3, vec![1, 2, 3, 4, 5, 6], true)).unwrap(), [4, 5, 6, 1, 2, 3]); + assert_eq!(page_rgb(&page_of(1, vec![7, 9], true)).unwrap(), [9, 9, 9, 7, 7, 7]); + assert!(page_rgb(&page_of(4, vec![0; 8], false)).is_err(), "CMYK can't go to a GDI printer"); + assert!(page_rgb(&page_of(3, vec![0; 5], false)).is_err(), "short data"); + // GDI's origin is the sheet's top-left; Emulsion Down moves the box to the mirrored side. + assert_eq!(page_rect_top_left(&page_of(3, vec![0; 6], false)), (100.0, 528.0, 144.0, 72.0)); + assert_eq!(page_rect_top_left(&page_of(3, vec![0; 6], true)), (356.0, 528.0, 144.0, 72.0)); +} + +#[test] +fn printer_commands_answer_without_a_document_and_reject_bad_settings() { + let mut s = Session::new(); + let r = s.execute("file.print.printers", json!({})).unwrap(); + assert_eq!(r["supported"], cfg!(windows)); + assert!(r["printers"].is_array()); + assert!(s.execute("file.print.page", json!({"printer": "Anything", "printerSettings": "zz"})).is_err()); + let mut s = session("rgb", 8); + assert!(s.execute("file.print", json!({"printer": "Anything", "printerSettings": "0102", "send": true})).is_err()); +} + #[test] fn printer_profile_can_be_an_installed_icc_file() { let dir = tmp("icc"); diff --git a/crates/engine/tests/panic_hunt.rs b/crates/engine/tests/panic_hunt.rs index b301b95389..d3b396f2b8 100644 --- a/crates/engine/tests/panic_hunt.rs +++ b/crates/engine/tests/panic_hunt.rs @@ -61,6 +61,15 @@ fn no_command_panics_or_hangs_on_adversarial_params() { let mut bad: Vec = Vec::new(); for spec in command_specs() { for p in ¶ms { + // Print and Print One Copy reach real printers (lp, or the Windows driver): fuzz them + // as dry runs so the hunt never prints a page. + let mut p = p.clone(); + if spec.id.starts_with("file.print") + && let Some(o) = p.as_object_mut() + { + o.insert("dryRun".into(), json!(true)); + } + let p = &p; if let Err(why) = run_guarded(spec.id, p) { bad.push(format!("{} [{why}] <- {p}", spec.id)); break; diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index a9af622286..1152a36680 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -12,6 +12,7 @@ craft-ui = { workspace = true } photocraft-geom = { workspace = true } photocraft-color = { workspace = true } photocraft-cms = { workspace = true } +photocraft-winprint = { workspace = true } photocraft-raster = { workspace = true } photocraft-doc = { workspace = true } photocraft-compose = { workspace = true } diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index 8b96a51a3d..323519c36b 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -593,9 +593,36 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 { ] { f.entry(k.to_string()).or_insert(v); } + // Windows: the system's printers, and the paper from the chosen printer's driver settings. + if let Ok(list) = photocraft_winprint::printers() { + f.insert("__printers".into(), json!(list.iter().map(|p| p.name.clone()).collect::>())); + let known = |n: &str| list.iter().any(|p| p.name == n); + if !known(&s(&f, "printer", "")) { + let fallback = list.iter().find(|p| p.default).or(list.first()).map(|p| p.name.clone()).unwrap_or_default(); + f.insert("printer".into(), json!(fallback)); + f.remove("printerSettings"); + } + driver_page(&mut f); + } app.ui.open_dialog(DialogKind::Command, f) } +/// Takes the paper from the printer's driver settings (Windows), as Photoshop does. +fn driver_page(f: &mut Map) { + let printer = s(f, "printer", ""); + let settings = f.get("printerSettings").and_then(Value::as_str).and_then(|h| photocraft_winprint::DevMode::from_hex(h).ok()); + match photocraft_winprint::page(&printer, settings.as_ref()) { + Ok(pg) => { + f.insert("paper".into(), json!([pg.paper.0, pg.paper.1])); + f.insert("orientation".into(), json!("portrait")); + f.remove("__pageError"); + } + Err(e) => { + f.insert("__pageError".into(), json!(e.to_string())); + } + } +} + /// Points per unit for the Position and Size fields. fn unit_pt(units: &str) -> f64 { match units { @@ -702,19 +729,61 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map> = + f.get("__printers").and_then(Value::as_array).map(|a| a.iter().filter_map(|v| v.as_str().map(str::to_string)).collect()); + if let Some(printers) = &printers { + // Windows: Photoshop's Printer menu and Print Settings… (the driver's own dialog). + ui.horizontal(|ui| { + ui.label(egui::RichText::new(tl!("Printer")).color(t.text_dim)); + let before = s(f, "printer", ""); + let opts: Vec<(&str, &str)> = printers.iter().map(|n| (n.as_str(), n.as_str())).collect(); + dropdown_str(ui, "print-printer", f, "printer", &opts, 220.0); + if s(f, "printer", "") != before { + f.remove("printerSettings"); + driver_page(f); + } + }); + ui.horizontal(|ui| { + number(ui, f, "copies", tl!("Copies"), 1.0..=999.0, "", 1.0); + if ui.button(tl!("Print Settings…")).clicked() { + let printer = s(f, "printer", ""); + let current = f.get("printerSettings").and_then(Value::as_str).and_then(|h| photocraft_winprint::DevMode::from_hex(h).ok()); + match photocraft_winprint::settings_dialog(&printer, current.as_ref(), None) { + Ok(Some(dm)) => { + f.insert("printerSettings".into(), json!(dm.to_hex())); + driver_page(f); + } + Ok(None) => {} + Err(e) => { + f.insert("__pageError".into(), json!(e.to_string())); + } + } + } + }); + let paper = p.get("paper").and_then(Value::as_array).map(|a| (a.first().and_then(Value::as_f64), a.get(1).and_then(Value::as_f64))); + let text = match paper { + Some((Some(w), Some(h))) => format!("{:.0} × {:.0} mm · {}", w / 72.0 * 25.4, h / 72.0 * 25.4, tl!("set in Print Settings…")), + _ => String::new(), + }; + ui.label(egui::RichText::new(text).color(t.text_dim).size(11.0)); + if let Some(e) = f.get("__pageError").and_then(Value::as_str) { + ui.label(egui::RichText::new(e).color(t.warning).size(11.0)); } - }); - number(ui, f, "copies", tl!("Copies"), 1.0..=999.0, "", 1.0); - ui.horizontal(|ui| { - let papers: Vec<(&str, &str)> = photocraft_engine::print_cmds::PAPERS.iter().map(|p| (p.0, p.0)).collect(); - dropdown_str(ui, "print-paper", f, "paper", &papers, 110.0); - dropdown_str(ui, "print-orient", f, "orientation", &[("portrait", tl!("Portrait")), ("landscape", tl!("Landscape"))], 110.0); - }); + } else { + ui.horizontal(|ui| { + ui.label(egui::RichText::new(tl!("Printer")).color(t.text_dim)); + let mut pr = s(f, "printer", ""); + if ui.add(egui::TextEdit::singleline(&mut pr).hint_text(tl!("Default printer")).desired_width(180.0)).changed() { + f.insert("printer".into(), json!(pr)); + } + }); + number(ui, f, "copies", tl!("Copies"), 1.0..=999.0, "", 1.0); + ui.horizontal(|ui| { + let papers: Vec<(&str, &str)> = photocraft_engine::print_cmds::PAPERS.iter().map(|p| (p.0, p.0)).collect(); + dropdown_str(ui, "print-paper", f, "paper", &papers, 110.0); + dropdown_str(ui, "print-orient", f, "orientation", &[("portrait", tl!("Portrait")), ("landscape", tl!("Landscape"))], 110.0); + }); + } head(ui, tl!("Color Management")); dropdown_str( ui, @@ -766,6 +835,11 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map) -> Result { - let r = app.run("file.print", params(f))?; - app.ui.status = if r["sent"] == json!(true) { + let mut p = params(f); + // The Print button prints (an empty Save as PDF path means the printer). + if s(f, "output", "").is_empty() { + p["send"] = json!(true); + } + let r = app.run("file.print", p)?; + app.ui.status = if let Some(printer) = r["spooler"]["printer"].as_str() { + let skipped = if r["spooler"]["marksSkipped"] == json!(true) { " (printer marks and labels are only in Save as PDF)" } else { "" }; + format!("Sent to {printer}{skipped}") + } else if r["sent"] == json!(true) { let template = "Sent to the printer ({spooler})"; crate::i18n::fmt(tl!(template), &[("spooler", r["spooler"].as_str().unwrap_or(""))]) } else { diff --git a/crates/winprint/Cargo.toml b/crates/winprint/Cargo.toml new file mode 100644 index 0000000000..09504156d5 --- /dev/null +++ b/crates/winprint/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "photocraft-winprint" +version.workspace = true +edition.workspace = true +license.workspace = true +description = "Windows printing: printer list, the driver's Print Settings dialog, page geometry and GDI printing with ICM off" + +# An isolated `unsafe` helper crate like photocraft-tablet (craftrules never-crash, AGENTS.md): +# Windows printing needs Win32 calls. Only `src/windows.rs` allows `unsafe`; every block has a +# `SAFETY:` comment. Elsewhere every function returns `Unsupported`. +[lints.rust] +unsafe_code = "deny" + +[lints.clippy] +dbg_macro = "warn" +todo = "warn" + +# Already in Cargo.lock (winit/wgpu pull it in); pinned exactly: review the diff before bumping. +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "=0.61.2", features = [ + "Win32_Foundation", + "Win32_Graphics_Gdi", + "Win32_Graphics_Printing", + "Win32_Storage_Xps", + "Win32_UI_ColorSystem", +] } diff --git a/crates/winprint/src/lib.rs b/crates/winprint/src/lib.rs new file mode 100644 index 0000000000..fac300a50f --- /dev/null +++ b/crates/winprint/src/lib.rs @@ -0,0 +1,263 @@ +//! Windows printing for File › Print: the system's printers, the driver's own Print Settings +//! dialog (paper, paper type, quality, the driver's colour correction and mirror), the page +//! geometry it implies, and printing an 8-bit RGB image through GDI with ICM off, so colours +//! converted with a printer profile ("PhotoCraft Manages Colors") reach the driver unchanged. +//! +//! The driver settings travel as an opaque DEVMODE blob ([`DevMode`]), validated before use. +//! Everything here is a safe API; the Win32 calls live in `windows.rs`. Other platforms get +//! [`PrintError::Unsupported`]. +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +#[cfg(windows)] +#[allow(unsafe_code)] +mod windows; + +use std::fmt; + +/// Why a printing call failed. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum PrintError { + /// Not on Windows. + Unsupported, + /// Bad input (an unknown printer, a malformed DEVMODE, an empty image). + Invalid(String), + /// A Win32 call failed. + System(String), +} + +impl fmt::Display for PrintError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + PrintError::Unsupported => f.write_str("printing to a printer needs Windows here"), + PrintError::Invalid(m) | PrintError::System(m) => f.write_str(m), + } + } +} + +impl std::error::Error for PrintError {} + +pub type Result = std::result::Result; + +/// One installed printer. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Printer { + pub name: String, + pub default: bool, +} + +/// A printer driver's settings (a Win32 DEVMODEW with the driver's private part), as bytes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct DevMode(Vec); + +/// Offsets inside DEVMODEW: `dmSize` and `dmDriverExtra` follow the 32-wchar device name and +/// two version words. +const DM_SIZE_AT: usize = 68; +const DM_EXTRA_AT: usize = 70; +/// The smallest DEVMODEW a driver writes (Windows 95's, up to `dmICMIntent`). +const DM_MIN: usize = 156; +/// Larger blobs are not driver settings. +const DM_MAX: usize = 1 << 20; + +impl DevMode { + /// Validates driver settings bytes: the header's own sizes must add up to the length. + pub fn from_bytes(b: Vec) -> Result { + let word = |at: usize| b.get(at..at + 2).map(|w| usize::from(u16::from_le_bytes([w[0], w[1]]))); + let (Some(size), Some(extra)) = (word(DM_SIZE_AT), word(DM_EXTRA_AT)) else { + return Err(PrintError::Invalid("printer settings are too short".into())); + }; + if b.len() > DM_MAX || size < DM_MIN || size.checked_add(extra) != Some(b.len()) { + return Err(PrintError::Invalid("printer settings are damaged; choose Print Settings again".into())); + } + Ok(DevMode(b)) + } + + pub fn as_bytes(&self) -> &[u8] { + &self.0 + } + + /// Hex, for JSON parameters and remembered print settings. + pub fn to_hex(&self) -> String { + self.0.iter().map(|b| format!("{b:02x}")).collect() + } + + pub fn from_hex(s: &str) -> Result { + if !s.len().is_multiple_of(2) || s.len() > DM_MAX * 2 { + return Err(PrintError::Invalid("printer settings are damaged; choose Print Settings again".into())); + } + let bytes: Option> = (0..s.len()).step_by(2).map(|i| s.get(i..i + 2).and_then(|h| u8::from_str_radix(h, 16).ok())).collect(); + Self::from_bytes(bytes.ok_or_else(|| PrintError::Invalid("printer settings are damaged; choose Print Settings again".into()))?) + } +} + +/// What a printer will print on with given settings. Points (1/72 in), origin top-left. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Page { + /// The whole sheet. + pub paper: (f64, f64), + /// The area the printer can reach: (x, y, width, height). + pub printable: (f64, f64, f64, f64), + /// Device resolution (dots per inch). + pub dpi: (u32, u32), +} + +/// An 8-bit RGB image, rows top to bottom, `width * 3` bytes each. +pub struct RgbImage<'a> { + pub width: u32, + pub height: u32, + pub data: &'a [u8], +} + +/// One print job. +pub struct Job<'a> { + pub printer: &'a str, + pub settings: Option<&'a DevMode>, + /// The name in the print queue. + pub title: &'a str, + /// Print to this file instead of the device (drivers that write files, and tests). + pub output_file: Option<&'a str>, + pub copies: u32, +} + +/// Where a job went: the image box in device pixels and the device resolution. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Printed { + pub device_rect: (i32, i32, i32, i32), + pub dpi: (u32, u32), +} + +/// Maps an image box in points (origin top-left of the sheet) to device pixels (origin at the +/// printable area's corner, as GDI draws). +pub fn device_rect(page: &Page, rect: (f64, f64, f64, f64)) -> (i32, i32, i32, i32) { + let (dx, dy) = (f64::from(page.dpi.0) / 72.0, f64::from(page.dpi.1) / 72.0); + let px = |v: f64| if v.is_finite() { v.round().clamp(-1e8, 1e8) as i32 } else { 0 }; + let (x, y, w, h) = rect; + (px((x - page.printable.0) * dx), px((y - page.printable.1) * dy), px(w * dx), px(h * dy)) +} + +/// The printers installed for this user, the default first. +pub fn printers() -> Result> { + #[cfg(windows)] + { + let mut v = windows::printers()?; + v.sort_by(|a, b| b.default.cmp(&a.default).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase()))); + Ok(v) + } + #[cfg(not(windows))] + Err(PrintError::Unsupported) +} + +/// The default printer's name. +pub fn default_printer() -> Result> { + #[cfg(windows)] + { + windows::default_printer() + } + #[cfg(not(windows))] + Err(PrintError::Unsupported) +} + +/// A printer's settings as its driver starts them. +pub fn default_settings(printer: &str) -> Result { + #[cfg(windows)] + { + windows::properties(printer, None, None, false)?.ok_or_else(|| PrintError::System("the printer driver gave no settings".into())) + } + #[cfg(not(windows))] + { + let _ = printer; + Err(PrintError::Unsupported) + } +} + +/// Shows the driver's Print Settings dialog (modal; `parent` is a window handle). `None` when +/// the user cancels. +pub fn settings_dialog(printer: &str, current: Option<&DevMode>, parent: Option) -> Result> { + #[cfg(windows)] + { + windows::properties(printer, current, parent, true) + } + #[cfg(not(windows))] + { + let _ = (printer, current, parent); + Err(PrintError::Unsupported) + } +} + +/// The sheet and printable area for `printer` with `settings`. +pub fn page(printer: &str, settings: Option<&DevMode>) -> Result { + #[cfg(windows)] + { + windows::page(printer, settings) + } + #[cfg(not(windows))] + { + let _ = (printer, settings); + Err(PrintError::Unsupported) + } +} + +/// Prints `image` into `rect` (points, origin top-left of the sheet), `job.copies` pages. +pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> Result { + let row = (image.width as usize).checked_mul(3).ok_or_else(|| PrintError::Invalid("image too large".into()))?; + if image.width == 0 || image.height == 0 || row.checked_mul(image.height as usize) != Some(image.data.len()) { + return Err(PrintError::Invalid("nothing to print".into())); + } + if !(rect.2 > 0.0 && rect.3 > 0.0) { + return Err(PrintError::Invalid("the print size is empty".into())); + } + #[cfg(windows)] + { + windows::print(job, image, rect) + } + #[cfg(not(windows))] + { + let _ = job; + Err(PrintError::Unsupported) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn devmode(size: u16, extra: u16) -> Vec { + let mut b = vec![0u8; usize::from(size) + usize::from(extra)]; + b[DM_SIZE_AT..DM_SIZE_AT + 2].copy_from_slice(&size.to_le_bytes()); + b[DM_EXTRA_AT..DM_EXTRA_AT + 2].copy_from_slice(&extra.to_le_bytes()); + b + } + + #[test] + fn devmode_checks_its_own_sizes_and_round_trips_hex() { + let d = DevMode::from_bytes(devmode(220, 1200)).unwrap(); + assert_eq!(DevMode::from_hex(&d.to_hex()).unwrap(), d); + for bad in [vec![], vec![0; 40], devmode(100, 0), { + let mut b = devmode(220, 10); + b.pop(); + b + }] { + assert!(DevMode::from_bytes(bad).is_err()); + } + for bad in ["", "abc", "zz", &"00".repeat(300)] { + assert!(DevMode::from_hex(bad).is_err(), "{bad}"); + } + } + + #[test] + fn device_rect_is_relative_to_the_printable_corner() { + // A3 at 360 dpi with a 3 mm (8.5 pt) unprintable margin. + let page = Page { paper: (841.89, 1190.55), printable: (8.5, 8.5, 824.89, 1173.55), dpi: (360, 360) }; + assert_eq!(device_rect(&page, (8.5, 8.5, 72.0, 36.0)), (0, 0, 360, 180)); + assert_eq!(device_rect(&page, (72.0, 144.0, 144.0, 72.0)), (318, 678, 720, 360)); + assert_eq!(device_rect(&page, (f64::NAN, f64::INFINITY, 1.0, 1.0)).0, 0); + } + + #[test] + fn print_rejects_empty_or_short_images_before_any_system_call() { + let job = Job { printer: "x", settings: None, title: "t", output_file: None, copies: 1 }; + let short = RgbImage { width: 2, height: 2, data: &[0; 11] }; + assert!(matches!(print(&job, &short, (0.0, 0.0, 10.0, 10.0)), Err(PrintError::Invalid(_)))); + let ok = RgbImage { width: 2, height: 2, data: &[0; 12] }; + assert!(matches!(print(&job, &ok, (0.0, 0.0, 0.0, 10.0)), Err(PrintError::Invalid(_)))); + } +} diff --git a/crates/winprint/src/windows.rs b/crates/winprint/src/windows.rs new file mode 100644 index 0000000000..104333cf03 --- /dev/null +++ b/crates/winprint/src/windows.rs @@ -0,0 +1,378 @@ +//! The Win32 side: winspool (printers, driver settings) and GDI (page geometry, printing). +//! Every `unsafe` block says why it is sound. Buffers the system writes into are `u64`-backed so +//! the structures read from them are aligned. + +use std::ptr::{null, null_mut}; + +use windows_sys::Win32::Foundation::HWND; +use windows_sys::Win32::Graphics::Gdi::{ + BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateDCW, DEVMODEW, DIB_RGB_COLORS, DM_IN_BUFFER, DM_IN_PROMPT, DM_OUT_BUFFER, DeleteDC, GetDeviceCaps, HALFTONE, + HDC, HORZRES, LOGPIXELSX, LOGPIXELSY, PHYSICALHEIGHT, PHYSICALOFFSETX, PHYSICALOFFSETY, PHYSICALWIDTH, SRCCOPY, SetBrushOrgEx, SetStretchBltMode, + StretchDIBits, VERTRES, +}; +use windows_sys::Win32::Graphics::Printing::{ + ClosePrinter, DocumentPropertiesW, EnumPrintersW, GetDefaultPrinterW, OpenPrinterW, PRINTER_ENUM_CONNECTIONS, PRINTER_ENUM_LOCAL, PRINTER_HANDLE, + PRINTER_INFO_4W, +}; +use windows_sys::Win32::Storage::Xps::{AbortDoc, DOCINFOW, EndDoc, EndPage, StartDocW, StartPage}; +use windows_sys::Win32::UI::ColorSystem::{ICM_OFF, SetICMMode}; + +use crate::{DevMode, Job, Page, PrintError, Printed, Printer, Result, RgbImage}; + +const IDOK: i32 = 1; +const IDCANCEL: i32 = 2; +/// Wide strings read from the system stop here at the latest. +const MAX_WCHARS: usize = 32 * 1024; + +fn last_error(what: &str) -> PrintError { + PrintError::System(format!("{what} failed: {}", std::io::Error::last_os_error())) +} + +fn wide(s: &str) -> Result> { + if s.contains('\0') { + return Err(PrintError::Invalid("a name contains a NUL character".into())); + } + Ok(s.encode_utf16().chain(Some(0)).collect()) +} + +/// A zeroed, 8-byte aligned buffer of at least `bytes` bytes. +fn aligned(bytes: usize) -> Vec { + vec![0u64; bytes.div_ceil(8).max(1)] +} + +fn bytes_of(buf: &[u64], len: usize) -> Vec { + buf.iter().flat_map(|w| w.to_le_bytes()).take(len).collect() +} + +/// Reads a NUL-terminated wide string. +/// +/// # Safety +/// `p` is null or points to a NUL-terminated UTF-16 string that stays alive for the call. +unsafe fn read_wide(p: *const u16) -> String { + if p.is_null() { + return String::new(); + } + let mut n = 0; + // SAFETY: the caller guarantees a NUL-terminated string; we stop at the NUL (or the cap). + while n < MAX_WCHARS && unsafe { *p.add(n) } != 0 { + n += 1; + } + // SAFETY: the `n` units before the NUL were just read and are valid. + String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(p, n) }) +} + +pub fn printers() -> Result> { + let flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; + let (mut needed, mut count) = (0u32, 0u32); + // SAFETY: a size query: no buffer (null, 0 bytes); the out-params are valid locals. + unsafe { EnumPrintersW(flags, null(), 4, null_mut(), 0, &mut needed, &mut count) }; + if needed == 0 { + return Ok(Vec::new()); + } + let mut buf = aligned(needed as usize); + // SAFETY: `buf` holds at least `needed` writable bytes, aligned for PRINTER_INFO_4W. + let ok = unsafe { EnumPrintersW(flags, null(), 4, buf.as_mut_ptr().cast(), needed, &mut needed, &mut count) }; + if ok == 0 { + return Err(last_error("listing printers")); + } + let default = default_printer().ok().flatten(); + let infos = buf.as_ptr().cast::(); + let fits = (needed as usize) / std::mem::size_of::(); + let mut out = Vec::new(); + for i in 0..(count as usize).min(fits) { + // SAFETY: the system wrote `count` PRINTER_INFO_4W at the start of `buf` (bounded by + // the buffer size above); their name pointers point into `buf`, which is alive. + let name = unsafe { read_wide((*infos.add(i)).pPrinterName) }; + if !name.is_empty() { + out.push(Printer { default: default.as_deref() == Some(name.as_str()), name }); + } + } + Ok(out) +} + +pub fn default_printer() -> Result> { + let mut len = 0u32; + // SAFETY: a size query with a null buffer; `len` is a valid local. + unsafe { GetDefaultPrinterW(null_mut(), &mut len) }; + if len == 0 || len as usize > MAX_WCHARS { + return Ok(None); + } + let mut buf = vec![0u16; len as usize]; + // SAFETY: `buf` holds `len` writable wide chars. + if unsafe { GetDefaultPrinterW(buf.as_mut_ptr(), &mut len) } == 0 { + return Ok(None); + } + // SAFETY: on success the buffer holds a NUL-terminated name. + Ok(Some(unsafe { read_wide(buf.as_ptr()) })) +} + +/// An open printer handle, closed on drop. +struct OpenPrinter(PRINTER_HANDLE); + +impl OpenPrinter { + fn open(name: &[u16]) -> Result { + let mut h = PRINTER_HANDLE { Value: null_mut() }; + // SAFETY: `name` is NUL-terminated; `h` is a valid out-param; no defaults. + if unsafe { OpenPrinterW(name.as_ptr(), &mut h, null()) } == 0 || h.Value.is_null() { + return Err(last_error("opening the printer")); + } + Ok(OpenPrinter(h)) + } +} + +impl Drop for OpenPrinter { + fn drop(&mut self) { + // SAFETY: the handle came from OpenPrinterW and is closed once. + unsafe { ClosePrinter(self.0) }; + } +} + +/// Settings the driver can take as input: `current`, copied aligned, when it is this printer's. +fn input_settings(printer: &str, current: Option<&DevMode>) -> Option> { + let d = current?; + let b = d.as_bytes(); + // dmDeviceName: the first 32 wide chars (31 + NUL), so long names are truncated. + let device: Vec = b.get(..64)?.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).take_while(|c| *c != 0).collect(); + let want: Vec = printer.encode_utf16().take(31).collect(); + if device != want { + return None; + } + let mut buf = aligned(b.len()); + for (i, chunk) in b.chunks(8).enumerate() { + let mut w = [0u8; 8]; + w[..chunk.len()].copy_from_slice(chunk); + if let Some(slot) = buf.get_mut(i) { + *slot = u64::from_le_bytes(w); + } + } + Some(buf) +} + +pub fn properties(printer: &str, current: Option<&DevMode>, parent: Option, prompt: bool) -> Result> { + let name = wide(printer)?; + let p = OpenPrinter::open(&name)?; + let hwnd: HWND = parent.map_or(null_mut(), |h| h as HWND); + // SAFETY: with fmode 0 DocumentPropertiesW only returns the size the settings need. + let needed = unsafe { DocumentPropertiesW(hwnd, p.0, name.as_ptr(), null_mut(), null(), 0) }; + if needed <= 0 || needed as usize > (1 << 20) { + return Err(last_error("reading the printer's settings")); + } + let mut out = aligned(needed as usize); + let input = input_settings(printer, current); + let mut mode = DM_OUT_BUFFER; + if input.is_some() { + mode |= DM_IN_BUFFER; + } + if prompt { + mode |= DM_IN_PROMPT; + } + let inp: *const DEVMODEW = input.as_ref().map_or(null(), |b| b.as_ptr().cast()); + // SAFETY: `out` holds `needed` aligned writable bytes as the size query asked for; `inp` is + // null or an aligned copy of settings written by this printer's driver. + let r = unsafe { DocumentPropertiesW(hwnd, p.0, name.as_ptr(), out.as_mut_ptr().cast(), inp, mode) }; + match r { + IDOK => { + let all = bytes_of(&out, needed as usize); + let word = |at: usize| all.get(at..at + 2).map_or(0, |w| usize::from(u16::from_le_bytes([w[0], w[1]]))); + let len = (word(68) + word(70)).min(all.len()); + DevMode::from_bytes(all.into_iter().take(len).collect()).map(Some) + } + IDCANCEL => Ok(None), + _ => Err(last_error("the printer's settings dialog")), + } +} + +/// A printer device context, deleted on drop. +struct Dc(HDC); + +impl Dc { + fn create(printer: &str, settings: Option<&DevMode>) -> Result { + let name = wide(printer)?; + let dm = input_settings(printer, settings); + let dmp: *const DEVMODEW = dm.as_ref().map_or(null(), |b| b.as_ptr().cast()); + // SAFETY: `name` is NUL-terminated; `dmp` is null or aligned settings from this + // printer's driver, alive for the call. + let hdc = unsafe { CreateDCW(null(), name.as_ptr(), null(), dmp) }; + if hdc.is_null() { + return Err(last_error(&format!("opening printer “{printer}”"))); + } + Ok(Dc(hdc)) + } + + fn cap(&self, index: u32) -> i32 { + // SAFETY: a valid printer DC and a documented capability index. + unsafe { GetDeviceCaps(self.0, index as i32) } + } + + fn page(&self) -> Result { + let (dx, dy) = (self.cap(LOGPIXELSX), self.cap(LOGPIXELSY)); + if dx <= 0 || dy <= 0 { + return Err(PrintError::System("the printer reported no resolution".into())); + } + let (fx, fy) = (72.0 / f64::from(dx), 72.0 / f64::from(dy)); + let (pw, ph) = (self.cap(PHYSICALWIDTH), self.cap(PHYSICALHEIGHT)); + let (ox, oy) = (self.cap(PHYSICALOFFSETX), self.cap(PHYSICALOFFSETY)); + let (hw, vh) = (self.cap(HORZRES), self.cap(VERTRES)); + let (pw, ph) = if pw > 0 && ph > 0 { (pw, ph) } else { (hw, vh) }; + Ok(Page { + paper: (f64::from(pw) * fx, f64::from(ph) * fy), + printable: (f64::from(ox) * fx, f64::from(oy) * fy, f64::from(hw) * fx, f64::from(vh) * fy), + dpi: (dx as u32, dy as u32), + }) + } +} + +impl Drop for Dc { + fn drop(&mut self) { + // SAFETY: the DC came from CreateDCW and is deleted once. + unsafe { DeleteDC(self.0) }; + } +} + +pub fn page(printer: &str, settings: Option<&DevMode>) -> Result { + Dc::create(printer, settings)?.page() +} + +/// RGB rows top-down → a bottom-up 24-bit DIB (BGR, rows padded to 4 bytes). +fn dib(image: &RgbImage<'_>) -> Result<(BITMAPINFO, Vec)> { + let (w, h) = (image.width as usize, image.height as usize); + let stride = (w.checked_mul(3).ok_or_else(|| PrintError::Invalid("image too large".into()))? + 3) & !3; + let total = stride.checked_mul(h).filter(|t| *t <= i32::MAX as usize).ok_or_else(|| PrintError::Invalid("image too large to print in one piece".into()))?; + let mut bits = vec![0u8; total]; + for (y, src) in image.data.chunks_exact(w * 3).enumerate() { + let start = (h - 1 - y) * stride; + let Some(dst) = bits.get_mut(start..start + w * 3) else { continue }; + for (d, s) in dst.as_chunks_mut::<3>().0.iter_mut().zip(src.as_chunks::<3>().0) { + *d = [s[2], s[1], s[0]]; + } + } + let header = BITMAPINFOHEADER { + biSize: std::mem::size_of::() as u32, + biWidth: image.width as i32, + biHeight: image.height as i32, + biPlanes: 1, + biBitCount: 24, + biCompression: BI_RGB, + biSizeImage: total as u32, + ..Default::default() + }; + Ok((BITMAPINFO { bmiHeader: header, bmiColors: [Default::default()] }, bits)) +} + +pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> Result { + if image.width > i32::MAX as u32 || image.height > i32::MAX as u32 { + return Err(PrintError::Invalid("image too large".into())); + } + let dc = Dc::create(job.printer, job.settings)?; + let page = dc.page()?; + let (x, y, w, h) = crate::device_rect(&page, rect); + let (bmi, bits) = dib(image)?; + let title = wide(job.title)?; + let output = job.output_file.map(wide).transpose()?; + let info = DOCINFOW { + cbSize: std::mem::size_of::() as i32, + lpszDocName: title.as_ptr(), + lpszOutput: output.as_ref().map_or(null(), |o| o.as_ptr()), + lpszDatatype: null(), + fwType: 0, + }; + // SAFETY: a valid printer DC. ICM off: the pixels are already in the printer's space + // (or deliberately unmanaged), so GDI must not convert them again. + unsafe { SetICMMode(dc.0, ICM_OFF) }; + // SAFETY: a valid DC; HALFTONE resampling needs the brush origin reset afterwards. + unsafe { + SetStretchBltMode(dc.0, HALFTONE); + SetBrushOrgEx(dc.0, 0, 0, null_mut()); + } + // SAFETY: `info` and the strings it points to live until EndDoc/AbortDoc below. + if unsafe { StartDocW(dc.0, &info) } <= 0 { + return Err(last_error("starting the print job")); + } + let fail = |what: &str| { + let e = last_error(what); + // SAFETY: the job was started on this DC. + unsafe { AbortDoc(dc.0) }; + e + }; + for _ in 0..job.copies.clamp(1, 999) { + // SAFETY: inside a started document on a valid DC. + if unsafe { StartPage(dc.0) } <= 0 { + return Err(fail("starting a page")); + } + // SAFETY: `bits` is a bottom-up DIB exactly as `bmi` describes (24-bit, padded rows, + // `biSizeImage` bytes); the source rectangle is the whole image. + let lines = + unsafe { StretchDIBits(dc.0, x, y, w, h, 0, 0, image.width as i32, image.height as i32, bits.as_ptr().cast(), &bmi, DIB_RGB_COLORS, SRCCOPY) }; + if lines <= 0 { + return Err(fail("sending the image to the printer")); + } + // SAFETY: closes the page started above. + if unsafe { EndPage(dc.0) } <= 0 { + return Err(fail("finishing a page")); + } + } + // SAFETY: closes the document started above. + if unsafe { EndDoc(dc.0) } <= 0 { + return Err(last_error("finishing the print job")); + } + Ok(Printed { device_rect: (x, y, w, h), dpi: page.dpi }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dib_is_bottom_up_bgr_with_padded_rows() { + // 2×2: red, green / blue, white. + let data = [255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255]; + let (bmi, bits) = dib(&RgbImage { width: 2, height: 2, data: &data }).unwrap(); + assert_eq!(bmi.bmiHeader.biHeight, 2); + assert_eq!(bits.len(), 16, "6 bytes per row padded to 8"); + // First stored row is the bottom one: blue, white. + assert_eq!(&bits[0..6], &[255, 0, 0, 255, 255, 255]); + assert_eq!(&bits[8..14], &[0, 0, 255, 0, 255, 0]); + } + + #[test] + fn settings_from_another_printer_are_not_passed_to_the_driver() { + let mut b = vec![0u8; 220]; + for (i, c) in "Printer A".encode_utf16().enumerate() { + b[i * 2..i * 2 + 2].copy_from_slice(&c.to_le_bytes()); + } + b[68..70].copy_from_slice(&220u16.to_le_bytes()); + let d = DevMode::from_bytes(b).unwrap(); + assert!(input_settings("Printer A", Some(&d)).is_some()); + assert!(input_settings("Printer B", Some(&d)).is_none()); + assert!(input_settings("Printer A", None).is_none()); + } + + /// A real job through Windows' "Microsoft Print to PDF" driver, written to a file (needs that + /// printer installed; run by hand). + #[test] + #[ignore] + fn prints_through_a_real_driver_to_a_file() { + let out = std::env::temp_dir().join(format!("pc-winprint-{}.pdf", std::process::id())); + let _ = std::fs::remove_file(&out); + let settings = crate::default_settings("Microsoft Print to PDF").unwrap(); + let (w, h) = (300u32, 200u32); + let data: Vec = (0..w * h).flat_map(|i| if (i % w) < w / 2 { [220, 30, 30] } else { [30, 30, 220] }).collect(); + let out_s = out.to_string_lossy().into_owned(); + let job = Job { printer: "Microsoft Print to PDF", settings: Some(&settings), title: "PhotoCraft test", output_file: Some(&out_s), copies: 2 }; + let done = crate::print(&job, &RgbImage { width: w, height: h, data: &data }, (72.0, 72.0, 216.0, 144.0)).unwrap(); + assert_eq!(done.device_rect.2, done.dpi.0 as i32 * 3, "3 inches wide"); + let pdf = std::fs::read(&out).unwrap(); + assert!(pdf.starts_with(b"%PDF"), "the driver wrote a PDF"); + assert!(pdf.len() > 1000, "the page has content"); + let _ = std::fs::remove_file(&out); + } + + /// Lists this machine's printers and their pages (needs real printers; run by hand). + #[test] + #[ignore] + fn lists_real_printers() { + for p in printers().unwrap() { + println!("{}{} {:?}", p.name, if p.default { " (default)" } else { "" }, page(&p.name, None)); + } + } +} diff --git a/xtask/src/layers.rs b/xtask/src/layers.rs index 2128345148..dcb66f7120 100644 --- a/xtask/src/layers.rs +++ b/xtask/src/layers.rs @@ -46,6 +46,8 @@ pub const TABLE: &[(&str, Class)] = &[ ("adobe-assets", Class::Standalone), // Pen tablet input (the one isolated `unsafe` helper: AppKit interop on macOS). ("tablet", Class::Standalone), + // Windows printing (an isolated `unsafe` helper: winspool and GDI). + ("winprint", Class::Standalone), ("doc", Class::Layer(1)), ("ops", Class::Layer(2)), ("paint", Class::Layer(2)), From ce8d8d950bd1b2e7908af7e41d51c2949f377f3b Mon Sep 17 00:00:00 2001 From: Moiztermind Date: Sun, 11 Oct 2026 15:10:57 +0100 Subject: [PATCH 02/39] winprint: let the driver validate settings; never print on defaults silently The settings check compared the printer name stored in the DEVMODE with the queue name. Windows keeps only 31 characters of it, so a long queue name ("Sublimation - EPSON ET-18100 Series") failed the check and the chosen paper type and quality were silently dropped: the job printed on the driver defaults (plain paper). Settings now go through DocumentProperties(DM_IN_BUFFER | DM_OUT_BUFFER) before CreateDC, so the driver validates them itself, and a refusal is an error instead of a silent fallback. Co-Authored-By: Claude Opus 5.5 --- crates/winprint/src/windows.rs | 79 +++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 26 deletions(-) diff --git a/crates/winprint/src/windows.rs b/crates/winprint/src/windows.rs index 104333cf03..9b8f8ba16e 100644 --- a/crates/winprint/src/windows.rs +++ b/crates/winprint/src/windows.rs @@ -127,25 +127,21 @@ impl Drop for OpenPrinter { } } -/// Settings the driver can take as input: `current`, copied aligned, when it is this printer's. -fn input_settings(printer: &str, current: Option<&DevMode>) -> Option> { - let d = current?; - let b = d.as_bytes(); - // dmDeviceName: the first 32 wide chars (31 + NUL), so long names are truncated. - let device: Vec = b.get(..64)?.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).take_while(|c| *c != 0).collect(); - let want: Vec = printer.encode_utf16().take(31).collect(); - if device != want { - return None; - } +/// `settings` copied into an aligned buffer the driver can read. +/// +/// There is deliberately no check of the printer name stored in the settings: Windows keeps only +/// 31 characters of it, and drivers fill it differently, so a long queue name ("Sublimation - +/// EPSON ET-18100 Series") failed such a check and the settings were silently dropped. The driver +/// itself validates them instead (see [`Dc::create`]). +fn aligned_copy(settings: &DevMode) -> Vec { + let b = settings.as_bytes(); let mut buf = aligned(b.len()); - for (i, chunk) in b.chunks(8).enumerate() { + for (slot, chunk) in buf.iter_mut().zip(b.chunks(8)) { let mut w = [0u8; 8]; w[..chunk.len()].copy_from_slice(chunk); - if let Some(slot) = buf.get_mut(i) { - *slot = u64::from_le_bytes(w); - } + *slot = u64::from_le_bytes(w); } - Some(buf) + buf } pub fn properties(printer: &str, current: Option<&DevMode>, parent: Option, prompt: bool) -> Result> { @@ -158,7 +154,7 @@ pub fn properties(printer: &str, current: Option<&DevMode>, parent: Option) -> Result { let name = wide(printer)?; - let dm = input_settings(printer, settings); + // The driver validates and completes the settings first (DM_IN_BUFFER | DM_OUT_BUFFER, no + // dialog). If it refuses them, that is an error: never print on the defaults instead. + let merged = match settings { + Some(s) => Some( + properties(printer, Some(s), None, false)? + .ok_or_else(|| PrintError::Invalid(format!("printer “{printer}” refused these settings; choose Print Settings… again")))?, + ), + None => None, + }; + let dm = merged.as_ref().map(aligned_copy); let dmp: *const DEVMODEW = dm.as_ref().map_or(null(), |b| b.as_ptr().cast()); // SAFETY: `name` is NUL-terminated; `dmp` is null or aligned settings from this // printer's driver, alive for the call. @@ -335,16 +340,38 @@ mod tests { } #[test] - fn settings_from_another_printer_are_not_passed_to_the_driver() { - let mut b = vec![0u8; 220]; - for (i, c) in "Printer A".encode_utf16().enumerate() { - b[i * 2..i * 2 + 2].copy_from_slice(&c.to_le_bytes()); - } + fn settings_are_copied_aligned_and_intact() { + let mut b: Vec = (0..230u32).map(|i| (i * 7) as u8).collect(); b[68..70].copy_from_slice(&220u16.to_le_bytes()); - let d = DevMode::from_bytes(b).unwrap(); - assert!(input_settings("Printer A", Some(&d)).is_some()); - assert!(input_settings("Printer B", Some(&d)).is_none()); - assert!(input_settings("Printer A", None).is_none()); + b[70..72].copy_from_slice(&10u16.to_le_bytes()); + let d = DevMode::from_bytes(b.clone()).unwrap(); + let buf = aligned_copy(&d); + assert_eq!(buf.as_ptr().align_offset(std::mem::align_of::()), 0); + assert_eq!(bytes_of(&buf, b.len()), b, "no name check, no truncation: the bytes go through as chosen"); + } + + /// Settings chosen for a printer are what the driver prints with: paper and resolution survive + /// the driver's own validation (needs "Microsoft Print to PDF"; run by hand). + #[test] + #[ignore] + fn chosen_settings_reach_the_device_context() { + let printer = "Microsoft Print to PDF"; + let d = crate::default_settings(printer).unwrap(); + let mut b = d.as_bytes().to_vec(); + // dmPaperSize = DMPAPER_A3 (8); dmFields |= DM_PAPERSIZE (2). + b[78..80].copy_from_slice(&8i16.to_le_bytes()); + let fields = u32::from_le_bytes([b[72], b[73], b[74], b[75]]) | 2; + b[72..76].copy_from_slice(&fields.to_le_bytes()); + let a3 = DevMode::from_bytes(b).unwrap(); + let pg = crate::page(printer, Some(&a3)).unwrap(); + assert!((pg.paper.0 - 841.9).abs() < 2.0 && (pg.paper.1 - 1190.6).abs() < 2.0, "A3, not the default: {:?}", pg.paper); + // Landscape (dmOrientation = 2, DM_ORIENTATION = 1): the page comes back wide. + let mut b = a3.as_bytes().to_vec(); + b[76..78].copy_from_slice(&2i16.to_le_bytes()); + let fields = u32::from_le_bytes([b[72], b[73], b[74], b[75]]) | 1; + b[72..76].copy_from_slice(&fields.to_le_bytes()); + let pg = crate::page(printer, Some(&DevMode::from_bytes(b).unwrap())).unwrap(); + assert!((pg.paper.0 - 1190.6).abs() < 2.0 && (pg.paper.1 - 841.9).abs() < 2.0, "landscape A3: {:?}", pg.paper); } /// A real job through Windows' "Microsoft Print to PDF" driver, written to a file (needs that From f9b3c6bd85cf92f266fb22df22354ab3eb0d83f2 Mon Sep 17 00:00:00 2001 From: moiztermind Date: Sun, 11 Oct 2026 19:23:18 +0100 Subject: [PATCH 03/39] winprint: draw crop and registration marks on GDI prints Printer marks (corner/centre crop, registration) were silently dropped on Windows GDI prints -- only the PDF export path drew them, as content-stream vector operators. A workshop test print surfaced the gap. Adds a Marks type to photocraft-winprint and draws the same geometry GDI's way (MoveToEx/LineTo for lines, Ellipse for registration circles) once per page between StretchDIBits and EndPage. The JSON field marksSkipped becomes textSkipped: description and label text are still PDF-only (no font layout in the GDI path yet). Co-Authored-By: Claude Sonnet 5 --- crates/engine/src/print_cmds.rs | 11 +++-- crates/ui-egui/src/file_ui.rs | 8 +++- crates/winprint/src/lib.rs | 23 +++++++++- crates/winprint/src/windows.rs | 79 ++++++++++++++++++++++++++++++--- 4 files changed, 109 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index e3e520ff9e..1b05a6511a 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -444,17 +444,20 @@ pub fn page_rect_top_left(page: &PrintPage) -> (f64, f64, f64, f64) { (x, ph - y - h, w, h) } -/// Prints `page` on a Windows printer. Printer marks, description and labels are PDF-only. +/// Prints `page` on a Windows printer. Corner/centre crop and registration marks are drawn with +/// GDI vector calls, matching the PDF path's geometry; description and label text are still +/// PDF-only (no font/text layout in the GDI path yet). fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: i64) -> Result { let printer = printer_name(p)?; let settings = printer_settings(p)?; let rgb = page_rgb(page)?; let image = photocraft_winprint::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; - let job = photocraft_winprint::Job { printer: &printer, settings: settings.as_ref(), title, output_file: None, copies: copies.clamp(1, 999) as u32 }; + let marks = photocraft_winprint::Marks { corner_crop: page.marks.corner_crop, center_crop: page.marks.center_crop, registration: page.marks.registration }; + let job = photocraft_winprint::Job { printer: &printer, settings: settings.as_ref(), title, output_file: None, copies: copies.clamp(1, 999) as u32, marks }; let done = photocraft_winprint::print(&job, &image, page_rect_top_left(page)).map_err(other)?; - let marks = page.marks.corner_crop || page.marks.center_crop || page.marks.registration || page.description.is_some() || page.label.is_some(); + let text_skipped = page.description.is_some() || page.label.is_some(); Ok( - json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3], "marksSkipped": marks}), + json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3], "textSkipped": text_skipped}), ) } diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index 323519c36b..75592be6d8 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -923,8 +923,12 @@ fn print_confirm(app: &mut PhotocraftApp, f: &Map) -> Result { pub data: &'a [u8], } +/// Printer marks drawn around the image box: corner and centre crop marks, and registration +/// marks. Matches [`photocraft_engine`]'s PDF `Marks`, mirrored here so this crate stays +/// independent of the engine; GDI draws the same geometry with vector line/ellipse calls instead +/// of PDF content stream operators. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Marks { + pub corner_crop: bool, + pub center_crop: bool, + pub registration: bool, +} + +impl Marks { + pub(crate) fn any(self) -> bool { + self.corner_crop || self.center_crop || self.registration + } +} + /// One print job. pub struct Job<'a> { pub printer: &'a str, @@ -116,6 +136,7 @@ pub struct Job<'a> { /// Print to this file instead of the device (drivers that write files, and tests). pub output_file: Option<&'a str>, pub copies: u32, + pub marks: Marks, } /// Where a job went: the image box in device pixels and the device resolution. @@ -254,7 +275,7 @@ mod tests { #[test] fn print_rejects_empty_or_short_images_before_any_system_call() { - let job = Job { printer: "x", settings: None, title: "t", output_file: None, copies: 1 }; + let job = Job { printer: "x", settings: None, title: "t", output_file: None, copies: 1, marks: Marks::default() }; let short = RgbImage { width: 2, height: 2, data: &[0; 11] }; assert!(matches!(print(&job, &short, (0.0, 0.0, 10.0, 10.0)), Err(PrintError::Invalid(_)))); let ok = RgbImage { width: 2, height: 2, data: &[0; 12] }; diff --git a/crates/winprint/src/windows.rs b/crates/winprint/src/windows.rs index 9b8f8ba16e..1ff5de984a 100644 --- a/crates/winprint/src/windows.rs +++ b/crates/winprint/src/windows.rs @@ -6,9 +6,9 @@ use std::ptr::{null, null_mut}; use windows_sys::Win32::Foundation::HWND; use windows_sys::Win32::Graphics::Gdi::{ - BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateDCW, DEVMODEW, DIB_RGB_COLORS, DM_IN_BUFFER, DM_IN_PROMPT, DM_OUT_BUFFER, DeleteDC, GetDeviceCaps, HALFTONE, - HDC, HORZRES, LOGPIXELSX, LOGPIXELSY, PHYSICALHEIGHT, PHYSICALOFFSETX, PHYSICALOFFSETY, PHYSICALWIDTH, SRCCOPY, SetBrushOrgEx, SetStretchBltMode, - StretchDIBits, VERTRES, + BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateDCW, CreatePen, DEVMODEW, DIB_RGB_COLORS, DM_IN_BUFFER, DM_IN_PROMPT, DM_OUT_BUFFER, DeleteDC, DeleteObject, + Ellipse, GetDeviceCaps, GetStockObject, HALFTONE, HDC, HOLLOW_BRUSH, HORZRES, LOGPIXELSX, LOGPIXELSY, LineTo, MoveToEx, PHYSICALHEIGHT, PHYSICALOFFSETX, + PHYSICALOFFSETY, PHYSICALWIDTH, PS_SOLID, SRCCOPY, SelectObject, SetBrushOrgEx, SetStretchBltMode, StretchDIBits, VERTRES, }; use windows_sys::Win32::Graphics::Printing::{ ClosePrinter, DocumentPropertiesW, EnumPrintersW, GetDefaultPrinterW, OpenPrinterW, PRINTER_ENUM_CONNECTIONS, PRINTER_ENUM_LOCAL, PRINTER_HANDLE, @@ -17,7 +17,7 @@ use windows_sys::Win32::Graphics::Printing::{ use windows_sys::Win32::Storage::Xps::{AbortDoc, DOCINFOW, EndDoc, EndPage, StartDocW, StartPage}; use windows_sys::Win32::UI::ColorSystem::{ICM_OFF, SetICMMode}; -use crate::{DevMode, Job, Page, PrintError, Printed, Printer, Result, RgbImage}; +use crate::{DevMode, Job, Marks, Page, PrintError, Printed, Printer, Result, RgbImage}; const IDOK: i32 = 1; const IDCANCEL: i32 = 2; @@ -264,6 +264,61 @@ fn dib(image: &RgbImage<'_>) -> Result<(BITMAPINFO, Vec)> { Ok((BITMAPINFO { bmiHeader: header, bmiColors: [Default::default()] }, bits)) } +/// Vector marks around the image box `(x, y, w, h)` (device pixels, origin top-left, as GDI +/// draws). Same geometry as `photocraft_engine::print_cmds::marks_ops`'s PDF content stream, with +/// the vertical sense flipped (PDF's origin is bottom-left; GDI's is top-left), scaled from points +/// to device pixels with `dpi` instead of PDF's user-space points. +/// +/// # Safety +/// `dc` is a valid, selected device context for the current page. +unsafe fn draw_marks(dc: HDC, marks: Marks, (x, y, w, h): (i32, i32, i32, i32), dpi: (u32, u32)) -> Result<()> { + let (sx, sy) = (f64::from(dpi.0) / 72.0, f64::from(dpi.1) / 72.0); + let px = |v: f64| v.round() as i32; + let (gap_x, gap_y) = (px(6.0 * sx), px(6.0 * sy)); + let (len_x, len_y) = (px(18.0 * sx), px(18.0 * sy)); + let pen_width = px(0.3 * sx).max(1); + // SAFETY: `dc` is valid; the pen and brush are deleted/restored before returning. + let pen = unsafe { CreatePen(PS_SOLID, pen_width, 0x0000_0000) }; + let hollow = unsafe { GetStockObject(HOLLOW_BRUSH) }; + let old_pen = unsafe { SelectObject(dc, pen) }; + let old_brush = unsafe { SelectObject(dc, hollow) }; + let ok = std::cell::Cell::new(true); + let line = |x0: i32, y0: i32, x1: i32, y1: i32| { + // SAFETY: `dc` is valid for the duration of this call. + ok.set(ok.get() && unsafe { MoveToEx(dc, x0, y0, null_mut()) != 0 && LineTo(dc, x1, y1) != 0 }); + }; + if marks.corner_crop { + for (cx, cy, dx, dy) in [(x, y, -1, -1), (x + w, y, 1, -1), (x, y + h, -1, 1), (x + w, y + h, 1, 1)] { + line(cx + dx * gap_x, cy, cx + dx * (gap_x + len_x), cy); + line(cx, cy + dy * gap_y, cx, cy + dy * (gap_y + len_y)); + } + } + if marks.center_crop { + let (mx, my) = (x + w / 2, y + h / 2); + line(mx, y - gap_y, mx, y - gap_y - len_y); + line(mx, y + h + gap_y, mx, y + h + gap_y + len_y); + line(x - gap_x, my, x - gap_x - len_x, my); + line(x + w + gap_x, my, x + w + gap_x + len_x, my); + } + if marks.registration { + let (rx, ry) = (px(5.0 * sx), px(5.0 * sy)); + let (off_x, off_y) = (gap_x + len_x / 2, gap_y + len_y / 2); + for (cx, cy) in [(x + w / 2, y - off_y), (x + w / 2, y + h + off_y), (x - off_x, y + h / 2), (x + w + off_x, y + h / 2)] { + // SAFETY: `dc` is valid for the duration of this call. + ok.set(ok.get() && unsafe { Ellipse(dc, cx - rx, cy - ry, cx + rx, cy + ry) != 0 }); + line(cx - (rx as f64 * 1.6) as i32, cy, cx + (rx as f64 * 1.6) as i32, cy); + line(cx, cy - (ry as f64 * 1.6) as i32, cx, cy + (ry as f64 * 1.6) as i32); + } + } + // SAFETY: restores the DC's previous pen/brush, then frees the ones created above. + unsafe { + SelectObject(dc, old_pen); + SelectObject(dc, old_brush); + DeleteObject(pen); + } + if ok.get() { Ok(()) } else { Err(last_error("drawing printer marks")) } +} + pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> Result { if image.width > i32::MAX as u32 || image.height > i32::MAX as u32 { return Err(PrintError::Invalid("image too large".into())); @@ -311,6 +366,12 @@ pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> if lines <= 0 { return Err(fail("sending the image to the printer")); } + if job.marks.any() { + // SAFETY: `dc.0` is the valid, started page's device context. + if let Err(e) = unsafe { draw_marks(dc.0, job.marks, (x, y, w, h), page.dpi) } { + return Err(fail(&e.to_string())); + } + } // SAFETY: closes the page started above. if unsafe { EndPage(dc.0) } <= 0 { return Err(fail("finishing a page")); @@ -385,12 +446,20 @@ mod tests { let (w, h) = (300u32, 200u32); let data: Vec = (0..w * h).flat_map(|i| if (i % w) < w / 2 { [220, 30, 30] } else { [30, 30, 220] }).collect(); let out_s = out.to_string_lossy().into_owned(); - let job = Job { printer: "Microsoft Print to PDF", settings: Some(&settings), title: "PhotoCraft test", output_file: Some(&out_s), copies: 2 }; + let marks = crate::Marks { corner_crop: true, center_crop: true, registration: true }; + let job = Job { printer: "Microsoft Print to PDF", settings: Some(&settings), title: "PhotoCraft test", output_file: Some(&out_s), copies: 2, marks }; let done = crate::print(&job, &RgbImage { width: w, height: h, data: &data }, (72.0, 72.0, 216.0, 144.0)).unwrap(); assert_eq!(done.device_rect.2, done.dpi.0 as i32 * 3, "3 inches wide"); let pdf = std::fs::read(&out).unwrap(); assert!(pdf.starts_with(b"%PDF"), "the driver wrote a PDF"); assert!(pdf.len() > 1000, "the page has content"); + // The driver vectorises our GDI line/ellipse calls as real PDF content: a job with every + // mark on is meaningfully bigger than the same image with marks off. + let no_marks_job = Job { marks: crate::Marks::default(), ..job }; + let done2 = crate::print(&no_marks_job, &RgbImage { width: w, height: h, data: &data }, (72.0, 72.0, 216.0, 144.0)).unwrap(); + let _ = done2; + let pdf_no_marks = std::fs::read(&out).unwrap(); + assert!(pdf.len() > pdf_no_marks.len() + 100, "marks add vector content: {} vs {}", pdf.len(), pdf_no_marks.len()); let _ = std::fs::remove_file(&out); } From 9f7f8b7a7b9ece8ab024d6f0194d11805d1b6bfa Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 13 Oct 2026 01:51:12 +0200 Subject: [PATCH 04/39] Check that driver copy fields remain active after validation --- crates/winprint/src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/winprint/src/lib.rs b/crates/winprint/src/lib.rs index e263926e73..e0c5729881 100644 --- a/crates/winprint/src/lib.rs +++ b/crates/winprint/src/lib.rs @@ -94,7 +94,10 @@ impl DevMode { #[cfg(any(windows, test))] pub(crate) fn has_manual_copies(&self) -> bool { - self.0.get(86..88) == Some(1i16.to_le_bytes().as_slice()) && self.0.get(100..102) == Some(0i16.to_le_bytes().as_slice()) + let fields = self.0.get(72..76).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(u32::from_le_bytes); + fields.is_some_and(|f| f & 0x100 != 0) + && self.0.get(86..88) == Some(1i16.to_le_bytes().as_slice()) + && self.0.get(100..102) == Some(0i16.to_le_bytes().as_slice()) } /// Hex, for JSON parameters and remembered print settings. @@ -296,6 +299,9 @@ mod tests { assert!(!original.has_manual_copies()); assert_eq!(&normalized.as_bytes()[220..], &original.as_bytes()[220..]); assert_eq!(normalized.for_manual_copies().unwrap(), normalized); + let mut ignored = normalized.as_bytes().to_vec(); + ignored[72..76].copy_from_slice(&0u32.to_le_bytes()); + assert!(!DevMode::from_bytes(ignored).unwrap().has_manual_copies(), "a driver must advertise that it accepts dmCopies"); } #[test] @@ -314,5 +320,8 @@ mod tests { assert!(matches!(print(&job, &short, (0.0, 0.0, 10.0, 10.0)), Err(PrintError::Invalid(_)))); let ok = RgbImage { width: 2, height: 2, data: &[0; 12] }; assert!(matches!(print(&job, &ok, (0.0, 0.0, 0.0, 10.0)), Err(PrintError::Invalid(_)))); + for rect in [(f64::NAN, 0.0, 10.0, 10.0), (0.0, 0.0, f64::INFINITY, 10.0)] { + assert!(matches!(print(&job, &ok, rect), Err(PrintError::Invalid(_)))); + } } } From 4a730912c0dd3868b3c4933f8734ca03e9698cae Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 13 Oct 2026 01:53:16 +0200 Subject: [PATCH 05/39] Preserve PDF text and CMYK paths and reject native layout mismatches --- crates/engine/src/print_cmds.rs | 20 +++++++++++++++++--- crates/engine/src/print_cmds/tests.rs | 15 +++++++++++++++ crates/winprint/src/windows.rs | 11 +++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index dd7f85971c..4093c8dd35 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -391,13 +391,16 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { // Windows has no `lp`: print through the driver (GDI, ICM off) instead of spooling a PDF. // Sending follows the shared command contract; fuzz tests explicitly request dryRun. // The worker returns its result before repeat settings and hooks are applied once. - if cfg!(windows) && send && !dry && output.is_none() { + if cfg!(windows) && send && !dry && output.is_none() && native_rgb_compatible(&page) { let printed = print_windows(&page, p, &doc.name, copies)?; return Ok(json!({ "pdf": Value::Null, "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0, "copies": copies, "command": Value::Null, "sent": true, "spooler": printed, "color": color, })); } + if cfg!(windows) && send && !dry && output.is_none() && printer_settings(p)?.is_some() { + return Err(other("CMYK profiles and description/label text use PDF composition; these native driver settings cannot be applied. Save as PDF, or clear the driver settings to use PDF printing.")); + } let pdf = print_pdf(&page); let job = craft_print::Job { printer: p.get("printer").and_then(Value::as_str).filter(|v| !v.is_empty()).map(str::to_string), @@ -427,6 +430,14 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { ) } +/// Keep CMYK output profiles and page text on the existing PDF composition path. GDI currently +/// draws RGB/gray and vector marks only; it must never silently omit requested page content. +fn native_rgb_compatible(page: &PrintPage) -> bool { + matches!(page.image.channels, 1 | 3) + && page.description.as_deref().is_none_or(str::is_empty) + && page.label.as_deref().is_none_or(str::is_empty) +} + /// Print One Copy repeats these. fn remember_print(s: &mut Session, p: &Value) { let mut remembered = p.clone(); @@ -490,14 +501,17 @@ pub fn page_rect_top_left(page: &PrintPage) -> (f64, f64, f64, f64) { fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Result { let printer = printer_name(p)?; let settings = printer_settings(p)?; + let device_page = photocraft_winprint::page(&printer, settings.as_ref()).map_err(other)?; + if (device_page.paper.0 - page.paper.0).abs() > 2.0 || (device_page.paper.1 - page.paper.1).abs() > 2.0 { + return Err(other("the driver paper or orientation differs from the print layout; choose the matching paper in Print Settings before printing")); + } let rgb = page_rgb(page)?; let image = photocraft_winprint::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; let marks = photocraft_winprint::Marks { corner_crop: page.marks.corner_crop, center_crop: page.marks.center_crop, registration: page.marks.registration }; let job = photocraft_winprint::Job { printer: &printer, settings: settings.as_ref(), title, output_file: None, copies: copies.clamp(1, 999), marks }; let done = photocraft_winprint::print(&job, &image, page_rect_top_left(page)).map_err(other)?; - let text_skipped = page.description.is_some() || page.label.is_some(); Ok( - json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3], "textSkipped": text_skipped}), + json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3]}), ) } diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index c15b7c6562..b240efb48a 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -175,6 +175,21 @@ fn windows_print_image_is_rgb_mirrored_for_emulsion_down() { assert_eq!(page_rect_top_left(&page_of(3, vec![0; 6], true)), (356.0, 528.0, 144.0, 72.0)); } +#[test] +fn native_rgb_route_preserves_pdf_only_color_and_page_content() { + let mut page = page_of(3, vec![0; 6], false); + assert!(native_rgb_compatible(&page)); + page.label = Some("Print Me".into()); + assert!(!native_rgb_compatible(&page)); + page.label = None; + page.description = Some("Proof description".into()); + assert!(!native_rgb_compatible(&page)); + page.description = Some(String::new()); + assert!(native_rgb_compatible(&page)); + assert!(!native_rgb_compatible(&page_of(4, vec![0; 8], false))); + assert!(native_rgb_compatible(&page_of(1, vec![0; 2], false))); +} + #[test] fn printer_commands_answer_without_a_document_and_reject_bad_settings() { let mut s = Session::new(); diff --git a/crates/winprint/src/windows.rs b/crates/winprint/src/windows.rs index ec9bd784fb..0e00338668 100644 --- a/crates/winprint/src/windows.rs +++ b/crates/winprint/src/windows.rs @@ -458,6 +458,11 @@ mod tests { let out = std::env::temp_dir().join(format!("pc-winprint-{}.pdf", std::process::id())); let _ = std::fs::remove_file(&out); let settings = crate::default_settings("Microsoft Print to PDF").unwrap(); + let mut bytes = settings.as_bytes().to_vec(); + bytes[86..88].copy_from_slice(&5i16.to_le_bytes()); + let fields = u32::from_le_bytes(bytes[72..76].try_into().unwrap()) | 0x100; + bytes[72..76].copy_from_slice(&fields.to_le_bytes()); + let settings = DevMode::from_bytes(bytes).unwrap(); let (w, h) = (300u32, 200u32); let data: Vec = (0..w * h).flat_map(|i| if (i % w) < w / 2 { [220, 30, 30] } else { [30, 30, 220] }).collect(); let out_s = out.to_string_lossy().into_owned(); @@ -475,6 +480,12 @@ mod tests { let _ = done2; let pdf_no_marks = std::fs::read(&out).unwrap(); assert!(pdf.len() > pdf_no_marks.len() + 100, "marks add vector content: {} vs {}", pdf.len(), pdf_no_marks.len()); + if let Ok(evidence) = std::env::var("PHOTOCRAFT_WINPRINT_TEST_EVIDENCE") { + let evidence = std::path::Path::new(&evidence); + std::fs::create_dir_all(evidence).unwrap(); + std::fs::write(evidence.join("copies2-driver5-marks.pdf"), &pdf).unwrap(); + std::fs::write(evidence.join("copies2-driver5-no-marks.pdf"), &pdf_no_marks).unwrap(); + } let _ = std::fs::remove_file(&out); } From 3702555aa9ded0393aa70cb1e81ae92a679d65b6 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 13 Oct 2026 02:02:13 +0200 Subject: [PATCH 06/39] Make native RGB color policy explicit and preserve embedded profiles --- crates/engine/src/print_cmds.rs | 23 ++++++++++++++--- crates/engine/src/print_cmds/tests.rs | 16 +++++++----- crates/winprint/src/lib.rs | 14 ++++++++++- crates/winprint/src/windows.rs | 36 +++++++++++++++++++++------ 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 4093c8dd35..aa8f7d2a8a 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -391,7 +391,8 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { // Windows has no `lp`: print through the driver (GDI, ICM off) instead of spooling a PDF. // Sending follows the shared command contract; fuzz tests explicitly request dryRun. // The worker returns its result before repeat settings and hooks are applied once. - if cfg!(windows) && send && !dry && output.is_none() && native_rgb_compatible(&page) { + let handling = p.get("colorHandling").and_then(Value::as_str).unwrap_or("printerManages"); + if cfg!(windows) && send && !dry && output.is_none() && native_rgb_compatible(&page, handling) { let printed = print_windows(&page, p, &doc.name, copies)?; return Ok(json!({ "pdf": Value::Null, "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0, @@ -399,7 +400,7 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { })); } if cfg!(windows) && send && !dry && output.is_none() && printer_settings(p)?.is_some() { - return Err(other("CMYK profiles and description/label text use PDF composition; these native driver settings cannot be applied. Save as PDF, or clear the driver settings to use PDF printing.")); + return Err(other("the selected color profile or page text requires PDF printing. Save as PDF, or clear Print Settings to use PDF printing.")); } let pdf = print_pdf(&page); let job = craft_print::Job { @@ -432,8 +433,9 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { /// Keep CMYK output profiles and page text on the existing PDF composition path. GDI currently /// draws RGB/gray and vector marks only; it must never silently omit requested page content. -fn native_rgb_compatible(page: &PrintPage) -> bool { +fn native_rgb_compatible(page: &PrintPage, handling: &str) -> bool { matches!(page.image.channels, 1 | 3) + && (page.image.icc.is_none() || matches!(handling, "photocraftManages" | "photoshopManages" | "noColorManagement")) && page.description.as_deref().is_none_or(str::is_empty) && page.label.as_deref().is_none_or(str::is_empty) } @@ -508,7 +510,20 @@ fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Resul let rgb = page_rgb(page)?; let image = photocraft_winprint::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; let marks = photocraft_winprint::Marks { corner_crop: page.marks.corner_crop, center_crop: page.marks.center_crop, registration: page.marks.registration }; - let job = photocraft_winprint::Job { printer: &printer, settings: settings.as_ref(), title, output_file: None, copies: copies.clamp(1, 999), marks }; + let color_policy = match p.get("colorHandling").and_then(Value::as_str) { + Some("photocraftManages" | "photoshopManages") => photocraft_winprint::ColorPolicy::ApplicationManaged, + Some("noColorManagement") => photocraft_winprint::ColorPolicy::Unmanaged, + _ => photocraft_winprint::ColorPolicy::DriverManaged, + }; + let job = photocraft_winprint::Job { + printer: &printer, + settings: settings.as_ref(), + title, + output_file: None, + copies: copies.clamp(1, 999), + color_policy, + marks, + }; let done = photocraft_winprint::print(&job, &image, page_rect_top_left(page)).map_err(other)?; Ok( json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3]}), diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index b240efb48a..023829d72d 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -178,16 +178,20 @@ fn windows_print_image_is_rgb_mirrored_for_emulsion_down() { #[test] fn native_rgb_route_preserves_pdf_only_color_and_page_content() { let mut page = page_of(3, vec![0; 6], false); - assert!(native_rgb_compatible(&page)); + assert!(native_rgb_compatible(&page, "printerManages")); page.label = Some("Print Me".into()); - assert!(!native_rgb_compatible(&page)); + assert!(!native_rgb_compatible(&page, "printerManages")); page.label = None; page.description = Some("Proof description".into()); - assert!(!native_rgb_compatible(&page)); + assert!(!native_rgb_compatible(&page, "printerManages")); page.description = Some(String::new()); - assert!(native_rgb_compatible(&page)); - assert!(!native_rgb_compatible(&page_of(4, vec![0; 8], false))); - assert!(native_rgb_compatible(&page_of(1, vec![0; 2], false))); + assert!(native_rgb_compatible(&page, "printerManages")); + page.image.icc = Some(vec![1, 2, 3]); + assert!(!native_rgb_compatible(&page, "printerManages"), "preserve embedded source ICC in the PDF route"); + assert!(native_rgb_compatible(&page, "photocraftManages")); + assert!(native_rgb_compatible(&page, "photoshopManages")); + assert!(!native_rgb_compatible(&page_of(4, vec![0; 8], false), "photocraftManages")); + assert!(native_rgb_compatible(&page_of(1, vec![0; 2], false), "noColorManagement")); } #[test] diff --git a/crates/winprint/src/lib.rs b/crates/winprint/src/lib.rs index e0c5729881..ef20e28ce1 100644 --- a/crates/winprint/src/lib.rs +++ b/crates/winprint/src/lib.rs @@ -150,6 +150,17 @@ impl Marks { } } +/// One print job. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ColorPolicy { + /// The driver applies its color settings to the RGB image. + DriverManaged, + /// The caller has converted the pixels into the printer's RGB space. + ApplicationManaged, + /// The caller deliberately requests no DC color correction. + Unmanaged, +} + /// One print job. pub struct Job<'a> { pub printer: &'a str, @@ -159,6 +170,7 @@ pub struct Job<'a> { /// Print to this file instead of the device (drivers that write files, and tests). pub output_file: Option<&'a str>, pub copies: u32, + pub color_policy: ColorPolicy, pub marks: Marks, } @@ -315,7 +327,7 @@ mod tests { #[test] fn print_rejects_empty_or_short_images_before_any_system_call() { - let job = Job { printer: "x", settings: None, title: "t", output_file: None, copies: 1, marks: Marks::default() }; + let job = Job { printer: "x", settings: None, title: "t", output_file: None, copies: 1, color_policy: ColorPolicy::Unmanaged, marks: Marks::default() }; let short = RgbImage { width: 2, height: 2, data: &[0; 11] }; assert!(matches!(print(&job, &short, (0.0, 0.0, 10.0, 10.0)), Err(PrintError::Invalid(_)))); let ok = RgbImage { width: 2, height: 2, data: &[0; 12] }; diff --git a/crates/winprint/src/windows.rs b/crates/winprint/src/windows.rs index 0e00338668..103309dcd0 100644 --- a/crates/winprint/src/windows.rs +++ b/crates/winprint/src/windows.rs @@ -15,9 +15,9 @@ use windows_sys::Win32::Graphics::Printing::{ PRINTER_INFO_4W, }; use windows_sys::Win32::Storage::Xps::{AbortDoc, DOCINFOW, EndDoc, EndPage, StartDocW, StartPage}; -use windows_sys::Win32::UI::ColorSystem::{ICM_OFF, SetICMMode}; +use windows_sys::Win32::UI::ColorSystem::{ICM_DONE_OUTSIDEDC, ICM_OFF, SetICMMode}; -use crate::{DevMode, Job, Marks, Page, PrintError, Printed, Printer, Result, RgbImage}; +use crate::{ColorPolicy, DevMode, Job, Marks, Page, PrintError, Printed, Printer, Result, RgbImage}; const IDOK: i32 = 1; const IDCANCEL: i32 = 2; @@ -349,11 +349,17 @@ pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> lpszDatatype: null(), fwType: 0, }; - // SAFETY: a valid printer DC. ICM off: the pixels are already in the printer's space - // (or deliberately unmanaged), so GDI must not convert them again. - if unsafe { SetICMMode(dc.0, ICM_OFF) } == 0 { - return Err(last_error("disabling GDI color management")); - } + let color_mode = match job.color_policy { + ColorPolicy::DriverManaged => ICM_OFF, + // ICM_OFF enables old-style halftone correction. Pixels converted by the caller + // need both DC conversion and that correction off (Microsoft SetICMMode contract). + ColorPolicy::ApplicationManaged | ColorPolicy::Unmanaged => ICM_DONE_OUTSIDEDC, + }; + let set_color_mode = || { + // SAFETY: a valid printer DC and a documented SetICMMode value. + if unsafe { SetICMMode(dc.0, color_mode) } == 0 { Err(last_error("setting GDI color policy")) } else { Ok(()) } + }; + set_color_mode()?; // SAFETY: a valid DC; HALFTONE resampling needs the brush origin reset afterwards. unsafe { SetStretchBltMode(dc.0, HALFTONE); @@ -374,6 +380,12 @@ pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> if unsafe { StartPage(dc.0) } <= 0 { return Err(fail("starting a page")); } + // StartPage saves/restores DC state. Reapply the policy for each page, before pixels. + if let Err(error) = set_color_mode() { + // SAFETY: this DC owns the document started above. + unsafe { AbortDoc(dc.0) }; + return Err(error); + } // SAFETY: `bits` is a bottom-up DIB exactly as `bmi` describes (24-bit, padded rows, // `biSizeImage` bytes); the source rectangle is the whole image. let lines = @@ -467,7 +479,15 @@ mod tests { let data: Vec = (0..w * h).flat_map(|i| if (i % w) < w / 2 { [220, 30, 30] } else { [30, 30, 220] }).collect(); let out_s = out.to_string_lossy().into_owned(); let marks = crate::Marks { corner_crop: true, center_crop: true, registration: true }; - let job = Job { printer: "Microsoft Print to PDF", settings: Some(&settings), title: "PhotoCraft test", output_file: Some(&out_s), copies: 2, marks }; + let job = Job { + printer: "Microsoft Print to PDF", + settings: Some(&settings), + title: "PhotoCraft test", + output_file: Some(&out_s), + copies: 2, + color_policy: ColorPolicy::ApplicationManaged, + marks, + }; let done = crate::print(&job, &RgbImage { width: w, height: h, data: &data }, (72.0, 72.0, 216.0, 144.0)).unwrap(); assert_eq!(done.device_rect.2, done.dpi.0 as i32 * 3, "3 inches wide"); let pdf = std::fs::read(&out).unwrap(); From d9bc5a8cd51bf967aede79d41a96e2e15328c00d Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 13 Oct 2026 02:56:11 +0200 Subject: [PATCH 07/39] Use shared native print transport and keep page composition in PhotoCraft --- Cargo.lock | 1229 ++++++++++++++++- Cargo.toml | 3 +- crates/engine/Cargo.toml | 1 - crates/engine/src/print_cmds.rs | 33 +- crates/engine/src/print_cmds/tests.rs | 4 +- crates/engine/src/print_cmds/windows_marks.rs | 47 + crates/ui-egui/Cargo.toml | 2 +- crates/ui-egui/src/file_ui.rs | 10 +- crates/winprint/Cargo.toml | 26 - crates/winprint/src/lib.rs | 339 ----- crates/winprint/src/windows.rs | 520 ------- xtask/src/layers.rs | 1 - 12 files changed, 1288 insertions(+), 927 deletions(-) create mode 100644 crates/engine/src/print_cmds/windows_marks.rs delete mode 100644 crates/winprint/Cargo.toml delete mode 100644 crates/winprint/src/lib.rs delete mode 100644 crates/winprint/src/windows.rs diff --git a/Cargo.lock b/Cargo.lock index 08d317ce94..9ba37f36a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,12 +550,48 @@ dependencies = [ "arrayvec", ] +[[package]] +name = "aws-lc-rs" +version = "1.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faac5829c2b74c28f830747e7818ccfb684261b5f48a1118b1e2a13d36dfab13" +dependencies = [ + "aws-lc-sys", + "untrusted 0.7.1", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1622d8446a2d4b2ce0c7eefc73dd43a99779028d5ee5c2dd8073a658ba8a2bc" +dependencies = [ + "cc", + "cmake", + "dunce", + "fs_extra", + "pkg-config", +] + [[package]] name = "base64" version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" + +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + [[package]] name = "bindgen" version = "0.72.1" @@ -645,7 +681,7 @@ dependencies = [ "cc", "cfg-if", "constant_time_eq", - "cpufeatures", + "cpufeatures 0.3.1", ] [[package]] @@ -654,6 +690,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "block2" version = "0.5.1" @@ -885,7 +930,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", - "cpufeatures", + "cpufeatures 0.3.1", "rand_core 0.10.1", ] @@ -896,8 +941,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" dependencies = [ "iana-time-zone", + "js-sys", "num-traits", "serde", + "wasm-bindgen", "windows-link", ] @@ -973,6 +1020,39 @@ dependencies = [ "error-code", ] +[[package]] +name = "cmake" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0f78a02292a74a88ac736019ab962ece0bc380e3f977bf72e376c5d78ff0678" +dependencies = [ + "cc", +] + +[[package]] +name = "cmpv2" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "961b955a666e25ee5a1091d219128d6e6401e3dab84efb1a2bf6b4035d797b39" +dependencies = [ + "crmf", + "der", + "spki", + "x509-cert", +] + +[[package]] +name = "cms" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b77c319abfd5219629c45c34c89ba945ed3c5e49fcde9d16b6c3885f118a730" +dependencies = [ + "const-oid", + "der", + "spki", + "x509-cert", +] + [[package]] name = "codespan-reporting" version = "0.13.1" @@ -1028,6 +1108,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + [[package]] name = "constant_time_eq" version = "0.4.2" @@ -1103,6 +1189,12 @@ dependencies = [ "libc", ] +[[package]] +name = "core_detect" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" + [[package]] name = "core_maths" version = "0.1.1" @@ -1112,6 +1204,15 @@ dependencies = [ "libm", ] +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "cpufeatures" version = "0.3.1" @@ -1124,9 +1225,14 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d991bf6792f8ade99c51358bf3c1d902c8cd96b5#d991bf6792f8ade99c51358bf3c1d902c8cd96b5" +source = "git+https://github.com/storytold/craft-libs?rev=ee70afc29a41bc50f2a55affa9aa2c62e0d28e3a#ee70afc29a41bc50f2a55affa9aa2c62e0d28e3a" dependencies = [ + "craft-winprint-gdi", + "miniz_oxide 0.8.9", + "roxmltree", "tempfile", + "winprint", + "winsafe", ] [[package]] @@ -1138,6 +1244,14 @@ dependencies = [ "serde", ] +[[package]] +name = "craft-winprint-gdi" +version = "0.1.0" +source = "git+https://github.com/storytold/winprint.rs?rev=417ac3c4d14d3da5485a99c7d5ce8f05664ec312#417ac3c4d14d3da5485a99c7d5ce8f05664ec312" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "crc32fast" version = "1.5.2" @@ -1181,6 +1295,18 @@ dependencies = [ "itertools 0.10.5", ] +[[package]] +name = "crmf" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36fe21b96d5b87f5de4b5b7202ec41c00110ac817ce6728fe75fb2fe5962ed92" +dependencies = [ + "cms", + "der", + "spki", + "x509-cert", +] + [[package]] name = "crossbeam-channel" version = "0.5.17" @@ -1221,6 +1347,16 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "cursor-icon" version = "1.2.0" @@ -1267,6 +1403,30 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" +[[package]] +name = "der" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c1832837b905bbfb5101e07cc24c8deddf52f93225eee6ead5f4d63d53ddcb" +dependencies = [ + "const-oid", + "der_derive", + "flagset", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "der_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8034092389675178f570469e6c3b0465d3d30b4505c294a6550db47f3c17ad18" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "dify" version = "0.8.0" @@ -1280,6 +1440,16 @@ dependencies = [ "rayon", ] +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -1394,6 +1564,12 @@ dependencies = [ "linux-raw-sys 0.9.4", ] +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + [[package]] name = "dyn-clone" version = "1.0.20" @@ -1578,6 +1754,20 @@ dependencies = [ "serde", ] +[[package]] +name = "encoding_rs" +version = "0.8.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e985e0451871ad22fb8d2b6b076e2028a502a0d3950998c2c5c0a4f9b5d9679" +dependencies = [ + "cfg-if", + "core_detect", + "multiversion_no_op", + "rustversion", + "scopeguard", + "simdutf8", +] + [[package]] name = "endi" version = "1.1.1" @@ -1797,6 +1987,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97b65636e5b9ef369943878ac74335ba1c55c1cb6adbf1e2c293c624248d693" +[[package]] +name = "filetime" +version = "0.2.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759" +dependencies = [ + "cfg-if", + "libc", +] + [[package]] name = "find-msvc-tools" version = "0.1.14" @@ -1809,6 +2009,12 @@ version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" +[[package]] +name = "flagset" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7ac824320a75a52197e8f2d787f6a38b6718bb6897a35142d749af3c0e8f4fe" + [[package]] name = "flate2" version = "1.1.10" @@ -1826,6 +2032,28 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +[[package]] +name = "fmt-derive" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cb4a4b45c26455885f8b462db12978e51f60cc914337bccb9a36faf93410169" +dependencies = [ + "fmt-derive-proc", +] + +[[package]] +name = "fmt-derive-proc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7704da0760734344ce8753f15b21e97c6be85dc5a8f2c3a963fbd042b3380d23" +dependencies = [ + "proc-macro-crate", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "fmv-macos-events" version = "0.1.0" @@ -1948,6 +2176,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + [[package]] name = "futures" version = "0.3.34" @@ -2073,6 +2307,16 @@ dependencies = [ "libc", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gethostname" version = "1.1.0" @@ -2092,6 +2336,19 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + [[package]] name = "getrandom" version = "0.3.4" @@ -2111,9 +2368,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi 6.0.0", "rand_core 0.10.1", + "wasm-bindgen", ] [[package]] @@ -2279,6 +2538,25 @@ dependencies = [ "euclid", ] +[[package]] +name = "h2" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d29020232d6aa3fb1daca64c1127cf662cf97f254ae16c18c05b8ab635fc118" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "half" version = "2.7.1" @@ -2359,6 +2637,107 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfa8e654703247911e29c23fbeaa261834bd9bb74efba2f9acddc37bfb127f53" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" +dependencies = [ + "base64 0.23.1", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "httparse", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "system-configuration", + "tokio", + "tower-service", + "tracing", + "windows-registry", +] + [[package]] name = "iana-time-zone" version = "0.1.65" @@ -3188,6 +3567,12 @@ dependencies = [ "imgref", ] +[[package]] +name = "lru-slab" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4050469837a6ff301cd14c1f8f24f88549e6d548f24f64e2148eb0f72cebc51f" + [[package]] name = "maybe-owned" version = "0.3.4" @@ -3237,6 +3622,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -3302,6 +3693,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multiversion_no_op" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" + [[package]] name = "naga" version = "30.0.1" @@ -3997,6 +4394,12 @@ dependencies = [ "libc", ] +[[package]] +name = "openssl-probe" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" + [[package]] name = "orbclient" version = "0.3.55" @@ -4116,6 +4519,25 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4" +[[package]] +name = "pem" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d30c53c26bc5b31a98cd02d20f25a7c8567146caf63ed593a9d87b2775291be" +dependencies = [ + "base64 0.22.1", + "serde_core", +] + +[[package]] +name = "pem-rfc7468" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88b39c9bfcfc231068454382784bb460aae594343fb030d46e9f50a645418412" +dependencies = [ + "base64ct", +] + [[package]] name = "peniko" version = "0.6.1" @@ -4258,7 +4680,7 @@ dependencies = [ name = "photocraft-automation" version = "0.8.2" dependencies = [ - "base64", + "base64 0.22.1", "cap-std", "getrandom 0.3.4", "photocraft-codecs", @@ -4379,7 +4801,6 @@ dependencies = [ "photocraft-raw", "photocraft-text", "photocraft-vector", - "photocraft-winprint", "rayon", "roxmltree", "serde", @@ -4579,7 +5000,8 @@ dependencies = [ name = "photocraft-ui-egui" version = "0.8.2" dependencies = [ - "base64", + "base64 0.22.1", + "craft-print", "craft-ui", "eframe", "egui", @@ -4607,7 +5029,6 @@ dependencies = [ "photocraft-testkit", "photocraft-text", "photocraft-vector", - "photocraft-winprint", "rayon", "serde", "serde_json", @@ -4649,13 +5070,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "photocraft-winprint" -version = "0.8.2" -dependencies = [ - "windows-sys 0.61.2", -] - [[package]] name = "pico-args" version = "0.5.0" @@ -4856,6 +5270,30 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.107" @@ -4962,6 +5400,63 @@ dependencies = [ "memchr", ] +[[package]] +name = "quinn" +version = "0.11.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4051e23e9185c255a7e33ef59cdbca87a22d359052eecd22fc6b901fb37d9d11" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash 2.1.3", + "rustls", + "socket2", + "thiserror 2.0.21", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e750cca55fe4f0439a15d0bb529da9651e79993e8e72c61a899a36d462befbe" +dependencies = [ + "aws-lc-rs", + "bytes", + "getrandom 0.4.3", + "lru-slab", + "rand 0.10.3", + "rand_pcg", + "ring", + "rustc-hash 2.1.3", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.21", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af66907df18639dcf4db56ca65490cabc4b27a97dbadd96f2926cca73298f016" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.52.0", +] + [[package]] name = "quote" version = "1.0.47" @@ -5014,6 +5509,12 @@ dependencies = [ "rand_core 0.9.5", ] +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + [[package]] name = "rand_core" version = "0.9.5" @@ -5029,6 +5530,15 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +[[package]] +name = "rand_pcg" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caa0f4137e1c0a72f4c651489402276c8e8e1cf081f3b0ba156d2cbeef09e86a" +dependencies = [ + "rand_core 0.10.1", +] + [[package]] name = "rand_xorshift" version = "0.4.0" @@ -5239,6 +5749,49 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" +[[package]] +name = "reqwest" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16a1cfa75cc186dd73d5818e510e042e40927bccc9c236b061cea97e1eb08029" +dependencies = [ + "base64 0.23.1", + "bytes", + "encoding_rs", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "mime", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "rustls-platform-verifier", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "resvg" version = "0.45.1" @@ -5292,6 +5845,20 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted 0.9.0", + "windows-sys 0.52.0", +] + [[package]] name = "rmcp" version = "3.5.0" @@ -5419,6 +5986,81 @@ dependencies = [ "rustix 1.1.5", ] +[[package]] +name = "rustls" +version = "0.23.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +dependencies = [ + "aws-lc-rs", + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dab5152771c58876a2146916e53e35057e1a4dfa2b9df0f0305b07f611fdea4d" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-platform-verifier" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1167586491e2b18b8bfbb293e8180ec17c201c4f076d7cb3070ca964e7598f98" +dependencies = [ + "core-foundation 0.10.1", + "core-foundation-sys", + "jni", + "log", + "once_cell", + "rustls", + "rustls-native-certs", + "rustls-platform-verifier-android", + "rustls-webpki", + "security-framework", + "security-framework-sys", + "webpki-root-certs", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustls-platform-verifier-android" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eec689c0bc40ff2458a5977b6619cb718087084a18e02a131c599b62d05e1a5f" + +[[package]] +name = "rustls-webpki" +version = "0.103.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +dependencies = [ + "aws-lc-rs", + "ring", + "rustls-pki-types", + "untrusted 0.9.0", +] + [[package]] name = "rustversion" version = "1.0.23" @@ -5464,6 +6106,18 @@ dependencies = [ "twox-hash", ] +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "ryu-js" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04d056b875a9d2e6cb9a61d127afee9ac5999b9f87bcb32079d1318e505be714" + [[package]] name = "same-file" version = "1.0.6" @@ -5473,6 +6127,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "schannel" +version = "0.1.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91c1b7e4904c873ef0710c1f407dde2e6287de2bebc1bbbf7d430bb7cbffd939" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "schemars" version = "1.2.2" @@ -5511,6 +6174,29 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.10.1", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "self_cell" version = "1.3.0" @@ -5577,6 +6263,17 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_json_canonicalizer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe52319a927259afbfa5180c5157cd8167edfd3e8c254f9558c7fef44c5649f2" +dependencies = [ + "ryu-js", + "serde", + "serde_json", +] + [[package]] name = "serde_repr" version = "0.1.21" @@ -5597,6 +6294,40 @@ dependencies = [ "serde_core", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + [[package]] name = "shlex" version = "1.3.0" @@ -5619,6 +6350,175 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core 0.6.4", +] + +[[package]] +name = "sigstore-bundle" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7dc53c8a941858d01479622dbb7650aaa2ee0ca1fb58fb6cdddbfc3064abbb" +dependencies = [ + "base64 0.22.1", + "hex", + "serde", + "serde_json", + "sigstore-crypto", + "sigstore-merkle", + "sigstore-rekor", + "sigstore-tsa", + "sigstore-types", + "thiserror 2.0.21", +] + +[[package]] +name = "sigstore-crypto" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da90c3d9af638898a5fdc347479b18f950bb0dde11ecf2244f94cd28135da53b" +dependencies = [ + "aws-lc-rs", + "base64 0.22.1", + "const-oid", + "der", + "digest", + "pem", + "rand_core 0.10.1", + "sha2", + "signature", + "sigstore-types", + "spki", + "thiserror 2.0.21", + "tracing", + "x509-cert", +] + +[[package]] +name = "sigstore-merkle" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c0d53558825c77716855c13794306fff766854c1bf92948e77d7890da3f2455" +dependencies = [ + "base64 0.22.1", + "hex", + "sigstore-crypto", + "sigstore-types", + "thiserror 2.0.21", +] + +[[package]] +name = "sigstore-rekor" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "641fdaaa40d0cd6e249cf1671c2240beff1a3ece578062cf43e750779e8db2b3" +dependencies = [ + "base64 0.22.1", + "hex", + "reqwest", + "serde", + "serde_json", + "sigstore-crypto", + "sigstore-merkle", + "sigstore-types", + "thiserror 2.0.21", + "url", +] + +[[package]] +name = "sigstore-trust-root" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6b0d2550f05c096400066e858db13d91f9de3b750a7541fb1291c1dc80ca290" +dependencies = [ + "base64 0.22.1", + "chrono", + "hex", + "rustls-pki-types", + "serde", + "serde_json", + "sigstore-crypto", + "sigstore-types", + "thiserror 2.0.21", + "x509-cert", +] + +[[package]] +name = "sigstore-tsa" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bc86c8abc1ece6832236e7e34e9baebb5d4d40490c0332b814a0d8624ca7255" +dependencies = [ + "aws-lc-rs", + "base64 0.22.1", + "chrono", + "cmpv2", + "cms", + "const-oid", + "der", + "hex", + "rand 0.10.3", + "reqwest", + "rustls-pki-types", + "rustls-webpki", + "sigstore-crypto", + "sigstore-types", + "thiserror 2.0.21", + "tracing", + "x509-cert", + "x509-tsp", +] + +[[package]] +name = "sigstore-types" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8d870c9bcfdf83396ac2bd2d6a23c5d09ec02cc9fda50fa1cbb3dd71a9bee53" +dependencies = [ + "base64 0.22.1", + "chrono", + "hex", + "pem", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "sigstore-verify" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d30aff69036e4579b416956e9ba68cec45b793116f78f10842d04c7f8b924bab" +dependencies = [ + "base64 0.22.1", + "chrono", + "cms", + "const-oid", + "hex", + "pem", + "rustls-pki-types", + "rustls-webpki", + "serde", + "serde_json", + "serde_json_canonicalizer", + "sigstore-bundle", + "sigstore-crypto", + "sigstore-merkle", + "sigstore-rekor", + "sigstore-trust-root", + "sigstore-tsa", + "sigstore-types", + "thiserror 2.0.21", + "tls_codec", + "tracing", + "x509-cert", +] + [[package]] name = "simd-adler32" version = "0.3.10" @@ -5796,6 +6696,16 @@ dependencies = [ "bitflags 2.13.2", ] +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -5839,6 +6749,33 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "strum" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9628de9b8791db39ceda2b119bbe13134770b56c138ec1d3af810d045c04f9bd" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab85eea0270ee17587ed4156089e10b9e6880ee688791d45a905f5b1ca36f664" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "svgtypes" version = "0.15.3" @@ -5849,6 +6786,16 @@ dependencies = [ "siphasher", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.119" @@ -5871,6 +6818,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + [[package]] name = "synstructure" version = "0.14.0" @@ -5891,6 +6847,27 @@ dependencies = [ "libc", ] +[[package]] +name = "system-configuration" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a13f3d0daba03132c0aa9767f98351b3488edc2c100cda2d2ec2b04f3d8d3c8b" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.9.4", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "system-deps" version = "7.0.8" @@ -5904,6 +6881,17 @@ dependencies = [ "version-compare", ] +[[package]] +name = "tar" +version = "0.4.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840" +dependencies = [ + "filetime", + "libc", + "xattr", +] + [[package]] name = "target-lexicon" version = "0.13.5" @@ -6039,6 +7027,27 @@ version = "1.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" +[[package]] +name = "tls_codec" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de2e01245e2bb89d6f05801c564fa27624dbd7b1846859876c7dad82e90bf6b" +dependencies = [ + "tls_codec_derive", + "zeroize", +] + +[[package]] +name = "tls_codec_derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2e76690929402faae40aebdda620a2c0e25dd6d3b9afe48867dfd95991f4bd" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "tokio" version = "1.53.1" @@ -6065,6 +7074,16 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "tokio-rustls" +version = "0.26.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9cc2678c2cdd569ef8215e2afd7954ada2ae20b4fdd2c5fe6139a3b02d105db" +dependencies = [ + "rustls", + "tokio", +] + [[package]] name = "tokio-stream" version = "0.1.19" @@ -6141,6 +7160,51 @@ version = "1.1.2+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags 2.13.2", + "bytes", + "futures-util", + "http", + "http-body", + "pin-project-lite", + "tower", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + [[package]] name = "tracing" version = "0.1.44" @@ -6194,6 +7258,12 @@ dependencies = [ "petgraph", ] +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "ttf-parser" version = "0.25.1" @@ -6224,6 +7294,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + [[package]] name = "uds_windows" version = "1.2.1" @@ -6301,6 +7377,18 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +[[package]] +name = "untrusted" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "url" version = "2.5.8" @@ -6319,7 +7407,7 @@ version = "0.45.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80be9b06fbae3b8b303400ab20778c80bbaf338f563afe567cf3c9eea17b47ef" dependencies = [ - "base64", + "base64 0.22.1", "data-url", "flate2", "fontdb", @@ -6427,6 +7515,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "want" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -6761,6 +7858,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-root-certs" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b96554aa2acc8ccdb7e1c9a58a7a68dd5d13bccc69cd124cb09406db612a1c9b" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "weezl" version = "0.1.12" @@ -7048,6 +8154,17 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link", + "windows-result", + "windows-strings", +] + [[package]] name = "windows-result" version = "0.4.1" @@ -7300,6 +8417,27 @@ dependencies = [ "memchr", ] +[[package]] +name = "winprint" +version = "0.2.1" +source = "git+https://github.com/storytold/winprint.rs?rev=5f80b9baf6b58937c401656634e42d33f00ddf49#5f80b9baf6b58937c401656634e42d33f00ddf49" +dependencies = [ + "flate2", + "fmt-derive", + "reqwest", + "scopeguard", + "sigstore-trust-root", + "sigstore-types", + "sigstore-verify", + "strum", + "tar", + "thiserror 2.0.21", + "windows", + "windows-core", + "windows-implement", + "xml", +] + [[package]] name = "winresource" version = "0.1.31" @@ -7388,6 +8526,41 @@ version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd" +[[package]] +name = "x509-cert" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1301e935010a701ae5f8655edc0ad17c44bad3ac5ce8c39185f75453b720ae94" +dependencies = [ + "const-oid", + "der", + "sha1", + "signature", + "spki", + "tls_codec", +] + +[[package]] +name = "x509-tsp" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5ceece934a21607055b7ac5c25adb56a2ff559804b10705dc674d1d838c15e1" +dependencies = [ + "cmpv2", + "cms", + "der", +] + +[[package]] +name = "xattr" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156" +dependencies = [ + "libc", + "rustix 1.1.5", +] + [[package]] name = "xcap" version = "0.9.8" @@ -7455,6 +8628,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" +[[package]] +name = "xml" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f45bb2c13fec6a6cb4c0f76a7e94839e110a14ec803ec2940777a94c347bc52" + [[package]] name = "xml-rs" version = "0.8.29" @@ -7652,6 +8831,26 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zeroize" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "zerotrie" version = "0.2.5" diff --git a/Cargo.toml b/Cargo.toml index 408997d343..54bd1a3a05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,10 +36,9 @@ photocraft-engine = { path = "crates/engine" } photocraft-ui-egui = { path = "crates/ui-egui" } photocraft-testkit = { path = "crates/testkit" } photocraft-tablet = { path = "crates/tablet" } -photocraft-winprint = { path = "crates/winprint" } # external -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "d991bf6792f8ade99c51358bf3c1d902c8cd96b5" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "ee70afc29a41bc50f2a55affa9aa2c62e0d28e3a" } thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 2f144ce57e..741d927a3f 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -19,7 +19,6 @@ photocraft-raster = { workspace = true } photocraft-doc = { workspace = true } photocraft-ops = { workspace = true } photocraft-compose = { workspace = true } -photocraft-winprint = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index aa8f7d2a8a..419ffd55aa 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -14,6 +14,8 @@ use std::io::Write as _; +mod windows_marks; + use photocraft_color::ColorMode; use photocraft_doc::{Document, LayerContent, SmartSource}; use serde_json::{Value, json}; @@ -457,13 +459,13 @@ fn printer_name(p: &Value) -> Result { if let Some(name) = p.get("printer").and_then(Value::as_str).filter(|v| !v.trim().is_empty()) { return Ok(name.to_string()); } - photocraft_winprint::default_printer().map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set")) + craft_print::rgb::default_printer().map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set")) } /// The driver settings chosen with Print Settings… (`printerSettings`, hex), when any. -fn printer_settings(p: &Value) -> Result> { +fn printer_settings(p: &Value) -> Result> { match p.get("printerSettings").and_then(Value::as_str).filter(|v| !v.is_empty()) { - Some(hex) => photocraft_winprint::DevMode::from_hex(hex).map(Some).map_err(|e| bad("file.print", e.to_string())), + Some(hex) => craft_print::rgb::DevMode::from_hex(hex).map(Some).map_err(|e| bad("file.print", e.to_string())), None => Ok(None), } } @@ -503,44 +505,45 @@ pub fn page_rect_top_left(page: &PrintPage) -> (f64, f64, f64, f64) { fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Result { let printer = printer_name(p)?; let settings = printer_settings(p)?; - let device_page = photocraft_winprint::page(&printer, settings.as_ref()).map_err(other)?; + let device_page = craft_print::rgb::page(&printer, settings.as_ref()).map_err(other)?; if (device_page.paper.0 - page.paper.0).abs() > 2.0 || (device_page.paper.1 - page.paper.1).abs() > 2.0 { return Err(other("the driver paper or orientation differs from the print layout; choose the matching paper in Print Settings before printing")); } let rgb = page_rgb(page)?; - let image = photocraft_winprint::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; - let marks = photocraft_winprint::Marks { corner_crop: page.marks.corner_crop, center_crop: page.marks.center_crop, registration: page.marks.registration }; + let image = craft_print::rgb::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; + let rect = page_rect_top_left(page); + let primitives = windows_marks::primitives(page.marks, rect); let color_policy = match p.get("colorHandling").and_then(Value::as_str) { - Some("photocraftManages" | "photoshopManages") => photocraft_winprint::ColorPolicy::ApplicationManaged, - Some("noColorManagement") => photocraft_winprint::ColorPolicy::Unmanaged, - _ => photocraft_winprint::ColorPolicy::DriverManaged, + Some("photocraftManages" | "photoshopManages") => craft_print::rgb::ColorPolicy::ApplicationManaged, + Some("noColorManagement") => craft_print::rgb::ColorPolicy::Unmanaged, + _ => craft_print::rgb::ColorPolicy::DriverManaged, }; - let job = photocraft_winprint::Job { + let job = craft_print::rgb::Job { printer: &printer, settings: settings.as_ref(), title, output_file: None, copies: copies.clamp(1, 999), color_policy, - marks, + primitives: &primitives, }; - let done = photocraft_winprint::print(&job, &image, page_rect_top_left(page)).map_err(other)?; + let done = craft_print::rgb::print(&job, &image, rect).map_err(other)?; Ok( json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3]}), ) } fn printers(_: &mut Session, _: &Value) -> Result { - match photocraft_winprint::printers() { + match craft_print::rgb::printers() { Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::>()})), - Err(photocraft_winprint::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})), + Err(craft_print::rgb::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})), Err(e) => Err(other(e)), } } fn printer_page(_: &mut Session, p: &Value) -> Result { let printer = printer_name(p)?; - let pg = photocraft_winprint::page(&printer, printer_settings(p)?.as_ref()).map_err(other)?; + let pg = craft_print::rgb::page(&printer, printer_settings(p)?.as_ref()).map_err(other)?; Ok( json!({"printer": printer, "paper": [pg.paper.0, pg.paper.1], "printable": [pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3], "dpi": [pg.dpi.0, pg.dpi.1]}), ) diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 023829d72d..6181097532 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -52,7 +52,7 @@ fn print_dry_run_renders_a_pdf_and_reports_the_platform_backend() { assert!(!cmd.contains(&out), "PDF bytes are streamed instead of passing a path"); } #[cfg(windows)] - assert!(cmd.last().unwrap().contains("craft-print PDF spooler")); + assert_eq!(cmd.first().map(String::as_str), Some("xps-print")); assert_eq!(r["sent"], false); assert_eq!(r["pdf"], out); let pdf = std::fs::read(&out).unwrap(); @@ -237,7 +237,7 @@ fn print_one_copy_repeats_the_last_settings() { #[cfg(unix)] assert_eq!(&cmd[..3], &["lp", "-n", "1"]); #[cfg(windows)] - assert!(cmd.last().unwrap().contains("craft-print PDF spooler")); + assert_eq!(cmd.first().map(String::as_str), Some("xps-print")); assert!(r["pdf"].is_null()); } diff --git a/crates/engine/src/print_cmds/windows_marks.rs b/crates/engine/src/print_cmds/windows_marks.rs new file mode 100644 index 0000000000..857e6335a9 --- /dev/null +++ b/crates/engine/src/print_cmds/windows_marks.rs @@ -0,0 +1,47 @@ +//! App-owned print-mark composition in points from the sheet's top-left. +use super::Marks; +use craft_print::rgb::{Primitive, Shape}; + +pub(super) fn primitives(marks: Marks, (x, y, w, h): (f64, f64, f64, f64)) -> Vec { + let mut out = Vec::with_capacity(24); + let line = |out: &mut Vec, x0, y0, x1, y1| { + out.push(Primitive { shape: Shape::Line { from: (x0, y0), to: (x1, y1) }, width: 0.3, color: [0; 3] }); + }; + let (gap, len) = (6.0, 18.0); + if marks.corner_crop { + for (cx, cy, dx, dy) in [(x, y, -1.0, -1.0), (x + w, y, 1.0, -1.0), (x, y + h, -1.0, 1.0), (x + w, y + h, 1.0, 1.0)] { + line(&mut out, cx + dx * gap, cy, cx + dx * (gap + len), cy); + line(&mut out, cx, cy + dy * gap, cx, cy + dy * (gap + len)); + } + } + if marks.center_crop { + let (mx, my) = (x + w / 2.0, y + h / 2.0); + line(&mut out, mx, y - gap, mx, y - gap - len); + line(&mut out, mx, y + h + gap, mx, y + h + gap + len); + line(&mut out, x - gap, my, x - gap - len, my); + line(&mut out, x + w + gap, my, x + w + gap + len, my); + } + if marks.registration { + let (r, off) = (5.0, gap + len / 2.0); + for (cx, cy) in [(x + w / 2.0, y - off), (x + w / 2.0, y + h + off), (x - off, y + h / 2.0), (x + w + off, y + h / 2.0)] { + out.push(Primitive { shape: Shape::Ellipse { bounds: (cx - r, cy - r, 2.0 * r, 2.0 * r) }, width: 0.3, color: [0; 3] }); + line(&mut out, cx - r * 1.6, cy, cx + r * 1.6, cy); + line(&mut out, cx, cy - r * 1.6, cx, cy + r * 1.6); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn marks_surround_the_final_image_box_in_physical_points() { + let out = primitives(Marks { corner_crop: true, center_crop: true, registration: true }, (100.0, 200.0, 144.0, 72.0)); + assert_eq!(out.len(), 24); + assert_eq!(out[0].shape, Shape::Line { from: (94.0, 200.0), to: (76.0, 200.0) }); + assert_eq!(out[12].shape, Shape::Ellipse { bounds: (167.0, 180.0, 10.0, 10.0) }); + assert!(out.iter().all(|p| p.width == 0.3 && p.color == [0; 3])); + assert!(primitives(Marks::default(), (0.0, 0.0, 10.0, 10.0)).is_empty()); + } +} diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 9925af20a9..aeefacaf2f 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -8,11 +8,11 @@ license.workspace = true workspace = true [dependencies] +craft-print = { workspace = true } craft-ui = { workspace = true } photocraft-geom = { workspace = true } photocraft-color = { workspace = true } photocraft-cms = { workspace = true } -photocraft-winprint = { workspace = true } photocraft-raster = { workspace = true } photocraft-doc = { workspace = true } photocraft-compose = { workspace = true } diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index 9805db14c3..d57a450e05 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -594,7 +594,7 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 { f.entry(k.to_string()).or_insert(v); } // Windows: the system's printers, and the paper from the chosen printer's driver settings. - if let Ok(list) = photocraft_winprint::printers() { + if let Ok(list) = craft_print::rgb::printers() { f.insert("__printers".into(), json!(list.iter().map(|p| p.name.clone()).collect::>())); let known = |n: &str| list.iter().any(|p| p.name == n); if !known(&s(&f, "printer", "")) { @@ -610,8 +610,8 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 { /// Takes the paper from the printer's driver settings (Windows), as Photoshop does. fn driver_page(f: &mut Map) { let printer = s(f, "printer", ""); - let settings = f.get("printerSettings").and_then(Value::as_str).and_then(|h| photocraft_winprint::DevMode::from_hex(h).ok()); - match photocraft_winprint::page(&printer, settings.as_ref()) { + let settings = f.get("printerSettings").and_then(Value::as_str).and_then(|h| craft_print::rgb::DevMode::from_hex(h).ok()); + match craft_print::rgb::page(&printer, settings.as_ref()) { Ok(pg) => { f.insert("paper".into(), json!([pg.paper.0, pg.paper.1])); f.insert("orientation".into(), json!("portrait")); @@ -747,8 +747,8 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map { f.insert("printerSettings".into(), json!(dm.to_hex())); driver_page(f); diff --git a/crates/winprint/Cargo.toml b/crates/winprint/Cargo.toml deleted file mode 100644 index 09504156d5..0000000000 --- a/crates/winprint/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -[package] -name = "photocraft-winprint" -version.workspace = true -edition.workspace = true -license.workspace = true -description = "Windows printing: printer list, the driver's Print Settings dialog, page geometry and GDI printing with ICM off" - -# An isolated `unsafe` helper crate like photocraft-tablet (craftrules never-crash, AGENTS.md): -# Windows printing needs Win32 calls. Only `src/windows.rs` allows `unsafe`; every block has a -# `SAFETY:` comment. Elsewhere every function returns `Unsupported`. -[lints.rust] -unsafe_code = "deny" - -[lints.clippy] -dbg_macro = "warn" -todo = "warn" - -# Already in Cargo.lock (winit/wgpu pull it in); pinned exactly: review the diff before bumping. -[target.'cfg(windows)'.dependencies] -windows-sys = { version = "=0.61.2", features = [ - "Win32_Foundation", - "Win32_Graphics_Gdi", - "Win32_Graphics_Printing", - "Win32_Storage_Xps", - "Win32_UI_ColorSystem", -] } diff --git a/crates/winprint/src/lib.rs b/crates/winprint/src/lib.rs deleted file mode 100644 index ef20e28ce1..0000000000 --- a/crates/winprint/src/lib.rs +++ /dev/null @@ -1,339 +0,0 @@ -//! Windows printing for File › Print: the system's printers, the driver's own Print Settings -//! dialog (paper, paper type, quality, the driver's colour correction and mirror), the page -//! geometry it implies, and printing an 8-bit RGB image through GDI with ICM off, so colours -//! converted with a printer profile ("PhotoCraft Manages Colors") reach the driver unchanged. -//! Corner/centre crop marks and registration marks are drawn around the image with GDI vector -//! calls (`MoveToEx`/`LineTo`/`Ellipse`), the same geometry as the PDF path's content stream. -//! Description and label text stay PDF-only (no font/text layout here yet). -//! -//! The driver settings travel as an opaque DEVMODE blob ([`DevMode`]), validated before use. -//! Everything here is a safe API; the Win32 calls live in `windows.rs`. Other platforms get -//! [`PrintError::Unsupported`]. -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] - -#[cfg(windows)] -#[allow(unsafe_code)] -mod windows; - -use std::fmt; - -/// Why a printing call failed. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum PrintError { - /// Not on Windows. - Unsupported, - /// Bad input (an unknown printer, a malformed DEVMODE, an empty image). - Invalid(String), - /// A Win32 call failed. - System(String), -} - -impl fmt::Display for PrintError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - PrintError::Unsupported => f.write_str("printing to a printer needs Windows here"), - PrintError::Invalid(m) | PrintError::System(m) => f.write_str(m), - } - } -} - -impl std::error::Error for PrintError {} - -pub type Result = std::result::Result; - -/// One installed printer. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Printer { - pub name: String, - pub default: bool, -} - -/// A printer driver's settings (a Win32 DEVMODEW with the driver's private part), as bytes. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct DevMode(Vec); - -/// Offsets inside DEVMODEW: `dmSize` and `dmDriverExtra` follow the 32-wchar device name and -/// two version words. -const DM_SIZE_AT: usize = 68; -const DM_EXTRA_AT: usize = 70; -/// The smallest DEVMODEW a driver writes (Windows 95's, up to `dmICMIntent`). -const DM_MIN: usize = 156; -/// Larger blobs are not driver settings. -const DM_MAX: usize = 1 << 20; - -impl DevMode { - /// Validates driver settings bytes: the header's own sizes must add up to the length. - pub fn from_bytes(b: Vec) -> Result { - let word = |at: usize| b.get(at..at + 2).map(|w| usize::from(u16::from_le_bytes([w[0], w[1]]))); - let (Some(size), Some(extra)) = (word(DM_SIZE_AT), word(DM_EXTRA_AT)) else { - return Err(PrintError::Invalid("printer settings are too short".into())); - }; - if b.len() > DM_MAX || size < DM_MIN || size.checked_add(extra) != Some(b.len()) { - return Err(PrintError::Invalid("printer settings are damaged; choose Print Settings again".into())); - } - Ok(DevMode(b)) - } - - pub fn as_bytes(&self) -> &[u8] { - &self.0 - } - - /// The application emits every copy as a page. Disable driver copies/collation too, - /// preserving the driver's private settings and all other public fields. - #[cfg(any(windows, test))] - pub(crate) fn for_manual_copies(&self) -> Result { - let mut bytes = self.0.clone(); - let invalid = || PrintError::Invalid("printer settings are too short".into()); - let fields: [u8; 4] = bytes.get(72..76).and_then(|b| b.try_into().ok()).ok_or_else(invalid)?; - let fields = u32::from_le_bytes(fields) | 0x100 | 0x8000; // DM_COPIES | DM_COLLATE - bytes.get_mut(72..76).ok_or_else(invalid)?.copy_from_slice(&fields.to_le_bytes()); - bytes.get_mut(86..88).ok_or_else(invalid)?.copy_from_slice(&1i16.to_le_bytes()); - bytes.get_mut(100..102).ok_or_else(invalid)?.copy_from_slice(&0i16.to_le_bytes()); - Ok(Self(bytes)) - } - - #[cfg(any(windows, test))] - pub(crate) fn has_manual_copies(&self) -> bool { - let fields = self.0.get(72..76).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(u32::from_le_bytes); - fields.is_some_and(|f| f & 0x100 != 0) - && self.0.get(86..88) == Some(1i16.to_le_bytes().as_slice()) - && self.0.get(100..102) == Some(0i16.to_le_bytes().as_slice()) - } - - /// Hex, for JSON parameters and remembered print settings. - pub fn to_hex(&self) -> String { - self.0.iter().map(|b| format!("{b:02x}")).collect() - } - - pub fn from_hex(s: &str) -> Result { - if !s.len().is_multiple_of(2) || s.len() > DM_MAX * 2 { - return Err(PrintError::Invalid("printer settings are damaged; choose Print Settings again".into())); - } - let bytes: Option> = (0..s.len()).step_by(2).map(|i| s.get(i..i + 2).and_then(|h| u8::from_str_radix(h, 16).ok())).collect(); - Self::from_bytes(bytes.ok_or_else(|| PrintError::Invalid("printer settings are damaged; choose Print Settings again".into()))?) - } -} - -/// What a printer will print on with given settings. Points (1/72 in), origin top-left. -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Page { - /// The whole sheet. - pub paper: (f64, f64), - /// The area the printer can reach: (x, y, width, height). - pub printable: (f64, f64, f64, f64), - /// Device resolution (dots per inch). - pub dpi: (u32, u32), -} - -/// An 8-bit RGB image, rows top to bottom, `width * 3` bytes each. -pub struct RgbImage<'a> { - pub width: u32, - pub height: u32, - pub data: &'a [u8], -} - -/// Printer marks drawn around the image box: corner and centre crop marks, and registration -/// marks. Matches [`photocraft_engine`]'s PDF `Marks`, mirrored here so this crate stays -/// independent of the engine; GDI draws the same geometry with vector line/ellipse calls instead -/// of PDF content stream operators. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Marks { - pub corner_crop: bool, - pub center_crop: bool, - pub registration: bool, -} - -impl Marks { - #[cfg(windows)] - pub(crate) fn any(self) -> bool { - self.corner_crop || self.center_crop || self.registration - } -} - -/// One print job. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum ColorPolicy { - /// The driver applies its color settings to the RGB image. - DriverManaged, - /// The caller has converted the pixels into the printer's RGB space. - ApplicationManaged, - /// The caller deliberately requests no DC color correction. - Unmanaged, -} - -/// One print job. -pub struct Job<'a> { - pub printer: &'a str, - pub settings: Option<&'a DevMode>, - /// The name in the print queue. - pub title: &'a str, - /// Print to this file instead of the device (drivers that write files, and tests). - pub output_file: Option<&'a str>, - pub copies: u32, - pub color_policy: ColorPolicy, - pub marks: Marks, -} - -/// Where a job went: the image box in device pixels and the device resolution. -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Printed { - pub device_rect: (i32, i32, i32, i32), - pub dpi: (u32, u32), -} - -/// Maps an image box in points (origin top-left of the sheet) to device pixels (origin at the -/// printable area's corner, as GDI draws). -pub fn device_rect(page: &Page, rect: (f64, f64, f64, f64)) -> (i32, i32, i32, i32) { - let (dx, dy) = (f64::from(page.dpi.0) / 72.0, f64::from(page.dpi.1) / 72.0); - let px = |v: f64| if v.is_finite() { v.round().clamp(-1e8, 1e8) as i32 } else { 0 }; - let (x, y, w, h) = rect; - (px((x - page.printable.0) * dx), px((y - page.printable.1) * dy), px(w * dx), px(h * dy)) -} - -/// The printers installed for this user, the default first. -pub fn printers() -> Result> { - #[cfg(windows)] - { - let mut v = windows::printers()?; - v.sort_by(|a, b| b.default.cmp(&a.default).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase()))); - Ok(v) - } - #[cfg(not(windows))] - Err(PrintError::Unsupported) -} - -/// The default printer's name. -pub fn default_printer() -> Result> { - #[cfg(windows)] - { - windows::default_printer() - } - #[cfg(not(windows))] - Err(PrintError::Unsupported) -} - -/// A printer's settings as its driver starts them. -pub fn default_settings(printer: &str) -> Result { - #[cfg(windows)] - { - windows::properties(printer, None, None, false)?.ok_or_else(|| PrintError::System("the printer driver gave no settings".into())) - } - #[cfg(not(windows))] - { - let _ = printer; - Err(PrintError::Unsupported) - } -} - -/// Shows the driver's Print Settings dialog (modal; `parent` is a window handle). `None` when -/// the user cancels. -pub fn settings_dialog(printer: &str, current: Option<&DevMode>, parent: Option) -> Result> { - #[cfg(windows)] - { - windows::properties(printer, current, parent, true) - } - #[cfg(not(windows))] - { - let _ = (printer, current, parent); - Err(PrintError::Unsupported) - } -} - -/// The sheet and printable area for `printer` with `settings`. -pub fn page(printer: &str, settings: Option<&DevMode>) -> Result { - #[cfg(windows)] - { - windows::page(printer, settings) - } - #[cfg(not(windows))] - { - let _ = (printer, settings); - Err(PrintError::Unsupported) - } -} - -/// Prints `image` into `rect` (points, origin top-left of the sheet), `job.copies` pages. -pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> Result { - let row = (image.width as usize).checked_mul(3).ok_or_else(|| PrintError::Invalid("image too large".into()))?; - if image.width == 0 || image.height == 0 || row.checked_mul(image.height as usize) != Some(image.data.len()) { - return Err(PrintError::Invalid("nothing to print".into())); - } - if ![rect.0, rect.1, rect.2, rect.3].iter().all(|v| v.is_finite()) || !(rect.2 > 0.0 && rect.3 > 0.0) { - return Err(PrintError::Invalid("the print size is empty".into())); - } - #[cfg(windows)] - { - windows::print(job, image, rect) - } - #[cfg(not(windows))] - { - let _ = job; - Err(PrintError::Unsupported) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - fn devmode(size: u16, extra: u16) -> Vec { - let mut b = vec![0u8; usize::from(size) + usize::from(extra)]; - b[DM_SIZE_AT..DM_SIZE_AT + 2].copy_from_slice(&size.to_le_bytes()); - b[DM_EXTRA_AT..DM_EXTRA_AT + 2].copy_from_slice(&extra.to_le_bytes()); - b - } - - #[test] - fn devmode_checks_its_own_sizes_and_round_trips_hex() { - let d = DevMode::from_bytes(devmode(220, 1200)).unwrap(); - assert_eq!(DevMode::from_hex(&d.to_hex()).unwrap(), d); - for bad in [vec![], vec![0; 40], devmode(100, 0), { - let mut b = devmode(220, 10); - b.pop(); - b - }] { - assert!(DevMode::from_bytes(bad).is_err()); - } - for bad in ["", "abc", "zz", &"00".repeat(300)] { - assert!(DevMode::from_hex(bad).is_err(), "{bad}"); - } - } - - #[test] - fn manual_copies_disable_driver_multiplication_without_losing_private_settings() { - let mut bytes = devmode(220, 32); - bytes[86..88].copy_from_slice(&7i16.to_le_bytes()); - bytes[100..102].copy_from_slice(&1i16.to_le_bytes()); - bytes[220..].fill(0x5a); - let original = DevMode::from_bytes(bytes).unwrap(); - let normalized = original.for_manual_copies().unwrap(); - assert!(normalized.has_manual_copies()); - assert!(!original.has_manual_copies()); - assert_eq!(&normalized.as_bytes()[220..], &original.as_bytes()[220..]); - assert_eq!(normalized.for_manual_copies().unwrap(), normalized); - let mut ignored = normalized.as_bytes().to_vec(); - ignored[72..76].copy_from_slice(&0u32.to_le_bytes()); - assert!(!DevMode::from_bytes(ignored).unwrap().has_manual_copies(), "a driver must advertise that it accepts dmCopies"); - } - - #[test] - fn device_rect_is_relative_to_the_printable_corner() { - // A3 at 360 dpi with a 3 mm (8.5 pt) unprintable margin. - let page = Page { paper: (841.89, 1190.55), printable: (8.5, 8.5, 824.89, 1173.55), dpi: (360, 360) }; - assert_eq!(device_rect(&page, (8.5, 8.5, 72.0, 36.0)), (0, 0, 360, 180)); - assert_eq!(device_rect(&page, (72.0, 144.0, 144.0, 72.0)), (318, 678, 720, 360)); - assert_eq!(device_rect(&page, (f64::NAN, f64::INFINITY, 1.0, 1.0)).0, 0); - } - - #[test] - fn print_rejects_empty_or_short_images_before_any_system_call() { - let job = Job { printer: "x", settings: None, title: "t", output_file: None, copies: 1, color_policy: ColorPolicy::Unmanaged, marks: Marks::default() }; - let short = RgbImage { width: 2, height: 2, data: &[0; 11] }; - assert!(matches!(print(&job, &short, (0.0, 0.0, 10.0, 10.0)), Err(PrintError::Invalid(_)))); - let ok = RgbImage { width: 2, height: 2, data: &[0; 12] }; - assert!(matches!(print(&job, &ok, (0.0, 0.0, 0.0, 10.0)), Err(PrintError::Invalid(_)))); - for rect in [(f64::NAN, 0.0, 10.0, 10.0), (0.0, 0.0, f64::INFINITY, 10.0)] { - assert!(matches!(print(&job, &ok, rect), Err(PrintError::Invalid(_)))); - } - } -} diff --git a/crates/winprint/src/windows.rs b/crates/winprint/src/windows.rs deleted file mode 100644 index 103309dcd0..0000000000 --- a/crates/winprint/src/windows.rs +++ /dev/null @@ -1,520 +0,0 @@ -//! The Win32 side: winspool (printers, driver settings) and GDI (page geometry, printing). -//! Every `unsafe` block says why it is sound. Buffers the system writes into are `u64`-backed so -//! the structures read from them are aligned. - -use std::ptr::{null, null_mut}; - -use windows_sys::Win32::Foundation::HWND; -use windows_sys::Win32::Graphics::Gdi::{ - BI_RGB, BITMAPINFO, BITMAPINFOHEADER, CreateDCW, CreatePen, DEVMODEW, DIB_RGB_COLORS, DM_IN_BUFFER, DM_IN_PROMPT, DM_OUT_BUFFER, DeleteDC, DeleteObject, - Ellipse, GetDeviceCaps, GetStockObject, HALFTONE, HDC, HOLLOW_BRUSH, HORZRES, LOGPIXELSX, LOGPIXELSY, LineTo, MoveToEx, PHYSICALHEIGHT, PHYSICALOFFSETX, - PHYSICALOFFSETY, PHYSICALWIDTH, PS_SOLID, SRCCOPY, SelectObject, SetBrushOrgEx, SetStretchBltMode, StretchDIBits, VERTRES, -}; -use windows_sys::Win32::Graphics::Printing::{ - ClosePrinter, DocumentPropertiesW, EnumPrintersW, GetDefaultPrinterW, OpenPrinterW, PRINTER_ENUM_CONNECTIONS, PRINTER_ENUM_LOCAL, PRINTER_HANDLE, - PRINTER_INFO_4W, -}; -use windows_sys::Win32::Storage::Xps::{AbortDoc, DOCINFOW, EndDoc, EndPage, StartDocW, StartPage}; -use windows_sys::Win32::UI::ColorSystem::{ICM_DONE_OUTSIDEDC, ICM_OFF, SetICMMode}; - -use crate::{ColorPolicy, DevMode, Job, Marks, Page, PrintError, Printed, Printer, Result, RgbImage}; - -const IDOK: i32 = 1; -const IDCANCEL: i32 = 2; -/// Wide strings read from the system stop here at the latest. -const MAX_WCHARS: usize = 32 * 1024; - -fn last_error(what: &str) -> PrintError { - PrintError::System(format!("{what} failed: {}", std::io::Error::last_os_error())) -} - -fn wide(s: &str) -> Result> { - if s.contains('\0') { - return Err(PrintError::Invalid("a name contains a NUL character".into())); - } - Ok(s.encode_utf16().chain(Some(0)).collect()) -} - -/// A zeroed, 8-byte aligned buffer of at least `bytes` bytes. -fn aligned(bytes: usize) -> Vec { - vec![0u64; bytes.div_ceil(8).max(1)] -} - -fn bytes_of(buf: &[u64], len: usize) -> Vec { - buf.iter().flat_map(|w| w.to_le_bytes()).take(len).collect() -} - -/// Reads a NUL-terminated wide string. -/// -/// # Safety -/// `p` is null or points to a NUL-terminated UTF-16 string that stays alive for the call. -unsafe fn read_wide(p: *const u16) -> String { - if p.is_null() { - return String::new(); - } - let mut n = 0; - // SAFETY: the caller guarantees a NUL-terminated string; we stop at the NUL (or the cap). - while n < MAX_WCHARS && unsafe { *p.add(n) } != 0 { - n += 1; - } - // SAFETY: the `n` units before the NUL were just read and are valid. - String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(p, n) }) -} - -pub fn printers() -> Result> { - let flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; - let (mut needed, mut count) = (0u32, 0u32); - // SAFETY: a size query: no buffer (null, 0 bytes); the out-params are valid locals. - unsafe { EnumPrintersW(flags, null(), 4, null_mut(), 0, &mut needed, &mut count) }; - if needed == 0 { - return Ok(Vec::new()); - } - let mut buf = aligned(needed as usize); - // SAFETY: `buf` holds at least `needed` writable bytes, aligned for PRINTER_INFO_4W. - let ok = unsafe { EnumPrintersW(flags, null(), 4, buf.as_mut_ptr().cast(), needed, &mut needed, &mut count) }; - if ok == 0 { - return Err(last_error("listing printers")); - } - let default = default_printer().ok().flatten(); - let infos = buf.as_ptr().cast::(); - let fits = (needed as usize) / std::mem::size_of::(); - let mut out = Vec::new(); - for i in 0..(count as usize).min(fits) { - // SAFETY: the system wrote `count` PRINTER_INFO_4W at the start of `buf` (bounded by - // the buffer size above); their name pointers point into `buf`, which is alive. - let name = unsafe { read_wide((*infos.add(i)).pPrinterName) }; - if !name.is_empty() { - out.push(Printer { default: default.as_deref() == Some(name.as_str()), name }); - } - } - Ok(out) -} - -pub fn default_printer() -> Result> { - let mut len = 0u32; - // SAFETY: a size query with a null buffer; `len` is a valid local. - unsafe { GetDefaultPrinterW(null_mut(), &mut len) }; - if len == 0 || len as usize > MAX_WCHARS { - return Ok(None); - } - let mut buf = vec![0u16; len as usize]; - // SAFETY: `buf` holds `len` writable wide chars. - if unsafe { GetDefaultPrinterW(buf.as_mut_ptr(), &mut len) } == 0 { - return Ok(None); - } - // SAFETY: on success the buffer holds a NUL-terminated name. - Ok(Some(unsafe { read_wide(buf.as_ptr()) })) -} - -/// An open printer handle, closed on drop. -struct OpenPrinter(PRINTER_HANDLE); - -impl OpenPrinter { - fn open(name: &[u16]) -> Result { - let mut h = PRINTER_HANDLE { Value: null_mut() }; - // SAFETY: `name` is NUL-terminated; `h` is a valid out-param; no defaults. - if unsafe { OpenPrinterW(name.as_ptr(), &mut h, null()) } == 0 || h.Value.is_null() { - return Err(last_error("opening the printer")); - } - Ok(OpenPrinter(h)) - } -} - -impl Drop for OpenPrinter { - fn drop(&mut self) { - // SAFETY: the handle came from OpenPrinterW and is closed once. - unsafe { ClosePrinter(self.0) }; - } -} - -/// `settings` copied into an aligned buffer the driver can read. -/// -/// There is deliberately no check of the printer name stored in the settings: Windows keeps only -/// 31 characters of it, and drivers fill it differently, so a long queue name ("Sublimation - -/// EPSON ET-18100 Series") failed such a check and the settings were silently dropped. The driver -/// itself validates them instead (see [`Dc::create`]). -fn aligned_copy(settings: &DevMode) -> Vec { - let b = settings.as_bytes(); - let mut buf = aligned(b.len()); - for (slot, chunk) in buf.iter_mut().zip(b.chunks(8)) { - let mut w = [0u8; 8]; - w[..chunk.len()].copy_from_slice(chunk); - *slot = u64::from_le_bytes(w); - } - buf -} - -pub fn properties(printer: &str, current: Option<&DevMode>, parent: Option, prompt: bool) -> Result> { - let name = wide(printer)?; - let p = OpenPrinter::open(&name)?; - let hwnd: HWND = parent.map_or(null_mut(), |h| h as HWND); - // SAFETY: with fmode 0 DocumentPropertiesW only returns the size the settings need. - let needed = unsafe { DocumentPropertiesW(hwnd, p.0, name.as_ptr(), null_mut(), null(), 0) }; - if needed <= 0 || needed as usize > (1 << 20) { - return Err(last_error("reading the printer's settings")); - } - let mut out = aligned(needed as usize); - let input = current.map(aligned_copy); - let mut mode = DM_OUT_BUFFER; - if input.is_some() { - mode |= DM_IN_BUFFER; - } - if prompt { - mode |= DM_IN_PROMPT; - } - let inp: *const DEVMODEW = input.as_ref().map_or(null(), |b| b.as_ptr().cast()); - // SAFETY: `out` holds `needed` aligned writable bytes as the size query asked for; `inp` is - // null or an aligned copy of settings written by this printer's driver. - let r = unsafe { DocumentPropertiesW(hwnd, p.0, name.as_ptr(), out.as_mut_ptr().cast(), inp, mode) }; - match r { - IDOK => { - let all = bytes_of(&out, needed as usize); - let word = |at: usize| all.get(at..at + 2).map_or(0, |w| usize::from(u16::from_le_bytes([w[0], w[1]]))); - let len = (word(68) + word(70)).min(all.len()); - DevMode::from_bytes(all.into_iter().take(len).collect()).map(Some) - } - IDCANCEL => Ok(None), - _ => Err(last_error("the printer's settings dialog")), - } -} - -/// A printer device context, deleted on drop. -struct Dc(HDC); - -impl Dc { - fn create(printer: &str, settings: Option<&DevMode>, manual_copies: bool) -> Result { - let name = wide(printer)?; - let normalized = if manual_copies { - let defaults = match settings { - Some(s) => s.clone(), - None => properties(printer, None, None, false)?.ok_or_else(|| PrintError::Invalid("the printer refused its default settings".into()))?, - }; - Some(defaults.for_manual_copies()?) - } else { - None - }; - let settings = normalized.as_ref().or(settings); - // The driver validates and completes the settings first (DM_IN_BUFFER | DM_OUT_BUFFER, no - // dialog). If it refuses them, that is an error: never print on the defaults instead. - let merged = match settings { - Some(s) => Some( - properties(printer, Some(s), None, false)? - .ok_or_else(|| PrintError::Invalid(format!("printer “{printer}” refused these settings; choose Print Settings… again")))?, - ), - None => None, - }; - if manual_copies && !merged.as_ref().is_some_and(DevMode::has_manual_copies) { - return Err(PrintError::Invalid("the driver refused single-copy settings; no print job was sent".into())); - } - let dm = merged.as_ref().map(aligned_copy); - let dmp: *const DEVMODEW = dm.as_ref().map_or(null(), |b| b.as_ptr().cast()); - // SAFETY: `name` is NUL-terminated; `dmp` is null or aligned settings from this - // printer's driver, alive for the call. - let hdc = unsafe { CreateDCW(null(), name.as_ptr(), null(), dmp) }; - if hdc.is_null() { - return Err(last_error(&format!("opening printer “{printer}”"))); - } - Ok(Dc(hdc)) - } - - fn cap(&self, index: u32) -> i32 { - // SAFETY: a valid printer DC and a documented capability index. - unsafe { GetDeviceCaps(self.0, index as i32) } - } - - fn page(&self) -> Result { - let (dx, dy) = (self.cap(LOGPIXELSX), self.cap(LOGPIXELSY)); - if dx <= 0 || dy <= 0 { - return Err(PrintError::System("the printer reported no resolution".into())); - } - let (fx, fy) = (72.0 / f64::from(dx), 72.0 / f64::from(dy)); - let (pw, ph) = (self.cap(PHYSICALWIDTH), self.cap(PHYSICALHEIGHT)); - let (ox, oy) = (self.cap(PHYSICALOFFSETX), self.cap(PHYSICALOFFSETY)); - let (hw, vh) = (self.cap(HORZRES), self.cap(VERTRES)); - let (pw, ph) = if pw > 0 && ph > 0 { (pw, ph) } else { (hw, vh) }; - Ok(Page { - paper: (f64::from(pw) * fx, f64::from(ph) * fy), - printable: (f64::from(ox) * fx, f64::from(oy) * fy, f64::from(hw) * fx, f64::from(vh) * fy), - dpi: (dx as u32, dy as u32), - }) - } -} - -impl Drop for Dc { - fn drop(&mut self) { - // SAFETY: the DC came from CreateDCW and is deleted once. - unsafe { DeleteDC(self.0) }; - } -} - -pub fn page(printer: &str, settings: Option<&DevMode>) -> Result { - Dc::create(printer, settings, false)?.page() -} - -/// RGB rows top-down → a bottom-up 24-bit DIB (BGR, rows padded to 4 bytes). -fn dib(image: &RgbImage<'_>) -> Result<(BITMAPINFO, Vec)> { - let (w, h) = (image.width as usize, image.height as usize); - let stride = (w.checked_mul(3).ok_or_else(|| PrintError::Invalid("image too large".into()))? + 3) & !3; - let total = stride.checked_mul(h).filter(|t| *t <= i32::MAX as usize).ok_or_else(|| PrintError::Invalid("image too large to print in one piece".into()))?; - let mut bits = vec![0u8; total]; - for (y, src) in image.data.chunks_exact(w * 3).enumerate() { - let start = (h - 1 - y) * stride; - let Some(dst) = bits.get_mut(start..start + w * 3) else { continue }; - for (d, s) in dst.as_chunks_mut::<3>().0.iter_mut().zip(src.as_chunks::<3>().0) { - *d = [s[2], s[1], s[0]]; - } - } - let header = BITMAPINFOHEADER { - biSize: std::mem::size_of::() as u32, - biWidth: image.width as i32, - biHeight: image.height as i32, - biPlanes: 1, - biBitCount: 24, - biCompression: BI_RGB, - biSizeImage: total as u32, - ..Default::default() - }; - Ok((BITMAPINFO { bmiHeader: header, bmiColors: [Default::default()] }, bits)) -} - -/// Vector marks around the image box `(x, y, w, h)` (device pixels, origin top-left, as GDI -/// draws). Same geometry as `photocraft_engine::print_cmds::marks_ops`'s PDF content stream, with -/// the vertical sense flipped (PDF's origin is bottom-left; GDI's is top-left), scaled from points -/// to device pixels with `dpi` instead of PDF's user-space points. -/// -/// # Safety -/// `dc` is a valid, selected device context for the current page. -unsafe fn draw_marks(dc: HDC, marks: Marks, (x, y, w, h): (i32, i32, i32, i32), dpi: (u32, u32)) -> Result<()> { - let (sx, sy) = (f64::from(dpi.0) / 72.0, f64::from(dpi.1) / 72.0); - let px = |v: f64| v.round() as i32; - let (gap_x, gap_y) = (px(6.0 * sx), px(6.0 * sy)); - let (len_x, len_y) = (px(18.0 * sx), px(18.0 * sy)); - let pen_width = px(0.3 * sx).max(1); - // SAFETY: `dc` is valid; the pen and brush are deleted/restored before returning. - let pen = unsafe { CreatePen(PS_SOLID, pen_width, 0x0000_0000) }; - let hollow = unsafe { GetStockObject(HOLLOW_BRUSH) }; - let old_pen = unsafe { SelectObject(dc, pen) }; - let old_brush = unsafe { SelectObject(dc, hollow) }; - let ok = std::cell::Cell::new(true); - let line = |x0: i32, y0: i32, x1: i32, y1: i32| { - // SAFETY: `dc` is valid for the duration of this call. - ok.set(ok.get() && unsafe { MoveToEx(dc, x0, y0, null_mut()) != 0 && LineTo(dc, x1, y1) != 0 }); - }; - if marks.corner_crop { - for (cx, cy, dx, dy) in [(x, y, -1, -1), (x + w, y, 1, -1), (x, y + h, -1, 1), (x + w, y + h, 1, 1)] { - line(cx + dx * gap_x, cy, cx + dx * (gap_x + len_x), cy); - line(cx, cy + dy * gap_y, cx, cy + dy * (gap_y + len_y)); - } - } - if marks.center_crop { - let (mx, my) = (x + w / 2, y + h / 2); - line(mx, y - gap_y, mx, y - gap_y - len_y); - line(mx, y + h + gap_y, mx, y + h + gap_y + len_y); - line(x - gap_x, my, x - gap_x - len_x, my); - line(x + w + gap_x, my, x + w + gap_x + len_x, my); - } - if marks.registration { - let (rx, ry) = (px(5.0 * sx), px(5.0 * sy)); - let (off_x, off_y) = (gap_x + len_x / 2, gap_y + len_y / 2); - for (cx, cy) in [(x + w / 2, y - off_y), (x + w / 2, y + h + off_y), (x - off_x, y + h / 2), (x + w + off_x, y + h / 2)] { - // SAFETY: `dc` is valid for the duration of this call. - ok.set(ok.get() && unsafe { Ellipse(dc, cx - rx, cy - ry, cx + rx, cy + ry) != 0 }); - line(cx - (rx as f64 * 1.6) as i32, cy, cx + (rx as f64 * 1.6) as i32, cy); - line(cx, cy - (ry as f64 * 1.6) as i32, cx, cy + (ry as f64 * 1.6) as i32); - } - } - // SAFETY: restores the DC's previous pen/brush, then frees the ones created above. - unsafe { - SelectObject(dc, old_pen); - SelectObject(dc, old_brush); - DeleteObject(pen); - } - if ok.get() { Ok(()) } else { Err(last_error("drawing printer marks")) } -} - -pub fn print(job: &Job<'_>, image: &RgbImage<'_>, rect: (f64, f64, f64, f64)) -> Result { - if image.width > i32::MAX as u32 || image.height > i32::MAX as u32 { - return Err(PrintError::Invalid("image too large".into())); - } - let dc = Dc::create(job.printer, job.settings, true)?; - let page = dc.page()?; - let (x, y, w, h) = crate::device_rect(&page, rect); - let (bmi, bits) = dib(image)?; - let title = wide(job.title)?; - let output = job.output_file.map(wide).transpose()?; - let info = DOCINFOW { - cbSize: std::mem::size_of::() as i32, - lpszDocName: title.as_ptr(), - lpszOutput: output.as_ref().map_or(null(), |o| o.as_ptr()), - lpszDatatype: null(), - fwType: 0, - }; - let color_mode = match job.color_policy { - ColorPolicy::DriverManaged => ICM_OFF, - // ICM_OFF enables old-style halftone correction. Pixels converted by the caller - // need both DC conversion and that correction off (Microsoft SetICMMode contract). - ColorPolicy::ApplicationManaged | ColorPolicy::Unmanaged => ICM_DONE_OUTSIDEDC, - }; - let set_color_mode = || { - // SAFETY: a valid printer DC and a documented SetICMMode value. - if unsafe { SetICMMode(dc.0, color_mode) } == 0 { Err(last_error("setting GDI color policy")) } else { Ok(()) } - }; - set_color_mode()?; - // SAFETY: a valid DC; HALFTONE resampling needs the brush origin reset afterwards. - unsafe { - SetStretchBltMode(dc.0, HALFTONE); - SetBrushOrgEx(dc.0, 0, 0, null_mut()); - } - // SAFETY: `info` and the strings it points to live until EndDoc/AbortDoc below. - if unsafe { StartDocW(dc.0, &info) } <= 0 { - return Err(last_error("starting the print job")); - } - let fail = |what: &str| { - let e = last_error(what); - // SAFETY: the job was started on this DC. - unsafe { AbortDoc(dc.0) }; - e - }; - for _ in 0..job.copies.clamp(1, 999) { - // SAFETY: inside a started document on a valid DC. - if unsafe { StartPage(dc.0) } <= 0 { - return Err(fail("starting a page")); - } - // StartPage saves/restores DC state. Reapply the policy for each page, before pixels. - if let Err(error) = set_color_mode() { - // SAFETY: this DC owns the document started above. - unsafe { AbortDoc(dc.0) }; - return Err(error); - } - // SAFETY: `bits` is a bottom-up DIB exactly as `bmi` describes (24-bit, padded rows, - // `biSizeImage` bytes); the source rectangle is the whole image. - let lines = - unsafe { StretchDIBits(dc.0, x, y, w, h, 0, 0, image.width as i32, image.height as i32, bits.as_ptr().cast(), &bmi, DIB_RGB_COLORS, SRCCOPY) }; - if lines <= 0 { - return Err(fail("sending the image to the printer")); - } - if job.marks.any() { - // SAFETY: `dc.0` is the valid, started page's device context. - if let Err(e) = unsafe { draw_marks(dc.0, job.marks, (x, y, w, h), page.dpi) } { - return Err(fail(&e.to_string())); - } - } - // SAFETY: closes the page started above. - if unsafe { EndPage(dc.0) } <= 0 { - return Err(fail("finishing a page")); - } - } - // SAFETY: closes the document started above. - if unsafe { EndDoc(dc.0) } <= 0 { - return Err(last_error("finishing the print job")); - } - Ok(Printed { device_rect: (x, y, w, h), dpi: page.dpi }) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn dib_is_bottom_up_bgr_with_padded_rows() { - // 2×2: red, green / blue, white. - let data = [255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255]; - let (bmi, bits) = dib(&RgbImage { width: 2, height: 2, data: &data }).unwrap(); - assert_eq!(bmi.bmiHeader.biHeight, 2); - assert_eq!(bits.len(), 16, "6 bytes per row padded to 8"); - // First stored row is the bottom one: blue, white. - assert_eq!(&bits[0..6], &[255, 0, 0, 255, 255, 255]); - assert_eq!(&bits[8..14], &[0, 0, 255, 0, 255, 0]); - } - - #[test] - fn settings_are_copied_aligned_and_intact() { - let mut b: Vec = (0..230u32).map(|i| (i * 7) as u8).collect(); - b[68..70].copy_from_slice(&220u16.to_le_bytes()); - b[70..72].copy_from_slice(&10u16.to_le_bytes()); - let d = DevMode::from_bytes(b.clone()).unwrap(); - let buf = aligned_copy(&d); - assert_eq!(buf.as_ptr().align_offset(std::mem::align_of::()), 0); - assert_eq!(bytes_of(&buf, b.len()), b, "no name check, no truncation: the bytes go through as chosen"); - } - - /// Settings chosen for a printer are what the driver prints with: paper and resolution survive - /// the driver's own validation (needs "Microsoft Print to PDF"; run by hand). - #[test] - #[ignore] - fn chosen_settings_reach_the_device_context() { - let printer = "Microsoft Print to PDF"; - let d = crate::default_settings(printer).unwrap(); - let mut b = d.as_bytes().to_vec(); - // dmPaperSize = DMPAPER_A3 (8); dmFields |= DM_PAPERSIZE (2). - b[78..80].copy_from_slice(&8i16.to_le_bytes()); - let fields = u32::from_le_bytes([b[72], b[73], b[74], b[75]]) | 2; - b[72..76].copy_from_slice(&fields.to_le_bytes()); - let a3 = DevMode::from_bytes(b).unwrap(); - let pg = crate::page(printer, Some(&a3)).unwrap(); - assert!((pg.paper.0 - 841.9).abs() < 2.0 && (pg.paper.1 - 1190.6).abs() < 2.0, "A3, not the default: {:?}", pg.paper); - // Landscape (dmOrientation = 2, DM_ORIENTATION = 1): the page comes back wide. - let mut b = a3.as_bytes().to_vec(); - b[76..78].copy_from_slice(&2i16.to_le_bytes()); - let fields = u32::from_le_bytes([b[72], b[73], b[74], b[75]]) | 1; - b[72..76].copy_from_slice(&fields.to_le_bytes()); - let pg = crate::page(printer, Some(&DevMode::from_bytes(b).unwrap())).unwrap(); - assert!((pg.paper.0 - 1190.6).abs() < 2.0 && (pg.paper.1 - 841.9).abs() < 2.0, "landscape A3: {:?}", pg.paper); - } - - /// A real job through Windows' "Microsoft Print to PDF" driver, written to a file (needs that - /// printer installed; run by hand). - #[test] - #[ignore] - fn prints_through_a_real_driver_to_a_file() { - let out = std::env::temp_dir().join(format!("pc-winprint-{}.pdf", std::process::id())); - let _ = std::fs::remove_file(&out); - let settings = crate::default_settings("Microsoft Print to PDF").unwrap(); - let mut bytes = settings.as_bytes().to_vec(); - bytes[86..88].copy_from_slice(&5i16.to_le_bytes()); - let fields = u32::from_le_bytes(bytes[72..76].try_into().unwrap()) | 0x100; - bytes[72..76].copy_from_slice(&fields.to_le_bytes()); - let settings = DevMode::from_bytes(bytes).unwrap(); - let (w, h) = (300u32, 200u32); - let data: Vec = (0..w * h).flat_map(|i| if (i % w) < w / 2 { [220, 30, 30] } else { [30, 30, 220] }).collect(); - let out_s = out.to_string_lossy().into_owned(); - let marks = crate::Marks { corner_crop: true, center_crop: true, registration: true }; - let job = Job { - printer: "Microsoft Print to PDF", - settings: Some(&settings), - title: "PhotoCraft test", - output_file: Some(&out_s), - copies: 2, - color_policy: ColorPolicy::ApplicationManaged, - marks, - }; - let done = crate::print(&job, &RgbImage { width: w, height: h, data: &data }, (72.0, 72.0, 216.0, 144.0)).unwrap(); - assert_eq!(done.device_rect.2, done.dpi.0 as i32 * 3, "3 inches wide"); - let pdf = std::fs::read(&out).unwrap(); - assert!(pdf.starts_with(b"%PDF"), "the driver wrote a PDF"); - assert!(pdf.len() > 1000, "the page has content"); - // The driver vectorises our GDI line/ellipse calls as real PDF content: a job with every - // mark on is meaningfully bigger than the same image with marks off. - let no_marks_job = Job { marks: crate::Marks::default(), ..job }; - let done2 = crate::print(&no_marks_job, &RgbImage { width: w, height: h, data: &data }, (72.0, 72.0, 216.0, 144.0)).unwrap(); - let _ = done2; - let pdf_no_marks = std::fs::read(&out).unwrap(); - assert!(pdf.len() > pdf_no_marks.len() + 100, "marks add vector content: {} vs {}", pdf.len(), pdf_no_marks.len()); - if let Ok(evidence) = std::env::var("PHOTOCRAFT_WINPRINT_TEST_EVIDENCE") { - let evidence = std::path::Path::new(&evidence); - std::fs::create_dir_all(evidence).unwrap(); - std::fs::write(evidence.join("copies2-driver5-marks.pdf"), &pdf).unwrap(); - std::fs::write(evidence.join("copies2-driver5-no-marks.pdf"), &pdf_no_marks).unwrap(); - } - let _ = std::fs::remove_file(&out); - } - - /// Lists this machine's printers and their pages (needs real printers; run by hand). - #[test] - #[ignore] - fn lists_real_printers() { - for p in printers().unwrap() { - println!("{}{} {:?}", p.name, if p.default { " (default)" } else { "" }, page(&p.name, None)); - } - } -} diff --git a/xtask/src/layers.rs b/xtask/src/layers.rs index dcb66f7120..e78613f37a 100644 --- a/xtask/src/layers.rs +++ b/xtask/src/layers.rs @@ -47,7 +47,6 @@ pub const TABLE: &[(&str, Class)] = &[ // Pen tablet input (the one isolated `unsafe` helper: AppKit interop on macOS). ("tablet", Class::Standalone), // Windows printing (an isolated `unsafe` helper: winspool and GDI). - ("winprint", Class::Standalone), ("doc", Class::Layer(1)), ("ops", Class::Layer(2)), ("paint", Class::Layer(2)), From 4de93daa912cdfe803984b0686c989a5b0490bad Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 04:25:32 +0200 Subject: [PATCH 08/39] Pin pre-submission annotation visibility guard --- Cargo.lock | 28 ++++++++++++++-------------- Cargo.toml | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ba37f36a7..9b052103d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -849,7 +849,7 @@ dependencies = [ "maybe-owned", "rustix 1.1.5", "rustix-linux-procfs", - "windows-sys 0.61.2", + "windows-sys 0.60.2", "winx", ] @@ -1225,7 +1225,7 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ee70afc29a41bc50f2a55affa9aa2c62e0d28e3a#ee70afc29a41bc50f2a55affa9aa2c62e0d28e3a" +source = "git+https://github.com/storytold/craft-libs?rev=6ba4d0b97ad09767c588d82ff9c988eea53f0546#6ba4d0b97ad09767c588d82ff9c988eea53f0546" dependencies = [ "craft-winprint-gdi", "miniz_oxide 0.8.9", @@ -1891,7 +1891,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -3028,7 +3028,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -3690,7 +3690,7 @@ dependencies = [ "objc2-foundation 0.3.2", "png 0.18.1", "thiserror 2.0.21", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -4436,7 +4436,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5454,7 +5454,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -5973,7 +5973,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -6040,7 +6040,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -6678,7 +6678,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -6905,10 +6905,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix 1.1.5", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7308,7 +7308,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -8068,7 +8068,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 54bd1a3a05..9d289b3438 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ photocraft-testkit = { path = "crates/testkit" } photocraft-tablet = { path = "crates/tablet" } # external -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "ee70afc29a41bc50f2a55affa9aa2c62e0d28e3a" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "6ba4d0b97ad09767c588d82ff9c988eea53f0546" } thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" From 70059ac1b43e4679c462f77efc1d0a2ef63de0ae Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 04:56:32 +0200 Subject: [PATCH 09/39] Pin driver option retention qualification fixes --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b052103d1..5c5b43b827 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1225,7 +1225,7 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=6ba4d0b97ad09767c588d82ff9c988eea53f0546#6ba4d0b97ad09767c588d82ff9c988eea53f0546" +source = "git+https://github.com/storytold/craft-libs?rev=d873514e8a773f02f54c34ba890e57c02c3a36f6#d873514e8a773f02f54c34ba890e57c02c3a36f6" dependencies = [ "craft-winprint-gdi", "miniz_oxide 0.8.9", diff --git a/Cargo.toml b/Cargo.toml index 9d289b3438..cd5017ba68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ photocraft-testkit = { path = "crates/testkit" } photocraft-tablet = { path = "crates/tablet" } # external -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "6ba4d0b97ad09767c588d82ff9c988eea53f0546" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "d873514e8a773f02f54c34ba890e57c02c3a36f6" } thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" From d6fbac1720941a477333c20c70948efd9365fd04 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 05:04:13 +0200 Subject: [PATCH 10/39] Pin qualification fixes integrated with current print progress --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c5b43b827..362a874cbb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1225,7 +1225,7 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=d873514e8a773f02f54c34ba890e57c02c3a36f6#d873514e8a773f02f54c34ba890e57c02c3a36f6" +source = "git+https://github.com/storytold/craft-libs?rev=3d37da389a3618ab9b76109da11416e9ca4ea719#3d37da389a3618ab9b76109da11416e9ca4ea719" dependencies = [ "craft-winprint-gdi", "miniz_oxide 0.8.9", diff --git a/Cargo.toml b/Cargo.toml index cd5017ba68..2c329d980d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ photocraft-testkit = { path = "crates/testkit" } photocraft-tablet = { path = "crates/tablet" } # external -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "d873514e8a773f02f54c34ba890e57c02c3a36f6" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "3d37da389a3618ab9b76109da11416e9ca4ea719" } thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" From 8f56cd5be5a8b3a384da888b6c769b0b8353dfc4 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 05:54:57 +0200 Subject: [PATCH 11/39] Preserve explicit PDF export before disabled submission refusal --- crates/engine/src/print_cmds.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 0235d3af11..9361f7d2b4 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -390,17 +390,12 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { let send = p.get("send").and_then(Value::as_bool).unwrap_or(output.is_none()); let copies = crate::commands::int(p, "copies").unwrap_or(1).clamp(1, 999) as u32; let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false); - // Apply the process-wide test/fuzz guard before either native transport can reach a driver. - if send && !dry && printing_disabled() { - return Err(other( - "printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true", - )); - } // Shared RGB transport handles compatible Windows pages; PDF-only content retains its route. // Sending follows the shared command contract; fuzz tests explicitly request dryRun. // The worker returns its result before repeat settings and hooks are applied once. let handling = p.get("colorHandling").and_then(Value::as_str).unwrap_or("printerManages"); if cfg!(windows) && send && !dry && output.is_none() && native_rgb_compatible(&page, handling) { + require_printing_enabled()?; let printed = print_windows(&page, p, &doc.name, copies)?; return Ok(json!({ "pdf": Value::Null, "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0, @@ -431,6 +426,7 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { let mut sent = false; let mut spool = Value::Null; if send && !dry { + require_printing_enabled()?; spool = json!(craft_print::submit(&pdf, &job).map_err(other)?); sent = true; } @@ -569,6 +565,14 @@ pub fn printing_disabled() -> bool { PRINTING_DISABLED.load(std::sync::atomic::Ordering::Relaxed) || std::env::var_os("PHOTOCRAFT_NO_PRINT").is_some_and(|v| !v.is_empty() && v != "0") } +fn require_printing_enabled() -> Result<()> { + if printing_disabled() { + Err(other("printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true")) + } else { + Ok(()) + } +} + fn print(s: &mut Session, p: &Value) -> Result { do_print(s, p, "file.print") } From 3454620f8933241ba3575a26c48c6e9a54f0e75f Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 06:04:03 +0200 Subject: [PATCH 12/39] Format shared print-disable error --- crates/engine/src/print_cmds.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 9361f7d2b4..87862b65f6 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -567,7 +567,9 @@ pub fn printing_disabled() -> bool { fn require_printing_enabled() -> Result<()> { if printing_disabled() { - Err(other("printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true")) + Err(other( + "printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true", + )) } else { Ok(()) } From 06387a955c960b3ced7c4267b35e39814c9ae411 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 06:36:33 +0200 Subject: [PATCH 13/39] Apply CI formatter output for print guard --- crates/engine/src/print_cmds.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 87862b65f6..9361f7d2b4 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -567,9 +567,7 @@ pub fn printing_disabled() -> bool { fn require_printing_enabled() -> Result<()> { if printing_disabled() { - Err(other( - "printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true", - )) + Err(other("printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true")) } else { Ok(()) } From f85eddf3cc8f1d1934822c4343f42cc2dc8c9075 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 07:20:25 +0200 Subject: [PATCH 14/39] Complete native printing translations and Serbian theme labels --- crates/ui-egui/src/i18n/cs.tsv | 7 +++++++ crates/ui-egui/src/i18n/de.tsv | 7 +++++++ crates/ui-egui/src/i18n/el.tsv | 7 +++++++ crates/ui-egui/src/i18n/es.tsv | 7 +++++++ crates/ui-egui/src/i18n/fr.tsv | 7 +++++++ crates/ui-egui/src/i18n/hu.tsv | 7 +++++++ crates/ui-egui/src/i18n/id.tsv | 7 +++++++ crates/ui-egui/src/i18n/it.tsv | 7 +++++++ crates/ui-egui/src/i18n/ja.tsv | 7 +++++++ crates/ui-egui/src/i18n/ko.tsv | 7 +++++++ crates/ui-egui/src/i18n/nl.tsv | 7 +++++++ crates/ui-egui/src/i18n/pl.tsv | 7 +++++++ crates/ui-egui/src/i18n/pt-br.tsv | 7 +++++++ crates/ui-egui/src/i18n/ru.tsv | 7 +++++++ crates/ui-egui/src/i18n/sr.tsv | 11 +++++++++++ crates/ui-egui/src/i18n/tr.tsv | 7 +++++++ crates/ui-egui/src/i18n/uk.tsv | 7 +++++++ crates/ui-egui/src/i18n/vi.tsv | 7 +++++++ crates/ui-egui/src/i18n/zh-hans.tsv | 7 +++++++ crates/ui-egui/src/i18n/zh-hant.tsv | 7 +++++++ 20 files changed, 144 insertions(+) diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv index 4fdabb4406..312fca6fba 100644 --- a/crates/ui-egui/src/i18n/cs.tsv +++ b/crates/ui-egui/src/i18n/cs.tsv @@ -3703,3 +3703,10 @@ navigator Zoom Zvětšení Step {step} is waiting for its dialog Krok {step} čeká na svůj dialog @plural {n} action|{n} actions {n} akce|{n} akce|{n} akcí Auto Add/Delete Automaticky přidat/odstranit + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Nezapínejte současně zrcadlení v nastavení tisku: obě převrácení se navzájem zruší. + Print Settings… Nastavení tisku… + Remember to turn off the printer's own colour management in Print Settings… Nezapomeňte v nastavení tisku vypnout vlastní správu barev tiskárny… + Sent to {printer} Odesláno do {printer} + set in Print Settings… nastavuje se v nastavení tisku… diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv index be910dda32..86dbf56351 100644 --- a/crates/ui-egui/src/i18n/de.tsv +++ b/crates/ui-egui/src/i18n/de.tsv @@ -2735,3 +2735,10 @@ curves Smooth Glätten Step {step} is waiting for its dialog Schritt {step} wartet auf seinen Dialog @plural {n} action|{n} actions {n} Aktion|{n} Aktionen Auto Add/Delete Autom. hinzuf./löschen + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Aktiviere nicht zusätzlich das Spiegelbild in den Druckeinstellungen: Die beiden Spiegelungen heben sich auf. + Print Settings… Druckeinstellungen… + Remember to turn off the printer's own colour management in Print Settings… Denke daran, die eigene Farbverwaltung des Druckers in den Druckeinstellungen auszuschalten… + Sent to {printer} An {printer} gesendet + set in Print Settings… in den Druckeinstellungen festlegen… diff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsv index d02c1318aa..94ebb84f0e 100644 --- a/crates/ui-egui/src/i18n/el.tsv +++ b/crates/ui-egui/src/i18n/el.tsv @@ -3793,3 +3793,10 @@ curves Smooth Εξομάλυνση Step {step} is waiting for its dialog Το βήμα {step} περιμένει το παράθυρο διαλόγου του @plural {n} action|{n} actions {n} ενέργεια|{n} ενέργειες Auto Add/Delete Αυτόματη προσθήκη/διαγραφή + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Μην ενεργοποιείτε επίσης τον κατοπτρισμό στις ρυθμίσεις εκτύπωσης: οι δύο αναστροφές αλληλοαναιρούνται. + Print Settings… Ρυθμίσεις εκτύπωσης… + Remember to turn off the printer's own colour management in Print Settings… Θυμηθείτε να απενεργοποιήσετε τη διαχείριση χρωμάτων του εκτυπωτή στις ρυθμίσεις εκτύπωσης… + Sent to {printer} Στάλθηκε στον {printer} + set in Print Settings… ορίζεται στις ρυθμίσεις εκτύπωσης… diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv index cdee7d44b9..f3f9893f2a 100644 --- a/crates/ui-egui/src/i18n/es.tsv +++ b/crates/ui-egui/src/i18n/es.tsv @@ -2776,3 +2776,10 @@ curves Smooth Suavizar Step {step} is waiting for its dialog El paso {step} está esperando su cuadro de diálogo @plural {n} action|{n} actions {n} acción|{n} acciones Auto Add/Delete Añadir/eliminar automáticamente + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. No actives también Imagen especular en Ajustes de impresión: las dos inversiones se anulan. + Print Settings… Ajustes de impresión… + Remember to turn off the printer's own colour management in Print Settings… Recuerda desactivar la gestión de color de la impresora en Ajustes de impresión… + Sent to {printer} Enviado a {printer} + set in Print Settings… se establece en Ajustes de impresión… diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv index daf5447d76..b0a4aedb6c 100644 --- a/crates/ui-egui/src/i18n/fr.tsv +++ b/crates/ui-egui/src/i18n/fr.tsv @@ -3096,3 +3096,10 @@ curves Smooth Lisser Step {step} is waiting for its dialog L’étape {step} attend sa boîte de dialogue @plural {n} action|{n} actions {n} action|{n} actions Auto Add/Delete Ajout/suppr. auto + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Ne cochez pas aussi Image miroir dans les paramètres d’impression : les deux inversions s’annulent. + Print Settings… Paramètres d’impression… + Remember to turn off the printer's own colour management in Print Settings… Pensez à désactiver la gestion des couleurs de l’imprimante dans les paramètres d’impression… + Sent to {printer} Envoyé à {printer} + set in Print Settings… défini dans les paramètres d’impression… diff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsv index ef7905b881..d0da3a93c7 100644 --- a/crates/ui-egui/src/i18n/hu.tsv +++ b/crates/ui-egui/src/i18n/hu.tsv @@ -4016,3 +4016,10 @@ cameraRaw Yellow Sárga Loaded Camera Raw settings from {file} Camera Raw-beállítások betöltve innen: {file} Loaded Camera Raw settings from {file} ({n} not supported) Camera Raw-beállítások betöltve innen: {file} ({n} nem támogatott) Auto Add/Delete Automatikus hozzáadás/törlés + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. A nyomtatási beállításokban ne jelöld be a tükrözést is: a két tükrözés kioltja egymást. + Print Settings… Nyomtatási beállítások… + Remember to turn off the printer's own colour management in Print Settings… Ne felejtsd el kikapcsolni a nyomtató saját színkezelését a nyomtatási beállításokban… + Sent to {printer} Elküldve ide: {printer} + set in Print Settings… a nyomtatási beállításokban adható meg… diff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsv index db948970fe..10876b8572 100644 --- a/crates/ui-egui/src/i18n/id.tsv +++ b/crates/ui-egui/src/i18n/id.tsv @@ -3083,3 +3083,10 @@ curves Smooth Haluskan Step {step} is waiting for its dialog Langkah {step} menunggu dialognya @plural {n} action|{n} actions {n} tindakan Auto Add/Delete Tambah/Hapus Otomatis + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Jangan aktifkan juga Gambar Cermin di Pengaturan Cetak: kedua pembalikan saling meniadakan. + Print Settings… Pengaturan Cetak… + Remember to turn off the printer's own colour management in Print Settings… Ingat untuk menonaktifkan pengelolaan warna milik printer di Pengaturan Cetak… + Sent to {printer} Dikirim ke {printer} + set in Print Settings… diatur di Pengaturan Cetak… diff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsv index cf4c8962cf..3de380dcde 100644 --- a/crates/ui-egui/src/i18n/it.tsv +++ b/crates/ui-egui/src/i18n/it.tsv @@ -4059,3 +4059,10 @@ navigator Zoom Zoom Step {step} is waiting for its dialog Il passaggio {step} attende la sua finestra di dialogo @plural {n} action|{n} actions {n} azione|{n} azioni Auto Add/Delete Aggiungi/elimina automaticamente + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Non attivare anche Immagine speculare nelle impostazioni di stampa: le due inversioni si annullano. + Print Settings… Impostazioni di stampa… + Remember to turn off the printer's own colour management in Print Settings… Ricorda di disattivare la gestione del colore della stampante nelle impostazioni di stampa… + Sent to {printer} Inviato a {printer} + set in Print Settings… impostato nelle impostazioni di stampa… diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv index 19c36d54ab..7620e9ad57 100644 --- a/crates/ui-egui/src/i18n/ja.tsv +++ b/crates/ui-egui/src/i18n/ja.tsv @@ -3705,3 +3705,10 @@ curves Smooth スムーズ Step {step} is waiting for its dialog ステップ {step} はダイアログを待っています @plural {n} action|{n} actions アクション {n} 個 Auto Add/Delete 自動追加・削除 + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. 印刷設定の「鏡像」も有効にしないでください。2回反転すると元に戻ります。 + Print Settings… 印刷設定… + Remember to turn off the printer's own colour management in Print Settings… 印刷設定でプリンター独自のカラーマネジメントを無効にしてください… + Sent to {printer} {printer} に送信しました + set in Print Settings… 印刷設定で指定… diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv index 7857b418b7..fa32cf82d0 100644 --- a/crates/ui-egui/src/i18n/ko.tsv +++ b/crates/ui-egui/src/i18n/ko.tsv @@ -3265,3 +3265,10 @@ curves Smooth 매끄럽게 Step {step} is waiting for its dialog {step}단계가 대화 상자를 기다리고 있습니다 @plural {n} action|{n} actions 액션 {n}개 Auto Add/Delete 자동 추가/삭제 + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. 인쇄 설정에서도 이미지 반전을 켜지 마세요. 두 번 반전하면 원래대로 돌아갑니다. + Print Settings… 인쇄 설정… + Remember to turn off the printer's own colour management in Print Settings… 인쇄 설정에서 프린터 자체의 색상 관리를 끄세요… + Sent to {printer} {printer}(으)로 전송됨 + set in Print Settings… 인쇄 설정에서 지정… diff --git a/crates/ui-egui/src/i18n/nl.tsv b/crates/ui-egui/src/i18n/nl.tsv index a7f7c2ae13..9412f08f4a 100644 --- a/crates/ui-egui/src/i18n/nl.tsv +++ b/crates/ui-egui/src/i18n/nl.tsv @@ -3855,3 +3855,10 @@ curves Smooth Vloeiend maken Step {step} is waiting for its dialog Stap {step} wacht op zijn dialoogvenster @plural {n} action|{n} actions {n} handeling|{n} handelingen Auto Add/Delete Automatisch toevoegen/verwijderen + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Schakel niet ook Spiegelbeeld in bij de afdrukinstellingen: de twee spiegelingen heffen elkaar op. + Print Settings… Afdrukinstellingen… + Remember to turn off the printer's own colour management in Print Settings… Vergeet niet het eigen kleurbeheer van de printer uit te schakelen in de afdrukinstellingen… + Sent to {printer} Verzonden naar {printer} + set in Print Settings… instellen in de afdrukinstellingen… diff --git a/crates/ui-egui/src/i18n/pl.tsv b/crates/ui-egui/src/i18n/pl.tsv index 0c82ac3b9b..24749182ea 100644 --- a/crates/ui-egui/src/i18n/pl.tsv +++ b/crates/ui-egui/src/i18n/pl.tsv @@ -3692,3 +3692,10 @@ curves Smooth Wygładź Step {step} is waiting for its dialog Krok {step} czeka na swoje okno dialogowe @plural {n} action|{n} actions {n} operacja|{n} operacje|{n} operacji Auto Add/Delete Automatyczne dodawanie/usuwanie + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Nie włączaj też odbicia lustrzanego w ustawieniach druku: oba odbicia wzajemnie się znoszą. + Print Settings… Ustawienia druku… + Remember to turn off the printer's own colour management in Print Settings… Pamiętaj o wyłączeniu zarządzania kolorami drukarki w ustawieniach druku… + Sent to {printer} Wysłano do {printer} + set in Print Settings… ustawiane w ustawieniach druku… diff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsv index 199fa97345..9fe2601cf6 100644 --- a/crates/ui-egui/src/i18n/pt-br.tsv +++ b/crates/ui-egui/src/i18n/pt-br.tsv @@ -3885,3 +3885,10 @@ layerLabel No Color Sem cor Step {step} is waiting for its dialog A etapa {step} está aguardando sua caixa de diálogo @plural {n} action|{n} actions {n} ação|{n} ações Auto Add/Delete Adicionar/excluir automaticamente + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Não ative também a imagem espelhada nas configurações de impressão: as duas inversões se anulam. + Print Settings… Configurações de impressão… + Remember to turn off the printer's own colour management in Print Settings… Lembre-se de desativar o gerenciamento de cores da própria impressora nas configurações de impressão… + Sent to {printer} Enviado para {printer} + set in Print Settings… definido nas configurações de impressão… diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv index dd7001bc1f..00ce3f12d0 100644 --- a/crates/ui-egui/src/i18n/ru.tsv +++ b/crates/ui-egui/src/i18n/ru.tsv @@ -3040,3 +3040,10 @@ curves Smooth Сгладить Step {step} is waiting for its dialog Шаг {step} ожидает своего диалога @plural {n} action|{n} actions {n} операция|{n} операции|{n} операций Auto Add/Delete Автодобавление/удаление + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Не включайте также зеркальное отражение в параметрах печати: два отражения отменяют друг друга. + Print Settings… Параметры печати… + Remember to turn off the printer's own colour management in Print Settings… Не забудьте отключить управление цветом самого принтера в параметрах печати… + Sent to {printer} Отправлено на {printer} + set in Print Settings… задаётся в параметрах печати… diff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsv index 9d8aebef63..76a8a421a0 100644 --- a/crates/ui-egui/src/i18n/sr.tsv +++ b/crates/ui-egui/src/i18n/sr.tsv @@ -4167,3 +4167,14 @@ shape Appearance Izgled Import .GRD Uvezi .GRD Choose a gradient file (.grd). Izaberite datoteku gradijenta (.grd). Auto Add/Delete Automatsko dodavanje/brisanje + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Nemojte uključiti i ogledalsku sliku u podešavanjima štampanja: dva preslikavanja se poništavaju. + Print Settings… Podešavanja štampanja… + Remember to turn off the printer's own colour management in Print Settings… Ne zaboravite da isključite upravljanje bojama samog štampača u podešavanjima štampanja… + Sent to {printer} Poslato na {printer} + set in Print Settings… podešava se u podešavanjima štampanja… + Soft dark Blaga tamna + Soft light Blaga svetla + Soft Dark Theme Blaga tamna tema + Soft Light Theme Blaga svetla tema diff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsv index 605ffd26f9..fb7ce8f663 100644 --- a/crates/ui-egui/src/i18n/tr.tsv +++ b/crates/ui-egui/src/i18n/tr.tsv @@ -3828,3 +3828,10 @@ cameraData Lens Lens Loaded Camera Raw settings from {file} Camera Raw ayarları {file} dosyasından yüklendi Loaded Camera Raw settings from {file} ({n} not supported) Camera Raw ayarları {file} dosyasından yüklendi ({n} desteklenmiyor) Auto Add/Delete Otomatik Ekle/Sil + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Yazdırma Ayarlarında ayrıca Ayna Görüntüsünü açmayın: iki yansıtma birbirini iptal eder. + Print Settings… Yazdırma Ayarları… + Remember to turn off the printer's own colour management in Print Settings… Yazdırma Ayarlarında yazıcının kendi renk yönetimini kapatmayı unutmayın… + Sent to {printer} {printer} yazıcısına gönderildi + set in Print Settings… Yazdırma Ayarlarında belirlenir… diff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsv index 712766a793..85418d216f 100644 --- a/crates/ui-egui/src/i18n/uk.tsv +++ b/crates/ui-egui/src/i18n/uk.tsv @@ -3793,3 +3793,10 @@ curves Smooth Згладити Step {step} is waiting for its dialog Крок {step} очікує на свій діалог @plural {n} action|{n} actions {n} операція|{n} операції|{n} операцій Auto Add/Delete Автододавання/видалення + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Не вмикайте також дзеркальне відображення в параметрах друку: два відображення скасовують одне одного. + Print Settings… Параметри друку… + Remember to turn off the printer's own colour management in Print Settings… Не забудьте вимкнути керування кольором самого принтера в параметрах друку… + Sent to {printer} Надіслано на {printer} + set in Print Settings… задається в параметрах друку… diff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsv index 7ef6f16dd6..eb66747a1a 100644 --- a/crates/ui-egui/src/i18n/vi.tsv +++ b/crates/ui-egui/src/i18n/vi.tsv @@ -3580,3 +3580,10 @@ curves Smooth Làm mượt Import .GRD Nhập .GRD Choose a gradient file (.grd). Chọn tệp chuyển sắc (.grd). Auto Add/Delete Tự động thêm/xóa + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. Đừng bật thêm Ảnh phản chiếu trong thiết lập in: hai lần lật sẽ triệt tiêu nhau. + Print Settings… Thiết lập in… + Remember to turn off the printer's own colour management in Print Settings… Nhớ tắt quản lý màu riêng của máy in trong thiết lập in… + Sent to {printer} Đã gửi tới {printer} + set in Print Settings… đặt trong thiết lập in… diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv index 142718f393..e46fd61904 100644 --- a/crates/ui-egui/src/i18n/zh-hans.tsv +++ b/crates/ui-egui/src/i18n/zh-hans.tsv @@ -2700,3 +2700,10 @@ curves Smooth 平滑 Step {step} is waiting for its dialog 步骤 {step} 正在等待其对话框 @plural {n} action|{n} actions {n} 个动作 Auto Add/Delete 自动添加/删除 + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. 不要同时勾选打印设置中的镜像:两次翻转会相互抵消。 + Print Settings… 打印设置… + Remember to turn off the printer's own colour management in Print Settings… 请记得在打印设置中关闭打印机自身的色彩管理… + Sent to {printer} 已发送到 {printer} + set in Print Settings… 在打印设置中设置… diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv index 8e4da035be..e6e8b921e5 100644 --- a/crates/ui-egui/src/i18n/zh-hant.tsv +++ b/crates/ui-egui/src/i18n/zh-hant.tsv @@ -2685,3 +2685,10 @@ curves Smooth 平滑 Step {step} is waiting for its dialog 步驟 {step} 正在等待其對話框 @plural {n} action|{n} actions {n} 個動作 Auto Add/Delete 自動增加/刪除 + +# Native Windows printing qualification + Don't also tick Mirror Image in Print Settings…: the two flips cancel out. 請勿同時勾選列印設定中的鏡像:兩次翻轉會互相抵消。 + Print Settings… 列印設定… + Remember to turn off the printer's own colour management in Print Settings… 請記得在列印設定中關閉印表機本身的色彩管理… + Sent to {printer} 已傳送至 {printer} + set in Print Settings… 在列印設定中設定… From 3a2d657b9aa05292b9b5fc9ef2e959526070ace9 Mon Sep 17 00:00:00 2001 From: skylar taylor-barrick Date: Tue, 13 Oct 2026 20:43:45 +0200 Subject: [PATCH 15/39] Keep custom paper for Windows PDF exports and repair merged catalogs --- crates/ui-egui/src/file_ui.rs | 19 ++++++++++++++++--- crates/ui-egui/src/i18n/ja.tsv | 7 ------- crates/ui-egui/src/i18n/sr.tsv | 9 +++++---- crates/ui-egui/tests/custom_print_paper.rs | 2 ++ 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index d86f9cf12d..ba7efccaf6 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -726,7 +726,6 @@ pub fn open_print(app: &mut PhotocraftApp) -> u64 { f.insert("printer".into(), json!(fallback)); f.remove("printerSettings"); } - driver_page(&mut f); } app.ui.open_dialog(DialogKind::Command, f) } @@ -828,6 +827,22 @@ fn print_paper_controls(ui: &mut egui::Ui, f: &mut Map) { fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map) { let t = Tokens::get(ui.ctx()); let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return }; + // PDF exports and previews retain editable paper on Windows too. Only a real native + // submission takes its media from the printer; entering that mode refreshes the driver + // geometry after any export-only paper edits. + let printers: Option> = if !b(f, "dryRun", false) && s(f, "output", "").is_empty() { + f.get("__printers").and_then(Value::as_array).map(|a| a.iter().filter_map(|v| v.as_str().map(str::to_string)).collect()) + } else { + None + }; + if printers.is_some() { + if !b(f, "__driverPageInitialized", false) { + driver_page(f); + f.insert("__driverPageInitialized".into(), json!(true)); + } + } else { + f.remove("__driverPageInitialized"); + } let p = params(f); ui.horizontal_top(|ui| { // Page preview. @@ -904,8 +919,6 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map> = - f.get("__printers").and_then(Value::as_array).map(|a| a.iter().filter_map(|v| v.as_str().map(str::to_string)).collect()); if let Some(printers) = &printers { // Windows: Photoshop's Printer menu and Print Settings… (the driver's own dialog). ui.horizontal(|ui| { diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv index d382744c51..905c62874e 100644 --- a/crates/ui-egui/src/i18n/ja.tsv +++ b/crates/ui-egui/src/i18n/ja.tsv @@ -3796,13 +3796,6 @@ curves Smooth スムーズ Sent to {printer} {printer} に送信しました set in Print Settings… 印刷設定で指定… # Restored rows (custom print paper #3820, brush presets, Vanishing Point help, brush panel menu) - Paper Width 用紙の幅 - Paper Height 用紙の高さ - Capture Brush Size in Preset ブラシサイズをプリセットに保存 - Include Tool Settings ツール設定を含める - Include Color カラーを含める - Enter a name for the brush preset ブラシプリセットの名前を入力してください - A brush preset with this name already exists この名前のブラシプリセットは既に存在します Drag to pan · Scroll to zoom · Double-click to fit ドラッグで移動 · スクロールでズーム · ダブルクリックで全体表示 Clear Brush Controls ブラシのコントロールを消去 Reset All Locked Settings ロックされた設定をすべて初期化 diff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsv index 3d01325ef8..66fd394b20 100644 --- a/crates/ui-egui/src/i18n/sr.tsv +++ b/crates/ui-egui/src/i18n/sr.tsv @@ -4192,10 +4192,11 @@ shape Appearance Izgled Remember to turn off the printer's own colour management in Print Settings… Ne zaboravite da isključite upravljanje bojama samog štampača u podešavanjima štampanja… Sent to {printer} Poslato na {printer} set in Print Settings… podešava se u podešavanjima štampanja… - Soft dark Blaga tamna - Soft light Blaga svetla - Soft Dark Theme Blaga tamna tema - Soft Light Theme Blaga svetla tema # Custom print paper (#3820) Paper Width Širina papira Paper Height Visina papira + +# Layer mask selection commands + Add Mask to Selection Dodaj masku izboru + Subtract Mask from Selection Oduzmi masku od izbora + Intersect Mask with Selection Presek maske i izbora diff --git a/crates/ui-egui/tests/custom_print_paper.rs b/crates/ui-egui/tests/custom_print_paper.rs index 7361468bbc..d0d81dea69 100644 --- a/crates/ui-egui/tests/custom_print_paper.rs +++ b/crates/ui-egui/tests/custom_print_paper.rs @@ -10,6 +10,8 @@ fn app() -> (PhotocraftApp, u64) { let id = open_print(&mut app); // Every UI path in these fixtures is safe even if an unexpected Enter confirms the dialog. app.ui.dialog_mut(id).unwrap().fields.insert("dryRun".into(), json!(true)); + // Exercise PDF paper controls even when native printers are available, on every OS. + app.ui.dialog_mut(id).unwrap().fields.insert("__printers".into(), json!(["Test native printer"])); (app, id) } From 6c1a3590f5116ad70de1a60b2cb4cdbf290969ed Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Tue, 13 Oct 2026 21:33:44 +0200 Subject: [PATCH 16/39] Remove obsolete app-owned Windows printing layer comment --- xtask/src/layers.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/xtask/src/layers.rs b/xtask/src/layers.rs index e78613f37a..2128345148 100644 --- a/xtask/src/layers.rs +++ b/xtask/src/layers.rs @@ -46,7 +46,6 @@ pub const TABLE: &[(&str, Class)] = &[ ("adobe-assets", Class::Standalone), // Pen tablet input (the one isolated `unsafe` helper: AppKit interop on macOS). ("tablet", Class::Standalone), - // Windows printing (an isolated `unsafe` helper: winspool and GDI). ("doc", Class::Layer(1)), ("ops", Class::Layer(2)), ("paint", Class::Layer(2)), From a9ac922fc12a07cf5bf228644130693db45bcbd1 Mon Sep 17 00:00:00 2001 From: skylartaylor Date: Wed, 14 Oct 2026 02:21:09 +0200 Subject: [PATCH 17/39] Align printer menu regression with the merged system-printer cache --- crates/ui-egui/src/file_ui.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index 9809f33c96..c3b8abc4d7 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -1445,7 +1445,8 @@ mod tests { let (mut app, ctx) = app(); let id = crate::menus::invoke(&mut app, &ctx, "file.print", json!({})).unwrap()["dialog"].as_u64().unwrap(); let f = &app.ui.dialog_mut(id).unwrap().fields; - assert!(f.get("__printers").is_some_and(Value::is_array)); + assert!(f.get("__systemPrinters").is_some_and(Value::is_array)); + assert!(params(f).get("__systemPrinters").is_none()); assert!(params(f).get("__printers").is_none()); } From 583425b4df1c57a4515f591fed64fcedf8440cba Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Tue, 13 Oct 2026 18:09:37 -0700 Subject: [PATCH 18/39] Stroke latency trace: PHOTOCRAFT_STROKE_TRACE times each frame of a brush stroke (#1343) (#4403) Testers report ~50 ms input-to-pixel while painting on Linux and macOS, and asked for a per-stage breakdown. With PHOTOCRAFT_STROKE_TRACE=1 the app prints, per stroke, p50/p95 of the frame interval, pointer moves per frame, the live stroke's render, the canvas refresh (composite + upload), the UI build, eframe's CPU time (UI, tessellation, submission) and the remaining vsync/swap-chain wait, with the GPU backend; = also appends every frame. The last summary is in the control channel's perf.strokeTrace. RTX 5090, Vulkan, 120 Hz: interval 8.33, cpu 2.2 (ui 0.9, stroke 0.8, canvas 0.4), wait 6.0 ms. --- crates/ui-egui/src/canvas.rs | 4 + crates/ui-egui/src/control.rs | 2 +- crates/ui-egui/src/gpu_canvas.rs | 4 + crates/ui-egui/src/lib.rs | 20 +- crates/ui-egui/src/stroke_timing_tests.rs | 42 +++ crates/ui-egui/src/stroke_trace.rs | 297 ++++++++++++++++++++++ docs/development.md | 21 ++ 7 files changed, 388 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/stroke_trace.rs diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index db0efe31fa..676d068070 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -697,10 +697,12 @@ fn feed_live_stroke(app: &mut PhotocraftApp) { return; } l.fed = d.points.len(); + let started = crate::gpu_canvas::now_ms(); match l.stroke.push(&pts) { Ok(r) => l.damage.push(r), Err(_) => app.live_stroke = None, } + app.stroke_trace.stroke(crate::gpu_canvas::now_ms() - started); } /// The points the live retouching stroke has rendered. @@ -5460,7 +5462,9 @@ fn commit_brush_stroke(app: &mut PhotocraftApp, d: &Drag) { { app.session.clear_live_stroke(); } + let started = crate::gpu_canvas::now_ms(); let result = app.run(stroke_command(app, d.tool), p); + app.stroke_trace.stroke(crate::gpu_canvas::now_ms() - started); app.session.clear_live_stroke(); let Ok(r) = result else { return }; let revision = app.session.documents().iter().find(|st| st.doc.id == doc).map(|st| st.revision); diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index db908d0943..209691b875 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -1315,7 +1315,7 @@ pub fn inspect(app: &PhotocraftApp, ctx: &egui::Context) -> Value { "frame": app.frame, "session": photocraft_engine::inspect::session(&app.session), "document": app.session.active().map(photocraft_engine::inspect::document), - "perf": {"fps": app.fps, "timings": app.perf}, + "perf": {"fps": app.fps, "timings": app.perf, "strokeTrace": app.stroke_trace.last}, "brush": {"size": app.session.tools.brush.size, "hardness": app.session.tools.brush.hardness, "opacity": app.session.tools.brush.opacity}, "distort": app.distort.describe(), "jobs": crate::jobs_ui::inspect(app), diff --git a/crates/ui-egui/src/gpu_canvas.rs b/crates/ui-egui/src/gpu_canvas.rs index 046d4ac2a7..3862aec534 100644 --- a/crates/ui-egui/src/gpu_canvas.rs +++ b/crates/ui-egui/src/gpu_canvas.rs @@ -898,6 +898,9 @@ pub struct Perf { pub spans: std::collections::BTreeMap<&'static str, f64>, #[serde(skip)] max_decay: f64, + /// Composite and upload time of this frame's canvas refreshes, in ms (the stroke trace). + #[serde(skip)] + pub(crate) frame_refresh_ms: f64, } /// What the app renders with and why (Help › System Info, Preferences › Performance, the `perf` @@ -1012,6 +1015,7 @@ pub fn backend_name(b: wgpu::Backend) -> &'static str { impl Perf { pub fn record(&mut self, kind: &'static str, px: u64, composite_ms: f64, upload_ms: f64) { + self.frame_refresh_ms += composite_ms + upload_ms; self.last_refresh = kind; self.last_refresh_px = px; self.composite_ms = composite_ms; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 2cd006087d..38118955c3 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -190,6 +190,7 @@ pub mod spring_tools; pub mod stack_dialog; pub mod state; pub mod stroke_constraint; +pub mod stroke_trace; pub mod stroke_trail; pub mod stroke_ui; pub mod stylus; @@ -730,6 +731,8 @@ pub struct PhotocraftApp { started: Option, /// Frame and canvas-upload timings (exposed via `ui.inspect`). pub perf: gpu_canvas::Perf, + /// Per-frame stroke timings for latency reports (`PHOTOCRAFT_STROKE_TRACE`, see `stroke_trace`). + pub stroke_trace: stroke_trace::StrokeTrace, /// Preferences, autosave and snapping runtime state (see `prefs_ui`, `snap_ui`). pub(crate) prefs_rt: prefs_ui::Runtime, /// Close, Revert or Exit parked behind the unsaved-changes prompt (see `discard_ui`). @@ -875,6 +878,7 @@ impl PhotocraftApp { gpu: None, started: None, perf: Default::default(), + stroke_trace: stroke_trace::StrokeTrace::from_env(), prefs_rt: Default::default(), discard: None, file_dialog: None, @@ -1756,6 +1760,7 @@ impl eframe::App for PhotocraftApp { } fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { + let frame_start = gpu_canvas::now_ms(); // An undecorated window gets its caption back when the toolkit changes its flags // (maximize, restore, …): take it off again before the frame is laid out (#2246). if let Some(window_frame) = self.services.window_frame.as_mut() { @@ -1779,6 +1784,11 @@ impl eframe::App for PhotocraftApp { if self.custom_titlebar { titlebar::release_after_os_resize(ctx, raw_input); } + if self.stroke_trace.enabled() { + // The OS's moves and the control channel's (injected just above). + let moves = raw_input.events.iter().filter(|e| matches!(e, egui::Event::PointerMoved(_))).count(); + self.stroke_trace.begin_frame(frame_start, moves); + } } fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { @@ -1792,6 +1802,8 @@ impl eframe::App for PhotocraftApp { return; } let t0 = gpu_canvas::now_ms(); + self.perf.frame_refresh_ms = 0.0; + self.stroke_trace.cpu_prev(frame.info().cpu_usage); // A text field's right-click menu, before any field is drawn (text_field_menu.rs). text_field_menu::show(&ctx); // Each painting tool keeps its own brush (#218), so switch the active tool's brush in @@ -1893,7 +1905,13 @@ impl eframe::App for PhotocraftApp { egui::Modal::new(egui::Id::new("screen-color-wait")).frame(egui::Frame::NONE).backdrop_color(egui::Color32::TRANSPARENT).show(&ctx, |_| {}); ctx.set_cursor_icon(egui::CursorIcon::Wait); } - self.perf.frame(gpu_canvas::now_ms() - t0); + let ui_ms = gpu_canvas::now_ms() - t0; + self.perf.frame(ui_ms); + if self.stroke_trace.enabled() { + self.stroke_trace.canvas(self.perf.frame_refresh_ms); + let info = &self.perf.gpu_info; + self.stroke_trace.end_frame(ui_ms, self.live_stroke.is_some(), || format!("{} {}", info.backend, info.adapter).trim().to_string()); + } // Synthetic input is injected one press/release step per frame: keep frames coming until // the queue is empty, then release control replies waiting on it. if !self.synthetic.is_empty() { diff --git a/crates/ui-egui/src/stroke_timing_tests.rs b/crates/ui-egui/src/stroke_timing_tests.rs index fc6d0c7e78..a472a4125c 100644 --- a/crates/ui-egui/src/stroke_timing_tests.rs +++ b/crates/ui-egui/src/stroke_timing_tests.rs @@ -338,6 +338,48 @@ fn a_committed_stroke_refreshes_only_what_each_cache_had_not_shown() { assert_eq!(c.since(99), None, "not a key of this stroke"); } +#[test] +fn the_stroke_trace_times_each_frame_of_a_stroke_and_ends_on_release() { + // The app's hooks around the canvas (`raw_input_hook` starts a frame, `ui` ends it); the + // canvas times the live stroke and the release's commit itself. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100, "background": "white"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Brush; + app.stroke_trace.output = crate::stroke_trace::Output::Stderr; + let mut h = Harness::builder().with_size(vec2(900.0, 600.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + let t0 = crate::gpu_canvas::now_ms(); + let moves = ctx.input(|i| i.raw.events.iter().filter(|e| matches!(e, egui::Event::PointerMoved(_))).count()); + app.stroke_trace.begin_frame(t0, moves); + app.stroke_trace.cpu_prev(Some(0.004)); + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + let stroking = app.live_stroke.is_some(); + app.stroke_trace.end_frame(crate::gpu_canvas::now_ms() - t0, stroking, || "test".into()); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + move_to(&mut h, 20.0, 50.0); + button(&mut h, 20.0, 50.0, true); + for x in [40.0, 60.0, 80.0, 100.0, 120.0] { + move_to(&mut h, x, 50.0); + } + assert!(h.state().stroke_trace.last.is_none(), "the stroke is still being drawn"); + button(&mut h, 120.0, 50.0, false); + h.run_steps(2); + let s = h.state().stroke_trace.last.clone().expect("releasing ends the stroke's trace"); + assert!(s.frames >= 6, "{s:?}"); + assert!(s.stroke[1] > 0.0 && s.ui[0] >= s.stroke[0], "{s:?}"); + assert_eq!(s.moves[1], 1.0, "one move per test frame: {s:?}"); + assert_eq!(s.backend, "test"); +} + /// A Clone Stamp stroke through the canvas renders the points its commit gets (position and /// pressure, no tilt or times), so the commit can take the live pixels; they are what replaying /// the journaled command paints. diff --git a/crates/ui-egui/src/stroke_trace.rs b/crates/ui-egui/src/stroke_trace.rs new file mode 100644 index 0000000000..206cc7e81a --- /dev/null +++ b/crates/ui-egui/src/stroke_trace.rs @@ -0,0 +1,297 @@ +//! Stroke latency trace (#1343): where each frame's time goes while a brush stroke is drawn, so +//! a tester on any platform can report a per-stage breakdown instead of one input-to-pixel number. +//! +//! Set `PHOTOCRAFT_STROKE_TRACE` before launching: `1` prints a summary to stderr when each stroke +//! ends; a file path also appends every frame of every stroke to that CSV. The last summary is in +//! the control channel's `perf` output (`strokeTrace`). +//! +//! Per frame of a stroke it records the interval since the previous frame started, the pointer +//! moves the frame delivered, the live stroke's render time, the canvas refresh (composite and +//! upload), the UI build, and eframe's CPU time for the frame before (UI, tessellation and GPU +//! submission, without the vsync wait). What the app can't see (the OS input queue before a frame +//! starts, frames queued for the display, the compositor) is the rest of the input-to-photon time; +//! the summary names the GPU backend because GL ignores the frame-latency setting. + +use std::fmt::Write as _; + +/// One frame of a stroke. Times in ms. +#[derive(Clone, Debug, Default, PartialEq)] +pub(crate) struct Row { + /// Frame start, from [`crate::gpu_canvas::now_ms`]. + pub t: f64, + /// Since the previous frame started (0 for the first frame the trace saw). + pub interval: f64, + /// Pointer moves the frame delivered. + pub moves: usize, + /// Rendering the stroke's new points (the live stroke, or the commit on release). + pub stroke: f64, + /// Canvas refresh: composite and upload. + pub canvas: f64, + /// Building the UI (`App::ui`), the two above included. + pub ui: f64, + /// eframe's CPU time for the previous frame: UI, tessellation and GPU submission. + pub cpu_prev: Option, +} + +/// Percentiles of a stroke's frames, for the summary line and the control channel. +#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Summary { + pub frames: usize, + /// GPU backend and adapter ("vulkan NVIDIA GeForce …"). + pub backend: String, + /// `[p50, p95]` of each stage, in ms. + pub interval: [f64; 2], + pub moves: [f64; 2], + pub stroke: [f64; 2], + pub canvas: [f64; 2], + pub ui: [f64; 2], + /// eframe's CPU time per frame (UI, tessellation, submission). + pub cpu: [f64; 2], + /// Frame interval minus that CPU time: waiting on vsync / the swap chain, or idle. + pub wait: [f64; 2], +} + +impl Summary { + /// One line for stderr. + pub fn line(&self) -> String { + let f = |name: &str, [p50, p95]: [f64; 2]| format!("{name} {p50:.2}/{p95:.2}"); + [ + format!("stroke trace: {} frames on {} (p50/p95 ms):", self.frames, self.backend), + f("interval", self.interval), + f("moves/frame", self.moves), + f("stroke", self.stroke), + f("canvas", self.canvas), + f("ui", self.ui), + f("cpu", self.cpu), + f("wait", self.wait), + ] + .join(" ") + } +} + +/// Where the trace goes ([`StrokeTrace::from_env`]). +// The web build has no clock, so it never traces. +#[cfg_attr(target_arch = "wasm32", allow(dead_code))] +#[derive(Clone, Debug, Default, PartialEq)] +pub(crate) enum Output { + #[default] + Off, + /// A summary per stroke on stderr. + Stderr, + /// The summary on stderr, and every frame appended to this CSV. + Csv(std::path::PathBuf), +} + +#[derive(Debug, Default)] +pub struct StrokeTrace { + pub(crate) output: Output, + /// The frame being built (started by `begin_frame`). + frame: Option, + last_start: Option, + /// The current stroke's frames. + rows: Vec, + /// Strokes finished so far (numbers the CSV rows). + strokes: usize, + /// The last finished stroke's summary. + pub last: Option, +} + +/// `[p50, p95]` of `v` (nearest rank; zeros when empty). +fn percentiles(mut v: Vec) -> [f64; 2] { + v.retain(|x| x.is_finite()); + if v.is_empty() { + return [0.0, 0.0]; + } + v.sort_by(f64::total_cmp); + let at = |p: f64| v.get(((p * (v.len() - 1) as f64).round() as usize).min(v.len() - 1)).copied().unwrap_or(0.0); + let r = |x: f64| (x * 100.0).round() / 100.0; + [r(at(0.5)), r(at(0.95))] +} + +impl StrokeTrace { + /// Reads `PHOTOCRAFT_STROKE_TRACE` (off on the web, where there is no clock). + pub fn from_env() -> Self { + #[cfg(not(target_arch = "wasm32"))] + if let Some(v) = std::env::var_os("PHOTOCRAFT_STROKE_TRACE").filter(|v| !v.is_empty()) { + let output = if v == "1" || v == "stderr" { Output::Stderr } else { Output::Csv(v.into()) }; + return Self { output, ..Default::default() }; + } + Self::default() + } + + pub fn enabled(&self) -> bool { + self.output != Output::Off + } + + /// A frame starts (the raw input hook) with `moves` pointer moves. + pub fn begin_frame(&mut self, now: f64, moves: usize) { + if !self.enabled() { + return; + } + let interval = self.last_start.map_or(0.0, |t| now - t); + self.last_start = Some(now); + self.frame = Some(Row { t: now, interval, moves, ..Default::default() }); + } + + /// eframe's CPU time for the previous frame, in seconds (`Frame::info().cpu_usage`). + pub fn cpu_prev(&mut self, secs: Option) { + if let Some(f) = self.frame.as_mut() { + f.cpu_prev = secs.map(|s| f64::from(s) * 1000.0); + } + } + + /// Time spent rendering stroke points this frame. + pub fn stroke(&mut self, ms: f64) { + if let Some(f) = self.frame.as_mut() { + f.stroke += ms; + } + } + + /// Time spent refreshing the canvas this frame. + pub fn canvas(&mut self, ms: f64) { + if let Some(f) = self.frame.as_mut() { + f.canvas += ms; + } + } + + /// The frame's UI is built (`ui` ms). `stroking`: a stroke is being drawn (a frame that + /// rendered stroke points counts too, such as the release's commit). A stroke's frames are + /// kept until the first frame without one, which ends it. + pub fn end_frame(&mut self, ui: f64, stroking: bool, backend: impl FnOnce() -> String) { + let Some(mut f) = self.frame.take() else { return }; + if stroking || f.stroke > 0.0 { + f.ui = ui; + // The first frame of a stroke doesn't follow a stroke frame: its interval is idle time. + if self.rows.is_empty() { + f.interval = 0.0; + } + self.rows.push(f); + } else if !self.rows.is_empty() { + self.finish(backend()); + } + } + + fn finish(&mut self, backend: String) { + let rows = std::mem::take(&mut self.rows); + self.strokes += 1; + let col = |g: fn(&Row) -> Option| percentiles(rows.iter().filter_map(g).collect()); + let summary = Summary { + frames: rows.len(), + backend, + interval: col(|r| (r.interval > 0.0).then_some(r.interval)), + moves: col(|r| Some(r.moves as f64)), + stroke: col(|r| Some(r.stroke)), + canvas: col(|r| Some(r.canvas)), + ui: col(|r| Some(r.ui)), + // A frame's cpu_prev belongs to the frame before it, so pair it with that interval. + cpu: percentiles(rows.iter().skip(1).filter_map(|r| r.cpu_prev).collect()), + wait: percentiles(rows.windows(2).filter_map(|w| Some((w[1].interval - w[1].cpu_prev?).max(0.0))).collect()), + }; + if self.enabled() { + eprintln!("{}", summary.line()); + } + #[cfg(not(target_arch = "wasm32"))] + if let Output::Csv(path) = &self.output + && let Err(e) = append_csv(path, self.strokes, &rows) + { + eprintln!("stroke trace: can't write {}: {e}", path.display()); + } + self.last = Some(summary); + } +} + +/// The CSV text for `rows` of stroke `stroke` (with the header when `header`). +// The web build has no clock, so it never traces. +#[cfg_attr(target_arch = "wasm32", allow(dead_code))] +pub(crate) fn csv(stroke: usize, rows: &[Row], header: bool) -> String { + let mut s = String::new(); + if header { + s.push_str("stroke,t_ms,interval_ms,moves,stroke_ms,canvas_ms,ui_ms,cpu_prev_ms\n"); + } + for r in rows { + let cpu = r.cpu_prev.map_or(String::new(), |c| format!("{c:.3}")); + let _ = writeln!(s, "{stroke},{:.3},{:.3},{},{:.3},{:.3},{:.3},{cpu}", r.t, r.interval, r.moves, r.stroke, r.canvas, r.ui); + } + s +} + +#[cfg(not(target_arch = "wasm32"))] +fn append_csv(path: &std::path::Path, stroke: usize, rows: &[Row]) -> std::io::Result<()> { + use std::io::Write as _; + let header = std::fs::metadata(path).map_or(true, |m| m.len() == 0); + let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?; + f.write_all(csv(stroke, rows, header).as_bytes()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn on() -> StrokeTrace { + StrokeTrace { output: Output::Stderr, ..Default::default() } + } + + fn frame(t: &mut StrokeTrace, now: f64, cpu: f32, stroking: bool) { + t.begin_frame(now, 3); + t.cpu_prev(Some(cpu / 1000.0)); + if stroking { + t.stroke(1.0); + t.canvas(2.0); + } + t.end_frame(4.0, stroking, || "vulkan Test GPU".into()); + } + + #[test] + fn a_stroke_is_its_frames_from_the_first_stroke_frame_to_the_first_without() { + let mut t = on(); + frame(&mut t, 0.0, 5.0, false); + frame(&mut t, 100.0, 5.0, true); + for k in 1..=10 { + frame(&mut t, 100.0 + 16.0 * f64::from(k), 6.0, true); + } + assert!(t.last.is_none(), "still drawing"); + frame(&mut t, 276.0, 6.0, false); + let s = t.last.clone().expect("the stroke ended"); + assert_eq!(s.frames, 11); + assert_eq!(s.interval, [16.0, 16.0], "the idle gap before the stroke isn't a frame interval"); + assert_eq!((s.stroke, s.canvas, s.ui, s.moves), ([1.0, 1.0], [2.0, 2.0], [4.0, 4.0], [3.0, 3.0])); + assert_eq!((s.cpu, s.wait), ([6.0, 6.0], [10.0, 10.0])); + assert!(s.line().contains("11 frames on vulkan Test GPU") && s.line().contains("wait 10.00/10.00"), "{}", s.line()); + // Off: nothing is kept. + let mut off = StrokeTrace::default(); + frame(&mut off, 0.0, 1.0, true); + frame(&mut off, 16.0, 1.0, false); + assert!(off.last.is_none() && off.rows.is_empty()); + } + + #[test] + fn csv_rows_and_odd_numbers() { + let rows = [Row { t: 1.0, interval: 16.5, moves: 2, stroke: 0.25, canvas: 1.0, ui: 3.0, cpu_prev: None }]; + assert_eq!(csv(1, &rows, true), "stroke,t_ms,interval_ms,moves,stroke_ms,canvas_ms,ui_ms,cpu_prev_ms\n1,1.000,16.500,2,0.250,1.000,3.000,\n"); + assert_eq!(percentiles(vec![]), [0.0, 0.0]); + assert_eq!(percentiles(vec![f64::NAN, 2.0, f64::INFINITY]), [2.0, 2.0]); + assert_eq!(percentiles((1..=100).map(f64::from).collect()), [51.0, 95.0]); + } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn the_csv_gets_one_header_and_every_strokes_frames() { + let dir = std::env::temp_dir().join(format!("photocraft-stroke-trace-{}", std::process::id())); + let _ = std::fs::create_dir_all(&dir); + let path = dir.join("trace.csv"); + let _ = std::fs::remove_file(&path); + let mut t = StrokeTrace { output: Output::Csv(path.clone()), ..Default::default() }; + for start in [0.0, 1000.0] { + for k in 0..3 { + frame(&mut t, start + 16.0 * f64::from(k), 5.0, true); + } + frame(&mut t, start + 48.0, 5.0, false); + } + let text = std::fs::read_to_string(&path).unwrap(); + let _ = std::fs::remove_dir_all(&dir); + let lines: Vec<&str> = text.lines().collect(); + assert_eq!(lines.len(), 7, "{text}"); + assert!(lines[0].starts_with("stroke,") && lines[1].starts_with("1,") && lines[6].starts_with("2,")); + } +} diff --git a/docs/development.md b/docs/development.md index 17c7e67dce..f13690102e 100644 --- a/docs/development.md +++ b/docs/development.md @@ -119,6 +119,7 @@ By default PhotoCraft's own crates log at `info` and everything else at `warn`. | `PHOTOCRAFT_CPU_COMPOSE=1` | Keep the wgpu canvas but composite on the CPU (compare GPU vs CPU renders, e.g. with the snapshot example) | | `PHOTOCRAFT_LOCALE` | Override Auto's native UI language for this launch; unsupported tags use English. See [system language detection](localization.md#first-launch-and-system-language). | | `PHOTOCRAFT_FX_TRACE=1` | Print the CPU time spent on GPU effect shapes and distance fields per rebuild | +| `PHOTOCRAFT_STROKE_TRACE=1` (or `=`) | Time every frame of each brush stroke; print a summary per stroke, and with a path also append each frame to that CSV (see [Stroke latency trace](#stroke-latency-trace)) | | `PHOTOCRAFT_THEME_FILE=tokens.json` | **Debug builds only:** live design-token overrides, re-read on change | ### Live design tokens @@ -129,6 +130,26 @@ By default PhotoCraft's own crates log at `info` and everything else at `warn`. Keys are the field names of `theme::Tokens` (`crates/ui-egui/src/theme.rs`). Edit and save the file while the app runs to see changes immediately, with no recompile. This is compiled out of release builds. +### Stroke latency trace + +To find where a stroke's input-to-pixel time goes on a given machine (#1343), launch with `PHOTOCRAFT_STROKE_TRACE=1` (a summary per stroke on standard error) or `PHOTOCRAFT_STROKE_TRACE=strokes.csv` (also every frame, appended to that file). Paint a few strokes with the mouse or pen. Each summary gives p50/p95 in ms: + +```text +stroke trace: 62 frames on vulkan NVIDIA GeForce RTX 5090 (p50/p95 ms): interval 8.33/8.53 moves/frame 1.00/2.00 stroke 0.80/1.53 canvas 0.40/0.75 ui 0.91/1.41 cpu 2.20/2.84 wait 6.04/6.64 +``` + +| Column | What it is | +|---|---| +| `interval` | Time between frame starts while drawing (the display's refresh period when the app keeps up) | +| `moves/frame` | Pointer moves the OS delivered per frame (control-channel `ui.pointer` input bypasses raw input and counts 0) | +| `stroke` | Rendering the stroke's new points (the live stroke; on the last frame, the commit) | +| `canvas` | Canvas refresh: composite and upload | +| `ui` | Building the UI, the two above included | +| `cpu` | eframe's CPU time per frame: UI, tessellation and GPU submission, without the vsync wait | +| `wait` | `interval − cpu`: waiting on vsync or the swap chain, or idle | + +What the app can't time is the rest of input-to-photon: the OS input queue before a frame starts (up to one `interval`), frames queued for the display (the surface keeps up to two; the GL backend ignores that setting), and the compositor. So the first line names the backend. The last summary is also in the control channel's `perf.strokeTrace` (`ui.inspect`). The code is `crates/ui-egui/src/stroke_trace.rs`. + ## Driving the app programmatically Start the app with a private token file. It then accepts authenticated JSON lines on `127.0.0.1:7878`: From 9df59c96b820e08ff63ecb94f2eecfc6c87d17a9 Mon Sep 17 00:00:00 2001 From: wwew <145259726+wwewtech@users.noreply.github.com> Date: Wed, 14 Oct 2026 09:58:14 +0400 Subject: [PATCH 19/39] docs: write product name as PhotoCraft in development guide (#5228) --- docs/development.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/development.md b/docs/development.md index f13690102e..3c539f70bc 100644 --- a/docs/development.md +++ b/docs/development.md @@ -264,7 +264,7 @@ A typical agent loop (place inputs under the configured read root and outputs un `crates/cms` is our own pure-Rust ICC engine (v2/v4 parsing, matrix/TRC and LUT profiles, all four intents, black point compensation). It ships CC0 built-in profiles, including a synthetic -"Photocraft Coated CMYK", because Adobe's CMYK profiles are proprietary (see `crates/cms/README.md`). +"PhotoCraft Coated CMYK", because Adobe's CMYK profiles are proprietary (see `crates/cms/README.md`). - Documents carry an optional embedded ICC profile (`Document::icc_profile`); `edit.assignProfile` and `edit.convertToProfile` change it. Mode changes (`image.mode.*`) convert through cms. From d11be6de78735205917a2efa5024a2e96052942d Mon Sep 17 00:00:00 2001 From: LorenzoFasolino Date: Wed, 14 Oct 2026 07:58:18 +0200 Subject: [PATCH 20/39] docs: drop stale missing-tool rows, only Art History Brush is missing (#5063) Task T-0002 del lab artcraft-lab. File toccati: docs/brush-parity.md | 5 +++-- docs/gaps.md | 6 +++--- docs/target-app-parity.md | 5 +++-- docs/ui-parity.md | 7 ++++--- 4 files changed, 13 insertions(+), 10 deletions(-) Evidenze: artifacts/T-0002/ (nel lab, non in questo repo). --- docs/brush-parity.md | 5 +++-- docs/gaps.md | 6 +++--- docs/target-app-parity.md | 5 +++-- docs/ui-parity.md | 7 ++++--- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/docs/brush-parity.md b/docs/brush-parity.md index 7a5e499820..96280dc856 100644 --- a/docs/brush-parity.md +++ b/docs/brush-parity.md @@ -1,6 +1,6 @@ # Brush and painting parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (Hue Jitter amount track; Brush Settings minimum fields follow their Control; checkbox-only rows and non-destructive Smoothing switch; shaped-tip cursor follows the view) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (Hue Jitter amount track; Brush Settings minimum fields follow their Control; checkbox-only rows and non-destructive Smoothing switch; shaped-tip cursor follows the view; Color Replacement is a live toolbar tool; no brush-feel estimate changed) · **Target:** Adobe Photoshop 2026 Painting is where Photoshop users notice differences first, so it gets its own checklist: the brush engine (`crates/paint/src/brush.rs`), the Brush Settings UI @@ -99,7 +99,7 @@ tablets against Photoshop side by side. | Blur, Sharpen, Smudge (premultiplied, Sample All Layers, spacing) | done | BUG-207-2/3, #2171 | | Dodge, Burn, Sponge | done | | | Background Eraser, Magic Eraser | done | | -| Color Replacement | missing as a tool | engine command exists | +| Color Replacement | done | | | Quick Selection (brush-driven) | done | | ## Presets @@ -115,6 +115,7 @@ tablets against Photoshop side by side. | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | Stale "missing tool" row corrected against main: Color Replacement (`paint.colorReplacement`) is a live toolbar tool; no brush-feel estimate changed | | 2026-10-10 | major | First version, from a source audit and the open issues | | 2026-10-11 | minor | `~` held with the Brush or Pencil erases with that tool's brush, and Clear joins their Mode menus (#3439) | | 2026-10-12 | minor | Five Brush Settings switch rows toggle without selecting a properties pane; Smoothing has a default-on switch and retains its settings while disabled (#4283). No brush-feel estimate changed. | diff --git a/docs/gaps.md b/docs/gaps.md index 25a286aab8..c952bac81b 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where PhotoCraft falls short of Photoshop -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (G7: Field Blur's on-canvas pins, #3373; G4: Hue Jitter amount track, #3825 partial; G4: minimum field interaction, #3825 partial; G1: CMYK Invert, Posterize and Threshold on the inks, 16-bit Lab Invert; Photoshop-oracle floor 145/258; G11: `.atn` import maps 112 events: Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, nudges in points, Bitmap mode; G5: Free Transform H/V skew fields; G9: toolbar tool shortcut editing; refreshed against main: G2, G5, G7, G9, G10, G13, G14, G17 items that landed; G10: DPX read and write, #1049; G4: checkbox-only Brush Settings rows; G11: Load/Save Actions `.atn` landed, AUTO-219-2 partial; G5: hold-a-key spring-loaded tools landed, #2672; G1: #2465 fixed) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (G7: Field Blur's on-canvas pins, #3373; G4: Hue Jitter amount track, #3825 partial; G4: minimum field interaction, #3825 partial; G1: CMYK Invert, Posterize and Threshold on the inks, 16-bit Lab Invert; Photoshop-oracle floor 145/258; G11: `.atn` import maps 112 events: Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, nudges in points, Bitmap mode; G5: Free Transform H/V skew fields; G9: toolbar tool shortcut editing; refreshed against main: G2, G5, G7, G9, G10, G13, G14, G17 items that landed; G10: DPX read and write, #1049; G4: checkbox-only Brush Settings rows; G11: Load/Save Actions `.atn` landed, AUTO-219-2 partial; G5: hold-a-key spring-loaded tools landed, #2672; G1: #2465 fixed; stale missing-tool rows corrected: Color Replacement, Red Eye and Add/Delete Anchor Point are live tools, so only Art History Brush is missing; no estimate changed) · **Target:** Adobe Photoshop 2026 Every known shortfall against Photoshop 2026 (27.11.0), one entry each, ranked by user impact on the way to beta. This is the work list agents pick from: take the highest-ranked entry that @@ -54,8 +54,7 @@ what an agent can't finish alone. scripted check there) is the only real oracle. ### G6. Missing tools -- **Missing (4 of 68):** Color Replacement (engine command exists), Art - History Brush, Add Anchor Point, Delete Anchor Point. Convert Point landed (#3204). Freeform +- **Missing (1 of 68):** Art History Brush. Convert Point landed (#3204). Freeform Pen and Curvature Pen landed (#1054); Color Sampler landed (#1046, `view.colorSamplers.*`); Horizontal and Vertical Type Mask landed (#1055, `type.createSelection`); Perspective Crop landed (#1043, `image.perspectiveCrop`); Frame landed (#2914). @@ -263,6 +262,7 @@ what an agent can't finish alone. | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | G6: stale "missing tool" list corrected against `main` — Color Replacement (`paint.colorReplacement`), Red Eye (`paint.redEye`) and Add/Delete Anchor Point (P flyout, #4144) are live tools, so the toolbox is 67 of 68 and Art History Brush is the only one missing; no percentage or estimate changed | | 2026-10-13 | minor | G4: Hue Jitter amount-track appearance (#3825, partial); no readiness percentage or estimate changed | | 2026-10-13 | minor | G4: minimum inputs in Shape Dynamics and Transfer follow their Control (#3825, partial); no readiness percentage or estimate changed | | 2026-10-12 | minor | G5: Free Transform options bar H/V skew fields done | diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index a7c802904b..c11ad8aff8 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1,6 +1,6 @@ # PhotoCraft vs Adobe Photoshop: target-app parity -> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-12 · **Change:** minor (`.atn` import maps 112 events; Free Transform H/V skew fields; stale rows corrected against `main` and the scorecard, estimates unchanged; Photoshop Actions `.atn` load and save; 2026-10-10: Tilt-Shift measurements added; earlier the same day: major, full re-measure against Photoshop 2026 27.11.0; merged `parity-estimate.md` and the roadmap's honest assessment) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-13 · **Change:** minor (`.atn` import maps 112 events; Free Transform H/V skew fields; stale rows corrected against `main` and the scorecard, estimates unchanged; Photoshop Actions `.atn` load and save; 2026-10-10: Tilt-Shift measurements added; earlier the same day: major, full re-measure against Photoshop 2026 27.11.0; merged `parity-estimate.md` and the roadmap's honest assessment; stale missing-tool rows corrected: Color Replacement and Red Eye are live toolbar tools, Frame landed; percentages unchanged) · **Target:** Adobe Photoshop 2026 The authoritative answer to "how close is PhotoCraft to Photoshop, and how much work is left". The one-page summary is [`ROADMAP.md`](../ROADMAP.md); the ranked work list is [`gaps.md`](gaps.md); @@ -444,7 +444,7 @@ filled bar. This extends UI reachability and does not claim Photoshop-authored s | Smart filters / text / effect shapes in PSDs | Measured on our Photoshop-authored set (https://github.com/storytold/photocraft-corpus, `corpus/photoshop`, 258 files): see the per-group floors in `crates/io/tests/corpus.rs` and `crates/engine/tests/photoshop_oracles.rs`. Smart objects and smart filters now survive PSD save and open (41 corpus files, 206 smart objects, round trip strict; Photoshop opens our exports with live filters); re-rendering Photoshop's smart filters with ours matches 5/30 (was 1/30) | low–medium | Remaining re-render gaps are filter maths (Gaussian/Motion Blur, Unsharp Mask, Emboss, Add Noise RNG) and bicubic placement. | | Core editing (layers, masks, selections, adjustments, filters, transforms) | Broad engine coverage; many interaction bugs fixed after 0.2.0 (adjustment dialogs, Curves, crop, Move/Transform modifiers, gesture origin) | medium | Fixes not yet user-validated. | | UI / UX polish | Shortcut audit 214 → 0 failures; dock, Layers rows and menus reworked; first visual-QA sweep found 14 defects (#147–#157). 2026-10-07: the keyboard-only shortcuts with no menu item (⌥[ ⌥] ⌥, ⌥. layer navigation, ⇧⌥[ ⇧⌥] to extend the selection, 1–0 for opacity and ⇧ for flow or fill, ⇧[ ⇧] hardness, ⌥⌘T to transform a copy and ⌥⇧⌘T to step and repeat), ⌥-click colour sampling with painting tools, double-click a Layers row for Layer Style, File › New from Clipboard, a centred main window and remembered Liquify settings (#352, #417, #350, #368, #419, #418). 2026-10-09: canvas zoom is physical at any display scale — 100% is one document pixel per physical display pixel, and every screen-space overlay, cursor, scrollbar, navigator and fit follows it (#1943) | low–medium | Needs recurring visual QA with realistic documents. | -| Tools | 2026-10-10: Pencil, Mixer Brush, Color Replacement, Patch, Content-Aware Move, Vertical Type, Pattern Stamp and Rotate View are toolbar tools. Remaining missing tools include Red Eye, Art History Brush and Frame (Perspective Crop landed 2026-10-10, #1043: `image.perspectiveCrop`) | low–medium | Patch has a live healing preview. Content-Aware Move is partial: Move/Extend, Structure/Color and Sample All Layers exist; Transform On Drop and a live result preview remain missing (`TOOL-213-4` in the scorecard). Magic/Background Eraser added; live gradients in progress (#180). Magnetic Lasso added 2026-10-08 (live-wire edge tracing, Width/Contrast/Frequency, `select.magneticLasso`). Pattern Stamp added 2026-10-08 (S flyout, `paint.patternStamp`, Aligned / Impressionist). Rotate View (2026-10-09) turns the canvas camera around its centre without rewriting pixels; Reset View and Match Rotation copy the angle. Crop rotation (2026-10-09, #1792): dragging outside the crop frame turns it (⇧ 15° steps, angle readout), and ↵ rotates the document and crops in one undo step (`image.crop` `angle`); the frame turns over the image as in Photoshop's Classic Mode, and the options-bar Straighten is still missing. | +| Tools | 2026-10-10: Pencil, Mixer Brush, Color Replacement, Patch, Content-Aware Move, Vertical Type, Pattern Stamp and Rotate View are toolbar tools. The only tool still missing is Art History Brush: Frame landed (#2914), Red Eye is a live toolbar tool (`paint.redEye`, TOOL-213-17) and Perspective Crop landed (2026-10-10, #1043, `image.perspectiveCrop`) | low–medium | Patch has a live healing preview. Content-Aware Move is partial: Move/Extend, Structure/Color and Sample All Layers exist; Transform On Drop and a live result preview remain missing (`TOOL-213-4` in the scorecard). Magic/Background Eraser added; live gradients in progress (#180). Magnetic Lasso added 2026-10-08 (live-wire edge tracing, Width/Contrast/Frequency, `select.magneticLasso`). Pattern Stamp added 2026-10-08 (S flyout, `paint.patternStamp`, Aligned / Impressionist). Rotate View (2026-10-09) turns the canvas camera around its centre without rewriting pixels; Reset View and Match Rotation copy the angle. Crop rotation (2026-10-09, #1792): dragging outside the crop frame turns it (⇧ 15° steps, angle readout), and ↵ rotates the document and crops in one undo step (`image.crop` `angle`); the frame turns over the image as in Photoshop's Classic Mode, and the options-bar Straighten is still missing. | | Painting | Brush model and Brush Settings panel near Photoshop; .abr/.grd import; persistent presets; pen pressure/tilt on Windows, web, macOS and X11 | medium | Native Wayland pen input open (#79; a pen opens the window through Xwayland meanwhile); X11 pressure confirmed by a user, macOS not yet verified on tablet hardware. | | Text / typography | Engine works; caret placement and size editing fixed; OpenType features, text-on-path editing, composer parity partial | medium-low | Measure with the Photoshop-authored set. | | Colour management | Colour-managed canvas (document → monitor), embedded CMYK profiles, linear EXR/HDR, 16-bit float canvas | medium-high | Monitor profile follows only at launch. | @@ -565,6 +565,7 @@ See [method, raw measurements and reproduction](five-algorithm-performance.md). | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | Stale "missing tool" rows corrected against main: Color Replacement (`paint.colorReplacement`), Red Eye (`paint.redEye`) and Add/Delete Anchor Point (the Pen flyout) are live toolbar tools and Frame landed, so Art History Brush is the only tool still missing; percentages unchanged | | 2026-10-12 | minor | Free Transform options bar H/V skew fields; percentages unchanged | | 2026-10-12 | minor | `.atn` import: 112 events mapped (TrueGrit Texture Supply sets play end to end); percentages unchanged | | 2026-10-12 | minor | Photoshop Actions `.atn` load and save (90 events mapped both ways); percentages unchanged | diff --git a/docs/ui-parity.md b/docs/ui-parity.md index f2092fd3e2..5abc8727a4 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity: tools, handles, modifiers and feel -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed) · **Target:** Adobe Photoshop 2026 How PhotoCraft's direct manipulation compares with Photoshop 2026 (27.11.0): the toolbox, the handles and modifier keys of each gesture, nudging, snapping, numeric precision, navigation, @@ -34,13 +34,13 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme | Frame | Frame (frames are groups clipped by a rectangular or elliptical mask) | | | Eyedropper | Eyedropper, Color Sampler, Ruler, Note, Count | | | Retouch | Remove, Spot Healing, Healing, Patch, Content-Aware Move, Red Eye | | -| Brush | Brush, Pencil, Mixer Brush | Color Replacement (`paint.colorReplacement` exists) | +| Brush | Brush, Pencil, Mixer Brush | | | Stamp | Clone Stamp, Pattern Stamp | | | History | History Brush | Art History Brush | | Eraser | Eraser, Background Eraser, Magic Eraser | | | Fill | Gradient, Paint Bucket | | | Focus, toning | Blur, Sharpen, Smudge, Dodge, Burn, Sponge | | -| Pen | Pen, Freeform Pen, Curvature Pen (#1054), Convert Point (#3204; also ⌥ with Pen) | Add Anchor Point, Delete Anchor Point | +| Pen | Pen, Freeform Pen, Curvature Pen (#1054), Convert Point (#3204; also ⌥ with Pen), Add Anchor Point, Delete Anchor Point (P cycles them; #4144 auto add/delete with the Pen itself) | | | Type | Horizontal, Vertical, Vertical Type Mask, Horizontal Type Mask | | | Path selection | Path Selection, Direct Selection | | | Shapes | Rectangle, Ellipse, Triangle, Polygon, Line, Custom Shape | (Star is a Polygon option in Photoshop) | @@ -217,6 +217,7 @@ Paragraph; the rest open as in-app floating windows. | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | Stale "missing tool" rows corrected against main: Color Replacement (`paint.colorReplacement`), Red Eye (`paint.redEye`) and Add/Delete Anchor Point (the P flyout cycle) are live toolbar tools, so only Art History Brush is missing; no percentage or estimate changed | | 2026-10-13 | minor | Smart object perspective (non-affine) corrected to done: #426 landed four-corner placements with PSD round trip; scorecard FILE-215-6 flipped | | 2026-10-10 | major | First version, from a source audit against Photoshop 2026 27.11.0's shortcuts file and the open issues | | 2026-10-10 | minor | Fixed Crop live resize snapping feedback and outdated-camera rendering, applied ratios immediately to centred inscribed previews, and removed two-point options-bar overflow; added pointer, rendering, dropdown/Enter, Swap/X and scrolling regressions | From 2388d821655a5f6081bd5e50f498426c0854ce1a Mon Sep 17 00:00:00 2001 From: Michael Higgins <55243596+Higgs32584@users.noreply.github.com> Date: Wed, 14 Oct 2026 01:58:22 -0400 Subject: [PATCH 21/39] Docs: oracle floor, open issues, workspace size and layer filters match main (#5376) (#5382) - ROADMAP.md: the oracle floor is 145 of 258 (crates/io/tests/corpus.rs, gaps.md G1), not 133 of 256; 953 open issues, not 579. - docs/architecture.md: ~539,000 lines in ~1,310 files and ~8,040 #[test] functions, recounted 2026-10-13. - README.md: "a layer kind filter" instead of "layer filters"; only the Kind filter exists. - AGENTS.md: drop an empty bullet at the end of section 7. Co-authored-by: Claude Opus 5.5 --- AGENTS.md | 1 - README.md | 2 +- ROADMAP.md | 7 ++++--- docs/architecture.md | 9 +++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index e44dbe32a8..6adc5923db 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -131,7 +131,6 @@ Then append a terse entry to `log/devlog.md` (what landed, numbers, what's still - `../craftrules/release/playbook.md`: how every storytold app builds signed release binaries (the canonical recipe; `docs/release-playbook.md` just points there); `docs/releasing.md` is PhotoCraft's specifics. - `plan/` (local, gitignored): research, parity plan, execution plan, estimates. - `log/` (local, gitignored): the dev log. -- ## 8. Keeping the native format complete diff --git a/README.md b/README.md index 5fd2650415..b272705e40 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ Every screenshot here is the real app at work on public-domain art, rendered off

🗃️ Layers, done properly

- Groups, clipping masks, pixel and vector masks, fill layers (solid, gradient and pattern), adjustment layers, live smart objects with smart filters and lossless transforms and warps, multi-layer selection with align, distribute and link, alpha channels and Quick Mask, 27 Photoshop blend modes plus six Paint.NET variants, opacity and fill, locks, colour labels, layer filters, merge, flatten, rasterize, Layer via Copy/Cut, Paste Into. + Groups, clipping masks, pixel and vector masks, fill layers (solid, gradient and pattern), adjustment layers, live smart objects with smart filters and lossless transforms and warps, multi-layer selection with align, distribute and link, alpha channels and Quick Mask, 27 Photoshop blend modes plus six Paint.NET variants, opacity and fill, locks, colour labels, a layer kind filter, merge, flatten, rasterize, Layer via Copy/Cut, Paste Into. diff --git a/ROADMAP.md b/ROADMAP.md index 98e8c1184c..f41b71bfc9 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~30 points (ready for real work ~45% → ~75%, with reliable PSD) and ~1,100–1,800 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-11 · **Change:** minor (toolbox count 56 of 68, 12 missing) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-13 · **Change:** minor (oracle floor 145 of 258 and 953 open issues, as in gaps.md and on GitHub; toolbox count 56 of 68, 12 missing) · **Target:** Adobe Photoshop 2026 PhotoCraft's one-page summary of how close it is to Adobe Photoshop 2026 (27.11.0, measured from the installed app on 2026-10-10) at version 0.6.0. Detail: [target-app parity](docs/target-app-parity.md) @@ -40,7 +40,7 @@ the one above. About 80% parallelizes (independent crates and command modules); architecture and the PSD writer are the serial parts. **Why alpha.** Core workflows (open, layer, select, paint, adjust, filter, type, save) work end to -end, and breadth is high, but users of 0.5/0.6 have 579 open issues, 22 of 25 performance budgets +end, and breadth is high, but 953 issues are open (2026-10-13), 22 of 25 performance budgets are missed, and PSDs we write are not yet reliably accepted by Photoshop (#1281, #2469). Beta needs ~75% ready and no blocking PSD gap. The [alpha gate](docs/roadmap.md#alpha-gate) passes: all six core workflows (retouch, composite, cut-out, design, paint, PSD exchange) complete end to end and save and reopen; @@ -110,7 +110,7 @@ Also shipped: Traditional Chinese, Russian, Ukrainian, Czech, Polish, Greek, Dut | Rank | Milestone | Estimate | |---|---|---:| -| 1 | PSD that Photoshop trusts: Save As corruption, Photoshop re-open check, Photoshop-oracle floor 133 → 200+ of 256 | 150–250 h | +| 1 | PSD that Photoshop trusts: Save As corruption, Photoshop re-open check, Photoshop-oracle floor 145 → 200+ of 258 | 150–250 h | | 2 | Performance budgets: the 22 over-budget scenarios and the 150-layer GPU crash | 200–350 h | | 3 | User bug backlog to under 100 open | 250–400 h | | 4 | Brush and transform feel (brush-parity, ui-parity rows) | 160–290 h | @@ -135,6 +135,7 @@ Detail and the next releases: [docs/roadmap.md](docs/roadmap.md). | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | Focus item 1 quoted the oracle floor as 133 of 256; it is 145 of 258 (`crates/io/tests/corpus.rs`, gaps.md G1). Open issues 579 → 953 (2026-10-13) | | 2026-10-11 | minor | Toolbox count 56 of 68 (12 missing) with the Convert Point tool (#3204) | | 2026-10-10 | minor | Readiness by audience with hours per tier | | 2026-10-10 | minor | Mainstream-practitioner and essentials-user numbers added | diff --git a/docs/architecture.md b/docs/architecture.md index 7856636ccb..67eebe9cce 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,6 +1,6 @@ # PhotoCraft architecture -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-10 · **Change:** minor (current-state summary added; the design plan below is kept, with its deviations listed) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (workspace size and test counts recounted; current-state summary added; the design plan below is kept, with its deviations listed) · **Target:** Adobe Photoshop 2026 ## Current state (2026-10-10) @@ -8,9 +8,9 @@ What exists on `main` today. The numbered sections below are the original design 2026-09-30; where the code differs, this summary wins. - **Workspace:** 25 library crates under `crates/`, 3 apps (`photocraft` desktop, `photocraft-cli`, - `photocraft-web`), `xtask`, an example plug-in and 5 fuzz crates; ~393,000 lines of Rust in - ~1,020 files; ~5,460 `#[test]` functions (ui-egui 1,677, engine 1,237, algo 408, io 377, - codecs 325, psd 263). Crate map and layers: [`AGENTS.md`](../AGENTS.md) §2 and + `photocraft-web`), `xtask`, an example plug-in and 5 fuzz crates; ~539,000 lines of Rust in + ~1,310 files; ~8,040 `#[test]` functions (ui-egui 2,809, engine 1,796, algo 572, io 539, + codecs 426, psd 341), counted 2026-10-13. Crate map and layers: [`AGENTS.md`](../AGENTS.md) §2 and `xtask/src/layers.rs` (enforced by `cargo xtask layers`). - **UI toolkit:** decided: egui/eframe on wgpu (no webview, no JavaScript). The same Rust builds to WebAssembly for the browser (trunk + wasm-bindgen). @@ -588,5 +588,6 @@ Each phase ends with a demoable build and green CI on all targets. Phases 8–10 | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | Workspace size and `#[test]` counts recounted (393k → 539k lines, 5,460 → 8,040 tests); other current-state figures unchanged | | 2026-10-10 | minor | Current-state summary and deviations from the plan; status line; broken link to `photoshop-parity.md` repointed to `target-app-parity.md` | | 2026-09-30 | major | Design plan v1 | From a954194277121a1673cfd579d0182929bb21a705 Mon Sep 17 00:00:00 2001 From: Zer0C00L01 Date: Wed, 14 Oct 2026 00:58:27 -0500 Subject: [PATCH 22/39] PSD import: ResolutionInfo stays pixels per inch when the display unit is cm (#5280) hRes and vRes are stored as pixels per inch. The unit that follows only controls how that number is shown, so a 300 ppi file with unit cm opened at 762 ppi. Export already writes the document ppi and does not divide by 2.54. Fixes #5201 Co-authored-by: Cursor Agent --- crates/io/src/psd_export.rs | 1 + crates/io/src/psd_import.rs | 5 +++-- crates/io/tests/psd_structure.rs | 11 ++++++++--- crates/io/tests/resolution_axes.rs | 20 +++++++++++++++++--- crates/psd/src/resources.rs | 20 ++++++++++++-------- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 9db3d4799f..47861dbcc3 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -1636,6 +1636,7 @@ fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, c // Resources. let mut resources = vec![ + // hRes/vRes are always pixels per inch. The display unit written here is inches. ImageResource::new(ids::RESOLUTION_INFO, ResolutionInfo::from_dpi(f64::from(doc.resolution_dpi)).to_bytes()), ImageResource::new(ids::GLOBAL_ANGLE, (doc.global_light.angle.round() as i32).to_be_bytes().to_vec()), ImageResource::new(ids::GLOBAL_ALTITUDE, (doc.global_light.altitude.round() as i32).to_be_bytes().to_vec()), diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index 444c2d6e5b..211bba4da9 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -673,8 +673,9 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt) match r.id { ids::RESOLUTION_INFO => { if let Ok(ri) = photocraft_psd::ResolutionInfo::from_bytes(&r.data) { - let ppi = |res: f64, unit: u16| if unit == 2 { res * 2.54 } else { res }; - let (x, y) = (ppi(ri.h_res(), ri.h_res_unit), ppi(ri.v_res(), ri.v_res_unit)); + // Fixed 16.16 hRes/vRes are pixels per inch. The unit shorts only + // say how to show that number (1 = px/in, 2 = px/cm). + let (x, y) = (ri.h_res(), ri.v_res()); doc.resolution_dpi = x as f32; warnings.extend(crate::unequal_resolution_warning(x, y)); } diff --git a/crates/io/tests/psd_structure.rs b/crates/io/tests/psd_structure.rs index e893adaf94..5627dca3f4 100644 --- a/crates/io/tests/psd_structure.rs +++ b/crates/io/tests/psd_structure.rs @@ -460,13 +460,18 @@ fn guides_and_alpha_names_roundtrip() { } #[test] -fn resolution_in_cm_converted() { +fn resolution_info_cm_display_unit_stays_pixels_per_inch() { let mut f = testgen::small(Version::Psd, Compression::Raw); - let mut ri = photocraft_psd::ResolutionInfo::from_dpi(100.0); + let mut ri = photocraft_psd::ResolutionInfo::from_dpi(300.0); ri.h_res_unit = 2; + ri.v_res_unit = 2; f.resources[0] = photocraft_psd::ImageResource::new(1005, ri.to_bytes()); let (d, _) = psd_to_document(&f); - assert!((d.resolution_dpi - 254.0).abs() < 1e-3); + assert!((d.resolution_dpi - 300.0).abs() < 1e-3, "{}", d.resolution_dpi); + let out = document_to_psd(&d); + let written = out.resolution().unwrap(); + assert!((written.h_res() - 300.0).abs() < 1e-3, "{}", written.h_res()); + assert!((written.v_res() - 300.0).abs() < 1e-3, "{}", written.v_res()); } #[test] diff --git a/crates/io/tests/resolution_axes.rs b/crates/io/tests/resolution_axes.rs index 58b67d93c4..f7f8cbf006 100644 --- a/crates/io/tests/resolution_axes.rs +++ b/crates/io/tests/resolution_axes.rs @@ -59,13 +59,27 @@ fn psd_with_equal_axes_opens_silently() { let r = import("x.psd", &psd(ResolutionInfo::from_dpi(300.0))).unwrap(); assert!((r.document.resolution_dpi - 300.0).abs() < 1e-3, "{}", r.document.resolution_dpi); assert!(resolution_notes(&r).is_empty(), "{:?}", r.warnings); - // Each axis is read in its own unit: 100 pixels per centimetre on both is 254 ppi on both. - let mut cm = ResolutionInfo::from_dpi(100.0); +} + +/// Resource 1005 stores 300 ppi even when the display unit is centimetres. Scaling +/// that fixed-point value by 2.54 opened the file at 762 ppi (#5201). +#[test] +fn resolution_info_cm_display_unit_is_pixels_per_inch() { + let mut cm = ResolutionInfo::from_dpi(300.0); cm.h_res_unit = 2; cm.v_res_unit = 2; + cm.width_unit = 2; + cm.height_unit = 2; let r = import("x.psd", &psd(cm)).unwrap(); - assert!((r.document.resolution_dpi - 254.0).abs() < 1e-3, "{}", r.document.resolution_dpi); + assert!((r.document.resolution_dpi - 300.0).abs() < 1e-3, "{}", r.document.resolution_dpi); assert!(resolution_notes(&r).is_empty(), "{:?}", r.warnings); + + let out = export(&r.document, "out.psd", &ExportOptions::default()).unwrap(); + let file = photocraft_psd::PsdFile::from_bytes(&out.bytes).unwrap(); + let written = file.resolution().unwrap(); + assert!((written.h_res() - 300.0).abs() < 1e-3, "{}", written.h_res()); + assert!((written.v_res() - 300.0).abs() < 1e-3, "{}", written.v_res()); + assert_eq!((written.h_res_unit, written.v_res_unit), (1, 1)); } /// A layered TIFF opens through the PSD importer with the TIFF's own resolution tags. diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index c20efbfc9e..4d78a062be 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -45,20 +45,24 @@ pub struct ImageResource { } /// Fixed-point resolution info (resource 1005). +/// +/// `h_res_fixed` and `v_res_fixed` are always pixels per inch (16.16 fixed). +/// `h_res_unit` and `v_res_unit` only say how to display that number +/// (1 = pixels/inch, 2 = pixels/cm) and do not rescale the stored value. #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ResolutionInfo { - /// Horizontal resolution, 16.16 fixed point (pixels per inch/cm). + /// Horizontal resolution, 16.16 fixed point, always pixels per inch. pub h_res_fixed: u32, - /// 1 = pixels per inch, 2 = pixels per cm. + /// Display unit for the horizontal resolution (1 = px/in, 2 = px/cm). pub h_res_unit: u16, - /// Display unit for width (1 in, 2 cm, 3 pt, 4 pica, 5 column). + /// Ruler unit for width (1 in, 2 cm, 3 pt, 4 pica, 5 column). pub width_unit: u16, - /// Vertical resolution, 16.16 fixed. + /// Vertical resolution, 16.16 fixed point, always pixels per inch. pub v_res_fixed: u32, - /// Vertical resolution unit. + /// Display unit for the vertical resolution (1 = px/in, 2 = px/cm). pub v_res_unit: u16, - /// Display unit for height. + /// Ruler unit for height (1 in, 2 cm, 3 pt, 4 pica, 5 column). pub height_unit: u16, } @@ -68,11 +72,11 @@ impl ResolutionInfo { let f = (dpi * 65536.0).round().clamp(0.0, u32::MAX as f64) as u32; ResolutionInfo { h_res_fixed: f, h_res_unit: 1, width_unit: 1, v_res_fixed: f, v_res_unit: 1, height_unit: 1 } } - /// Horizontal resolution as float. + /// Horizontal resolution in pixels per inch. pub fn h_res(&self) -> f64 { f64::from(self.h_res_fixed) / 65536.0 } - /// Vertical resolution as float. + /// Vertical resolution in pixels per inch. pub fn v_res(&self) -> f64 { f64::from(self.v_res_fixed) / 65536.0 } From e12b7f73525f9623692c5085a928061a225680f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=C2=B7=20VanTan?= <77483789+VanTan666@users.noreply.github.com> Date: Wed, 14 Oct 2026 07:58:31 +0200 Subject: [PATCH 23/39] fix(prefs): preserve selected themes during legacy migration (#5179) --- crates/engine/src/prefs.rs | 3 ++- crates/engine/src/prefs/tests.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 5e728cb546..b163d40d88 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -1553,7 +1553,8 @@ impl Session { } }; interface.insert("appearanceMode".into(), json!(mode)); - interface.insert(slot.into(), json!(theme.name())); + // A partially migrated file may already contain a newer theme selection (#5139). + interface.entry(slot).or_insert_with(|| json!(theme.name())); } let color = v.as_object_mut().and_then(|m| m.remove("colorSettings")); let presets = v.as_object_mut().and_then(|m| m.remove("presets")); diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs index 36a5b69abc..6b56cbc7ba 100644 --- a/crates/engine/src/prefs/tests.rs +++ b/crates/engine/src/prefs/tests.rs @@ -67,6 +67,17 @@ fn appearance_defaults_and_legacy_theme_migrate() { } } +#[test] +fn legacy_theme_migration_preserves_saved_theme_slots() { + for (legacy, slot, saved) in [("proMedium", "darkTheme", "solarizedDark"), ("studioLight", "lightTheme", "adwaita")] { + let mut interface = json!({"theme": legacy}); + interface[slot] = json!(saved); + let mut session = Session::new(); + session.load_prefs_json(&json!({"interface": interface}).to_string()).unwrap(); + assert_eq!(session.prefs_value()["interface"][slot], saved, "explicit {slot} survives migration"); + } +} + #[test] fn appearance_choices_validate_and_legacy_theme_still_selects() { let mut s = Session::new(); From 077e513686ce4f79e957d3ce71340495cbcdb0af Mon Sep 17 00:00:00 2001 From: Gert Pellin - GPE Date: Wed, 14 Oct 2026 07:58:36 +0200 Subject: [PATCH 24/39] Color Range presets: weigh dark colours like Photoshop (#5243) (#5293) Reds/Yellows/Greens/Cyans/Blues/Magentas selected hue closeness times chroma. Measured against Photoshop 2025 on 126 colours per preset (Reds, Yellows, Blues; hue +-40 degrees, saturation 5..100 %, brightness 33..100 %), Photoshop also multiplies by (2 - max): bright colours were already exact, darker ones came out up to 61 levels short. With the factor every sample matches within one level. The muted navy #3d3f54 from the issue now gets 35/255, as in Photoshop (it was 21): Photoshop also leaves it below 50 %. --- crates/algo/src/selection.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/algo/src/selection.rs b/crates/algo/src/selection.rs index ad858df2a2..94f744d0be 100644 --- a/crates/algo/src/selection.rs +++ b/crates/algo/src/selection.rs @@ -682,18 +682,23 @@ fn hue_chroma([r, g, b]: [f32; 3]) -> (f32, f32) { /// Color Range › Select: Reds, Yellows, Greens, Cyans, Blues or Magentas (`center` = 0, 60, /// 120, 180, 240, 300°). A pixel belongs to a family by how close its hue is (full at the -/// family's hue, none 60° away, so an orange is half red, half yellow) times its chroma, so -/// neutrals are never selected and muted colours only partly. +/// family's hue, none 60° away, so an orange is half red, half yellow) times its chroma +/// (max − min), so neutrals are never selected and muted colours only partly. Darker colours +/// weigh more, by `2 − max`: measured against Photoshop 2025's presets on 126 colours each for +/// Reds, Yellows and Blues (hue ±40°, saturation 5..100 %, brightness 33..100 %), this matches +/// its selection to within one level, where chroma alone fell up to 61 levels short (#5243). pub fn hue_range(px: &[[f32; 4]], center: f32) -> Vec { px.iter() .map(|&[r, g, b, a]| { if a <= 0.0 { return 0.0; } - let (h, chroma) = hue_chroma([r, g, b].map(|v| v.clamp(0.0, 1.0))); + let rgb = [r, g, b].map(|v| v.clamp(0.0, 1.0)); + let (h, chroma) = hue_chroma(rgb); + let max = rgb[0].max(rgb[1]).max(rgb[2]); let dh = (h - center).rem_euclid(360.0); let dh = dh.min(360.0 - dh); - ((1.0 - dh / 60.0).clamp(0.0, 1.0) * chroma).clamp(0.0, 1.0) + ((1.0 - dh / 60.0).clamp(0.0, 1.0) * chroma * (2.0 - max)).clamp(0.0, 1.0) }) .collect() } @@ -1180,6 +1185,17 @@ mod tests { assert!((hue_range(&[[1.0, 0.0, 0.5, 1.0]], 300.0)[0] - 0.5).abs() < 1e-5); } + #[test] + fn hue_families_weigh_dark_colours_like_photoshop() { + // Photoshop 2025's Blues on an 8-bit sRGB document (selection levels 0..255, #5243): the + // muted navy #3d3f54, a dark blue (0, 0, 84) and a mid blue (61, 61, 153). + let c = |r: u8, g: u8, b: u8| [f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0, 1.0]; + let blues = hue_range(&[c(0x3d, 0x3f, 0x54), c(0, 0, 84), c(61, 61, 153)], 240.0); + for (got, want) in blues.iter().zip([35.0, 140.0, 129.0]) { + assert!((got * 255.0 - want).abs() <= 1.0, "{} vs Photoshop {want}", got * 255.0); + } + } + #[test] fn tonal_ranges() { let g = |v: f32| [v / 255.0, v / 255.0, v / 255.0, 1.0]; From 33b360d5ee2a45e22053265239f52875ce3110ce Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Wed, 14 Oct 2026 16:58:42 +1100 Subject: [PATCH 25/39] Reject clipboard origins that cannot form a new document (#4586) * Reject clipboard origins that cannot be moved into a new document. * Fill Clip.resolution in the unrepresentable-origin clipboard test. main added resolution to Clip; the synthetic overflow fixture omitted it and failed engine lib tests. * Format the clipboard origin test (cargo fmt) Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/engine/src/edit_cmds.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rs index f0541136ad..20ef23a41b 100644 --- a/crates/engine/src/edit_cmds.rs +++ b/crates/engine/src/edit_cmds.rs @@ -495,9 +495,12 @@ fn new_from_clipboard(s: &mut Session) -> Result { if w == 0 || h == 0 { return Err(EngineError::Other("the clipboard image has no size".into())); } + let (Some(dx), Some(dy)) = (b.x0.checked_neg(), b.y0.checked_neg()) else { + return Err(EngineError::Other("the clipboard origin is outside the coordinate range for a new document".into())); + }; let fmt = clip.surface.format(); let target = PixelFormat::new(fmt.mode, fmt.sample, true); - let moved = if b.x0 == 0 && b.y0 == 0 { clip.surface } else { photocraft_algo::resample::translate_surface(&clip.surface, -b.x0, -b.y0) }; + let moved = if dx == 0 && dy == 0 { clip.surface } else { photocraft_algo::resample::translate_surface(&clip.surface, dx, dy) }; let mut doc = Document::new("Untitled", photocraft_geom::Size::new(w, h), fmt.mode, fmt.sample); if let Some(r) = clip.resolution.filter(|r| r.is_finite() && *r > 0.0) { doc.resolution_dpi = r; @@ -1044,6 +1047,19 @@ mod tests { s } + #[test] + fn new_clipboard_document_rejects_an_unrepresentable_origin() { + let mut s = Session::new(); + let mut surface = Surface::new(PixelFormat::RGBA8); + surface.write_pixel(i32::MIN, 0, &[1.0, 0.0, 0.0, 1.0]); + s.clipboard = Some(Clip { surface, bounds: Rect::from_xywh(i32::MIN, 0, 1, 1), layers: None, resolution: None }); + let result = new_from_clipboard(&mut s); + assert!(result.unwrap_err().to_string().contains("clipboard origin")); + assert!(s.execute("file.newFromClipboard", json!({})).is_err()); + assert!(s.documents().is_empty()); + assert_eq!(s.clipboard.as_ref().unwrap().surface.rgba(i32::MIN, 0), [1.0, 0.0, 0.0, 1.0]); + } + #[test] fn injected_clear_buffer_failure_leaves_the_document_unchanged() { let mut s = session(); From 79e2107f71cbe12e5dd4bb9aaf79f91449c50453 Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Wed, 14 Oct 2026 16:58:46 +1100 Subject: [PATCH 26/39] Report channel copy allocation failures without losing the clipboard. (#4587) --- crates/engine/src/channel_clip.rs | 9 +++++---- crates/engine/src/channel_clip/tests.rs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/channel_clip.rs b/crates/engine/src/channel_clip.rs index fc52cf0a43..fea1048235 100644 --- a/crates/engine/src/channel_clip.rs +++ b/crates/engine/src/channel_clip.rs @@ -59,15 +59,16 @@ pub(crate) fn copy(s: &mut Session) -> Result> { return Err(EngineError::Other("Could not copy: the selected area is empty".into())); } let fmt = PixelFormat::new(ColorMode::Grayscale, doc.pixel_format().sample, true); - let values = src.read_region(area); - let cover = sel.map(|m| (m.read_region(area), m.format().channels().max(1))); - let mut px = Vec::with_capacity(values.len() / n * 2); + let values = crate::allocation::read_region(src, area, "copying a channel")?; + let cover = sel.map(|m| crate::allocation::read_region(m, area, "copying a channel selection").map(|v| (v, m.format().channels().max(1)))).transpose()?; + let len = (values.len() / n).checked_mul(2).ok_or_else(|| EngineError::Other("the channel is too large to copy".into()))?; + let mut px = crate::allocation::capacity(len, "copying a channel")?; for (i, p) in values.chunks_exact(n).enumerate() { px.push(p.get(k).copied().unwrap_or(0.0)); px.push(cover.as_ref().map_or(1.0, |(m, mk)| m.get(i * mk).copied().unwrap_or(0.0))); } let mut surface = Surface::new(fmt); - surface.write_region(area, &px); + surface.try_write_region(area, &px)?; surface.prune(); let bounds = surface.content_bounds(); if bounds.is_empty() { diff --git a/crates/engine/src/channel_clip/tests.rs b/crates/engine/src/channel_clip/tests.rs index 39cb029b3d..be7e51dfd7 100644 --- a/crates/engine/src/channel_clip/tests.rs +++ b/crates/engine/src/channel_clip/tests.rs @@ -163,3 +163,22 @@ fn clearing_a_channel_reports_allocation_failure_without_editing() { assert_eq!(s.active().unwrap().history.entries(), history); } } + +#[test] +fn channel_copy_allocation_failure_keeps_clipboard_and_document() { + for depth in [8, 16, 32] { + let mut s = session(depth); + s.execute("edit.copy", json!({})).unwrap(); + s.execute("channel.target", json!({"channel": "red"})).unwrap(); + let before = s.active().unwrap().doc.clone(); + let clip = s.clipboard.as_ref().unwrap().surface.clone(); + let history = s.active().unwrap().history.entries(); + crate::allocation::fail_next_for_test(); + let result = copy(&mut s); + let _ = crate::allocation::capacity::(0, "reset failpoint"); + assert!(result.is_err()); + assert_eq!(*s.active().unwrap().doc, *before); + assert_eq!(s.active().unwrap().history.entries(), history); + assert_eq!(s.clipboard.as_ref().unwrap().surface, clip); + } +} From 7c916d9ecdd8a36c1e1f6d5ac9691fe604cd2bd7 Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Wed, 14 Oct 2026 16:58:51 +1100 Subject: [PATCH 27/39] Validate selection modification limits before allocating the canvas mask. (#4591) --- crates/engine/src/selection_cmds.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs index 8efb8d7b94..74de286f04 100644 --- a/crates/engine/src/selection_cmds.rs +++ b/crates/engine/src/selection_cmds.rs @@ -461,7 +461,12 @@ fn modify(s: &mut Session, p: &Value, op: &str) -> Result { // continue past the canvas. Border has no such option: its band always follows the canvas edge, // or a Select All would border to nothing. let at_bounds = op == "border" || b(p, "applyAtCanvasBounds", false); - let (area, m) = current_mask(&s.active().ok_or(EngineError::NoDocument)?.doc); + let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; + let area = doc.bounds(); + if area.size().area() > MAX_MODIFY_PIXELS as u64 { + return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: "the selection is too large to modify".into() }); + } + let (_, m) = current_mask(doc); let (w, h) = (area.width() as usize, area.height() as usize); // Feather reaches 3σ = 3 r (σ = r, as in Photoshop); the others reach r. A margin past that // keeps the canvas edge out of reach. A selection clear of the canvas edge needs none: both @@ -765,6 +770,28 @@ mod tests { assert!(err.to_string().contains("detectFaces"), "{err}"); } + #[test] + fn modify_rejects_oversized_canvases_before_reading_the_mask() { + let mut s = Session::new(); + let mut doc = photocraft_doc::Document::new( + "large", + photocraft_geom::Size::new(300_000, 300_000), + photocraft_color::ColorMode::Rgb, + photocraft_color::SampleType::U8, + ); + let mut selection = photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8); + selection.write_pixel(0, 0, &[1.0]); + doc.selection = Some(selection); + s.add_document(doc, None); + let before = s.active().unwrap().doc.clone(); + for op in ["expand", "contract", "border", "smooth", "feather"] { + let error = s.execute(&format!("select.modify.{op}"), json!({"radius": 1})).unwrap_err(); + assert!(error.to_string().contains("too large to modify")); + assert_eq!(*s.active().unwrap().doc, *before); + assert!(!s.active().unwrap().history.can_undo()); + } + } + #[test] fn color_range_replace_is_bounded_by_existing_selection() { for params in [json!({"color": "#ff0000", "fuzziness": 0}), json!({"select": "reds"})] { From 2da2889546ec0c40f02628a1db0af13d9260e30d Mon Sep 17 00:00:00 2001 From: Gert Pellin - GPE Date: Wed, 14 Oct 2026 07:58:55 +0200 Subject: [PATCH 28/39] Background: an opacity, fill, blend mode or Select and Mask layer mask makes it Layer 0 (#4837) The Background has none of them. layer.setProps applied them to it as it was, and Select and Mask's Layer Mask output gave it a mask. Like Photoshop (and like PhotoCraft already does for moving, transforming, erasing, styles and Layer > Layer Mask), it now becomes a normal layer, "Layer 0", first. Values that leave its look alone (opacity and fill 100 %, Normal) keep it the Background. Fixes #4836 --- crates/engine/src/background_cmds.rs | 26 ++++++++++++++++++++++++++ crates/engine/src/commands.rs | 7 +++++++ crates/engine/src/smartselect_cmds.rs | 1 + 3 files changed, 34 insertions(+) diff --git a/crates/engine/src/background_cmds.rs b/crates/engine/src/background_cmds.rs index 81c47501d3..e733b54809 100644 --- a/crates/engine/src/background_cmds.rs +++ b/crates/engine/src/background_cmds.rs @@ -144,6 +144,32 @@ mod tests { (s, [bg, a, b]) } + #[test] + fn opacity_fill_or_a_blend_mode_turn_the_background_into_a_layer() { + // Like Photoshop: the Background takes none of them, so it becomes "Layer 0" first. + for (key, value) in [("opacity", json!(0.5)), ("fill", json!(0.5)), ("blend", json!("Multiply"))] { + let (mut s, [bg, ..]) = with_background(); + s.execute("layer.setProps", json!({"layer": bg.0, key: value})).unwrap(); + let l = doc(&s).layer(bg).unwrap(); + assert!(!doc(&s).is_background(bg) && l.name == "Layer 0", "{key}"); + assert!(l.opacity < 1.0 || l.fill_opacity < 1.0 || l.blend == photocraft_color::BlendMode::Multiply, "{key}: applied"); + } + // Values that leave its look alone keep it the Background. + let (mut s, [bg, ..]) = with_background(); + s.execute("layer.setProps", json!({"layer": bg.0, "opacity": 1.0, "fill": 1.0, "blend": "Normal", "visible": false})).unwrap(); + assert!(doc(&s).is_background(bg)); + } + + #[test] + fn select_and_mask_output_to_a_layer_mask_turns_the_background_into_a_layer() { + let (mut s, [bg, ..]) = with_background(); + s.execute("layer.select", json!({"layer": bg.0})).unwrap(); + s.execute("select.rect", json!({"x": 2, "y": 2, "width": 6, "height": 6})).unwrap(); + s.execute("select.refineEdge", json!({"output": "layerMask"})).unwrap(); + let l = doc(&s).layer(bg).unwrap(); + assert!(l.mask.is_some() && !doc(&s).is_background(bg) && l.name == "Layer 0"); + } + #[test] fn makes_the_layer_the_background_at_the_bottom() { for depth in [8, 16, 32] { diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index a32c5feb8d..318b105d12 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -688,6 +688,13 @@ fn build() -> Vec { // Clipping and channel changes reach the layers around it: recomposite everything. let local = p.get("clipped").is_none() && p.get("channels").is_none(); s.edit(label, |doc, _| { + // The Background has no opacity, fill or blend mode: like Photoshop, setting + // one that changes its look turns it into a normal layer ("Layer 0") first. + let num = |k: &str| p.get(k).and_then(Value::as_f64).is_some_and(|v| v < 1.0); + let blends = p.get("blend").and_then(Value::as_str).and_then(blend_from_str).is_some_and(|b| b != photocraft_color::BlendMode::Normal); + if num("opacity") || num("fill") || blends { + crate::extra_cmds::background_to_layer(doc, id); + } let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; if let Some(v) = p.get("name").and_then(Value::as_str) { l.name = v.to_string(); diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs index 651db7cdc0..d8c2b345a9 100644 --- a/crates/engine/src/smartselect_cmds.rs +++ b/crates/engine/src/smartselect_cmds.rs @@ -417,6 +417,7 @@ fn refine_edge(s: &mut Session, p: &Value) -> Result { s.edit("Select and Mask", |doc, _| { let surface = region.as_ref().map(matting::region_surface).unwrap_or_else(|| photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8)); + crate::extra_cmds::background_to_layer_for_mask(doc, id); let existing = doc.layer(id).and_then(|l| l.mask.as_ref()).cloned().unwrap_or_else(LayerMask::reveal_all); doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.mask = Some(LayerMask { surface, ..existing }); doc.selection = None; From 4bbcede5725fdb8bdb4bfd501eef58b5af140834 Mon Sep 17 00:00:00 2001 From: Gert Pellin - GPE Date: Wed, 14 Oct 2026 07:59:00 +0200 Subject: [PATCH 29/39] Fibers: fill an empty layer, as Photoshop does (#5241) (#5289) Fibers renders from the foreground and background colours without reading the layer, but its output area was the layer's content grown by a zero halo: on an empty layer that is empty, so nothing was drawn (and a following transform found nothing to transform). On a partly filled layer only the content's bounding box was filled. It now writes the reference bounds (the selection's, else the canvas) like the global filters, without reading the whole layer as they do. --- crates/algo/src/lib.rs | 14 +++++++++++--- crates/algo/src/tests.rs | 4 ++++ crates/engine/src/filters_ext/tests.rs | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs index 78dae7b06f..49ab15871c 100644 --- a/crates/algo/src/lib.rs +++ b/crates/algo/src/lib.rs @@ -600,6 +600,13 @@ impl FilterParams { self.halo() == Halo::Bounds } + /// Whether the filter renders new pixels without reading the layer's own (Fibers): it fills + /// the reference bounds even where the layer is transparent, as Photoshop's does on an + /// empty layer, while still reading no neighbourhood. + pub fn renders_from_nothing(&self) -> bool { + matches!(self, FilterParams::Fibers { .. }) + } + /// Human-readable name. pub fn label(&self) -> &'static str { match self { @@ -905,12 +912,13 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec) -> Rect { let mut area = match params.halo_for(bounds) { Halo::Bounds => bounds, + Halo::Radius(_) if params.renders_from_nothing() => bounds, Halo::Radius(r) => { if content.is_empty() { Rect::EMPTY diff --git a/crates/algo/src/tests.rs b/crates/algo/src/tests.rs index 9a90012df9..31def5f99d 100644 --- a/crates/algo/src/tests.rs +++ b/crates/algo/src/tests.rs @@ -446,6 +446,10 @@ fn halos_and_output_area() { assert_eq!(output_area(&FilterParams::Twirl { angle: 1.0 }, content, R, None), R); assert_eq!(output_area(&FilterParams::BoxBlur { radius: 1.0 }, content, R, Some(Rect::new(0, 0, 12, 12))), Rect::new(8, 8, 12, 12)); assert!(output_area(&FilterParams::Invert, Rect::EMPTY, R, None).is_empty()); + // Fibers renders without reading the layer: an empty layer still fills the bounds (#5241). + let fibers = FilterParams::Fibers { variance: 16.0, strength: 4.0, seed: 0, foreground: [0.0; 4], background: [1.0; 4] }; + assert_eq!(output_area(&fibers, Rect::EMPTY, R, None), R); + assert_eq!(output_area(&fibers, content, R, Some(Rect::new(0, 0, 12, 12))), Rect::new(0, 0, 12, 12)); } #[test] diff --git a/crates/engine/src/filters_ext/tests.rs b/crates/engine/src/filters_ext/tests.rs index b616101f7c..0f32c5367d 100644 --- a/crates/engine/src/filters_ext/tests.rs +++ b/crates/engine/src/filters_ext/tests.rs @@ -164,6 +164,27 @@ fn colour_filters_use_and_record_tool_colours() { } } +#[test] +fn fibers_fill_an_empty_layer() { + // #5241: Fibers renders from the colours, not the layer's pixels, so an empty layer fills + // like in Photoshop (and can be transformed afterwards). + let mut s = Session::new(); + s.execute("file.new", json!({"width": 48, "height": 32})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("filter.render.fibers", json!({"seed": 3})).unwrap(); + let px = pixels(&s); + assert!(px.as_chunks::<4>().0.iter().all(|p| p[3] == 1.0), "every pixel is rendered opaque"); + s.execute("edit.transform.flipHorizontal", json!({})).unwrap(); + + // With a selection only the selection fills. + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 6})).unwrap(); + s.execute("filter.render.fibers", json!({"seed": 3})).unwrap(); + let d = s.active().unwrap(); + let surf = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap(); + assert_eq!(surf.content_bounds(), Rect::new(4, 4, 14, 10)); +} + #[test] fn displace_reads_another_document() { let mut s = session(); From 15f7618504d9eaeb5ed910de9baa7a64c171a288 Mon Sep 17 00:00:00 2001 From: Gert Pellin - GPE Date: Wed, 14 Oct 2026 07:59:04 +0200 Subject: [PATCH 30/39] Object Selection: say so when the sampled layer is empty (#5246) (#5298) select.object samples the active layer by default. On an empty layer (scripts often add one above the photo) it found nothing and returned without an error; the failure only surfaced at the next command that needed a selection. It now fails with an error that names the cause and the sampleAllLayers option, also when the layer's pixels lie outside the rectangle or point. --- crates/engine/src/smartselect_cmds.rs | 38 ++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs index d8c2b345a9..ea77e022b0 100644 --- a/crates/engine/src/smartselect_cmds.rs +++ b/crates/engine/src/smartselect_cmds.rs @@ -217,10 +217,23 @@ impl ObjectFinderCache { } } +/// Object Selection samples only the active layer by default: when that layer has no pixels in +/// `area`, say so instead of quietly finding nothing (#5246; scripts often add an empty layer +/// above the photo first). +fn check_sampled_pixels(doc: &Document, layer: Option, all_layers: bool, area: Rect) -> Result<()> { + let Some(surf) = sampled_target(doc, layer, all_layers).and_then(|id| doc.layer(id)).and_then(Layer::surface) else { return Ok(()) }; + if surf.content_bounds().intersect(&area).is_empty() { + let msg = if surf.content_bounds().is_empty() { "the active layer is empty" } else { "the active layer has no pixels in that area" }; + return Err(bad("select.object", &format!("{msg}; set \"sampleAllLayers\": true or select a layer with pixels"))); + } + Ok(()) +} + /// `select.object` with `"point"`: the Object Finder's object under the point (cached). fn object_at_point(s: &mut Session, p: &Value, (x, y): (f32, f32)) -> Result { let all_layers = b(p, "sampleAllLayers", false); let d = s.active().ok_or(EngineError::NoDocument)?; + check_sampled_pixels(&d.doc, d.active_layer, all_layers, Rect::from_xywh(x.floor() as i32, y.floor() as i32, 1, 1))?; let target = sampled_target(&d.doc, d.active_layer, all_layers); let region = match s.object_finder.lookup(&d.doc, target, x, y) { Some(r) => Some(r), @@ -248,7 +261,10 @@ fn object_selection(s: &mut Session, p: &Value) -> Result { return object_at_point(s, p, pt); } let rect = rect_param(p).ok_or_else(|| bad("select.object", "needs \"rect\": [x, y, w, h] or \"point\": [x, y]"))?; - let region = with_sampler(s, b(p, "sampleAllLayers", false), |smp, doc| grabcut::object_select(smp, doc.bounds(), rect, 160_000))?; + let all_layers = b(p, "sampleAllLayers", false); + let d = s.active().ok_or(EngineError::NoDocument)?; + check_sampled_pixels(&d.doc, d.active_layer, all_layers, rect)?; + let region = with_sampler(s, all_layers, |smp, doc| grabcut::object_select(smp, doc.bounds(), rect, 160_000))?; apply(s, "Object Selection", region.as_ref(), mode(p, "replace")) } @@ -731,6 +747,26 @@ mod tests { assert!(cov(&s, 100, 80) >= 0.5); } + #[test] + fn object_selection_on_an_empty_active_layer_says_so() { + // #5246: an empty layer above the photo used to find nothing without a word. + let (mut s, inside) = disc(8); + s.execute("layer.new.layer", json!({})).unwrap(); + for p in [json!({"rect": [40, 20, 120, 120]}), json!({"point": [100, 80]})] { + let e = s.execute("select.object", p.clone()).unwrap_err().to_string(); + assert!(e.contains("active layer is empty") && e.contains("sampleAllLayers"), "{p}: {e}"); + assert!(s.active().unwrap().doc.selection.is_none()); + } + // Sampling all layers finds the disc below. + s.execute("select.object", json!({"rect": [40, 20, 120, 120], "sampleAllLayers": true})).unwrap(); + assert!(sel_iou(&s, 200, 160, &inside) > 0.95); + // A layer with pixels elsewhere: the rectangle holds none of them. + s.execute("select.deselect", json!({})).unwrap(); + s.execute("paint.pencil", json!({"points": [[5, 5], [10, 5]], "size": 2, "color": "#000000"})).unwrap(); + let e = s.execute("select.object", json!({"rect": [60, 60, 100, 80]})).unwrap_err().to_string(); + assert!(e.contains("no pixels in that area"), "{e}"); + } + #[test] fn select_subject_centred_object() { let (mut s, inside) = disc(8); From 7e28900df31a6a64d16b9d252cfd7551b57b7352 Mon Sep 17 00:00:00 2001 From: Gert Pellin - GPE Date: Wed, 14 Oct 2026 07:59:09 +0200 Subject: [PATCH 31/39] layer.setProps: refuse opacity and fill outside 0..1 (#5242) (#5291) Opacity and Fill are fractions in layer.setProps but percentages in the Layer Style commands, and a value above 1 was clamped silently: 55 gave an opaque layer. Out-of-range or non-numeric values now fail with an error that names the range, and the layer is left alone. --- crates/engine/src/commands.rs | 45 +++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index 318b105d12..46361d9513 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -681,6 +681,16 @@ fn build() -> Vec { has_layer, |s, p| { let id = layer_param(s, p)?; + // Opacity and Fill are fractions here, while the Layer Style commands take + // percentages: a value outside 0..1 is refused, never clamped to 100 % (#5242). + let unit = |key: &str| -> Result> { + let Some(v) = p.get(key) else { return Ok(None) }; + match v.as_f64() { + Some(x) if (0.0..=1.0).contains(&x) => Ok(Some(x as f32)), + _ => Err(bad("layer.setProps", format!("{key} must be a number in 0..1 (1 = 100 %), got {v}"))), + } + }; + let (opacity, fill) = (unit("opacity")?, unit("fill")?); let only_visibility = p.as_object().is_some_and(|o| o.contains_key("visible") && o.keys().all(|k| matches!(k.as_str(), "layer" | "visible" | "coalesce"))); let label = if only_visibility { "Layer Visibility" } else { "Layer Properties" }; @@ -702,11 +712,11 @@ fn build() -> Vec { if let Some(v) = p.get("visible").and_then(Value::as_bool) { l.visible = v; } - if let Some(v) = p.get("opacity").and_then(Value::as_f64) { - l.opacity = (v as f32).clamp(0.0, 1.0); + if let Some(v) = opacity { + l.opacity = v; } - if let Some(v) = p.get("fill").and_then(Value::as_f64) { - l.fill_opacity = (v as f32).clamp(0.0, 1.0); + if let Some(v) = fill { + l.fill_opacity = v; } if let Some(v) = p.get("blend").and_then(Value::as_str) { l.blend = blend_from_str(v).ok_or_else(|| bad("layer.setProps", format!("unknown blend mode `{v}`")))?; @@ -1554,3 +1564,30 @@ mod registry_tests { assert_eq!(registry.specs.len(), 2); } } + +#[cfg(test)] +mod set_props_tests { + use super::*; + + #[test] + fn opacity_and_fill_outside_zero_to_one_are_refused_not_clamped() { + // #5242: `55` (a percentage, as the Layer Style commands take) used to become 100 %. + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.setProps", json!({"opacity": 0.55, "fill": 0.4})).unwrap(); + let layer = |s: &Session| { + let d = s.active().unwrap(); + let l = d.doc.layer(d.active_layer.unwrap()).unwrap(); + (l.opacity, l.fill_opacity) + }; + assert_eq!(layer(&s), (0.55, 0.4)); + for bad in [json!({"opacity": 55}), json!({"opacity": -0.1}), json!({"fill": 1.5}), json!({"opacity": "50%"}), json!({"opacity": 0.5, "fill": 40})] { + let e = s.execute("layer.setProps", bad.clone()).unwrap_err().to_string(); + assert!(e.contains("0..1"), "{bad}: {e}"); + assert_eq!(layer(&s), (0.55, 0.4), "{bad} left the layer alone"); + } + s.execute("layer.setProps", json!({"opacity": 0, "fill": 1})).unwrap(); + assert_eq!(layer(&s), (0.0, 1.0)); + } +} From 7a0e60c1047567e37b4693aa182578f63f9a43b6 Mon Sep 17 00:00:00 2001 From: Nicolas Falesy <112726756+nicolasfalesy@users.noreply.github.com> Date: Wed, 14 Oct 2026 01:59:13 -0400 Subject: [PATCH 32/39] test(engine): add Color Range fuzziness 0 exact-match test (TOOL-214-3) (#3581) * test(engine): add Color Range fuzziness 0 exact-match test (#214) Adds an explicit test for Sampled Colors at Fuzziness 0 (point sampling and hex colors) asserting that exact matches are fully selected and adjacent shades are rejected. Updates scorecard TOOL-214-3 to done. * docs: regenerate scorecard (TOOL-214-3 done) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017WeVmjnnPPKdxVTpr2Vjtw --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/selection_cmds.rs | 39 +++++++++++++++++++++++++++++ docs/scorecard.md | 4 +-- scorecard/tools.toml | 4 +-- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs index 74de286f04..be0639418d 100644 --- a/crates/engine/src/selection_cmds.rs +++ b/crates/engine/src/selection_cmds.rs @@ -1004,6 +1004,45 @@ mod tests { assert!(lighter > redder + 0.15 && redder > 0.0, "lighter {lighter}, redder {redder}"); } + #[test] + fn color_range_fuzziness_zero_selects_exact_match_only() { + // Sampled Colors at Fuzziness 0 selects only the exact match (TOOL-214-3, #214). + let mut s = Session::new(); + s.execute("file.new", json!({"width": 50, "height": 10})).unwrap(); + s.edit("paint", |doc, _| { + let bg = doc.layers[0].surface_mut().unwrap(); + let v = |c: u8| f32::from(c) / 255.0; + // Exact target color [200, 140, 120] + bg.fill_rect(Rect::new(0, 0, 10, 10), &[v(200), v(140), v(120), 1.0]); + // Off by 1 level in red [201, 140, 120] + bg.fill_rect(Rect::new(10, 0, 20, 10), &[v(201), v(140), v(120), 1.0]); + // Off by 1 level in green [200, 141, 120] + bg.fill_rect(Rect::new(20, 0, 30, 10), &[v(200), v(141), v(120), 1.0]); + // Off by 2 levels in blue [200, 140, 122] (crosses 8-bit Lab grid threshold) + bg.fill_rect(Rect::new(30, 0, 40, 10), &[v(200), v(140), v(122), 1.0]); + // Exact target color again at a different position + bg.fill_rect(Rect::new(40, 0, 50, 10), &[v(200), v(140), v(120), 1.0]); + Ok(()) + }) + .unwrap(); + + // Sampled via eyedropper point at [5, 5] with Fuzziness 0 + s.execute("select.colorRange", json!({"points": [[5, 5]], "fuzziness": 0})).unwrap(); + assert_eq!(coverage(&s, 5, 5), 1.0, "sampled pixel itself is fully selected"); + assert_eq!(coverage(&s, 45, 5), 1.0, "identical color elsewhere on canvas is fully selected"); + assert_eq!(coverage(&s, 15, 5), 0.0, "1 level off in red is unselected at fuzziness 0"); + assert_eq!(coverage(&s, 25, 5), 0.0, "1 level off in green is unselected at fuzziness 0"); + assert_eq!(coverage(&s, 35, 5), 0.0, "1 level off in blue is unselected at fuzziness 0"); + + // Sampled via hex color with Fuzziness 0 + s.execute("select.colorRange", json!({"color": "#c88c78", "fuzziness": 0})).unwrap(); + assert_eq!(coverage(&s, 5, 5), 1.0, "exact color match from hex is fully selected"); + assert_eq!(coverage(&s, 45, 5), 1.0, "identical color elsewhere on canvas is fully selected"); + assert_eq!(coverage(&s, 15, 5), 0.0, "1 level off in red is unselected for hex at fuzziness 0"); + assert_eq!(coverage(&s, 25, 5), 0.0, "1 level off in green is unselected for hex at fuzziness 0"); + assert_eq!(coverage(&s, 35, 5), 0.0, "1 level off in blue is unselected for hex at fuzziness 0"); + } + /// Red squares (from `session`), a blue square, a black and a mid-grey strip on white. fn session_colours() -> Session { let mut s = session(); diff --git a/docs/scorecard.md b/docs/scorecard.md index 92f9315ebd..56a16654bd 100644 --- a/docs/scorecard.md +++ b/docs/scorecard.md @@ -8,7 +8,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js |---|---:|---:|---:|---| | [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 17 not run, 5 not measurable yet (baseline 2026-10-05) | | [File compatibility](#file-compatibility) | 7 | 13 | 7 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 145/256, round trip ≥ 258/256 | -| [Tools](#tools) | 28 | 14 | 1 | | +| [Tools](#tools) | 29 | 13 | 1 | | | [Workspace and UI](#workspace-and-ui) | 13 | 6 | 4 | settings that do nothing: 26 of 157 (target 0) | | [Type](#type) | 4 | 3 | 2 | | | [Automation](#automation) | 5 | 2 | 4 | 631 of 631 menu items live (100.0%) | @@ -147,7 +147,7 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t | TOOL-213-18 | Pattern Stamp tool: sibling of Clone Stamp in the S flyout | done | [#213](https://github.com/storytold/photocraft/issues/213) | Tool::PatternStamp in the S group; paint.patternStamp stamps the Patterns-panel tile with Aligned and Impressionist (an approximation: it jitters each dab's tile phase, not Photoshop's smeared impressionist look); engine tests cover depths, seams, hostile input | | TOOL-214-1 | Select and Mask workspace: view modes, Refine Edge brush, 6 outputs | partial | [#214](https://github.com/storytold/photocraft/issues/214) | select.refineEdge dialog with a live preview in Photoshop's view modes: Onion Skin, Marching Ants, Overlay, On Black, On White, Black & White, On Layers, per-view Opacity, Show Edge (J), Show Original (P), F cycles, X shows the image (select_mask_view.rs, #1754); Refine Edge Brush (R, size, Expand Detection Area / Restore Original Edge, Alt swaps) paints select.refineEdge refineStrokes, estimated by colour-line matting (matting/brush.rs, #2972); outputs Selection, Layer Mask, New Layer, New Layer with Mask, New Document, New Document with Mask (#2998); no workspace layout | | TOOL-214-2 | Marquee: Shift square, Alt from centre, live W x H readout | done | [#214](https://github.com/storytold/photocraft/issues/214) | canvas.rs marquee_corners: Shift square or circle, Alt from the centre, live W x H readout beside the cursor (marquee_readout); marquee_tests.rs | -| TOOL-214-3 | Color Range test: Sampled Colors at Fuzziness 0 selects the exact match only | partial | [#214](https://github.com/storytold/photocraft/issues/214) | Fuzziness-0 tests cover midtones/shadows only (selection_cmds.rs:444-447) | +| TOOL-214-3 | Color Range test: Sampled Colors at Fuzziness 0 selects the exact match only | done | [#214](https://github.com/storytold/photocraft/issues/214) | color_range_fuzziness_zero_selects_exact_match_only in selection_cmds.rs asserts exact match is selected and 1-level-off pixels are rejected at fuzziness 0; tonal presets covered in color_range_presets | | TOOL-214-4 | Move tool: box-select layers, Auto-Select hover outline, double-click fit, drag to a tab | partial | [#214](https://github.com/storytold/photocraft/issues/214) | Auto-Select hover outline (Preferences › Tools › Show Highlight on Rollover, #2834) and box-select layers from an empty spot (move_box.rs, layer.selectInRect, #2844) done; double-click fit and drag to a tab open | | TOOL-214-5 | Right-click menu during Free Transform (scale, rotate, skew, warp, flip, rotate 90/180) | done | [#214](https://github.com/storytold/photocraft/issues/214) | canvas_tool_menu.rs TRANSFORM_MENU: modes, Content-Aware Scale / Puppet Warp greyed, Rotate 180 / 90 CW / 90 CCW and Flip turn the open box (transform_tool::intercept, #2832) | | TOOL-214-6 | Puppet Warp mesh follows the object outline (constrained triangulation) | done | [#214](https://github.com/storytold/photocraft/issues/214) | algo/puppet.rs build_mesh: traced outline of the opaque region grown by Expansion, triangulated with a Density-spaced lattice (conforming Delaunay); separate shapes get separate meshes (#3105) | diff --git a/scorecard/tools.toml b/scorecard/tools.toml index cbff50e333..1123c6aab6 100644 --- a/scorecard/tools.toml +++ b/scorecard/tools.toml @@ -127,9 +127,9 @@ note = "canvas.rs marquee_corners: Shift square or circle, Alt from the centre, [[item]] id = "TOOL-214-3" target = "Color Range test: Sampled Colors at Fuzziness 0 selects the exact match only" -status = "partial" +status = "done" issue = 214 -note = "Fuzziness-0 tests cover midtones/shadows only (selection_cmds.rs:444-447)" +note = "color_range_fuzziness_zero_selects_exact_match_only in selection_cmds.rs asserts exact match is selected and 1-level-off pixels are rejected at fuzziness 0; tonal presets covered in color_range_presets" [[item]] id = "TOOL-214-4" From c0cb78090b694241a88fd62b6f47ed652d82fd8a Mon Sep 17 00:00:00 2001 From: prekabreki <133610573+prekabreki@users.noreply.github.com> Date: Wed, 14 Oct 2026 07:59:18 +0200 Subject: [PATCH 33/39] panic_hunt: count panics that Session::execute contains (#4982) (#5219) Session::execute catches a command's panic and returns an ordinary Err (the last-resort guard in jobs.rs), so the unwind never reached panic_hunt's own catch_unwind and a panicking command counted as a pass. The harness now installs a panic hook that counts every panic in the process, including on rayon workers, and a run fails if the count moved. Runs are serialised so a panic is charged to the run that caused it. No production code changes. Test: contained_panics_count_and_graceful_errors_do_not. A contained panic fails the run and a graceful Err passes; the first assertion fails without the hook. The full --ignored fuzz stays green, so no command panics today. Co-authored-by: prekabreki Co-authored-by: Claude Opus 5.5 --- crates/engine/tests/panic_hunt.rs | 59 +++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/crates/engine/tests/panic_hunt.rs b/crates/engine/tests/panic_hunt.rs index fc68b21164..893ba5c23d 100644 --- a/crates/engine/tests/panic_hunt.rs +++ b/crates/engine/tests/panic_hunt.rs @@ -5,7 +5,8 @@ use photocraft_engine::{Session, command_specs}; use serde_json::{Value, json}; use std::panic::{AssertUnwindSafe, catch_unwind}; -use std::sync::mpsc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::{Mutex, Once, PoisonError, mpsc}; use std::time::Duration; fn fresh() -> Session { @@ -22,19 +23,41 @@ fn fresh() -> Session { /// 0.3 s even on a heavily loaded machine, so this only catches real hangs. const DEADLINE: Duration = Duration::from_secs(4); -/// Run `id` with `p` on a fresh session, on a worker thread. `None` if it timed out, else whether -/// it finished without panicking (returning Ok or a graceful Err both count). -fn run_once(id: &str, p: &Value) -> Option { - let (tx, rx) = mpsc::channel(); - let (id, p) = (id.to_string(), p.clone()); - std::thread::spawn(move || { - let r = catch_unwind(AssertUnwindSafe(|| { - let mut s = fresh(); - let _ = s.execute(&id, p); +/// Panics anywhere in the process, counted by a hook. `Session::execute` contains a command's +/// panic and returns an ordinary `Err`, so the unwind never reaches `catch_unwind` here; the hook +/// still sees it, on whichever thread (rayon workers included) it happened (#4982). +static PANICS: AtomicUsize = AtomicUsize::new(0); +/// One run at a time, so a panic is charged to the run that caused it. +static RUN: Mutex<()> = Mutex::new(()); + +/// Run `f` on a worker thread. `None` if it timed out, else whether it finished without +/// panicking, contained or not (returning Ok or a graceful Err both count). +fn run_watched(f: impl FnOnce() + Send + 'static) -> Option { + static HOOK: Once = Once::new(); + HOOK.call_once(|| { + let default = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |info| { + PANICS.fetch_add(1, Ordering::SeqCst); + default(info); })); + }); + let _one = RUN.lock().unwrap_or_else(PoisonError::into_inner); + let before = PANICS.load(Ordering::SeqCst); + let (tx, rx) = mpsc::channel(); + std::thread::spawn(move || { + let r = catch_unwind(AssertUnwindSafe(f)); let _ = tx.send(r.is_ok()); }); - rx.recv_timeout(DEADLINE).ok() + rx.recv_timeout(DEADLINE).ok().map(|ok| ok && PANICS.load(Ordering::SeqCst) == before) +} + +/// Run `id` with `p` on a fresh session; see [`run_watched`]. +fn run_once(id: &str, p: &Value) -> Option { + let (id, p) = (id.to_string(), p.clone()); + run_watched(move || { + let mut s = fresh(); + let _ = s.execute(&id, p); + }) } /// `Err` with a reason on panic or hang, `Ok` otherwise. @@ -80,3 +103,17 @@ fn no_command_panics_or_hangs_on_adversarial_params() { } assert!(bad.is_empty(), "{} commands panicked or hung:\n{}", bad.len(), bad.join("\n")); } + +/// #4982: a panic the code under test contains still fails the run; a graceful `Err` does not. +#[test] +fn contained_panics_count_and_graceful_errors_do_not() { + let contained = run_watched(|| { + let _ = catch_unwind(|| panic!("contained, as Session::execute does")); + }); + assert_eq!(contained, Some(false)); + let graceful = run_watched(|| { + let mut s = fresh(); + assert!(s.execute("layer.delete", json!({"layer": 999999})).is_err()); + }); + assert_eq!(graceful, Some(true)); +} From c7366b8938cf722af8e098bea25c8cc55abb4f37 Mon Sep 17 00:00:00 2001 From: Deeev201 Date: Wed, 14 Oct 2026 18:59:23 +1300 Subject: [PATCH 34/39] Drop shadow: knocked out beneath the whole shape when the layer's mode blends with it (#5304) Layer Knocks Out Drop Shadow only knocked the shadow out through see-through fill: at 100 % fill the layer was taken to cover it. That holds in Normal mode, but a layer in Exclusion, Difference, Multiply, Screen and the rest blends with what's beneath it, shadow and all: a white Exclusion bar with a drop shadow over black type came out as a blank white bar instead of white type on black (Photoshop's merged image of the same file shows the shadow kept out from under the bar), and a Multiply layer was darkened by its own shadow. shadow_see_through() now knocks it out wholly in those modes, for the CPU compositor and the GPU plan alike. Co-authored-by: Daniel van Lith Co-authored-by: Claude Opus 5.5 --- crates/compose/src/effects.rs | 21 ++++++++++++++++----- crates/compose/src/tests.rs | 30 ++++++++++++++++++++++++++++++ crates/gpu/src/plan.rs | 5 +++-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs index 6966254547..5acdd79314 100644 --- a/crates/compose/src/effects.rs +++ b/crates/compose/src/effects.rs @@ -13,7 +13,8 @@ //! Interior effects are painted relative to the layer's shape and then take the shape's alpha //! (an overlay recolours a half-transparent edge without adding coverage). The outside parts //! of strokes blend onto the backdrop with their own modes, an upper stroke covering the -//! lower ones. Drop shadows are knocked out only through see-through fill. +//! lower ones. Drop shadows are knocked out through see-through fill, and beneath the whole shape +//! when the layer's mode lets what's under it show through (see [`shadow_see_through`]). //! //! Shapes come from the layer's alpha (after its mask). A stroke's outer edge, and precise glows / //! chiselled bevels, use an exact Euclidean transform: a 5 × 5 chamfer's level sets are polygons, @@ -1850,6 +1851,16 @@ fn knock_out_exterior(map: &mut Map, shape: &Map, fill_opacity: f32) { } } +/// How far the layer lets a knocked-out drop shadow show beneath its own shape (Layer Knocks Out +/// Drop Shadow), 0 = covered, 1 = knocked out wholly. In a normal mode an opaque layer covers the +/// shadow anyway, so only see-through fill knocks it out (and anti-aliased edges are not +/// attenuated twice). In any other mode the layer blends with what's beneath it, shadow and all: +/// an Exclusion or Difference layer would invert it, Multiply darken by it, Screen show it as +/// grey. Photoshop keeps it out from under the shape, so here it's knocked out wholly. +pub fn shadow_see_through(layer: &Layer) -> f32 { + if matches!(layer.blend, BlendMode::Normal | BlendMode::Dissolve | BlendMode::PassThrough) { 1.0 - layer.fill_opacity.clamp(0.0, 1.0) } else { 1.0 } +} + /// Blend Interior Effects as Group off (Photoshop's default): the layer's mode applies to its /// own pixels only, and the interior effects paint over the blended result. The layer is merged /// into the backdrop before they paint, so an Overlay colour overlay on a black Color-mode layer @@ -1916,10 +1927,10 @@ pub(crate) fn composite_with_effects_prepared( if let Effect::DropShadow(s) = e { let mut m = fx(i, 0); if s.knocks_out { - // The layer hides the shadow beneath it only where its fill is see-through: at - // 100 % fill the layer covers it anyway (and anti-aliased edges are not - // attenuated twice), at 0 % the shape shows the bare backdrop. - knock_out_exterior(&mut m, &shape, layer.fill_opacity); + // The layer hides the shadow beneath it where its fill is see-through (at 100 % + // fill in a normal mode it covers it anyway; at 0 % the shape shows the bare + // backdrop), and wholly in a mode that blends with it ([`shadow_see_through`]). + knock_out_exterior(&mut m, &shape, 1.0 - shadow_see_through(layer)); } paint_color(&mut work, &m, rgb(&s.color), s.common.blend, s.common.opacity); } diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index f9e49ac04e..778d9cf29a 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -1473,6 +1473,36 @@ fn shadow_knockout_needs_see_through_fill() { assert!(close4(on, px(&base, 10, 20)), "{on:?}"); } +#[test] +fn shadow_knockout_beneath_a_layer_in_a_blending_mode() { + // Layer Knocks Out Drop Shadow keeps the shadow out from under the layer's shape. In Normal + // mode an opaque layer covers it anyway; in a mode that blends with what's beneath, the shadow + // would show through: under a white Exclusion square on white, it read as white (white over + // the black shadow) instead of black (white over white), so a logo's Exclusion bar with a + // drop shadow came out blank. A red Multiply square came out black instead of red. + for (blend, colour, inside, depth) in [ + (photocraft_color::BlendMode::Exclusion, [1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0]), + (photocraft_color::BlendMode::Multiply, [1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 1.0]), + ] + .into_iter() + .flat_map(|c| SampleType::ALL.map(|depth| (c, depth))) + .map(|((b, c, i), depth)| (b, c, i, depth)) + { + let mut d = Document::new("t", Size::new(40, 40), ColorMode::Rgb, depth); + d.layers.push(solid_layer("bg", Rect::new(0, 0, 40, 40), [1.0; 4])); + let mut l = solid_layer("sq", Rect::new(10, 10, 30, 30), colour); + l.blend = blend; + l.effects.items = vec![Effect::DropShadow(shadow(0.0, 90.0))]; + d.layers.push(l); + assert!(close4(px(&d, 20, 20), inside), "{blend:?} {depth:?}: {:?}", px(&d, 20, 20)); + // Not knocked out, the shadow shows through the blend, as in Photoshop with the box unticked. + if let Effect::DropShadow(s) = &mut d.layers[1].effects.items[0] { + s.knocks_out = false; + } + assert!(!close4(px(&d, 20, 20), inside), "{blend:?} {depth:?} without knockout: {:?}", px(&d, 20, 20)); + } +} + #[test] fn linked_pattern_overlay_anchors_at_the_effects_reference_point() { use photocraft_doc::pattern::Pattern; diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs index b8c083c7af..48abbc0930 100644 --- a/crates/gpu/src/plan.rs +++ b/crates/gpu/src/plan.rs @@ -1097,9 +1097,10 @@ impl<'a> Planner<'a> { }; for &(i, e) in &rev { if let Effect::DropShadow(s) = e { - // The layer hides the shadow beneath it where its fill is see-through. + // The layer hides the shadow beneath it where its fill is see-through, and wholly + // in a mode that blends with it (as the CPU renderer: `shadow_see_through`). let flags = if s.knocks_out { F_KNOCKOUT } else { 0 }; - let see_through = 1.0 - layer.fill_opacity.clamp(0.0, 1.0); + let see_through = photocraft_compose::effects::shadow_see_through(layer); w = self.paint_k( w, content, From 7ad75b5db2b113f2dc08fe5807a6b24c4c795482 Mon Sep 17 00:00:00 2001 From: abc2cb Date: Wed, 14 Oct 2026 08:59:27 +0300 Subject: [PATCH 35/39] Layer masks: Reveal Selection drops the selection, as Photoshop does (#5251) A mask made from the selection takes the selection with it in Photoshop: after Layer > Layer Mask > Reveal Selection (the Layers panel's mask button with a selection) the marching ants are gone. PhotoCraft kept the selection, so the usual next step, Invert (Cmd/Ctrl+I) on the new mask, inverted only the part inside the old selection and left the whole mask black, instead of flipping the mask. Hide Selection (the Alt-click variant) already dropped it. Reveal Selection now clears the selection in the same history step as the mask: one undo brings the selection back and removes the mask. Tests: adjust_cmds::tests::reveal_selection_drops_the_selection_so_invert_flips_the_whole_mask (8/16/32-bit). Tests that deselected right after Reveal Selection no longer need to (Deselect is disabled without a selection); layer_mask_depth and the automation agent tasks now expect the selection to be gone. Co-authored-by: abc2cb <271337822+abc2cb@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- crates/automation/tests/agent_tasks.rs | 4 +-- crates/engine/src/adjust_cmds/tests.rs | 27 +++++++++++++++++-- crates/engine/src/commands.rs | 17 +++++++++++- crates/engine/src/file_cmds/tests.rs | 3 --- crates/engine/src/filters.rs | 1 - crates/engine/src/mask_view_cmds/tests.rs | 1 - .../engine/src/rasterize_style_cmds/tests.rs | 1 - crates/engine/src/selection_cmds.rs | 1 - crates/engine/src/smartselect_cmds.rs | 1 - crates/engine/src/vector_cmds/tests.rs | 1 - crates/engine/tests/layer_mask_depth.rs | 10 +++++-- crates/ui-egui/src/mask_thumbs_ui/tests.rs | 1 - 12 files changed, 51 insertions(+), 17 deletions(-) diff --git a/crates/automation/tests/agent_tasks.rs b/crates/automation/tests/agent_tasks.rs index 271993d2bb..e1585e2040 100644 --- a/crates/automation/tests/agent_tasks.rs +++ b/crates/automation/tests/agent_tasks.rs @@ -166,7 +166,7 @@ async fn agent_completes_ten_scripted_tasks() { let outside = pixel(&c, 1, 1).await; assert!(inside[0] > 0.99 && inside[1] < 0.01, "task 5: centre is red {inside:?}"); assert!(outside[0] < 0.1, "task 5: corner shows the photo {outside:?}"); - run(&c, "select.deselect", json!({})).await; + assert_eq!(inspect(&c).await["hasSelection"], false, "task 5: the selection became the mask"); // 6. Save a selection to a channel and load it back. run(&c, "select.rect", json!({"x": 4, "y": 6, "width": 20, "height": 10})).await; @@ -413,7 +413,7 @@ async fn agent_completes_ten_more_scripted_tasks() { assert_eq!(top_layer(&doc)["hasMask"], true, "task 17"); let boundary = pixel(&c, 20, 40).await; assert!(boundary[3] > 0.05 && boundary[3] < 0.95, "task 17: the feathered edge keeps partial alpha {boundary:?}"); - run(&c, "select.deselect", json!({})).await; + assert_eq!(doc["hasSelection"], false, "task 17: the selection became the mask"); // 18. Expand a selection and read its bounds back. run(&c, "select.rect", json!({"x": 20, "y": 20, "width": 20, "height": 20})).await; diff --git a/crates/engine/src/adjust_cmds/tests.rs b/crates/engine/src/adjust_cmds/tests.rs index 8fa053e86e..9f614732de 100644 --- a/crates/engine/src/adjust_cmds/tests.rs +++ b/crates/engine/src/adjust_cmds/tests.rs @@ -353,7 +353,6 @@ fn invert_with_the_mask_targeted_inverts_the_mask() { let mut s = session(depth, "rgb"); s.execute("select.rect", json!({"x": 24, "y": 0, "width": 24, "height": 32})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); let (pixels, off_canvas) = (rgba(&s, 3, 3), mask_at(&s, -500, 3)); let steps = s.active().unwrap().history.past_len(); s.execute("image.adjustments.invert", json!({"target": "mask"})).unwrap(); @@ -372,6 +371,31 @@ fn invert_with_the_mask_targeted_inverts_the_mask() { } } +/// Photoshop: a mask made from the selection (Reveal Selection, the Layers panel's mask button) +/// takes the selection with it, as Hide Selection does. So ⌘I on the new mask right away inverts +/// all of it; with the selection kept it inverted only the inside, leaving the mask all black. +/// Undo brings the selection back with the mask gone, in one step. +#[test] +fn reveal_selection_drops_the_selection_so_invert_flips_the_whole_mask() { + for depth in DEPTHS { + let mut s = session(depth, "rgb"); + s.execute("select.rect", json!({"x": 24, "y": 0, "width": 24, "height": 32})).unwrap(); + let selection = s.active().unwrap().doc.selection.clone(); + let steps = s.active().unwrap().history.past_len(); + s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); + assert!(s.active().unwrap().doc.selection.is_none(), "depth {depth}: the selection became the mask"); + assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "one step"); + assert_eq!((mask_at(&s, 3, 3), mask_at(&s, 30, 3)), (0.0, 1.0)); + s.execute("image.adjustments.invert", json!({"target": "mask"})).unwrap(); + assert_eq!((mask_at(&s, 3, 3), mask_at(&s, 30, 3)), (1.0, 0.0), "depth {depth}: inside and outside both flip"); + s.undo(); + s.undo(); + let st = s.active().unwrap(); + assert_eq!(st.doc.selection, selection, "undo restores the selection"); + assert!(st.doc.layer(st.active_layer.unwrap()).unwrap().mask.is_none(), "and removes the mask"); + } +} + // ---------- Image › Adjustments as smart filters (FILE-215-4) ---------- /// A painted pixel layer converted to a smart object, over the painted Background. @@ -492,7 +516,6 @@ fn a_targeted_mask_enables_editing_it_on_any_layer() { s.execute("layer.delete", json!({})).unwrap(); s.execute("select.rect", json!({"x": 24, "y": 0, "width": 24, "height": 32})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); let pixels = rgba(&s, 23, 3); s.execute("filter.blur.gaussianBlur", json!({"radius": 3, "target": "mask"})).unwrap(); assert!(mask_at(&s, 23, 3) > 0.0 && mask_at(&s, 23, 3) < 1.0, "the mask's edge is blurred"); diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs index 46361d9513..a954eca7e2 100644 --- a/crates/engine/src/commands.rs +++ b/crates/engine/src/commands.rs @@ -805,7 +805,10 @@ fn build() -> Vec { |s, p| { let sel = s.active().and_then(|d| d.doc.selection.clone()).ok_or(EngineError::Other("no selection".into()))?; let mask = LayerMask { surface: sel, ..LayerMask::reveal_all() }; - set_mask(s, p, "Add Layer Mask", Some(mask)) + // The selection becomes the mask, so Photoshop drops it (as Hide Selection does): a + // following Invert (⌘I) on the targeted mask then inverts all of it, not just the + // part inside the old selection. Same undo step as the mask. + set_mask_and_deselect(s, p, "Add Layer Mask", mask) } ), cmd!("layer.layerMask.delete", "Delete", ["Layer", "Layer Mask"], None, r##"{"layer":id?}"##, has_layer, |s, p| set_mask( @@ -1444,6 +1447,15 @@ fn arrange(s: &mut Session, p: &Value, delta: i32) -> Result { /// Add (`Some`) or remove (`None`) a layer mask. Adding acts on every selected layer that can take /// one ([`crate::layer_multi_cmds::mask_targets`]), in one history step. fn set_mask(s: &mut Session, p: &Value, label: &str, mask: Option) -> Result { + set_mask_with(s, p, label, mask, false) +} + +/// [`set_mask`] that also drops the selection the mask was made from, in the same history step. +fn set_mask_and_deselect(s: &mut Session, p: &Value, label: &str, mask: LayerMask) -> Result { + set_mask_with(s, p, label, Some(mask), true) +} + +fn set_mask_with(s: &mut Session, p: &Value, label: &str, mask: Option, deselect: bool) -> Result { let ids = if mask.is_some() { crate::layer_multi_cmds::mask_targets(s, p)? } else { vec![layer_param(s, p)?] }; s.edit(label, |doc, _| { let mask = mask.clone().map(|mask| mask_at_depth(mask, doc.depth)).transpose()?; @@ -1453,6 +1465,9 @@ fn set_mask(s: &mut Session, p: &Value, label: &str, mask: Option) -> } doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?.mask = mask.clone(); } + if deselect { + doc.selection = None; + } Ok(()) })?; Ok(Value::Null) diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rs index 99ddce15de..9b6567c2a8 100644 --- a/crates/engine/src/file_cmds/tests.rs +++ b/crates/engine/src/file_cmds/tests.rs @@ -586,7 +586,6 @@ fn flatten_all_layer_effects_and_masks() { s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 40})).unwrap(); s.execute("select.inverse", json!({})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); assert!(composite(&s, 15, 15)[1] > 0.99 && composite(&s, 15, 15)[0] > 0.99, "white shows through the mask"); let r = s.execute("file.scripts.flattenAllMasks", json!({})).unwrap(); assert_eq!(r["flattened"], 1); @@ -633,7 +632,6 @@ fn flatten_all_layer_effects_keeps_masks() { .unwrap(); s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 40})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); // Layer 2: a red square at the bottom-right with the same overlay and a *disabled* mask. s.execute("layer.new.layer", json!({})).unwrap(); s.execute("select.rect", json!({"x": 30, "y": 30, "width": 8, "height": 8})).unwrap(); @@ -652,7 +650,6 @@ fn flatten_all_layer_effects_keeps_masks() { .unwrap(); s.execute("select.rect", json!({"x": 34, "y": 34, "width": 2, "height": 2})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); s.edit("disable", |doc, _| { if let Some(m) = doc.layer_mut(id2).and_then(|l| l.mask.as_mut()) { m.enabled = false; diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs index a9b6564309..9cd6ff4600 100644 --- a/crates/engine/src/filters.rs +++ b/crates/engine/src/filters.rs @@ -460,7 +460,6 @@ mod tests { if reveal_selection { s.execute("select.all", json!({})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); } else { s.execute("layer.layerMask.revealAll", json!({})).unwrap(); } diff --git a/crates/engine/src/mask_view_cmds/tests.rs b/crates/engine/src/mask_view_cmds/tests.rs index 33e36b1421..c73ca0d0ab 100644 --- a/crates/engine/src/mask_view_cmds/tests.rs +++ b/crates/engine/src/mask_view_cmds/tests.rs @@ -10,7 +10,6 @@ fn session() -> (Session, u64, u64) { s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); s.execute("select.rect", json!({"x": 20, "y": 0, "width": 20, "height": 20})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); let st = s.active_mut().unwrap(); st.saved_revision = st.revision; (s, masked, bg) diff --git a/crates/engine/src/rasterize_style_cmds/tests.rs b/crates/engine/src/rasterize_style_cmds/tests.rs index 9b5ef7d359..57db9adbae 100644 --- a/crates/engine/src/rasterize_style_cmds/tests.rs +++ b/crates/engine/src/rasterize_style_cmds/tests.rs @@ -78,7 +78,6 @@ fn fill_opacity_masks_and_layer_opacity_survive_the_bake() { s.execute("layer.setProps", json!({"layer": id, "fill": 0.4, "opacity": 0.7})).unwrap(); s.execute("select.rect", json!({"x": 0, "y": 0, "width": 34, "height": 48})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); let square = json!({"subpaths": [{"closed": true, "knots": [[4, 4], [60, 4], [60, 26], [4, 26]]}]}); s.execute("layer.vectorMask.add", json!({"layer": id, "path": square})).unwrap(); bake_and_check(&mut s, id, depth); diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs index be0639418d..ecc812a51c 100644 --- a/crates/engine/src/selection_cmds.rs +++ b/crates/engine/src/selection_cmds.rs @@ -877,7 +877,6 @@ mod tests { let mut s = session(); s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap(); assert_eq!(coverage(&s, 2, 2), 1.0, "gray mask view selects the mask"); diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs index ea77e022b0..b535fd1525 100644 --- a/crates/engine/src/smartselect_cmds.rs +++ b/crates/engine/src/smartselect_cmds.rs @@ -1007,7 +1007,6 @@ mod tests { fn refine_edge_works_on_layer_mask_without_selection() { let mut s = blurred_edge_selected(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); assert!(s.active().unwrap().doc.selection.is_none()); assert!(s.active().unwrap().doc.layers[0].mask.is_some()); assert!(s.is_enabled("select.refineEdge")); diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rs index 4c2a9a9071..c11de7c813 100644 --- a/crates/engine/src/vector_cmds/tests.rs +++ b/crates/engine/src/vector_cmds/tests.rs @@ -646,7 +646,6 @@ fn rasterizing_a_vector_mask_keeps_what_the_masks_show() { if let Some((feather, density)) = pm { s.execute("select.rect", json!({"x": 0, "y": 0, "width": 40, "height": 64})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); s.edit("mask settings", |doc, _| { let m = doc.layer_mut(id).unwrap().mask.as_mut().unwrap(); (m.feather, m.density) = (feather, density); diff --git a/crates/engine/tests/layer_mask_depth.rs b/crates/engine/tests/layer_mask_depth.rs index 082761286c..7093164a5f 100644 --- a/crates/engine/tests/layer_mask_depth.rs +++ b/crates/engine/tests/layer_mask_depth.rs @@ -45,7 +45,12 @@ fn newly_created_layer_masks_follow_document_depth() { assert_eq!(m.value(20, 20), white); if selected { assert_eq!(m.value(0, 0), 0.0); - assert_eq!(d.doc.selection, before.selection, "the selection is unchanged"); + if command == "layer.layerMask.revealSelection" { + // The selection became the mask: Photoshop drops it, as Hide Selection does. + assert!(d.doc.selection.is_none(), "{command}: the selection is dropped"); + } else { + assert_eq!(d.doc.selection, before.selection, "the selection is unchanged"); + } } else { assert_eq!(m.surface.tile_count(), 0, "uniform masks stay sparse"); assert_eq!(m.value(-300_000, 300_000), white); @@ -133,7 +138,8 @@ fn mask_creation_preserves_selection_coverage_and_existing_masks() { before.selection.as_ref().unwrap().read_region(before.bounds()), "{depth}-bit coverage is unchanged" ); - assert_eq!(after.selection, before.selection); + let kept = if command == "layer.layerMask.revealSelection" { None } else { before.selection.clone() }; + assert_eq!(after.selection, kept, "{command}"); assert_eq!(after.layers[0].mask, before.layers[0].mask, "creating a mask does not requantize an existing mask"); } } diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rs index 50ff96f083..eda8224fa0 100644 --- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs +++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs @@ -223,7 +223,6 @@ fn dragging_the_mask_thumbnail_to_trash_asks_to_apply_or_delete_it() { s.execute("layer.layerMask.delete", json!({"layer": masked})).unwrap(); s.execute("select.rect", json!({"x": 100, "y": 0, "width": 100, "height": 100})).unwrap(); s.execute("layer.layerMask.revealSelection", json!({"layer": masked})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); let mut h = harness(s, 0, 1.0, 290.0); let drag_mask = |h: &mut Harness<'_, PhotocraftApp>| { let from = recorded(&h.ctx, masked).unwrap().0.into_iter().find(|(k, _)| *k == MaskKind::Pixel).unwrap().1.center(); From ff57499774f65a733413b45a645516476741c43b Mon Sep 17 00:00:00 2001 From: Timothy Daly <100327160+timothydaly1@users.noreply.github.com> Date: Wed, 14 Oct 2026 01:59:32 -0400 Subject: [PATCH 36/39] PSD: Mt16/Mt32 blocks mark the merged image's transparency (#5178) The merged image carries its transparency in the first extra channel when the layer count is stored negative, or when a "Saving Merged Transparency" global block (Mtrn, Mt16, Mt32) is present. Photoshop saves 32-bit documents the second way: a positive layer count, Mt32, and a fourth channel named "Transparency" (psd-tools transparentbg). merged_has_alpha only knew the first, so opening such a file added a "Transparency" alpha channel to the document, and the oracle compared it as if it were opaque white (83.7 % of pixels off; now 0.9 %, all on the shape's edge). Co-authored-by: Claude Opus 5.5 --- crates/io/tests/psd32_export.rs | 33 +++++++++++++++++++++++++++++++++ crates/psd/src/file.rs | 8 ++++++-- docs/file-format-parity.md | 3 ++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/io/tests/psd32_export.rs b/crates/io/tests/psd32_export.rs index cbb4a31524..9ff2b4113b 100644 --- a/crates/io/tests/psd32_export.rs +++ b/crates/io/tests/psd32_export.rs @@ -203,3 +203,36 @@ fn merged_oracle_does_not_treat_extra_channels_as_transparency() { assert_eq!(merged_composite(&file).unwrap(), vec![[2.0, 2.0, 2.0, 1.0]], "{mode:?}"); } } + +// Photoshop saves a transparent 32-bit document with a positive layer count and an `Mt32` +// ("Saving Merged Transparency") global block: the merged image's first extra channel, named +// "Transparency", is its transparency (psd-tools transparentbg.psd). The oracle reads it as the +// composite's alpha, and opening the file doesn't turn it into an alpha channel. +#[test] +fn mt32_marks_the_merged_transparency() { + let mut doc = Document::new("transparent", Size::new(2, 1), ColorMode::Rgb, SampleType::F32); + let fmt = doc.pixel_format(); + let mut layer = photocraft_doc::Layer::raster("shape", fmt); + if let Some(s) = layer.surface_mut() { + s.fill_rect(photocraft_geom::Rect::new(0, 0, 1, 1), &[0.0, 0.0, 0.0, 1.0]); + } + doc.layers.push(layer); + let mut file = PsdFile::from_bytes(&export_psd(&doc)).unwrap(); + file.header.channels = 4; + if let Some(li) = file.layer_info.as_mut() { + li.merged_alpha = false; + } + file.image_data.compression = Compression::Raw; + let plane = |v: [f32; 2]| v.into_iter().flat_map(f32::to_be_bytes).collect::>(); + file.image_data.data = [plane([0.0, 1.0]), plane([0.0, 1.0]), plane([0.0, 1.0]), plane([1.0, 0.0])].concat(); + let name: Vec = [0u8, 0, 0, 13].into_iter().chain("Transparency\0".encode_utf16().flat_map(u16::to_be_bytes)).collect(); + file.resources.retain(|r| r.id != 1045); + file.resources.push(photocraft_psd::ImageResource::new(1045, name)); + assert!(!file.merged_has_alpha(), "a positive layer count alone"); + file.global_blocks.push(photocraft_psd::TaggedBlock::new(*b"Mt32", Vec::new())); + assert!(file.merged_has_alpha()); + let merged = merged_composite(&file).unwrap(); + assert_eq!(merged.iter().map(|p| p[3]).collect::>(), vec![1.0, 0.0]); + let opened = import("transparent.psd", &file.to_bytes().unwrap()).unwrap(); + assert!(opened.document.channels.is_empty(), "{:?}", opened.document.channels.iter().map(|c| &c.name).collect::>()); +} diff --git a/crates/psd/src/file.rs b/crates/psd/src/file.rs index 1e86665ad8..effaa1fa11 100644 --- a/crates/psd/src/file.rs +++ b/crates/psd/src/file.rs @@ -272,7 +272,10 @@ impl PsdFile { self.image_data.decode(&self.header) } - /// `true` if the merged image carries a transparency channel. + /// `true` if the merged image carries a transparency channel: its first extra channel, when + /// the layer count was stored negative or a "Saving Merged Transparency" global block (`Mtrn`, + /// `Mt16`, `Mt32`) is present. Photoshop marks 32-bit documents with `Mt32` and a positive + /// layer count (psd-tools transparentbg.psd). pub fn merged_has_alpha(&self) -> bool { let Some(color) = self.header.color_mode.color_channels() else { return false; @@ -280,8 +283,9 @@ impl PsdFile { if self.header.channels <= color { return false; } + let marked = self.global_blocks.iter().any(|b| matches!(&b.key, b"Mtrn" | b"Mt16" | b"Mt32")); match &self.layer_info { - Some(li) => li.merged_alpha, + Some(li) => li.merged_alpha || marked, None => true, } } diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index cb2ca13acd..590b9febc5 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -1,6 +1,6 @@ # File-format parity -> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-13 · **Change:** minor (palette PNGs open in Indexed Color mode, #4804; earlier: CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-12 · **Last updated:** 2026-10-13 · **Change:** minor (PSD: `Mt16`/`Mt32` merged transparency read; palette PNGs open in Indexed Color mode, #4804; earlier: CMYK Invert, Posterize and Threshold on the inks; 16-bit Lab Invert; Photoshop-oracle floor 145/258; `.atn` import maps 112 events; DDS textures open, #1327; DPX read and write, #1049; #2465 fixed: CS2 placed layers with only a `PlLd` block open their contents; Photoshop Actions `.atn` read and write; `.shc` contour files read and write; earlier the same day: indexed BMP palette mode; Save keeps a JPEG's quality and a WebP's encoding) · **Target:** Adobe Photoshop 2026 Every format Photoshop 2026 (27.11.0) reads or writes, with PhotoCraft's support, fidelity and tests. Photoshop's list comes from its `Info.plist` `CFBundleDocumentTypes`, the PiPL read/write @@ -142,6 +142,7 @@ remaining gaps in #1606 stay tracked with per-sample corpus ceilings. | Date | Change | Summary | |---|---|---| +| 2026-10-13 | minor | PSD: a "Saving Merged Transparency" global block (`Mtrn`, `Mt16`, `Mt32`; Photoshop's 32-bit documents keep a positive layer count) marks the merged image's first extra channel as its transparency: opening such a file no longer adds a "Transparency" alpha channel, and the oracle compares psd-tools transparentbg.psd's alpha (83.7 % → 0.9 % of pixels off; its shape edges remain) | | 2026-10-13 | minor | Palette PNGs open in Indexed Color mode (#4804) | | 2026-10-12 | minor | `.atn`: 112 events mapped (Select and Mask, Filter Gallery Spatter, Ripple, Crystallize, Pattern Fill layers, mask targeting, Step Backward, points, Bitmap) | | 2026-10-12 | minor | DDS textures open, read-only (#1327) | From 74812f4874e7c5dd21a78d18b6ea76b393aad61f Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Wed, 14 Oct 2026 07:59:38 +0200 Subject: [PATCH 37/39] IO: abr fit and gradient eval check their own bounds (#5218) fit() computed (y + 1) * h in u32, which passes u32::MAX for a plane over 2^32 / max rows; the ABR and pattern parsers' edge caps keep real inputs below that, but the guard lived in another crate. Spans are now computed in u64. gradient_bake::eval indexed stops[0] and stops[i + 1] relying on bake's two-stop check; it now handles an empty or lone stop itself. Same results for every input that worked before. Co-authored-by: Claude Opus 5.5 --- crates/io/src/abr_map.rs | 26 ++++++++++++++++++++++++-- crates/io/src/gradient_bake.rs | 21 +++++++++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/crates/io/src/abr_map.rs b/crates/io/src/abr_map.rs index c21800f13d..53cfd205ff 100644 --- a/crates/io/src/abr_map.rs +++ b/crates/io/src/abr_map.rs @@ -527,10 +527,17 @@ fn fit(w: u32, h: u32, v: Vec, max: u32) -> (u32, u32, Vec) { let k = (w.max(h) as f32 / max as f32).max(1.0); let (nw, nh) = (((w as f32 / k).round() as u32).clamp(1, max), ((h as f32 / k).round() as u32).clamp(1, max)); let mut out = vec![0.0f32; nw as usize * nh as usize]; + // Source rows/columns `[i * n / m, (i + 1) * n / m)` (at least one) for output index `i` of + // `m`; in u64, as `(i + 1) * n` passes u32 for a plane over 2^32 / `max` long. The parsers' + // edge caps keep today's inputs below that, but this function should not depend on them. + let span = |i: u32, n: u32, m: u32| { + let at = |k: u32| usize::try_from(u64::from(k) * u64::from(n) / u64::from(m)).unwrap_or(usize::MAX); + (at(i), at(i + 1).max(at(i).saturating_add(1)).min(n as usize)) + }; for y in 0..nh { - let (y0, y1) = ((y * h / nh) as usize, (((y + 1) * h / nh) as usize).max((y * h / nh) as usize + 1).min(h as usize)); + let (y0, y1) = span(y, h, nh); for x in 0..nw { - let (x0, x1) = ((x * w / nw) as usize, (((x + 1) * w / nw) as usize).max((x * w / nw) as usize + 1).min(w as usize)); + let (x0, x1) = span(x, w, nw); let (mut sum, mut n) = (0.0, 0usize); for yy in y0..y1 { for xx in x0..x1 { @@ -593,3 +600,18 @@ fn pattern_tile(p: &PsdPattern) -> Option { #[cfg(test)] mod tests; + +#[cfg(test)] +mod fit_tests { + use super::fit; + + /// A tall, narrow plane: `(y + 1) * h` passes `u32::MAX` once `h` is over 2^32 / `max` rows. + /// The ABR and pattern parsers cap edges well below that, but `fit` must not rely on it. + #[test] + fn fit_tall_planes_without_overflow() { + let h = 5_000_000u32; + let (w, nh, out) = fit(1, h, vec![1.0; h as usize], 1024); + assert_eq!((w, nh), (1, 1024)); + assert!(out.iter().all(|v| (*v - 1.0).abs() < 1e-6), "every output row averages real input rows"); + } +} diff --git a/crates/io/src/gradient_bake.rs b/crates/io/src/gradient_bake.rs index e452ae9e1f..0d34816a79 100644 --- a/crates/io/src/gradient_bake.rs +++ b/crates/io/src/gradient_bake.rs @@ -106,12 +106,15 @@ fn smooth_ease(u: f32) -> f32 { /// Colour at `t` for sorted stops (positions in 0..1) in `m`'s space. fn eval(stops: &[(f32, [f32; 3])], mids: &[f32], smooth: f32, t: f32, m: Method) -> [f32; 3] { let n = stops.len(); - if t <= stops[0].0 { - return stops[0].1; + let (Some(first), Some(last)) = (stops.first(), stops.last()) else { return [0.0; 3] }; + // A lone stop (even at a NaN position, which neither test below catches) is the colour. + if t <= first.0 || n < 2 { + return first.1; } - if t >= stops[n - 1].0 { - return stops[n - 1].1; + if t >= last.0 { + return last.1; } + // From here `stops[i + 1]` and the tangents' `n - 2` are in range: `i < n - 1` and `n >= 2`. let i = stops.windows(2).position(|w| t >= w[0].0 && t <= w[1].0).unwrap_or(0); let (t0, t1) = (stops[i].0, stops[i + 1].0); let h = (t1 - t0).max(1e-6); @@ -281,4 +284,14 @@ mod tests { let s = bake(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], &[0.25], 0.0, Method::Classic); assert!((at(&s, 0.25)[0] - 0.5).abs() < 0.01); } + + /// `eval` guards its own stops instead of relying on `bake`'s two-stop check. + #[test] + fn eval_with_fewer_than_two_stops_does_not_index_out_of_range() { + assert_eq!(eval(&[], &[], 1.0, 0.5, Method::Smooth), [0.0; 3]); + let lone = [(f32::NAN, [0.25, 0.5, 0.75])]; + for m in [Method::Classic, Method::Smooth] { + assert_eq!(eval(&lone, &[], 1.0, 0.5, m), [0.25, 0.5, 0.75]); + } + } } From 706638b2842a330cfc5c305c7860d0b7c9921960 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Wed, 14 Oct 2026 07:59:43 +0200 Subject: [PATCH 38/39] Tests: bad-layer-id checks use an id that can't exist (#3343) (#3351) * Tests: bad-layer-id checks use an id that can't exist (#3343) Layer ids come from one process-wide counter (shared with document and smart-object ids, and bumped by ensure_ids_above on file loads). A full `cargo test` run passes ids like 9999, so a layer a test just created can get the "bad" id, the command succeeds and the assertion fails at random (seen on CI in type_extra_cmds::tests::vertical_orientation_renders_columns_inside_the_canvas). Use u64::MAX, which the counter never reaches, for every "no such layer" sentinel in the engine and ui-egui tests. Test-only change. Co-Authored-By: Claude Opus 5.5 * Tests: use u64::MAX for bad-layer-id sentinels added on main since (#3343) Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/align_cmds.rs | 2 +- crates/engine/src/automate_cmds/tests.rs | 4 ++-- crates/engine/src/background_cmds.rs | 2 +- crates/engine/src/cutout_cmds.rs | 2 +- crates/engine/src/extra_cmds.rs | 4 ++-- crates/engine/src/fill_key_cmds.rs | 2 +- crates/engine/src/fx_view_cmds.rs | 2 +- crates/engine/src/fx_visibility_cmds.rs | 2 +- crates/engine/src/gradient_fill_cmds/tests.rs | 2 +- crates/engine/src/group_view_cmds.rs | 2 +- crates/engine/src/layer_copy_cmds/tests.rs | 4 ++-- crates/engine/src/layer_menu_cmds/tests.rs | 2 +- crates/engine/src/layer_multi_cmds.rs | 8 ++++---- crates/engine/src/layer_style.rs | 2 +- crates/engine/src/mask_view_cmds/tests.rs | 2 +- crates/engine/src/pattern_fill_cmds.rs | 2 +- crates/engine/src/rasterize_style_cmds/tests.rs | 2 +- crates/engine/src/retouch_cmds/tests.rs | 6 +++--- crates/engine/src/smart_cmds/tests.rs | 2 +- crates/engine/src/smartselect_cmds.rs | 2 +- crates/engine/src/tests.rs | 6 +++--- crates/engine/src/type_caret_cmds.rs | 2 +- crates/engine/src/type_cmds.rs | 2 +- crates/engine/src/type_extra_cmds/tests.rs | 7 ++++--- crates/engine/src/variables_cmds/tests.rs | 2 +- crates/engine/src/vector_cmds/tests.rs | 2 +- crates/engine/tests/eraser_locked.rs | 2 +- crates/engine/tests/explicit_layer_target.rs | 2 +- crates/engine/tests/rasterize_selected_layers.rs | 2 +- crates/engine/tests/selection_layer_commands.rs | 2 +- crates/ui-egui/src/layer_style/color_picker/tests.rs | 2 +- crates/ui-egui/src/mask_thumbs_ui/tests.rs | 2 +- crates/ui-egui/src/rasterize_prompt_tests.rs | 2 +- 33 files changed, 46 insertions(+), 45 deletions(-) diff --git a/crates/engine/src/align_cmds.rs b/crates/engine/src/align_cmds.rs index b54f4b7a73..c7df82902f 100644 --- a/crates/engine/src/align_cmds.rs +++ b/crates/engine/src/align_cmds.rs @@ -381,7 +381,7 @@ mod tests { let m = r["aligned"][0]["matrix"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect::>(); assert!((m[2] + 6.0).abs() < 1.0 && (m[5] + 5.0).abs() < 1.0, "{r}"); assert!(s.execute("edit.autoAlignLayers", json!({"projection": "sideways"})).is_err()); - assert!(s.execute("edit.autoAlignLayers", json!({"reference": 9999})).is_err()); + assert!(s.execute("edit.autoAlignLayers", json!({"reference": u64::MAX})).is_err()); } #[test] diff --git a/crates/engine/src/automate_cmds/tests.rs b/crates/engine/src/automate_cmds/tests.rs index b60dc2940e..67852534c3 100644 --- a/crates/engine/src/automate_cmds/tests.rs +++ b/crates/engine/src/automate_cmds/tests.rs @@ -50,7 +50,7 @@ fn browse_runs_a_script_file() { assert_eq!((d.size.width, d.size.height), (20, 30)); assert!(s.execute("file.scripts.browse", json!({"script": "no.such.command"})).is_err()); // A failing step stops the script and is reported. - let r = s.execute("file.scripts.browse", json!({"steps": [["layer.delete", {"layer": 999_999}], ["image.imageRotation.90cw", {}]]})).unwrap(); + let r = s.execute("file.scripts.browse", json!({"steps": [["layer.delete", {"layer": u64::MAX}], ["image.imageRotation.90cw", {}]]})).unwrap(); assert_eq!(r["results"].as_array().unwrap().len(), 1); } @@ -348,7 +348,7 @@ fn a_failed_step_inside_a_called_action_fails_scripts_and_batch() { }); s.actions.list.push(Action { name: "Bad".into(), - steps: vec![("layer.delete".into(), json!({"layer": 999_999}))], + steps: vec![("layer.delete".into(), json!({"layer": u64::MAX}))], disabled: Default::default(), ..Default::default() }); diff --git a/crates/engine/src/background_cmds.rs b/crates/engine/src/background_cmds.rs index e733b54809..0c067b3842 100644 --- a/crates/engine/src/background_cmds.rs +++ b/crates/engine/src/background_cmds.rs @@ -233,7 +233,7 @@ mod tests { assert!(s.execute(CMD, json!({"layer": bg.0})).is_err()); // Bad params. let (mut s, [_, a, _]) = session(8); - for p in [json!({"layer": "A"}), json!({"layer": -1}), json!({"layer": 1.5}), json!({"layer": 99999}), json!({"layer": [a.0]})] { + for p in [json!({"layer": "A"}), json!({"layer": -1}), json!({"layer": 1.5}), json!({"layer": u64::MAX}), json!({"layer": [a.0]})] { assert!(s.execute(CMD, p.clone()).is_err(), "{p}"); } assert!(doc(&s).background().is_none()); diff --git a/crates/engine/src/cutout_cmds.rs b/crates/engine/src/cutout_cmds.rs index d5cc734668..2f6e2b70b5 100644 --- a/crates/engine/src/cutout_cmds.rs +++ b/crates/engine/src/cutout_cmds.rs @@ -254,7 +254,7 @@ mod tests { let (mut s, _) = disc(8); let before = s.active().unwrap().revision; - assert!(s.execute(CMD, json!({"layer": 999_999})).is_err()); + assert!(s.execute(CMD, json!({"layer": u64::MAX})).is_err()); // A `layer` string names a layer; one that isn't there is an error, not the active layer. assert!(s.execute(CMD, json!({"layer": "x"})).is_err()); assert_eq!(s.active().unwrap().revision, before); diff --git a/crates/engine/src/extra_cmds.rs b/crates/engine/src/extra_cmds.rs index 1f3748d7b9..3a27c99be3 100644 --- a/crates/engine/src/extra_cmds.rs +++ b/crates/engine/src/extra_cmds.rs @@ -1549,7 +1549,7 @@ mod tests { // Layer 1 is already the only visible layer: ⌥-clicking it shows everything. assert_eq!(s.execute("layer.showOnly", json!({})).unwrap()["shownAlone"], false); assert!(doc(&s).layers.iter().all(|l| l.visible)); - assert!(s.execute("layer.showOnly", json!({"layer": 9999})).is_err()); + assert!(s.execute("layer.showOnly", json!({"layer": u64::MAX})).is_err()); assert!(Session::new().execute("layer.showOnly", json!({})).is_err()); } @@ -1624,7 +1624,7 @@ mod tests { let bg_locks = doc(&s).layer(bg).unwrap().locks; assert!(s.execute("layer.toggleLockTransparency", json!({"layer": bg.0})).is_err()); assert_eq!(doc(&s).layer(bg).unwrap().locks, bg_locks, "the Background is left alone"); - assert!(s.execute("layer.toggleLockTransparency", json!({"layer": 999_999})).is_err()); + assert!(s.execute("layer.toggleLockTransparency", json!({"layer": u64::MAX})).is_err()); } #[test] diff --git a/crates/engine/src/fill_key_cmds.rs b/crates/engine/src/fill_key_cmds.rs index 6b94e6cd47..ce3b478a70 100644 --- a/crates/engine/src/fill_key_cmds.rs +++ b/crates/engine/src/fill_key_cmds.rs @@ -264,7 +264,7 @@ mod tests { for id in IDS { assert!(s.execute(id, json!([1, 2])).is_err(), "{id}"); assert!(s.execute(id, json!("x")).is_err(), "{id}"); - assert!(s.execute(id, json!({"layer": 99_999})).is_err(), "{id}"); + assert!(s.execute(id, json!({"layer": u64::MAX})).is_err(), "{id}"); assert!(s.execute(id, json!({"target": {"channel": 42}})).is_err(), "{id}"); } } diff --git a/crates/engine/src/fx_view_cmds.rs b/crates/engine/src/fx_view_cmds.rs index 03241d8405..3598ea56b0 100644 --- a/crates/engine/src/fx_view_cmds.rs +++ b/crates/engine/src/fx_view_cmds.rs @@ -118,7 +118,7 @@ mod tests { let bg = s.active().unwrap().doc.layers[0].id.0; for p in [ json!({"layer": bg}), - json!({"layer": 9999}), + json!({"layer": u64::MAX}), json!({"layer": "x"}), json!({"layer": -1}), json!({"expanded": "yes", "all": true}), diff --git a/crates/engine/src/fx_visibility_cmds.rs b/crates/engine/src/fx_visibility_cmds.rs index bc24d84b18..7911bcd3cc 100644 --- a/crates/engine/src/fx_visibility_cmds.rs +++ b/crates/engine/src/fx_visibility_cmds.rs @@ -161,7 +161,7 @@ mod tests { let before = state(&s, a); for p in [ json!({"layer": bg}), - json!({"layer": 9999}), + json!({"layer": u64::MAX}), json!({"layer": "x"}), json!({"layer": -1}), json!({"layer": a, "index": 2}), diff --git a/crates/engine/src/gradient_fill_cmds/tests.rs b/crates/engine/src/gradient_fill_cmds/tests.rs index bb357df050..efcdb494d9 100644 --- a/crates/engine/src/gradient_fill_cmds/tests.rs +++ b/crates/engine/src/gradient_fill_cmds/tests.rs @@ -273,7 +273,7 @@ fn commands_fail_gracefully() { // A zero-length drag still makes a valid gradient. s.execute(CREATE, json!({"from": [3, 3], "to": [3, 3]})).unwrap(); for p in [ - json!({"layer": 999_999}), + json!({"layer": u64::MAX}), json!({"from": [0]}), json!({"to": [1e30, 0]}), json!({"angle": "x"}), diff --git a/crates/engine/src/group_view_cmds.rs b/crates/engine/src/group_view_cmds.rs index 32a22f5e39..b95acff6a7 100644 --- a/crates/engine/src/group_view_cmds.rs +++ b/crates/engine/src/group_view_cmds.rs @@ -142,7 +142,7 @@ mod tests { let bg = s.active().unwrap().doc.layers[0].id.0; for p in [ json!({"layer": bg}), - json!({"layer": 9999}), + json!({"layer": u64::MAX}), json!({"layer": "x"}), json!({"layer": -1}), json!({"expanded": "yes", "all": true}), diff --git a/crates/engine/src/layer_copy_cmds/tests.rs b/crates/engine/src/layer_copy_cmds/tests.rs index a81ebbdc47..93bd7b94ef 100644 --- a/crates/engine/src/layer_copy_cmds/tests.rs +++ b/crates/engine/src/layer_copy_cmds/tests.rs @@ -130,7 +130,7 @@ fn bad_params_and_states_fail_without_changing_anything() { json!({"document": -1}), json!({"document": "1"}), json!({"document": 1, "source": 9}), - json!({"document": 1, "layers": [999_999]}), + json!({"document": 1, "layers": [u64::MAX]}), json!({"document": 1, "layers": "all"}), json!({"document": 1, "layers": []}), json!({"document": 1, "at": [1.0]}), @@ -314,7 +314,7 @@ fn duplicate_destinations_that_are_not_new_fail_cleanly() { json!({"document": []}), json!({"document": {}}), json!({"document": "other"}), - json!({"document": "new", "layer": 999_999}), + json!({"document": "new", "layer": u64::MAX}), ] { let before = (doc(&s, 0).clone(), s.documents().len(), s.active_index()); let err = s.execute("layer.duplicate", p.clone()).unwrap_err().to_string(); diff --git a/crates/engine/src/layer_menu_cmds/tests.rs b/crates/engine/src/layer_menu_cmds/tests.rs index 708bee5fce..46688500b1 100644 --- a/crates/engine/src/layer_menu_cmds/tests.rs +++ b/crates/engine/src/layer_menu_cmds/tests.rs @@ -588,7 +588,7 @@ fn transfer_effects_moves_or_copies_between_layers() { for bad in [ json!({"from": a.0, "to": a.0}), json!({"from": a.0}), - json!({"from": a.0, "to": 9999}), + json!({"from": a.0, "to": u64::MAX}), json!({"from": a.0, "to": b.0, "effect": 7}), json!({"from": a.0, "to": b.0, "effect": -1}), json!({"from": b.0, "to": a.0}), diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs index 21e9e2caac..80da3bdd81 100644 --- a/crates/engine/src/layer_multi_cmds.rs +++ b/crates/engine/src/layer_multi_cmds.rs @@ -1625,8 +1625,8 @@ mod tests { let all = doc(&s).bounds(); assert!(damage(&s).is_none_or(|r| r.contains_rect(&all)), "{:?}", damage(&s)); // Bad params still fail cleanly. - assert!(s.execute("layer.setProps", json!({"layer": 999_999, "visible": true})).is_err()); - assert!(s.execute("layer.translate", json!({"layer": 999_999, "dx": 1})).is_err()); + assert!(s.execute("layer.setProps", json!({"layer": u64::MAX, "visible": true})).is_err()); + assert!(s.execute("layer.translate", json!({"layer": u64::MAX, "dx": 1})).is_err()); } /// #1771: a new empty layer or group changes no pixels, so it (and undoing or redoing it) @@ -1805,7 +1805,7 @@ mod tests { let mut locked = (*before).clone(); locked.layer_mut(a).unwrap().locks.position = true; assert!(moved(&locked, &ids, 1, 1).is_err()); - assert!(moved(&before, &[LayerId(999_999)], 1, 1).is_err()); + assert!(moved(&before, &[LayerId(u64::MAX)], 1, 1).is_err()); } #[test] @@ -1894,7 +1894,7 @@ mod tests { assert_eq!(s.active().unwrap().doc.layer(a).unwrap().name, "x"); s.execute("layer.showLayers", json!({"layer": a.0})).unwrap(); assert!(s.active().unwrap().doc.layer(a).unwrap().visible); - assert!(s.execute("layer.hideLayers", json!({"layer": 999_999})).is_err()); + assert!(s.execute("layer.hideLayers", json!({"layer": u64::MAX})).is_err()); assert!(s.execute("layer.hideLayers", json!({})).is_err()); assert!(!s.is_enabled("layer.align.topEdges")); } diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rs index fbbc949e20..5f7b8f0eee 100644 --- a/crates/engine/src/layer_style.rs +++ b/crates/engine/src/layer_style.rs @@ -1206,7 +1206,7 @@ mod tests { let layer = s.active().unwrap().active_layer.unwrap().0; s.execute("layer.layerStyle.clear", json!({"layer": layer, "effect": 0})).unwrap(); assert!(matches!(effects(&s)[..], [Effect::ColorOverlay { .. }])); - for bad in [json!({"effect": 5}), json!({"effect": -1}), json!({"effect": "x"}), json!({"effect": 0, "layer": 999_999})] { + for bad in [json!({"effect": 5}), json!({"effect": -1}), json!({"effect": "x"}), json!({"effect": 0, "layer": u64::MAX})] { assert!(s.execute("layer.layerStyle.clear", bad).is_err()); } assert_eq!(effects(&s).len(), 1, "bad params change nothing"); diff --git a/crates/engine/src/mask_view_cmds/tests.rs b/crates/engine/src/mask_view_cmds/tests.rs index c73ca0d0ab..ce9bffa744 100644 --- a/crates/engine/src/mask_view_cmds/tests.rs +++ b/crates/engine/src/mask_view_cmds/tests.rs @@ -95,7 +95,7 @@ fn bad_params_and_no_mask_fail_gracefully() { for p in [ json!({"layer": bg, "mode": "gray"}), json!({"layer": bg}), - json!({"layer": 999_999, "mode": "gray"}), + json!({"layer": u64::MAX, "mode": "gray"}), json!({"layer": "x"}), json!({"layer": -1}), json!({"mode": 3}), diff --git a/crates/engine/src/pattern_fill_cmds.rs b/crates/engine/src/pattern_fill_cmds.rs index daa7e9660f..b9d75e6cb5 100644 --- a/crates/engine/src/pattern_fill_cmds.rs +++ b/crates/engine/src/pattern_fill_cmds.rs @@ -225,7 +225,7 @@ mod tests { json!([1, 2]), json!({"layer": -1}), json!({"layer": "x"}), - json!({"layer": 999_999}), + json!({"layer": u64::MAX}), json!({"document": 999_999}), json!({"pattern": "nope"}), json!({"pattern": ""}), diff --git a/crates/engine/src/rasterize_style_cmds/tests.rs b/crates/engine/src/rasterize_style_cmds/tests.rs index 57db9adbae..b4391cb6f2 100644 --- a/crates/engine/src/rasterize_style_cmds/tests.rs +++ b/crates/engine/src/rasterize_style_cmds/tests.rs @@ -152,7 +152,7 @@ fn disabled_and_bad_params_fail_gracefully() { let (mut s, id) = scene(8); let bg = doc(&s).layers[0].id.0; assert!(s.execute(ID, json!({"layer": bg})).is_err(), "the Background has no effects"); - assert!(s.execute(ID, json!({"layer": 9999})).is_err(), "no such layer"); + assert!(s.execute(ID, json!({"layer": u64::MAX})).is_err(), "no such layer"); assert!(s.execute(ID, json!({"layer": u64::MAX})).is_err(), "no such layer"); assert_eq!(layer(&s, id).effects.items.len(), 5, "a bad call changes nothing"); s.execute("layer.layerStyle.hideAllEffects", json!({})).unwrap(); diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs index 9a0ae22f08..e20706857a 100644 --- a/crates/engine/src/retouch_cmds/tests.rs +++ b/crates/engine/src/retouch_cmds/tests.rs @@ -993,7 +993,7 @@ fn patch_fails_gracefully() { json!({"offset": [0, -30]}), json!({"offset": [10, 0], "target": "mask"}), json!({"offset": [10, 0], "target": {"channel": 7}}), - json!({"offset": [10, 0], "layer": 999}), + json!({"offset": [10, 0], "layer": u64::MAX}), ] { assert!(s.execute("paint.patch", p.clone()).is_err(), "{p}"); } @@ -1269,7 +1269,7 @@ fn content_aware_move_fails_gracefully() { json!({"offset": [0, -30]}), json!({"offset": [10, 0], "target": "mask"}), json!({"offset": [10, 0], "target": {"channel": 7}}), - json!({"offset": [10, 0], "layer": 999}), + json!({"offset": [10, 0], "layer": u64::MAX}), json!({"offset": [10, 0], "scaleX": 0}), json!({"offset": [10, 0], "scaleY": 0.5}), json!({"offset": [10, 0], "scaleX": 1e9}), @@ -1727,7 +1727,7 @@ fn remove_bad_params_are_errors() { json!({"points": []}), json!({"points": "x"}), json!({"points": [[1, 1]], "size": "big", "closeLoops": "yes", "sampleAllLayers": 3}), - json!({"points": [[1, 1]], "layer": 999_999}), + json!({"points": [[1, 1]], "layer": u64::MAX}), ] { let _ = s.execute("paint.remove", p); } diff --git a/crates/engine/src/smart_cmds/tests.rs b/crates/engine/src/smart_cmds/tests.rs index a764712c9f..1caaff6ce2 100644 --- a/crates/engine/src/smart_cmds/tests.rs +++ b/crates/engine/src/smart_cmds/tests.rs @@ -243,7 +243,7 @@ fn smart_filter_commands_check_an_explicit_layer_target() { assert_eq!(active(&s), curves, "the active layer stays"); // A target that isn't a smart object with filters is still refused, and the active layer stays. assert!(matches!(s.execute("layer.smartFilter.setParams", json!({"layer": curves, "params": {}})), Err(EngineError::Disabled(..)))); - assert!(s.execute("layer.smartFilter.setParams", json!({"layer": 999_999, "params": {}})).is_err()); + assert!(s.execute("layer.smartFilter.setParams", json!({"layer": u64::MAX, "params": {}})).is_err()); assert_eq!(active(&s), curves); } diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs index b535fd1525..380b6a7200 100644 --- a/crates/engine/src/smartselect_cmds.rs +++ b/crates/engine/src/smartselect_cmds.rs @@ -720,7 +720,7 @@ mod tests { // Any point on the object hits; the background and another target miss. assert!(std::sync::Arc::ptr_eq(&s.object_finder.lookup(&doc, target, 120.0, 90.0).unwrap(), &r)); assert!(s.object_finder.lookup(&doc, target, 3.0, 3.0).is_none()); - assert!(s.object_finder.lookup(&doc, Some(LayerId(9999)), 100.0, 80.0).is_none()); + assert!(s.object_finder.lookup(&doc, Some(LayerId(u64::MAX)), 100.0, 80.0).is_none()); // The click reuses it, and a selection-only edit keeps it. s.execute("select.object", json!({"point": [110, 70]})).unwrap(); assert_eq!(s.object_finder.len(), 1); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 08e17bd39c..83458f05a6 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -553,7 +553,7 @@ fn clipping_masks_apply_to_every_selected_layer() { // A `layer` param still acts on that layer alone. s.execute("layer.releaseClippingMask", json!({"layer": ids[2]})).unwrap(); assert_eq!(clipped(&s), [false, true, false]); - assert!(s.execute("layer.createClippingMask", json!({"layer": 999_999})).is_err()); + assert!(s.execute("layer.createClippingMask", json!({"layer": u64::MAX})).is_err()); } #[test] @@ -992,8 +992,8 @@ fn move_to_copy_rejects_bad_params() { for p in [ json!({"layer": a, "copy": true}), json!({"layer": a, "target": "x", "copy": true}), - json!({"layer": a, "target": 9_999_999, "copy": true}), - json!({"layer": 9_999_999, "target": a, "copy": true}), + json!({"layer": a, "target": u64::MAX, "copy": true}), + json!({"layer": u64::MAX, "target": a, "copy": true}), json!({"layer": a, "target": a, "position": "into", "copy": true}), ] { assert!(s.execute("layer.moveTo", p.clone()).is_err(), "{p}"); diff --git a/crates/engine/src/type_caret_cmds.rs b/crates/engine/src/type_caret_cmds.rs index fbc9322c62..419d305ee6 100644 --- a/crates/engine/src/type_caret_cmds.rs +++ b/crates/engine/src/type_caret_cmds.rs @@ -482,7 +482,7 @@ mod tests { bad(&mut s, "type.navigate", json!({"layer": id, "index": 9, "move": "end"})); bad(&mut s, "type.navigate", json!({"layer": id, "index": 0, "move": "lineNext", "x": "no"})); bad(&mut s, "type.navigate", json!({"layer": pixel, "index": 0, "move": "start"})); - assert!(matches!(s.execute("type.caret", json!({"layer": 9_999_999, "index": 0})), Err(EngineError::NoLayer(_)))); + assert!(matches!(s.execute("type.caret", json!({"layer": u64::MAX, "index": 0})), Err(EngineError::NoLayer(_)))); // A point on the named layer but outside its text still returns a caret, marked outside. let far = s.execute("type.hitTest", json!({"layer": id, "x": 390, "y": 290})).unwrap(); assert_eq!(far["inside"], false); diff --git a/crates/engine/src/type_cmds.rs b/crates/engine/src/type_cmds.rs index 4bf193b679..d6b92e840c 100644 --- a/crates/engine/src/type_cmds.rs +++ b/crates/engine/src/type_cmds.rs @@ -1184,7 +1184,7 @@ mod tests { st.saved_revision = st.revision; } let rev = s.active().unwrap().revision; - let unknown = [(doc_id, LayerId(9999)), (photocraft_doc::DocId(u64::MAX), id)]; + let unknown = [(doc_id, LayerId(u64::MAX)), (photocraft_doc::DocId(u64::MAX), id)]; assert!(s.refresh_type_layers(&[&[(doc_id, id)][..], &unknown].concat()).is_empty()); assert!(text_layer(&s, id.0).cache.is_some()); let st = s.active().unwrap(); diff --git a/crates/engine/src/type_extra_cmds/tests.rs b/crates/engine/src/type_extra_cmds/tests.rs index 9f604eed1b..7386927bb7 100644 --- a/crates/engine/src/type_extra_cmds/tests.rs +++ b/crates/engine/src/type_extra_cmds/tests.rs @@ -108,8 +108,9 @@ fn vertical_orientation_renders_columns_inside_the_canvas() { assert!(l.vertical && l.lines.len() == 2); let [x0, y0, x1, y1] = l.bounds().unwrap(); assert!(x1 > 0.0 && x0 < -40.0 && y0 >= -1e-3 && y1 > 60.0, "{:?}", l.bounds()); - // Bad layer ids fail cleanly. - assert!(s.execute("type.orientation.vertical", json!({"layer": 9999})).is_err()); + // Bad layer ids fail cleanly. Not a small number like 9999: ids come from one process-wide + // counter, so in a full test run a layer made above can get it (#3343). + assert!(s.execute("type.orientation.vertical", json!({"layer": u64::MAX})).is_err()); } #[test] @@ -299,7 +300,7 @@ fn type_attaches_to_a_path_and_releases_it() { .unwrap(); assert!(s.execute("type.attachToPath", json!({"subpath": 5})).is_err()); assert!(s.execute("type.attachToPath", json!({"start": "far"})).is_err()); - assert!(s.execute("type.attachToPath", json!({"shape": 9999})).is_err()); + assert!(s.execute("type.attachToPath", json!({"shape": u64::MAX})).is_err()); s.execute("type.attachToPath", json!({"start": 4})).unwrap(); assert!(matches!(text(&s, id).shape, TextShape::Path { start, .. } if start == 4.0)); let on_path = ink(&s, id); diff --git a/crates/engine/src/variables_cmds/tests.rs b/crates/engine/src/variables_cmds/tests.rs index 28fc14a36f..c2a30cab68 100644 --- a/crates/engine/src/variables_cmds/tests.rs +++ b/crates/engine/src/variables_cmds/tests.rs @@ -105,7 +105,7 @@ fn listed_pixel_methods_can_be_used_to_redefine_variables() { #[test] fn bad_params_are_errors() { let (mut s, _p, badge, _t) = session(); - assert!(s.execute("image.variables.define", json!({"defs": [{"name": "x", "layer": 999999, "type": "visibility"}]})).is_err()); + assert!(s.execute("image.variables.define", json!({"defs": [{"name": "x", "layer": u64::MAX, "type": "visibility"}]})).is_err()); assert!(s.execute("image.variables.define", json!({"defs": [{"name": "x", "layer": badge.0, "type": "nope"}]})).is_err()); assert!(s.execute("image.applyDataSet", json!({"name": "missing"})).is_err()); } diff --git a/crates/engine/src/vector_cmds/tests.rs b/crates/engine/src/vector_cmds/tests.rs index c11de7c813..7e1c871dff 100644 --- a/crates/engine/src/vector_cmds/tests.rs +++ b/crates/engine/src/vector_cmds/tests.rs @@ -924,7 +924,7 @@ fn combine_shapes_fails_gracefully() { let (mut s, a, _) = two_selected_rects(8); let before = doc(&s).clone(); let history = s.active().unwrap().history.past_len(); - let bad_params = [json!({"layer": 999_999}), json!({"layer": a, "subpath": 99}), json!({"subpath": 99}), json!({"tolerance": "x", "layer": 999_999})]; + let bad_params = [json!({"layer": u64::MAX}), json!({"layer": a, "subpath": 99}), json!({"subpath": 99}), json!({"tolerance": "x", "layer": u64::MAX})]; for cmd in [ "layer.combineShapes.unite", "layer.combineShapes.subtractFrontShape", diff --git a/crates/engine/tests/eraser_locked.rs b/crates/engine/tests/eraser_locked.rs index ae160ca277..d7405a7311 100644 --- a/crates/engine/tests/eraser_locked.rs +++ b/crates/engine/tests/eraser_locked.rs @@ -122,7 +122,7 @@ fn eraser_bad_params_fail_gracefully() { assert!(s.execute("paint.stroke", json!({"erase": true})).is_err()); assert!(s.execute("paint.stroke", json!({"erase": true, "points": []})).is_err()); assert!(s.execute("paint.stroke", json!({"erase": true, "points": "x"})).is_err()); - assert!(erase(&mut s, json!({"layer": 99999})).is_err()); + assert!(erase(&mut s, json!({"layer": u64::MAX})).is_err()); assert!(erase(&mut s, json!({"target": "mask"})).is_err(), "no mask"); // Absurd coordinates are refused up front (a stroke that long would run billions of dabs). assert!(erase(&mut s, json!({"points": [[0, 0], [f64::MAX, 0.0]]})).is_err()); diff --git a/crates/engine/tests/explicit_layer_target.rs b/crates/engine/tests/explicit_layer_target.rs index 6479b9c6d7..6d968c410a 100644 --- a/crates/engine/tests/explicit_layer_target.rs +++ b/crates/engine/tests/explicit_layer_target.rs @@ -67,7 +67,7 @@ fn a_named_layer_of_the_wrong_kind_or_that_does_not_exist_is_refused() { let r = s.execute("layer.smartObjects.rasterize", json!({"layer": pixels})); assert!(matches!(r, Err(EngineError::Disabled(..))), "{r:?}"); // A layer that doesn't exist falls back to the active layer's check, and still fails. - assert!(s.execute("layer.smartObjects.rasterize", json!({"layer": 999_999})).is_err()); + assert!(s.execute("layer.smartObjects.rasterize", json!({"layer": u64::MAX})).is_err()); assert!(s.execute("layer.smartObjects.rasterize", json!({"layer": "nope"})).is_err()); assert_eq!(s.active().unwrap().doc, before); assert_eq!(active(&s), pixels); diff --git a/crates/engine/tests/rasterize_selected_layers.rs b/crates/engine/tests/rasterize_selected_layers.rs index 40b962aca8..a738b51681 100644 --- a/crates/engine/tests/rasterize_selected_layers.rs +++ b/crates/engine/tests/rasterize_selected_layers.rs @@ -140,6 +140,6 @@ fn bad_targets_fail_gracefully() { } let mut sc = scene(); for id in ["layer.rasterize.layer", "layer.rasterize.shape", "layer.rasterize.smartObject"] { - assert!(sc.s.execute(id, json!({"layer": 9_999_999})).is_err(), "{id}: no such layer"); + assert!(sc.s.execute(id, json!({"layer": u64::MAX})).is_err(), "{id}: no such layer"); } } diff --git a/crates/engine/tests/selection_layer_commands.rs b/crates/engine/tests/selection_layer_commands.rs index 8d144d44de..44f63cf5d2 100644 --- a/crates/engine/tests/selection_layer_commands.rs +++ b/crates/engine/tests/selection_layer_commands.rs @@ -148,6 +148,6 @@ fn bad_targets_fail_gracefully() { } let mut sc = scene(); for id in ["layer.layerMask.revealAll", "layer.layerStyle.pasteLayerStyle", "layer.layerStyle.clear"] { - assert!(sc.s.execute(id, json!({"layer": 9_999_999})).is_err(), "{id}: no such layer"); + assert!(sc.s.execute(id, json!({"layer": u64::MAX})).is_err(), "{id}: no such layer"); } } diff --git a/crates/ui-egui/src/layer_style/color_picker/tests.rs b/crates/ui-egui/src/layer_style/color_picker/tests.rs index 9bfe484573..2d87c81ef8 100644 --- a/crates/ui-egui/src/layer_style/color_picker/tests.rs +++ b/crates/ui-egui/src/layer_style/color_picker/tests.rs @@ -163,7 +163,7 @@ fn stale_targets_and_invalid_colors_never_apply_or_leave_preview() { app.ui.dialog_mut(picker).unwrap().fields.insert(TARGET.into(), json!(null)); } _ => { - app.ui.dialog_mut(parent).unwrap().fields.insert("layer".into(), json!(999999)); + app.ui.dialog_mut(parent).unwrap().fields.insert("layer".into(), json!(u64::MAX)); } } let current_parent = app.ui.dialogs.iter().find(|d| d.id == parent).cloned(); diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rs index eda8224fa0..163c7cbae8 100644 --- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs +++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs @@ -594,7 +594,7 @@ fn mask_view_gestures_fail_gracefully_without_a_mask() { h.run_steps(2); // A stale view (layer gone) draws nothing instead of panicking. h.state_mut().session.active_mut().unwrap().channel_view.layer_mask = Some(photocraft_engine::mask_view_cmds::LayerMaskView { - layer: photocraft_doc::LayerId(987_654), + layer: photocraft_doc::LayerId(u64::MAX), mode: photocraft_engine::mask_view_cmds::MaskViewMode::Gray, }); assert!(canvas_px(&h).is_none()); diff --git a/crates/ui-egui/src/rasterize_prompt_tests.rs b/crates/ui-egui/src/rasterize_prompt_tests.rs index 510723beb6..9e6a10583c 100644 --- a/crates/ui-egui/src/rasterize_prompt_tests.rs +++ b/crates/ui-egui/src/rasterize_prompt_tests.rs @@ -137,7 +137,7 @@ fn a_stale_or_bad_prompt_fails_gracefully() { app.run("layer.rasterize.type", json!({})).unwrap(); assert!(crate::dialogs::confirm(&mut app, id).is_err()); // Garbage fields. - for f in [json!({"__rasterize": "plaid", "layer": 1}), json!({"__rasterize": "type"}), json!({"__rasterize": "type", "layer": 999_999})] { + for f in [json!({"__rasterize": "plaid", "layer": 1}), json!({"__rasterize": "type"}), json!({"__rasterize": "type", "layer": u64::MAX})] { let id = app.ui.open_dialog(crate::state::DialogKind::Command, f.as_object().unwrap().clone()); assert!(crate::dialogs::confirm(&mut app, id).is_err(), "{f}"); } From ed7df56a2455375240959bae6e5273a71d858d84 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Wed, 14 Oct 2026 10:02:34 +0200 Subject: [PATCH 39/39] fix(format): recover autosaves with missing sidecars Discover committed autosave bundles whose JSON sidecar was never published. Rebuild recovery metadata from the readable manifest and retain these bundles during orphan cleanup. Fixes #4967 --- crates/format/src/autosave.rs | 66 +++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/crates/format/src/autosave.rs b/crates/format/src/autosave.rs index 4767413617..06150e60ed 100644 --- a/crates/format/src/autosave.rs +++ b/crates/format/src/autosave.rs @@ -199,12 +199,33 @@ pub fn list_recovery(recovery_dir: &Path) -> Vec { if bundle.join(crate::store::MANIFEST).is_file() { out.push(RecoveryEntry { info, bundle }); } + } else if e.file_type().is_ok_and(|ft| ft.is_dir()) + && let Some(entry) = sidecarless_entry(recovery_dir, &p) + { + out.push(entry); } } out.sort_by(|a, b| b.info.saved_at.cmp(&a.info.saved_at).then(a.info.key.cmp(&b.info.key))); out } +/// A committed `.pcraft` bundle (its manifest is written last) whose sidecar was never +/// published, e.g. a crash during a document's first autosave. The metadata is rebuilt from the +/// manifest (read like a load reads it) and its modification time. Incomplete bundles (no +/// readable manifest) and bundles with anything at the sidecar's path are not entries. +fn sidecarless_entry(recovery_dir: &Path, bundle: &Path) -> Option { + let key = bundle.file_name()?.to_str()?.strip_suffix(".pcraft")?; + if key.is_empty() || sanitize(key) != key || std::fs::symlink_metadata(recovery_dir.join(format!("{key}.json"))).is_ok() { + return None; + } + let meta = std::fs::symlink_metadata(bundle.join(crate::store::MANIFEST)).ok().filter(|m| m.is_file())?; + let source = crate::store::DirSource { root: bundle.to_path_buf() }; + let manifest = crate::store::read_manifest(&source, &crate::LoadOptions::default()).ok()?; + let saved_at = meta.modified().ok().and_then(|t| t.duration_since(UNIX_EPOCH).ok()).map_or(0, |d| d.as_secs()); + let info = RecoveryInfo { key: key.to_string(), document_name: manifest.document.name, original_path: None, saved_at, revision: 0 }; + Some(RecoveryEntry { info, bundle: bundle.to_path_buf() }) +} + /// Load a recovered document. pub fn recover(entry: &RecoveryEntry) -> Result { crate::load_path(&entry.bundle) @@ -292,8 +313,9 @@ impl RecoveryStore { const ORPHAN_AGE: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60); /// Best-effort removal of recovery data nothing can ever load or clean up: -/// - `.pcraft` bundle folders with no `.json` sidecar (a crash during a document's -/// first autosave: the sidecar is written after the bundle, and `list_recovery` skips them); +/// - `.pcraft` bundle folders with no `.json` sidecar and no readable manifest (a +/// crash in the middle of a document's first autosave). A sidecar-less bundle whose manifest +/// was committed is complete and listed by `list_recovery`, so it is kept; /// - `*.pcsave.tmp` files left by an interrupted atomic write (see [`crate::atomic::temp_name`]) /// that another process created. /// @@ -321,7 +343,8 @@ fn sweep_orphans(recovery_dir: &Path, min_age: std::time::Duration) { // Anything at the sidecar's path (even a broken one) keeps the bundle. let has_sidecar = std::fs::symlink_metadata(recovery_dir.join(format!("{key}.json"))).is_ok(); let parts = [path.clone(), path.join("tiles"), path.join("blobs")]; - if !has_sidecar && parts.iter().all(|p| untouched_for(p, min_age, true)) { + // A committed bundle is recoverable work: keep exactly what `list_recovery` lists. + if !has_sidecar && sidecarless_entry(recovery_dir, &path).is_none() && parts.iter().all(|p| untouched_for(p, min_age, true)) { let _ = std::fs::remove_dir_all(&path); } else { bundles.extend(parts); @@ -437,4 +460,41 @@ mod tests { assert_eq!(recover(&entries[0]).unwrap(), *doc); let _ = std::fs::remove_dir_all(&dir); } + + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_committed_bundle_without_a_sidecar_is_listed_and_survives_the_sweep() { + let dir = std::env::temp_dir().join(format!( + "pcraft-nosidecar-{}-{}", + std::process::id(), + SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0) + )); + let doc = + Arc::new(Document::new("Lost", photocraft_doc::Size { width: 4, height: 4 }, photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8)); + let saver = Autosaver::new(&dir, "first"); + saver.request(doc.clone(), 1, None, SaveOptions::default()); + assert!(saver.flush().is_some_and(|r| r.is_ok())); + // The crash: the bundle was committed, the sidecar never published. + std::fs::remove_file(dir.join("first.json")).unwrap(); + + let entries = list_recovery(&dir); + assert_eq!(entries.len(), 1, "the committed bundle is discoverable"); + assert_eq!((entries[0].info.key.as_str(), entries[0].info.document_name.as_str()), ("first", "Lost")); + assert_eq!(recover(&entries[0]).unwrap(), *doc); + + sweep_orphans(&dir, std::time::Duration::ZERO); + assert!(dir.join("first.pcraft").is_dir(), "aged cleanup keeps a committed bundle"); + + // Incomplete bundles (no manifest, or one that does not read) are neither listed nor kept. + let partial = dir.join("partial.pcraft"); + std::fs::create_dir_all(partial.join("tiles")).unwrap(); + let broken = dir.join("broken.pcraft"); + std::fs::create_dir_all(&broken).unwrap(); + std::fs::write(broken.join("manifest.json"), b"{").unwrap(); + assert_eq!(list_recovery(&dir).len(), 1); + sweep_orphans(&dir, std::time::Duration::ZERO); + assert!(!partial.exists() && !broken.exists()); + assert!(dir.join("first.pcraft").is_dir()); + let _ = std::fs::remove_dir_all(&dir); + } }