Allocate and share native PDF font objects

This commit is contained in:
gloamxun committed 2026-11-27 16:15:18 +08:00
1 parent d12c4b034b
commit 51451813dc
12 files changed
+345 -20

No files matched your search

@@ -18,6 +18,7 @@ pub(crate) enum ObjectBody {
Form,
FormResources,
Image(usize),
Font(FontId),
PageResources,
PageContents,
Pages,
@@ -38,7 +39,7 @@ impl ObjectBody {
fn len(&self) -> usize {
match self {
Self::Page | Self::Link | Self::FormatLink | Self::Form
| Self::FormResources | Self::Image(_) | Self::PageResources | Self::PageContents
| Self::FormResources | Self::Image(_) | Self::Font(_) | Self::PageResources | Self::PageContents
| Self::Pages | Self::DiscardedAtDump => 0,
Self::Direct(bytes) => bytes.len(),
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
@@ -57,7 +58,7 @@ impl ObjectBody {
fn parse(&self) -> Result<Object, String> {
match self {
Self::Page | Self::Link | Self::FormatLink | Self::Form | Self::FormResources
| Self::Image(_) | Self::PageResources | Self::PageContents | Self::Pages => {
| Self::Image(_) | Self::Font(_) | Self::PageResources | Self::PageContents | Self::Pages => {
Err("expected a generic native PDF object".into())
}
Self::DiscardedAtDump => Err("PDF object belongs to the format job and cannot be reused".into()),
@@ -96,6 +97,10 @@ pub struct PdfObjects {
page_references: BTreeMap<u32, u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
page_parent: Option<u32>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
font_numbers: BTreeMap<FontId, u32>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
scalable_fonts: BTreeMap<String, u32>,
}
impl PdfObjects {
@@ -142,6 +147,43 @@ impl PdfObjects {
self.allocate(Some(Arc::new(ObjectBody::FormResources)), limit)
}
pub(crate) fn font_number(&self, font: FontId) -> Option<u32> {
self.font_numbers.get(&font).copied()
}
pub(crate) fn font_owner(&self, number: u32) -> Option<FontId> {
match self.entry(number)?.as_deref()? {
ObjectBody::Font(font) => Some(*font),
_ => None,
}
}
pub(crate) fn font_object_numbers(&self) -> impl Iterator<Item = u32> + '_ {
self.values.iter().enumerate().filter_map(|(index, entry)| {
matches!(entry.as_deref(), Some(ObjectBody::Font(_))).then_some(index as u32 + 1)
})
}
pub(crate) fn bind_font(&mut self, font: FontId, number: u32) {
self.font_numbers.insert(font, number);
}
pub(crate) fn register_font(
&mut self, font: FontId, scalable_name: Option<&str>, limit: usize,
) -> Result<u32, String> {
let number = if let Some(number) = scalable_name.and_then(|name| self.scalable_fonts.get(name)) {
*number
} else {
let number = self.allocate(Some(Arc::new(ObjectBody::Font(font))), limit)?;
if let Some(name) = scalable_name {
self.scalable_fonts.insert(name.to_owned(), number);
}
number
};
self.bind_font(font, number);
Ok(number)
}
pub(crate) fn reserve_image(&mut self, image: usize, limit: usize) -> Result<u32, String> {
self.allocate(Some(Arc::new(ObjectBody::Image(image))), limit)
}
@@ -203,7 +245,9 @@ 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::DiscardedAtDump,
| ObjectBody::PageContents | ObjectBody::Pages | ObjectBody::Font(_)) => {
ObjectBody::DiscardedAtDump
}
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
_ => continue,
};
@@ -214,6 +258,8 @@ impl PdfObjects {
self.bytes = self.values.iter().flatten().map(|body| body.len()).sum();
self.page_references.clear();
self.page_parent = None;
self.font_numbers.clear();
self.scalable_fonts.clear();
!self.values.is_empty()
}
@@ -668,7 +714,8 @@ impl ResourceOutput {
link_references.entry(number).or_insert(None);
continue;
}
if values.contains_key(&number) || page_objects.contains(&number) || device_objects.contains(&number) {
if values.contains_key(&number) || page_objects.contains(&number)
|| device_objects.contains(&number) || objects.font_owner(number).is_some() {
continue;
}
if objects.is_discarded_at_dump(number) {