Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

M6: preserve nested PDF layer choices in embedded IDML

skylar taylor-barrick25218f37594e
diff --git a/ROADMAP.md b/ROADMAP.md
index b517f64e9..625e83073 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -12,6 +12,9 @@
DesignCraft aims at full Adobe InDesign parity — and to be better: faster, open (documented JSON format + IDML), scriptable by agents (MCP), and available on the web.
+- **2026-10-24** · Embedded IDML keeps independent placed-PDF layer choices in table-cell and
+ footnote text. The variant rewrite reaches endnote stories too, but IDML endnote structures and
+ artwork remain unsupported. External-only export rejects active nested choices; 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 nested
IDML 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.rs
index a7ce794ea..2d955d9f3 100644
--- a/crates/engine/src/cmd/interchange.rs
+++ b/crates/engine/src/cmd/interchange.rs
@@ -53,9 +53,9 @@ pub(crate) fn has_applicable_pdf_layer_overrides(doc: &Document) -> bool {
/// choose different layers. The live document and original linked bytes stay untouched.
///
/// Parent-page and anchored placements count too, so a page that inherits the placement does not
-/// quietly fall back to the file's defaults. Choices inside table-cell and note text are found
-/// by the read walk, but the legacy update_items write pass below cannot apply their variants
-/// yet (see `docs/pdf-layer-visibility.md`).
+/// quietly fall back to the file's defaults. Read and write walks also reach table-cell and
+/// note text. Endnote variants are prepared too, but IDML endnote structures/artwork are not
+/// serialized yet (see `docs/pdf-layer-visibility.md`).
fn with_pdf_layer_variants(doc: &Document, embed: bool) -> Result<Option<Document>> {
let choices: Vec<_> = doc
.placed_items()
@@ -87,7 +87,7 @@ 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.
- out.update_items(
+ out.update_placed_items(
|it| variants.contains_key(&it.id),
|it| {
if let designcraft_doc::Content::Graphic(g) = &mut it.content
@@ -621,3 +621,181 @@ mod tests {
assert_eq!(without_document_extension("notes.txt"), "notes.txt");
}
}
+
+#[cfg(test)]
+mod nested_layer_tests {
+ use std::{io::Read, sync::Arc};
+
+ use designcraft_doc::{AnchoredObject, ItemId, SpreadRef, Story, StoryId, Table};
+ use designcraft_geom::Rect;
+
+ use super::*;
+
+ // One default placement and two nested placements share bytes. The latter use a legacy
+ // nonempty hidden list and explicit all-on, so each needs a different embedded variant.
+ fn fixture(location: &str) -> (Session, ItemId, [ItemId; 2]) {
+ let pdf = designcraft_render::pdf_hide_layers(&super::super::pdflayers::tests::two_layer_pdf(), &["Red".into()]).unwrap();
+ let mut session = Session::new();
+ session.execute("file.new", &json!({})).unwrap();
+ let placed = session.execute("file.place", &json!({"base64": base64_encode(&pdf), "name": "layers.pdf"})).unwrap();
+ let control = ItemId(placed["id"].as_u64().unwrap());
+ let ids = {
+ let doc = Arc::make_mut(&mut session.doc_mut().unwrap().doc);
+ let original = doc.item(control).unwrap().clone();
+ let aid = original.graphic().unwrap().asset;
+ Arc::make_mut(doc.assets.get_mut(&aid).unwrap()).link = Some("/synthetic/layers.pdf".into());
+ let mut hidden = original.clone();
+ hidden.id = ItemId(doc.alloc());
+ hidden.pdf_hidden_layers = vec!["Blue".into()];
+ let mut all_on = original;
+ all_on.id = ItemId(doc.alloc());
+ all_on.pdf_layer_override = true;
+ let ids = [hidden.id, all_on.id];
+ let mut nested = Story::new(StoryId(0));
+ for item in [hidden, all_on] {
+ nested.insert_object(nested.len(), AnchoredObject { item, position: Default::default() });
+ }
+ let layer = doc.default_layer();
+ let (_, sid) = doc.add_text_frame(SpreadRef::Doc(0), Rect::new(0.0, 0.0, 200.0, 200.0), layer, "Body", Default::default()).unwrap();
+ let story = doc.story_mut(sid).unwrap();
+ match location {
+ "cell" => {
+ let mut table = Table::new(1, 1, 1, 0, 0, 100.0);
+ table.cells[0].text = nested;
+ story.insert_table(0, table);
+ }
+ "footnote" => {
+ story.insert_note(0, "Note", Default::default()).unwrap();
+ Arc::make_mut(&mut story.notes[0]).text = nested;
+ }
+ _ => {
+ assert_eq!(location, "endnote");
+ story.insert_endnote(0, "Endnote", Default::default()).unwrap();
+ Arc::make_mut(&mut story.endnotes[0]).text = nested;
+ }
+ }
+ ids
+ };
+ (session, control, ids)
+ }
+
+ fn states(blue: bool, red: bool) -> Vec<(String, bool)> {
+ vec![("Blue".into(), blue), ("Red".into(), red)]
+ }
+
+ fn prepared(location: &str) {
+ let (session, control, ids) = fixture(location);
+ let source = &session.doc().unwrap().doc;
+ let original_asset = source.item(control).unwrap().graphic().unwrap().asset;
+ let copy = with_pdf_layer_variants(source, true).unwrap().unwrap();
+ for (id, expected) in ids.into_iter().zip([states(false, true), states(true, true)]) {
+ let item = copy.placed_items().into_iter().find(|i| i.id == id).unwrap();
+ let asset = &copy.assets[&item.graphic().unwrap().asset];
+ assert_eq!(designcraft_render::pdf_layers(&asset.data), expected, "{location}: placement {id}");
+ assert_ne!(asset.id, original_asset);
+ assert!(asset.link.is_none());
+ assert!(!item.pdf_layer_override && item.pdf_hidden_layers.is_empty());
+ }
+ assert_eq!(copy.item(control).unwrap().graphic().unwrap().asset, original_asset);
+ assert!(Arc::ptr_eq(&copy.assets[&original_asset], &source.assets[&original_asset]));
+ assert_eq!(designcraft_render::pdf_layers(&source.assets[&original_asset].data), states(true, false));
+ assert!(source.placed_items().into_iter().filter(|i| ids.contains(&i.id)).all(|i| i.has_pdf_layer_override()));
+ }
+
+ fn embedded(location: &str) {
+ let (mut session, _, _) = fixture(location);
+ let original = session.doc().unwrap().doc.clone();
+ let result = session.execute("file.exportIdml", &json!({})).unwrap();
+ let bytes = base64_decode(result["base64"].as_str().unwrap());
+ let imported = designcraft_idml::import_idml(&bytes).unwrap();
+ let mut roundtrip: Vec<_> =
+ imported.assets.values().filter(|a| designcraft_render::is_pdf(&a.data)).map(|a| designcraft_render::pdf_layers(&a.data)).collect();
+ let mut expected = vec![states(true, false), states(false, true), states(true, true)];
+ roundtrip.sort();
+ expected.sort();
+ assert_eq!(roundtrip, expected, "{location}: reopened IDML keeps each choice");
+ let mut package = zip::ZipArchive::new(std::io::Cursor::new(bytes)).unwrap();
+ let mut actual = Vec::new();
+ for index in 0..package.len() {
+ let mut entry = package.by_index(index).unwrap();
+ if !entry.name().ends_with(".xml") {
+ continue;
+ }
+ let mut xml = String::new();
+ entry.read_to_string(&mut xml).unwrap();
+ for cdata in xml.split("<![CDATA[").skip(1) {
+ let data = base64_decode(cdata.split("]]>").next().unwrap());
+ if designcraft_render::is_pdf(&data) {
+ actual.push(designcraft_render::pdf_layers(&data));
+ }
+ }
+ }
+ actual.sort();
+ assert_eq!(actual, expected, "{location}: each placement embeds its own visibility state");
+ assert!(Arc::ptr_eq(&original, &session.doc().unwrap().doc), "export must not edit the live document");
+ }
+
+ fn external(location: &str) {
+ let (mut session, _, ids) = fixture(location);
+ // Each choice alone must reject the export, including a legacy nonempty hidden list.
+ for active in ids {
+ let original = session.doc().unwrap().doc.clone();
+ let mut one = (*original).clone();
+ one.update_placed_items(
+ |i| ids.contains(&i.id) && i.id != active,
+ |i| {
+ i.pdf_layer_override = false;
+ i.pdf_hidden_layers.clear();
+ },
+ );
+ let mut single = Session::new();
+ single.add_document(DocState::new(one, None));
+ let err = single.execute("file.exportIdml", &json!({"embedImages": false})).unwrap_err().to_string();
+ assert!(err.contains("embedded images"), "{location}: {err}");
+ assert!(Arc::ptr_eq(&original, &session.doc().unwrap().doc));
+ }
+ Arc::make_mut(&mut session.doc_mut().unwrap().doc).update_placed_items(
+ |i| ids.contains(&i.id),
+ |i| {
+ i.pdf_layer_override = false;
+ i.pdf_hidden_layers.clear();
+ },
+ );
+ session.execute("file.exportIdml", &json!({"embedImages": false})).unwrap();
+ }
+
+ #[test]
+ fn nested_idml_cell_variants_are_applied() {
+ prepared("cell");
+ }
+ #[test]
+ fn nested_idml_footnote_variants_are_applied() {
+ prepared("footnote");
+ }
+ // Endnote structures/artwork are not serialized by IDML yet. Check preparation without
+ // mistaking a correct export-only variant for endnote-artwork serialization support.
+ #[test]
+ fn nested_idml_endnote_variants_are_prepared() {
+ prepared("endnote");
+ }
+ #[test]
+ fn nested_idml_cell_embedded_choices() {
+ embedded("cell");
+ }
+ #[test]
+ fn nested_idml_footnote_embedded_choices() {
+ embedded("footnote");
+ }
+ #[test]
+ fn nested_idml_cell_external_choices_are_rejected() {
+ external("cell");
+ }
+ #[test]
+ fn nested_idml_footnote_external_choices_are_rejected() {
+ external("footnote");
+ }
+ #[test]
+ fn nested_idml_endnote_external_choices_are_rejected() {
+ external("endnote");
+ }
+}
diff --git a/docs/gaps.md b/docs/gaps.md
index 8efd54bed..b6e5fc73d 100644
--- a/docs/gaps.md
+++ b/docs/gaps.md
@@ -213,9 +213,10 @@ Kind: **F** feature · **U** UI/UX · **T** typography · **FF** file format ·
- **Layer choices:** placed PDFs distinguish explicit all-on visibility from file defaults;
choices survive native saves, raster/PDF output and embedded IDML export for the placements the
export walks reach: document and parent spreads, groups, and objects anchored in a story. A
- placement anchored inside table-cell or footnote/endnote text is also prepared for PDF/A-2b,
- but the IDML variant write pass cannot update those nested placements yet. IDML layer variants
- are embedded copies; external-only export is rejected when choices would be lost.
+ placement anchored inside table-cell or footnote/endnote text is also prepared for PDF/A-2b.
+ Embedded IDML applies variants inside table-cell and footnote text; endnote variants are prepared,
+ but IDML endnote structures/artwork are not serialized yet. IDML layer variants are embedded
+ copies; external-only export is rejected when choices would be lost.
- **Missing:** EPS is shown through its preview only (no PostScript interpreter); PSD layer
comps beyond visibility; the Clipping Path Options dialog (tracing into a clip that keeps the
frame, Threshold/Tolerance/Inset Frame preview, picking an alpha channel or a path saved in a
diff --git a/docs/pdf-layer-visibility.md b/docs/pdf-layer-visibility.md
index 48fbf1f5b..bfc95e668 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 exp
The 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. The IDML reader finds those choices too, but its legacy write pass cannot update the nested placements yet: embedded IDML still leaves them at the file's defaults, while external-only IDML rejects the active choice. Fixing that IDML write pass remains separate work (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. 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.