Skip to content

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

IDML: layers import and export in stacking order

aiongg699d89c0ce4a
diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs
index 919991eb0..c32d62674 100644
--- a/crates/idml/src/export.rs
+++ b/crates/idml/src/export.rs
@@ -346,7 +346,8 @@ impl<'a> Ex<'a> {
let n = format!("<?AID 001b?>TV {name}");
root.push(El::new("TextVariable").attr("Self", format!("dTextVariablen{n}")).attr("Name", &n).attr("VariableType", ty));
}
- for l in &d.layers {
+ // IDML lists layers back to front.
+ for l in d.layers.iter().rev() {
let color = match names::layer_color_out(l.color) {
Some(n) => p("LayerColor", "enumeration", n),
None => {
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index 3e1c17e28..b333f87ee 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -356,6 +356,8 @@ impl<'r> Importer<'r> {
self.layer_ids.insert(s.to_string(), id);
}
}
+ // IDML lists layers back to front; `Document::layers` is front to back.
+ self.layers.reverse();
if self.layers.is_empty() {
let id = LayerId(self.alloc());
self.layers.push(Layer {
diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs
index b636a2ac1..f64045047 100644
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@ -857,3 +857,38 @@ fn arabic_story_and_table_directions_survive_idml_export() {
assert_eq!(st.direction, designcraft_doc::TextDirection::RightToLeft);
assert_eq!(st.tables.values().next().unwrap().options.direction, designcraft_doc::TextDirection::RightToLeft);
}
+
+/// InDesign's designmap lists layers back to front; `Document::layers[0]` is the front layer.
+#[test]
+fn layers_import_and_export_in_stacking_order() {
+ let designmap = r#"<Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" Self="d">
+ <Layer Self="L1" Name="Background"/>
+ <Layer Self="L2" Name="Content"/>
+ <idPkg:Spread src="Spreads/Spread_s.xml"/>
+ </Document>"#;
+ let spread = r#"<idPkg:Spread xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
+ <Spread Self="s">
+ <Page Self="p" GeometricBounds="0 0 100 100" ItemTransform="1 0 0 1 0 0"/>
+ <Rectangle Self="r" ItemLayer="L1">
+ <Properties><PathGeometry><GeometryPathType PathOpen="false"><PathPointArray>
+ <PathPointType Anchor="0 0"/><PathPointType Anchor="0 100"/>
+ <PathPointType Anchor="100 100"/><PathPointType Anchor="100 0"/>
+ </PathPointArray></GeometryPathType></PathGeometry></Properties>
+ </Rectangle>
+ </Spread>
+ </idPkg:Spread>"#;
+ let d = import_idml(&zip_files(&[("designmap.xml", designmap), ("Spreads/Spread_s.xml", spread)])).unwrap();
+ let names = |d: &Document| d.layers.iter().map(|l| l.name.clone()).collect::<Vec<_>>();
+ assert_eq!(names(&d), ["Content", "Background"]);
+ assert_eq!(d.spreads[0].items[0].layer, d.layers[1].id, "the rectangle stays on Background");
+
+ let bytes = export_idml(&d);
+ let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes.as_slice())).unwrap();
+ let mut map = String::new();
+ std::io::Read::read_to_string(&mut zip.by_name("designmap.xml").unwrap(), &mut map).unwrap();
+ let (bg, content) = (map.find(r#"Name="Background""#).unwrap(), map.find(r#"Name="Content""#).unwrap());
+ assert!(bg < content, "designmap lists the back layer first");
+ let back = import_idml(&bytes).unwrap();
+ assert_eq!(names(&back), ["Content", "Background"]);
+ assert_eq!(back.spreads[0].items[0].layer, back.layers[1].id);
+}