feat: write text and paragraph destinations in story text

Class 0x13504 objects are read as text or paragraph destinations (kind
in chunk 0x13526). The story's owned items of class 0x1353C name them at
their U+FEFF, which is now written as an empty HyperlinkTextDestination
or ParagraphDestination element instead of the character. Hyperlinks and
bookmarks can point at them.

Trustworthy pairs: reproduced 10,513,964 -> 10,530,141 (91.36 % ->
91.50 %); stories with identical text 18,517 -> 18,565; documents at
>= 99 % 18 -> 20; HyperlinkTextDestination 685 of 685,
ParagraphDestination 119 of 119, bookmarks 373 -> 452. Extra values
unchanged (26,634); no key reproduces fewer values. Schema validation: 0
failures. The 7 text destinations of InDesign 5.0 files (no key) are
left out with a warning, like their page and URL destinations.
This commit is contained in:
aiongg committed 2026-10-08 01:39:43 +00:00
1 parent 5da32d1925
commit bd8044dfd9
6 files changed
+192 -10

No files matched your search

+38 -4
View File
@@ -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<u32> {
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<u32> {
@@ -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<u32>,
) {
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();
+66
View File
@@ -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(
"<Content>x</Content><HyperlinkTextDestination \
Self=\"HyperlinkTextDestination/a%3ab\" Name=\"a:b\" Hidden=\"false\" \
DestinationUniqueKey=\"7\" /><Content>y</Content>"
),
"{out}"
);
}
#[test]
fn writes_xml_elements_at_their_markers() {
use crate::model::{Attrs, XmlStructure};
@@ -539,6 +604,7 @@ mod tests {
text_variables: Default::default(),
sources: Vec::new(),
xml_markers: markers.into_iter().collect(),
text_destinations: Default::default(),
xml_element: None,
orientation: None,
toc_style: None,
+3 -1
View File
@@ -410,7 +410,9 @@ impl<'a> Reader<'a> {
out.page_item_sources.push(s);
}
}
hyperlink::class::PAGE_DESTINATION | hyperlink::class::URL_DESTINATION => {
hyperlink::class::PAGE_DESTINATION
| hyperlink::class::URL_DESTINATION
| hyperlink::class::TEXT_DESTINATION => {
if let Some(d) = self.destination(uid, cls)? {
out.destinations.push(d);
}
+31 -5
View File
@@ -10,9 +10,13 @@ pub mod class {
pub const HYPERLINK: u32 = 0x13501;
pub const TEXT_SOURCE: u32 = 0x13502;
pub const PAGE_ITEM_SOURCE: u32 = 0x13503;
/// Text and paragraph destinations.
pub const TEXT_DESTINATION: u32 = 0x13504;
pub const PAGE_DESTINATION: u32 = 0x13505;
pub const URL_DESTINATION: u32 = 0x13506;
pub const BOOKMARK: u32 = 0x1354C;
/// An item owned by a story at the position of a text destination.
pub const DESTINATION_OWNER: u32 = 0x1353C;
/// A story strand that holds text ranges in a tree.
pub const RANGE_STRAND: u32 = 0xCA1C;
/// A node of a range tree.
@@ -39,6 +43,12 @@ pub mod chunk {
pub const PAGE_DESTINATION: u32 = 0x13507;
pub const PAGE_DESTINATION_VIEW: u32 = 0x13527;
pub const URL_DESTINATION: u32 = 0x13509;
/// Text destination: hidden, name and key.
pub const TEXT_DESTINATION: u32 = 0x13508;
/// Text destination: owner UID and kind.
pub const TEXT_DESTINATION_OWNER: u32 = 0x13526;
/// Destination owner: the destination UID.
pub const OWNER_DESTINATION: u32 = 0x1352B;
/// URL destination: the URL.
pub const URL: u32 = 0x100B;
pub const BOOKMARK: u32 = 0x13547;
@@ -171,6 +181,11 @@ pub enum DestinationKind {
Url {
url: String,
},
/// A position in story text (`HyperlinkTextDestination`).
Text,
/// A paragraph (`ParagraphDestination`), the target of
/// cross-references.
Paragraph,
}
#[derive(Debug, Clone, PartialEq)]
@@ -193,6 +208,8 @@ impl Destination {
match self.kind {
DestinationKind::Page { .. } => format!("HyperlinkPageDestination/{escaped}"),
DestinationKind::Url { .. } => format!("HyperlinkURLDestination/{escaped}"),
DestinationKind::Text => format!("HyperlinkTextDestination/{escaped}"),
DestinationKind::Paragraph => format!("ParagraphDestination/{escaped}"),
}
}
}
@@ -295,10 +312,10 @@ impl TextSource {
impl Destination {
pub fn read(uid: u32, class: u32, obj: &Object) -> Result<Option<Destination>, Error> {
let enc = obj.encoding;
let id = if class == class::PAGE_DESTINATION {
chunk::PAGE_DESTINATION
} else {
chunk::URL_DESTINATION
let id = match class {
class::PAGE_DESTINATION => chunk::PAGE_DESTINATION,
class::TEXT_DESTINATION => chunk::TEXT_DESTINATION,
_ => chunk::URL_DESTINATION,
};
let Some(d) = obj.chunk(id) else {
return Ok(None);
@@ -308,7 +325,16 @@ impl Destination {
c.skip(1)?;
let name = c.name()?.name;
let key = c.u32()?;
let kind = if class == class::PAGE_DESTINATION {
let kind = if class == class::TEXT_DESTINATION {
// Chunk 0x13526: owner UID, u16 kind (InDesign 5.0: the owner
// UID alone).
let owner = obj.chunk(chunk::TEXT_DESTINATION_OWNER);
match owner.map(|d| enc.u16_at(d, 4)) {
Some(Some(0) | None) => DestinationKind::Text,
Some(Some(1)) => DestinationKind::Paragraph,
_ => return Ok(None),
}
} else if class == class::PAGE_DESTINATION {
let Some(v) = obj.chunk(chunk::PAGE_DESTINATION_VIEW) else {
return Ok(None);
};
+20
View File
@@ -34,6 +34,8 @@ pub struct Story {
pub sources: Vec<SourceRange>,
/// XML markers, by UTF-16 offset of their U+FEFF.
pub xml_markers: BTreeMap<usize, XmlMarker>,
/// Text and paragraph destinations, by UTF-16 offset of their U+FEFF.
pub text_destinations: BTreeMap<usize, Vec<Destination>>,
/// The XML element whose content is this story.
pub xml_element: Option<xml::Key>,
/// Text orientation, from the frames that show the story; `None` when
@@ -309,8 +311,25 @@ impl<'a> Reader<'a> {
let mut anchors: BTreeMap<usize, Vec<PageItem>> = BTreeMap::new();
let mut tables: BTreeMap<usize, Table> = BTreeMap::new();
let mut text_variables = BTreeMap::new();
let mut text_destinations: BTreeMap<usize, Vec<Destination>> = BTreeMap::new();
for (pos, cls, item) in owned {
match cls {
hyperlink::class::DESTINATION_OWNER => {
// The owner names its destination (hyperlinks.md).
let dest = self
.chunk(item, hyperlink::chunk::OWNER_DESTINATION)?
.and_then(|d| self.enc().u32_at(&d, 0))
.and_then(uid_or_none);
if let Some(dest) = dest
&& let Some(d) = Destination::read(
dest,
hyperlink::class::TEXT_DESTINATION,
&*self.object(dest)?,
)?
{
text_destinations.entry(pos).or_default().push(d);
}
}
class::TEXT_VARIABLE_INSTANCE => {
let v = variable::Instance::read(item, &*self.object(item)?)?;
text_variables.insert(pos, v);
@@ -401,6 +420,7 @@ impl<'a> Reader<'a> {
text_variables,
sources,
xml_markers,
text_destinations,
xml_element,
orientation: None,
// The story names an object (class 0x8C20) whose chunk 0x11613