Allocate native PNG transparency and palette objects

This commit is contained in:
gloamxun committed 2026-11-27 21:30:31 +08:00
1 parent 51451813dc
commit 342bedf9b4
9 files changed
+215 -29

No files matched your search

@@ -18,6 +18,8 @@ pub(crate) enum ObjectBody {
Form,
FormResources,
Image(usize),
PngGroup,
PngAuxiliary,
Font(FontId),
PageResources,
PageContents,
@@ -40,7 +42,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::DiscardedAtDump => 0,
| Self::Pages | Self::PngGroup | Self::PngAuxiliary | Self::DiscardedAtDump => 0,
Self::Direct(bytes) => bytes.len(),
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
Self::File {
@@ -58,7 +60,8 @@ impl ObjectBody {
fn parse(&self) -> Result<Object, String> {
match self {
Self::Page | Self::Link | Self::FormatLink | Self::Form | Self::FormResources
| Self::Image(_) | Self::Font(_) | Self::PageResources | Self::PageContents | Self::Pages => {
| Self::Image(_) | Self::Font(_) | Self::PageResources | Self::PageContents
| Self::Pages | Self::PngGroup | Self::PngAuxiliary => {
Err("expected a generic native PDF object".into())
}
Self::DiscardedAtDump => Err("PDF object belongs to the format job and cannot be reused".into()),
@@ -81,6 +84,8 @@ pub(crate) struct PageObjects {
pub resources: u32,
pub contents: u32,
pub parent: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub png_group: Option<u32>,
}
pub(crate) const PAGE_TREE_FANOUT: u32 = 6;
@@ -101,6 +106,10 @@ pub struct PdfObjects {
font_numbers: BTreeMap<FontId, u32>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
scalable_fonts: BTreeMap<String, u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
png_group: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
written_png_group: Option<u32>,
}
impl PdfObjects {
@@ -184,6 +193,38 @@ impl PdfObjects {
Ok(number)
}
pub(crate) fn reserve_png_group(&mut self, limit: usize) -> Result<u32, String> {
if let Some(group) = self.png_group {
return Ok(group);
}
let group = self.allocate(Some(Arc::new(ObjectBody::PngGroup)), limit)?;
self.png_group = Some(group);
Ok(group)
}
pub(crate) fn is_png_group(&self, number: u32) -> bool {
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> {
let number = self.allocate(Some(Arc::new(ObjectBody::PngAuxiliary)), limit)?;
if mask {
self.written_png_group = self.png_group;
}
Ok(number)
}
pub(crate) fn png_object_numbers(&self) -> impl Iterator<Item = u32> + '_ {
self.values.iter().enumerate().filter_map(|(index, entry)| {
matches!(entry.as_deref(), Some(ObjectBody::PngGroup | ObjectBody::PngAuxiliary))
.then_some(index as u32 + 1)
})
}
pub(crate) fn written_png_group(&self) -> Option<u32> {
self.written_png_group
}
pub(crate) fn reserve_image(&mut self, image: usize, limit: usize) -> Result<u32, String> {
self.allocate(Some(Arc::new(ObjectBody::Image(image))), limit)
}
@@ -210,7 +251,7 @@ impl PdfObjects {
let resources = self.allocate(Some(Arc::new(ObjectBody::PageResources)), limit)?;
self.page_reference(page, limit)?;
let contents = self.allocate(Some(Arc::new(ObjectBody::PageContents)), limit)?;
Ok(PageObjects { resources, contents, parent: 0 })
Ok(PageObjects { resources, contents, parent: 0, png_group: None })
}
pub(crate) fn finish_page(&mut self, page: u32, limit: usize) -> Result<u32, String> {
@@ -245,7 +286,8 @@ impl PdfObjects {
// A restored link whatsit gets its own annotation object when shipped.
Some(ObjectBody::Link) => ObjectBody::FormatLink,
Some(ObjectBody::Page | ObjectBody::FormResources | ObjectBody::PageResources
| ObjectBody::PageContents | ObjectBody::Pages | ObjectBody::Font(_)) => {
| ObjectBody::PageContents | ObjectBody::Pages | ObjectBody::Font(_)
| ObjectBody::PngAuxiliary) => {
ObjectBody::DiscardedAtDump
}
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
@@ -260,6 +302,8 @@ impl PdfObjects {
self.page_parent = None;
self.font_numbers.clear();
self.scalable_fonts.clear();
self.png_group = None;
self.written_png_group = None;
!self.values.is_empty()
}
@@ -715,7 +759,10 @@ impl ResourceOutput {
continue;
}
if values.contains_key(&number) || page_objects.contains(&number)
|| device_objects.contains(&number) || objects.font_owner(number).is_some() {
|| device_objects.contains(&number) || objects.font_owner(number).is_some()
|| matches!(objects.entry(number).and_then(Option::as_deref),
Some(ObjectBody::PngGroup | ObjectBody::PngAuxiliary))
{
continue;
}
if objects.is_discarded_at_dump(number) {