Skip to content

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

Paragraph rules: Keep In Frame stops at the frame's edge; Text Color gaps import and export

moppeeer02749d3b7b9b
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 111910929..0e875e77b 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1873,8 +1873,9 @@ fn compose_with_db(doc: &Document, story: &Story, frames: &[FrameSpec], opts: &C
.map(|l| if r.column_width { (l.x0, l.x1) } else { (l.x0, l.end_x) })
.unwrap_or((0.0, 0.0));
let (color, tint) = rule_color(r, &above);
- // Keep In Frame: not above the frame's text area.
- let top = frames.get(fi).map_or(f64::NEG_INFINITY, |f| f.area.y0);
+ // Keep In Frame: not above the frame, as InDesign's (its top, not the text area's
+ // below the top inset).
+ let top = frames.get(fi).map_or(f64::NEG_INFINITY, |f| f.area.y0 - if f.vertical { 0.0 } else { f.opts.inset[0] });
let y = if r.keep_in_frame && y - r.weight < top { top + r.weight } else { y };
let rect = Rect::new(col.0 + r.left_indent, y - r.weight, col.1 - r.right_indent, y);
ft.decos.extend(rule_decos(doc, r, rect, (color, tint), &above));
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 6ea012270..2fc53cc0b 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -3731,6 +3731,17 @@ fn paragraph_rules_have_a_type_a_gap_colour_and_keep_in_frame() {
};
assert!(above(false).y0 < 50.0, "{:?}", above(false));
assert!((above(true).y0 - 50.0).abs() < 1e-6, "{:?}", above(true));
+ // With a top inset the rule may still rise into it: raised out of the frame, it comes back
+ // to the frame's edge, not to the text area's below the inset.
+ let r = Rule { on: true, weight: 2.0, offset: 60.0, keep_in_frame: true, ..Default::default() };
+ let para = ParaAttrs { rule_above: Some(r), ..Default::default() };
+ let (mut d, sid, fid) = doc_with("Title", Rect::new(0.0, 50.0, 300.0, 200.0), para);
+ if let Some(tf) = d.item_mut(fid).and_then(|it| it.text_frame_mut()) {
+ tf.options.inset = [20.0, 0.0, 0.0, 0.0];
+ }
+ let cs = compose_story(&d, sid, &ComposeOptions::default());
+ let rule = cs.frame(fid).unwrap().decos[0].rect;
+ assert!((rule.y0 - 50.0).abs() < 1e-6, "kept at the frame's top, in the inset: {rule:?}");
}
/// A rule in Text Color takes the colour of the paragraph's text: the first character's for the
diff --git a/crates/idml/src/export.rs b/crates/idml/src/export.rs
index b247ac709..9f8298760 100644
--- a/crates/idml/src/export.rs
+++ b/crates/idml/src/export.rs
@@ -1747,7 +1747,12 @@ impl<'a> Ex<'a> {
el.set(&format!("{k}LeftIndent"), num(r.left_indent));
el.set(&format!("{k}RightIndent"), num(r.right_indent));
el.set(&format!("{k}Type"), names::stroke_type_out(&r.kind));
- el.set(&format!("{k}GapColor"), self.sw(&r.gap_color));
+ // A gap in Text Color is a string property, as the rule's own colour is.
+ if r.gap_color == designcraft_doc::TEXT_COLOR {
+ props.push(p(&format!("{k}GapColor"), "string", designcraft_doc::TEXT_COLOR));
+ } else {
+ el.set(&format!("{k}GapColor"), self.sw(&r.gap_color));
+ }
el.set(&format!("{k}GapTint"), pct(r.gap_tint as f64));
if k == "RuleAbove" {
el.set("KeepRuleAboveInFrame", bool_s(r.keep_in_frame));
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rs
index c60380a27..7ee7c0e8a 100644
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@ -1970,8 +1970,10 @@ impl<'r> Importer<'r> {
if let Some(t) = self.para_setting(e, &format!("{k}Type")) {
r.kind = names::stroke_type_in(t.trim());
}
- if let Some(c) = self.para_setting(e, &format!("{k}GapColor")).filter(|c| c.contains('/')) {
- r.gap_color = self.swatch_ref(c.trim());
+ if let Some(c) = self.para_setting(e, &format!("{k}GapColor"))
+ && let Some(c) = self.rule_color_ref(&c)
+ {
+ r.gap_color = c;
}
if let Some(t) = tint(self.para_setting(e, &format!("{k}GapTint")).and_then(|v| v.trim().parse::<f64>().ok()).filter(|v| v.is_finite())) {
r.gap_tint = t;
@@ -1979,17 +1981,23 @@ impl<'r> Importer<'r> {
if k == "RuleAbove" {
r.keep_in_frame = self.para_setting(e, "KeepRuleAboveInFrame").is_some_and(|v| v.trim() == "true");
}
- if let Some(c) = get(2) {
- let c = c.trim();
- if c == designcraft_doc::TEXT_COLOR {
- r.color = c.to_string();
- } else if c.contains('/') {
- r.color = self.swatch_ref(c);
- }
+ if let Some(c) = get(2).and_then(|c| self.rule_color_ref(&c)) {
+ r.color = c;
}
Some(r)
}
+ /// A rule's colour or gap colour: Text Color (written `Text Color` or `$ID/Text Color`), or
+ /// a swatch reference; anything else leaves the default.
+ fn rule_color_ref(&mut self, c: &str) -> Option<String> {
+ let c = c.trim();
+ if c == designcraft_doc::TEXT_COLOR || c.strip_prefix("$ID/") == Some(designcraft_doc::TEXT_COLOR) {
+ Some(designcraft_doc::TEXT_COLOR.to_string())
+ } else {
+ c.contains('/').then(|| self.swatch_ref(c))
+ }
+ }
+
/// Paragraph setting `k` of `e` (a paragraph style or a range): its own, else from its
/// paragraph style chain (`BasedOn` of a style, the applied style of a range).
fn para_setting(&self, e: &El, k: &str) -> Option<String> {
diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rs
index ba0acb78d..924eba437 100644
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@ -3733,6 +3733,9 @@ fn a_style_setting_only_a_rules_type_gap_or_keep_in_frame_overrides_it() {
<ParagraphStyle Self="ParagraphStyle/Kept" Name="Kept" KeepRuleAboveInFrame="true">
<Properties><BasedOn type="string">$ID/[No paragraph style]</BasedOn></Properties>
</ParagraphStyle>
+ <ParagraphStyle Self="ParagraphStyle/TextGap" Name="TextGap" RuleBelowGapColor="$ID/Text Color" RuleAboveColor="$ID/Text Color">
+ <Properties><BasedOn type="string">$ID/[No paragraph style]</BasedOn><RuleAboveGapColor type="string">Text Color</RuleAboveGapColor></Properties>
+ </ParagraphStyle>
</RootParagraphStyleGroup>"#,
);
let d = import_idml(&fixture_with_table_styles(&styles, STORY)).unwrap();
@@ -3742,6 +3745,15 @@ fn a_style_setting_only_a_rules_type_gap_or_keep_in_frame_overrides_it() {
let gap = style("Gap").rule_below.expect("the gap colour alone, with no on or weight in the chain");
assert!(!gap.on && gap.gap_color != designcraft_color::swatch::NONE, "{gap:?}");
assert!(style("Kept").rule_above.expect("Keep In Frame alone").keep_in_frame);
+ // Text Color, in either spelling, for a gap and a rule colour.
+ let text = style("TextGap");
+ let (below, above) = (text.rule_below.expect("rule below"), text.rule_above.expect("rule above"));
+ assert_eq!(below.gap_color, designcraft_doc::TEXT_COLOR);
+ assert_eq!((above.gap_color.as_str(), above.color.as_str()), (designcraft_doc::TEXT_COLOR, designcraft_doc::TEXT_COLOR));
+ // And it is written back as Text Color, not as no swatch.
+ let back = import_idml(&export_idml(&d)).unwrap();
+ let below = back.styles.paragraph.iter().find(|s| s.name == "TextGap").unwrap().para.rule_below.clone().unwrap();
+ assert_eq!(below.gap_color, designcraft_doc::TEXT_COLOR, "round-trips");
}
#[test]