DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Drop caps: an IDML nested style list of its own clears an inherited drop cap style
aiongg276b6f31709f
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rsindex 4dbcbb705..4f2fc5fd6 100644--- a/crates/idml/src/import.rs+++ b/crates/idml/src/import.rs@@ -2330,12 +2330,39 @@ impl<'r> Importer<'r> {self.spreads.iter().chain(self.parents.iter()).find_map(|s| find(&s.items, id))}+ /// A nested style list replaces the one a style or paragraph inherits, and with it the drop+ /// cap style (the list's leading "through 1 Dropcap" entry, read into the drop cap style). A+ /// list of its own without that entry clears an inherited drop cap style.+ fn clear_inherited_drop_cap_styles(&mut self) {+ let clears = |a: &designcraft_doc::ParaAttrs| a.drop_cap_style.is_none() && a.nested_styles.is_some();+ let cleared: Vec<String> = self+ .styles+ .paragraph+ .iter()+ .filter(|s| clears(&s.para) && inherits_drop_cap_style(&self.styles, s.based_on.as_deref()))+ .map(|s| s.name.clone())+ .collect();+ for name in cleared {+ if let Some(s) = self.styles.para_mut(&name) {+ s.para.drop_cap_style = Some(st::NO_CHAR_STYLE.into());+ }+ }+ for story in self.stories.values_mut() {+ for p in &mut story.paras {+ if clears(&p.para) && inherits_drop_cap_style(&self.styles, Some(&p.style)) {+ p.para.drop_cap_style = Some(st::NO_CHAR_STYLE.into());+ }+ }+ }+ }+fn finish(mut self, root: &El) -> Result<Document> {let title = root.get("Name").map(|n| n.trim_end_matches(".indd").to_string()).filter(|n| !n.is_empty()).unwrap_or_else(|| "Untitled".into());if self.spreads.is_empty() {return Err(IdmlError::Invalid("document has no spreads".into()));}self.settings.lists = std::mem::take(&mut self.lists);+ self.clear_inherited_drop_cap_styles();// Make sure built-in paragraph styles exist and are first.let d = Document {title,@@ -2527,6 +2554,23 @@ fn text_frame_options(prefs: &[&El]) -> TextFrameOptions {o}+/// Does paragraph style `name` (and its based-on chain) set a drop cap character style? A style+/// with a nested style list of its own and no drop cap style has none.+fn inherits_drop_cap_style<'a>(styles: &'a Styles, mut name: Option<&'a str>) -> bool {+ let mut seen = HashSet::new();+ while let Some(n) = name {+ let Some(s) = styles.para(n).filter(|_| seen.insert(n)) else { break };+ if let Some(c) = s.para.drop_cap_style.as_deref() {+ return !c.is_empty() && c != st::NO_CHAR_STYLE;+ }+ if s.para.nested_styles.is_some() {+ return false;+ }+ name = s.based_on.as_deref();+ }+ false+}+/// Attribute lookup over several elements: the first that has it.struct Prefs<'a>(&'a [&'a El]);diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rsindex 88a7d7bc3..61bdf3374 100644--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@ -1848,6 +1848,65 @@ fn drop_caps_import_and_round_trip() {assert_eq!(st.para.nested_styles, Some(vec![designcraft_doc::NestedStyle::drop_cap("Strong")]));}+/// IDML styles and stories with an `Opener` style whose drop cap style is `Strong`, plus `styles`+/// (more paragraph styles) and `paras` (the story's paragraph ranges). `WORDS` in them stands for+/// a nested style list of `Strong` through 1 word.+fn drop_cap_fixture(styles: &str, paras: &str) -> Vec<u8> {+ let nested = |style: &str, delim: &str| {+ format!(+ r#"<AllNestedStyles type="list"><ListItem type="record"><AppliedCharacterStyle type="object">CharacterStyle/{style}</AppliedCharacterStyle><Delimiter type="enumeration">{delim}</Delimiter><Repetition type="long">1</Repetition><Inclusive type="boolean">true</Inclusive></ListItem></AllNestedStyles>"#+ )+ };+ let words = nested("Strong", "AnyWord");+ let opener = format!(+ r#"<ParagraphStyle Self="ParagraphStyle/Opener" Name="Opener" DropCapCharacters="1" DropCapLines="3"><Properties>{}</Properties></ParagraphStyle>+ {}+ </RootParagraphStyleGroup>"#,+ nested("Strong", "Dropcap"),+ styles.replace("WORDS", &words)+ );+ let styles = STYLES.replace("</RootParagraphStyleGroup>", &opener);+ let story = format!(+ r#"<?xml version="1.0" encoding="UTF-8"?>+<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="16.0">+ <Story Self="s1">{}</Story>+</idPkg:Story>"#,+ paras.replace("WORDS", &words)+ );+ 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),+ ])+}++/// A nested style list replaces the inherited one, drop cap style entry included: a style or+/// paragraph with a list of its own and no Dropcap entry has no drop cap style.+#[test]+fn own_nested_styles_clear_an_inherited_drop_cap_style() {+ let styles = r#"<ParagraphStyle Self="ParagraphStyle/Child" Name="Child"><Properties><BasedOn type="object">ParagraphStyle/Opener</BasedOn>WORDS</Properties></ParagraphStyle>+ <ParagraphStyle Self="ParagraphStyle/Plain" Name="Plain"><Properties>WORDS</Properties></ParagraphStyle>"#;+ let paras = r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener"><Properties>WORDS</Properties>+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Cleared</Content><Br/></CharacterStyleRange>+ </ParagraphStyleRange>+ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener">+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Inherited</Content></CharacterStyleRange>+ </ParagraphStyleRange>"#;+ let d = import_idml_with(&drop_cap_fixture(styles, paras), &|_| None).unwrap();+ let cap = |name: &str| d.styles.para(name).unwrap().para.drop_cap_style.clone();+ assert_eq!(cap("Opener").as_deref(), Some("Strong"));+ assert_eq!(cap("Child").as_deref(), Some(st::NO_CHAR_STYLE), "its own list replaces Opener's");+ assert_eq!(cap("Plain"), None, "nothing to clear");+ let story = d.stories.values().find(|s| s.text.starts_with("Cleared")).unwrap();+ assert_eq!(story.paras[0].para.drop_cap_style.as_deref(), Some(st::NO_CHAR_STYLE));+ assert_eq!(story.paras[1].para.drop_cap_style, None);+ assert_eq!(d.styles.resolve_para(&story.paras[1]).0.drop_cap_style, "Strong");+}+/// Frames take the corners and Text Frame Options they don't write from their object style chain,/// then `[None]`. Per-corner attributes win over the legacy all-corners pair, at each level; a/// corner shape without a radius takes the chain's radius.