Resolve XDV named destinations at PDF shipout

This commit is contained in:
gloamxun committed 2026-11-25 19:47:17 +08:00
1 parent dd695a8945
commit cc147e93d8
12 files changed
+456 -9

No files matched your search

+29 -1
View File
@@ -2755,7 +2755,35 @@ fn write_pdf(
catalog.push_str(&format!(" /Threads {} 0 R", objects.len()));
}
let destinations = navigation.named;
if !destinations.is_empty() {
let mut xdv_destinations = BTreeMap::new();
for list in pages {
for destination in &list.pdf_links.xdv_destinations {
use std::collections::btree_map::Entry;
match xdv_destinations.entry(destination.name.as_slice()) {
Entry::Vacant(entry) => {
entry.insert((destination, list.height, list.magnification));
}
Entry::Occupied(entry) => diagnostics.push(format!(
"xdvipdfmx: warning: Object @{} already defined.",
String::from_utf8_lossy(entry.key()),
)),
}
}
}
if unicode.is_some_and(|options| options.xdv_keep_destinations) && !xdv_destinations.is_empty() {
let mut entries = Vec::with_capacity(xdv_destinations.len());
for (name, (destination, height, magnification)) in xdv_destinations {
let page_object = navigation.pages.get(destination.page - 1)
.ok_or_else(|| format!("XDV destination refers to nonexistent page {}", destination.page))?;
let name = format!("<{}>", name.iter().map(|byte| format!("{byte:02X}")).collect::<String>());
entries.push((name, destination.array(*page_object, height, magnification)));
}
let first = &entries.first().unwrap().0;
let last = &entries.last().unwrap().0;
let names = entries.iter().map(|(name, value)| format!("{name} {value}")).collect::<Vec<_>>().join(" ");
objects.push(format!("<< /Names [{names}] /Limits [{first} {last}] >>").into_bytes());
catalog.push_str(&format!(" /Names << /Dests {} 0 R >>", objects.len()));
} else if !destinations.is_empty() {
let mut entries = Vec::with_capacity(destinations.len());
let first = pdf_text(destinations.first_key_value().unwrap().0);
let last = pdf_text(destinations.last_key_value().unwrap().0);