Skip to content

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

Merge idml-deleted-text (update): deleted text inside nested paragraphs stays deleted; a deleted break doesn't break (#581)

aiongg7cc6f6db72b8
diff --cc crates/compose/src/lib.rs
index 5156dbfa9,f5660f117..f17ec0ac5
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@@ -888,14 -589,18 +888,18 @@@ fn compose_with_db(doc: &Document, stor
continue;
}
let pf = &story.paras[pi];
- let (pp, base_chars) = doc.styles.resolve_para(pf);
+ let (mut pp, base_chars) = doc.styles.resolve_para(pf);
// A break character that ends the previous paragraph sends this one on, like a start option.
- let after_break = pi
- .checked_sub(1)
- .and_then(|p| para_ranges.get(p))
- .and_then(|r| story.text.get(r.clone()))
- .and_then(|t| t.chars().next_back())
- .and_then(break_start);
+ // Deleted paragraphs in between don't count, nor does a deleted or hidden break.
+ let after_break = (0..pi).rev().find(|p| !deleted.get(*p).copied().unwrap_or(false)).and_then(|p| {
+ let r = para_ranges.get(p)?;
+ let (i, c) = story.text.get(r.clone())?.char_indices().next_back()?;
+ let start = break_start(c)?;
+ let (_, chars) = doc.styles.resolve_para(story.paras.get(p)?);
+ let props = doc.styles.resolve_char(&chars, story.char_format_at(r.start + i + c.len_utf8()));
+ let gone = props.change == designcraft_doc::ChangeMark::Deleted || doc.conditions_hide(&props.conditions);
+ (!gone).then_some(start)
+ });
if let Some(t) = story.para_table(pi) {
if cur.last_baseline.is_some() {
cur.start(after_break.unwrap_or(pp.start_paragraph), &cols, frames, doc);
diff --cc crates/idml/src/tests.rs
index 2f0092337,c4381fea4..404def0f5
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@@ -3226,67 -1507,85 +3226,150 @@@ fn tracked_deleted_text_takes_no_room_a
assert_eq!(marks(back.stories.values().next().unwrap()), want);
}
+#[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)));
+ // The explicit Ignore survives the round trip too.
+ assert_eq!(b.paras[1].para.same_para_style_spacing, Some(None));
+}
+
+/// SameParaStyleSpacing set as a `Properties` element on the based-on style (`Normal`) reaches
+/// the headings: consecutive headings sit one leading apart. SetIgnore keeps the usual spacing.
+#[test]
+fn same_para_style_spacing_from_a_based_on_style_element_closes_the_gap() {
+ let styles = r#"<?xml version="1.0" encoding="UTF-8"?>
+<idPkg:Styles xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="16.0">
+ <RootCharacterStyleGroup Self="rc">
+ <CharacterStyle Self="CharacterStyle/$ID/[No character style]" Name="$ID/[No character style]"/>
+ </RootCharacterStyleGroup>
+ <RootParagraphStyleGroup Self="rp">
+ <ParagraphStyle Self="ParagraphStyle/$ID/[No paragraph style]" Name="$ID/[No paragraph style]" PointSize="12">
+ <Properties><AppliedFont type="string">Source Serif 4</AppliedFont><Leading type="unit">13</Leading></Properties>
+ </ParagraphStyle>
+ <ParagraphStyle Self="ParagraphStyle/$ID/NormalParagraphStyle" Name="$ID/NormalParagraphStyle">
+ <Properties><BasedOn type="string">$ID/[No paragraph style]</BasedOn><SameParaStyleSpacing type="unit">0</SameParaStyleSpacing></Properties>
+ </ParagraphStyle>
+ <ParagraphStyle Self="ParagraphStyle/Heading" Name="Heading" SpaceBefore="18" SpaceAfter="4.5">
+ <Properties><BasedOn type="object">ParagraphStyle/$ID/NormalParagraphStyle</BasedOn></Properties>
+ </ParagraphStyle>
+ <ParagraphStyle Self="ParagraphStyle/Loose" Name="Loose" SpaceBefore="18" SpaceAfter="4.5">
+ <Properties><BasedOn type="object">ParagraphStyle/$ID/NormalParagraphStyle</BasedOn><SameParaStyleSpacing type="enumeration">SetIgnore</SameParaStyleSpacing></Properties>
+ </ParagraphStyle>
+ </RootParagraphStyleGroup>
+</idPkg:Styles>"#;
+ let para = |style: &str, text: &str| {
+ format!(
+ r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/{style}"><CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"><Content>{text}</Content><Br/></CharacterStyleRange></ParagraphStyleRange>"#
+ )
+ };
+ let story = format!(
+ r#"<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"><Story Self="s1">{}{}{}{}</Story></idPkg:Story>"#,
+ para("Heading", "One"),
+ para("Heading", "Two"),
+ para("Loose", "Three"),
+ para("Loose", "Four")
+ );
+ let d = import_idml(&fixture_with_table_styles(styles, &story)).unwrap();
+ let sid = d.stories.iter().find(|(_, s)| s.text.contains("One")).map(|(id, _)| *id).unwrap();
+ let cs = designcraft_compose::compose_story(&d, sid, &Default::default());
+ let baseline = |para: usize| cs.frames[0].lines.iter().find(|l| l.para == para).unwrap().baseline;
+ assert!((baseline(1) - baseline(0) - 13.0).abs() < 1e-6, "{} {}", baseline(0), baseline(1));
+ assert!((baseline(3) - baseline(2) - 13.0 - 4.5 - 18.0).abs() < 1e-6, "{} {}", baseline(2), baseline(3));
+}
++
+ /// Composed lines (baseline, left, right) of the only story.
+ fn story_lines(d: &Document) -> Vec<(f64, f64, f64)> {
+ let sid = *d.stories.keys().next().unwrap();
+ let cs = designcraft_compose::compose_story(d, sid, &Default::default());
+ cs.frames.iter().flat_map(|f| f.lines.iter().map(|l| (l.baseline, l.x0, l.end_x))).collect()
+ }
+
+ const PSR: &str = r#"<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/NormalParagraphStyle">"#;
+
+ /// Lays out like the same story without the deleted paragraph.
+ fn assert_deleted_para_takes_no_room(deleted: &str, deleted_text: &str) {
+ let tracked = tracked_story(&format!(
+ r#"{PSR}{CSR}<Content>First live paragraph.</Content><Br/></CharacterStyleRange></ParagraphStyleRange>
+ {deleted}
+ {PSR}{CSR}<Content>Second live paragraph.</Content></CharacterStyleRange></ParagraphStyleRange>"#
+ ));
+ let plain = tracked_story(&format!(
+ r#"{PSR}{CSR}<Content>First live paragraph.</Content><Br/></CharacterStyleRange></ParagraphStyleRange>
+ {PSR}{CSR}<Content>Second live paragraph.</Content></CharacterStyleRange></ParagraphStyleRange>"#
+ ));
+ let d = import_idml_with(&fixture_with_story(&tracked), &|_| None).unwrap();
+ let story = d.stories.values().next().unwrap();
+ let marks: Vec<_> = story.runs().map(|(r, f)| (story.text[r].to_string(), f.over.change)).collect();
+ let want = vec![
+ ("First live paragraph.\n".to_string(), None),
+ (deleted_text.to_string(), Some(designcraft_doc::ChangeMark::Deleted)),
+ ("Second live paragraph.".to_string(), None),
+ ];
+ assert_eq!(marks, want);
+ let expected = story_lines(&import_idml_with(&fixture_with_story(&plain), &|_| None).unwrap());
+ assert_eq!(expected.len(), 2);
+ assert_eq!(story_lines(&d), expected);
+ }
+
+ #[test]
+ fn tracked_deleted_paragraph_range_stays_deleted() {
+ assert_deleted_para_takes_no_room(
+ &format!(
+ r#"<Change Self="c1" ChangeType="DeletedText">{PSR}{CSR}<Content>Deleted text</Content><Br/></CharacterStyleRange></ParagraphStyleRange></Change>"#
+ ),
+ "Deleted text\n",
+ );
+ }
+
+ #[test]
+ fn tracked_deleted_frame_break_does_not_break() {
+ let fb = "\u{E003}";
+ let nf = r#"<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" ParagraphBreakType="NextFrame">"#;
+ // The change around the paragraph range.
+ assert_deleted_para_takes_no_room(
+ &format!(
+ r#"<Change Self="c1" ChangeType="DeletedText">{PSR}{CSR}<Content>Gone</Content></CharacterStyleRange>{nf}<Br/></CharacterStyleRange></ParagraphStyleRange></Change>"#
+ ),
+ &format!("Gone{fb}\n"),
+ );
+ // The change inside the character range.
+ assert_deleted_para_takes_no_room(
+ &format!(
+ r#"{PSR}{nf}<Change Self="c1" ChangeType="DeletedText"><Content>Gone</Content><Br/></Change></CharacterStyleRange></ParagraphStyleRange>"#
+ ),
+ &format!("Gone{fb}\n"),
+ );
+ }
+
+ #[test]
+ fn live_frame_break_before_deleted_paragraph_still_breaks() {
+ let nf = r#"<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" ParagraphBreakType="NextFrame">"#;
+ let first =
+ format!(r#"{PSR}{CSR}<Content>First live paragraph.</Content></CharacterStyleRange>{nf}<Br/></CharacterStyleRange></ParagraphStyleRange>"#);
+ let second = format!(r#"{PSR}{CSR}<Content>Second live paragraph.</Content></CharacterStyleRange></ParagraphStyleRange>"#);
+ let deleted = format!(
+ r#"<Change Self="c1" ChangeType="DeletedText">{PSR}{CSR}<Content>Gone</Content><Br/></CharacterStyleRange></ParagraphStyleRange></Change>"#
+ );
+ let import = |body: String| import_idml_with(&fixture_with_story(&tracked_story(&body)), &|_| None).unwrap();
+ let reference = import(format!("{first}\n{second}"));
+ let sid = *reference.stories.keys().next().unwrap();
+ let per_frame: Vec<usize> =
+ designcraft_compose::compose_story(&reference, sid, &Default::default()).frames.iter().map(|f| f.lines.len()).collect();
+ assert!(per_frame.first() == Some(&1) && per_frame.get(1).is_some_and(|n| *n > 0), "the break sends the second paragraph on");
+ let expected = story_lines(&reference);
+ assert_eq!(story_lines(&import(format!("{first}\n{deleted}\n{second}"))), expected);
+ }