From dd87a1b149776a4c0f13b4bf0332d655e5dd05df Mon Sep 17 00:00:00 2001 From: jlange <15385135+jlange91@users.noreply.github.com> Date: Mon, 19 Oct 2026 16:52:10 +0200 Subject: [PATCH 1/4] Print: Scale to Fit Media fills the driver's printable area (#7782) `layout()` fitted the image to the paper minus a fixed 18 pt margin, so a borderless page (the driver reports the whole sheet as printable) still printed with a white border, and a driver margin wider than 18 pt could clip the image. A new optional `printable` parameter (the rectangle `file.print.page` reports) is now the area Scale to Fit Media fills and centres in, with the 36 pt mark inset kept inside it and Emulsion Down fitting its mirror. On the native Windows route the engine reads it from the driver, so Print One Copy, the CLI and replayed actions match the Print dialog, which passes it for its preview. Without one (CUPS, PDF), the layout is unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/print_cmds.rs | 101 ++++++++++++++++++++++---- crates/engine/src/print_cmds/tests.rs | 88 ++++++++++++++++++++++ crates/ui-egui/src/file_ui.rs | 4 + 3 files changed, 177 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 0a1701a166..af06fd2e95 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -475,6 +475,28 @@ fn paper(p: &Value, cmd: &str) -> Result<(f64, f64)> { Ok(if p.get("orientation").and_then(Value::as_str) == Some("landscape") { (h.max(w), w.min(h)) } else { (w, h) }) } +/// The driver's printable area (`printable`: [x, y, width, height] pt from the paper's top-left, +/// as `file.print.page` reports it), clipped to the paper, with the layout's bottom-left origin. +fn printable(p: &Value, (pw, ph): (f64, f64), cmd: &str) -> Result> { + let Some(v) = p.get("printable").filter(|v| !v.is_null()) else { return Ok(None) }; + let r: Option> = v.as_array().and_then(|a| a.iter().map(Value::as_f64).collect()); + let Some([x, y, w, h]) = r.as_deref().and_then(|r| <[f64; 4]>::try_from(r).ok()) else { + return Err(bad(cmd, "printable is [x, y, width, height] in points from the paper's top-left")); + }; + // Drivers report their paper a fraction of a point off the named size (842.29 pt for A3's + // 841.89), so clip that much; an area that doesn't fit the paper (another paper or + // orientation) is refused. 2 pt, as `print_windows` allows between driver and layout. + const SLACK: f64 = 2.0; + if !(x >= -SLACK && y >= -SLACK && x + w <= pw + SLACK && y + h <= ph + SLACK) { + return Err(bad(cmd, "the printable area doesn't fit the paper and orientation")); + } + let (x0, y0, x1, y1) = (x.max(0.0), y.max(0.0), (x + w).min(pw), (y + h).min(ph)); + if !(x1 > x0 && y1 > y0) { + return Err(bad(cmd, "the printable area is empty")); + } + Ok(Some((x0, ph - y1, x1 - x0, y1 - y0))) +} + /// Where the image lands on the paper. #[derive(Clone, Copy, Debug, PartialEq)] pub struct PrintLayout { @@ -499,16 +521,34 @@ pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { center_crop: p.get("centerCropMarks").and_then(Value::as_bool).unwrap_or(false), registration: p.get("registrationMarks").and_then(Value::as_bool).unwrap_or(false), }; - let margin = if marks.corner_crop || marks.center_crop || marks.registration { 36.0 } else { 18.0 }; - let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); - let scale = if fit { - ((pw - 2.0 * margin) / iw).min((ph - 2.0 * margin) / ih) - } else { - p.get("scale").and_then(Value::as_f64).filter(|v| *v > 0.0).unwrap_or(100.0) / 100.0 + let has_marks = marks.corner_crop || marks.center_crop || marks.registration; + // Scale to Fit Media fits the driver's printable area when it is known (the whole sheet for + // borderless paper); marks keep their inset inside it. Without one (CUPS, PDF), the paper + // minus a fixed margin. + let emulsion_down = p.get("emulsionDown").and_then(Value::as_bool).unwrap_or(false); + let fit_box = match printable(p, (pw, ph), cmd)? { + Some((x, y, w, h)) => { + // Emulsion Down mirrors the page as it prints: fit the mirror of the printable area, + // so the printed image lands inside the real one. + let x = if emulsion_down { pw - x - w } else { x }; + let inset = if has_marks { 36.0 } else { 0.0 }; + if !(w > 2.0 * inset && h > 2.0 * inset) { + return Err(bad(cmd, "the printable area is too small for crop and registration marks")); + } + (x + inset, y + inset, w - 2.0 * inset, h - 2.0 * inset) + } + None => { + let margin = if has_marks { 36.0 } else { 18.0 }; + (margin, margin, pw - 2.0 * margin, ph - 2.0 * margin) + } }; + let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); + let scale = if fit { (fit_box.2 / iw).min(fit_box.3 / ih) } else { p.get("scale").and_then(Value::as_f64).filter(|v| *v > 0.0).unwrap_or(100.0) / 100.0 }; let (w, h) = (iw * scale, ih * scale); let center = p.get("center").and_then(Value::as_bool).unwrap_or(true); - let (x, y) = if center { + let (x, y) = if center && fit { + (fit_box.0 + (fit_box.2 - w) / 2.0, fit_box.1 + (fit_box.3 - h) / 2.0) + } else if center { ((pw - w) / 2.0, (ph - h) / 2.0) } else { // Top / left in inches from the paper's top-left corner (Photoshop's Position fields). @@ -516,7 +556,6 @@ pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { let left = p.get("left").and_then(Value::as_f64).unwrap_or(0.0) * 72.0; (left, ph - top - h) }; - let emulsion_down = p.get("emulsionDown").and_then(Value::as_bool).unwrap_or(false); Ok(PrintLayout { paper: (pw, ph), rect: (x, y, w, h), scale, marks, emulsion_down }) } @@ -568,7 +607,38 @@ fn do_print(s: &mut Session, p: &Value, cmd: &str) -> Result { Ok(result) } +/// True when a print may go to a Windows printer through the driver rather than a PDF spooler. +fn native_route(p: &Value, spooler: Option<&Spooler>) -> bool { + // A named print server always takes the IPP route and a preferred app spooler takes every + // job (the route order in `spool_job`), on Windows too: neither may enter the native path, + // which would print on the local default printer instead. + let named_server = p.get("printServer").and_then(Value::as_str).is_some_and(|v| !v.is_empty()); + let skip_native = named_server || spooler.is_some_and(|sp| sp.preferred); + let output = p.get("output").and_then(Value::as_str).is_some_and(|v| !v.is_empty()); + let send = p.get("send").and_then(Value::as_bool).unwrap_or(!output); + let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false); + cfg!(windows) && !skip_native && send && !dry && !output +} + +/// `p` with the driver's printable area, read from the printer itself, so Print One Copy, the +/// CLI, control and replayed actions fit the same area as the Print dialog does (#7782). +fn with_driver_printable(p: &Value) -> Option { + let printer = printer_name(p).ok()?; + let pg = craft_print::rgb::page(&printer, printer_settings(p).ok()?.as_ref()).ok()?; + // Only for the paper the driver is set to: a print laid out on another paper keeps the + // fixed margin (and `print_windows` refuses it), as before. + let (pw, ph) = paper(p, "file.print").ok()?; + if (pg.paper.0 - pw).abs() > 2.0 || (pg.paper.1 - ph).abs() > 2.0 { + return None; + } + let mut p = p.clone(); + p.as_object_mut()?.insert("printable".into(), json!([pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3])); + Some(p) +} + fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spooler>) -> Result { + let driver = native_route(p, spooler).then(|| with_driver_printable(p)).flatten(); + let p = driver.as_ref().unwrap_or(p); let PrintLayout { paper: (pw, ph), rect: (x, y, w, h), scale, marks, emulsion_down } = layout(doc, p, cmd)?; let (img, color) = print_image(doc, p, cmd)?; let description = p @@ -586,12 +656,8 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spool // 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"); - // A named print server always takes the IPP route and a preferred app spooler takes every - // job (the route order in `spool_job`), on Windows too: neither may enter the native path - // below, which would print on the local default printer instead. - let named_server = p.get("printServer").and_then(Value::as_str).is_some_and(|v| !v.is_empty()); - let skip_native = named_server || spooler.is_some_and(|sp| sp.preferred); - if cfg!(windows) && !skip_native && send && !dry && output.is_none() && native_rgb_compatible(&page, handling) { + let native = native_route(p, spooler); + if native && native_rgb_compatible(&page, handling) { require_printing_enabled()?; let printed = print_windows(&page, p, &doc.name, copies)?; return Ok(json!({ @@ -599,7 +665,7 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spool "copies": copies, "command": Value::Null, "sent": true, "spooler": printed, "color": color, })); } - if cfg!(windows) && !skip_native && send && !dry && output.is_none() && printer_settings(p)?.is_some() { + if native && printer_settings(p)?.is_some() { 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); @@ -666,6 +732,9 @@ fn remember_print(s: &mut Session, p: &Value) { o.remove("dryRun"); // Output and dry-run choices are not remembered, so a send override must not survive them. o.remove("send"); + // The driver's printable area belongs to the printer and paper chosen now: the dialog + // reads it again, and a later print to PDF or another printer must not inherit it. + o.remove("printable"); } let kept = remembered.clone(); s.prefs.edit(|prefs| { @@ -995,7 +1064,7 @@ 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##"{"printerSettings":hex? (Windows driver settings),"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?,"printServer":"host[:port]|socket path"? (default: the system's CUPS; a named server is always printed to over IPP),"dryRun":bool=false (render the PDF, report the backend command, don't spool; write a PDF only at an explicit output path)} → {pdf:path|null (null without explicit output), imageRect, command:diagnostic array|null, sent, spooler:where the job went|null, backend:"lp|windows|ipp|portal"|null}"##; + const PRINT_PARAMS: &str = r##"{"printerSettings":hex? (Windows driver settings),"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,"printable":[x,y,w,h] pt? (the driver's printable area from the paper's top-left, as file.print.page reports it; Scale to Fit Media fits it, else the paper minus 18 pt),"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?,"printServer":"host[:port]|socket path"? (default: the system's CUPS; a named server is always printed to over IPP),"dryRun":bool=false (render the PDF, report the backend command, don't spool; write a PDF only at an explicit output path)} → {pdf:path|null (null without explicit output), imageRect, command:diagnostic array|null, sent, spooler:where the job went|null, backend:"lp|windows|ipp|portal"|null}"##; vec![ spec!("file.print", "Print…", &["File"], Some("Cmd+P"), PRINT_PARAMS, native_doc, print), diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index c54dddf6f8..0a41a2247a 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -772,3 +772,91 @@ mod cups { assert_eq!(jobs, 2, "both prints reached the fake CUPS"); } } + +/// A 500 × 1000 pt document (72 ppi) on A3 (841.89 × 1190.55 pt): its height sets the fit. +fn a3_layout(extra: Value) -> Result { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 500, "height": 1000, "resolution": 72})).unwrap(); + let mut p = json!({"paper": "a3", "scaleToFit": true}); + if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) { + p.extend(extra.clone()); + } + layout(&s.active().unwrap().doc, &p, "file.print") +} + +#[test] +fn scale_to_fit_fills_a_borderless_printable_area() { + let (pw, ph) = (841.89, 1190.55); + // Without a printable area, the fixed 18 pt margin stays (CUPS, PDF). + let l = a3_layout(json!({})).unwrap(); + assert!((l.scale - (ph - 36.0) / 1000.0).abs() < 1e-9, "{l:?}"); + // Borderless: the driver reports the whole sheet (#7782: a Canon PRO-1000 reports its A3 as + // 842.29 × 1191.14 pt), and the image reaches the paper's edges. + let l = a3_layout(json!({"printable": [0.0, 0.0, 842.29, 1191.14]})).unwrap(); + assert!((l.scale - ph / 1000.0).abs() < 1e-9, "{l:?}"); + let (x, y, w, h) = l.rect; + assert!(y.abs() < 1e-9 && (h - ph).abs() < 1e-9 && (x - (pw - w) / 2.0).abs() < 1e-9, "{l:?}"); +} + +#[test] +fn scale_to_fit_stays_inside_an_uneven_printable_area() { + let (pw, ph) = (841.89, 1190.55); + // 17 pt left and right, 14 pt at the top, 48 pt at the bottom (from the top-left, as + // `file.print.page` reports it). The layout's rect has its origin at the bottom-left. + let l = a3_layout(json!({"printable": [17.0, 14.0, pw - 34.0, ph - 62.0]})).unwrap(); + assert!((l.scale - (ph - 62.0) / 1000.0).abs() < 1e-9, "{l:?}"); + let (x, y, w, h) = l.rect; + assert!((y - 48.0).abs() < 1e-9 && (y + h - (ph - 14.0)).abs() < 1e-9, "fills the printable height: {l:?}"); + assert!((x - (pw - w) / 2.0).abs() < 1e-9, "centred between equal side margins: {l:?}"); + // Crop and registration marks keep their 36 pt inset, inside the printable area. + let l = a3_layout(json!({"printable": [17.0, 14.0, pw - 34.0, ph - 62.0], "cornerCropMarks": true})).unwrap(); + assert!((l.scale - (ph - 62.0 - 72.0) / 1000.0).abs() < 1e-9, "{l:?}"); + assert!((l.rect.1 - 84.0).abs() < 1e-9, "{l:?}"); + // Scale to fit off: the printable area changes nothing. + let l = a3_layout(json!({"printable": [17.0, 14.0, pw - 34.0, ph - 62.0], "scaleToFit": false})).unwrap(); + assert_eq!(l, a3_layout(json!({"scaleToFit": false})).unwrap()); +} + +#[test] +fn a_bad_printable_area_is_refused() { + let portrait = json!([0.0, 0.0, 842.29, 1191.14]); + for bad in [ + json!([0, 0, 100]), + json!([0, 0, 0, 100]), + json!([0, 0, 100, -5]), + json!("whole"), + json!([2000, 0, 100, 100]), + json!([0, "oops", 0, 842.29, 1191.14]), + json!([0, 0, 842.29, null]), + ] { + assert!(a3_layout(json!({ "printable": bad })).is_err(), "{bad}"); + } + // A portrait sheet's area on a landscape layout is another paper, not a rounding error. + assert!(a3_layout(json!({"printable": portrait, "orientation": "landscape"})).is_err()); +} + +#[test] +fn emulsion_down_fits_the_mirrored_printable_area() { + let (pw, ph) = (841.89, 1190.55); + // 10 pt on the left, 40 pt on the right of the sheet as the driver sees it. The page is + // mirrored as it prints, so the image box (before mirroring) must fit the mirror image. + // A wide image, so the printable width sets the fit. + let mut s = Session::new(); + s.execute("file.new", json!({"width": 1000, "height": 500, "resolution": 72})).unwrap(); + let p = json!({"paper": "a3", "scaleToFit": true, "printable": [10.0, 0.0, pw - 50.0, ph], "emulsionDown": true}); + let l = layout(&s.active().unwrap().doc, &p, "file.print").unwrap(); + let (x, _, w, _) = l.rect; + let printed = (pw - x - w, pw - x); + assert!(printed.0 >= 10.0 - 1e-9 && printed.1 <= pw - 40.0 + 1e-9, "printed at {printed:?}: {l:?}"); +} + +#[test] +fn a_print_uses_the_printable_area_but_does_not_remember_it() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 500, "height": 1000, "resolution": 72})).unwrap(); + let r = s.execute("file.print", json!({"paper": "a3", "scaleToFit": true, "printable": [0.0, 0.0, 842.29, 1191.14], "dryRun": true})).unwrap(); + assert!((r["scale"].as_f64().unwrap() - 119.055).abs() < 1e-6, "{r}"); + // The next print (Print One Copy, or the dialog on another printer or to PDF) reads its own. + let last = last_print(&s).unwrap(); + assert!(last.get("printable").is_none() && last["scaleToFit"] == true, "{last}"); +} diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index a84bfe7779..cd5d8327ac 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -778,10 +778,12 @@ fn driver_page(f: &mut Map) { match craft_print::rgb::page(&printer, settings.as_ref()) { Ok(pg) => { f.insert("paper".into(), json!([pg.paper.0, pg.paper.1])); + f.insert("printable".into(), json!([pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3])); f.insert("orientation".into(), json!("portrait")); f.remove("__pageError"); } Err(e) => { + f.remove("printable"); f.insert("__pageError".into(), json!(e.to_string())); } } @@ -882,7 +884,9 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map Date: Mon, 19 Oct 2026 18:22:56 +0200 Subject: [PATCH 2/4] Print: marks need room in the printable area only when fitting (#7782) Also test a rejected printable area through `file.print`: the command fails and leaves the document and the remembered print settings as they were. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/print_cmds.rs | 5 +++-- crates/engine/src/print_cmds/tests.rs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index af06fd2e95..019dd7330a 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -526,13 +526,15 @@ pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { // borderless paper); marks keep their inset inside it. Without one (CUPS, PDF), the paper // minus a fixed margin. let emulsion_down = p.get("emulsionDown").and_then(Value::as_bool).unwrap_or(false); + let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); let fit_box = match printable(p, (pw, ph), cmd)? { Some((x, y, w, h)) => { // Emulsion Down mirrors the page as it prints: fit the mirror of the printable area, // so the printed image lands inside the real one. let x = if emulsion_down { pw - x - w } else { x }; let inset = if has_marks { 36.0 } else { 0.0 }; - if !(w > 2.0 * inset && h > 2.0 * inset) { + // Only Scale to Fit Media needs room for the marks; a manual scale is the user's. + if fit && !(w > 2.0 * inset && h > 2.0 * inset) { return Err(bad(cmd, "the printable area is too small for crop and registration marks")); } (x + inset, y + inset, w - 2.0 * inset, h - 2.0 * inset) @@ -542,7 +544,6 @@ pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { (margin, margin, pw - 2.0 * margin, ph - 2.0 * margin) } }; - let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); let scale = if fit { (fit_box.2 / iw).min(fit_box.3 / ih) } else { p.get("scale").and_then(Value::as_f64).filter(|v| *v > 0.0).unwrap_or(100.0) / 100.0 }; let (w, h) = (iw * scale, ih * scale); let center = p.get("center").and_then(Value::as_bool).unwrap_or(true); diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 0a41a2247a..8093547c2d 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -860,3 +860,21 @@ fn a_print_uses_the_printable_area_but_does_not_remember_it() { let last = last_print(&s).unwrap(); assert!(last.get("printable").is_none() && last["scaleToFit"] == true, "{last}"); } + +#[test] +fn a_rejected_printable_area_fails_the_print_and_changes_nothing() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 500, "height": 1000, "resolution": 72})).unwrap(); + s.execute("file.print", json!({"paper": "a3", "scale": 50, "dryRun": true})).unwrap(); + let before = (last_print(&s), s.prefs.get().dialogs.get(REMEMBERED).cloned(), s.active().unwrap().doc.clone()); + for printable in [json!([2000, 0, 100, 100]), json!([0, "oops", 0, 842.29, 1191.14])] { + let r = s.execute("file.print", json!({"paper": "a3", "scaleToFit": true, "printable": printable, "dryRun": true})); + assert!(r.is_err(), "{printable}: {r:?}"); + } + assert!(before == (last_print(&s), s.prefs.get().dialogs.get(REMEMBERED).cloned(), s.active().unwrap().doc.clone())); + // Marks need room only to fit: a small manual scale in a 60 pt-wide area prints. + let r = s.execute("file.print", json!({"paper": "a3", "scale": 5, "printable": [0, 0, 60, 1191.14], "cornerCropMarks": true, "dryRun": true})); + assert!(r.is_ok(), "{r:?}"); + let r = s.execute("file.print", json!({"paper": "a3", "scaleToFit": true, "printable": [0, 0, 60, 1191.14], "cornerCropMarks": true, "dryRun": true})); + assert!(r.is_err(), "{r:?}"); +} From c706af6a3727859a40e27c71dc54cc5ac3759202 Mon Sep 17 00:00:00 2001 From: jlange <15385135+jlange91@users.noreply.github.com> Date: Mon, 19 Oct 2026 18:50:19 +0200 Subject: [PATCH 3/4] Print: only a fitted print reads the printable area (#7782) A print at a manual scale doesn't use the area, so it no longer validates it (a stale one from a replayed action can't fail the print) or asks the Windows driver for it. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/print_cmds.rs | 11 +++++++---- crates/engine/src/print_cmds/tests.rs | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 019dd7330a..414aa49b82 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -527,14 +527,16 @@ pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { // minus a fixed margin. let emulsion_down = p.get("emulsionDown").and_then(Value::as_bool).unwrap_or(false); let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); - let fit_box = match printable(p, (pw, ph), cmd)? { + // Only a fitted print reads the area, so a stale one (a replayed action, another paper) can't + // fail a print at a manual scale. + let area = if fit { printable(p, (pw, ph), cmd)? } else { None }; + let fit_box = match area { Some((x, y, w, h)) => { // Emulsion Down mirrors the page as it prints: fit the mirror of the printable area, // so the printed image lands inside the real one. let x = if emulsion_down { pw - x - w } else { x }; let inset = if has_marks { 36.0 } else { 0.0 }; - // Only Scale to Fit Media needs room for the marks; a manual scale is the user's. - if fit && !(w > 2.0 * inset && h > 2.0 * inset) { + if !(w > 2.0 * inset && h > 2.0 * inset) { return Err(bad(cmd, "the printable area is too small for crop and registration marks")); } (x + inset, y + inset, w - 2.0 * inset, h - 2.0 * inset) @@ -638,7 +640,8 @@ fn with_driver_printable(p: &Value) -> Option { } fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spooler>) -> Result { - let driver = native_route(p, spooler).then(|| with_driver_printable(p)).flatten(); + let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); + let driver = (fit && native_route(p, spooler)).then(|| with_driver_printable(p)).flatten(); let p = driver.as_ref().unwrap_or(p); let PrintLayout { paper: (pw, ph), rect: (x, y, w, h), scale, marks, emulsion_down } = layout(doc, p, cmd)?; let (img, color) = print_image(doc, p, cmd)?; diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 8093547c2d..90628167ff 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -872,6 +872,9 @@ fn a_rejected_printable_area_fails_the_print_and_changes_nothing() { assert!(r.is_err(), "{printable}: {r:?}"); } assert!(before == (last_print(&s), s.prefs.get().dialogs.get(REMEMBERED).cloned(), s.active().unwrap().doc.clone())); + // A print at a manual scale doesn't read the area, so a stale one doesn't fail it. + let r = s.execute("file.print", json!({"paper": "a3", "scale": 50, "printable": [0.0, 0.0, 842.29, 1191.14], "orientation": "landscape", "dryRun": true})); + assert!(r.is_ok(), "{r:?}"); // Marks need room only to fit: a small manual scale in a 60 pt-wide area prints. let r = s.execute("file.print", json!({"paper": "a3", "scale": 5, "printable": [0, 0, 60, 1191.14], "cornerCropMarks": true, "dryRun": true})); assert!(r.is_ok(), "{r:?}"); From a67b6bca687d70e0d83ed2fade663895aae24b6d Mon Sep 17 00:00:00 2001 From: jlange <15385135+jlange91@users.noreply.github.com> Date: Tue, 20 Oct 2026 22:47:43 +0200 Subject: [PATCH 4/4] Print: keep the printable area to the driver's on a native print, off PDF output (#7782) A fitted native print drops a caller's printable area and fits only the one the driver reports for this paper. The Print dialog drops it whenever output or a preview is set, also when that change and the confirm land in one frame. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/print_cmds.rs | 24 ++++++++++++++++------- crates/engine/src/print_cmds/tests.rs | 9 +++++++++ crates/ui-egui/src/file_ui.rs | 28 ++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 414aa49b82..c71010f084 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -624,8 +624,20 @@ fn native_route(p: &Value, spooler: Option<&Spooler>) -> bool { } /// `p` with the driver's printable area, read from the printer itself, so Print One Copy, the -/// CLI, control and replayed actions fit the same area as the Print dialog does (#7782). -fn with_driver_printable(p: &Value) -> Option { +/// CLI, control and replayed actions fit the same area as the Print dialog does (#7782). A +/// native print never fits an area the caller passed: without the driver's, it has none. +fn with_driver_printable(p: &Value) -> Value { + let mut out = p.clone(); + if let Some(o) = out.as_object_mut() { + o.remove("printable"); + if let Some(area) = driver_printable(p) { + o.insert("printable".into(), area); + } + } + out +} + +fn driver_printable(p: &Value) -> Option { let printer = printer_name(p).ok()?; let pg = craft_print::rgb::page(&printer, printer_settings(p).ok()?.as_ref()).ok()?; // Only for the paper the driver is set to: a print laid out on another paper keeps the @@ -634,14 +646,12 @@ fn with_driver_printable(p: &Value) -> Option { if (pg.paper.0 - pw).abs() > 2.0 || (pg.paper.1 - ph).abs() > 2.0 { return None; } - let mut p = p.clone(); - p.as_object_mut()?.insert("printable".into(), json!([pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3])); - Some(p) + Some(json!([pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3])) } fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spooler>) -> Result { let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); - let driver = (fit && native_route(p, spooler)).then(|| with_driver_printable(p)).flatten(); + let driver = (fit && native_route(p, spooler)).then(|| with_driver_printable(p)); let p = driver.as_ref().unwrap_or(p); let PrintLayout { paper: (pw, ph), rect: (x, y, w, h), scale, marks, emulsion_down } = layout(doc, p, cmd)?; let (img, color) = print_image(doc, p, cmd)?; @@ -1068,7 +1078,7 @@ 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##"{"printerSettings":hex? (Windows driver settings),"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,"printable":[x,y,w,h] pt? (the driver's printable area from the paper's top-left, as file.print.page reports it; Scale to Fit Media fits it, else the paper minus 18 pt),"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?,"printServer":"host[:port]|socket path"? (default: the system's CUPS; a named server is always printed to over IPP),"dryRun":bool=false (render the PDF, report the backend command, don't spool; write a PDF only at an explicit output path)} → {pdf:path|null (null without explicit output), imageRect, command:diagnostic array|null, sent, spooler:where the job went|null, backend:"lp|windows|ipp|portal"|null}"##; + const PRINT_PARAMS: &str = r##"{"printerSettings":hex? (Windows driver settings),"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,"printable":[x,y,w,h] pt? (the driver's printable area from the paper's top-left, as file.print.page reports it; Scale to Fit Media fits it, else the paper minus 18 pt; a native Windows print reads the driver's own instead),"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?,"printServer":"host[:port]|socket path"? (default: the system's CUPS; a named server is always printed to over IPP),"dryRun":bool=false (render the PDF, report the backend command, don't spool; write a PDF only at an explicit output path)} → {pdf:path|null (null without explicit output), imageRect, command:diagnostic array|null, sent, spooler:where the job went|null, backend:"lp|windows|ipp|portal"|null}"##; vec![ spec!("file.print", "Print…", &["File"], Some("Cmd+P"), PRINT_PARAMS, native_doc, print), diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index 90628167ff..7f13da0458 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -850,6 +850,15 @@ fn emulsion_down_fits_the_mirrored_printable_area() { assert!(printed.0 >= 10.0 - 1e-9 && printed.1 <= pw - 40.0 + 1e-9, "printed at {printed:?}: {l:?}"); } +/// A native print fits only the driver's area: one the caller passed is dropped when the +/// driver can't give one for this paper (no such paper here, or no driver at all). +#[test] +fn a_native_print_drops_a_printable_area_the_driver_did_not_give() { + let p = with_driver_printable(&json!({"paper": [500.0, 500.0], "printable": [0.0, 0.0, 100.0, 100.0], "scaleToFit": true})); + assert!(p.get("printable").is_none(), "{p}"); + assert_eq!(p["paper"], json!([500.0, 500.0])); +} + #[test] fn a_print_uses_the_printable_area_but_does_not_remember_it() { let mut s = Session::new(); diff --git a/crates/ui-egui/src/file_ui.rs b/crates/ui-egui/src/file_ui.rs index 54e474215b..10374d7713 100644 --- a/crates/ui-egui/src/file_ui.rs +++ b/crates/ui-egui/src/file_ui.rs @@ -1171,8 +1171,20 @@ fn print_body(app: &mut PhotocraftApp, ui: &mut egui::Ui, f: &mut Map) -> Value { + let mut p = params(f); + if (b(f, "dryRun", false) || !s(f, "output", "").is_empty()) + && let Some(o) = p.as_object_mut() + { + o.remove("printable"); + } + p +} + fn print_confirm(app: &mut PhotocraftApp, f: &Map) -> Result { - let r = app.run("file.print", params(f))?; + let r = app.run("file.print", print_params(f))?; if r["pending"] != json!(true) { print_status(app, &r); } @@ -1482,6 +1494,20 @@ mod tests { assert!(params(f).get("__printers").is_none()); } + /// An output path or a preview typed and confirmed in one frame doesn't take the driver's area. + #[test] + fn the_printable_area_goes_only_with_a_print_to_the_printer() { + let area = json!([0.0, 0.0, 842.29, 1191.14]); + let mut f = Map::new(); + f.insert("printable".into(), area.clone()); + assert_eq!(print_params(&f)["printable"], area); + f.insert("output".into(), json!("/tmp/out.pdf")); + assert!(print_params(&f).get("printable").is_none()); + f.insert("output".into(), json!("")); + f.insert("dryRun".into(), json!(true)); + assert!(print_params(&f).get("printable").is_none()); + } + /// #7410: File › Print opens with the settings of the last print, also those remembered from /// an earlier session (the preferences), before anything was printed in this one. #[test]