Skip to content

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

Actions: a setting Photoshop's event cannot hold refuses the step

Hamzae5c906b49bc8
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index 9e8eb6cb7..b7f93b20a 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -881,11 +881,38 @@ macro_rules! ev {
Some(m) => step($id, Value::Object(m)),
None => Mapped::Unsupported { harmless: false },
},
- to: |_, p| if pixel_target(p) { fields_to(p, $fields, desc("null")) } else { None },
+ to: |_, p| if pixel_target(p) && holds_its_settings($id, p) { fields_to(p, $fields, desc("null")) } else { None },
}
};
}
+/// Settings a mapped event has no field for, by command: `(param, the value the event plays as)`.
+/// Photoshop's event has nowhere to put these, so a step that sets one cannot be written: it
+/// would import as a different edit, silently. Such a step is refused, the way [`pixel_target`]
+/// refuses a filter aimed at a mask -- and no key Photoshop does not define is invented for it,
+/// which the clean-room rule forbids and which would make the file wrong in another reader.
+///
+/// A step that leaves the setting at the value the event plays as exports as before.
+const UNHOLDABLE: &[(&str, &[(&str, &str)])] = &[
+ // Crst carries ClSz only; replay seeds the pattern from 0 (#7668).
+ ("filter.pixelate.crystallize", &[("seed", "0")]),
+ // RdlB carries Amnt, BlrM and BlrQ; replay spins or zooms about the middle of the layer.
+ ("filter.blur.radialBlur", &[("centerX", "0.5"), ("centerY", "0.5")]),
+ // Mnm /Mxm carry Rds only; replay keeps squareness.
+ ("filter.other.minimum", &[("preserve", "squareness")]),
+ ("filter.other.maximum", &[("preserve", "squareness")]),
+];
+
+/// Whether every setting of `p` that its event cannot hold is at the value the event plays as
+/// (see [`UNHOLDABLE`]).
+fn holds_its_settings(id: &str, p: &Value) -> bool {
+ let Some((_, settings)) = UNHOLDABLE.iter().find(|(cmd, _)| *cmd == id) else { return true };
+ settings.iter().all(|(param, played)| match p.get(param) {
+ None | Some(Value::Null) => true,
+ Some(v) => v.as_str() == Some(played) || v.as_f64().is_some_and(|n| played.parse::<f64>().is_ok_and(|d| n == d)),
+ })
+}
+
/// Whether `p` edits the layer's pixels: no `"target"` or `"target":"pixels"`. Photoshop's
/// descriptor for a plain event has no field for a mask, Quick Mask or channel target, so such
/// a step can't be exported faithfully.
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 26865d406..84578b842 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -199,6 +199,44 @@ fn save_round_trips_through_photoshop_events() {
assert_eq!(r["set"], "Ours");
}
+/// A setting Photoshop's event has no field for is dropped by export, and the step then plays back
+/// as a different edit: Crystallize's `seed` (#7668), Radial Blur's centre, Minimum's and
+/// Maximum's `preserve`. Such a step is refused, as one with no equivalent event already is.
+#[test]
+fn a_setting_the_event_cannot_hold_is_refused() {
+ for (id, params) in [
+ ("filter.pixelate.crystallize", json!({"cellSize": 5, "seed": 37})),
+ ("filter.blur.radialBlur", json!({"amount": 10, "centerX": 0.25, "centerY": 0.5})),
+ ("filter.other.minimum", json!({"radius": 4, "preserve": "roundness"})),
+ ("filter.other.maximum", json!({"radius": 4, "preserve": "roundness"})),
+ ] {
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![(id.into(), params)]));
+ let e = s.execute("actions.save", json!({"action": "A"})).unwrap_err().to_string();
+ assert!(e.contains(id), "{id}: {e}");
+ let r = s.execute("actions.save", json!({"action": "A", "skipUnsupported": true})).unwrap();
+ assert_eq!(r["skipped"], json!([{"action": "A", "step": 0, "id": id}]), "{id}");
+ }
+}
+
+/// The same steps at the value the event plays as, and with the setting absent: written as before.
+#[test]
+fn settings_at_the_value_the_event_plays_still_export() {
+ for (id, params) in [
+ ("filter.pixelate.crystallize", json!({"cellSize": 5, "seed": 0})),
+ ("filter.pixelate.crystallize", json!({"cellSize": 5})),
+ ("filter.blur.radialBlur", json!({"amount": 10, "centerX": 0.5, "centerY": 0.5})),
+ ("filter.blur.radialBlur", json!({"amount": 10})),
+ ("filter.other.minimum", json!({"radius": 4, "preserve": "squareness"})),
+ ("filter.other.maximum", json!({"radius": 4})),
+ ] {
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![(id.into(), params.clone())]));
+ let r = s.execute("actions.save", json!({"action": "A"})).unwrap_or_else(|e| panic!("{id} {params}: {e}"));
+ assert_eq!(r["skipped"], json!([]), "{id} {params}");
+ }
+}
+
#[test]
fn every_mapped_command_exports_and_imports_back() {
let cases: Vec<(&str, Value)> = vec![