Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Print: keep the printable area to the driver's on a native print, off PDF output (#7782)

jlangea67b6bca687d
diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs
index 414aa49b8..c71010f08 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<Value> {
+/// 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<Value> {
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<Value> {
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<Value> {
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> {
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 90628167f..7f13da045 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 54e474215..10374d771 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<String, Va
}
}
+/// `file.print`'s params. The driver's printable area only goes with a print to the printer:
+/// an output path or a preview set in the same frame as the confirm still drops it.
+fn print_params(f: &Map<String, Value>) -> 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<String, Value>) -> Result<Value, String> {
- 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]