DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
M6: reject conflicting shared-id PDF layer choices in IDML
skylar taylor-barrick74171726635a
diff --git a/ROADMAP.md b/ROADMAP.mdindex d46697f99..d5041907b 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -14,7 +14,8 @@ DesignCraft aims at full Adobe InDesign parity — and to be better: faster, ope- **2026-10-24** · Embedded IDML keeps independent placed-PDF layer choices in table-cell andfootnote text. The variant rewrite reaches endnote stories too, but IDML endnote structures and- artwork remain unsupported. External-only export continues to reject active nested choices; no readiness percentage change.+ artwork remain unsupported. External-only export continues to reject active nested choices.+ Shared placement ids with different sources or choices fail closed; no readiness percentage change.- **2026-10-24** · PDF/A-2b preparation applies placed-PDF layer choices, including explicit all-on,to anchors inside table-cell text, footnotes and endnotes in an export-only copy. The nestedIDML variant write pass remains separate work; no readiness percentage change.diff --git a/crates/engine/src/cmd/interchange.rs b/crates/engine/src/cmd/interchange.rsindex 1ff29d634..eeca5ae5d 100644--- a/crates/engine/src/cmd/interchange.rs+++ b/crates/engine/src/cmd/interchange.rs@@ -63,7 +63,7 @@ fn with_pdf_layer_variants(doc: &Document, embed: bool) -> Result<Option<Documen.filter(|it| it.has_pdf_layer_override()).filter_map(|it| {let asset = it.graphic().and_then(|g| doc.assets.get(&g.asset)).filter(|a| designcraft_render::is_pdf(&a.data))?;- Some((it.id, asset, it.pdf_hidden_layers.clone()))+ Some((it.id, asset, it.pdf_hidden_layers.clone(), it.pdf_layer_override))}).collect();if choices.is_empty() {@@ -74,7 +74,17 @@ fn with_pdf_layer_variants(doc: &Document, embed: bool) -> Result<Option<Documen}let mut out = doc.clone();let mut variants = std::collections::HashMap::new();- for (id, asset, hidden) in choices {+ // Duplicated anchors can share an id. Reuse a variant only when source and choice agree.+ let mut expected = std::collections::HashMap::new();+ for (id, asset, hidden, overridden) in choices {+ let signature = (asset.id, hidden.clone(), overridden);+ if let Some(previous) = expected.get(&id) {+ if previous != &signature {+ return Err(EngineError::Other(format!("PDF layer choices: placements sharing id {id} have different sources or layer choices")));+ }+ continue;+ }+ expected.insert(id, signature);let bytes = designcraft_render::pdf_hide_layers(&asset.data, &hidden).ok_or_else(|| EngineError::Other(format!("{}: PDF layer choices could not be applied", asset.name)))?;let mut variant = (**asset).clone();@@ -87,13 +97,20 @@ fn with_pdf_layer_variants(doc: &Document, embed: bool) -> Result<Option<Documen}// Anchored objects live in stories, and parent pages outside the document-spread walk, so// the choice is cleared where the placement actually is.- // Count distinct ids reached, not updates: a duplicated frame can leave two placements- // sharing one id (a renumber gap tracked separately), which is not a divergence.+ // Count distinct ids, allowing identical duplicated anchors, but check every hit against+ // the original source and choice before rewriting (including placements using defaults).let mut reached = std::collections::HashSet::new();+ let mut conflicting_id = None;out.update_placed_items(|it| variants.contains_key(&it.id),|it| {reached.insert(it.id);+ if let Some((asset, hidden, overridden)) = expected.get(&it.id)+ && (it.graphic().map(|g| g.asset) != Some(*asset) || &it.pdf_hidden_layers != hidden || it.pdf_layer_override != *overridden)+ {+ conflicting_id = Some(it.id);+ return;+ }if let designcraft_doc::Content::Graphic(g) = &mut it.content&& let Some(aid) = variants.get(&it.id){@@ -103,6 +120,9 @@ fn with_pdf_layer_variants(doc: &Document, embed: bool) -> Result<Option<Documenit.pdf_hidden_layers.clear();},);+ if let Some(id) = conflicting_id {+ return Err(EngineError::Other(format!("PDF layer choices: placements sharing id {id} have different sources or layer choices")));+ }if reached.len() != variants.len() {return Err(EngineError::Other(format!("PDF layer choices could not be applied to every placement (reached {} of {})",@@ -809,6 +829,81 @@ mod nested_layer_tests {assert_eq!(layers, vec![states(false, true), states(true, false)]);}+ // Shared anchored ids are a pre-existing duplication gap. Conflicting choices must not+ // silently select one variant; this fixture changes only the copied anchor's choice.+ #[test]+ fn shared_id_with_divergent_pdf_choices_is_rejected() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let pdf = designcraft_render::pdf_hide_layers(&super::super::pdflayers::tests::two_layer_pdf(), &["Red".into()]).unwrap();+ let placed = s.execute("file.place", &json!({"base64": base64_encode(&pdf), "name": "layers.pdf"})).unwrap();+ let control = ItemId(placed["id"].as_u64().unwrap());+ let frame = {+ let doc = Arc::make_mut(&mut s.doc_mut().unwrap().doc);+ let mut item = doc.item(control).unwrap().clone();+ item.id = ItemId(doc.alloc());+ item.pdf_hidden_layers = vec!["Blue".into()];+ let layer = doc.default_layer();+ let (frame, sid) = doc.add_text_frame(SpreadRef::Doc(0), Rect::new(0.0, 0.0, 200.0, 200.0), layer, "Body", Default::default()).unwrap();+ doc.story_mut(sid).unwrap().insert_object(0, AnchoredObject { item, position: Default::default() });+ frame+ };+ s.execute("transform.move", &json!({"ids": [frame.0], "dx": 10, "dy": 10, "copy": true})).unwrap();+ // Change ONLY the copy in the newest story to explicit all-on.+ {+ let doc = Arc::make_mut(&mut s.doc_mut().unwrap().doc);+ let (_, st) = doc.stories.iter_mut().filter(|(_, st)| !st.objects.is_empty()).last().unwrap();+ let o = &mut Arc::make_mut(st).objects[0];+ let it = &mut Arc::make_mut(o).item;+ it.pdf_hidden_layers.clear();+ it.pdf_layer_override = true;+ }+ let before = s.doc().unwrap().doc.clone();+ let chosen: Vec<_> = before.placed_items().into_iter().filter(|i| i.has_pdf_layer_override()).collect();+ assert_eq!(chosen.len(), 2);+ assert_eq!(chosen[0].id, chosen[1].id);+ assert_eq!(chosen[0].graphic().unwrap().asset, chosen[1].graphic().unwrap().asset);+ assert_ne!(chosen[0].pdf_hidden_layers, chosen[1].pdf_hidden_layers);+ let err = s.execute("file.exportIdml", &json!({})).unwrap_err().to_string();+ assert!(err.contains("placements sharing id") && err.contains("different sources or layer choices"), "{err}");+ assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));+ }++ // Cross-document paste: the pasted frame's anchored placement keeps its source id,+ // which can collide with an unrelated placement in the destination.+ #[test]+ fn shared_id_with_different_pdf_assets_is_rejected() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let pdf = designcraft_render::pdf_hide_layers(&super::super::pdflayers::tests::two_layer_pdf(), &["Red".into()]).unwrap();+ let a = s.execute("file.place", &json!({"base64": base64_encode(&pdf), "name": "a.pdf"})).unwrap()["id"].as_u64().unwrap();+ s.execute("object.layerOptions", &json!({"ids": [a], "hidden": ["Blue"]})).unwrap();+ let f = s.execute("frame.create", &json!({"rect": [72, 72, 400, 400], "content": "text", "text": "Hello"})).unwrap()["id"].as_u64().unwrap();+ let sid = s.doc().unwrap().doc.item(ItemId(f)).unwrap().text_frame().unwrap().story;+ s.execute("anchored.insert", &json!({"ids": [a], "story": sid.0, "pos": 0})).unwrap();+ s.execute("selection.set", &json!({"ids": [f]})).unwrap();+ s.execute("edit.copy", &json!({})).unwrap();+ s.execute("file.new", &json!({})).unwrap();+ // A different, unrelated PDF placement in the destination (no choice).+ let b = s+ .execute("file.place", &json!({"base64": base64_encode(&super::super::pdflayers::tests::two_layer_pdf()), "name": "b.pdf"}))+ .unwrap()["id"]+ .as_u64()+ .unwrap();+ s.execute("edit.paste", &json!({})).unwrap();+ assert_eq!(a, b, "fresh documents allocate the same first placement id");+ let before = s.doc().unwrap().doc.clone();+ let graphics: Vec<_> = before.placed_items().into_iter().filter(|i| i.graphic().is_some()).collect();+ assert_eq!(graphics.len(), 2);+ assert_eq!(graphics[0].id, graphics[1].id);+ assert_ne!(graphics[0].graphic().unwrap().asset, graphics[1].graphic().unwrap().asset);+ assert!(!graphics[0].has_pdf_layer_override(), "destination placement uses file defaults");+ assert!(graphics[1].has_pdf_layer_override());+ let err = s.execute("file.exportIdml", &json!({})).unwrap_err().to_string();+ assert!(err.contains("placements sharing id") && err.contains("different sources or layer choices"), "{err}");+ assert!(Arc::ptr_eq(&before, &s.doc().unwrap().doc));+ }+#[test]fn nested_idml_cell_variants_are_applied() {prepared("cell");diff --git a/docs/pdf-layer-visibility.md b/docs/pdf-layer-visibility.mdindex bfc95e668..1e0525e40 100644--- a/docs/pdf-layer-visibility.md+++ b/docs/pdf-layer-visibility.md@@ -4,6 +4,6 @@ Object Layer Options distinguishes a PDF's own default configuration from an expThe engine command `object.layerOptions` accepts `hidden` names and optional `useDefaults`. Existing calls with `hidden: []` retain their reset behavior; pass `useDefaults: false` to explicitly show all layers. `useDefaults: true` resets defaults and cannot be combined with hidden names. Every explicit choice, including all-on (`useDefaults: false` with an empty `hidden` list), is checked with one actual visibility rewrite per distinct source before any target is edited or an undo step is recorded: a PDF can list readable layers yet refuse the rewrite (for example an over-budget cross-reference history), and PDFs without readable optional-content layers cannot be rewritten at all. Resetting to file defaults needs no rewrite and is always allowed. A stored choice whose source can no longer be rewritten still fails closed: the canvas does not draw the unmodified source and PDF export reports an error. With no explicit ID, a query selects the first placed PDF anywhere in the selection; an explicit non-PDF ID remains an error. Queries include `overridden` separately from each layer's `visible` and `default` states.-Native saves use format 4 only for an explicit all-on override, on document or parent spreads, in groups or anchored: older readers would otherwise silently render file defaults. Format 4 is one reader capability shared with retained placed-graphic layer options (#1647, see [graphic-layer-options.md](graphic-layer-options.md)), so a version 4 reader understands both. Existing nonempty hidden lists retain their older semantics and format requirements. Raster and PDF export honor the choice; PDF export keeps its existing warned rasterization behavior. Embedded IDML export stores a separate PDF variant per placement and leaves original linked bytes untouched. The export walks cover the placements a page shows through its spreads and through its stories: parent-page and anchored ones too, so a page that inherits the placement does not quietly fall back to the file's defaults. PDF/A-2b preparation also follows stories nested in table cells, footnotes and endnotes, applying both hidden-layer and explicit all-on choices to an export-only raster copy. Embedded IDML applies those choices to anchors in table-cell and footnote text too, with independent variants for placements sharing source bytes. Variants inside endnote stories are prepared, but IDML does not serialize endnote structures or their artwork; that remains unsupported (gap 19). External-only IDML export rejects active PDF layer choices because the original external file would lose the choice. A package's IDML copy links the placed files, so it cannot carry a choice either; the package report says so for a placement the export walk reaches, and the packaged DesignCraft document keeps it.+Native saves use format 4 only for an explicit all-on override, on document or parent spreads, in groups or anchored: older readers would otherwise silently render file defaults. Format 4 is one reader capability shared with retained placed-graphic layer options (#1647, see [graphic-layer-options.md](graphic-layer-options.md)), so a version 4 reader understands both. Existing nonempty hidden lists retain their older semantics and format requirements. Raster and PDF export honor the choice; PDF export keeps its existing warned rasterization behavior. Embedded IDML export stores a separate PDF variant per placement and leaves original linked bytes untouched. The export walks cover the placements a page shows through its spreads and through its stories: parent-page and anchored ones too, so a page that inherits the placement does not quietly fall back to the file's defaults. PDF/A-2b preparation also follows stories nested in table cells, footnotes and endnotes, applying both hidden-layer and explicit all-on choices to an export-only raster copy. Embedded IDML applies those choices to anchors in table-cell and footnote text too, with independent variants for placements sharing source bytes. If placements share an item id, they must agree on source asset and layer choice or the export fails rather than embedding the wrong variant. Identical duplicated anchors can still export; the shared-id gap in frame duplication remains separate. Variants inside endnote stories are prepared, but IDML does not serialize endnote structures or their artwork; that remains unsupported (gap 19). External-only IDML export rejects active PDF layer choices because the original external file would lose the choice. A package's IDML copy links the placed files, so it cannot carry a choice either; the package report says so for a placement the export walk reaches, and the packaged DesignCraft document keeps it.This fixes visibility-state handling, not full InDesign layer/print parity. It does not implement editing the contents of placed PDFs or native INDD placement. Catalog/reference safety fixes landed in #1670.