Implement native PDF links and vertical discard lists

This commit is contained in:
gloamxun committed 2026-11-18 23:39:45 +08:00
1 parent b6567f67e2
commit a53d9c9bf9
29 files changed
+980 -109

No files matched your search

+88 -57
View File
@@ -23,6 +23,7 @@ mod page;
mod paragraphs;
mod pdf_color;
mod pdf_graphics;
mod pdf_links;
mod pdf_image;
mod quantities;
mod restores;
@@ -160,6 +161,7 @@ pub struct Executor {
expansion_depth: usize,
io: IoState,
pdf_colors: crate::pdf_device::PdfColorStacks,
pdf_link_stack: Vec<pdf_links::ActiveLink>,
pdf_images: Vec<pdf_image::RegisteredImage>,
input_position: Option<(usize, usize)>,
paragraph: Option<super::paragraph::Builder>,
@@ -173,6 +175,7 @@ pub struct Executor {
math_tokens_processed: usize,
display: Option<display::DisplayFrame>,
native_page: Option<NativePageState>,
split_discards: Vec<NativeVerticalNode>,
output_frame: Option<page::OutputFrame>,
pending_shipouts: VecDeque<NativeBox>,
job_ended: bool,
@@ -226,6 +229,7 @@ impl Executor {
expansion_depth: 0,
io: IoState::default(),
pdf_colors: Default::default(),
pdf_link_stack: Vec::new(),
pdf_images: Vec::new(),
input_position: None,
paragraph: None,
@@ -239,6 +243,7 @@ impl Executor {
math_tokens_processed: 0,
display: None,
native_page: None,
split_discards: Vec::new(),
output_frame: None,
pending_shipouts: VecDeque::new(),
job_ended: false,
@@ -262,6 +267,7 @@ impl Executor {
executor.sources = execution.sources;
executor.stream_sources = execution.stream_sources;
executor.generated_inputs = execution.generated_inputs;
executor.split_discards = execution.split_discards;
executor.io = execution.io;
executor.after_assignment = execution.after_assignment;
executor.input_position = execution.input_position;
@@ -528,22 +534,44 @@ impl Executor {
return self.fail("native expansion nesting limit exceeded");
}
self.expansion_depth += 1;
let result = self.expand_one_inner(input, files, definition_context);
let result = if input.frozen {
Ok(Some(input))
} else {
match self.meaning(&input.token) {
Meaning::Primitive(Primitive::Expandafter) => self.expand_after(&input.token, files),
meaning => self.expand_one_inner(input, meaning, files, definition_context),
}
};
self.expansion_depth -= 1;
result
}
// Keep the recursive path outside the expansion dispatch frame.
fn expand_after(
&mut self,
token: &Token,
files: &mut impl SourceProvider,
) -> Result<Option<InputToken>, Error> {
if self.read_parameter(Parameter::Tracingcommands).width() > 1 {
self.trace_command(token, files)?;
}
let first = self.required_raw(files)?;
let second = self.required_raw(files)?;
if let Some(second) = self.expand_one(second, files, false)? {
self.unread(second)?;
}
self.unread(first)?;
Ok(None)
}
fn expand_one_inner(
&mut self,
input: InputToken,
meaning: Meaning,
files: &mut impl SourceProvider,
definition_context: bool,
) -> Result<Option<InputToken>, Error> {
if input.frozen {
return Ok(Some(input));
}
let token = &input.token;
use Primitive::*;
match self.meaning(token) {
match meaning {
Meaning::Undefined => {
return self.fail(format!(
"undefined native control sequence: {}",
@@ -574,14 +602,6 @@ impl Executor {
next.frozen = matches!(next.token.kind, TokenKind::Command(_));
return Ok(Some(next));
}
Expandafter => {
let first = self.required_raw(files)?;
let second = self.required_raw(files)?;
if let Some(second) = self.expand_one(second, files, false)? {
self.unread(second)?;
}
self.unread(first)?;
}
Number => {
let value = self.integer(files)?;
self.print_tokens(&value.to_string(), token)?;
@@ -1573,6 +1593,46 @@ impl Executor {
});
Ok(())
}
fn define_shorthand(
&mut self,
primitive: Primitive,
global: bool,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
use Primitive::*;
let name = self.command(files)?;
// The new name can be the very next token after the number.
self.define_traced(name.clone(), Meaning::Primitive(Relax), global, files)?;
self.optional_equals(files)?;
let value = match primitive {
Chardef => Meaning::CharGiven(
u8::try_from(self.integer(files)?)
.map_err(|_| self.error("chardef must be in 0..255", self.current))?,
),
Mathchardef => {
let code = self.integer(files)?;
if !(0..32768).contains(&code) {
return self.fail("mathchardef must be in 0..32767");
}
Meaning::MathCharGiven(code as u16)
}
Countdef => Meaning::Count(self.register_index(files)?),
Toksdef => Meaning::Toks(self.register_index(files)?),
Dimendef | Skipdef | Muskipdef => {
let kind = match primitive {
Dimendef => RegisterKind::Dimen,
Skipdef => RegisterKind::Skip,
_ => RegisterKind::Muskip,
};
Meaning::Register(Register {
kind,
index: self.register_index(files)?,
})
}
_ => unreachable!("shorthand definition primitive"),
};
self.define_traced(name, value, global, files)
}
fn optional_equals(&mut self, files: &mut impl SourceProvider) -> Result<(), Error> {
let token = self.expanded_non_space(files)?;
if !matches!(token.token.kind, TokenKind::Character('=', CatCode::Other)) {
@@ -2469,6 +2529,9 @@ impl Executor {
OutputOperation::PdfColor { .. } => {
return self.fail("PDF color operation requires native PDF paint");
}
OutputOperation::PdfLink(_) => {
return self.fail("PDF link operation requires a positioned shipout");
}
OutputOperation::PdfSave
| OutputOperation::PdfSetMatrix(_)
| OutputOperation::PdfRestore => {
@@ -2549,6 +2612,7 @@ impl Executor {
job_name: self.job_name,
interaction_mode: self.interaction_mode,
native_page: self.native_page,
split_discards: self.split_discards,
after_assignment: self.after_assignment,
incoming_state,
outgoing_state: self.state.fingerprint(),
@@ -2851,6 +2915,7 @@ impl Executor {
after_assignment: self.after_assignment,
events: self.events,
native_page: self.native_page,
split_discards: self.split_discards,
outgoing_state: self.state.fingerprint(),
state: self.state,
io: self.io,
@@ -2986,6 +3051,8 @@ impl Executor {
| Primitive::Hrule
| Primitive::Unvbox
| Primitive::Unvcopy
| Primitive::Pagediscards
| Primitive::Splitdiscards
| Primitive::Halign
)
)
@@ -3187,7 +3254,9 @@ impl Executor {
files,
)?,
Raise | Lower | Moveleft | Moveright => self.shift_box(primitive, files)?,
Unhbox | Unhcopy | Unvbox | Unvcopy => self.unpackage(primitive, files)?,
Unhbox | Unhcopy | Unvbox | Unvcopy | Pagediscards | Splitdiscards => {
self.unpackage(primitive, files)?
}
Unskip | Unkern | Unpenalty => self.delete_last(primitive)?,
ItalicCorrection => self.append_italic_correction()?,
Shipout => self.scan_box(boxes::Destination::Shipout, files)?,
@@ -3277,6 +3346,9 @@ impl Executor {
Pdfsave | Pdfsetmatrix | Pdfrestore => {
self.scan_pdf_graphics(primitive, token.span, files)?
}
Pdfstartlink | Pdfendlink | Pdfdest => {
self.scan_pdf_link(primitive, token.span, files)?;
}
Pdfximage => self.scan_pdf_image(&token, false, files)?,
Pdfrefximage => self.reference_pdf_image(&token, files)?,
_ => {
@@ -3634,49 +3706,8 @@ impl Executor {
},
)?;
}
Some(Chardef | Mathchardef) => {
let name = self.command(files)?;
self.optional_equals(files)?;
let code = self.integer(files)?;
let value = if primitive == Some(Chardef) {
Meaning::CharGiven(
u8::try_from(code)
.map_err(|_| self.error("chardef must be in 0..255", self.current))?,
)
} else {
if !(0..32768).contains(&code) {
return self.fail("mathchardef must be in 0..32767");
}
Meaning::MathCharGiven(code as u16)
};
self.define_traced(name, value, global, files)?;
}
Some(Countdef | Toksdef) => {
let name = self.command(files)?;
self.optional_equals(files)?;
let index = self.register_index(files)?;
let value = if primitive == Some(Countdef) {
Meaning::Count(index)
} else {
Meaning::Toks(index)
};
self.define_traced(name, value, global, files)?;
}
Some(Dimendef | Skipdef | Muskipdef) => {
let name = self.command(files)?;
self.optional_equals(files)?;
let index = self.register_index(files)?;
let kind = match primitive {
Some(Dimendef) => RegisterKind::Dimen,
Some(Skipdef) => RegisterKind::Skip,
_ => RegisterKind::Muskip,
};
self.define_traced(
name,
Meaning::Register(Register { kind, index }),
global,
files,
)?;
Some(p @ (Chardef | Mathchardef | Countdef | Toksdef | Dimendef | Skipdef | Muskipdef)) => {
self.define_shorthand(p, global, files)?;
}
Some(Dimen | Skip | Muskip) => {
let register = self.scan_register(primitive.unwrap(), files)?;