mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-20 01:00:46 +01:00
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:
1 parent
5da32d1925
commit
bd8044dfd9
6 files changed
+192
-10
No files matched your search
@@ -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/<name>`) 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
|
||||
|
||||
+38
-4
@@ -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();
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user