Fit Image: a width or height of 0 leaves that side unconstrained (#6766)

fit_image read width/height with f64_param and clamped to 1, so a 0 or
negative side (dialogs send 0 for an empty field) became a 1 px box and
shrank the document to 1x1. It now uses size_param like the rest of
file_cmds.rs, so such a side falls back to the document's own size.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-16 22:49:38 +02:00
1 parent 2a952f164c
commit ab81052308
2 files changed
+15 -2

No files matched your search

+2 -2
View File
@@ -867,8 +867,8 @@ fn file_info(s: &mut Session, p: &Value) -> Result<Value> {
fn fit_image(s: &mut Session, p: &Value) -> Result<Value> {
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}));
+13
View File
@@ -541,6 +541,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<()> {