Emit native PDF literals at scan and shipout boundaries

This commit is contained in:
gloamxun committed 2026-11-21 02:03:25 +08:00
1 parent 13ab32247d
commit 432c1d2ba7
21 files changed
+264 -61

No files matched your search

@@ -75,11 +75,11 @@ impl NativeShipout {
return;
}
boxes::GeometryItem::PdfGraphics { x, y } => {
if let Some(&operation) = graphics_steps.next() {
if let Some(operation) = graphics_steps.next() {
page.items.push(DisplayItem::PdfGraphics {
x: self.device.horizontal_origin + x,
y: self.device.vertical_origin + self.box_node.height + y,
operation,
operation: operation.clone(),
});
} else {
missing_graphics_step = true;
@@ -614,6 +614,19 @@ impl Printer<'_> {
LinkNode::Destination { .. } => "pdfdest",
});
}
OutputOperation::PdfLiteral { mode, shipout, tokens } => {
use crate::pdf_device::PdfLiteralMode;
self.command("pdfliteral");
if *shipout {
self.literal(" shipout");
}
match mode {
PdfLiteralMode::Origin => {}
PdfLiteralMode::Page => self.literal(" page"),
PdfLiteralMode::Direct => self.literal(" direct"),
}
self.tokens(tokens)?;
}
OutputOperation::PdfSetMatrix(tokens) => {
self.command("pdfsetmatrix");
self.tokens(tokens)?;
@@ -1510,7 +1510,10 @@ impl Executor {
self.push(vec![
Token::new(TokenKind::Character('{', CatCode::BeginGroup), span).into(),
])?;
let expanded = self.scan_expanded_group(files)?;
let previous_writing = std::mem::replace(&mut self.writing, true);
let expanded = self.scan_expanded_group(files);
self.writing = previous_writing;
let expanded = expanded?;
if !self.required_raw(files)?.end_write {
return self.fail("unbalanced expanded native write");
}
@@ -2790,7 +2793,8 @@ impl Executor {
}
OutputOperation::PdfSave
| OutputOperation::PdfSetMatrix(_)
| OutputOperation::PdfRestore => {
| OutputOperation::PdfRestore
| OutputOperation::PdfLiteral { .. } => {
return self.fail("PDF graphics operation requires a positioned shipout");
}
OutputOperation::Open { stream, name, from } => {
@@ -2812,10 +2816,7 @@ impl Executor {
}
}
OutputOperation::Write { stream, tokens } => {
let previous_writing = std::mem::replace(&mut self.writing, true);
let expanded = self.fully_expand(tokens.clone(), files);
self.writing = previous_writing;
let expanded = expanded?;
let expanded = self.fully_expand(tokens.clone(), files)?;
let text = self.token_list_text(&expanded);
let mut text = self.external_print(&text);
if *stream == 18 {
@@ -3758,7 +3759,7 @@ impl Executor {
self.output_command(primitive, token.span, files)?
}
Pdfcolorstack => self.scan_pdf_color_action(token.span, files)?,
Pdfsave | Pdfsetmatrix | Pdfrestore => {
Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfliteral => {
self.scan_pdf_graphics(primitive, token.span, files)?
}
Pdfstartlink | Pdfendlink | Pdfdest => {
@@ -788,7 +788,7 @@ impl Executor {
| Scriptscriptfont | Endlinechar | Newlinechar | Escapechar | Globaldefs | Time
| Day | Month | Year | BeginGroup | EndGroup | Relax | Message | Errmessage
| Endinput | Showbox | Show | Showthe | Discretionary | DiscretionaryHyphen
| Parshape | Partokenname => {
| Parshape | Partokenname | Pdfliteral => {
return Ok(false);
}
_ => return self.fail(format!("unsupported native math primitive: {p:?}")),
@@ -1,5 +1,5 @@
use super::*;
use crate::pdf_device::{PdfGraphics, PdfMatrix};
use crate::pdf_device::{PdfGraphics, PdfLiteralMode, PdfMatrix};
impl Executor {
pub(super) fn scan_pdf_graphics(
@@ -14,6 +14,26 @@ impl Executor {
let operation = match command {
Primitive::Pdfsave => OutputOperation::PdfSave,
Primitive::Pdfrestore => OutputOperation::PdfRestore,
Primitive::Pdfliteral => {
let shipout = self.keyword("shipout", files)?;
let mode = if self.keyword("direct", files)? {
PdfLiteralMode::Direct
} else if self.keyword("page", files)? {
PdfLiteralMode::Page
} else {
PdfLiteralMode::Origin
};
let tokens = if shipout {
self.scan_group(files)?
} else {
self.scan_expanded_group(files)?
};
OutputOperation::PdfLiteral {
mode,
shipout,
tokens,
}
}
Primitive::Pdfsetmatrix => {
let tokens = self.with_scanner(ScannerStatus::Absorbing, |scanner| {
scanner.scan_expanded_group_mode(files, false)
@@ -45,6 +65,19 @@ impl Executor {
.map_err(|error| self.error(format!("\\pdfsetmatrix: {error}"), node.span))?;
Ok(PdfGraphics::Matrix(matrix))
}
OutputOperation::PdfLiteral { mode, shipout, tokens } => {
let expanded;
let tokens = if *shipout {
expanded = self.fully_expand(tokens.clone(), files)?;
&expanded
} else {
tokens
};
Ok(PdfGraphics::Literal {
mode: *mode,
bytes: self.pdf_token_bytes(tokens)?.into(),
})
}
OutputOperation::PdfRestore => {
let (shift, warning) = match saved.pop() {
Some((previous, _)) if previous == point => ([0, 0], None),
@@ -231,6 +231,7 @@ impl Executor {
previous.1 || *dx != 0 || *dy != 0);
}
}
Some(PdfGraphics::Literal { .. }) => {}
None => failure = Some(self.error("missing PDF transform at link shipout", self.current)),
},
BoxPaint::RowStart(row) => {
@@ -8,8 +8,8 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 37;
const COMPATIBLE_SCHEMAS: [u32; 9] = [27, 28, 31, 32, 33, 34, 35, 36, SCHEMA];
const SCHEMA: u32 = 38;
const COMPATIBLE_SCHEMAS: [u32; 10] = [27, 28, 31, 32, 33, 34, 35, 36, 37, SCHEMA];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
#[derive(serde::Serialize, serde::Deserialize)]
@@ -246,6 +246,11 @@ pub enum OutputOperation {
image: std::sync::Arc<crate::pdf_image::PdfImageUse>,
origin: super::TokenOrigin,
},
PdfLiteral {
mode: crate::pdf_device::PdfLiteralMode,
shipout: bool,
tokens: Vec<Token>,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -265,6 +270,7 @@ impl OutputNode {
OutputOperation::PdfSave
| OutputOperation::PdfSetMatrix(_)
| OutputOperation::PdfRestore
| OutputOperation::PdfLiteral { .. }
)
}
@@ -328,6 +328,7 @@ pub enum Primitive {
Pdfnormaldeviate,
Pdfpageref,
Partokenname,
Pdfliteral,
}
impl Primitive {
@@ -970,6 +971,7 @@ impl Default for State {
("pdfnormaldeviate", Pdfnormaldeviate),
("pdfpageref", Pdfpageref),
("partokenname", Partokenname),
("pdfliteral", Pdfliteral),
("pdfcolorstack", Pdfcolorstack),
("pdfsave", Pdfsave),
("pdfstartlink", Pdfstartlink),
@@ -174,7 +174,9 @@ impl Collector {
}
}
pub fn output(&mut self, n: &OutputNode) {
if let OutputOperation::Write { tokens, .. } = &n.operation {
if let OutputOperation::Write { tokens, .. }
| OutputOperation::PdfLiteral { tokens, .. } = &n.operation
{
self.usage.token_list_cells += tokens.len();
}
}