Skip to content

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

Drop caps: a cleared drop cap style round-trips through IDML

aionggf9fb64022979
diff --git a/crates/compose/src/shape.rs b/crates/compose/src/shape.rs
index 06cf04fc0..cfab72107 100644
--- a/crates/compose/src/shape.rs
+++ b/crates/compose/src/shape.rs
@@ -215,12 +215,18 @@ pub(crate) fn shape_para(
// Nested line styles lie under nested and GREP styles, and the drop cap's style over them all
// (later overlays win).
let mut overlays: Vec<(std::ops::Range<usize>, String)> = lines.to_vec();
- // The drop cap's style counts as a leading "through 1 Dropcap" nested style (the form IDML
- // stores it in), so the nested styles after it start after the drop cap.
- let cap_style = Some(drop.1).filter(|s| !s.is_empty() && *s != designcraft_doc::NO_CHAR_STYLE);
+ // The drop cap's style is the nested styles' leading "through 1 Dropcap" entry (the form IDML
+ // stores it in). Set (`[None]` included), it restyles that entry; a real one stands in for a
+ // missing entry. Either way the nested styles after it start after the drop cap.
+ let set = Some(drop.1).filter(|s| !s.is_empty());
+ let cap_style = set.filter(|s| *s != designcraft_doc::NO_CHAR_STYLE);
let with_cap: Vec<designcraft_doc::NestedStyle>;
- let nested = match cap_style {
- Some(s) if !nested.is_empty() && !nested.first().is_some_and(designcraft_doc::NestedStyle::is_drop_cap) => {
+ let nested = match set {
+ Some(s) if nested.first().is_some_and(designcraft_doc::NestedStyle::is_drop_cap) => {
+ with_cap = std::iter::once(designcraft_doc::NestedStyle::drop_cap(s)).chain(nested.iter().skip(1).cloned()).collect();
+ &with_cap[..]
+ }
+ Some(s) if cap_style.is_some() && !nested.is_empty() => {
with_cap = std::iter::once(designcraft_doc::NestedStyle::drop_cap(s)).chain(nested.iter().cloned()).collect();
&with_cap[..]
}
diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs
index 67ab22f28..6fce9c77c 100644
--- a/crates/idml/src/export.rs
+++ b/crates/idml/src/export.rs
@@ -859,7 +859,8 @@ impl<'a> Ex<'a> {
}
(ps.para.clone(), ps.chars.clone())
};
- self.para_attrs(&mut el, &mut props, &pa);
+ let parent = (ps.name != designcraft_doc::NO_PARA_STYLE).then(|| ps.based_on.as_deref().unwrap_or(designcraft_doc::NO_PARA_STYLE));
+ self.para_attrs(&mut el, &mut props, &pa, parent);
self.char_attrs(&mut el, &mut props, &ca);
pg.insert(&ps.name, with_props(el, props));
}
@@ -1237,7 +1238,9 @@ impl<'a> Ex<'a> {
}
}
- fn para_attrs(&mut self, el: &mut El, props: &mut Vec<El>, a: &ParaAttrs) {
+ /// Write paragraph attributes `a`, set over the resolved paragraph style `parent` (`None` for the
+ /// root style).
+ fn para_attrs(&mut self, el: &mut El, props: &mut Vec<El>, a: &ParaAttrs, parent: Option<&str>) {
if let Some(v) = &a.mojikumi {
props.push(p("Mojikumi", if v.starts_with("MojikumiTable/") { "object" } else { "enumeration" }, v));
}
@@ -1286,18 +1289,32 @@ impl<'a> Ex<'a> {
}
}
// Nested and GREP styles: property lists of records. The drop cap's character style is the
- // nested style list's leading "through 1 Dropcap" entry (IDML has no attribute for it).
- let cap_style = a.drop_cap_style.as_deref().filter(|s| !s.is_empty() && *s != st::NO_CHAR_STYLE);
- if a.nested_styles.is_some() || cap_style.is_some() {
- let mut list = a.nested_styles.clone().unwrap_or_default();
- if let Some(s) = cap_style {
- match list.first_mut() {
- Some(ns) if ns.is_drop_cap() => ns.style = s.to_string(),
- _ => list.insert(0, designcraft_doc::NestedStyle::drop_cap(s)),
- }
+ // nested style list's leading "through 1 Dropcap" entry (IDML has no attribute for it): a
+ // drop cap style set here (`[None]` included) restyles that entry, and a real one is
+ // inserted where there is none. Without a list of its own, the inherited list is written
+ // when that is what carries the drop cap style, since any list replaces the inherited one.
+ let real = |s: &str| !s.is_empty() && s != st::NO_CHAR_STYLE;
+ let cap = a.drop_cap_style.as_deref().filter(|s| !s.is_empty());
+ let mut list = a.nested_styles.clone();
+ if let (Some(cap), None) = (cap, &list) {
+ let inherited = parent.map(|n| self.d.styles.resolve_para_style(n).0);
+ let inherited_cap = inherited
+ .as_ref()
+ .is_some_and(|pp| real(&pp.drop_cap_style) || pp.nested_styles.first().is_some_and(|ns| ns.is_drop_cap() && real(&ns.style)));
+ if real(cap) || inherited_cap {
+ list = Some(inherited.map(|pp| pp.nested_styles).unwrap_or_default());
+ }
+ }
+ if let (Some(cap), Some(list)) = (cap, list.as_mut()) {
+ match list.first_mut() {
+ Some(ns) if ns.is_drop_cap() => ns.style = cap.to_string(),
+ _ if real(cap) => list.insert(0, designcraft_doc::NestedStyle::drop_cap(cap)),
+ _ => {}
}
+ }
+ if let Some(list) = &list {
let mut l = El::new("AllNestedStyles").attr("type", "list");
- for ns in &list {
+ for ns in list {
let (ty, delim) = names::nested_until_out(&ns.until);
l = l.child(
El::new("ListItem")
@@ -2414,7 +2431,7 @@ impl<'a> Ex<'a> {
fn psr_el(&mut self, pf: &ParaFormat) -> El {
let mut el = El::new("ParagraphStyleRange").attr("AppliedParagraphStyle", names::style_self("ParagraphStyle", PARA_BUILTINS, &pf.style));
let mut props = Vec::new();
- self.para_attrs(&mut el, &mut props, &pf.para);
+ self.para_attrs(&mut el, &mut props, &pf.para, Some(&pf.style));
self.char_attrs(&mut el, &mut props, &pf.chars);
with_props(el, props)
}
diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs
index 61bdf3374..66488d1fc 100644
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@ -1907,6 +1907,38 @@ fn own_nested_styles_clear_an_inherited_drop_cap_style() {
assert_eq!(d.styles.resolve_para(&story.paras[1]).0.drop_cap_style, "Strong");
}
+/// A paragraph that clears its style's drop cap style without nested styles of its own round-trips:
+/// it is written with the style's list, the Dropcap entry set to [None].
+#[test]
+fn a_cleared_drop_cap_style_round_trips() {
+ let paras = r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Opener">
+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Once upon a time</Content></CharacterStyleRange>
+ </ParagraphStyleRange>"#;
+ let mut d = import_idml_with(&drop_cap_fixture("", paras), &|_| None).unwrap();
+ let sid = *d.stories.keys().next().unwrap();
+ let clear = |d: &mut Document| d.story_mut(sid).unwrap().paras[0].para.drop_cap_style = Some(st::NO_CHAR_STYLE.into());
+ let resolved = |d: &Document| {
+ let st = d.stories.values().find(|s| s.text.starts_with("Once")).unwrap();
+ d.styles.resolve_para(&st.paras[0]).0
+ };
+ clear(&mut d);
+ let back = import_idml(&export_idml(&d)).unwrap();
+ let pp = resolved(&back);
+ assert_eq!(pp.drop_cap_style, st::NO_CHAR_STYLE);
+ assert_eq!(pp.nested_styles, vec![designcraft_doc::NestedStyle::drop_cap(st::NO_CHAR_STYLE)]);
+ // The style's drop cap style without nested styles: the paragraph is written with an empty
+ // list, which clears it on import.
+ if let Some(s) = d.styles_mut().para_mut("Opener") {
+ s.para.nested_styles = None;
+ }
+ assert_eq!(resolved(&d).drop_cap_style, st::NO_CHAR_STYLE);
+ let back = import_idml(&export_idml(&d)).unwrap();
+ assert_eq!(back.styles.para("Opener").unwrap().para.drop_cap_style.as_deref(), Some("Strong"));
+ let pp = resolved(&back);
+ assert_eq!(pp.drop_cap_style, st::NO_CHAR_STYLE);
+ assert!(pp.nested_styles.iter().all(|ns| ns.style != "Strong"), "{:?}", pp.nested_styles);
+}
+
/// 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.