Register native PDF navigation and raw objects in write order
This commit is contained in:
1 parent
70ebacb056
commit
e95d1264da
9 files changed
+350
-48
No files matched your search
@@ -1867,12 +1867,23 @@ fn pdf_file_specification(out: &mut Vec<u8>, file: &[u8]) {
|
||||
#[derive(Default)]
|
||||
struct PdfNavigation {
|
||||
pages: Vec<usize>,
|
||||
named: BTreeMap<String, String>,
|
||||
named: BTreeMap<String, usize>,
|
||||
numbered: BTreeMap<i32, usize>,
|
||||
structures: BTreeMap<DestinationId<String>, usize>,
|
||||
pending_destinations: BTreeMap<(DestinationId<String>, bool), usize>,
|
||||
threads: BTreeMap<DestinationId<String>, usize>,
|
||||
}
|
||||
|
||||
fn write_destination(objects: &mut Vec<Vec<u8>>, slot: Option<usize>, body: String) -> usize {
|
||||
if let Some(id) = slot {
|
||||
objects[id - 1] = body.into_bytes();
|
||||
id
|
||||
} else {
|
||||
objects.push(body.into_bytes());
|
||||
objects.len()
|
||||
}
|
||||
}
|
||||
|
||||
fn destination_identifier(id: &DestinationId<String>) -> String {
|
||||
match id {
|
||||
DestinationId::Name(name) => format!("name{{{name}}}"),
|
||||
@@ -1886,7 +1897,7 @@ fn catalog_action_bytes(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
diagnostics: &mut Vec<String>,
|
||||
) -> Vec<u8> {
|
||||
let PdfNavigation { pages, named, numbered, structures, threads } = destinations;
|
||||
let PdfNavigation { pages, named, numbered, structures, pending_destinations, threads } = destinations;
|
||||
let mut output = b"<< ".to_vec();
|
||||
match action {
|
||||
CatalogAction::User(object) => output.clone_from(object),
|
||||
@@ -1911,7 +1922,10 @@ fn catalog_action_bytes(
|
||||
diagnostics.push(format!(
|
||||
"destination name{{{name}}} has been referenced but does not exist, replaced by a fixed one"
|
||||
));
|
||||
named.insert(name.clone(), format!("[{} 0 R /Fit]", pages[0]));
|
||||
let slot = pending_destinations.get(&(DestinationId::Name(name.clone()), false)).copied();
|
||||
let view = format!("[{} 0 R /Fit]", pages[0]);
|
||||
let body = if slot.is_some() { format!("<< /D {view} >>") } else { view };
|
||||
named.insert(name.clone(), write_destination(objects, slot, body));
|
||||
}
|
||||
output.extend_from_slice(pdf_text(name).as_bytes());
|
||||
}
|
||||
@@ -1920,8 +1934,8 @@ fn catalog_action_bytes(
|
||||
diagnostics.push(format!(
|
||||
"destination num{number} has been referenced but does not exist, replaced by a fixed one"
|
||||
));
|
||||
objects.push(format!("[{} 0 R /Fit]", pages[0]).into_bytes());
|
||||
objects.len()
|
||||
let slot = pending_destinations.get(&(DestinationId::Number(*number), false)).copied();
|
||||
write_destination(objects, slot, format!("[{} 0 R /Fit]", pages[0]))
|
||||
});
|
||||
write!(&mut output, "{id} 0 R").unwrap();
|
||||
}
|
||||
@@ -1951,8 +1965,11 @@ fn catalog_action_bytes(
|
||||
destination_identifier(destination)
|
||||
));
|
||||
// pdfTeX leaves the missing destination in the free list.
|
||||
objects.push(Vec::new());
|
||||
objects.len()
|
||||
pending_destinations.get(&(destination.clone(), true)).copied()
|
||||
.unwrap_or_else(|| {
|
||||
objects.push(Vec::new());
|
||||
objects.len()
|
||||
})
|
||||
});
|
||||
write!(&mut output, " /SD {id} 0 R").unwrap();
|
||||
}
|
||||
@@ -2563,6 +2580,17 @@ fn write_pdf(
|
||||
device_objects.insert(stream.number, objects.len() as u32);
|
||||
}
|
||||
}
|
||||
let mut navigation = PdfNavigation::default();
|
||||
if let Some(registry) = registry {
|
||||
for (key, number, defined) in registry.destinations() {
|
||||
objects.push(Vec::new());
|
||||
let id = objects.len();
|
||||
device_objects.insert(number, id as u32);
|
||||
if !defined {
|
||||
navigation.pending_destinations.insert(key.clone(), id);
|
||||
}
|
||||
}
|
||||
}
|
||||
let parents: BTreeSet<_> = pages.iter().filter_map(|page| {
|
||||
page.pdf_resources.objects.map(|allocation| allocation.parent)
|
||||
}).collect();
|
||||
@@ -2598,7 +2626,6 @@ fn write_pdf(
|
||||
}
|
||||
let mut kids = Vec::new();
|
||||
let mut page_groups = BTreeMap::<usize, Vec<usize>>::new();
|
||||
let mut navigation = PdfNavigation::default();
|
||||
let mut annotation_actions = Vec::new();
|
||||
let mut xdv_annotations = Vec::new();
|
||||
let pdf_version = pages
|
||||
@@ -3014,14 +3041,14 @@ fn write_pdf(
|
||||
format!("/FitR {}", coordinates.join(" "))
|
||||
}
|
||||
};
|
||||
let slot = destination.object_number.map(|number| device_objects[&number] as usize);
|
||||
if let Some(number) = destination.structure {
|
||||
use std::collections::btree_map::Entry;
|
||||
match navigation.structures.entry(destination.destination.clone()) {
|
||||
Entry::Vacant(entry) => {
|
||||
let structure = remapped.as_ref().expect("structure objects were remapped")
|
||||
.structure_objects[&number];
|
||||
objects.push(format!("[{structure} 0 R {view}]").into_bytes());
|
||||
entry.insert(objects.len());
|
||||
entry.insert(write_destination(&mut objects, slot, format!("[{structure} 0 R {view}]")));
|
||||
}
|
||||
Entry::Occupied(entry) => diagnostics.push(format!(
|
||||
"structure destination with the same identifier ({}) has been already used, duplicate ignored",
|
||||
@@ -3032,12 +3059,14 @@ fn write_pdf(
|
||||
let object = || format!("[{page_id} 0 R {view}]");
|
||||
match &destination.destination {
|
||||
DestinationId::Name(name) => {
|
||||
navigation.named.entry(name.clone()).or_insert_with(object);
|
||||
navigation.named.entry(name.clone()).or_insert_with(|| {
|
||||
let body = if slot.is_some() { format!("<< /D {} >>", object()) } else { object() };
|
||||
write_destination(&mut objects, slot, body)
|
||||
});
|
||||
}
|
||||
DestinationId::Number(number) => {
|
||||
navigation.numbered.entry(*number).or_insert_with(|| {
|
||||
objects.push(object().into_bytes());
|
||||
objects.len()
|
||||
write_destination(&mut objects, slot, object())
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3056,6 +3085,7 @@ fn write_pdf(
|
||||
}
|
||||
objects[1].extend_from_slice(b" >>");
|
||||
|
||||
let after_pages_tree = objects.len() + 1;
|
||||
for (id, mut body, action) in annotation_actions {
|
||||
body.extend(catalog_action_bytes(action, &mut navigation, &mut objects, diagnostics));
|
||||
body.extend_from_slice(b" >>");
|
||||
@@ -3236,12 +3266,17 @@ fn write_pdf(
|
||||
let first = pdf_text(destinations.first_key_value().unwrap().0);
|
||||
let last = pdf_text(destinations.last_key_value().unwrap().0);
|
||||
for (name, destination) in destinations {
|
||||
objects.push(destination.into_bytes());
|
||||
entries.push(format!("{} {} 0 R", pdf_text(&name), objects.len()));
|
||||
entries.push(format!("{} {destination} 0 R", pdf_text(&name)));
|
||||
}
|
||||
objects.push(format!("<< /Names [{}] /Limits [{first} {last}] >>",
|
||||
entries.join(" ")).into_bytes());
|
||||
catalog.push_str(&format!(" /Names << /Dests {} 0 R >>", objects.len()));
|
||||
let tree = objects.len();
|
||||
if pages[0].pdf.as_ref().is_some_and(|pdf| pdf.driver == crate::pdf_device::PdfDriver::PdfTeX) {
|
||||
objects.push(format!("<< /Dests {tree} 0 R >>").into_bytes());
|
||||
catalog.push_str(&format!(" /Names {} 0 R", objects.len()));
|
||||
} else {
|
||||
catalog.push_str(&format!(" /Names << /Dests {tree} 0 R >>"));
|
||||
}
|
||||
}
|
||||
objects[0] = if let Some(xdv) = xdv {
|
||||
xdv.finish_catalog(&catalog, &navigation.pages)?
|
||||
@@ -3305,7 +3340,7 @@ fn write_pdf(
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Some(objects::ObjectPacking { streams, uncompressed })
|
||||
Some(objects::ObjectPacking { streams, uncompressed, after_pages_tree })
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user