diff --git a/docs/format/hyperlinks.md b/docs/format/hyperlinks.md index 8297386..7daba6b 100644 --- a/docs/format/hyperlinks.md +++ b/docs/format/hyperlinks.md @@ -196,6 +196,22 @@ The converter writes: `Cell`, so the 5 story-level sources (all in one pair) cannot be written validly. +**Sources at inserted text.** Where a tracked change run with an +insertion entry (`objects.md`, tracked changes) starts at a source's +first character and ends before the source does, IDML writes an empty +``, with the run's attributes, before +the source, and writes the source's first character without a `Change`. +For a paragraph-level source the empty `Change` is the last child of a +`CharacterStyleRange` of its own, with the attributes of the source's +first range, after the text deleted there (`Change DeletedText`); for a +character-level source it is in the open range, after the deleted text. +A source that ends inside the run or with it is inside the run's +`Change` instead; the converter writes the `Change` inside such a +source. Trustworthy pairs: IDML has 1,681 empty `Change` elements in 19 +documents, each directly before a source (1,668 paragraph-level, 13 +character-level), and the rule gives the same sources in every pair +(stale pairs: 385 of 385). + A paragraph-level source that holds an XML marker is left out with a warning; a character-level source ends at an XML marker. A character-level source that holds an anchored object is left out (below); at paragraph diff --git a/docs/format/objects.md b/docs/format/objects.md index f898627..49d07c3 100644 --- a/docs/format/objects.md +++ b/docs/format/objects.md @@ -1689,11 +1689,16 @@ change that crosses character ranges or elements is written as several `Change` elements. Over all pairs, 6,024 of the 6,027 reference `Change` elements have an equal `Change` in the output (`Date`, `ChangeType`, `UserName`, `AppliedDocumentUser`, compared as multisets per pair); the -output has 92 more, from such splits. Where a paragraph-level hyperlink -source starts at an inserted character, InDesign writes an empty -`Change` before the source (1,668 of the 1,681 empty changes in the -research run); the converter writes the inserted text inside the -`Change` instead. +output has 92 more, from such splits. Where a hyperlink source starts +with inserted text, IDML writes an empty `Change` before the source +(`hyperlinks.md`, sources at inserted text). + +A deletion owned by the U+000D that ends a table cell, which no +character range holds, is the last child of the `Cell`, after its last +`ParagraphStyleRange` (2 in one trustworthy pair, 1 in a stale pair; +every other deletion of the corpus is owned by a character inside a +range). With it the converter writes all 1,889 deleted-text `Change` +elements of the trustworthy pairs. **Dates of notes and changes.** IDML writes `CreationDate`, `ModificationDate` and `Date` as local time of the exporting computer, diff --git a/src/idml/story.rs b/src/idml/story.rs index 70b7bfe..a4e5b7d 100644 --- a/src/idml/story.rs +++ b/src/idml/story.rs @@ -15,6 +15,21 @@ pub(super) struct TextState { inline: Vec, /// Elements open between character ranges. blocks: Vec, + /// A character written without its `Change` and deleted text, which + /// were written before the text source that starts there. + bare: Option, +} + +/// The insertion entry of a change run of inserted text that starts where +/// a text source of `len` characters starts, at `pos`, and ends before +/// the source does. +fn insertion_at_source(story: &Story, pos: usize, len: usize) -> Option<&ChangeEntry> { + let i = story.changes.partition_point(|c| c.start < pos); + let c = story + .changes + .get(i) + .filter(|c| c.start == pos && c.len < len)?; + c.entries.iter().find(|e| e.is_insertion()) } impl TextState { @@ -243,6 +258,19 @@ impl Writer<'_> { && let Some(s) = sources.iter().find(|s| s.start == r.start) && let Some(src) = self.doc.text_sources.get(&s.source) { + // A source that starts with inserted text and goes on + // past it follows a character range with the deleted + // text there and an empty `Change` (hyperlinks.md, + // sources at inserted text). + if let Some(e) = insertion_at_source(story, r.start, s.len) { + self.csr_start(x, r); + self.deleted_text(x, story, r.start, scope); + x.start("Change"); + self.change_attrs(x, e, "InsertedText"); + x.end(); + x.end(); + st.bare = Some(r.start); + } self.source_start(x, src); open = Some(s.start + s.len); } @@ -340,26 +368,11 @@ impl Writer<'_> { } // Deleted text of tracked changes, before the character that // follows it (objects.md, tracked changes). - if let Some(ds) = story.deletions.get(&pos) { + if st.bare != Some(pos) && story.deletions.contains_key(&pos) { close_inserted(x, &mut buf, &mut inserted); flush(x, &mut buf); st.open_csr(self, x, run); - let mut entries = story - .changes - .iter() - .filter(|c| c.start == pos) - .flat_map(|c| &c.entries) - .filter(|e| e.is_deletion()); - for d in ds { - x.start("Change"); - if let Some(e) = entries.next() { - self.change_attrs(x, e, "DeletedText"); - } else { - x.attr("ChangeType", "DeletedText"); - } - self.text_ranges(x, &d.runs, story, scope); - x.end(); - } + self.deleted_text(x, story, pos, scope); } // A character-level text source opens before its first // character, which can be an element (a note, a text @@ -382,6 +395,15 @@ impl Writer<'_> { close_inserted(x, &mut buf, &mut inserted); flush(x, &mut buf); st.open_csr(self, x, run); + // A source that starts with inserted text and goes on past + // it follows an empty `Change`, and its first character has + // none (hyperlinks.md, sources at inserted text). + if let Some(e) = insertion_at_source(story, pos, r.len) { + x.start("Change"); + self.change_attrs(x, e, "InsertedText"); + x.end(); + st.bare = Some(pos); + } self.source_start(x, src); open = Some(r.start + r.len); } @@ -395,7 +417,9 @@ impl Writer<'_> { .filter(|c| c.start <= pos && c.entries.iter().any(|e| e.is_insertion())) .map(|_| want) .filter(|_| { - !matches!(ch, '\u{16}' | '\u{FFFC}') && !story.xml_markers.contains_key(&pos) + !matches!(ch, '\u{16}' | '\u{FFFC}') + && !story.xml_markers.contains_key(&pos) + && st.bare != Some(pos) }); if inserted != want { close_inserted(x, &mut buf, &mut inserted); @@ -579,6 +603,30 @@ impl Writer<'_> { } } + /// The deleted text of tracked changes owned by the character at + /// `pos`, each in a `Change` (objects.md, tracked changes). + fn deleted_text(&self, x: &mut Xml, story: &Story, pos: usize, scope: &str) { + let Some(ds) = story.deletions.get(&pos) else { + return; + }; + let mut entries = story + .changes + .iter() + .filter(|c| c.start == pos) + .flat_map(|c| &c.entries) + .filter(|e| e.is_deletion()); + for d in ds { + x.start("Change"); + if let Some(e) = entries.next() { + self.change_attrs(x, e, "DeletedText"); + } else { + x.attr("ChangeType", "DeletedText"); + } + self.text_ranges(x, &d.runs, story, scope); + x.end(); + } + } + /// The attributes of a `Change`, in IDML's order (objects.md, tracked /// changes). fn change_attrs(&self, x: &mut Xml, e: &ChangeEntry, kind: &str) { @@ -792,6 +840,14 @@ impl Writer<'_> { .attr("AppliedCellStylePriority", "0"); } self.text_ranges(x, &c.runs, story, &cell_id); + // Deleted text owned by the cell's terminator, which no range + // holds, ends the cell (objects.md, tracked changes). + if let Some(r) = c.runs.last() + && r.text.ends_with('\r') + { + let end = r.start + r.text.encode_utf16().count() - 1; + self.deleted_text(x, story, end, &cell_id); + } x.end(); } x.end();