Share pdfTeX image and page object numbers
This commit is contained in:
1 parent
57a7097857
commit
d12c4b034b
11 files changed
+321
-62
No files matched your search
@@ -17,6 +17,10 @@ pub(crate) enum ObjectBody {
|
||||
FormatLink,
|
||||
Form,
|
||||
FormResources,
|
||||
Image(usize),
|
||||
PageResources,
|
||||
PageContents,
|
||||
Pages,
|
||||
DiscardedAtDump,
|
||||
Direct(Vec<u8>),
|
||||
Stream {
|
||||
@@ -34,7 +38,8 @@ impl ObjectBody {
|
||||
fn len(&self) -> usize {
|
||||
match self {
|
||||
Self::Page | Self::Link | Self::FormatLink | Self::Form
|
||||
| Self::FormResources | Self::DiscardedAtDump => 0,
|
||||
| Self::FormResources | Self::Image(_) | Self::PageResources | Self::PageContents
|
||||
| Self::Pages | Self::DiscardedAtDump => 0,
|
||||
Self::Direct(bytes) => bytes.len(),
|
||||
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
|
||||
Self::File {
|
||||
@@ -51,7 +56,8 @@ impl ObjectBody {
|
||||
|
||||
fn parse(&self) -> Result<Object, String> {
|
||||
match self {
|
||||
Self::Page | Self::Link | Self::FormatLink | Self::Form | Self::FormResources => {
|
||||
Self::Page | Self::Link | Self::FormatLink | Self::Form | Self::FormResources
|
||||
| Self::Image(_) | 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()),
|
||||
@@ -69,6 +75,15 @@ impl ObjectBody {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub(crate) struct PageObjects {
|
||||
pub resources: u32,
|
||||
pub contents: u32,
|
||||
pub parent: u32,
|
||||
}
|
||||
|
||||
pub(crate) const PAGE_TREE_FANOUT: u32 = 6;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct PdfObjects {
|
||||
values: Vec<Option<Arc<ObjectBody>>>,
|
||||
@@ -79,6 +94,8 @@ pub struct PdfObjects {
|
||||
bytes: usize,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
page_references: BTreeMap<u32, u32>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
page_parent: Option<u32>,
|
||||
}
|
||||
|
||||
impl PdfObjects {
|
||||
@@ -125,6 +142,42 @@ impl PdfObjects {
|
||||
self.allocate(Some(Arc::new(ObjectBody::FormResources)), limit)
|
||||
}
|
||||
|
||||
pub(crate) fn reserve_image(&mut self, image: usize, limit: usize) -> Result<u32, String> {
|
||||
self.allocate(Some(Arc::new(ObjectBody::Image(image))), limit)
|
||||
}
|
||||
|
||||
pub(crate) fn image_slot(&self, number: u32) -> Option<usize> {
|
||||
match self.entry(number)?.as_deref()? {
|
||||
ObjectBody::Image(slot) => Some(*slot),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn image_count(&self) -> usize {
|
||||
self.values.iter().filter(|entry| matches!(entry.as_deref(), Some(ObjectBody::Image(_)))).count()
|
||||
}
|
||||
|
||||
pub(crate) fn retain_legacy_image_numbers(&mut self, slots: usize) {
|
||||
if slots > self.values.len() {
|
||||
let reserved = Arc::new(ObjectBody::DiscardedAtDump);
|
||||
self.values.resize(slots, Some(reserved));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn begin_page(&mut self, page: u32, limit: usize) -> Result<PageObjects, String> {
|
||||
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 })
|
||||
}
|
||||
|
||||
pub(crate) fn finish_page(&mut self, page: u32, limit: usize) -> Result<u32, String> {
|
||||
if (page - 1) % PAGE_TREE_FANOUT == 0 {
|
||||
self.page_parent = Some(self.allocate(Some(Arc::new(ObjectBody::Pages)), limit)?);
|
||||
}
|
||||
self.page_parent.ok_or_else(|| "missing native PDF page parent".into())
|
||||
}
|
||||
|
||||
pub(crate) fn form_numbers(&self) -> impl Iterator<Item = u32> + '_ {
|
||||
self.values.iter().enumerate().filter_map(|(index, entry)| {
|
||||
matches!(entry.as_deref(), Some(ObjectBody::Form)).then_some(index as u32 + 1)
|
||||
@@ -149,7 +202,8 @@ impl PdfObjects {
|
||||
let replacement = match entry.as_deref() {
|
||||
// A restored link whatsit gets its own annotation object when shipped.
|
||||
Some(ObjectBody::Link) => ObjectBody::FormatLink,
|
||||
Some(ObjectBody::Page | ObjectBody::FormResources) => ObjectBody::DiscardedAtDump,
|
||||
Some(ObjectBody::Page | ObjectBody::FormResources | ObjectBody::PageResources
|
||||
| ObjectBody::PageContents | ObjectBody::Pages) => ObjectBody::DiscardedAtDump,
|
||||
_ if self.written.contains(&number) => ObjectBody::DiscardedAtDump,
|
||||
_ => continue,
|
||||
};
|
||||
@@ -159,6 +213,7 @@ impl PdfObjects {
|
||||
self.last_link = None;
|
||||
self.bytes = self.values.iter().flatten().map(|body| body.len()).sum();
|
||||
self.page_references.clear();
|
||||
self.page_parent = None;
|
||||
!self.values.is_empty()
|
||||
}
|
||||
|
||||
@@ -342,6 +397,7 @@ pub(super) fn encode_value(object: Object) -> Result<Vec<u8>, String> {
|
||||
/// Referenced names affect SVG painting only when they are used by the page.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct PageResources {
|
||||
pub(crate) objects: Option<PageObjects>,
|
||||
pub(crate) entries: Arc<[u8]>,
|
||||
pub(crate) attributes: Arc<[u8]>,
|
||||
pub(crate) viewport: Option<super::page_attributes::Viewport>,
|
||||
@@ -397,11 +453,13 @@ impl PageResources {
|
||||
pub(crate) fn from_page(
|
||||
bytes: Arc<[u8]>,
|
||||
attributes: Arc<[u8]>,
|
||||
allocation: Option<PageObjects>,
|
||||
names: &BTreeSet<Vec<u8>>,
|
||||
objects: &PdfObjects,
|
||||
) -> Result<Self, String> {
|
||||
if names.is_empty() {
|
||||
return Ok(Self {
|
||||
objects: allocation,
|
||||
entries: bytes,
|
||||
attributes,
|
||||
..Self::default()
|
||||
@@ -413,6 +471,7 @@ impl PageResources {
|
||||
.map_err(|_| "PDF graphics state used without an ExtGState resource")?;
|
||||
let definitions = resolve_dictionary(definitions.clone(), objects)?;
|
||||
let mut resources = Self {
|
||||
objects: allocation,
|
||||
entries: bytes,
|
||||
attributes,
|
||||
..Self::default()
|
||||
@@ -544,8 +603,17 @@ impl ResourceOutput {
|
||||
}
|
||||
}
|
||||
let page_objects: BTreeSet<_> = objects.page_references.values().copied().collect();
|
||||
let form_objects: BTreeSet<_> = pages.iter().flat_map(|page| {
|
||||
let device_objects: BTreeSet<_> = pages.iter().flat_map(|page| {
|
||||
use crate::render::DisplayItem;
|
||||
page.forms.iter().flat_map(|form| [form.form.number, form.resource_number])
|
||||
.chain(page.pdf_resources.objects.into_iter().flat_map(|objects| {
|
||||
[objects.resources, objects.contents, objects.parent]
|
||||
}))
|
||||
.chain(page.items.iter().filter_map(|item| match item {
|
||||
DisplayItem::PdfImage { image, .. } => image.pdf_object_number,
|
||||
DisplayItem::PdfImageObject(object) => object.image.pdf_object_number,
|
||||
_ => None,
|
||||
}))
|
||||
}).collect();
|
||||
let annotation_entries = pages
|
||||
.iter()
|
||||
@@ -600,7 +668,7 @@ impl ResourceOutput {
|
||||
link_references.entry(number).or_insert(None);
|
||||
continue;
|
||||
}
|
||||
if values.contains_key(&number) || page_objects.contains(&number) || form_objects.contains(&number) {
|
||||
if values.contains_key(&number) || page_objects.contains(&number) || device_objects.contains(&number) {
|
||||
continue;
|
||||
}
|
||||
if objects.is_discarded_at_dump(number) {
|
||||
|
||||
Reference in new issue
Block a user