mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-22 18:20:45 +01:00
feat: read hyperlinks of InDesign 5.0 files
InDesign 5.0 destinations have no key, hyperlinks name their destination by UID, text destination owners use chunk 0x1351E, and text sources are placed by start and end markers owned by the story (chunk 0x13524) instead of a range tree. These are read by version; keys are left out where there are none. No 5.0 file has an IDML. Over the 17 5.0 files with hyperlinks: 121 hyperlinks, 118 text sources, 7 text destinations and 45 bookmarks are now written, the 116 warnings about their destination objects are gone, and every output validates. Pair results unchanged (91.92 %, extra values 26,516). Schema validation: 0 failures.
This commit is contained in:
1 parent
18449cc430
commit
64e5667dfd
8 files changed
+150
-21
No files matched your search
@@ -609,7 +609,7 @@ impl Writer<'_> {
|
||||
DestinationKind::Text | DestinationKind::Paragraph => continue,
|
||||
}
|
||||
x.attr("Hidden", d.hidden.to_string())
|
||||
.attr("DestinationUniqueKey", d.key.to_string());
|
||||
.attr_opt("DestinationUniqueKey", d.key.map(|k| k.to_string()));
|
||||
if let Some([left, top, right, bottom]) = bounds {
|
||||
// IDML writes the unset bounds (1e256) in exponent form.
|
||||
let n = |v: f64| if v == 1e256 { "1e+256".into() } else { num(v) };
|
||||
@@ -647,7 +647,10 @@ impl Writer<'_> {
|
||||
// hyperlink without one (hyperlinks.md).
|
||||
let dest = if h.other_document {
|
||||
None
|
||||
} else if let Some(d) = dests.iter().find(|d| d.key == h.key) {
|
||||
} else if let Some(d) = dests.iter().find(|d| match h.destination {
|
||||
Some(u) => d.uid == u,
|
||||
None => h.key.is_some() && d.key == h.key,
|
||||
}) {
|
||||
Some(d.reference())
|
||||
} else if h.kind_is_none() {
|
||||
Some("n".to_string())
|
||||
@@ -675,7 +678,7 @@ impl Writer<'_> {
|
||||
x.attr("Width", "Thin").attr("BorderStyle", "Solid");
|
||||
}
|
||||
x.attr("Hidden", h.hidden.to_string())
|
||||
.attr("DestinationUniqueKey", h.key.to_string());
|
||||
.attr_opt("DestinationUniqueKey", h.key.map(|k| k.to_string()));
|
||||
x.start("Properties");
|
||||
// Black on every corpus hyperlink with the known appearance,
|
||||
// apart from those to a page of another document.
|
||||
|
||||
+2
-2
@@ -272,7 +272,7 @@ impl Writer<'_> {
|
||||
.attr("Self", d.reference())
|
||||
.attr("Name", &d.name)
|
||||
.attr("Hidden", d.hidden.to_string())
|
||||
.attr("DestinationUniqueKey", d.key.to_string())
|
||||
.attr_opt("DestinationUniqueKey", d.key.map(|k| k.to_string()))
|
||||
.end();
|
||||
}
|
||||
if !story.xml_markers.contains_key(&pos) {
|
||||
@@ -686,7 +686,7 @@ mod tests {
|
||||
uid: 5,
|
||||
name: "a:b".into(),
|
||||
hidden: false,
|
||||
key: 7,
|
||||
key: Some(7),
|
||||
kind: DestinationKind::Text,
|
||||
};
|
||||
let story = Story {
|
||||
|
||||
@@ -120,6 +120,14 @@ impl Xml {
|
||||
self
|
||||
}
|
||||
|
||||
/// Set attribute `name` when there is a value.
|
||||
pub fn attr_opt(&mut self, name: &str, value: Option<impl AsRef<str>>) -> &mut Self {
|
||||
if let Some(v) = value {
|
||||
self.attr(name, v);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the open start tag has attribute `name`.
|
||||
pub fn has_attr(&self, name: &str) -> bool {
|
||||
self.open && self.attrs.iter().any(|(k, _)| k == name)
|
||||
|
||||
@@ -133,6 +133,7 @@ impl<'a> Reader<'a> {
|
||||
/// An object that cannot be read is left out with a warning; the
|
||||
/// warnings are in `Document::warnings`.
|
||||
pub fn document(&self, version: Version) -> Result<Document, Error> {
|
||||
self.major.set(version.major);
|
||||
// The first layer of the list is the internal layer, whatever its
|
||||
// name (objects.md, layers).
|
||||
let layers = self
|
||||
@@ -396,7 +397,7 @@ impl<'a> Reader<'a> {
|
||||
}
|
||||
}
|
||||
hyperlink::class::HYPERLINK => {
|
||||
if let Some(h) = Hyperlink::read(uid, &*self.object(uid)?)? {
|
||||
if let Some(h) = Hyperlink::read(uid, &*self.object(uid)?, self.major.get())? {
|
||||
out.hyperlinks.push(h);
|
||||
}
|
||||
}
|
||||
|
||||
+37
-8
@@ -19,6 +19,10 @@ pub mod class {
|
||||
pub const BOOKMARK: u32 = 0x1354C;
|
||||
/// An item owned by a story at the position of a text destination.
|
||||
pub const DESTINATION_OWNER: u32 = 0x1353C;
|
||||
/// InDesign 5.0: items owned by a story at the first and the last
|
||||
/// character of a text source.
|
||||
pub const SOURCE_START: u32 = 0x13508;
|
||||
pub const SOURCE_END: u32 = 0x1353B;
|
||||
/// A story strand that holds text ranges in a tree.
|
||||
pub const RANGE_STRAND: u32 = 0xCA1C;
|
||||
/// A node of a range tree.
|
||||
@@ -57,6 +61,10 @@ pub mod chunk {
|
||||
pub const TEXT_DESTINATION_OWNER: u32 = 0x13526;
|
||||
/// Destination owner: the destination UID.
|
||||
pub const OWNER_DESTINATION: u32 = 0x1352B;
|
||||
/// Destination owner in InDesign 5.0: the destination UID at offset 8.
|
||||
pub const OWNER_DESTINATION_5: u32 = 0x1351E;
|
||||
/// Text source in InDesign 5.0: start and end marker UIDs.
|
||||
pub const SOURCE_MARKERS: u32 = 0x13524;
|
||||
/// URL destination: the URL.
|
||||
pub const URL: u32 = 0x100B;
|
||||
pub const BOOKMARK: u32 = 0x13547;
|
||||
@@ -72,8 +80,11 @@ pub struct Hyperlink {
|
||||
pub name: String,
|
||||
pub source: u32,
|
||||
pub hidden: bool,
|
||||
/// `DestinationUniqueKey`, shared with the destination.
|
||||
pub key: u32,
|
||||
/// `DestinationUniqueKey`, shared with the destination; `None` in
|
||||
/// InDesign 5.0 files.
|
||||
pub key: Option<u32>,
|
||||
/// The destination UID (InDesign 5.0 files, which have no keys).
|
||||
pub destination: Option<u32>,
|
||||
/// Destination kind (u32 at offset 12 of chunk 0x13502).
|
||||
pub kind: u32,
|
||||
/// Chunk 0x13553 has the pattern of every corpus hyperlink, whose
|
||||
@@ -216,7 +227,9 @@ pub struct Destination {
|
||||
pub uid: u32,
|
||||
pub name: String,
|
||||
pub hidden: bool,
|
||||
pub key: u32,
|
||||
/// `DestinationUniqueKey`; `None` in InDesign 5.0 files, whose
|
||||
/// destination chunks end after the name.
|
||||
pub key: Option<u32>,
|
||||
pub kind: DestinationKind,
|
||||
}
|
||||
|
||||
@@ -274,7 +287,8 @@ impl Hyperlink {
|
||||
self.kind == kind::EXTERNAL_PAGE
|
||||
}
|
||||
|
||||
pub fn read(uid: u32, obj: &Object) -> Result<Option<Hyperlink>, Error> {
|
||||
/// Read a hyperlink of a document of major version `major`.
|
||||
pub fn read(uid: u32, obj: &Object, major: u32) -> Result<Option<Hyperlink>, Error> {
|
||||
let enc = obj.encoding;
|
||||
let Some(d) = obj.chunk(chunk::HYPERLINK) else {
|
||||
return Ok(None);
|
||||
@@ -283,9 +297,19 @@ impl Hyperlink {
|
||||
let source = c.u32()?;
|
||||
c.skip(2)?;
|
||||
let hidden = c.u16()? != 0;
|
||||
let key = c.u32()?;
|
||||
let kind = c.u32()?;
|
||||
c.skip(8)?;
|
||||
// InDesign 5.0: the destination UID twice, then the kind;
|
||||
// later: the key, the kind and a u32 not identified.
|
||||
let (key, destination, kind) = if major <= 5 {
|
||||
let dest = c.u32()?;
|
||||
c.skip(4)?;
|
||||
(None, (dest != 0).then_some(dest), c.u32()?)
|
||||
} else {
|
||||
let key = c.u32()?;
|
||||
let kind = c.u32()?;
|
||||
c.skip(4)?;
|
||||
(Some(key), None, kind)
|
||||
};
|
||||
c.skip(4)?;
|
||||
let name = c.name()?.name;
|
||||
let highlight = appearance(obj.chunk(chunk::HYPERLINK_APPEARANCE));
|
||||
Ok(Some(Hyperlink {
|
||||
@@ -294,6 +318,7 @@ impl Hyperlink {
|
||||
source,
|
||||
hidden,
|
||||
key,
|
||||
destination,
|
||||
kind,
|
||||
appearance_known: highlight.is_some(),
|
||||
highlight,
|
||||
@@ -360,7 +385,11 @@ impl Destination {
|
||||
let hidden = c.u8()? != 0;
|
||||
c.skip(1)?;
|
||||
let name = c.name()?.name;
|
||||
let key = c.u32()?;
|
||||
let key = if c.remaining() >= 4 {
|
||||
Some(c.u32()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let kind = if class == class::TEXT_DESTINATION {
|
||||
// Chunk 0x13526: owner UID, u16 kind (InDesign 5.0: the owner
|
||||
// UID alone).
|
||||
|
||||
@@ -16,6 +16,11 @@ pub struct Reader<'a> {
|
||||
pub(super) frame_orientations: std::cell::RefCell<BTreeMap<u32, Vec<Option<Orientation>>>>,
|
||||
/// The groups whose page items are being read, outermost first.
|
||||
pub(super) item_path: std::cell::RefCell<Vec<u32>>,
|
||||
/// The major version of the document, set when it is read.
|
||||
pub(super) major: std::cell::Cell<u32>,
|
||||
/// InDesign 5.0 text sources by their start marker: (source, end
|
||||
/// marker). Built on first use.
|
||||
pub(super) source_markers: std::cell::OnceCell<HashMap<u32, (u32, u32)>>,
|
||||
}
|
||||
|
||||
pub(super) fn uid_or_none(v: u32) -> Option<u32> {
|
||||
@@ -31,6 +36,8 @@ impl<'a> Reader<'a> {
|
||||
xml_nodes: Default::default(),
|
||||
frame_orientations: Default::default(),
|
||||
item_path: Default::default(),
|
||||
major: Default::default(),
|
||||
source_markers: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+54
-4
@@ -261,6 +261,31 @@ impl<'a> Reader<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
// InDesign 5.0 text sources: items owned at their first and last
|
||||
// character (hyperlinks.md).
|
||||
if self.major.get() <= 5 {
|
||||
let ends: HashMap<u32, usize> = owned
|
||||
.iter()
|
||||
.filter(|o| o.1 == hyperlink::class::SOURCE_END)
|
||||
.map(|o| (o.2, o.0))
|
||||
.collect();
|
||||
for &(pos, cls, item) in &owned {
|
||||
if cls != hyperlink::class::SOURCE_START {
|
||||
continue;
|
||||
}
|
||||
if let Some(&(source, end)) = self.source_markers()?.get(&item)
|
||||
&& let Some(&last) = ends.get(&end)
|
||||
&& last >= pos
|
||||
{
|
||||
sources.push(SourceRange {
|
||||
start: pos,
|
||||
len: last + 1 - pos,
|
||||
source,
|
||||
paragraph: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
// Text records count UTF-16 code units, every other strand counts
|
||||
// characters (a surrogate pair is one position). Convert the
|
||||
// positions to UTF-16 offsets.
|
||||
@@ -316,10 +341,14 @@ impl<'a> Reader<'a> {
|
||||
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);
|
||||
let dest = match self.chunk(item, hyperlink::chunk::OWNER_DESTINATION)? {
|
||||
Some(d) => self.enc().u32_at(&d, 0),
|
||||
// InDesign 5.0.
|
||||
None => self
|
||||
.chunk(item, hyperlink::chunk::OWNER_DESTINATION_5)?
|
||||
.and_then(|d| self.enc().u32_at(&d, 8)),
|
||||
}
|
||||
.and_then(uid_or_none);
|
||||
if let Some(dest) = dest
|
||||
&& let Some(d) = Destination::read(
|
||||
dest,
|
||||
@@ -482,6 +511,27 @@ impl<'a> Reader<'a> {
|
||||
})
|
||||
}
|
||||
|
||||
/// InDesign 5.0 text sources by their start marker: (source UID, end
|
||||
/// marker UID), from chunk 0x13524 of every text source.
|
||||
fn source_markers(&self) -> Result<&HashMap<u32, (u32, u32)>, Error> {
|
||||
if let Some(m) = self.source_markers.get() {
|
||||
return Ok(m);
|
||||
}
|
||||
let mut m = HashMap::new();
|
||||
for &(uid, cls) in self.db.classes() {
|
||||
if cls != hyperlink::class::TEXT_SOURCE || self.db.object(uid)?.is_none() {
|
||||
continue;
|
||||
}
|
||||
if let Some(d) = self.chunk(uid, hyperlink::chunk::SOURCE_MARKERS)?
|
||||
&& let (Some(start), Some(end)) =
|
||||
(self.enc().u32_at(&d, 0), self.enc().u32_at(&d, 4))
|
||||
{
|
||||
m.insert(start, (uid, end));
|
||||
}
|
||||
}
|
||||
Ok(self.source_markers.get_or_init(|| m))
|
||||
}
|
||||
|
||||
/// Read the XML nodes stored with story `uid` and place their markers.
|
||||
/// `unit` converts a character position to a UTF-16 offset; `para` are
|
||||
/// the paragraph runs, in UTF-16 units. An element whose start and end
|
||||
|
||||
Reference in new issue
Block a user