Save the TeX cursor in native shipout order

This commit is contained in:
gloamxun committed 2026-11-21 04:27:06 +08:00
1 parent 555a029505
commit 413939911f
12 files changed
+173 -28

No files matched your search

+11 -11
View File
@@ -192,7 +192,7 @@ impl WalkBudget<'_> {
pub(super) struct LeaderEffects {
pub outputs: Vec<OutputNode>,
pub graphics_positions: Vec<[i64; 2]>,
pub output_positions: Vec<[i64; 2]>,
pub work: usize,
}
@@ -262,7 +262,7 @@ impl NativeBox {
cancellation: Option<&Cancellation>,
) -> Result<LeaderEffects, &'static str> {
let mut outputs = Vec::new();
let mut graphics_positions = Vec::new();
let mut output_positions = Vec::new();
let mut budget = WalkBudget {
remaining: limit,
cancellation,
@@ -270,15 +270,15 @@ impl NativeBox {
};
self.walk_at(0, 0, 0, false, &mut budget, &mut |x, y, item| {
if let BoxPaint::Output(o) = item {
if o.is_pdf_graphics() {
graphics_positions.push([x, y]);
if o.needs_shipout_position() {
output_positions.push([x, y]);
}
outputs.push(o.clone());
}
})?;
Ok(LeaderEffects {
outputs,
graphics_positions,
output_positions,
work: limit - budget.remaining,
})
}
@@ -762,15 +762,15 @@ impl NativeBox {
}
}
pub(super) fn pdf_graphics_positions(&self) -> Vec<[i64; 2]> {
let mut all = Vec::new();
pub(super) fn positioned_outputs(&self) -> Vec<[i64; 2]> {
let mut positions = Vec::new();
self.walk(0, 0, &mut |x, y, node| {
if matches!(node, BoxPaint::PdfGraphics) {
all.push([x, y]);
if matches!(node, BoxPaint::Output(node) if node.needs_shipout_position()) {
positions.push([x, y]);
}
})
.expect("graphics traversal of an already measured list");
all
.expect("output traversal of an already measured list");
positions
}
fn walk(
@@ -632,6 +632,7 @@ impl Printer<'_> {
}
self.tokens(tokens)?;
}
OutputOperation::PdfSavePosition => self.command("pdfsavepos"),
OutputOperation::PdfSetMatrix(tokens) => {
self.command("pdfsetmatrix");
self.tokens(tokens)?;
@@ -2152,6 +2152,10 @@ impl Executor {
self.reads.insert(Dependency::PdfRandomState);
Ok(self.state.random_seed())
}
Meaning::Primitive(p @ (Primitive::Pdflastxpos | Primitive::Pdflastypos)) => {
self.reads.insert(Dependency::PdfLastPosition);
Ok(self.state.pdf_last_position[usize::from(p == Primitive::Pdflastypos)])
}
Meaning::Primitive(Primitive::Pdflastlink) => {
self.reads.insert(Dependency::PdfObjects);
Ok(self
@@ -2824,6 +2828,9 @@ impl Executor {
OutputOperation::PdfLink(_) => {
return self.fail("PDF link operation requires a positioned shipout");
}
OutputOperation::PdfSavePosition => {
return self.fail("PDF position requires a positioned shipout");
}
OutputOperation::PdfSave
| OutputOperation::PdfSetMatrix(_)
| OutputOperation::PdfRestore
@@ -19,6 +19,12 @@ impl Executor {
self.output_command(primitive, token.span, files)?
}
Pdfcolorstack => self.scan_pdf_color_action(token.span, files)?,
Pdfsavepos => {
self.defer_output(OutputNode {
operation: OutputOperation::PdfSavePosition,
span: token.span,
})?;
}
Pdfsave | Pdfsetmatrix | Pdfrestore | Pdfliteral => {
self.scan_pdf_graphics(primitive, token.span, files)?
}
@@ -247,13 +247,15 @@ impl Executor {
let vertical_origin =
i64::from(self.read_parameter(Parameter::Pdfvorigin).width()) + v_offset;
let mut page_width = i64::from(self.read_parameter(Parameter::Pdfpagewidth).width());
let mut page_height = i64::from(self.read_parameter(Parameter::Pdfpageheight).width());
let requested_page_height = i64::from(self.read_parameter(Parameter::Pdfpageheight).width());
if page_width == 0 {
page_width = b.width + 2 * horizontal_origin;
}
if page_height == 0 {
page_height = b.height + b.depth + 2 * vertical_origin;
}
let page_height = if requested_page_height == 0 {
b.height + b.depth + 2 * vertical_origin
} else {
requested_page_height
};
let device = ShipoutDevice {
magnification,
page_width,
@@ -300,10 +302,10 @@ impl Executor {
}
let mut link_depth = self.pdf_link_stack.len();
let has_link_material = link_depth != 0 || outputs.iter().any(|node| node.is_pdf_link());
let mut graphics_positions = if let Some(effects) = &leader_effects {
effects.graphics_positions.clone().into_iter()
} else if outputs.iter().any(|node| node.is_pdf_graphics()) {
b.pdf_graphics_positions().into_iter()
let mut output_positions = if let Some(effects) = &leader_effects {
effects.output_positions.clone().into_iter()
} else if outputs.iter().any(|node| node.needs_shipout_position()) {
b.positioned_outputs().into_iter()
} else {
Vec::new().into_iter()
};
@@ -313,6 +315,13 @@ impl Executor {
let mut has_space_nodes = false;
let mut pdf_objects = Vec::new();
for output in outputs {
let position = if output.needs_shipout_position() {
Some(output_positions.next().ok_or_else(|| {
self.error("missing positioned native output node", output.span)
})?)
} else {
None
};
if let OutputOperation::PdfObject(number) = output.operation {
self.current = output.span;
self.tick()?;
@@ -329,15 +338,34 @@ impl Executor {
if device.pdf.is_none() {
return self.fail("cannot ship a PDF graphics operation to DVI");
}
let point = graphics_positions.next().ok_or_else(|| {
self.error("missing positioned PDF graphics node", output.span)
})?;
pdf_graphics_steps.push(self.ship_pdf_graphics(
output,
point,
position.expect("PDF graphics need a shipout position"),
&mut graphics_stack,
files,
)?);
} else if let OutputOperation::PdfSavePosition = output.operation {
self.current = output.span;
self.tick()?;
let [x, y] = position.expect("savepos needs a shipout position");
let (left, top, height) = if device.pdf.is_some() {
(horizontal_origin, vertical_origin, page_height)
} else {
const DVI_ORIGIN: i64 = 4_736_286;
let top = v_offset + DVI_ORIGIN;
let height = if requested_page_height == 0 {
b.height + b.depth + 2 * top
} else {
requested_page_height
};
(h_offset + DVI_ORIGIN, top, height)
};
self.state.pdf_last_position = [
i32::try_from(left + x)
.map_err(|_| self.error("horizontal saved position overflow", output.span))?,
i32::try_from(height - top - b.height - y)
.map_err(|_| self.error("vertical saved position overflow", output.span))?,
];
} else if let OutputOperation::PdfTextSpace(operation) = output.operation {
self.current = output.span;
self.tick()?;
@@ -371,8 +399,8 @@ impl Executor {
self.output_node(output, files)?;
}
}
if graphics_positions.next().is_some() {
return self.fail("unexpected positioned PDF graphics node");
if output_positions.next().is_some() {
return self.fail("unexpected positioned native output node");
}
if let Some((_, span)) = graphics_stack.last() {
return Err(self.error(
@@ -213,6 +213,8 @@ impl Executor {
| Primitive::Pdflastximage
| Primitive::Pdflastximagepages
| Primitive::Pdflastximagecolordepth
| Primitive::Pdflastxpos
| Primitive::Pdflastypos
| Primitive::Numexpr
| Primitive::Etexversion
| Primitive::Currentgrouplevel
@@ -8,8 +8,8 @@ use std::{
sync::{Arc, Mutex, OnceLock, Weak},
};
const SCHEMA: u32 = 43;
const COMPATIBLE_SCHEMAS: [u32; 12] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, SCHEMA];
const SCHEMA: u32 = 44;
const COMPATIBLE_SCHEMAS: [u32; 13] = [27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, SCHEMA];
const ENGINE: &str = concat!("texrra-native/", env!("CARGO_PKG_VERSION"));
#[derive(serde::Serialize, serde::Deserialize)]
@@ -252,6 +252,7 @@ pub enum OutputOperation {
shipout: bool,
tokens: Vec<Token>,
},
PdfSavePosition,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -275,6 +276,10 @@ impl OutputNode {
)
}
pub(super) fn needs_shipout_position(&self) -> bool {
self.is_pdf_graphics() || matches!(self.operation, OutputOperation::PdfSavePosition)
}
pub(super) fn dimensions(&self) -> Option<(i64, i64, i64)> {
match &self.operation {
OutputOperation::PdfImage { image, .. } => Some(image.dimensions()),
@@ -370,6 +370,7 @@ pub enum Dependency {
PdfCatalogState,
PdfObjects,
PdfImages,
PdfLastPosition,
GlyphUnicode,
ResourceStatistics,
PdfDeviceConfiguration {
@@ -118,6 +118,9 @@ pub enum Primitive {
Pdfoutline,
Pdfinfo,
Pdfsetmatrix,
Pdfsavepos,
Pdflastxpos,
Pdflastypos,
Pdfrestore,
Pdfglyphtounicode,
Pdffontattr,
@@ -637,6 +640,8 @@ pub struct State {
#[serde(with = "ordered_map")]
mathfonts: BTreeMap<(u8, u8), FontId>,
pub(super) last_badness: i32,
#[serde(default)]
pub(super) pdf_last_position: [i32; 2],
/// TeX prepare_mag freeze, independent of ordinary grouping/assignments.
pub(super) magnification_set: Option<i32>,
pub(super) output_mode_set: Option<i32>,
@@ -745,6 +750,7 @@ impl Default for State {
case_codes: BTreeMap::new(),
mathfonts: BTreeMap::new(),
last_badness: 0,
pdf_last_position: [0, 0],
magnification_set: None,
output_mode_set: None,
pdf_file_options: None,
@@ -1006,6 +1012,9 @@ impl Default for State {
("pdfoutline", Pdfoutline),
("pdfinfo", Pdfinfo),
("pdfsetmatrix", Pdfsetmatrix),
("pdfsavepos", Pdfsavepos),
("pdflastxpos", Pdflastxpos),
("pdflastypos", Pdflastypos),
("pdfrestore", Pdfrestore),
("pdfglyphtounicode", Pdfglyphtounicode),
("pdffontattr", Pdffontattr),
@@ -1413,6 +1422,7 @@ impl State {
self.split_marks.clear();
self.page_marks.clear();
self.last_badness = 0;
self.pdf_last_position = [0, 0];
self.magnification_set = None;
self.output_mode_set = None;
self.pdf_file_options = None;