Allocate indirect PDF import objects during native writes
This commit is contained in:
1 parent
5a2df6781a
commit
861c9dbfc9
14 files changed
+588
-32
No files matched your search
@@ -1,13 +1,14 @@
|
||||
//! Native PDF objects and extra resources captured at page shipout.
|
||||
use crate::execution::FontId;
|
||||
use crate::{execution::FontId, resources::ResourceVersion};
|
||||
use super::links::DestinationId;
|
||||
use lopdf::{
|
||||
Dictionary, Object, Stream,
|
||||
Dictionary, Object, ObjectId, Stream,
|
||||
content::{Content, Operation},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
@@ -38,6 +39,7 @@ pub(crate) enum ObjectBody {
|
||||
},
|
||||
ObjectStream,
|
||||
Destination,
|
||||
ImportedImage,
|
||||
}
|
||||
|
||||
impl ObjectBody {
|
||||
@@ -46,7 +48,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::DiscardedAtDump => 0,
|
||||
| Self::Destination | Self::ImportedImage | Self::DiscardedAtDump => 0,
|
||||
Self::Direct(bytes) => bytes.len(),
|
||||
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
|
||||
Self::File {
|
||||
@@ -66,7 +68,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::Destination | Self::ImportedImage => {
|
||||
Err("expected a generic native PDF object".into())
|
||||
}
|
||||
Self::DiscardedAtDump => Err("PDF object belongs to the format job and cannot be reused".into()),
|
||||
@@ -130,6 +132,9 @@ pub struct PdfObjects {
|
||||
destination_numbers: BTreeMap<(DestinationId<String>, bool), (u32, bool)>,
|
||||
#[serde(skip)]
|
||||
shipped_links: BTreeSet<u32>,
|
||||
// Source object numbers belong to one particular version of one file.
|
||||
#[serde(skip)]
|
||||
imported_images: BTreeMap<PathBuf, BTreeMap<String, BTreeMap<ObjectId, u32>>>,
|
||||
}
|
||||
|
||||
impl PdfObjects {
|
||||
@@ -263,6 +268,27 @@ impl PdfObjects {
|
||||
self.written_png_group
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
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);
|
||||
Ok((number, true))
|
||||
}
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_image(&mut self, image: usize, limit: usize) -> Result<u32, String> {
|
||||
self.allocate(Some(Arc::new(ObjectBody::Image(image))), limit)
|
||||
}
|
||||
@@ -361,7 +387,8 @@ 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::Destination) => {
|
||||
| ObjectBody::PngAuxiliary | ObjectBody::ObjectStream | ObjectBody::Destination
|
||||
| ObjectBody::ImportedImage) => {
|
||||
ObjectBody::DiscardedAtDump
|
||||
}
|
||||
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
|
||||
@@ -381,6 +408,7 @@ impl PdfObjects {
|
||||
self.object_streams.clear();
|
||||
self.destination_numbers.clear();
|
||||
self.shipped_links.clear();
|
||||
self.imported_images.clear();
|
||||
!self.values.is_empty()
|
||||
}
|
||||
|
||||
@@ -841,7 +869,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::Destination | ObjectBody::ImportedImage))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user