diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs index 8577b4dd2..958d7c522 100644 --- a/crates/idml/src/import.rs +++ b/crates/idml/src/import.rs @@ -184,6 +184,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, @@ -290,6 +293,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 5a8bd1f02..d62d0f5e1 100644 --- a/crates/idml/src/import/item.rs +++ b/crates/idml/src/import/item.rs @@ -61,7 +61,7 @@ impl<'r> Importer<'r> { if let Some(s) = model_item.self_id() { self.item_ids.insert(s.to_string(), id); } - let layer = model_item.item_layer().and_then(|l| self.layer_ids.get(l)).copied().unwrap_or_else(|| self.layers[0].id); + let layer = model_item.item_layer().and_then(|l| self.layer_ids.get(l)).copied().or(self.parent_layer).unwrap_or_else(|| self.layers[0].id); let path = path_from_model(model_item.geometry()); let mut it = Item::new(id, layer, shape, path); it.xf = parent * Affine::new(model_item.item_transform()); @@ -233,12 +233,14 @@ impl<'r> Importer<'r> { } "Group" => { let mut kids = Vec::new(); + let outer = self.parent_layer.replace(item.layer); for child in model_item.items() { let child = crate::xml::from_craft(child.to_el()); if let Some(k) = self.item(&child, 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(); @@ -265,12 +267,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 dba8b8cec..eb6d0a02e 100644 --- a/crates/idml/src/import/styles.rs +++ b/crates/idml/src/import/styles.rs @@ -224,8 +224,12 @@ 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: 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()); @@ -327,7 +331,11 @@ 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)) }; + // 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 { @@ -405,6 +413,18 @@ 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, + } +} + /// The corner options (Stroke & Corner Options) of the object style that starts `levels` (the /// style, then the styles it is based on), by name, when it sets them. A style that sets some /// corners, or turns the category on (itself or through the style it is based on), takes the diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs index 2066f7ce8..0e91e08e3 100644 --- a/crates/idml/src/tests.rs +++ b/crates/idml/src/tests.rs @@ -2688,6 +2688,72 @@ 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); +} + +/// 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"); +} + /// Layer Options › Lock Guides is IDML's `LockGuides`: read, and written back (it was always /// written as false). #[test] @@ -3409,6 +3475,46 @@ fn list_numbering_format_expression_and_bullet_import_and_round_trip() { check(&import_idml(&export_idml(&d)).unwrap()); } +/// 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#" + + + + "#; + let rect = |me: &str| { + format!( + r#" + + "# + ) + }; + let spread = format!( + r#" + + {}{} + {} + "#, + rect("a"), + 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 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(); + 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:?}"); +} + /// 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] @@ -4505,6 +4611,36 @@ 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 + + + + ParagraphStyle/Base + Color/Black + + ", + ); + let d = import_idml(&fixture_with_table_styles(&styles, STORY)).unwrap(); + // "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] fn a_style_setting_only_a_rules_type_gap_or_keep_in_frame_overrides_it() { let styles = STYLES.replace(