Register native PDF navigation and raw objects in write order

This commit is contained in:
gloamxun committed 2026-11-27 22:19:15 +08:00
1 parent 70ebacb056
commit e95d1264da
9 files changed
+350 -48

No files matched your search

@@ -90,6 +90,8 @@ pub struct LinkAnnotation {
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PageDestination {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub object_number: Option<u32>,
pub destination: DestinationId<String>,
pub point: [i64; 2],
pub view: DestinationView<[i64; 4]>,
@@ -1,5 +1,6 @@
//! Native PDF objects and extra resources captured at page shipout.
use crate::execution::FontId;
use super::links::DestinationId;
use lopdf::{
Dictionary, Object, Stream,
content::{Content, Operation},
@@ -36,6 +37,7 @@ pub(crate) enum ObjectBody {
from: String,
},
ObjectStream,
Destination,
}
impl ObjectBody {
@@ -44,7 +46,7 @@ impl ObjectBody {
Self::Page | Self::Link | Self::FormatLink | Self::Form
| Self::FormResources | Self::Image(_) | Self::Font(_) | Self::PageResources | Self::PageContents
| Self::Pages | Self::PngGroup | Self::PngAuxiliary | Self::ObjectStream
| Self::DiscardedAtDump => 0,
| Self::Destination | Self::DiscardedAtDump => 0,
Self::Direct(bytes) => bytes.len(),
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
Self::File {
@@ -63,7 +65,8 @@ impl ObjectBody {
match self {
Self::Page | Self::Link | Self::FormatLink | Self::Form | Self::FormResources
| Self::Image(_) | Self::Font(_) | Self::PageResources | Self::PageContents
| Self::Pages | Self::PngGroup | Self::PngAuxiliary | Self::ObjectStream => {
| Self::Pages | Self::PngGroup | Self::PngAuxiliary | Self::ObjectStream
| Self::Destination => {
Err("expected a generic native PDF object".into())
}
Self::DiscardedAtDump => Err("PDF object belongs to the format job and cannot be reused".into()),
@@ -123,6 +126,10 @@ pub struct PdfObjects {
// previous output file's pending stream or its cross references.
#[serde(skip)]
object_streams: Vec<Arc<PdfObjectStream>>,
#[serde(skip)]
destination_numbers: BTreeMap<(DestinationId<String>, bool), (u32, bool)>,
#[serde(skip)]
shipped_links: BTreeSet<u32>,
}
impl PdfObjects {
@@ -307,6 +314,37 @@ impl PdfObjects {
self.allocate(Some(Arc::new(ObjectBody::Link)), limit)
}
pub(crate) fn ship_link(&mut self, number: Option<u32>, limit: usize) -> Result<u32, String> {
if let Some(number) = number
&& matches!(self.entry(number).and_then(Option::as_deref), Some(ObjectBody::Link))
&& self.shipped_links.insert(number)
{
return Ok(number);
}
let number = self.reserve_link(limit)?;
self.shipped_links.insert(number);
Ok(number)
}
pub(crate) fn destination_number(
&mut self, id: DestinationId<String>, structure: bool, define: bool, limit: usize,
) -> Result<Option<u32>, String> {
let key = (id, structure);
if let Some((number, defined)) = self.destination_numbers.get_mut(&key) {
if define && std::mem::replace(defined, true) {
return Ok(None);
}
return Ok(Some(*number));
}
let number = self.allocate(Some(Arc::new(ObjectBody::Destination)), limit)?;
self.destination_numbers.insert(key, (number, define));
Ok(Some(number))
}
pub(crate) fn destinations(&self) -> impl Iterator<Item = (&(DestinationId<String>, bool), u32, bool)> {
self.destination_numbers.iter().map(|(key, &(number, defined))| (key, number, defined))
}
pub(crate) fn record_last_link(&mut self, number: u32) {
self.last_link = Some(number);
}
@@ -323,7 +361,7 @@ impl PdfObjects {
Some(ObjectBody::Link) => ObjectBody::FormatLink,
Some(ObjectBody::Page | ObjectBody::FormResources | ObjectBody::PageResources
| ObjectBody::PageContents | ObjectBody::Pages | ObjectBody::Font(_)
| ObjectBody::PngAuxiliary | ObjectBody::ObjectStream) => {
| ObjectBody::PngAuxiliary | ObjectBody::ObjectStream | ObjectBody::Destination) => {
ObjectBody::DiscardedAtDump
}
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
@@ -341,6 +379,8 @@ impl PdfObjects {
self.png_group = None;
self.written_png_group = None;
self.object_streams.clear();
self.destination_numbers.clear();
self.shipped_links.clear();
!self.values.is_empty()
}
@@ -359,25 +399,27 @@ impl PdfObjects {
Ok(number)
}
pub(crate) fn begin_definition(&mut self, requested: Option<u32>, limit: usize) -> Result<u32, String> {
requested.map_or_else(|| self.allocate(None, limit), Ok)
}
pub(crate) fn define(
&mut self,
requested: Option<u32>,
number: u32,
value: ObjectBody,
limit: usize,
) -> Result<u32, String> {
) -> Result<(), String> {
let bytes = self.bytes.saturating_add(value.len());
if bytes > limit {
return Err("native PDF object payload capacity exceeded".into());
}
let number = match requested {
Some(number) if self.is_reserved(number) => number,
Some(_) => return Err("native PDF object was already defined".into()),
None => self.reserve(limit)?,
};
if !self.is_reserved(number) {
return Err("native PDF object was already defined".into());
}
self.values[number as usize - 1] = Some(Arc::new(value));
self.bytes = bytes;
self.last = number as i32;
Ok(number)
Ok(())
}
pub(crate) fn unwritten(&self, number: u32) -> Result<Option<&ObjectBody>, String> {
@@ -798,7 +840,8 @@ impl ResourceOutput {
if values.contains_key(&number) || page_objects.contains(&number)
|| device_objects.contains(&number) || objects.font_owner(number).is_some()
|| matches!(objects.entry(number).and_then(Option::as_deref),
Some(ObjectBody::PngGroup | ObjectBody::PngAuxiliary | ObjectBody::ObjectStream))
Some(ObjectBody::PngGroup | ObjectBody::PngAuxiliary | ObjectBody::ObjectStream
| ObjectBody::Destination))
{
continue;
}