PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(atn): refuse strict export of options the descriptors can't carry
Finn Newick1ec378bd0289
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rsindex d5c688f0a..66ad6f0c5 100644--- a/crates/engine/src/atn_cmds.rs+++ b/crates/engine/src/atn_cmds.rs@@ -435,7 +435,15 @@ fn fields_to(p: &Value, fields: &[F], mut d: Descriptor) -> Option<Descriptor> {Kind::Px => px(v.as_f64()?),Kind::Pct => pct(v.as_f64()?),Kind::Ang => ang(v.as_f64()?),- Kind::Long => long(v.as_f64()?),+ // A value outside the descriptor's 32-bit integer refuses the step rather than being+ // clamped to another value (an Add Noise seed above i32::MAX replayed as i32::MAX, #7287).+ Kind::Long => {+ let n = v.as_f64()?.round();+ if !(f64::from(i32::MIN)..=f64::from(i32::MAX)).contains(&n) {+ return None;+ }+ long(n)+ }Kind::Dbl => D::Double(v.as_f64()?),Kind::Bool => D::Boolean(v.as_bool()?),Kind::Enum(ty, table) => enm(ty, lookup_back(table, v.as_str()?)?),@@ -1110,6 +1118,18 @@ fn make_to(id: &str, p: &Value) -> Option<Descriptor> {};Some(match id {"layer.new.layer" => {+ // The descriptor carries the name, opacity and mode only. A step that also clips to+ // the layer below, labels the layer, fills it with the neutral colour or places it+ // below the active layer would replay as a different layer, so it isn't exported+ // (strict export fails, a partial one skips it: #7502, #7512, #7513, #7514).+ let set = |key: &str, default: Value| p.get(key).is_some_and(|v| !v.is_null() && *v != default);+ if set("clipToPrevious", Value::Bool(false))+ || set("color", json!("none"))+ || set("fillNeutral", Value::Bool(false))+ || set("position", json!("above"))+ {+ return None;+ }let mut u = with_name("Lyr ");if let Some(o) = jnum(p, "opacity") {u = u.with("Opct", pct(o * 100.0));@@ -1121,6 +1141,11 @@ fn make_to(id: &str, p: &Value) -> Option<Descriptor> {}"layer.new.group" => desc("null").with("null", class_ref("layerSection")).with("Usng", D::Descriptor(with_name("layerSection"))),"layer.groupLayers" => {+ // The descriptor groups the selected layers; an explicit `layer` would group another+ // one on replay (#7236).+ if p.get("layer").is_some_and(|v| !v.is_null()) {+ return None;+ }desc("null").with("null", class_ref("layerSection")).with("From", target_ref("Lyr ")).with("Usng", D::Descriptor(with_name("layerSection")))}"layer.new.layerViaCopy" | "layer.new.layerViaCut" => {@@ -1132,8 +1157,10 @@ fn make_to(id: &str, p: &Value) -> Option<Descriptor> {}"layer.newFillLayer.solidColor" => {let mut t = desc("solidColorLayer");- if let Some(c) = jstr(p, "color").and_then(color_desc) {- t = t.with("Clr ", D::Descriptor(c));+ // The descriptor's colour is opaque RGB: a colour with alpha, or one it can't parse,+ // would replay as the default black (#7507).+ if let Some(c) = p.get("color").filter(|v| !v.is_null()) {+ t = t.with("Clr ", D::Descriptor(c.as_str().and_then(color_desc)?));}desc("null").with("null", class_ref("contentLayer")).with("Usng", D::Descriptor(desc("contentLayer").with("Type", D::Descriptor(t))))}@@ -1170,10 +1197,19 @@ fn make_to(id: &str, p: &Value) -> Option<Descriptor> {}n = n.with("Wdth", px(jnum(p, "width").unwrap_or(1920.0))).with("Hght", px(jnum(p, "height").unwrap_or(1080.0)));n = n.with("Rslt", D::UnitFloat { unit: *b"#Rsl", value: jnum(p, "resolution").unwrap_or(72.0) });- let fill = match jstr(p, "background") {- Some("transparent") => "Trns",- Some("backgroundColor") => "BckC",- _ => "Wht ",+ // `Fl ` is white, transparent or Photoshop's own background colour: black, a hex+ // colour or a given `backgroundColor` would replay as something else (#7503).+ if p.get("backgroundColor").is_some_and(|v| !v.is_null()) {+ return None;+ }+ let fill = match p.get("background").filter(|v| !v.is_null()) {+ None => "Wht ",+ Some(b) => match b.as_str()? {+ "white" => "Wht ",+ "transparent" => "Trns",+ "backgroundColor" => "BckC",+ _ => return None,+ },};n = n.with("Fl ", enm("Fl ", fill)).with("Dpth", long(jnum(p, "depth").unwrap_or(8.0)));desc("null").with("Nw ", D::Descriptor(n))@@ -1327,6 +1363,14 @@ fn set_to(id: &str, p: &Value) -> Option<Descriptor> {"select.all" => sel(enm("Ordn", "Al ")),"select.deselect" => sel(enm("Ordn", "None")),"select.reselect" => sel(enm("Ordn", "Prvs")),+ // The descriptor loads a plain new selection: inverting it or combining it with the current+ // one (add, subtract, intersect) would be lost (#7506).+ "select.loadSelection"+ if p.get("invert").is_some_and(|v| *v != Value::Bool(false) && !v.is_null())+ || p.get("operation").is_some_and(|v| *v != json!("new") && !v.is_null()) =>+ {+ return None;+ }"select.loadSelection" => match jstr(p, "channel")? {"transparency" => sel(D::Reference(vec![ReferenceItem::Enumerated { class: class("Chnl"), type_id: Id::new("Chnl"), value: Id::new("Trsp") }])),"targeted" => sel(D::Reference(vec![ReferenceItem::Enumerated { class: class("Chnl"), type_id: Id::new("Ordn"), value: Id::new("Trgt") }])),@@ -1356,6 +1400,12 @@ fn set_to(id: &str, p: &Value) -> Option<Descriptor> {sel(D::Descriptor(desc(cls).with("Top ", px(y)).with("Left", px(x)).with("Btom", px(y + h)).with("Rght", px(x + w))))}"layer.setProps" | "layer.renameLayer" | "layer.setLabelColor" => {+ // Only these keys map onto the `Lyr ` descriptor; locks, clipping, Advanced Blending+ // channels or an explicit `layers` list would be dropped (#7196, #7246).+ const MAPPED: [&str; 7] = ["layer", "name", "opacity", "fill", "blend", "visible", "color"];+ if p.as_object().is_some_and(|m| m.iter().any(|(k, v)| !v.is_null() && !MAPPED.contains(&k.as_str()))) {+ return None;+ }let mut t = desc("Lyr ");if let Some(n) = jstr(p, "name") {t = t.with("Nm ", text(n));diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rsindex c4fe00eb9..7a4e7ec57 100644--- a/crates/engine/src/atn_cmds/tests.rs+++ b/crates/engine/src/atn_cmds/tests.rs@@ -1033,6 +1033,76 @@ 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");}+/// #7502, #7512, #7513, #7514: a New Layer step that clips to the layer below, has a colour+/// label, fills with the neutral colour or goes below the active layer isn't exported, since the+/// descriptor carries only its name, opacity and mode. Those options at their defaults still+/// export, and the step round-trips.+#[test]+fn new_layer_options_the_descriptor_cannot_carry_refuse_strict_export() {+ let new_layer = |p: Value| Action { name: "Probe".into(), steps: vec![("layer.new.layer".into(), p)], ..Default::default() };+ for p in [+ json!({"name": "New", "clipToPrevious": true}),+ json!({"name": "New", "color": "red"}),+ json!({"name": "New", "mode": "Overlay", "fillNeutral": true}),+ json!({"name": "New", "position": "below"}),+ json!({"name": "New", "clipToPrevious": 1}),+ ] {+ assert!(export("Audit7", &[&new_layer(p.clone())], &[], false).is_err(), "{p}");+ let (_, skipped) = export("Audit7", &[&new_layer(p.clone())], &[], true).unwrap();+ assert_eq!(skipped, vec![Skipped { action: "Probe".into(), step: 0, id: "layer.new.layer".into() }], "{p}");+ }+ let defaults =+ json!({"name": "New", "mode": "Multiply", "opacity": 0.6, "clipToPrevious": false, "color": "none", "fillNeutral": false, "position": "above"});+ let (bytes, skipped) = export("Audit7", &[&new_layer(defaults)], &[], false).unwrap();+ assert!(skipped.is_empty());+ let imported = import(&bytes).unwrap();+ assert_eq!(imported.actions[0].steps, vec![("layer.new.layer".to_string(), json!({"name": "New", "mode": "Multiply", "opacity": 0.6}))]);+}++/// Options the exported descriptors can't carry refuse strict export (and a partial export skips+/// the step) instead of exporting a step that replays differently; the forms they can carry still+/// export (#7196, #7236, #7246, #7287, #7503, #7506, #7507).+#[test]+fn options_the_descriptors_cannot_carry_refuse_strict_export() {+ let one = |id: &str, p: Value| Action { name: "Probe".into(), steps: vec![(id.into(), p)], ..Default::default() };+ let refused = [+ ("layer.groupLayers", json!({"layer": 3, "name": "Group"})),+ ("layer.newFillLayer.solidColor", json!({"color": "#c0603080"})),+ ("layer.newFillLayer.solidColor", json!({"color": "teal"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "black"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "#ab2233"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "backgroundColor", "backgroundColor": [0.2, 0.4, 0.6]})),+ ("select.loadSelection", json!({"channel": "Alpha 1", "invert": true})),+ ("select.loadSelection", json!({"channel": "Alpha 1", "operation": "subtract"})),+ ("layer.setProps", json!({"opacity": 0.5, "locked": true})),+ ("layer.setProps", json!({"opacity": 0.5, "clipped": true})),+ ("layer.setProps", json!({"layers": [2, 3], "opacity": 0.5})),+ ("filter.noise.addNoise", json!({"amount": 30, "seed": 2147483648_u64})),+ ("filter.noise.addNoise", json!({"amount": 30, "seed": 4294967295_u64})),+ ];+ for (id, p) in refused {+ assert!(export("Audit7", &[&one(id, p.clone())], &[], false).is_err(), "{id} {p}");+ let (_, skipped) = export("Audit7", &[&one(id, p.clone())], &[], true).unwrap();+ assert_eq!(skipped.len(), 1, "{id} {p}");+ }+ let carried = [+ ("layer.groupLayers", json!({"name": "Group"})),+ ("layer.newFillLayer.solidColor", json!({"color": "#c06030"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "white"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "transparent"})),+ ("file.new", json!({"width": 24, "height": 18, "background": "backgroundColor"})),+ ("select.loadSelection", json!({"channel": "Alpha 1", "invert": false, "operation": "new"})),+ ("layer.setProps", json!({"layer": 2, "opacity": 0.5, "visible": true})),+ ("filter.noise.addNoise", json!({"amount": 30, "seed": 2147483647})),+ ];+ for (id, p) in carried {+ let (bytes, skipped) = export("Audit7", &[&one(id, p.clone())], &[], false).unwrap_or_else(|e| panic!("{id} {p}: {e}"));+ assert!(skipped.is_empty(), "{id} {p}");+ let imported = import(&bytes).unwrap();+ assert_eq!(imported.actions[0].steps.first().map(|(i, _)| i.as_str()), Some(id), "{p}");+ }+}+/// 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]