Skip to content

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

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

Finn Newick6b4acf299f26
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index d5c688f0a..77a4475b4 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -871,6 +871,21 @@ struct Event {
}
macro_rules! ev {
+ // `uncarried`: flags the command reads that the event's descriptor has no field for; a step
+ // with one of them set isn't exported (strict export fails, a partial one skips it).
+ ($code:literal, $name:literal, $label:literal, $id:literal, $fields:expr, uncarried: $flags:expr) => {
+ Event {
+ code: $code,
+ name: $name,
+ label: $label,
+ ids: &[$id],
+ from: |d| match fields_from(d, $fields) {
+ Some(m) => step($id, Value::Object(m)),
+ None => Mapped::Unsupported { harmless: false },
+ },
+ to: |_, p| if pixel_target(p) && !flag_set(p, $flags) { fields_to(p, $fields, desc("null")) } else { None },
+ }
+ };
($code:literal, $name:literal, $label:literal, $id:literal, $fields:expr) => {
Event {
code: $code,
@@ -886,6 +901,11 @@ macro_rules! ev {
};
}
+/// 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)))
+}
+
/// 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.
@@ -2053,6 +2073,14 @@ fn fill_to(id: &str, p: &Value) -> Option<Descriptor> {
d = d.with("Tlrn", long(jnum(p, "tolerance").unwrap_or(32.0))).with("AntA", D::Boolean(jbool(p, "antiAlias").unwrap_or(true)));
// `source` is the command's older name for `contents`.
d = if p.get("contents").or_else(|| p.get("source")).and_then(Value::as_str) == Some("pattern") {
+ // `Pttn` names no pattern and holds no placement: a chosen pattern, a scale, an angle
+ // or a phase would replay with the current pattern at its defaults (#7543, #7544,
+ // #7545, #7546).
+ let num = |k: &str, default: f64| p.get(k).is_some_and(|v| !v.is_null() && v.as_f64() != Some(default));
+ let phase = p.get("phase").is_some_and(|v| !v.is_null() && v.as_array().is_none_or(|a| a.iter().any(|c| c.as_f64() != Some(0.0))));
+ if p.get("pattern").is_some_and(|v| !v.is_null()) || num("scale", 100.0) || num("angle", 0.0) || phase {
+ return None;
+ }
d.with("Usng", enm("FlCn", "Pttn"))
} else {
// An explicit colour overrides the foreground (#7199): it travels as Fill's `Clr `.
@@ -2915,10 +2943,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!("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 --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index c4fe00eb9..c5624551c 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -1033,6 +1033,33 @@ fn remove_white_clears_the_targeted_layer_not_the_converted_background() {
assert!(bottom.surface().unwrap().sample_channel(2, 2, 1) > 0.99, "and kept its pixels");
}
+/// #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]