diff --git a/docs/format/hyperlinks.md b/docs/format/hyperlinks.md index 0e46afb..db1d34d 100644 --- a/docs/format/hyperlinks.md +++ b/docs/format/hyperlinks.md @@ -11,6 +11,7 @@ Implemented in `src/model/hyperlink.rs`. | 0x13504 | Text or paragraph destination | `HyperlinkTextDestination`, `ParagraphDestination` (story text) | | 0x13505 | Page destination | `HyperlinkPageDestination` (designmap) | | 0x13506 | URL destination | `HyperlinkURLDestination` (designmap) | +| 0x13552 | External page destination | `HyperlinkExternalPageDestination` (designmap) | | 0x1354C | Bookmark | `Bookmark` (designmap) | ## Evidence @@ -296,6 +297,36 @@ with that `mailto:` URL as `DestinationURL`. Chunk 0x100B is therefore taken as the URL. `Self` is `HyperlinkURLDestination/` and the name with `:` written as `%3a`. +## External page destination (class 0x13552) + +| Chunk | Contents | IDML | +|---|---|---| +| 0x13580 | u8 hidden, u8 0, flag byte 1, string (empty in all), u32 key | `Hidden`, `DestinationUniqueKey` | +| 0x1B8 | u32 page index from 0 | `DestinationPageIndex` = value + 1 | +| 0x13527 | as for page destinations: u32 page (0), f64 zoom, u32 view setting, four f64 | `ViewPercentage`, `ViewSetting`, `ViewBounds` | +| 0x1359F | u32 0x13501, u32 1, u32 link UID (class 0x8C42) | `DocumentPath` (below) | + +Evidence: 643 destinations in two pairs (all pairs). `Self` is the UID. +The name is not stored: IDML writes ` - Page + [Fixed]`, where the file name is the last part of +the linked document's URI (`objects.md`, links), percent-decoded (643 of +643; all have view setting 0, `Fixed`, so the text for other settings is +not shown, and the converter writes `Name` only for view setting 0). +`DocumentPath` is that path with `/` written as `:`, percent-decoding +applied, preceded by a disk volume name and `:` (643 of 643 apart from +the volume name, which the INDD does not hold), so the converter leaves +it out. The flag byte 1 with an empty name, against 0 or 2 with a stored +name for page destinations, suggests the flag marks an automatic name; +IDML writes no `NameManually` here. Attribute order: `Self Name +DocumentPath DestinationPageIndex ViewSetting ViewPercentage Hidden +DestinationUniqueKey`, then `Properties/ViewBounds`. They follow the URL +destinations, sorted by key. + +Hyperlinks of kind 2004 point at them by key (576), and bookmarks by UID +(575). In the trustworthy pairs the converter writes all 643 with every +value apart from `DocumentPath`, and their hyperlinks apart from +`BorderColor` (see the hyperlink appearance). + ## Bookmarks (class 0x1354C) **Chunk 0x13547:** flag byte and string (`Name`), u32 (1 for a top-level @@ -340,10 +371,11 @@ described above, so the converter leaves them out with a warning. ## Order in designmap -The schema puts page destinations, URL destinations and hyperlinks after -the `idPkg:Story` elements, and bookmarks after them. The converter sorts -destinations by key, as the pair with the most destinations does, and -hyperlinks by UID. +The schema puts page destinations, URL destinations, external page +destinations, page item sources and hyperlinks after the `idPkg:Story` +elements, in that order, and bookmarks after them. The converter sorts +each kind of destination by key, as the pairs do, and hyperlinks by +UID. ## Not converted diff --git a/src/idml/designmap.rs b/src/idml/designmap.rs index de5141f..cd8f40e 100644 --- a/src/idml/designmap.rs +++ b/src/idml/designmap.rs @@ -538,11 +538,38 @@ impl Writer<'_> { || in_text.contains(&d.uid) }) .collect(); - dests.sort_by_key(|d| (matches!(d.kind, DestinationKind::Url { .. }), d.key)); + // Schema order: page, URL, external page destinations; each sorted + // by key. + dests.sort_by_key(|d| { + let rank = match d.kind { + DestinationKind::Page { .. } => 0, + DestinationKind::Url { .. } => 1, + _ => 2, + }; + (rank, d.key) + }); + let view_setting = |x: &mut Xml, view: u32, zoom: Option| { + match view { + 0 => { + x.attr("ViewSetting", "Fixed"); + } + 1 => { + x.attr("ViewSetting", "FitWindow"); + } + _ => {} + } + if let Some(zoom) = zoom { + x.attr("ViewPercentage", num(zoom * 100.0)); + } + }; for d in &dests { + let mut bounds = None; match &d.kind { DestinationKind::Page { - page, zoom, view, .. + page, + zoom, + view, + bounds: b, } => { // NameManually is true on every corpus page // destination (hyperlinks.md). @@ -551,18 +578,8 @@ impl Writer<'_> { .attr("Name", &d.name) .attr("NameManually", "true") .attr("DestinationPage", uref(Some(*page))); - match view { - 0 => { - x.attr("ViewSetting", "Fixed"); - } - 1 => { - x.attr("ViewSetting", "FitWindow"); - } - _ => {} - } - if let Some(zoom) = zoom { - x.attr("ViewPercentage", num(zoom * 100.0)); - } + view_setting(x, *view, *zoom); + bounds = *b; } DestinationKind::Url { url } => { x.start("HyperlinkURLDestination") @@ -570,15 +587,30 @@ impl Writer<'_> { .attr("Name", &d.name) .attr("DestinationURL", url); } + DestinationKind::ExternalPage { + page_index, + zoom, + view, + bounds: b, + file_name, + .. + } => { + x.start("HyperlinkExternalPageDestination") + .attr("Self", d.reference()); + // The name is not stored; IDML makes it from the file + // name and the page (shown for the Fixed view only). + if let (0, Some(f)) = (view, file_name) { + x.attr("Name", format!("{f} - Page {} [Fixed]", page_index + 1)); + } + x.attr("DestinationPageIndex", (page_index + 1).to_string()); + view_setting(x, *view, *zoom); + bounds = *b; + } DestinationKind::Text | DestinationKind::Paragraph => continue, } x.attr("Hidden", d.hidden.to_string()) .attr("DestinationUniqueKey", d.key.to_string()); - if let DestinationKind::Page { - bounds: Some([left, top, right, bottom]), - .. - } = d.kind - { + 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) }; x.start("Properties") diff --git a/src/model/document.rs b/src/model/document.rs index e883bb8..c690f25 100644 --- a/src/model/document.rs +++ b/src/model/document.rs @@ -412,6 +412,7 @@ impl<'a> Reader<'a> { } hyperlink::class::PAGE_DESTINATION | hyperlink::class::URL_DESTINATION + | hyperlink::class::EXTERNAL_PAGE_DESTINATION | hyperlink::class::TEXT_DESTINATION => { if let Some(d) = self.destination(uid, cls)? { out.destinations.push(d); @@ -533,9 +534,25 @@ impl<'a> Reader<'a> { } } - /// A page or URL destination. + /// A page, URL, external page or text destination. fn destination(&self, uid: u32, cls: u32) -> Result, Error> { - let d = Destination::read(uid, cls, &*self.object(uid)?)?; + let mut d = Destination::read(uid, cls, &*self.object(uid)?)?; + // The file name of an external page destination is the last part + // of its link's URI (hyperlinks.md). + if let Some(hyperlink::DestinationKind::ExternalPage { + link: Some(link), + file_name, + .. + }) = d.as_mut().map(|d| &mut d.kind) + && let Some((l, _)) = self.link(*link)? + { + *file_name = l + .uri + .rsplit('/') + .next() + .filter(|n| !n.is_empty()) + .map(percent_decode); + } if let Some(hyperlink::DestinationKind::Page { zoom: None, .. }) = d.as_ref().map(|d| &d.kind) { @@ -608,3 +625,35 @@ impl<'a> Reader<'a> { Ok(Some((name, color))) } } + +/// `s` with `%xx` escapes replaced by their bytes, read as UTF-8. +fn percent_decode(s: &str) -> String { + let b = s.as_bytes(); + let mut out = Vec::with_capacity(b.len()); + let mut i = 0; + while i < b.len() { + if b[i] == b'%' + && let Some(v) = s + .get(i + 1..i + 3) + .and_then(|h| u8::from_str_radix(h, 16).ok()) + { + out.push(v); + i += 3; + } else { + out.push(b[i]); + i += 1; + } + } + String::from_utf8_lossy(&out).into_owned() +} + +#[cfg(test)] +mod percent_tests { + use super::percent_decode; + + #[test] + fn decodes_percent_escapes() { + assert_eq!(percent_decode("Ch%201%20%C3%A9.indd"), "Ch 1 é.indd"); + assert_eq!(percent_decode("100%"), "100%"); + } +} diff --git a/src/model/hyperlink.rs b/src/model/hyperlink.rs index 8445813..4d0df42 100644 --- a/src/model/hyperlink.rs +++ b/src/model/hyperlink.rs @@ -14,6 +14,8 @@ pub mod class { pub const TEXT_DESTINATION: u32 = 0x13504; pub const PAGE_DESTINATION: u32 = 0x13505; pub const URL_DESTINATION: u32 = 0x13506; + /// A page of another document. + pub const EXTERNAL_PAGE_DESTINATION: u32 = 0x13552; pub const BOOKMARK: u32 = 0x1354C; /// An item owned by a story at the position of a text destination. pub const DESTINATION_OWNER: u32 = 0x1353C; @@ -45,6 +47,10 @@ pub mod chunk { pub const PAGE_DESTINATION: u32 = 0x13507; pub const PAGE_DESTINATION_VIEW: u32 = 0x13527; pub const URL_DESTINATION: u32 = 0x13509; + /// External page destination: hidden, name and key. + pub const EXTERNAL_PAGE_DESTINATION: u32 = 0x13580; + /// External page destination: page index from 0. + pub const EXTERNAL_PAGE_INDEX: u32 = 0x1B8; /// Text destination: hidden, name and key. pub const TEXT_DESTINATION: u32 = 0x13508; /// Text destination: owner UID and kind. @@ -186,6 +192,18 @@ pub enum DestinationKind { Url { url: String, }, + /// A page of another document (`HyperlinkExternalPageDestination`). + ExternalPage { + /// Page index from 0. + page_index: u32, + zoom: Option, + view: u32, + bounds: Option<[f64; 4]>, + /// The link to the other document (chunk 0x1359F). + link: Option, + /// The last part of the linked document's path, decoded. + file_name: Option, + }, /// A position in story text (`HyperlinkTextDestination`). Text, /// A paragraph (`ParagraphDestination`), the target of @@ -213,6 +231,7 @@ impl Destination { match self.kind { DestinationKind::Page { .. } => format!("HyperlinkPageDestination/{escaped}"), DestinationKind::Url { .. } => format!("HyperlinkURLDestination/{escaped}"), + DestinationKind::ExternalPage { .. } => format!("u{:x}", self.uid), DestinationKind::Text => format!("HyperlinkTextDestination/{escaped}"), DestinationKind::Paragraph => format!("ParagraphDestination/{escaped}"), } @@ -330,6 +349,7 @@ impl Destination { let enc = obj.encoding; let id = match class { class::PAGE_DESTINATION => chunk::PAGE_DESTINATION, + class::EXTERNAL_PAGE_DESTINATION => chunk::EXTERNAL_PAGE_DESTINATION, class::TEXT_DESTINATION => chunk::TEXT_DESTINATION, _ => chunk::URL_DESTINATION, }; @@ -350,7 +370,8 @@ impl Destination { Some(Some(1)) => DestinationKind::Paragraph, _ => return Ok(None), } - } else if class == class::PAGE_DESTINATION { + } else if class == class::PAGE_DESTINATION || class == class::EXTERNAL_PAGE_DESTINATION { + // Chunk 0x13527: page UID, zoom, view setting, bounds. let Some(v) = obj.chunk(chunk::PAGE_DESTINATION_VIEW) else { return Ok(None); }; @@ -363,11 +384,31 @@ impl Destination { } else { None }; - DestinationKind::Page { - page, - zoom, - view, - bounds, + if class == class::PAGE_DESTINATION { + DestinationKind::Page { + page, + zoom, + view, + bounds, + } + } else { + let Some(page_index) = obj + .chunk(chunk::EXTERNAL_PAGE_INDEX) + .and_then(|d| enc.u32_at(d, 0)) + else { + return Ok(None); + }; + DestinationKind::ExternalPage { + page_index, + zoom, + view, + bounds, + link: obj + .chunk(chunk::OTHER_DOCUMENT) + .and_then(|d| enc.u32_at(d, 8)) + .filter(|&u| u != 0), + file_name: None, + } } } else { let url = match obj.chunk(chunk::URL) {