From adf0e57132292d1fef573f4d675e1759bb8491e4 Mon Sep 17 00:00:00 2001 From: Youssef Azriouil Date: Thu, 15 Oct 2026 08:39:24 +0200 Subject: [PATCH 1/4] IDML import: style names ending in a space, layers of group items A BasedOn reference to a style whose name ends in a space was trimmed before it was looked up, so the base style went missing and its font and colour with it. The reference is now looked up as written first. Items inside a group or pasted into a frame carry no ItemLayer in IDML: they sit on their parent's layer. They were put on the first layer, which may be hidden or locked, and took their text wrap and editing with it. --- crates/idml/src/import.rs | 4 ++ crates/idml/src/import/item.rs | 6 ++- crates/idml/src/import/styles.rs | 7 +++- crates/idml/src/tests.rs | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 3 deletions(-) diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs index 161594e1e..eef282866 100644 --- a/crates/idml/src/import.rs +++ b/crates/idml/src/import.rs @@ -169,6 +169,9 @@ struct Importer<'r> { para_els: HashMap, layers: Vec, layer_ids: HashMap, + /// The layer of the group (or frame) whose children are being read: IDML gives children no + /// ItemLayer of their own, they sit on their parent's layer. + parent_layer: Option, stories: BTreeMap, story_ids: HashMap, parents: Vec, @@ -250,6 +253,7 @@ impl<'r> Importer<'r> { para_els: HashMap::new(), layers: Vec::new(), layer_ids: HashMap::new(), + parent_layer: None, stories: BTreeMap::new(), story_ids: HashMap::new(), parents: Vec::new(), diff --git a/crates/idml/src/import/item.rs b/crates/idml/src/import/item.rs index d25044dc6..ddb8e7bf2 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -60,7 +60,7 @@ impl<'r> Importer<'r> { if let Some(s) = e.get("Self") { self.item_ids.insert(s.to_string(), id); } - let layer = e.get("ItemLayer").and_then(|l| self.layer_ids.get(l)).copied().unwrap_or_else(|| self.layers[0].id); + let layer = e.get("ItemLayer").and_then(|l| self.layer_ids.get(l)).copied().or(self.parent_layer).unwrap_or_else(|| self.layers[0].id); let path = e.prop_el("PathGeometry").map(path_of).unwrap_or_default(); let mut it = Item::new(id, layer, shape, path); it.xf = parent * parse_affine(e.get("ItemTransform")); @@ -223,11 +223,13 @@ impl<'r> Importer<'r> { } "Group" => { let mut kids = Vec::new(); + let outer = self.parent_layer.replace(item.layer); for c in e.elements() { if let Some(k) = self.item(c, Affine::IDENTITY) { kids.push(Arc::new(k)); } } + self.parent_layer = outer; item.content = Content::Group { items: kids }; item.fill = Fill::none(); item.stroke = Stroke::none(); @@ -251,12 +253,14 @@ impl<'r> Importer<'r> { } } else { // Page items pasted into this frame (Paste Into). + let outer = self.parent_layer.replace(item.layer); let kids: Vec> = e .elements() .filter(|c| matches!(c.local(), "Rectangle" | "Oval" | "Polygon" | "GraphicLine" | "Group" | "TextFrame")) .filter_map(|c| self.item(c, Affine::IDENTITY)) .map(Arc::new) .collect(); + self.parent_layer = outer; if !kids.is_empty() { item.content = Content::Group { items: kids }; } diff --git a/crates/idml/src/import/styles.rs b/crates/idml/src/import/styles.rs index 5719393a2..ca245e4db 100644 --- a/crates/idml/src/import/styles.rs +++ b/crates/idml/src/import/styles.rs @@ -202,8 +202,11 @@ impl<'r> Importer<'r> { } fn based_on(&self, e: &El, para: bool) -> Option { - let b = e.prop("BasedOn")?; - let b = b.trim(); + let raw = e.prop("BasedOn")?; + // Style names may end in a space ("arabic purple "): look the reference up as written + // before trimming it. + let known = if para { self.para_names.contains_key(&raw) } else { self.char_names.contains_key(&raw) }; + let b = if known { raw.as_str() } else { raw.trim() }; if para { if let Some(n) = self.para_names.get(b) { return Some(n.clone()); diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 0d078c63c..4fc8288dd 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -2221,6 +2221,43 @@ fn layers_import_and_export_in_stacking_order() { assert_eq!(back.spreads[0].items[0].layer, back.layers[1].id); } +/// A character style based on one whose name ends in a space ("arabic purple ") inherits from +/// it: the reference was trimmed, the base went missing and its font and colour with it. +#[test] +fn based_on_a_style_name_ending_in_a_space() { + let styles = STYLES.replace( + r#""#, + r#" + Source Sans 3 + + + CharacterStyle/purple + + "#, + ); + let story = STORY.replace("CharacterStyle/Strong", "CharacterStyle/purple 23 pt "); + let pkg = zip_files(&[ + ("designmap.xml", DESIGNMAP), + ("Resources/Graphic.xml", GRAPHIC), + ("Resources/Styles.xml", &styles), + ("Resources/Preferences.xml", PREFS), + ("MasterSpreads/MasterSpread_m1.xml", MASTER), + ("Spreads/Spread_sp1.xml", SPREAD), + ("Stories/Story_s1.xml", &story), + ]); + let d = import_idml(&pkg).unwrap(); + let st = d.styles.character.iter().find(|c| c.name == "purple 23 pt ").expect("the derived style"); + assert_eq!(st.based_on.as_deref(), Some("purple ")); + let sid = *d.stories.keys().next().unwrap(); + let story = d.story(sid).unwrap(); + let at = story.text.find("bold").unwrap(); + let (_, base) = d.styles.resolve_para(&story.paras[0]); + let props = d.styles.resolve_char(&base, story.char_format_at(at + 1)); + assert_eq!(props.font_family, "Source Sans 3"); + let fill = d.styles.char_style_attrs("purple ").fill.expect("the base style's colour"); + assert_eq!(props.fill, fill); +} + /// Synthetic forward references: both a body anchor and a footnote anchor own /// a group containing a text frame whose story is serialized later. #[test] @@ -2868,6 +2905,39 @@ fn list_numbering_format_expression_and_bullet_import_and_round_trip() { check(&import_idml(&export_idml(&d)).unwrap()); } +/// The items of a group carry no ItemLayer in IDML: they sit on the group's layer, not on the +/// front layer (which may be hidden or locked and took their wrap and editing with it). +#[test] +fn group_children_sit_on_the_groups_layer() { + let designmap = r#" + + + + "#; + let rect = |me: &str| { + format!( + r#" + + "# + ) + }; + let spread = format!( + r#" + + {}{} + "#, + rect("a"), + rect("b") + ); + let d = import_idml(&zip_files(&[("designmap.xml", designmap), ("Spreads/Spread_s.xml", &spread)])).unwrap(); + let background = d.layers.iter().find(|l| l.name == "Background").unwrap().id; + let g = &d.spreads[0].items[0]; + assert_eq!(g.layer, background); + let mut layers = Vec::new(); + g.walk(&mut |it| layers.push(it.layer)); + assert!(layers.iter().all(|l| *l == background), "{layers:?}"); +} + /// InDesign can write `AppliedFont` with its `$ID/` prefix (`$ID/Source Serif 4`). The text is /// set in that family instead of counting as a missing font drawn in the fallback. #[test] From 0444d88777800f171e8e9a8f8157e1199aba841b Mon Sep 17 00:00:00 2001 From: Youssef Azriouil Date: Thu, 29 Oct 2026 16:16:34 +0100 Subject: [PATCH 2/4] IDML import: BasedOn on lines of its own, layers of pasted-in items tested - A BasedOn reference written on lines of its own (an indented file) to a style whose name ends in a space lost that space: it didn't match as written, so it was trimmed and the base style went missing. Now only the whitespace before the reference and from the first line break after it goes when that names a style; otherwise it is trimmed as before. - The group layer test also covers an item pasted into a frame: it sits on the frame's layer. --- crates/idml/src/import/styles.rs | 21 +++++++++++--- crates/idml/src/tests.rs | 48 ++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/crates/idml/src/import/styles.rs b/crates/idml/src/import/styles.rs index ca245e4db..8bd76f771 100644 --- a/crates/idml/src/import/styles.rs +++ b/crates/idml/src/import/styles.rs @@ -203,10 +203,11 @@ impl<'r> Importer<'r> { fn based_on(&self, e: &El, para: bool) -> Option { let raw = e.prop("BasedOn")?; - // Style names may end in a space ("arabic purple "): look the reference up as written - // before trimming it. - let known = if para { self.para_names.contains_key(&raw) } else { self.char_names.contains_key(&raw) }; - let b = if known { raw.as_str() } else { raw.trim() }; + // Style names may end in a space: use the reference as written, or without the line + // breaks and indentation an indented file puts around it, when that names a style; only + // otherwise trim it. + let names = if para { &self.para_names } else { &self.char_names }; + let b = [raw.as_str(), unindented(&raw)].into_iter().find(|r| names.contains_key(*r)).unwrap_or_else(|| raw.trim()); if para { if let Some(n) = self.para_names.get(b) { return Some(n.clone()); @@ -351,3 +352,15 @@ impl<'r> Importer<'r> { } } } + +/// `r` without the whitespace an indented file puts around an element's text: all of it before +/// (a style reference never starts with a space), and after it from the first line break on (a +/// name's own trailing spaces come before that). +fn unindented(r: &str) -> &str { + let r = r.trim_start(); + let text = r.trim_end().len(); + match r.get(text..).and_then(|tail| tail.find(['\n', '\r'])) { + Some(i) => r.get(..text + i).unwrap_or(r), + None => r, + } +} diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 4fc8288dd..b9ba109ea 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -2258,6 +2258,35 @@ fn based_on_a_style_name_ending_in_a_space() { assert_eq!(props.fill, fill); } +/// A BasedOn on lines of its own (an indented file) that names a style whose name ends in a +/// space: only the line breaks and indentation around the reference go, not the name's space. +#[test] +fn based_on_on_lines_of_its_own_keeps_a_trailing_space() { + let styles = STYLES.replace( + r#""#, + &[ + r#""#, + r#""#, + "\n CharacterStyle/purple \n ", + r#""#, + ] + .concat(), + ); + let pkg = zip_files(&[ + ("designmap.xml", DESIGNMAP), + ("Resources/Graphic.xml", GRAPHIC), + ("Resources/Styles.xml", &styles), + ("Resources/Preferences.xml", PREFS), + ("MasterSpreads/MasterSpread_m1.xml", MASTER), + ("Spreads/Spread_sp1.xml", SPREAD), + ("Stories/Story_s1.xml", STORY), + ]); + let d = import_idml(&pkg).unwrap(); + let st = d.styles.character.iter().find(|c| c.name == "purple 23 pt ").expect("the derived style"); + assert_eq!(st.based_on.as_deref(), Some("purple ")); + assert!(d.styles.char_style_attrs("purple 23 pt ").fill.is_some(), "the base style's colour"); +} + /// Synthetic forward references: both a body anchor and a footnote anchor own /// a group containing a text frame whose story is serialized later. #[test] @@ -2905,8 +2934,9 @@ fn list_numbering_format_expression_and_bullet_import_and_round_trip() { check(&import_idml(&export_idml(&d)).unwrap()); } -/// The items of a group carry no ItemLayer in IDML: they sit on the group's layer, not on the -/// front layer (which may be hidden or locked and took their wrap and editing with it). +/// The items of a group, and those pasted into a frame, carry no ItemLayer in IDML: they sit on +/// the group's or the frame's layer, not on the front layer (which may be hidden or locked and +/// took their wrap and editing with it). #[test] fn group_children_sit_on_the_groups_layer() { let designmap = r#" @@ -2925,16 +2955,22 @@ fn group_children_sit_on_the_groups_layer() { r#" {}{} + {} "#, rect("a"), - rect("b") + rect("b"), + // A frame with an item pasted into it. + rect("f").replace(r#"Self="f""#, r#"Self="f" ItemLayer="L1""#).replace("", &format!("{}", rect("c"))) ); let d = import_idml(&zip_files(&[("designmap.xml", designmap), ("Spreads/Spread_s.xml", &spread)])).unwrap(); let background = d.layers.iter().find(|l| l.name == "Background").unwrap().id; - let g = &d.spreads[0].items[0]; - assert_eq!(g.layer, background); + let items = &d.spreads[0].items; + assert!(items.len() == 2 && items[1].shape == Shape::Rectangle && items[1].has_nested_items(), "{items:?}"); let mut layers = Vec::new(); - g.walk(&mut |it| layers.push(it.layer)); + for it in items { + it.walk(&mut |i| layers.push(i.layer)); + } + assert_eq!(layers.len(), 6); assert!(layers.iter().all(|l| *l == background), "{layers:?}"); } From 7f6aeddf6200f08d574d1664c12ce3cbb67e7155 Mon Sep 17 00:00:00 2001 From: Youssef Azriouil Date: Fri, 13 Nov 2026 20:47:02 +0100 Subject: [PATCH 3/4] IDML import: a rule inherits through an indented BasedOn A paragraph style's BasedOn written on lines of its own (an indented file) kept its line breaks and indentation in the rule-setting chain lookup, so the base style was not found and its rule weight and offset were lost. Strip them the same way the style's own BasedOn is read. --- crates/idml/src/import/styles.rs | 7 ++++++- crates/idml/src/tests.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/idml/src/import/styles.rs b/crates/idml/src/import/styles.rs index 0cf411d20..8162da2f1 100644 --- a/crates/idml/src/import/styles.rs +++ b/crates/idml/src/import/styles.rs @@ -343,7 +343,12 @@ impl<'r> Importer<'r> { if let Some(v) = e.prop(k) { return Some(v); } - let style_ref = |r: String| if r.starts_with("ParagraphStyle/") { r } else { format!("ParagraphStyle/{}", names::escape_id(&r)) }; + // An indented file puts line breaks and indentation around a `BasedOn` written on lines + // of its own; a name's own trailing spaces stay. + let style_ref = |r: String| { + let r = unindented(&r); + if r.starts_with("ParagraphStyle/") { r.to_string() } else { format!("ParagraphStyle/{}", names::escape_id(r)) } + }; let mut next = if e.local() == "ParagraphStyle" { e.prop("BasedOn") } else { e.get("AppliedParagraphStyle").map(str::to_string) }.map(style_ref); for _ in 0..16 { diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index d97cdeb73..85e473b75 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -4506,6 +4506,28 @@ fn style_names_ending_in_a_space_and_partial_rule_overrides_import() { assert_ne!(black.color, white.color, "only the colour is overridden"); } +/// A paragraph style whose `BasedOn` sits on lines of its own (an indented file) takes the +/// rule settings it doesn't set from the style it is based on. +#[test] +fn a_rule_inherits_through_a_based_on_on_lines_of_its_own() { + let styles = STYLES.replace( + r#" "#, + " + $ID/[No paragraph style] + + + + ParagraphStyle/Base + Color/Black + + ", + ); + let d = import_idml(&fixture_with_table_styles(&styles, STORY)).unwrap(); + let rule = d.styles.paragraph.iter().find(|s| s.name == "Derived").unwrap().para.rule_above.clone().unwrap(); + assert!(rule.on, "the rule stays on"); + assert_eq!((rule.weight, rule.offset), (16.0, -3.0), "weight and offset come from the base style"); +} + #[test] fn a_style_setting_only_a_rules_type_gap_or_keep_in_frame_overrides_it() { let styles = STYLES.replace( From cafa4abe6450b7448e05fe84c2a0ceef8bee42d8 Mon Sep 17 00:00:00 2001 From: Youssef Azriouil Date: Fri, 13 Nov 2026 20:57:21 +0100 Subject: [PATCH 4/4] IDML import: the rule chain reads BasedOn as the style chain does Try the reference as written, then without the indentation around it, and fall back to the trimmed reference, so a space before the line break no longer hides the base style when no style id has that space. --- crates/idml/src/import/styles.rs | 11 +++++------ crates/idml/src/tests.rs | 14 +++++++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/idml/src/import/styles.rs b/crates/idml/src/import/styles.rs index 8162da2f1..fd5874e62 100644 --- a/crates/idml/src/import/styles.rs +++ b/crates/idml/src/import/styles.rs @@ -343,12 +343,11 @@ impl<'r> Importer<'r> { if let Some(v) = e.prop(k) { return Some(v); } - // An indented file puts line breaks and indentation around a `BasedOn` written on lines - // of its own; a name's own trailing spaces stay. - let style_ref = |r: String| { - let r = unindented(&r); - if r.starts_with("ParagraphStyle/") { r.to_string() } else { format!("ParagraphStyle/{}", names::escape_id(r)) } - }; + // As `based_on` reads it: the reference as written, or without the line breaks and + // indentation an indented file puts around it, when that names a style; else trimmed. + let id = |r: &str| if r.starts_with("ParagraphStyle/") { r.to_string() } else { format!("ParagraphStyle/{}", names::escape_id(r)) }; + let style_ref = + |r: String| [r.as_str(), unindented(&r)].into_iter().map(id).find(|i| self.para_els.contains_key(i)).unwrap_or_else(|| id(r.trim())); let mut next = if e.local() == "ParagraphStyle" { e.prop("BasedOn") } else { e.get("AppliedParagraphStyle").map(str::to_string) }.map(style_ref); for _ in 0..16 { diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 85e473b75..19ba9ec4f 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -4520,12 +4520,20 @@ fn a_rule_inherits_through_a_based_on_on_lines_of_its_own() { ParagraphStyle/Base Color/Black + + + ParagraphStyle/Base + Color/Black + ", ); let d = import_idml(&fixture_with_table_styles(&styles, STORY)).unwrap(); - let rule = d.styles.paragraph.iter().find(|s| s.name == "Derived").unwrap().para.rule_above.clone().unwrap(); - assert!(rule.on, "the rule stays on"); - assert_eq!((rule.weight, rule.offset), (16.0, -3.0), "weight and offset come from the base style"); + // "Spaced" has a space before the line break, and no style is named with that space. + for name in ["Derived", "Spaced"] { + let rule = d.styles.paragraph.iter().find(|s| s.name == name).unwrap().para.rule_above.clone().unwrap(); + assert!(rule.on, "{name}: the rule stays on"); + assert_eq!((rule.weight, rule.offset), (16.0, -3.0), "{name}: weight and offset come from the base style"); + } } #[test]