From 1149d01150effac4e797fcfffa7734659e3ae18f Mon Sep 17 00:00:00 2001 From: aiongg <67543387+aiongg@users.noreply.github.com> Date: Sun, 11 Oct 2026 17:24:08 +0000 Subject: [PATCH] feat: write a paragraph range per paragraph-style record IDML starts a ParagraphStyleRange at every paragraph-style run record, also between neighbouring records with equal formatting. Document the rule and the paragraph length list of the records in objects.md, and the remaining splits inside one record in unknowns.md. --- docs/format/objects.md | 33 +++++++++++++++++++++++++++++++++ docs/format/unknowns.md | 2 ++ src/idml/story.rs | 25 +++++++++++++++++++++---- src/model/mod.rs | 1 + src/model/story.rs | 16 +++++++++++----- 5 files changed, 68 insertions(+), 9 deletions(-) diff --git a/docs/format/objects.md b/docs/format/objects.md index 532098c..058953c 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -2087,6 +2087,39 @@ Each record starts with a u32 run length in UTF-16 code units. Text records continue with text segments. Style records continue with the style UID. +**Paragraph-style records and paragraph ranges.** Counts are over the +INDD files of the 1,252 trustworthy pairs and the 37,156 stories that +both IDMLs have. + +- Every paragraph-style record ends at a paragraph end (U+000D): + 87,496 of 87,496 records. +- After its text attribute list (`attributes.md`), a paragraph-style + record has a u16 count and that many u32 values. In 82,842 of the + 87,496 records they are the lengths of the record's paragraphs, in + order, and add up to the run length. In the other 4,654 some values + have high bits set (for example 0x10000F74). The converter does not + read these values. +- IDML starts a `ParagraphStyleRange` at every paragraph-style record, + also between neighbouring records with the same style and the same + decoded attributes. There are 2,496 such boundaries in 459 stories: + 913 between two data objects of the strand and 1,583 within one. 153 + of them are between records whose bytes after the run length are + identical. Other rules split a record's range further (XML elements, + `xml.md`; tracked changes, below). +- With one range per record, the text lengths of the non-empty + paragraph ranges equal the reference in 37,133 of 37,156 stories, + including 455 of the 459 stories with equal neighbours. No story + has more ranges than the reference. Merging neighbours with equal + formatting matches 36,810 stories. Of the 23 other stories, 3 have + different text and 20 have ranges that the reference splits inside + one record (`unknowns.md`, resources and text). +- Character-style records with the same style and decoded attributes + are neighbours only across a data-object boundary: 3,288 boundaries + in 533 stories, none within one data object. IDML splits there too. + The converter writes a `CharacterStyleRange` per character-style + record (and further cuts), and the character range lengths equal the + reference in 525 of these 533 stories and in 37,112 of all 37,156. + **Stories outside the document's list.** The document's story list does not name the stories of text frames anchored in the XML backing story. IDML writes them like the others and lists them at the end of diff --git a/docs/format/unknowns.md b/docs/format/unknowns.md index dfdd3d9..651f72a 100644 --- a/docs/format/unknowns.md +++ b/docs/format/unknowns.md @@ -63,6 +63,8 @@ Status values: | Saving edition rule exceptions | tints, users, endnote and index titles | 9 | not needed | the IDML was exported by another edition than the one that saved the INDD (one trustworthy document, 8 stale pairs) | `objects.md`, saving edition | | `ColorGroup` `Self` | `ColorGroup` | 2 | open: not examined | 6 extra values: groups whose `Self` differs from the one the DOM version gives | `objects.md`, colour groups | | Order of stories outside the story list | `StoryList` | 2 pairs | open: not explained | IDML lists them in another order than text order | `objects.md`, stories | +| Paragraph ranges split inside one record | `ParagraphStyleRange` | 17 | open: not explained | 20 stories have 41 range boundaries inside one paragraph-style record, with the same attributes on both sides: 36 at a paragraph end, 5 before a paragraph's U+000D. Examples: every paragraph of a table-of-contents story whose paragraphs hold a hyperlink source; a final empty paragraph. No stored value was found that marks these positions | `objects.md`, stories | +| Character ranges split inside one record | `CharacterStyleRange` | 28 | open: not explained | 39 stories have character range boundaries inside one character-style record, with the same attributes on both sides | `objects.md`, stories | ## Page items diff --git a/src/idml/story.rs b/src/idml/story.rs index edd4d59..2b4b52d 100644 --- a/src/idml/story.rs +++ b/src/idml/story.rs @@ -1270,10 +1270,10 @@ impl Writer<'_> { } } -/// The paragraph formatting of a run: runs with the same one share a -/// paragraph range. -fn para_of(t: &TextRun) -> (Option, &crate::model::Attrs) { - (t.paragraph_style, &t.paragraph_attrs) +/// The paragraph formatting of a run and its paragraph-style record: runs +/// with the same ones share a paragraph range. +fn para_of(t: &TextRun) -> (Option, &crate::model::Attrs, usize) { + (t.paragraph_style, &t.paragraph_attrs, t.paragraph_run) } /// `runs` with each run that holds one of `cuts` strictly inside split @@ -1355,6 +1355,7 @@ mod tests { character_style: style, paragraph_attrs: Attrs::default(), character_attrs: Attrs::default(), + paragraph_run: 0, }; let story = Story { uid: 9, @@ -1430,6 +1431,7 @@ mod tests { character_style: style, paragraph_attrs: Attrs::default(), character_attrs: Attrs::default(), + paragraph_run: 0, }; let inserted = |start, len| ChangeRun { start, @@ -1531,6 +1533,7 @@ mod tests { character_style: None, paragraph_attrs: Attrs::default(), character_attrs: Attrs::default(), + paragraph_run: 0, }], anchors: Default::default(), tables: Default::default(), @@ -1613,9 +1616,23 @@ mod tests { character_style: character, paragraph_attrs: Default::default(), character_attrs: Default::default(), + paragraph_run: paragraph.unwrap_or(0) as usize, } } + #[test] + fn starts_a_paragraph_range_at_every_paragraph_style_record() { + let mut runs = vec![ + text_run(0, "a\r", Some(1), None), + text_run(2, "b\r", Some(1), None), + text_run(4, "c\r", Some(1), None), + ]; + runs[2].paragraph_run = 2; + let doc = Document::default(); + let out = Writer::for_test(&doc).story(&xml_story(runs, &[])); + assert_eq!(out.matches(" Option<(Option, Attrs)> { + let run_at = |list: &[StyleRun], at: usize| -> Option<(Option, Attrs, usize)> { let mut pos = 0; - for (len, style, attrs) in list { + for (k, (len, style, attrs)) in list.iter().enumerate() { if at < pos + len { - return Some((uid_or_none(*style), attrs.clone())); + return Some((uid_or_none(*style), attrs.clone(), k)); } pos += len; } @@ -254,8 +258,9 @@ pub(super) fn split_runs( cuts.windows(2) .filter(|w| w[0] < w[1]) .map(|w| { - let (paragraph_style, paragraph_attrs) = run_at(para, w[0]).unwrap_or_default(); - let (character_style, character_attrs) = run_at(chars, w[0]).unwrap_or_default(); + let (paragraph_style, paragraph_attrs, paragraph_run) = + run_at(para, w[0]).unwrap_or_default(); + let (character_style, character_attrs, _) = run_at(chars, w[0]).unwrap_or_default(); TextRun { start: w[0], text: String::from_utf16_lossy(&utf16[w[0]..w[1]]), @@ -263,6 +268,7 @@ pub(super) fn split_runs( character_style, paragraph_attrs, character_attrs, + paragraph_run, } }) .collect()