Skip to content

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

Map .atn duplicate-into-new-document (Mk) to layer.duplicate

Abdul Karim Miace383de7d8ee
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index 391c21abd..ebeda4868 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -983,6 +983,26 @@ fn pattern_desc(p: &Value) -> Option<Descriptor> {
}
fn make_from(d: &Descriptor) -> Mapped {
+ // Duplicate layer/group into a new document: `Mk` with a document class, a layer
+ // `Usng`, an optional `Nm` (the new document's name) and an optional `LyrN`
+ // (the copy's name). A `Dplc` with a `T` document reference (duplicate into another
+ // open document) stays a kept Photoshop step: document indices mean nothing at replay.
+ if let Some(r) = get_ref(d, "null")
+ && ref_class(r).as_deref() == Some("Dcmn")
+ && matches!(target(r), Target::Class)
+ && let Some(u) = get_ref(d, "Usng")
+ && matches!(ref_class(u).as_deref(), Some("Lyr") | Some("layerSection"))
+ && let Some(mut p) = layer_target(u)
+ {
+ p.insert("document".into(), "new".into());
+ if let Some(n) = get_text(d, "Nm ") {
+ p.insert("documentName".into(), n.into());
+ }
+ if let Some(n) = get_text(d, "LyrN") {
+ p.insert("name".into(), n.into());
+ }
+ return step("layer.duplicate", Value::Object(p));
+ }
if let Some(nw) = get_obj(d, "Nw ") {
// Layer via Copy / Cut (`Usng` = the selection), or a new document.
if get_enum(d, "Usng").as_deref() == Some("Slct") {
@@ -1117,6 +1137,21 @@ fn make_to(id: &str, p: &Value) -> Option<Descriptor> {
let t = pattern_desc(p)?;
desc("null").with("null", class_ref("contentLayer")).with("Usng", D::Descriptor(desc("contentLayer").with("Type", D::Descriptor(t))))
}
+ "layer.duplicate" => {
+ // Duplicate into a new document is a `Mk`, not a `Dplc` (see `make_from`).
+ // Anything else falls through to the `Dplc` event below.
+ if jstr(p, "document") != Some("new") {
+ return None;
+ }
+ let mut d = desc("null").with("null", class_ref("Dcmn")).with("Usng", layer_ref(p));
+ if let Some(n) = jstr(p, "documentName") {
+ d = d.with("Nm ", text(n));
+ }
+ if let Some(n) = jstr(p, "name") {
+ d = d.with("LyrN", text(n));
+ }
+ d
+ }
"file.newFromClipboard" => desc("null").with("Nw ", D::Descriptor(desc("Dcmn").with("preset", text("Clipboard")))),
"file.new" => {
let mut n = desc("Dcmn");
@@ -1876,6 +1911,11 @@ fn duplicate_from(d: &Descriptor) -> Mapped {
let Some(r) = get_ref(d, "null") else { return Mapped::Unsupported { harmless: false } };
match ref_class(r).as_deref() {
Some("Lyr") => {
+ // A `T` document reference (duplicate into another open document) has no
+ // replayable meaning here: keep the step verbatim rather than misplay it.
+ if get_ref(d, "T ").is_some() {
+ return Mapped::Unsupported { harmless: false };
+ }
let Some(mut p) = layer_target(r) else { return Mapped::Unsupported { harmless: false } };
if let Some(n) = get_text(d, "Nm ") {
p.insert("name".into(), n.into());
@@ -1897,6 +1937,10 @@ fn duplicate_from(d: &Descriptor) -> Mapped {
}
fn duplicate_to(id: &str, p: &Value) -> Option<Descriptor> {
+ // Duplicate into a new document is a `Mk` (handled above); let it fall through.
+ if id == "layer.duplicate" && jstr(p, "document") == Some("new") {
+ return None;
+ }
let mut d = match id {
"layer.duplicate" => desc("null").with("null", layer_ref(p)),
"image.duplicate" => {
@@ -2696,6 +2740,7 @@ const EVENTS: &[Event] = &[
"layer.new.layer",
"layer.new.group",
"layer.groupLayers",
+ "layer.duplicate",
"layer.new.layerViaCopy",
"layer.new.layerViaCut",
"layer.newFillLayer.solidColor",
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index bdb839fa3..b5c3a9e54 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -212,6 +212,12 @@ fn every_mapped_command_exports_and_imports_back() {
("image.mode.grayscale", json!({})),
("image.mode.bits16", json!({})),
("layer.duplicate", json!({"name": "copy"})),
+ // Duplicate into a new document is a `Mk`, not a `Dplc` (the three shapes of
+ // a recorded Duplicate Layer with Destination: New, plus a named layer).
+ ("layer.duplicate", json!({"document": "new"})),
+ ("layer.duplicate", json!({"document": "new", "documentName": "test new name"})),
+ ("layer.duplicate", json!({"document": "new", "name": "Laer name change"})),
+ ("layer.duplicate", json!({"layer": "Shadow", "document": "new", "documentName": "Doc"})),
("image.duplicate", json!({"mergedOnly": true})),
("layer.new.layer", json!({"name": "Ink", "opacity": 0.5, "mode": "Multiply"})),
("layer.new.group", json!({"name": "G"})),
@@ -448,6 +454,13 @@ fn unsupported_shapes_become_photoshop_steps_not_errors() {
Some(desc("null").with("null", class_ref("AdjL")).with("Usng", D::Descriptor(desc("AdjL").with("Type", D::Descriptor(desc("GdMp")))))),
),
item("GsnB", "Gaussian Blur", None),
+ // Duplicate into another open document: the `T` reference means nothing at
+ // replay, so the step is kept verbatim instead of misplayed as a same-doc copy.
+ item(
+ "Dplc",
+ "Duplicate",
+ Some(desc("null").with("null", target_ref("Lyr ")).with("T ", D::Reference(vec![ReferenceItem::Name { class: class("Dcmn"), name: UnicodeString::new_nul("Other") }]))),
+ ),
];
let file = AtnFile {
name: UnicodeString::new_nul("Odd"),
@@ -457,8 +470,8 @@ fn unsupported_shapes_become_photoshop_steps_not_errors() {
};
let imp = import(&atn::write(&file).unwrap()).unwrap();
let ids: Vec<&str> = imp.actions[0].steps.iter().map(|(id, _)| id.as_str()).collect();
- assert_eq!(ids, [PHOTOSHOP_STEP, PHOTOSHOP_STEP, PHOTOSHOP_STEP, PHOTOSHOP_STEP, "filter.blur.gaussianBlur"]);
- assert_eq!(imp.unsupported.len(), 4);
+ assert_eq!(ids, [PHOTOSHOP_STEP, PHOTOSHOP_STEP, PHOTOSHOP_STEP, PHOTOSHOP_STEP, "filter.blur.gaussianBlur", PHOTOSHOP_STEP]);
+ assert_eq!(imp.unsupported.len(), 5);
assert!(imp.actions[0].disabled.is_empty(), "steps with a document effect stay on so playback reports them");
}