Skip to content

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

Honor IDML SameParaStyleSpacing between same-style paragraphs (#449)

MarekSpodBiedry30cbd67e3de3
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 3dd7c25b3..072973d71 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -849,7 +849,12 @@ fn compose_with_db(doc: &Document, story: &Story, frames: &[FrameSpec], opts: &C
}
}
if cur.last_baseline.is_some() {
- cur.pending += pp.space_before;
+ // Same style as the previous paragraph with a same-style spacing: it replaces space after + before.
+ let same_style = pi.checked_sub(1).and_then(|p| story.paras.get(p)).is_some_and(|prev| prev.style == pf.style && prev.table.is_none());
+ match pp.same_para_style_spacing {
+ Some(gap) if same_style => cur.pending = gap,
+ _ => cur.pending += pp.space_before,
+ }
}
if let Some(cfg) = split_cfg
&& cur.split.is_none()
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 0d1fe2d27..2eccf7d18 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -2771,3 +2771,30 @@ fn empty_paragraphs_get_no_bullet_or_number() {
assert_eq!(list_labels(&cs, LABEL_CHARS), ["1.", "", "2.", ""], "list `{name}`");
}
}
+
+#[test]
+fn same_para_style_spacing_replaces_space_between_same_style_paragraphs() {
+ let rect = Rect::new(0.0, 0.0, 300.0, 700.0);
+ let base = ParaAttrs { space_before: Some(5.0), space_after: Some(10.0), ..Default::default() };
+ // `other_last`: the last paragraph gets a different paragraph style.
+ let baselines = |para: ParaAttrs, other_last: bool| {
+ let (mut d, sid, _) = doc_with("A\n\nB", rect, para);
+ if other_last && let Some(st) = d.stories.get_mut(&sid) {
+ std::sync::Arc::make_mut(st).paras[2].style = "Other".into();
+ }
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ all_lines(&cs).iter().map(|l| l.baseline).collect::<Vec<_>>()
+ };
+ let tight_attrs = ParaAttrs { same_para_style_spacing: Some(Some(0.0)), ..base.clone() };
+ let normal = baselines(base.clone(), false);
+ let tight = baselines(tight_attrs.clone(), false);
+ let wide = baselines(ParaAttrs { same_para_style_spacing: Some(Some(20.0)), ..base }, false);
+ let mixed = baselines(tight_attrs, true);
+ assert_eq!(normal.len(), 3);
+ // Ordinary spacing: space after (10) + space before (5) between each pair, empty paragraph included.
+ assert!((normal[1] - normal[0] - 14.4 - 15.0).abs() < 1e-6, "{normal:?}");
+ assert!((tight[1] - tight[0] - 14.4).abs() < 1e-6 && (tight[2] - tight[1] - 14.4).abs() < 1e-6, "{tight:?}");
+ assert!((wide[2] - wide[1] - 14.4 - 20.0).abs() < 1e-6, "{wide:?}");
+ // A different style keeps the ordinary spacing at its boundary.
+ assert!((mixed[1] - mixed[0] - 14.4).abs() < 1e-6 && (mixed[2] - mixed[1] - 14.4 - 15.0).abs() < 1e-6, "{mixed:?}");
+}
diff --git a/crates/doc/src/attrs.rs b/crates/doc/src/attrs.rs
index b99740e2c..04c643c80 100644
--- a/crates/doc/src/attrs.rs
+++ b/crates/doc/src/attrs.rs
@@ -809,6 +809,8 @@ attr_set! {
last_line_indent: f64 = 0.0,
space_before: f64 = 0.0,
space_after: f64 = 0.0,
+ /// Space between consecutive paragraphs of the same style; replaces space after/before there (None = ignore).
+ same_para_style_spacing: Option<f64> = None,
drop_cap_lines: u32 = 0,
drop_cap_chars: u32 = 0,
/// Nested styles, in order from the paragraph start.
diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs
index 9d73562b9..fc55d526b 100644
--- a/crates/idml/src/export.rs
+++ b/crates/idml/src/export.rs
@@ -1368,6 +1368,9 @@ impl<'a> Ex<'a> {
n!(last_line_indent, "LastLineIndent");
n!(space_before, "SpaceBefore");
n!(space_after, "SpaceAfter");
+ if let Some(Some(v)) = a.same_para_style_spacing {
+ el.set("SameParaStyleSpacing", num(v));
+ }
n!(drop_cap_lines, "DropCapLines");
n!(drop_cap_chars, "DropCapCharacters");
if let Some(g) = a.grid_align {
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index d85d9ee02..77f0447f6 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -1278,6 +1278,8 @@ impl<'r> Importer<'r> {
a.grep_styles = Some(v);
}
a.space_after = e.num("SpaceAfter");
+ // A number, or an enumeration (Ignore) that turns it off even over a based-on style's value.
+ a.same_para_style_spacing = e.prop("SameParaStyleSpacing").map(|v| v.trim().parse::<f64>().ok().filter(|v| v.is_finite()));
a.drop_cap_lines = u("DropCapLines");
a.drop_cap_chars = u("DropCapCharacters");
if let Some(g) = e.prop("GridAlignment") {
diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs
index 7b747587a..e5f08d429 100644
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@ -1399,3 +1399,22 @@ fn list_numbering_format_expression_and_bullet_import_and_round_trip() {
check(&d);
check(&import_idml(&export_idml(&d)).unwrap());
}
+
+#[test]
+fn same_para_style_spacing_imports_and_round_trips() {
+ let story = r#"<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"><Story Self="s1">
+ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/[No paragraph style]" SameParaStyleSpacing="0">
+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Gap</Content><Br/></CharacterStyleRange>
+ </ParagraphStyleRange>
+ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/[No paragraph style]" SameParaStyleSpacing="Nothing">
+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>Ignore</Content></CharacterStyleRange>
+ </ParagraphStyleRange></Story></idPkg:Story>"#;
+ let d = import_idml(&fixture_with_story(story)).unwrap();
+ let st = d.stories.values().find(|s| s.text.contains("Gap")).unwrap();
+ assert_eq!(st.paras[0].para.same_para_style_spacing, Some(Some(0.0)));
+ // Ignore is an explicit "off", not an unset value.
+ assert_eq!(st.paras[1].para.same_para_style_spacing, Some(None));
+ let back = import_idml(&export_idml(&d)).unwrap();
+ let b = back.stories.values().find(|s| s.text.contains("Gap")).unwrap();
+ assert_eq!(b.paras[0].para.same_para_style_spacing, Some(Some(0.0)));
+}