Render XeLaTeX shared page resources and scoped graphics

This commit is contained in:
gloamxun committed 2026-11-28 18:58:49 +08:00
1 parent e20acc3eb9
commit 009fde04f7
13 files changed
+445 -12

No files matched your search

@@ -663,9 +663,9 @@ impl Executor {
} else {
None
};
use crate::pdf_device::PdfGraphics;
use crate::pdf_device::{PdfGraphics, PdfLiteralMode};
match &graphics {
Some(PdfGraphics::Save) => {
Some(PdfGraphics::Save | PdfGraphics::Literal { mode: PdfLiteralMode::XdvBeginContent, .. }) => {
xdv_graphics_stack.push((special.span, xdv_transform, self.xdv_colors.current()));
}
Some(operation @ (PdfGraphics::SaveMatrix(matrix) | PdfGraphics::Matrix(matrix))) => {
@@ -680,7 +680,7 @@ impl Executor {
);
xdv_transform = xdv_transform.concatenate(pivot);
}
Some(PdfGraphics::Restore { .. }) => {
Some(PdfGraphics::Restore { .. } | PdfGraphics::Literal { mode: PdfLiteralMode::XdvEndContent, .. }) => {
let Some((_, previous, color_at_save)) = xdv_graphics_stack.pop() else {
return Err(self.error("unmatched XDV graphics restore", special.span));
};
@@ -869,6 +869,17 @@ impl Executor {
)?;
}
}
if xdv && let Some(navigation) = &mut self.state.xdv_navigation
&& navigation.has_pending_page_resources()
{
self.reads.insert(Dependency::PdfCatalogState);
let messages = Arc::make_mut(navigation).finish_page_resources(self.shipped_pages + 1)
.map_err(|message| self.error(message, current))?;
for message in messages {
files.observe_diagnostic(WriteDestination::TerminalAndLog, &message)
.map_err(|message| self.error(message, current))?;
}
}
if xdv && (!xdv_annotations.is_empty() || self.xdv_annotations.is_active()) {
shipout.pdf_links.xdv_annotations = self.ship_xdv_annotations(&shipout, &xdv_annotations, files)?;
}
@@ -69,12 +69,14 @@ impl Executor {
return Ok(());
}
self.reads.insert(Dependency::PdfCatalogState);
let warnings = if matches!(kind, b"docview" | b"outline") {
let warnings = 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)
} else {
} else if kind == b"outline" {
navigation.add_outline(operands, position, (position.page, graphics_step, 1), self.limits.tokens)
} else {
navigation.set_page_resources(operands, position, self.limits.tokens)
}
} else {
use crate::pdf_device::xdv_navigation::ObjectCommand;
@@ -176,6 +178,21 @@ impl Executor {
return Ok(Some(graphics));
}
let body = data.strip_prefix(b"pdf:").map(xdv_skip_white);
if let Some(body) = body {
for (command, mode) in [
(b"bcontent".as_slice(), PdfLiteralMode::XdvBeginContent),
(b"econtent", PdfLiteralMode::XdvEndContent),
(b"code", PdfLiteralMode::XdvCode),
] {
if let Some(operands) = xdv_operand(body, command) {
let bytes = xdv_skip_white(operands);
if mode != PdfLiteralMode::XdvCode && !bytes.is_empty() {
return self.fail("unexpected operands to XDV content boundary");
}
return Ok(Some(PdfGraphics::Literal { mode, bytes: Arc::from(bytes) }));
}
}
}
if let Some(content) = body.and_then(|body| xdv_operand(body, b"content")) {
let bytes = xdv_skip_white(content);
return Ok((!bytes.is_empty()).then(|| PdfGraphics::Literal {
@@ -339,7 +356,7 @@ pub(super) fn xdv_navigation_operand(data: &[u8]) -> Option<(&'static [u8], &[u8
(b"dest".as_slice(), b"dest".as_slice()), (b"destination", b"dest"),
(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"stream", b"stream"), (b"fstream", b"fstream"), (b"pageresources", b"pageresources"),
] {
if let Some(operands) = xdv_operand(body, name) {
return Some((kind, operands));
@@ -88,7 +88,9 @@ impl Executor {
})
} else if let Some(operation) = shipout.pdf_graphics_steps.get(step) {
match operation {
Some(PdfGraphics::Save) => saved.push(frame.transform),
Some(PdfGraphics::Save | PdfGraphics::Literal {
mode: crate::pdf_device::PdfLiteralMode::XdvBeginContent, ..
}) => saved.push(frame.transform),
Some(
operation @ (PdfGraphics::SaveMatrix(matrix)
| PdfGraphics::Matrix(matrix)),
@@ -100,7 +102,9 @@ impl Executor {
.transform
.concatenate(matrix.about(point[0], point[1], true));
}
Some(PdfGraphics::Restore { .. }) => {
Some(PdfGraphics::Restore { .. } | PdfGraphics::Literal {
mode: crate::pdf_device::PdfLiteralMode::XdvEndContent, ..
}) => {
frame.transform =
saved.pop().expect("validated graphics stack");
}