From e279d5c8241cd108e71a906bc7e497b8eafcabd5 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sat, 24 Oct 2026 18:58:09 +0200 Subject: [PATCH] PSD export: keep linked and embedded copies of the same contents apart in the cache Duplicates share a contents identity, so a linked and an embedded copy of the same bytes shared one cached conversion: whichever was written first decided for both. The cache key now includes whether the bytes came from a link. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/engine/src/doc_path/tests.rs | 32 +++++++++++++++++++++++++++++ crates/io/src/psd_export.rs | 7 ++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/doc_path/tests.rs b/crates/engine/src/doc_path/tests.rs index 441ba5e702..fa806adf1e 100644 --- a/crates/engine/src/doc_path/tests.rs +++ b/crates/engine/src/doc_path/tests.rs @@ -287,3 +287,35 @@ photocraft-text-marker assert!(s.execute("layer.smartObjects.relinkToFile", json!({"layer": id.0, "path": abs.to_string_lossy()})).is_err(), "{path}"); } } + +/// Duplicates share their contents identity, so a linked and an embedded copy of the same bytes +/// meet in the export's conversion cache: each keeps its own rule, whichever is written first. +#[test] +fn a_linked_and_an_embedded_copy_of_the_same_bytes_keep_their_own_rule() { + const TEXT: &[u8] = b"plain text, not an image +photocraft-shared-marker +"; + let d = dir("shared-contents"); + std::fs::write(d.join("readme"), TEXT).unwrap(); + png(&d.join("red.png"), |_| [255, 0, 0]); + let (mut s, linked) = document(&d, &d.join("red.png")); + s.execute("layer.duplicate", json!({})).unwrap(); + let embedded = s.active().unwrap().active_layer.unwrap(); + assert_ne!(linked, embedded); + relink(&mut s, linked, "readme"); + s.edit("Embed", |doc, _| { + if let Some(LayerContent::Smart(sm)) = doc.layer_mut(embedded).map(|l| &mut l.content) { + sm.source = SmartSource::Embedded { file_name: "readme".into(), bytes: std::sync::Arc::new(TEXT.to_vec()) }; + } + Ok(()) + }) + .unwrap(); + for ext in ["psd", "psb", "tif"] { + let out = d.join(format!("copy.{ext}")); + let r = s.execute("file.saveACopy", json!({"path": out.to_string_lossy(), "tiffLayers": true})).unwrap(); + let bytes = std::fs::read(&out).unwrap(); + let copies = bytes.windows(TEXT.len()).filter(|w| *w == TEXT).count(); + assert_eq!(copies, 1, "{ext}: only the embedded copy is kept: {r}"); + assert!(r["warnings"].to_string().contains("written as pixels"), "{ext}: {r}"); + } +} diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 20000e32dd..4835397822 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -169,8 +169,9 @@ struct SmartOut { prune_ok: bool, /// Files to embed. add: Vec, - /// Sources converted so far, by contents identity and bytes (`Err` = can't be embedded). - converted: std::collections::HashMap<(SmartContentsId, [u8; 32]), Result>, + /// Sources converted so far, by contents identity, bytes and whether they were read from a + /// link (`Err` = can't be embedded). + converted: std::collections::HashMap<(SmartContentsId, [u8; 32], bool), Result>, /// Each placed layer has its own id, even when it duplicates an imported layer. placed_used: std::collections::HashSet, /// Original instances reserve their imported placement regardless of layer order. @@ -1060,7 +1061,7 @@ impl Ex { /// `linked`: the bytes were read from a path the document names, so only a file PhotoCraft /// can open is embedded (a link to a text or settings file is written as pixels instead). fn embed(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8], linked: bool) -> Result { - let key = (contents_id, *blake3::hash(bytes).as_bytes()); + let key = (contents_id, *blake3::hash(bytes).as_bytes(), linked); if let Some(r) = self.smart.converted.get(&key) { return r.clone(); }