Allocate mapped Type 1 import objects in PDF write order
This commit is contained in:
1 parent
861c9dbfc9
commit
c1c3c25b3f
9 files changed
+466
-95
No files matched your search
@@ -1,5 +1,8 @@
|
||||
//! Native PDF objects and extra resources captured at page shipout.
|
||||
use crate::{execution::FontId, resources::ResourceVersion};
|
||||
use crate::{
|
||||
execution::FontId, native_font::Type1ProgramKey, resources::ResourceVersion,
|
||||
type1::Type1Transform,
|
||||
};
|
||||
use super::links::DestinationId;
|
||||
use lopdf::{
|
||||
Dictionary, Object, ObjectId, Stream,
|
||||
@@ -40,6 +43,7 @@ pub(crate) enum ObjectBody {
|
||||
ObjectStream,
|
||||
Destination,
|
||||
ImportedImage,
|
||||
ImportedFont,
|
||||
}
|
||||
|
||||
impl ObjectBody {
|
||||
@@ -48,7 +52,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::Destination | Self::ImportedImage | Self::DiscardedAtDump => 0,
|
||||
| Self::Destination | Self::ImportedImage | Self::ImportedFont | Self::DiscardedAtDump => 0,
|
||||
Self::Direct(bytes) => bytes.len(),
|
||||
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
|
||||
Self::File {
|
||||
@@ -68,7 +72,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::Destination | Self::ImportedImage => {
|
||||
| Self::Destination | Self::ImportedImage | Self::ImportedFont => {
|
||||
Err("expected a generic native PDF object".into())
|
||||
}
|
||||
Self::DiscardedAtDump => Err("PDF object belongs to the format job and cannot be reused".into()),
|
||||
@@ -135,6 +139,8 @@ pub struct PdfObjects {
|
||||
// Source object numbers belong to one particular version of one file.
|
||||
#[serde(skip)]
|
||||
imported_images: BTreeMap<PathBuf, BTreeMap<String, BTreeMap<ObjectId, u32>>>,
|
||||
#[serde(skip)]
|
||||
imported_type1: BTreeMap<(PathBuf, String, Type1Transform), (u32, Option<u32>)>,
|
||||
}
|
||||
|
||||
impl PdfObjects {
|
||||
@@ -268,21 +274,67 @@ impl PdfObjects {
|
||||
self.written_png_group
|
||||
}
|
||||
|
||||
pub(crate) fn imported_image_number(&self, version: &ResourceVersion, source: ObjectId) -> Option<u32> {
|
||||
self.imported_images.get(&version.path)?.get(&version.sha256)?.get(&source).copied()
|
||||
}
|
||||
|
||||
pub(crate) fn bind_imported_image(&mut self, version: &ResourceVersion, source: ObjectId, number: u32) {
|
||||
self.imported_images.entry(version.path.clone()).or_default()
|
||||
.entry(version.sha256.clone()).or_default().entry(source).or_insert(number);
|
||||
}
|
||||
|
||||
pub(crate) fn import_image_object(
|
||||
&mut self, version: &ResourceVersion, source: ObjectId, limit: usize,
|
||||
) -> Result<(u32, bool), String> {
|
||||
if let Some(&number) = self.imported_images.get(&version.path)
|
||||
.and_then(|versions| versions.get(&version.sha256))
|
||||
.and_then(|objects| objects.get(&source))
|
||||
{
|
||||
if let Some(number) = self.imported_image_number(version, source) {
|
||||
return Ok((number, false));
|
||||
}
|
||||
let number = self.allocate(Some(Arc::new(ObjectBody::ImportedImage)), limit)?;
|
||||
self.imported_images.entry(version.path.clone()).or_default()
|
||||
.entry(version.sha256.clone()).or_default().insert(source, number);
|
||||
self.bind_imported_image(version, source, number);
|
||||
Ok((number, true))
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_imported_font_part(&mut self, limit: usize) -> Result<u32, String> {
|
||||
self.allocate(Some(Arc::new(ObjectBody::ImportedFont)), limit)
|
||||
}
|
||||
|
||||
pub(crate) fn imported_font_numbers(&self) -> impl Iterator<Item = u32> + '_ {
|
||||
self.values.iter().enumerate().filter_map(|(index, entry)| {
|
||||
matches!(entry.as_deref(), Some(ObjectBody::ImportedFont)).then_some(index as u32 + 1)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn imported_type1_slots(&self, key: Type1ProgramKey<'_>) -> Option<(u32, Option<u32>)> {
|
||||
let (version, transform) = key;
|
||||
self.imported_type1.get(&(version.path.clone(), version.sha256.clone(), transform)).copied()
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_imported_type1_descriptor(
|
||||
&mut self, key: Type1ProgramKey<'_>, limit: usize,
|
||||
) -> Result<u32, String> {
|
||||
if let Some((descriptor, _)) = self.imported_type1_slots(key) {
|
||||
return Ok(descriptor);
|
||||
}
|
||||
let number = self.reserve_imported_font_part(limit)?;
|
||||
let (version, transform) = key;
|
||||
self.imported_type1.insert((version.path.clone(), version.sha256.clone(), transform), (number, None));
|
||||
Ok(number)
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_imported_type1_name(
|
||||
&mut self, key: Type1ProgramKey<'_>, limit: usize,
|
||||
) -> Result<(), String> {
|
||||
let descriptor = self.reserve_imported_type1_descriptor(key, limit)?;
|
||||
if self.imported_type1_slots(key).is_some_and(|(_, name)| name.is_some()) {
|
||||
return Ok(());
|
||||
}
|
||||
let number = self.reserve_imported_font_part(limit)?;
|
||||
let (version, transform) = key;
|
||||
self.imported_type1.insert((version.path.clone(), version.sha256.clone(), transform),
|
||||
(descriptor, Some(number)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn imported_image_numbers(&self) -> impl Iterator<Item = u32> + '_ {
|
||||
self.values.iter().enumerate().filter_map(|(index, entry)| {
|
||||
matches!(entry.as_deref(), Some(ObjectBody::ImportedImage)).then_some(index as u32 + 1)
|
||||
@@ -388,7 +440,7 @@ impl PdfObjects {
|
||||
Some(ObjectBody::Page | ObjectBody::FormResources | ObjectBody::PageResources
|
||||
| ObjectBody::PageContents | ObjectBody::Pages | ObjectBody::Font(_)
|
||||
| ObjectBody::PngAuxiliary | ObjectBody::ObjectStream | ObjectBody::Destination
|
||||
| ObjectBody::ImportedImage) => {
|
||||
| ObjectBody::ImportedImage | ObjectBody::ImportedFont) => {
|
||||
ObjectBody::DiscardedAtDump
|
||||
}
|
||||
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
|
||||
@@ -409,6 +461,7 @@ impl PdfObjects {
|
||||
self.destination_numbers.clear();
|
||||
self.shipped_links.clear();
|
||||
self.imported_images.clear();
|
||||
self.imported_type1.clear();
|
||||
!self.values.is_empty()
|
||||
}
|
||||
|
||||
@@ -574,7 +627,7 @@ pub(crate) fn font_attributes_affect_paint(bytes: &[u8]) -> Result<bool, String>
|
||||
Ok(attributes.iter().any(|(name, _)| name != b"ToUnicode"))
|
||||
}
|
||||
|
||||
pub(super) fn encode_value(object: Object) -> Result<Vec<u8>, String> {
|
||||
pub(crate) fn encode_value(object: Object) -> Result<Vec<u8>, String> {
|
||||
if let Object::Stream(stream) = object {
|
||||
let mut output = encode_value(Object::Dictionary(stream.dict))?;
|
||||
output.extend_from_slice(b"\nstream\n");
|
||||
@@ -869,7 +922,7 @@ impl ResourceOutput {
|
||||
|| 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
|
||||
| ObjectBody::Destination | ObjectBody::ImportedImage))
|
||||
| ObjectBody::Destination | ObjectBody::ImportedImage | ObjectBody::ImportedFont))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user