Capture and reuse XeLaTeX PDF form contents

This commit is contained in:
gloamxun committed 2026-11-28 19:29:07 +08:00
1 parent 009fde04f7
commit c0e2346b05
16 files changed
+706 -53

No files matched your search

@@ -634,12 +634,15 @@ impl Executor {
} else if xdv && crate::pdf_device::xdv_paper::Special::parse(&special.bytes).is_some() {
xdv_paper_specials.push(shipout.specials.len());
None
} else if xdv && super::specials::xdv_navigation_operand(&special.bytes).is_some() {
} else if xdv && let Some((kind, _)) = super::specials::xdv_navigation_operand(&special.bytes) {
if kind == b"bxobj" && let Some(program) = self.xdv_colors.start_page() {
shipout.xdv_color_steps.insert(graphics_step, program);
}
let point = xdv_transform.point(
(special.position[0] + horizontal_origin) as f64,
(special.position[1] + vertical_origin + b.height) as f64,
).map(|coordinate| coordinate.round() as i64);
xdv_navigation.push((shipout.specials.len(), graphics_step, point));
xdv_navigation.push((shipout.specials.len(), graphics_step, point, *origin));
None
} else if xdv && self.xdv_setup_special(&special)? {
None
@@ -856,8 +859,8 @@ impl Executor {
device.vertical_origin = offset + v_offset;
}
let delta = [device.horizontal_origin - horizontal_origin, device.vertical_origin - vertical_origin];
for (index, graphics_step, [x, y]) in xdv_navigation {
self.ship_xdv_navigation(
for (index, graphics_step, [x, y], origin) in xdv_navigation {
if let Some(operation) = self.ship_xdv_navigation(
&shipout.specials[index],
crate::pdf_device::xdv_object::Position {
page: self.shipped_pages + 1,
@@ -865,8 +868,13 @@ impl Executor {
height: device.page_height,
magnification,
},
graphics_step, &mut shipout.pdf_links, files,
)?;
graphics_step, &mut shipout.pdf_links, origin, files,
)? {
shipout.pdf_graphics_steps[graphics_step] = Some(operation);
}
}
if let Some(navigation) = &self.state.xdv_navigation {
navigation.validate_form_boundary().map_err(|error| self.error(error, current))?;
}
}
if xdv && let Some(navigation) = &mut self.state.xdv_navigation
@@ -45,6 +45,7 @@ impl Executor {
.reserve_form(limit)
.map_err(|message| self.error(message, self.current))?;
let form = FormUse {
xdv: None,
number,
width: box_node.width,
height: box_node.height,
@@ -472,7 +472,9 @@ impl PdfLinkShipout {
*matrix = previous;
}
}
Some(PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary) | None => {}
Some(PdfGraphics::Literal { .. } | PdfGraphics::TextBoundary
| PdfGraphics::XdvFormBegin(_) | PdfGraphics::XdvFormEnd(_)
| PdfGraphics::XdvFormUse { .. }) | None => {}
}
}
LinkPaint::RowStart(row) if !row.horizontal => {
@@ -56,8 +56,9 @@ impl Executor {
position: crate::pdf_device::xdv_object::Position,
graphics_step: usize,
links: &mut crate::pdf_device::links::PageLinks,
origin: Option<TokenOrigin>,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
) -> Result<Option<PdfGraphics>, Error> {
let Some((kind, operands)) = xdv_navigation_operand(&special.bytes) else {
return self.fail("missing shipped XDV navigation operands");
};
@@ -66,10 +67,18 @@ impl Executor {
operands, position.page, position.coordinates,
).map_err(|error| self.error(error, special.span))?;
links.xdv_destinations.push(destination);
return Ok(());
return Ok(None);
}
self.reads.insert(Dependency::PdfCatalogState);
let warnings = if matches!(kind, b"docview" | b"outline" | b"pageresources") {
let mut graphics = None;
let warnings = if matches!(kind, b"bxobj" | b"exobj" | b"uxobj") {
let source = origin.unwrap_or(TokenOrigin { span: special.span, invocation: None });
let navigation = Arc::make_mut(self.state.xdv_navigation.get_or_insert_with(Default::default));
let (operation, warnings) = navigation.form_special(kind, operands, position, source, self.limits.tokens)
.map_err(|error| self.error(error, special.span))?;
graphics = Some(operation);
Ok(warnings)
} else if matches!(kind, b"docview" | b"outline" | b"pageresources") {
let navigation = Arc::make_mut(self.state.xdv_navigation.get_or_insert_with(Default::default));
if kind == b"docview" {
navigation.merge_catalog(operands, position, self.limits.tokens)
@@ -100,7 +109,7 @@ impl Executor {
files.observe_diagnostic(WriteDestination::TerminalAndLog, &warning)
.map_err(|error| self.error(error, special.span))?;
}
Ok(())
Ok(graphics)
}
pub(super) fn xdv_setup_special(
@@ -357,6 +366,9 @@ pub(super) fn xdv_navigation_operand(data: &[u8]) -> Option<(&'static [u8], &[u8
(b"docview", b"docview"), (b"outline", b"outline"), (b"out", b"outline"),
(b"obj", b"obj"), (b"object", b"obj"), (b"put", b"put"), (b"close", b"close"),
(b"stream", b"stream"), (b"fstream", b"fstream"), (b"pageresources", b"pageresources"),
(b"bxobj", b"bxobj"), (b"bform", b"bxobj"), (b"beginxobj", b"bxobj"),
(b"exobj", b"exobj"), (b"eform", b"exobj"), (b"endxobj", b"exobj"),
(b"uxobj", b"uxobj"), (b"usexobj", b"uxobj"),
] {
if let Some(operands) = xdv_operand(body, name) {
return Some((kind, operands));