Skip to content

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

Merge PR #7591: fix(atn): refuse strict export of canvas-bound selection modifies and placed pattern buckets

aionggb7e01f8bdac1
diff --cc crates/engine/src/atn_cmds.rs
index afba79302,3ed2ace97..a59bdaecf
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@@ -966,13 -901,11 +966,24 @@@ macro_rules! ev
};
}
+/// Minimum / Maximum with Preserve: Roundness. The descriptor here maps the radius only, and
+/// Photoshop's encoding of the shape isn't verified, so such a step is not exported: it would
+/// replay as the default Squareness (#7233).
+fn preserves_roundness(p: &Value) -> bool {
+ p.get("preserve").and_then(Value::as_str) == Some("roundness")
+}
+
+ /// Whether any of the boolean `flags` is on in `p` (present, not null and not `false`).
+ fn flag_set(p: &Value, flags: &[&str]) -> bool {
+ flags.iter().any(|f| p.get(*f).is_some_and(|v| !v.is_null() && *v != Value::Bool(false)))
+ }
+
++/// Select > Modify with "Apply effect at canvas bounds": the descriptor has no field for it, so such a
++/// step is not exported (it would replay without the option).
++fn applies_at_canvas_bounds(p: &Value) -> bool {
++ flag_set(p, &["applyAtCanvasBounds"])
++}
++
/// 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.
@@@ -3344,10 -3021,10 +3363,10 @@@ const EVENTS: &[Event] = &
},
// Selection.
ev!("Invs", "inverse", "Inverse", "select.inverse", &[]),
- ev!("Fthr", "feather", "Feather", "select.modify.feather", &[F("Rds ", "radius", Kind::Px)]),
- ev!("Expn", "expand", "Expand", "select.modify.expand", &[F("By ", "radius", Kind::Px)]),
- ev!("Cntc", "contract", "Contract", "select.modify.contract", &[F("By ", "radius", Kind::Px)]),
- ev!("Smth", "smoothness", "Smooth", "select.modify.smooth", &[F("Rds ", "radius", Kind::Px)]),
- ev!("Fthr", "feather", "Feather", "select.modify.feather", &[F("Rds ", "radius", Kind::Px)], uncarried: &["applyAtCanvasBounds"]),
- ev!("Expn", "expand", "Expand", "select.modify.expand", &[F("By ", "radius", Kind::Px)], uncarried: &["applyAtCanvasBounds"]),
- ev!("Cntc", "contract", "Contract", "select.modify.contract", &[F("By ", "radius", Kind::Px)], uncarried: &["applyAtCanvasBounds"]),
- ev!("Smth", "smoothness", "Smooth", "select.modify.smooth", &[F("Rds ", "radius", Kind::Px)], uncarried: &["applyAtCanvasBounds"]),
++ ev!("Fthr", "feather", "Feather", "select.modify.feather", &[F("Rds ", "radius", Kind::Px)], unless applies_at_canvas_bounds),
++ ev!("Expn", "expand", "Expand", "select.modify.expand", &[F("By ", "radius", Kind::Px)], unless applies_at_canvas_bounds),
++ ev!("Cntc", "contract", "Contract", "select.modify.contract", &[F("By ", "radius", Kind::Px)], unless applies_at_canvas_bounds),
++ ev!("Smth", "smoothness", "Smooth", "select.modify.smooth", &[F("Rds ", "radius", Kind::Px)], unless applies_at_canvas_bounds),
ev!("Brdr", "border", "Border", "select.modify.border", &[F("Wdth", "radius", Kind::Px)]),
ev!("Grow", "grow", "Grow", "select.grow", &[F("Tlrn", "tolerance", Kind::Long)]),
ev!("Smlr", "similar", "Similar", "select.similar", &[F("Tlrn", "tolerance", Kind::Long)]),
diff --cc crates/engine/src/atn_cmds/tests.rs
index bbec1def4,272fe6a28..072c98f7d
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@@ -1558,28 -1267,33 +1558,55 @@@ fn save_while_recording_includes_pendin
assert_eq!(action["steps"].as_array().unwrap().len(), 2, "both steps must be present without duplication");
}
+/// #7233: the Minimum / Maximum descriptor carries the radius only, so a step with Preserve:
+/// Roundness is refused by strict export (and left out, reported, otherwise) instead of replaying
+/// as the default Squareness. Squareness, explicit or by default, still round-trips.
+#[test]
+fn minimum_and_maximum_with_roundness_are_not_exported_as_squareness() {
+ for id in ["filter.other.minimum", "filter.other.maximum"] {
+ let round = Action { name: "Round".into(), steps: vec![(id.into(), json!({"radius": 2, "preserve": "roundness"}))], ..Default::default() };
+ assert!(export("Audit", &[&round], &[], false).is_err(), "{id}: strict export refuses it");
+ let (_, skipped) = export("Audit", &[&round], &[], true).unwrap();
+ assert_eq!(skipped, vec![Skipped { action: "Round".into(), step: 0, id: id.into() }], "{id}");
+ for p in [json!({"radius": 2}), json!({"radius": 2, "preserve": "squareness"})] {
+ let square = Action { name: "Square".into(), steps: vec![(id.into(), p.clone())], ..Default::default() };
+ let (bytes, skipped) = export("Audit", &[&square], &[], false).unwrap();
+ assert!(skipped.is_empty(), "{id} {p}");
+ let imported = import(&bytes).unwrap();
+ let (got, params) = &imported.actions[0].steps[0];
+ assert_eq!((got.as_str(), params["radius"].as_f64()), (id, Some(2.0)), "{id} {p}");
+ assert!(params.get("preserve").is_none_or(|v| v == "squareness"), "{id} {p}: {params}");
+ }
+ }
+}
+
+ /// #7540-#7542: Feather, Contract and Smooth (and Expand) with Apply Effect at Canvas Bounds, and
+ /// #7543-#7546: a pattern Paint Bucket with a chosen pattern, scale, angle or phase, can't be
+ /// written, so strict export refuses them. Their defaults still export and round-trip.
+ #[test]
+ fn canvas_bound_modify_and_placed_pattern_bucket_refuse_strict_export() {
+ let one = |id: &str, p: Value| Action { name: "Probe".into(), steps: vec![(id.into(), p)], ..Default::default() };
+ for id in ["select.modify.feather", "select.modify.expand", "select.modify.contract", "select.modify.smooth"] {
+ assert!(export("Audit7", &[&one(id, json!({"radius": 2, "applyAtCanvasBounds": true}))], &[], false).is_err(), "{id}");
+ let plain = json!({"radius": 2, "applyAtCanvasBounds": false});
+ let (bytes, _) = export("Audit7", &[&one(id, plain)], &[], false).unwrap_or_else(|e| panic!("{id}: {e}"));
+ assert_eq!(import(&bytes).unwrap().actions[0].steps, vec![(id.to_string(), json!({"radius": 2}))]);
+ }
+ let bucket = |extra: Value| {
+ let mut p = json!({"x": 5, "y": 5, "contents": "pattern", "opacity": 70, "tolerance": 80});
+ if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) {
+ p.extend(e.clone());
+ }
+ one("paint.bucket", p)
+ };
+ for extra in [json!({"pattern": "AuditPattern"}), json!({"scale": 50}), json!({"angle": 30}), json!({"phase": [3, 5]})] {
+ assert!(export("Audit7", &[&bucket(extra.clone())], &[], false).is_err(), "{extra}");
+ }
+ for extra in [json!({}), json!({"scale": 100, "angle": 0, "phase": [0, 0]})] {
+ assert!(export("Audit7", &[&bucket(extra.clone())], &[], false).is_ok(), "{extra}");
+ }
+ }
+
/// Photoshop's Close has no document target: a Close of a specific document must not be
/// exported as a plain Close, which would close the active document on playback.
#[test]