Resolve native PDF structure destinations and local actions

This commit is contained in:
gloamxun committed 2026-11-21 05:04:54 +08:00
1 parent 58bcfca7b7
commit 22154b53a6
11 files changed
+284 -78

No files matched your search

@@ -17,13 +17,13 @@ pub(crate) struct PdfOutline<A> {
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub(crate) enum CatalogAction {
pub enum CatalogAction {
User(Vec<u8>),
GoTo {
target: CatalogTarget,
file: Option<Vec<u8>>,
window: Option<bool>,
structure: Option<Vec<u8>>,
structure: Option<StructureTarget>,
},
Thread {
destination: DestinationId<String>,
@@ -48,7 +48,10 @@ impl CatalogAction {
CatalogTarget::Page { view, .. } => view.len(),
};
target_len + file.as_ref().map_or(0, Vec::len)
+ structure.as_ref().map_or(0, Vec::len)
+ structure.as_ref().map_or(0, |structure| match structure {
StructureTarget::Local(id) => destination_len(id),
StructureTarget::Remote(bytes) => bytes.len(),
})
}
Self::Thread { destination, file } => {
destination_len(destination) + file.as_ref().map_or(0, Vec::len)
@@ -58,7 +61,15 @@ impl CatalogAction {
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub(crate) enum CatalogTarget {
pub enum CatalogTarget {
Destination(DestinationId<String>),
Page { number: i32, view: Vec<u8> },
}
/// Local identifier or explicit remote PDF structure destination.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum StructureTarget {
Local(DestinationId<String>),
Remote(Vec<u8>),
}
@@ -1,10 +1,12 @@
//! Typed link nodes and the annotations produced when TeX ships their lists.
use super::catalog::CatalogAction;
use crate::execution::Token;
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum LinkAction {
User(Vec<Token>),
GotoName(Vec<Token>),
Catalog(CatalogAction),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -45,6 +47,8 @@ pub enum LinkNode {
Destination {
destination: DestinationId<Vec<Token>>,
view: DestinationView,
#[serde(default, skip_serializing_if = "Option::is_none")]
structure: Option<u32>,
},
}
@@ -52,6 +56,7 @@ pub enum LinkNode {
pub enum AnnotationAction {
User(String),
GotoName(String),
Catalog(CatalogAction),
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -69,6 +74,8 @@ pub struct PageDestination {
pub destination: DestinationId<String>,
pub point: [i64; 2],
pub view: DestinationView,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub structure: Option<u32>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -427,6 +427,7 @@ pub(crate) struct ResourceOutput {
values: BTreeMap<u32, Object>,
page_references: BTreeMap<u32, u32>,
link_references: BTreeMap<u32, Option<(usize, usize)>>,
structure_references: BTreeSet<u32>,
page_entries: Vec<Dictionary>,
page_attributes: Vec<Dictionary>,
page_tree_attributes: Dictionary,
@@ -443,6 +444,7 @@ pub(crate) struct RemappedResources {
pub fonts: BTreeMap<FontId, Vec<u8>>,
pub page_objects: BTreeMap<usize, usize>,
pub link_objects: BTreeMap<(usize, usize), usize>,
pub structure_objects: BTreeMap<u32, usize>,
pub annotations: Vec<Vec<Option<Vec<u8>>>>,
pub catalog: Option<Vec<u8>>,
pub info: Option<Vec<u8>>,
@@ -537,6 +539,11 @@ impl ResourceOutput {
}
}
}
let structure_references = pages
.iter()
.flat_map(|page| &page.pdf_links.destinations)
.filter_map(|destination| destination.structure)
.collect();
let mut values = BTreeMap::new();
while let Some(number) = pending.pop_first() {
if objects.is_link(number) {
@@ -554,6 +561,7 @@ impl ResourceOutput {
values,
page_references: objects.page_references.clone(),
link_references,
structure_references,
page_entries,
page_attributes,
page_tree_attributes,
@@ -605,6 +613,21 @@ impl ResourceOutput {
remap(&mut object, &ids);
output.push(encode_value(object).expect("valid PDF object in memory"));
}
let structure_objects = self
.structure_references
.into_iter()
.map(|number| {
let id = ids.get(&number).map_or_else(
|| {
// A reference to an unwritten structure keeps a free entry.
output.push(Vec::new());
output.len()
},
|&id| id as usize,
);
(number, id)
})
.collect();
let encode = |dictionary: Dictionary| {
if dictionary.is_empty() {
return Vec::new();
@@ -623,6 +646,7 @@ impl ResourceOutput {
.collect(),
page_objects,
link_objects,
structure_objects,
annotations: self.annotation_entries.into_iter()
.map(|row| row.into_iter().map(|dict| dict.map(&encode)).collect())
.collect(),