Integrate native repeat boxes and PDF bookmarks
This commit is contained in:
1 parent
5163fc9854
commit
38e4822370
33 files changed
+1208
-143
No files matched your search
@@ -14,7 +14,7 @@ use rusttype::Font;
|
||||
|
||||
use crate::cmr10::Cmr10Metrics;
|
||||
use crate::pdf_device::{
|
||||
catalog::{CatalogAction, CatalogTarget},
|
||||
catalog::{CatalogAction, CatalogTarget, PdfOutline},
|
||||
links::DestinationId,
|
||||
};
|
||||
use crate::render::{DisplayItem, DisplayList, FontFace};
|
||||
@@ -1520,12 +1520,12 @@ fn write_catalog_action(
|
||||
action: &CatalogAction,
|
||||
page_ids: &[usize],
|
||||
destinations: &mut BTreeMap<String, String>,
|
||||
numbers: &BTreeMap<i32, usize>,
|
||||
numbers: &mut BTreeMap<i32, usize>,
|
||||
threads: &mut BTreeMap<DestinationId<String>, usize>,
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
diagnostics: &mut Vec<String>,
|
||||
) -> (usize, Option<usize>) {
|
||||
) -> usize {
|
||||
let mut output = b"<< ".to_vec();
|
||||
let mut threads = None;
|
||||
match action {
|
||||
CatalogAction::User(object) => output.clone_from(object),
|
||||
CatalogAction::GoTo {
|
||||
@@ -1554,7 +1554,7 @@ fn write_catalog_action(
|
||||
output.extend_from_slice(pdf_text(name).as_bytes());
|
||||
}
|
||||
CatalogTarget::Destination(DestinationId::Number(number)) => {
|
||||
let id = numbers.get(number).copied().unwrap_or_else(|| {
|
||||
let id = numbers.entry(*number).or_insert_with(|| {
|
||||
diagnostics.push(format!(
|
||||
"destination num{number} has been referenced but does not exist, replaced by a fixed one"
|
||||
));
|
||||
@@ -1596,21 +1596,20 @@ fn write_catalog_action(
|
||||
DestinationId::Name(name) => (name.clone(), format!("name{{{name}}}")),
|
||||
DestinationId::Number(number) => (number.to_string(), format!("num{number}")),
|
||||
};
|
||||
diagnostics.push(format!(
|
||||
"thread destination {identifier} has been referenced but does not exist, replaced by a fixed one"
|
||||
));
|
||||
// There are no thread definitions yet. As pdfTeX does for an
|
||||
// undefined thread, link its 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());
|
||||
objects.push(format!(
|
||||
"<< /T {thread} 0 R /V {bead} 0 R /N {bead} 0 R /P {} 0 R /R [0 0 0 0] >>",
|
||||
page_ids[0]
|
||||
).into_bytes());
|
||||
objects.push(format!("[{thread} 0 R]").into_bytes());
|
||||
local_id = Some(thread);
|
||||
threads = Some(objects.len());
|
||||
local_id = Some(*threads.entry(destination.clone()).or_insert_with(|| {
|
||||
diagnostics.push(format!(
|
||||
"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());
|
||||
objects.push(format!(
|
||||
"<< /T {thread} 0 R /V {bead} 0 R /N {bead} 0 R /P {} 0 R /R [0 0 0 0] >>",
|
||||
page_ids[0]
|
||||
).into_bytes());
|
||||
thread
|
||||
}));
|
||||
}
|
||||
output.extend_from_slice(b"/S /Thread /D ");
|
||||
match destination {
|
||||
@@ -1627,7 +1626,101 @@ fn write_catalog_action(
|
||||
}
|
||||
}
|
||||
objects.push(output);
|
||||
(objects.len(), threads)
|
||||
objects.len()
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct OutlinePosition {
|
||||
parent: usize,
|
||||
first: Option<usize>,
|
||||
last: Option<usize>,
|
||||
previous: Option<usize>,
|
||||
next: Option<usize>,
|
||||
visible_children: usize,
|
||||
}
|
||||
|
||||
fn write_pdf_outlines(
|
||||
outlines: &[PdfOutline<Vec<u8>>],
|
||||
action_ids: &[usize],
|
||||
objects: &mut Vec<Vec<u8>>,
|
||||
) -> usize {
|
||||
let mut positions = vec![OutlinePosition::default(); outlines.len() + 1];
|
||||
let mut parents = vec![(0, usize::MAX)];
|
||||
for (offset, outline) in outlines.iter().enumerate() {
|
||||
let index = offset + 1;
|
||||
let (parent, remaining) = parents.last_mut().unwrap();
|
||||
let parent = *parent;
|
||||
*remaining -= 1;
|
||||
positions[parent].first.get_or_insert(index);
|
||||
if let Some(previous) = positions[parent].last.replace(index) {
|
||||
positions[previous].next = Some(index);
|
||||
positions[index].previous = Some(previous);
|
||||
}
|
||||
positions[index].parent = parent;
|
||||
if outline.count != 0 {
|
||||
parents.push((index, outline.count.unsigned_abs() as usize));
|
||||
} else {
|
||||
while parents.last().is_some_and(|(_, remaining)| *remaining == 0) {
|
||||
parents.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (offset, outline) in outlines.iter().enumerate().rev() {
|
||||
let position = &positions[offset + 1];
|
||||
let visible = if outline.count > 0 { position.visible_children } else { 0 };
|
||||
let parent = position.parent;
|
||||
positions[parent].visible_children += visible + 1;
|
||||
}
|
||||
|
||||
let root_id = objects.len() + 1;
|
||||
objects.resize_with(root_id + outlines.len(), Vec::new);
|
||||
let root = &positions[0];
|
||||
// pdfTeX uses the current sibling at the end of input as the root's Last.
|
||||
let last = positions[parents.last().unwrap().0].last.map_or(0, |index| root_id + index);
|
||||
objects[root_id - 1] = format!(
|
||||
"<< /Type /Outlines /First {} 0 R /Last {last} 0 R /Count {} >>",
|
||||
root_id + root.first.unwrap(),
|
||||
root.visible_children,
|
||||
)
|
||||
.into_bytes();
|
||||
|
||||
for (offset, (outline, action_id)) in outlines.iter().zip(action_ids).enumerate() {
|
||||
let position = &positions[offset + 1];
|
||||
let mut title = Vec::with_capacity(outline.title.len() + 2);
|
||||
title.push(b'(');
|
||||
title.extend_from_slice(&outline.title);
|
||||
title.push(b')');
|
||||
objects.push(title);
|
||||
let mut entry = format!(
|
||||
"<< /Title {} 0 R /A {action_id} 0 R /Parent {} 0 R",
|
||||
objects.len(),
|
||||
root_id + position.parent,
|
||||
)
|
||||
.into_bytes();
|
||||
for (key, index) in [
|
||||
("Prev", position.previous),
|
||||
("Next", position.next),
|
||||
("First", position.first),
|
||||
("Last", position.last),
|
||||
] {
|
||||
if let Some(index) = index {
|
||||
write!(&mut entry, " /{key} {} 0 R", root_id + index).unwrap();
|
||||
}
|
||||
}
|
||||
if position.visible_children != 0 {
|
||||
let sign = if outline.count < 0 { "-" } else { "" };
|
||||
write!(&mut entry, " /Count {sign}{}", position.visible_children).unwrap();
|
||||
}
|
||||
if !outline.attributes.is_empty() {
|
||||
entry.push(b'\n');
|
||||
entry.extend_from_slice(&outline.attributes);
|
||||
entry.push(b'\n');
|
||||
}
|
||||
entry.extend_from_slice(b" >>");
|
||||
objects[root_id + offset] = entry;
|
||||
}
|
||||
root_id
|
||||
}
|
||||
|
||||
fn write_pdf(
|
||||
@@ -2209,13 +2302,29 @@ fn write_pdf(
|
||||
catalog.push_str(&format!(" /Outlines {root_id} 0 R"));
|
||||
}
|
||||
let extra = unicode.and_then(|options| options.catalog);
|
||||
let mut threads = BTreeMap::new();
|
||||
let action = extra.and_then(|extra| extra.open_action.as_ref()).map(|action| {
|
||||
write_catalog_action(
|
||||
action, &page_ids, &mut destinations, &numbers, &mut objects, diagnostics,
|
||||
action, &page_ids, &mut destinations, &mut numbers, &mut threads, &mut objects,
|
||||
diagnostics,
|
||||
)
|
||||
});
|
||||
if let Some((_, Some(threads))) = action {
|
||||
catalog.push_str(&format!(" /Threads {threads} 0 R"));
|
||||
if let Some(extra) = extra.filter(|extra| !extra.outlines.is_empty()) {
|
||||
let action_ids: Vec<_> = extra.outlines.iter().map(|outline| {
|
||||
write_catalog_action(
|
||||
&outline.action, &page_ids, &mut destinations, &mut numbers, &mut threads,
|
||||
&mut objects, diagnostics,
|
||||
)
|
||||
}).collect();
|
||||
let outlines = write_pdf_outlines(&extra.outlines, &action_ids, &mut objects);
|
||||
catalog.push_str(&format!(" /Outlines {outlines} 0 R"));
|
||||
}
|
||||
if !threads.is_empty() {
|
||||
let mut ids: Vec<_> = 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()));
|
||||
}
|
||||
if !destinations.is_empty() {
|
||||
let mut entries = Vec::with_capacity(destinations.len());
|
||||
@@ -2236,7 +2345,7 @@ fn write_pdf(
|
||||
catalog.extend_from_slice(&extra.entries);
|
||||
catalog.push(b'\n');
|
||||
}
|
||||
if let Some((id, _)) = action {
|
||||
if let Some(id) = action {
|
||||
write!(&mut catalog, " /OpenAction {id} 0 R").unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user