Write native pdfTeX Form XObjects

This commit is contained in:
gloamxun committed 2026-11-27 15:06:44 +08:00
1 parent 7ff988031f
commit c2d3312e5a
23 files changed
+534 -88

No files matched your search

@@ -0,0 +1,25 @@
//! Geometry and captured contents of a native PDF Form XObject.
use crate::{execution::NativeShipout, render::DisplayList};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct FormUse {
pub number: u32,
pub width: i64,
pub height: i64,
pub depth: i64,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ShippedForm {
pub form: FormUse,
pub attributes: Vec<u8>,
pub canvas: NativeShipout,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormDisplay {
pub form: FormUse,
pub list: DisplayList,
}
@@ -15,6 +15,7 @@ pub(crate) enum ObjectBody {
Page,
Link,
FormatLink,
Form,
Direct(Vec<u8>),
Stream {
attributes: Vec<u8>,
@@ -30,7 +31,7 @@ pub(crate) enum ObjectBody {
impl ObjectBody {
fn len(&self) -> usize {
match self {
Self::Page | Self::Link | Self::FormatLink => 0,
Self::Page | Self::Link | Self::FormatLink | Self::Form => 0,
Self::Direct(bytes) => bytes.len(),
Self::Stream { attributes, bytes } => attributes.len().saturating_add(bytes.len()),
Self::File {
@@ -47,7 +48,7 @@ impl ObjectBody {
fn parse(&self) -> Result<Object, String> {
match self {
Self::Page | Self::Link | Self::FormatLink => Err("expected a generic native PDF object".into()),
Self::Page | Self::Link | Self::FormatLink | Self::Form => Err("expected a generic native PDF object".into()),
Self::Direct(bytes) => parse_value(bytes),
Self::File { .. } => Err("PDF object file payload was not read before export".into()),
Self::Stream { attributes, bytes } => {
@@ -86,7 +87,7 @@ impl PdfObjects {
pub(crate) fn contains(&self, number: u32) -> bool {
self.entry(number)
.is_some_and(|entry| {
!matches!(entry.as_deref(), Some(ObjectBody::Page | ObjectBody::Link | ObjectBody::FormatLink))
!matches!(entry.as_deref(), Some(ObjectBody::Page | ObjectBody::Link | ObjectBody::FormatLink | ObjectBody::Form))
})
}
@@ -108,6 +109,10 @@ impl PdfObjects {
Ok(number)
}
pub(crate) fn reserve_form(&mut self, limit: usize) -> Result<u32, String> {
self.allocate(Some(Arc::new(ObjectBody::Form)), limit)
}
pub(crate) fn reserve_link(&mut self, limit: usize) -> Result<u32, String> {
self.allocate(Some(Arc::new(ObjectBody::Link)), limit)
}
@@ -424,6 +429,7 @@ impl PageResources {
/// Validated values prior to choosing the exporter's physical object numbers.
pub(crate) struct ResourceOutput {
actual_pages: usize,
values: BTreeMap<u32, Object>,
page_references: BTreeMap<u32, u32>,
link_references: BTreeMap<u32, Option<(usize, usize)>>,
@@ -456,7 +462,8 @@ pub(crate) struct RemappedResources {
impl ResourceOutput {
pub(crate) fn prepare(
pages: &[crate::render::DisplayList],
pages: &[&crate::render::DisplayList],
actual_pages: usize,
objects: Option<&PdfObjects>,
fonts: BTreeMap<FontId, &[u8]>,
page_tree: &[u8],
@@ -594,6 +601,7 @@ impl ResourceOutput {
}
}
Ok(Self {
actual_pages,
values,
page_references: objects.page_references.clone(),
link_references,
@@ -622,7 +630,7 @@ impl ResourceOutput {
// page leaves a free entry in the cross-reference table.
output.push(Vec::new());
let id = output.len();
if page as usize <= self.page_entries.len() {
if page as usize <= self.actual_pages {
page_objects.insert(page as usize - 1, id);
} else {
diagnostics.push(format!("PDF page {page} has been referenced but does not exist"));