Skip to content

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

Refuse .atn export of resolution-only Image Size without keepPrintSize

NetterSilver35c7065692e4
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index 9e8eb6cb7..732e1b5e8 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -2443,6 +2443,16 @@ fn shadows_highlights_to(_: &str, p: &Value) -> Option<Descriptor> {
}
fn image_size_to(_: &str, p: &Value) -> Option<Descriptor> {
+ // Resolution only with Resample on imports as keep-print-size; a command without it cannot be told apart.
+ if jnum(p, "resolution").is_some()
+ && jnum(p, "width").is_none()
+ && jnum(p, "height").is_none()
+ && jnum(p, "percent").is_none()
+ && jstr(p, "resample").is_some_and(|r| r != "none")
+ && jbool(p, "keepPrintSize") != Some(true)
+ {
+ return None;
+ }
let mut d = desc("null");
if let Some(pc) = jnum(p, "percent") {
d = d.with("Wdth", pct(pc)).with("Hght", pct(pc)).with("CnsP", D::Boolean(true));
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 26865d406..eaf23c445 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -1387,3 +1387,30 @@ fn function_keys_keep_shift_and_command() {
}
assert_eq!(super::function_key_name(7, false, false), "F7");
}
+
+fn image_size_round_trip(params: &Value) -> Option<Value> {
+ let item = export_item("image.imageSize", params, "S")?;
+ let e = event_for(&item.event).unwrap();
+ match (e.from)(item.descriptor.as_ref().unwrap()) {
+ Mapped::Steps(steps) => Some(steps[0].1.clone()),
+ _ => panic!("image size descriptor did not import as steps"),
+ }
+}
+
+#[test]
+fn image_size_resolution_only_does_not_gain_keep_print_size() {
+ // The .atn cannot tell resolution-only from keepPrintSize:true, so the former must not
+ // export as a descriptor that imports back as keep-print-size.
+ let params = json!({"resolution": 144, "resample": "nearest"});
+ assert_eq!(image_size_round_trip(&params), None);
+}
+
+#[test]
+fn image_size_intentional_controls_still_round_trip() {
+ for params in [
+ json!({"resolution": 144.0, "resample": "nearest", "keepPrintSize": true}),
+ json!({"width": 12.0, "height": 10.0, "resolution": 144.0, "resample": "nearest"}),
+ ] {
+ assert_eq!(image_size_round_trip(&params), Some(params.clone()));
+ }
+}