From 5715e72de81a4827980e2c87a2bf83a1722bc0fb Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 13:35:35 +0500 Subject: [PATCH 1/9] IDML export: OverrideList pairs each parent item with its override InDesign reads a page's OverrideList two ids at a time: the parent item, then the item that overrides it. The exporter wrote the parent items alone, so InDesign took a parent item for the override and, on a page with an odd number of overrides, read past the end; it crashed opening the file. Each overridden parent item now pairs with its local copy on that page (Item::overrides), else a copy on the spread not yet paired; one whose override was deleted pairs with an id nothing else uses, as InDesign's own files do. Import keeps reading the parent items out of the list. Fixes #2896 --- crates/idml/src/export/spread.rs | 47 +++++++++++++++++++++- crates/idml/src/tests.rs | 67 ++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/crates/idml/src/export/spread.rs b/crates/idml/src/export/spread.rs index 0610980d1..2e9920933 100644 --- a/crates/idml/src/export/spread.rs +++ b/crates/idml/src/export/spread.rs @@ -64,10 +64,34 @@ impl<'a> Ex<'a> { }, ); let based_on = sp.parent.as_ref().and_then(|i| i.based_on); + // The spread's local copies of parent items: (parent item, copy, page the copy is on). + let mut copies = Vec::new(); + for it in &sp.items { + let page = sp.page_at_x(it.bounds().center().x); + collect_copies(it, page, &mut copies); + } + let mut used = vec![false; copies.len()]; for (i, pg) in sp.pages.iter().enumerate() { let name = if master { sp.parent.as_ref().map(|i| i.prefix.clone()).unwrap_or_default() } else { d.page_name(first_page + i) }; let applied = if master { based_on } else { pg.parent }; - let overrides: Vec = pg.overridden.iter().map(|i| uid(i.0)).collect(); + // InDesign reads OverrideList two ids at a time: the parent item, then the item that + // overrides it, and a list of parent items alone crashed it on open (#2896). The copy is + // the one on this page, else any on the spread not yet paired; a parent item whose + // override was deleted pairs with an id nothing else uses, as InDesign's own files do. + let mut overrides: Vec = Vec::with_capacity(pg.overridden.len() * 2); + for parent in &pg.overridden { + let free = |k: &usize| !used[*k] && copies[*k].0 == *parent; + let k = (0..copies.len()).find(|k| free(k) && copies[*k].2 == Some(i)).or_else(|| (0..copies.len()).find(free)); + let copy = match k { + Some(k) => { + used[k] = true; + uid(copies[k].1.0) + } + None => self.fresh(), + }; + overrides.push(uid(parent.0)); + overrides.push(copy); + } let pe = El::new("Page") .attr("Self", uid(pg.id.0)) .attr("Name", name) @@ -137,3 +161,24 @@ fn push_page_guides(d: &Document, sp: &Spread, pg: &designcraft_doc::Page, pe: & pe.push(ge); } } + +/// The items in `it` (and `it` itself) that override a parent item: (parent item, item, `page`). +/// Groups nested past 64 levels (only a hostile file has them) are not searched. +fn collect_copies(it: &designcraft_doc::Item, page: Option, out: &mut Vec<(designcraft_doc::ItemId, designcraft_doc::ItemId, Option)>) { + fn walk( + it: &designcraft_doc::Item, + page: Option, + depth: usize, + out: &mut Vec<(designcraft_doc::ItemId, designcraft_doc::ItemId, Option)>, + ) { + if let Some(parent) = it.overrides { + out.push((parent, it.id, page)); + } + if depth < 64 { + for child in it.children() { + walk(child, page, depth + 1, out); + } + } + } + walk(it, page, 0, out); +} diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index e4a19619d..cef0bad6c 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1137,6 +1137,73 @@ fn round_trips_overridden_parent_items() { assert_eq!(shown_parent + local.len(), 1); } +/// #2896: InDesign reads a page's `OverrideList` two ids at a time, the parent item and then the +/// item that overrides it; a list of parent items alone crashed InDesign when it opened the file. +/// Each overridden parent item is paired with its local copy on that page, and one whose +/// override was deleted with an id nothing else in the package uses. +#[test] +fn override_lists_pair_each_parent_item_with_its_override() { + // Page 1 on a spread of its own, pages 2 and 3 facing. + let mut d = Document::new(&NewDocument { pages: 3, facing_pages: true, ..Default::default() }); + let lid = d.default_layer(); + let (ppi, ppage) = d.parent_page_for(0).unwrap(); + let px = d.parents[ppi].pages[ppage].x; + let parent = |d: &mut Document, y: f64| { + let id = designcraft_doc::ItemId(d.alloc()); + let item = designcraft_doc::Item::new(id, lid, Shape::Rectangle, shapes::rectangle(Rect::new(px + 40.0, y, px + 200.0, y + 60.0))); + d.insert_item(SpreadRef::Parent(ppi), item.clone(), None).unwrap(); + item + }; + let (kept, deleted) = (parent(&mut d, 40.0), parent(&mut d, 200.0)); + // Each page overrides the first parent item with a copy of its own; page 1 also overrides the + // second and deleted its copy. + let mut copies = Vec::new(); + for (si, pi) in [(0, 0), (1, 0), (1, 1)] { + let mut copy = kept.clone(); + copy.id = designcraft_doc::ItemId(d.alloc()); + copy.xf = designcraft_geom::Affine::translate((d.spreads[si].pages[pi].x - px, 0.0)) * copy.xf; + copy.overrides = Some(kept.id); + copies.push(format!("u{:x}", copy.id.0)); + d.insert_item(SpreadRef::Doc(si), copy, None).unwrap(); + std::sync::Arc::make_mut(&mut d.spreads[si]).pages[pi].overridden.push(kept.id); + } + std::sync::Arc::make_mut(&mut d.spreads[0]).pages[0].overridden.push(deleted.id); + + let bytes = export_idml(&d); + let (masters, spreads) = (zip_text(&bytes, "MasterSpreads/"), zip_text(&bytes, "Spreads/")); + let lists: Vec> = spreads + .split(">(), [4, 2, 2], "pairs: {lists:?}"); + // The element whose Self is `id` (its start tag). + let element = |xml: &str, id: &str| xml.split('<').find(|e| e.contains(&format!("Self=\"{id}\""))).map(str::to_owned); + for list in &lists { + for pair in list.chunks(2) { + assert!(element(&masters, &pair[0]).is_some(), "{} is a parent item: {list:?}", pair[0]); + } + } + // Each page pairs the first parent item with its own copy, which names it as the item it + // overrides. + for ((list, copy), page) in lists.iter().zip(&copies).zip(1..) { + assert_eq!(&list[1], copy, "page {page}: {lists:?}"); + let e = element(&spreads, copy).unwrap_or_else(|| panic!("page {page}: {copy} is an item on the spread")); + assert!(e.contains(&format!("OverriddenPageItem=\"{}\"", list[0])), "page {page}: {e}"); + } + // The deleted override's id is used nowhere else in the package. + let all = zip_text(&bytes, ""); + let orphan = &lists[0][3]; + assert_eq!(all.matches(orphan.as_str()).count(), 1, "{orphan} appears only in the list"); + + // Read back, each page lists its parent items, as before. + let back = import_idml(&bytes).unwrap(); + let ids: Vec<_> = back.parents[ppi].items.iter().map(|i| i.id).collect(); + assert_eq!(back.spreads[0].pages[0].overridden, ids); + assert_eq!(back.spreads[1].pages[0].overridden, ids[..1]); + assert_eq!(back.spreads[1].pages[1].overridden, ids[..1]); +} + #[test] fn uri_and_base64_helpers() { assert_eq!(import::uri_to_path("file:/a%20b/c.png"), "/a b/c.png"); From e55641031631c1ee0f54615225bbf6bc57f54d72 Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:25:44 +0000 Subject: [PATCH 2/9] Apply review fixes - crates/idml/src/export/spread.rs --- crates/idml/src/export/spread.rs | 33 ++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/idml/src/export/spread.rs b/crates/idml/src/export/spread.rs index 2e9920933..7d9ff57af 100644 --- a/crates/idml/src/export/spread.rs +++ b/crates/idml/src/export/spread.rs @@ -71,22 +71,39 @@ impl<'a> Ex<'a> { collect_copies(it, page, &mut copies); } let mut used = vec![false; copies.len()]; + // Each page's own copies are paired first, for every page, so one page's spread-wide fallback + // can't take the copy another page overrides with. + let mut own: Vec>> = Vec::with_capacity(sp.pages.len()); + for (i, pg) in sp.pages.iter().enumerate() { + let mut row = Vec::with_capacity(pg.overridden.len()); + for parent in &pg.overridden { + let k = (0..copies.len()).find(|k| !used[*k] && copies[*k].0 == *parent && copies[*k].2 == Some(i)); + if let Some(k) = k { + used[k] = true; + } + row.push(k); + } + own.push(row); + } for (i, pg) in sp.pages.iter().enumerate() { let name = if master { sp.parent.as_ref().map(|i| i.prefix.clone()).unwrap_or_default() } else { d.page_name(first_page + i) }; let applied = if master { based_on } else { pg.parent }; // InDesign reads OverrideList two ids at a time: the parent item, then the item that // overrides it, and a list of parent items alone crashed it on open (#2896). The copy is - // the one on this page, else any on the spread not yet paired; a parent item whose - // override was deleted pairs with an id nothing else uses, as InDesign's own files do. + // the one on this page paired in the first pass, else any on the spread not yet paired; a + // parent item whose override was deleted pairs with an id nothing else uses, as + // InDesign's own files do. let mut overrides: Vec = Vec::with_capacity(pg.overridden.len() * 2); - for parent in &pg.overridden { - let free = |k: &usize| !used[*k] && copies[*k].0 == *parent; - let k = (0..copies.len()).find(|k| free(k) && copies[*k].2 == Some(i)).or_else(|| (0..copies.len()).find(free)); - let copy = match k { - Some(k) => { + for (j, parent) in pg.overridden.iter().enumerate() { + let k = own.get(i).and_then(|r| r.get(j)).copied().flatten().or_else(|| { + let k = (0..copies.len()).find(|k| !used[*k] && copies[*k].0 == *parent); + if let Some(k) = k { used[k] = true; - uid(copies[k].1.0) } + k + }); + let copy = match k.and_then(|k| copies.get(k)) { + Some(c) => uid(c.1.0), None => self.fresh(), }; overrides.push(uid(parent.0)); From 0a6a6ba0f972864685faadbeada2315fd5c98156 Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 15:08:05 +0500 Subject: [PATCH 3/9] IDML export: a nested override copy is on the page under its own centre, at any depth collect_copies walks the spread with its own stack, so a copy nested deeper than 64 groups (which the writer still writes) is paired, and gives each copy the page under its own centre through the groups it sits in, not its top-level group's. Bounds are asked for copies only. The pairing test lists page 3's copy before page 2's, so it fails if same-page matching goes. --- crates/idml/src/export/spread.rs | 31 +++++--------- crates/idml/src/tests.rs | 71 ++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 23 deletions(-) diff --git a/crates/idml/src/export/spread.rs b/crates/idml/src/export/spread.rs index 7d9ff57af..229030b41 100644 --- a/crates/idml/src/export/spread.rs +++ b/crates/idml/src/export/spread.rs @@ -66,10 +66,7 @@ impl<'a> Ex<'a> { let based_on = sp.parent.as_ref().and_then(|i| i.based_on); // The spread's local copies of parent items: (parent item, copy, page the copy is on). let mut copies = Vec::new(); - for it in &sp.items { - let page = sp.page_at_x(it.bounds().center().x); - collect_copies(it, page, &mut copies); - } + collect_copies(sp, &mut copies); let mut used = vec![false; copies.len()]; // Each page's own copies are paired first, for every page, so one page's spread-wide fallback // can't take the copy another page overrides with. @@ -179,23 +176,17 @@ fn push_page_guides(d: &Document, sp: &Spread, pg: &designcraft_doc::Page, pe: & } } -/// The items in `it` (and `it` itself) that override a parent item: (parent item, item, `page`). -/// Groups nested past 64 levels (only a hostile file has them) are not searched. -fn collect_copies(it: &designcraft_doc::Item, page: Option, out: &mut Vec<(designcraft_doc::ItemId, designcraft_doc::ItemId, Option)>) { - fn walk( - it: &designcraft_doc::Item, - page: Option, - depth: usize, - out: &mut Vec<(designcraft_doc::ItemId, designcraft_doc::ItemId, Option)>, - ) { +/// The items on the spread that override a parent item, in the order they are written: (parent +/// item, item, page). A copy's page is the one under its own centre, a nested copy's through the +/// groups it sits in. The walk keeps its own stack, so it reaches every copy the writer writes, +/// however deep, and asks for the bounds of copies only. +fn collect_copies(sp: &Spread, out: &mut Vec<(designcraft_doc::ItemId, designcraft_doc::ItemId, Option)>) { + let mut stack: Vec<(&designcraft_doc::Item, Affine)> = sp.items.iter().rev().map(|it| (&**it, Affine::IDENTITY)).collect(); + while let Some((it, outer)) = stack.pop() { if let Some(parent) = it.overrides { - out.push((parent, it.id, page)); - } - if depth < 64 { - for child in it.children() { - walk(child, page, depth + 1, out); - } + out.push((parent, it.id, sp.page_at_x((outer * it.bounds().center()).x))); } + let inner = outer * it.xf; + stack.extend(it.children().iter().rev().map(|c| (&**c, inner))); } - walk(it, page, 0, out); } diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index cef0bad6c..18fa9798e 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1157,13 +1157,15 @@ fn override_lists_pair_each_parent_item_with_its_override() { let (kept, deleted) = (parent(&mut d, 40.0), parent(&mut d, 200.0)); // Each page overrides the first parent item with a copy of its own; page 1 also overrides the // second and deleted its copy. - let mut copies = Vec::new(); - for (si, pi) in [(0, 0), (1, 0), (1, 1)] { + // The facing spread lists page 3's copy before page 2's, so a page that took the first copy + // on the spread instead of its own would get the other page's. + let mut copies = vec![String::new(); 3]; + for (si, pi) in [(0, 0), (1, 1), (1, 0)] { let mut copy = kept.clone(); copy.id = designcraft_doc::ItemId(d.alloc()); copy.xf = designcraft_geom::Affine::translate((d.spreads[si].pages[pi].x - px, 0.0)) * copy.xf; copy.overrides = Some(kept.id); - copies.push(format!("u{:x}", copy.id.0)); + copies[si + pi] = format!("u{:x}", copy.id.0); d.insert_item(SpreadRef::Doc(si), copy, None).unwrap(); std::sync::Arc::make_mut(&mut d.spreads[si]).pages[pi].overridden.push(kept.id); } @@ -1204,6 +1206,69 @@ fn override_lists_pair_each_parent_item_with_its_override() { assert_eq!(back.spreads[1].pages[1].overridden, ids[..1]); } +/// #2896: a copy inside a group is paired on the page under its own centre, not the group's, and a +/// copy nested deeper than 64 groups is still paired with its parent item. +#[test] +fn override_lists_pair_nested_copies_on_their_own_pages() { + use designcraft_doc::{Content, Item, ItemId}; + use designcraft_geom::Affine; + use std::sync::Arc; + let mut d = Document::new(&NewDocument { pages: 3, facing_pages: true, ..Default::default() }); + let lid = d.default_layer(); + let (ppi, _) = d.parent_page_for(0).unwrap(); + let parent = |d: &mut Document, y: f64| { + let item = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(Rect::new(40.0, y, 200.0, y + 60.0))); + d.insert_item(SpreadRef::Parent(ppi), item.clone(), None).unwrap(); + item + }; + let (kept, deep) = (parent(&mut d, 40.0), parent(&mut d, 200.0)); + let copy = |d: &mut Document, of: &Item, r: Rect| { + let mut c = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(r)); + c.overrides = Some(of.id); + c + }; + let group = |d: &mut Document, items: Vec, xf: Affine| { + let mut g = Item::new(ItemId(d.alloc()), lid, Shape::Group, Default::default()); + g.content = Content::Group { items: items.into_iter().map(Arc::new).collect() }; + g.xf = xf; + g + }; + let (left, right) = (d.spreads[1].pages[0].clone(), d.spreads[1].pages[1].clone()); + assert!(left.x + left.width <= right.x + 1e-9, "page 2 is left of page 3"); + // One group, moved onto page 2, holds page 3's small copy first and page 2's wide one, so its + // centre is on page 2. + let to_left = Affine::translate((left.x, 0.0)); + let on_right = copy(&mut d, &kept, Rect::new(right.x - left.x + 10.0, 40.0, right.x - left.x + 40.0, 80.0)); + let on_left = copy(&mut d, &kept, Rect::new(10.0, 40.0, left.width - 10.0, 80.0)); + let (right_id, left_id) = (format!("u{:x}", on_right.id.0), format!("u{:x}", on_left.id.0)); + let g = group(&mut d, vec![on_right, on_left], to_left); + assert!(g.bounds().center().x < left.x + left.width, "the group's centre is on page 2"); + d.insert_item(SpreadRef::Doc(1), g, None).unwrap(); + // Page 3 also overrides the second parent item with a copy 70 groups deep. + let mut nested = copy(&mut d, &deep, Rect::new(right.x + 10.0, 200.0, right.x + 100.0, 260.0)); + let deep_id = format!("u{:x}", nested.id.0); + for _ in 0..70 { + nested = group(&mut d, vec![nested], Affine::IDENTITY); + } + d.insert_item(SpreadRef::Doc(1), nested, None).unwrap(); + let sp = Arc::make_mut(&mut d.spreads[1]); + sp.pages[0].overridden.push(kept.id); + sp.pages[1].overridden.extend([kept.id, deep.id]); + + let spreads = zip_text(&export_idml(&d), "Spreads/"); + // Each page's OverrideList, from its start tag. + let lists: Vec> = spreads + .split("').next().unwrap_or_default(); + tag.split("OverrideList=\"").nth(1).and_then(|l| l.split('"').next()).unwrap_or_default().split_whitespace().map(str::to_owned).collect() + }) + .collect(); + let (k, dp) = (format!("u{:x}", kept.id.0), format!("u{:x}", deep.id.0)); + assert_eq!(lists, [vec![], vec![k.clone(), left_id], vec![k, right_id, dp, deep_id]]); +} + #[test] fn uri_and_base64_helpers() { assert_eq!(import::uri_to_path("file:/a%20b/c.png"), "/a b/c.png"); From aee88fc9d573e7e8a08ade92ed6ae8929806180c Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 15:10:02 +0500 Subject: [PATCH 4/9] IDML test: the group is offset, so a nested copy's page must go through it --- crates/idml/src/tests.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 18fa9798e..46f515a45 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1235,11 +1235,13 @@ fn override_lists_pair_nested_copies_on_their_own_pages() { }; let (left, right) = (d.spreads[1].pages[0].clone(), d.spreads[1].pages[1].clone()); assert!(left.x + left.width <= right.x + 1e-9, "page 2 is left of page 3"); - // One group, moved onto page 2, holds page 3's small copy first and page 2's wide one, so its - // centre is on page 2. - let to_left = Affine::translate((left.x, 0.0)); - let on_right = copy(&mut d, &kept, Rect::new(right.x - left.x + 10.0, 40.0, right.x - left.x + 40.0, 80.0)); - let on_left = copy(&mut d, &kept, Rect::new(10.0, 40.0, left.width - 10.0, 80.0)); + // One group holds page 3's small copy first and page 2's wide one, so its centre is on page 2. + // The group is moved 300 pt right and its items drawn 300 pt left of where they show, so a page + // read from an item's own coordinates, not through its group, would be wrong. + let shift = 300.0; + let to_left = Affine::translate((left.x + shift, 0.0)); + let on_right = copy(&mut d, &kept, Rect::new(right.x - left.x - shift + 10.0, 40.0, right.x - left.x - shift + 40.0, 80.0)); + let on_left = copy(&mut d, &kept, Rect::new(10.0 - shift, 40.0, left.width - 10.0 - shift, 80.0)); let (right_id, left_id) = (format!("u{:x}", on_right.id.0), format!("u{:x}", on_left.id.0)); let g = group(&mut d, vec![on_right, on_left], to_left); assert!(g.bounds().center().x < left.x + left.width, "the group's centre is on page 2"); From a2c52510b5037f6602c763bac5b0a2ba3e26b1c8 Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 15:50:29 +0500 Subject: [PATCH 5/9] IDML export: a copy's page is measured without recursion collect_copies measured an overriding copy with Item::bounds, which recurses through a group's children. bounds_under gives the same bounds with a stack of its own, so a copy that is a deeply nested group can't overflow the stack (review). --- crates/idml/src/export/spread.rs | 73 +++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/crates/idml/src/export/spread.rs b/crates/idml/src/export/spread.rs index 229030b41..d011be3ba 100644 --- a/crates/idml/src/export/spread.rs +++ b/crates/idml/src/export/spread.rs @@ -184,9 +184,80 @@ fn collect_copies(sp: &Spread, out: &mut Vec<(designcraft_doc::ItemId, designcra let mut stack: Vec<(&designcraft_doc::Item, Affine)> = sp.items.iter().rev().map(|it| (&**it, Affine::IDENTITY)).collect(); while let Some((it, outer)) = stack.pop() { if let Some(parent) = it.overrides { - out.push((parent, it.id, sp.page_at_x((outer * it.bounds().center()).x))); + let centre = bounds_under(it, outer).map_or_else(|| outer * designcraft_geom::Point::ZERO, |r| r.center()); + out.push((parent, it.id, sp.page_at_x(centre.x))); } let inner = outer * it.xf; stack.extend(it.children().iter().rev().map(|c| (&**c, inner))); } } + +/// `it`'s bounds under `outer`, as `Item::bounds` measures them (a group's are its children's +/// outlines under the full transform), with a stack of its own: a copy that is a group can hold +/// groups nested deeper than a recursive walk should go (#2940 review). `None` for a group with +/// nothing in it. +fn bounds_under(it: &designcraft_doc::Item, outer: Affine) -> Option { + let mut stack = vec![(it, outer)]; + let mut out: Option = None; + while let Some((it, outer)) = stack.pop() { + let xf = outer * it.xf; + if it.shape == designcraft_doc::Shape::Group + && let designcraft_doc::Content::Group { items } = &it.content + { + stack.extend(items.iter().map(|c| (&**c, xf))); + continue; + } + if let Some(r) = it.path.transformed(xf).bounds() { + out = Some(out.map_or(r, |o| o.union(r))); + } + } + out +} + +#[cfg(test)] +mod bounds_tests { + use designcraft_doc::{Content, Item, ItemId, LayerId, Shape}; + use designcraft_geom::{Affine, Rect, shapes}; + use std::sync::Arc; + + fn rect(id: u64, r: Rect) -> Item { + Item::new(ItemId(id), LayerId(0), Shape::Rectangle, shapes::rectangle(r)) + } + + fn group(id: u64, items: Vec, xf: Affine) -> Item { + let mut g = Item::new(ItemId(id), LayerId(0), Shape::Group, Default::default()); + g.content = Content::Group { items: items.into_iter().map(Arc::new).collect() }; + g.xf = xf; + g + } + + /// The same bounds as `Item::bounds`, for a plain item, nested and turned groups, a frame + /// with items pasted into it (its own outline) and an empty group. + #[test] + fn bounds_match_the_items_own() { + let inner = group(3, vec![rect(1, Rect::new(0.0, 0.0, 10.0, 20.0)), rect(2, Rect::new(30.0, 5.0, 40.0, 8.0))], Affine::rotate(0.4)); + let outer = group(4, vec![inner.clone(), rect(5, Rect::new(-5.0, -5.0, 0.0, 0.0))], Affine::translate((100.0, 50.0)) * Affine::scale(2.0)); + let mut frame = rect(6, Rect::new(0.0, 0.0, 50.0, 50.0)); + frame.content = Content::Group { items: vec![Arc::new(rect(7, Rect::new(-100.0, -100.0, 200.0, 200.0)))] }; + for it in [rect(8, Rect::new(1.0, 2.0, 3.0, 4.0)), inner, outer, frame] { + let (want, got) = (it.bounds(), super::bounds_under(&it, Affine::IDENTITY).unwrap()); + for (a, b) in [(want.x0, got.x0), (want.y0, got.y0), (want.x1, got.x1), (want.y1, got.y1)] { + assert!((a - b).abs() < 1e-9, "{want:?} vs {got:?}"); + } + } + assert_eq!(super::bounds_under(&group(9, vec![], Affine::IDENTITY), Affine::IDENTITY), None); + } + + /// A group 100,000 deep is measured without recursion. It is leaked rather than dropped, + /// since dropping it recurses. + #[test] + fn a_deep_group_is_measured_without_recursion() { + let mut it = rect(0, Rect::new(10.0, 20.0, 30.0, 40.0)); + for i in 1..=100_000 { + it = group(i, vec![it], Affine::translate((1.0, 0.0))); + } + let r = super::bounds_under(&it, Affine::IDENTITY); + std::mem::forget(it); + assert_eq!(r, Some(Rect::new(100_010.0, 20.0, 100_030.0, 40.0))); + } +} From cef75b8c0b089e773414b62560c5851331882c14 Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 16:32:54 +0500 Subject: [PATCH 6/9] IDML export: pair override copies from queues, not a search per entry Pairing searched every copy for every OverrideList entry, which is quadratic: a page with 20,000 overrides took 200 million candidate checks (review). The copies are now queued in writer order by (parent item, page) for the same-page pass and by parent item for the fallback, which drops copies paired since, so each copy is looked at once. The pairs are the same. --- crates/idml/src/export/spread.rs | 23 ++++++++++++++++++----- crates/idml/src/tests.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/idml/src/export/spread.rs b/crates/idml/src/export/spread.rs index d011be3ba..2acc091fb 100644 --- a/crates/idml/src/export/spread.rs +++ b/crates/idml/src/export/spread.rs @@ -68,13 +68,21 @@ impl<'a> Ex<'a> { let mut copies = Vec::new(); collect_copies(sp, &mut copies); let mut used = vec![false; copies.len()]; + // The copies in writer order, by (parent item, page) and by parent item, so pairing takes + // each copy from a queue instead of searching every copy for every entry (review). + let mut by_page: std::collections::HashMap<_, std::collections::VecDeque> = std::collections::HashMap::new(); + let mut by_parent: std::collections::HashMap<_, std::collections::VecDeque> = std::collections::HashMap::new(); + for (k, c) in copies.iter().enumerate() { + by_page.entry((c.0, c.2)).or_default().push_back(k); + by_parent.entry(c.0).or_default().push_back(k); + } // Each page's own copies are paired first, for every page, so one page's spread-wide fallback // can't take the copy another page overrides with. let mut own: Vec>> = Vec::with_capacity(sp.pages.len()); for (i, pg) in sp.pages.iter().enumerate() { let mut row = Vec::with_capacity(pg.overridden.len()); for parent in &pg.overridden { - let k = (0..copies.len()).find(|k| !used[*k] && copies[*k].0 == *parent && copies[*k].2 == Some(i)); + let k = by_page.get_mut(&(*parent, Some(i))).and_then(|q| q.pop_front()); if let Some(k) = k { used[k] = true; } @@ -93,11 +101,16 @@ impl<'a> Ex<'a> { let mut overrides: Vec = Vec::with_capacity(pg.overridden.len() * 2); for (j, parent) in pg.overridden.iter().enumerate() { let k = own.get(i).and_then(|r| r.get(j)).copied().flatten().or_else(|| { - let k = (0..copies.len()).find(|k| !used[*k] && copies[*k].0 == *parent); - if let Some(k) = k { - used[k] = true; + // The first copy of this parent item not yet paired: the queue drops the ones + // paired since, so each copy is looked at once. + let q = by_parent.get_mut(parent)?; + while let Some(k) = q.pop_front() { + if !used[k] { + used[k] = true; + return Some(k); + } } - k + None }); let copy = match k.and_then(|k| copies.get(k)) { Some(c) => uid(c.1.0), diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 46f515a45..73e30b162 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1271,6 +1271,35 @@ fn override_lists_pair_nested_copies_on_their_own_pages() { assert_eq!(lists, [vec![], vec![k.clone(), left_id], vec![k, right_id, dp, deep_id]]); } +/// A page overriding 20,000 parent items, each with its copy: every parent item pairs with its own +/// copy, in order. Pairing takes each copy from a queue instead of searching every copy for every +/// entry (#2940 review), so this exports in about a second. +#[test] +fn many_overrides_pair_in_linear_time() { + use designcraft_doc::{Item, ItemId}; + let mut d = Document::new(&NewDocument { pages: 1, ..Default::default() }); + let lid = d.default_layer(); + let (ppi, _) = d.parent_page_for(0).unwrap(); + let n = 20_000; + let mut copies = Vec::with_capacity(n); + for i in 0..n { + let y = (i % 500) as f64; + let parent = Item::new(ItemId(d.alloc()), lid, Shape::Rectangle, shapes::rectangle(Rect::new(40.0, y, 60.0, y + 1.0))); + let mut copy = parent.clone(); + copy.id = ItemId(d.alloc()); + copy.overrides = Some(parent.id); + copies.push(format!("u{:x}", copy.id.0)); + std::sync::Arc::make_mut(&mut d.spreads[0]).pages[0].overridden.push(parent.id); + std::sync::Arc::make_mut(&mut d.parents[ppi]).items.push(std::sync::Arc::new(parent)); + std::sync::Arc::make_mut(&mut d.spreads[0]).items.push(std::sync::Arc::new(copy)); + } + let spreads = zip_text(&export_idml(&d), "Spreads/"); + let list = spreads.split("OverrideList=\"").nth(1).and_then(|l| l.split('"').next()).unwrap_or_default(); + let ids: Vec<&str> = list.split_whitespace().collect(); + assert_eq!(ids.len(), 2 * n); + assert!(ids.chunks(2).zip(&copies).all(|(pair, copy)| pair[1] == copy), "each parent item pairs with its own copy"); +} + #[test] fn uri_and_base64_helpers() { assert_eq!(import::uri_to_path("file:/a%20b/c.png"), "/a b/c.png"); From 8d9d9329849790c9ebf10354a7cade2809d39c02 Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 16:33:43 +0500 Subject: [PATCH 7/9] IDML export: keep the per-entry search; the 20,000-override test stays Measured on the 20,000-override test, the search took 1.02 s against 0.92 s for the queues (debug build), so the queues aren't worth their code. The test stays as a check that every parent item pairs with its own copy at that size. --- crates/idml/src/export/spread.rs | 23 +++++------------------ crates/idml/src/tests.rs | 4 ++-- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/crates/idml/src/export/spread.rs b/crates/idml/src/export/spread.rs index 2acc091fb..d011be3ba 100644 --- a/crates/idml/src/export/spread.rs +++ b/crates/idml/src/export/spread.rs @@ -68,21 +68,13 @@ impl<'a> Ex<'a> { let mut copies = Vec::new(); collect_copies(sp, &mut copies); let mut used = vec![false; copies.len()]; - // The copies in writer order, by (parent item, page) and by parent item, so pairing takes - // each copy from a queue instead of searching every copy for every entry (review). - let mut by_page: std::collections::HashMap<_, std::collections::VecDeque> = std::collections::HashMap::new(); - let mut by_parent: std::collections::HashMap<_, std::collections::VecDeque> = std::collections::HashMap::new(); - for (k, c) in copies.iter().enumerate() { - by_page.entry((c.0, c.2)).or_default().push_back(k); - by_parent.entry(c.0).or_default().push_back(k); - } // Each page's own copies are paired first, for every page, so one page's spread-wide fallback // can't take the copy another page overrides with. let mut own: Vec>> = Vec::with_capacity(sp.pages.len()); for (i, pg) in sp.pages.iter().enumerate() { let mut row = Vec::with_capacity(pg.overridden.len()); for parent in &pg.overridden { - let k = by_page.get_mut(&(*parent, Some(i))).and_then(|q| q.pop_front()); + let k = (0..copies.len()).find(|k| !used[*k] && copies[*k].0 == *parent && copies[*k].2 == Some(i)); if let Some(k) = k { used[k] = true; } @@ -101,16 +93,11 @@ impl<'a> Ex<'a> { let mut overrides: Vec = Vec::with_capacity(pg.overridden.len() * 2); for (j, parent) in pg.overridden.iter().enumerate() { let k = own.get(i).and_then(|r| r.get(j)).copied().flatten().or_else(|| { - // The first copy of this parent item not yet paired: the queue drops the ones - // paired since, so each copy is looked at once. - let q = by_parent.get_mut(parent)?; - while let Some(k) = q.pop_front() { - if !used[k] { - used[k] = true; - return Some(k); - } + let k = (0..copies.len()).find(|k| !used[*k] && copies[*k].0 == *parent); + if let Some(k) = k { + used[k] = true; } - None + k }); let copy = match k.and_then(|k| copies.get(k)) { Some(c) => uid(c.1.0), diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 73e30b162..2550852aa 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1272,8 +1272,8 @@ fn override_lists_pair_nested_copies_on_their_own_pages() { } /// A page overriding 20,000 parent items, each with its copy: every parent item pairs with its own -/// copy, in order. Pairing takes each copy from a queue instead of searching every copy for every -/// entry (#2940 review), so this exports in about a second. +/// copy, in order, and the export takes about a second (#2940 review asked about the cost of the +/// search for each entry at this size). #[test] fn many_overrides_pair_in_linear_time() { use designcraft_doc::{Item, ItemId}; From bfd0cb6fc7129a5f4070772ab9c4540f7c03baa1 Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 16:34:16 +0500 Subject: [PATCH 8/9] IDML test: name the 20,000-override test for what it checks --- crates/idml/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 2550852aa..e66e2036b 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1275,7 +1275,7 @@ fn override_lists_pair_nested_copies_on_their_own_pages() { /// copy, in order, and the export takes about a second (#2940 review asked about the cost of the /// search for each entry at this size). #[test] -fn many_overrides_pair_in_linear_time() { +fn many_overrides_pair_with_their_own_copies() { use designcraft_doc::{Item, ItemId}; let mut d = Document::new(&NewDocument { pages: 1, ..Default::default() }); let lid = d.default_layer(); From 381aac8c38397f0282419399ee9dd210f2fd88d4 Mon Sep 17 00:00:00 2001 From: Hamza Date: Wed, 18 Nov 2026 18:52:51 +0500 Subject: [PATCH 9/9] IDML test: the 20,000-override test checks both ids of every pair As autocraft-bot asked: a swapped parent id with the copy ids in place now fails too. --- crates/idml/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index e66e2036b..4e083add1 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -1288,7 +1288,7 @@ fn many_overrides_pair_with_their_own_copies() { let mut copy = parent.clone(); copy.id = ItemId(d.alloc()); copy.overrides = Some(parent.id); - copies.push(format!("u{:x}", copy.id.0)); + copies.push((format!("u{:x}", parent.id.0), format!("u{:x}", copy.id.0))); std::sync::Arc::make_mut(&mut d.spreads[0]).pages[0].overridden.push(parent.id); std::sync::Arc::make_mut(&mut d.parents[ppi]).items.push(std::sync::Arc::new(parent)); std::sync::Arc::make_mut(&mut d.spreads[0]).items.push(std::sync::Arc::new(copy)); @@ -1297,7 +1297,7 @@ fn many_overrides_pair_with_their_own_copies() { let list = spreads.split("OverrideList=\"").nth(1).and_then(|l| l.split('"').next()).unwrap_or_default(); let ids: Vec<&str> = list.split_whitespace().collect(); assert_eq!(ids.len(), 2 * n); - assert!(ids.chunks(2).zip(&copies).all(|(pair, copy)| pair[1] == copy), "each parent item pairs with its own copy"); + assert!(ids.chunks(2).zip(&copies).all(|(pair, (p, c))| pair[0] == p && pair[1] == c), "each parent item pairs with its own copy"); } #[test]