Allocate compressed PDF object streams during native output

This commit is contained in:
gloamxun committed 2026-11-27 22:02:52 +08:00
1 parent d87464fde3
commit 70ebacb056
9 files changed
+315 -46

No files matched your search

@@ -35,6 +35,7 @@ pub(crate) enum ObjectBody {
name: String,
from: String,
},
ObjectStream,
}
impl ObjectBody {
@@ -42,7 +43,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::DiscardedAtDump => 0,
| Self::Pages | Self::PngGroup | Self::PngAuxiliary | Self::ObjectStream
| Self::DiscardedAtDump => 0,
Self::Direct(bytes) => bytes.len(),
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
Self::File {
@@ -61,7 +63,7 @@ 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::Pages | Self::PngGroup | Self::PngAuxiliary | Self::ObjectStream => {
Err("expected a generic native PDF object".into())
}
Self::DiscardedAtDump => Err("PDF object belongs to the format job and cannot be reused".into()),
@@ -89,6 +91,13 @@ pub(crate) struct PageObjects {
}
pub(crate) const PAGE_TREE_FANOUT: u32 = 6;
pub(crate) const OBJECT_STREAM_CAPACITY: usize = 100;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PdfObjectStream {
pub number: u32,
pub members: Vec<u32>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PdfObjects {
@@ -110,6 +119,10 @@ pub struct PdfObjects {
png_group: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
written_png_group: Option<u32>,
// A new format job keeps the allocated numbers but never carries the
// previous output file's pending stream or its cross references.
#[serde(skip)]
object_streams: Vec<Arc<PdfObjectStream>>,
}
impl PdfObjects {
@@ -142,6 +155,19 @@ impl PdfObjects {
Ok(self.values.len() as u32)
}
pub(crate) fn pack_object(&mut self, member: u32, limit: usize) -> Result<(), String> {
if self.object_streams.last().is_none_or(|stream| stream.members.len() == OBJECT_STREAM_CAPACITY) {
let number = self.allocate(Some(Arc::new(ObjectBody::ObjectStream)), limit)?;
self.object_streams.push(Arc::new(PdfObjectStream { number, members: Vec::new() }));
}
Arc::make_mut(self.object_streams.last_mut().unwrap()).members.push(member);
Ok(())
}
pub(crate) fn object_streams(&self) -> &[Arc<PdfObjectStream>] {
&self.object_streams
}
pub(crate) fn reserve(&mut self, limit: usize) -> Result<u32, String> {
let number = self.allocate(None, limit)?;
self.last = number as i32;
@@ -206,10 +232,15 @@ impl PdfObjects {
matches!(self.entry(number).and_then(Option::as_deref), Some(ObjectBody::PngGroup))
}
pub(crate) fn reserve_png_auxiliary(&mut self, mask: bool, limit: usize) -> Result<u32, String> {
pub(crate) fn reserve_png_auxiliary(
&mut self, mask: bool, object_compression: u8, limit: usize,
) -> Result<u32, String> {
let number = self.allocate(Some(Arc::new(ObjectBody::PngAuxiliary)), limit)?;
if mask {
self.written_png_group = self.png_group;
if mask && self.written_png_group.is_none() && let Some(group) = self.png_group {
if object_compression >= 2 {
self.pack_object(group, limit)?;
}
self.written_png_group = Some(group);
}
Ok(number)
}
@@ -254,7 +285,12 @@ impl PdfObjects {
Ok(PageObjects { resources, contents, parent: 0, png_group: None })
}
pub(crate) fn finish_page(&mut self, page: u32, limit: usize) -> Result<u32, String> {
pub(crate) fn finish_page(
&mut self, page: u32, object_compression: u8, limit: usize,
) -> Result<u32, String> {
if object_compression > 0 {
self.pack_object(self.page_references[&page], limit)?;
}
if (page - 1) % PAGE_TREE_FANOUT == 0 {
self.page_parent = Some(self.allocate(Some(Arc::new(ObjectBody::Pages)), limit)?);
}
@@ -287,7 +323,7 @@ impl PdfObjects {
Some(ObjectBody::Link) => ObjectBody::FormatLink,
Some(ObjectBody::Page | ObjectBody::FormResources | ObjectBody::PageResources
| ObjectBody::PageContents | ObjectBody::Pages | ObjectBody::Font(_)
| ObjectBody::PngAuxiliary) => {
| ObjectBody::PngAuxiliary | ObjectBody::ObjectStream) => {
ObjectBody::DiscardedAtDump
}
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
@@ -304,6 +340,7 @@ impl PdfObjects {
self.scalable_fonts.clear();
self.png_group = None;
self.written_png_group = None;
self.object_streams.clear();
!self.values.is_empty()
}
@@ -761,7 +798,7 @@ 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))
Some(ObjectBody::PngGroup | ObjectBody::PngAuxiliary | ObjectBody::ObjectStream))
{
continue;
}