diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs index 3cec3e4492b..c9008ac0c90 100644 --- a/crates/engine/src/file_cmds.rs +++ b/crates/engine/src/file_cmds.rs @@ -963,8 +963,8 @@ fn file_info(s: &mut Session, p: &Value) -> Result { fn fit_image(s: &mut Session, p: &Value) -> Result { let d = s.active().ok_or(EngineError::NoDocument)?; let (w, h) = (d.doc.size.width as f64, d.doc.size.height as f64); - let bw = f64_param(p, "width").unwrap_or(w).max(1.0); - let bh = f64_param(p, "height").unwrap_or(h).max(1.0); + let bw = size_param(p, "width").unwrap_or(w); + let bh = size_param(p, "height").unwrap_or(h); let k = (bw / w).min(bh / h); if (k > 1.0 && p.get("dontEnlarge").and_then(Value::as_bool).unwrap_or(false)) || ((w * k).round() == w && (h * k).round() == h) { return Ok(json!({"width": w, "height": h, "changed": false})); diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rs index 9b5ec43a785..b4e25796892 100644 --- a/crates/engine/src/file_cmds/tests.rs +++ b/crates/engine/src/file_cmds/tests.rs @@ -653,6 +653,19 @@ fn fit_image_and_conditional_mode_change() { assert!(s.execute("file.automate.conditionalModeChange", json!({"to": "duotone"})).is_err()); } +#[test] +fn fit_image_zero_or_negative_side_is_unconstrained() { + // Dialogs send 0 for an empty field: that side is not a limit, not a 1 px box. + let mut s = session(1000, 800, 8); + s.execute("file.automate.fitImage", json!({"width": 0, "height": 400})).unwrap(); + assert_eq!((doc(&s).size.width, doc(&s).size.height), (500, 400)); + s.execute("file.automate.fitImage", json!({"width": 250, "height": -5})).unwrap(); + assert_eq!((doc(&s).size.width, doc(&s).size.height), (250, 200)); + let r = s.execute("file.automate.fitImage", json!({"width": 0, "height": 0})).unwrap(); + assert_eq!(r["changed"], false); + assert_eq!((doc(&s).size.width, doc(&s).size.height), (250, 200)); +} + #[test] fn conditional_mode_change_cannot_run_a_mode_change_the_gate_denies() { fn deny_mode(id: &str, _: &Value) -> Result<()> {