Read insertion class registers in the output routine

This commit is contained in:
gloamxun committed 2026-10-23 12:28:09 +08:00
1 parent 72f5436318
commit c2ba59906c
3 files changed
+145 -3

No files matched your search

+104 -1
View File
@@ -10,7 +10,7 @@
use std::fmt;
/// Page registers visible to the output routine.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OutputContext {
/// TeX `\pagegoal`: the goal the current page was built for.
pub page_goal: i64,
@@ -40,6 +40,37 @@ pub struct OutputContext {
pub pageno: i64,
/// TeX `\deadcycles`: output routines since the last shipout.
pub deadcycles: i64,
/// TeX `\count<n>` for insertion class `n`: the insertion multiplier.
pub insertion_counts: [i64; 256],
/// TeX `\dimen<n>` for insertion class `n`: the class' maximum size.
pub insertion_dimens: [i64; 256],
/// TeX `\skip<n>` for insertion class `n`.
pub insertion_skips: [i64; 256],
}
impl Default for OutputContext {
fn default() -> Self {
Self {
page_goal: 0,
page_total: 0,
page_depth: 0,
page_stretch: 0,
page_fil_stretch: 0,
page_fill_stretch: 0,
page_filll_stretch: 0,
page_shrink: 0,
vsize: 0,
top_skip: 0,
max_depth: 0,
output_penalty: 0,
pageno: 0,
deadcycles: 0,
// TeX's default insertion multiplier is 1000.
insertion_counts: [1000; 256],
insertion_dimens: [0; 256],
insertion_skips: [0; 256],
}
}
}
/// A parsed `\output` program.
@@ -118,6 +149,8 @@ enum Register {
Vsize,
TopSkip,
MaxDepth,
InsertionDimen(u8),
InsertionSkip(u8),
}
impl Dimension {
@@ -136,6 +169,8 @@ impl Dimension {
Register::Vsize => context.vsize,
Register::TopSkip => context.top_skip,
Register::MaxDepth => context.max_depth,
Register::InsertionDimen(class) => context.insertion_dimens[class as usize],
Register::InsertionSkip(class) => context.insertion_skips[class as usize],
},
}
}
@@ -152,6 +187,7 @@ enum IntegerRegister {
PageNo,
DeadCycles,
OutputPenalty,
InsertionCount(u8),
}
impl Integer {
@@ -162,6 +198,7 @@ impl Integer {
IntegerRegister::PageNo => context.pageno,
IntegerRegister::DeadCycles => context.deadcycles,
IntegerRegister::OutputPenalty => context.output_penalty,
IntegerRegister::InsertionCount(class) => context.insertion_counts[class as usize],
},
}
}
@@ -468,6 +505,18 @@ fn parse_integer(tokens: &[Token], index: &mut usize) -> Result<Integer, String>
"pageno" => IntegerRegister::PageNo,
"deadcycles" => IntegerRegister::DeadCycles,
"outputpenalty" => IntegerRegister::OutputPenalty,
"count" => {
*index += 1;
if matches!(
tokens.get(*index),
Some(Token::Number { value, unit: None }) if value.round() as i64 == 0
) {
*index += 1;
return Ok(Integer::Register(IntegerRegister::PageNo));
}
let class = parse_insertion_class(tokens, index, "\\count", false)?;
return Ok(Integer::Register(IntegerRegister::InsertionCount(class)));
}
other => return Err(format!("unsupported integer register `\\{other}`")),
};
*index += 1;
@@ -507,6 +556,16 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result<Dimension, Str
"vsize" => Register::Vsize,
"topskip" => Register::TopSkip,
"maxdepth" => Register::MaxDepth,
"dimen" => {
*index += 1;
let class = parse_insertion_class(tokens, index, "\\dimen", true)?;
return Ok(Dimension::Register(Register::InsertionDimen(class)));
}
"skip" => {
*index += 1;
let class = parse_insertion_class(tokens, index, "\\skip", true)?;
return Ok(Dimension::Register(Register::InsertionSkip(class)));
}
other => return Err(format!("unsupported page register `\\{other}`")),
};
*index += 1;
@@ -517,6 +576,28 @@ fn parse_dimension(tokens: &[Token], index: &mut usize) -> Result<Dimension, Str
Ok(dimension)
}
fn parse_insertion_class(
tokens: &[Token],
index: &mut usize,
command: &str,
allow_zero: bool,
) -> Result<u8, String> {
match tokens.get(*index) {
Some(Token::Number { value, unit: None }) => {
let class = value.round() as i64;
let lowest = if allow_zero { 0 } else { 1 };
if !(lowest..=255).contains(&class) {
return Err(format!(
"`{command}` needs an insertion class number between {lowest} and 255"
));
}
*index += 1;
Ok(class as u8)
}
_ => Err(format!("`{command}` needs an insertion class number")),
}
}
fn execute(ops: &[Op], context: &OutputContext, shipped: &mut usize) -> Result<(), String> {
for op in ops {
match op {
@@ -603,6 +684,7 @@ mod tests {
output_penalty: 0,
pageno: 1,
deadcycles: 0,
..OutputContext::default()
}
}
@@ -655,6 +737,27 @@ mod tests {
assert_eq!(program.shipout_count(&context(0)).unwrap(), 0);
}
#[test]
fn insertion_registers_are_visible() {
let mut context = context(0);
context.insertion_counts[1] = 2000;
context.insertion_dimens[1] = 7 * 65_536;
context.insertion_skips[1] = 3 * 65_536;
let count = OutputProgram::parse(r"\ifnum\count1=2000\shipout\box255\fi").unwrap();
assert_eq!(count.shipout_count(&context).unwrap(), 1);
assert_eq!(count.shipout_count(&self::context(0)).unwrap(), 0);
let dimen = OutputProgram::parse(r"\ifdim\dimen1>6pt\shipout\box255\fi").unwrap();
assert_eq!(dimen.shipout_count(&context).unwrap(), 1);
let skip = OutputProgram::parse(r"\ifdim\skip1<4pt\shipout\box255\fi").unwrap();
assert_eq!(skip.shipout_count(&context).unwrap(), 1);
let pageno = OutputProgram::parse(r"\ifnum\count0=1\shipout\box255\fi").unwrap();
assert_eq!(pageno.shipout_count(&context).unwrap(), 1);
}
#[test]
fn conditional_ships_only_large_pages() {
let program = OutputProgram::parse(r"\ifdim\pagetotal>50pt\shipout\box255\fi").unwrap();