Implement native PDF links and vertical discard lists
This commit is contained in:
1 parent
b6567f67e2
commit
a53d9c9bf9
29 files changed
+980
-109
No files matched your search
@@ -1717,6 +1717,7 @@ fn write_pdf(
|
||||
|
||||
let mut kids = Vec::new();
|
||||
let mut page_ids: Vec<usize> = Vec::new();
|
||||
let mut destinations = BTreeMap::new();
|
||||
let mut form_ids = BTreeMap::new();
|
||||
for list in pages {
|
||||
for item in &list.items {
|
||||
@@ -1828,15 +1829,12 @@ fn write_pdf(
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let annotation_base = image_base + used.len();
|
||||
let annots = if links.is_empty() {
|
||||
let annotation_count = links.len() + list.pdf_links.annotations.len();
|
||||
let annots = if annotation_count == 0 {
|
||||
String::new()
|
||||
} else {
|
||||
let refs = (0..links.len())
|
||||
.map(|index| {
|
||||
format!("/Annots [{}]", (annotation_base + index))
|
||||
.replace("/Annots [", "")
|
||||
.replacen("]", "", 1)
|
||||
})
|
||||
let refs = (0..annotation_count)
|
||||
.map(|index| format!("{} 0 R", annotation_base + index))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
format!(" /Annots [{refs}]")
|
||||
@@ -1908,6 +1906,37 @@ fn write_pdf(
|
||||
.into_bytes(),
|
||||
);
|
||||
}
|
||||
for annotation in &list.pdf_links.annotations {
|
||||
use crate::pdf_device::links::AnnotationAction;
|
||||
let [left, top, right, bottom] = annotation.rect;
|
||||
let coordinates = [left, height - bottom, right, height - top].map(physical);
|
||||
let action = match &annotation.action {
|
||||
AnnotationAction::User(raw) => raw.clone(),
|
||||
AnnotationAction::GotoName(name) =>
|
||||
format!("/Subtype /Link /A << /S /GoTo /D {} >>", pdf_text(name)),
|
||||
};
|
||||
objects.push(format!("<< /Type /Annot {} /Rect [{}] {} >>",
|
||||
annotation.attributes, coordinates.join(" "), action).into_bytes());
|
||||
}
|
||||
for destination in &list.pdf_links.destinations {
|
||||
use crate::pdf_device::links::DestinationView;
|
||||
let x = physical(destination.point[0]);
|
||||
let y = physical(height - destination.point[1]);
|
||||
let view = match destination.view {
|
||||
DestinationView::Xyz(zoom) => {
|
||||
let zoom = zoom.filter(|n| *n > 0).map_or("null".to_owned(),
|
||||
|n| crate::pdf_device::print_decimal(i64::from(n), 3));
|
||||
format!("/XYZ {x} {y} {zoom}")
|
||||
}
|
||||
DestinationView::Fit => "/Fit".into(),
|
||||
DestinationView::FitB => "/FitB".into(),
|
||||
DestinationView::FitH => format!("/FitH {y}"),
|
||||
DestinationView::FitV => format!("/FitV {x}"),
|
||||
DestinationView::FitBH => format!("/FitBH {y}"),
|
||||
DestinationView::FitBV => format!("/FitBV {x}"),
|
||||
};
|
||||
destinations.entry(destination.name.clone()).or_insert_with(|| format!("[{page_id} 0 R {view}]"));
|
||||
}
|
||||
}
|
||||
objects[1] = format!(
|
||||
"<< /Type /Pages /Kids [{}] /Count {} >>",
|
||||
@@ -1916,6 +1945,7 @@ fn write_pdf(
|
||||
)
|
||||
.into_bytes();
|
||||
|
||||
let mut catalog = "<< /Type /Catalog /Pages 2 0 R".to_owned();
|
||||
if !metadata.outline.is_empty() {
|
||||
let root_id = objects.len() + 1;
|
||||
objects.push(Vec::new());
|
||||
@@ -1993,9 +2023,22 @@ fn write_pdf(
|
||||
metadata.outline.len()
|
||||
)
|
||||
.into_bytes();
|
||||
objects[0] =
|
||||
format!("<< /Type /Catalog /Pages 2 0 R /Outlines {root_id} 0 R >>").into_bytes();
|
||||
catalog.push_str(&format!(" /Outlines {root_id} 0 R"));
|
||||
}
|
||||
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);
|
||||
for (name, destination) in destinations {
|
||||
objects.push(destination.into_bytes());
|
||||
entries.push(format!("{} {} 0 R", pdf_text(&name), objects.len()));
|
||||
}
|
||||
objects.push(format!("<< /Names [{}] /Limits [{first} {last}] >>",
|
||||
entries.join(" ")).into_bytes());
|
||||
catalog.push_str(&format!(" /Names << /Dests {} 0 R >>", objects.len()));
|
||||
}
|
||||
catalog.push_str(" >>");
|
||||
objects[0] = catalog.into_bytes();
|
||||
|
||||
let info_ref = match metadata_object(metadata) {
|
||||
Some(object) => {
|
||||
|
||||
Reference in new issue
Block a user