.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.
This commit is contained in:
dotCooCoo
2026-11-12 09:48:38 -08:00
parent 0e146cc363
commit 6785e8df53
2 changed files with 30 additions and 5 deletions
+24 -2
View File
@@ -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<usize> = 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;
+6 -3
View File
@@ -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<String> = doc.layers.iter().map(|l| l.name.clone().unwrap_or_default()).collect();