diff --git a/docs/format/hyperlinks.md b/docs/format/hyperlinks.md index c9740a1..ddbb6b8 100644 --- a/docs/format/hyperlinks.md +++ b/docs/format/hyperlinks.md @@ -8,6 +8,7 @@ Implemented in `src/model/hyperlink.rs`. | 0x13501 | Hyperlink | `Hyperlink` (designmap) | | 0x13502 | Text source | `HyperlinkTextSource` (story text) | | 0x13503 | Page item source | `HyperlinkPageItemSource` (designmap) | +| 0x13504 | Text or paragraph destination | `HyperlinkTextDestination`, `ParagraphDestination` (story text) | | 0x13505 | Page destination | `HyperlinkPageDestination` (designmap) | | 0x13506 | URL destination | `HyperlinkURLDestination` (designmap) | | 0x1354C | Bookmark | `Bookmark` (designmap) | @@ -181,6 +182,33 @@ Over 41 destinations (39, plus 2 in a pair from another save): `Self` is `HyperlinkPageDestination/` and the name. +## Text and paragraph destinations (class 0x13504) + +**Chunk 0x13508:** u8 hidden (1 true), u8, flag byte and string +(`Name`), u32 key (`DestinationUniqueKey`). **Chunk 0x13526:** u32 owner +UID (class 0x1353C), u16 kind: 0 `HyperlinkTextDestination` (1,021 of +1,021), 1 `ParagraphDestination` (169 of 169). + +The owner object is an owned item (run kind 0x209, `objects.md`) of the +story strand at the destination's position, and that position holds +U+FEFF (1,190 of 1,190). The owner's chunk 0x1352B starts with the +destination UID (1,021 of 1,021 text destinations checked). IDML writes +the destination as an empty element at that position, in place of the +U+FEFF, inside the `CharacterStyleRange` (746 text destinations and all +169 paragraph destinations; the other 275 are inside tracked changes). +`Self` is `HyperlinkTextDestination/` or `ParagraphDestination/` and the +name, escaped as for page destinations. Attributes: `Self`, `Name`, +`Hidden`, `DestinationUniqueKey`, in that order. Names need not be +unique: one pair has 7 text destinations with the same name, and IDML +writes 7 elements with the same `Self`. + +The converter writes them so: in the trustworthy pairs 685 of 685 text +and 119 of 119 paragraph destinations are reproduced with all values, +and 48 stories whose text differed only by the U+FEFF became identical. +Hyperlinks find them by key and bookmarks by UID, like the other +destinations; a destination that is not written in a story (inside text +the converter leaves out) is not referred to. + ## URL destination (class 0x13506) **Chunk 0x13509:** u8 hidden, u8, flag byte and string (`Name`), u32 key. @@ -205,6 +233,12 @@ a bookmark), UID list of child bookmarks, u32 destination UID. All 7 bookmarks match on `Name` and `Destination` (the destination's `Self`), and IDML nests each bookmark's children inside it, in the order of the child list (2 top-level bookmarks with 2 and 3 children). +Over the 1,072 bookmarks of the later corpus, name, parent and child +order match in all. The destination UID can also be a text destination +(82 of 82, written `HyperlinkTextDestination/`) or an external page +destination (575 of 575, written as its UID). The u32 after the name (0 +to 4) grows with nesting but is not the same depth in every file; no +attribute matches it. The document's chunk 0x13501 is u32, u16, then UID lists of the text sources, the hyperlinks and the bookmarks, then a fourth list. The diff --git a/src/idml/designmap.rs b/src/idml/designmap.rs index 5762c32..de5141f 100644 --- a/src/idml/designmap.rs +++ b/src/idml/designmap.rs @@ -490,6 +490,16 @@ impl Writer<'_> { .collect() } + /// UIDs of the text and paragraph destinations written in stories. + pub(super) fn written_text_destinations(&self) -> std::collections::HashSet { + self.doc + .stories + .iter() + .flat_map(|s| s.text_destinations.values().flatten()) + .map(|d| d.uid) + .collect() + } + /// UIDs of the page items written: on spreads and master spreads, in /// groups and anchored in stories. pub(super) fn written_items(&self) -> std::collections::HashSet { @@ -517,7 +527,17 @@ impl Writer<'_> { /// docs/format/hyperlinks.md. pub(super) fn hyperlinks(&self, x: &mut Xml) { let doc = self.doc; - let mut dests: Vec<_> = doc.destinations.iter().collect(); + // Text and paragraph destinations are written in the stories, at + // their position; only those found there can be referred to. + let in_text = self.written_text_destinations(); + let mut dests: Vec<_> = doc + .destinations + .iter() + .filter(|d| { + !matches!(d.kind, DestinationKind::Text | DestinationKind::Paragraph) + || in_text.contains(&d.uid) + }) + .collect(); dests.sort_by_key(|d| (matches!(d.kind, DestinationKind::Url { .. }), d.key)); for d in &dests { match &d.kind { @@ -550,6 +570,7 @@ impl Writer<'_> { .attr("Name", &d.name) .attr("DestinationURL", url); } + DestinationKind::Text | DestinationKind::Paragraph => continue, } x.attr("Hidden", d.hidden.to_string()) .attr("DestinationUniqueKey", d.key.to_string()); @@ -643,22 +664,35 @@ impl Writer<'_> { .filter_map(|u| doc.bookmarks.get(u)) .filter(|b| !doc.bookmarks.contains_key(&b.parent)); for b in top { - self.bookmark(x, b, 0); + self.bookmark(x, b, 0, &in_text); } } - pub(super) fn bookmark(&self, x: &mut Xml, b: &crate::model::Bookmark, depth: usize) { + pub(super) fn bookmark( + &self, + x: &mut Xml, + b: &crate::model::Bookmark, + depth: usize, + in_text: &std::collections::HashSet, + ) { let doc = self.doc; let Some(dest) = doc.destinations.iter().find(|d| d.uid == b.destination) else { return; }; + if matches!( + dest.kind, + DestinationKind::Text | DestinationKind::Paragraph + ) && !in_text.contains(&dest.uid) + { + return; + } x.start("Bookmark") .attr("Self", uref(Some(b.uid))) .attr("Name", &b.name) .attr("Destination", dest.reference()); if depth < 64 { for c in b.children.iter().filter_map(|u| doc.bookmarks.get(u)) { - self.bookmark(x, c, depth + 1); + self.bookmark(x, c, depth + 1, in_text); } } x.end(); diff --git a/src/idml/story.rs b/src/idml/story.rs index 9127ea6..f92b137 100644 --- a/src/idml/story.rs +++ b/src/idml/story.rs @@ -214,6 +214,28 @@ impl Writer<'_> { x.end(); open = None; } + if ch == '\u{FEFF}' + && let Some(dests) = story.text_destinations.get(&pos) + { + // Written as empty elements in place of the character. + flush(x, &mut buf); + st.open_csr(self, x, run); + for d in dests { + x.start(match d.kind { + DestinationKind::Paragraph => "ParagraphDestination", + _ => "HyperlinkTextDestination", + }) + .attr("Self", d.reference()) + .attr("Name", &d.name) + .attr("Hidden", d.hidden.to_string()) + .attr("DestinationUniqueKey", d.key.to_string()) + .end(); + } + if !story.xml_markers.contains_key(&pos) { + pos += 1; + continue; + } + } if ch == '\u{FEFF}' && let Some(&m) = story.xml_markers.get(&pos) { @@ -504,6 +526,49 @@ impl Writer<'_> { mod tests { use super::*; + #[test] + fn writes_text_destinations_in_place_of_their_character() { + use crate::model::{Attrs, Destination}; + let dest = Destination { + uid: 5, + name: "a:b".into(), + hidden: false, + key: 7, + kind: DestinationKind::Text, + }; + let story = Story { + uid: 9, + runs: vec![TextRun { + start: 0, + text: "x\u{FEFF}y\r".into(), + paragraph_style: None, + character_style: None, + paragraph_attrs: Attrs::default(), + character_attrs: Attrs::default(), + }], + anchors: Default::default(), + tables: Default::default(), + text_variables: Default::default(), + sources: Vec::new(), + xml_markers: Default::default(), + text_destinations: [(1, vec![dest])].into_iter().collect(), + xml_element: None, + orientation: None, + toc_style: None, + }; + let doc = Document::default(); + let w = Writer::for_test(&doc); + let out: String = w.story(&story).lines().map(str::trim).collect(); + assert!( + out.contains( + "x