Build native PDF article threads and bead rings
This commit is contained in:
1 parent
de4bd1fa76
commit
4fb0f2c89b
15 files changed
+528
-28
No files matched your search
@@ -2042,6 +2042,7 @@ struct PdfNavigation {
|
||||
structures: BTreeMap<DestinationId<String>, usize>,
|
||||
pending_destinations: BTreeMap<(DestinationId<String>, bool), usize>,
|
||||
threads: BTreeMap<DestinationId<String>, usize>,
|
||||
pending_threads: BTreeMap<DestinationId<String>, usize>,
|
||||
}
|
||||
|
||||
fn write_destination(objects: &mut Vec<Vec<u8>>, slot: Option<usize>, body: String) -> usize {
|
||||
@@ -2067,7 +2068,7 @@ fn catalog_action_bytes(
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
diagnostics: &mut Vec<String>,
|
||||
) -> Vec<u8> {
|
||||
let PdfNavigation { pages, named, numbered, structures, pending_destinations, threads } = destinations;
|
||||
let PdfNavigation { pages, named, numbered, structures, pending_destinations, threads, pending_threads } = destinations;
|
||||
let mut output = b"<< ".to_vec();
|
||||
match action {
|
||||
CatalogAction::User(object) => output.clone_from(object),
|
||||
@@ -2165,9 +2166,12 @@ fn catalog_action_bytes(
|
||||
"thread destination {identifier} has been referenced but does not exist, replaced by a fixed one"
|
||||
));
|
||||
// Link the missing thread's single empty bead to the first page.
|
||||
let thread = objects.len() + 1;
|
||||
let bead = thread + 1;
|
||||
objects.push(format!("<< /I << /Title {} >> /F {bead} 0 R >>", pdf_text(&title)).into_bytes());
|
||||
let thread = pending_threads.get(destination).copied().unwrap_or_else(|| {
|
||||
objects.push(Vec::new());
|
||||
objects.len()
|
||||
});
|
||||
let bead = objects.len() + 1;
|
||||
objects[thread - 1] = format!("<< /I << /Title {} >> /F {bead} 0 R >>", pdf_text(&title)).into_bytes();
|
||||
objects.push(format!(
|
||||
"<< /T {thread} 0 R /V {bead} 0 R /N {bead} 0 R /P {} 0 R /R [0 0 0 0] >>",
|
||||
pages[0]
|
||||
@@ -2815,6 +2819,31 @@ fn write_pdf(
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut bead_slots = BTreeMap::new();
|
||||
if let Some(registry) = registry {
|
||||
let defined: BTreeSet<_> = pages.iter().flat_map(|page| &page.pdf_links.thread_beads)
|
||||
.map(|bead| bead.thread).collect();
|
||||
let mut threads: Vec<_> = registry.threads().collect();
|
||||
threads.sort_unstable_by_key(|&(_, number)| number);
|
||||
for (id, number) in threads {
|
||||
objects.push(Vec::new());
|
||||
let slot = objects.len();
|
||||
device_objects.insert(number, slot as u32);
|
||||
let map = if defined.contains(&number) { &mut navigation.threads } else { &mut navigation.pending_threads };
|
||||
map.insert(id.clone(), slot);
|
||||
}
|
||||
for (bead, rectangle) in registry.bead_rectangles() {
|
||||
objects.extend([Vec::new(), Vec::new()]);
|
||||
let slots = (objects.len() - 1, objects.len());
|
||||
device_objects.insert(bead, slots.0 as u32);
|
||||
device_objects.insert(rectangle, slots.1 as u32);
|
||||
bead_slots.insert(bead, slots);
|
||||
}
|
||||
for number in registry.thread_open_action().into_iter().chain(registry.thread_array()) {
|
||||
objects.push(Vec::new());
|
||||
device_objects.insert(number, objects.len() as u32);
|
||||
}
|
||||
}
|
||||
let parents: BTreeSet<_> = pages.iter().filter_map(|page| {
|
||||
page.pdf_resources.objects.map(|allocation| allocation.parent)
|
||||
}).collect();
|
||||
@@ -2852,6 +2881,8 @@ fn write_pdf(
|
||||
let mut page_groups = BTreeMap::<usize, Vec<usize>>::new();
|
||||
let mut annotation_actions = Vec::new();
|
||||
let mut xdv_annotations = Vec::new();
|
||||
// Each triple records the bead, its page, and its rectangle object.
|
||||
let mut thread_rings: BTreeMap<u32, Vec<(usize, usize, usize)>> = BTreeMap::new();
|
||||
let pdf_version = pages
|
||||
.first()
|
||||
.and_then(|page| page.pdf.as_ref())
|
||||
@@ -3099,6 +3130,16 @@ fn write_pdf(
|
||||
} else {
|
||||
format!(" /Annots [{annotation_refs}]")
|
||||
};
|
||||
let mut bead_references = String::new();
|
||||
for bead in &list.pdf_links.thread_beads {
|
||||
let &(bead_id, rectangle_id) = bead_slots.get(&bead.number).ok_or("missing PDF bead allocation")?;
|
||||
bead_references.push_str(&format!(" {bead_id} 0 R"));
|
||||
let [left, top, right, bottom] = bead.rect;
|
||||
let rect = [left, height - bottom, right, height - top].map(physical).join(" ");
|
||||
objects[rectangle_id - 1] = format!("[{rect}]").into_bytes();
|
||||
thread_rings.entry(bead.thread).or_default().push((bead_id, page_id, rectangle_id));
|
||||
}
|
||||
let beads = if bead_references.is_empty() { String::new() } else { format!(" /B [{bead_references} ]") };
|
||||
navigation.pages.push(page_id);
|
||||
kids.push(page_id);
|
||||
if parent != 2 {
|
||||
@@ -3135,9 +3176,9 @@ fn write_pdf(
|
||||
if decorated {
|
||||
page.extend_from_slice(format!(" /Contents {content_id} 0 R ").as_bytes());
|
||||
page.extend_from_slice(&remapped.as_ref().expect("validated page dictionary").page_attributes[page_index]);
|
||||
page.extend_from_slice(format!(" /Parent {parent} 0 R{annots}{png_group_ref} >>").as_bytes());
|
||||
page.extend_from_slice(format!(" /Parent {parent} 0 R{annots}{beads}{png_group_ref} >>").as_bytes());
|
||||
} else {
|
||||
page.extend_from_slice(format!("{annots} /Contents {content_id} 0 R{png_group_ref} >>").as_bytes());
|
||||
page.extend_from_slice(format!("{annots}{beads} /Contents {content_id} 0 R{png_group_ref} >>").as_bytes());
|
||||
}
|
||||
if let Some(id) = reserved_page {
|
||||
objects[id - 1] = page;
|
||||
@@ -3321,6 +3362,33 @@ fn write_pdf(
|
||||
}
|
||||
objects[1].extend_from_slice(b" >>");
|
||||
|
||||
if let Some(registry) = registry {
|
||||
for (destination, number) in registry.threads() {
|
||||
let Some(ring) = thread_rings.remove(&number) else { continue };
|
||||
let thread = device_objects[&number] as usize;
|
||||
let first = ring[0].0;
|
||||
let mut body = b"<< ".to_vec();
|
||||
if let Some(entries) = remapped.as_ref().and_then(|resources| resources.thread_attributes.get(&number)) {
|
||||
body.extend_from_slice(entries);
|
||||
} else {
|
||||
let title = match destination {
|
||||
DestinationId::Name(name) => name.clone(),
|
||||
DestinationId::Number(number) => number.to_string(),
|
||||
};
|
||||
write!(&mut body, "/I << /Title {} >>", pdf_text(&title)).unwrap();
|
||||
}
|
||||
write!(&mut body, " /F {first} 0 R >>").unwrap();
|
||||
objects[thread - 1] = body;
|
||||
for (index, &(bead, page, rectangle)) in ring.iter().enumerate() {
|
||||
let previous = ring[(index + ring.len() - 1) % ring.len()].0;
|
||||
let next = ring[(index + 1) % ring.len()].0;
|
||||
let owner = if index == 0 { format!("/T {thread} 0 R ") } else { String::new() };
|
||||
objects[bead - 1] = format!(
|
||||
"<< {owner}/V {previous} 0 R /N {next} 0 R /P {page} 0 R /R {rectangle} 0 R >>"
|
||||
).into_bytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
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));
|
||||
@@ -3408,15 +3476,23 @@ fn write_pdf(
|
||||
catalog.push_str(&format!(" /Outlines {root_id} 0 R"));
|
||||
}
|
||||
let extra = unicode.and_then(|options| options.catalog);
|
||||
let mut action_object = |action| {
|
||||
let mut action_object = |action, slot: Option<usize>| {
|
||||
let body = catalog_action_bytes(action, &mut navigation, &mut objects, diagnostics);
|
||||
objects.push(body);
|
||||
objects.len()
|
||||
if let Some(slot) = slot {
|
||||
objects[slot - 1] = body;
|
||||
slot
|
||||
} else {
|
||||
objects.push(body);
|
||||
objects.len()
|
||||
}
|
||||
};
|
||||
let action = extra.and_then(|extra| extra.open_action.as_ref()).map(&mut action_object);
|
||||
let open_action_slot = registry.and_then(|registry| registry.thread_open_action())
|
||||
.map(|number| device_objects[&number] as usize);
|
||||
let action = extra.and_then(|extra| extra.open_action.as_ref())
|
||||
.map(|action| action_object(action, open_action_slot));
|
||||
if let Some(extra) = extra.filter(|extra| !extra.outlines.is_empty()) {
|
||||
let action_ids: Vec<_> = extra.outlines.iter()
|
||||
.map(|outline| action_object(&outline.action))
|
||||
.map(|outline| action_object(&outline.action, None))
|
||||
.collect();
|
||||
let outlines = write_pdf_outlines(&extra.outlines, &action_ids, &mut objects);
|
||||
catalog.push_str(&format!(" /Outlines {outlines} 0 R"));
|
||||
@@ -3449,8 +3525,10 @@ fn write_pdf(
|
||||
let mut ids: Vec<_> = navigation.threads.into_values().collect();
|
||||
ids.sort_unstable_by(|left, right| right.cmp(left));
|
||||
let references: Vec<_> = ids.iter().map(|id| format!("{id} 0 R")).collect();
|
||||
objects.push(format!("[{}]", references.join(" ")).into_bytes());
|
||||
catalog.push_str(&format!(" /Threads {} 0 R", objects.len()));
|
||||
let slot = registry.and_then(|registry| registry.thread_array())
|
||||
.map(|number| device_objects[&number] as usize);
|
||||
let id = write_destination(&mut objects, slot, format!("[{}]", references.join(" ")));
|
||||
catalog.push_str(&format!(" /Threads {id} 0 R"));
|
||||
}
|
||||
let destinations = navigation.named;
|
||||
let mut xdv_destinations = BTreeMap::new();
|
||||
|
||||
Reference in new issue
Block a user