From 6785e8df53c5c5694dc1c1f0e9ac797f43942f36 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 12 Nov 2026 09:48:38 -0800 Subject: [PATCH] .cdr import: a page that shares no layer puts its layers on top A page whose bottom layer is new now goes below the lowest layer the rest of the page shares with earlier pages, and on top when it shares none, so the layers keep the order they were first seen in. --- crates/engine/src/cmd/fileio/cdr.rs | 26 +++++++++++++++++++++-- crates/engine/src/cmd/fileio/tests_cdr.rs | 9 +++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/fileio/cdr.rs b/crates/engine/src/cmd/fileio/cdr.rs index f483f2f0a6..394220f194 100644 --- a/crates/engine/src/cmd/fileio/cdr.rs +++ b/crates/engine/src/cmd/fileio/cdr.rs @@ -171,7 +171,7 @@ impl shapes::Builder { // The layer this page's last layer went to: the next one goes above it. let mut below: Option = None; // The file lists layers front to back. - for l in page.layers.iter().rev() { + for (j, l) in page.layers.iter().rev().enumerate() { self.guides(&l.guides, p, artboard); if l.kind != LayerKind::Drawing && l.kind != LayerKind::Desktop { continue; @@ -193,7 +193,29 @@ impl shapes::Builder { } None if layers.len() >= MAX_LAYERS => self.notes.push(Warning::TooDeep), None => { - let at = below.map_or(pages_from, |b| b + 1); + // The page's bottom layer goes below the lowest layer that the rest of the + // page shares with earlier pages, so the page keeps its own order. When the + // page shares none, it goes on top, in the order the layers were first seen. + let at = match below { + Some(b) => b + 1, + None => { + let mut counts = seen.clone(); + page.layers + .iter() + .rev() + .skip(j + 1) + .filter(|l| l.kind == LayerKind::Drawing || l.kind == LayerKind::Desktop) + .filter_map(|l| { + let n = counts.entry(l.name.as_str()).or_insert(0); + let key = (l.name.clone(), *n); + *n += 1; + by_name.get(&key).and_then(|is| is.first().copied()) + }) + .min() + .unwrap_or(layers.len()) + .max(pages_from) + } + }; if at < layers.len() { for i in by_name.values_mut().flatten().filter(|i| **i >= at) { *i += 1; diff --git a/crates/engine/src/cmd/fileio/tests_cdr.rs b/crates/engine/src/cmd/fileio/tests_cdr.rs index 353c6ed9da..6cd91c3204 100644 --- a/crates/engine/src/cmd/fileio/tests_cdr.rs +++ b/crates/engine/src/cmd/fileio/tests_cdr.rs @@ -263,15 +263,18 @@ fn layers_of_one_name_on_a_page_stay_apart_and_pages_share_them() { /// Pages that stack layers of the same names in another order keep their own order: the later /// page gets a new layer rather than art under a layer it stacks above. A layer a later page adds -/// below the shared ones goes below them. +/// below the shared ones goes below them, and a page that shares no layer puts its layers on top. #[test] fn each_page_keeps_its_own_layer_order() { let v = V(1100); let rect = || synth::object(v, 1, &[(30, [v.coord(5.0), v.coord(5.0), v.coord(0.0), v.coord(0.0), v.coord(0.0), v.coord(0.0)].concat())], None); let page = |names: &[&str]| synth::page(false, names.iter().map(|n| synth::layer(v, [0, 0, 0, 0x98], n, vec![rect()])).collect()); // Front to back on each page. - for (second, want, counts) in [(&["A", "B"][..], vec!["A", "B", "A"], vec![1, 2, 1]), (&["B", "A", "C"][..], vec!["C", "A", "B"], vec![1, 2, 2])] - { + for (second, want, counts) in [ + (&["A", "B"][..], vec!["A", "B", "A"], vec![1, 2, 1]), + (&["B", "A", "C"][..], vec!["C", "A", "B"], vec![1, 2, 2]), + (&["D", "C"][..], vec!["A", "B", "C", "D"], vec![1, 1, 1, 1]), + ] { let file = synth::riff(1100, &[chunk(b"vrsn", 1100u16.to_le_bytes().to_vec()), page(&["B", "A"]), page(second)]); let (doc, _) = super::cdr::import(&file).unwrap(); let names: Vec = doc.layers.iter().map(|l| l.name.clone().unwrap_or_default()).collect();