Ship XDV document views and PDF outline navigation

This commit is contained in:
gloamxun committed 2026-11-25 20:32:05 +08:00
1 parent 8be7867b37
commit 515dbe6181
13 files changed
+743 -111

No files matched your search

+54 -21
View File
@@ -2752,6 +2752,19 @@ fn write_pdf(
let outlines = write_pdf_outlines(&extra.outlines, &action_ids, &mut objects);
catalog.push_str(&format!(" /Outlines {outlines} 0 R"));
}
let xdv = unicode.and_then(|options| options.xdv_navigation);
let keep_xdv_destinations = unicode.is_some_and(|options| options.xdv_keep_destinations);
let mut xdv_names = BTreeMap::new();
if let Some(xdv) = xdv {
let open_depth = unicode.unwrap().xdv_outline_open_depth;
let (outlines, names) = xdv.write_outlines(
&navigation.pages, &mut objects, keep_xdv_destinations, open_depth,
)?;
if let Some(outlines) = outlines {
catalog.push_str(&format!(" /Outlines {outlines} 0 R"));
}
xdv_names = names;
}
if !navigation.threads.is_empty() {
let mut ids: Vec<_> = navigation.threads.into_values().collect();
ids.sort_unstable_by(|left, right| right.cmp(left));
@@ -2775,14 +2788,30 @@ fn write_pdf(
}
}
}
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)));
for name in xdv_names.keys() {
if !xdv_destinations.contains_key(name.as_slice()) {
diagnostics.push(format!("xdvipdfmx: warning: PDF destination {:?} not defined.",
String::from_utf8_lossy(name)));
}
}
let mut retained = BTreeMap::new();
for (name, (destination, height, magnification)) in xdv_destinations {
let name = if keep_xdv_destinations {
name
} else if let Some(reference) = xdv_names.get(name) {
reference
} else {
continue;
};
let page_object = navigation.pages.get(destination.page - 1)
.ok_or_else(|| format!("XDV destination refers to nonexistent page {}", destination.page))?;
retained.insert(name, destination.array(*page_object, height, magnification));
}
if !retained.is_empty() {
let entries: Vec<_> = retained.into_iter().map(|(name, value)| {
let name = format!("<{}>", name.iter().map(|byte| format!("{byte:02X}")).collect::<String>());
(name, value)
}).collect();
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(" ");
@@ -2800,21 +2829,25 @@ fn write_pdf(
entries.join(" ")).into_bytes());
catalog.push_str(&format!(" /Names << /Dests {} 0 R >>", objects.len()));
}
let mut catalog = catalog.into_bytes();
if let Some(extra) = extra {
let entries = remapped.as_ref().and_then(|resources| resources.catalog.as_deref())
.unwrap_or(&extra.entries);
if !entries.is_empty() {
catalog.push(b'\n');
catalog.extend_from_slice(entries);
catalog.push(b'\n');
objects[0] = if let Some(xdv) = xdv {
xdv.finish_catalog(&catalog, &navigation.pages)?
} else {
let mut catalog = catalog.into_bytes();
if let Some(extra) = extra {
let entries = remapped.as_ref().and_then(|resources| resources.catalog.as_deref())
.unwrap_or(&extra.entries);
if !entries.is_empty() {
catalog.push(b'\n');
catalog.extend_from_slice(entries);
catalog.push(b'\n');
}
if let Some(id) = action {
write!(&mut catalog, " /OpenAction {id} 0 R").unwrap();
}
}
if let Some(id) = action {
write!(&mut catalog, " /OpenAction {id} 0 R").unwrap();
}
}
catalog.extend_from_slice(b" >>");
objects[0] = catalog;
catalog.extend_from_slice(b" >>");
catalog
};
if let Some(level) = final_compression {
let device = pages[0].pdf.as_ref().expect("validated native PDF options");