Expose prevgraf to the output routine

This commit is contained in:
gloamxun committed 2026-10-23 13:18:21 +08:00
1 parent d63db8d12d
commit 7a42ddc1bf
4 files changed
+68 -6

No files matched your search

+38
View File
@@ -920,6 +920,19 @@ fn item_height(item: &VerticalItem) -> i64 {
}
}
/// TeX `\prevgraf` for a finished page: the line count of the last paragraph
/// broken on it.
fn page_prev_graf(page: &Page) -> i64 {
page.items
.iter()
.rev()
.find_map(|item| match item {
VerticalItem::Paragraph(paragraph) => Some(paragraph.lines.len() as i64),
_ => None,
})
.unwrap_or(0)
}
/// Take the longest prefix of `items` whose reserved height fits `capacity`.
///
/// Returns `(accepted, remainder, reserved_height)`. A single item larger than
@@ -1923,6 +1936,7 @@ impl Engine {
insertion_skips,
pageno,
deadcycles,
prev_graf: page_prev_graf(&page),
};
let shipped_now = program.shipout_count(&mut context).ok()?;
pageno = context.pageno.wrapping_add(1);
@@ -2097,6 +2111,7 @@ impl Engine {
insertion_counts: [1000; 256],
insertion_dimens: [0; 256],
insertion_skips: [0; 256],
prev_graf: page_prev_graf(&page),
pageno,
deadcycles,
};
@@ -5849,6 +5864,29 @@ mod tests {
);
}
#[test]
fn page_prev_graf_counts_the_last_paragraph_lines() {
let style = ParagraphStyle {
line_width: 60 * 65_536,
par_indent: 0,
..ParagraphStyle::default()
};
let paragraph = layout_paragraph_with_font(
"alpha beta gamma delta epsilon zeta eta theta",
&style,
&Cmr10Metrics::Roman,
);
let lines = paragraph.lines.len() as i64;
assert!(lines > 1, "fixture should wrap, got {lines} lines");
let mut page = Page::empty();
page.items.push(VerticalItem::Paragraph(paragraph));
assert_eq!(page_prev_graf(&page), lines);
let empty = Page::empty();
assert_eq!(page_prev_graf(&empty), 0);
}
#[test]
fn output_routine_sees_the_page_depth() {
let math = Unit {
+23
View File
@@ -42,6 +42,8 @@ pub struct OutputContext {
pub pageno: i64,
/// TeX `\deadcycles`: output routines since the last shipout.
pub deadcycles: i64,
/// TeX `\prevgraf`: lines in the paragraph most recently broken.
pub prev_graf: 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.
@@ -67,6 +69,7 @@ impl Default for OutputContext {
output_penalty: 0,
pageno: 0,
deadcycles: 0,
prev_graf: 0,
// TeX's default insertion multiplier is 1000.
insertion_counts: [1000; 256],
insertion_dimens: [0; 256],
@@ -223,6 +226,7 @@ enum IntegerRegister {
PageNo,
DeadCycles,
OutputPenalty,
PrevGraf,
InsertionCount(u8),
}
@@ -234,6 +238,7 @@ impl Integer {
IntegerRegister::PageNo => context.pageno,
IntegerRegister::DeadCycles => context.deadcycles,
IntegerRegister::OutputPenalty => context.output_penalty,
IntegerRegister::PrevGraf => context.prev_graf,
IntegerRegister::InsertionCount(class) => context.insertion_counts[class as usize],
},
}
@@ -581,6 +586,7 @@ fn parse_integer(tokens: &[Token], index: &mut usize) -> Result<Integer, String>
"pageno" => IntegerRegister::PageNo,
"deadcycles" => IntegerRegister::DeadCycles,
"outputpenalty" => IntegerRegister::OutputPenalty,
"prevgraf" => IntegerRegister::PrevGraf,
"count" => {
*index += 1;
if matches!(
@@ -741,6 +747,7 @@ fn execute(ops: &[Op], context: &mut OutputContext, shipped: &mut usize) -> Resu
IntegerRegister::PageNo => context.pageno = value,
IntegerRegister::DeadCycles => context.deadcycles = value,
IntegerRegister::OutputPenalty => context.output_penalty = value,
IntegerRegister::PrevGraf => context.prev_graf = value,
IntegerRegister::InsertionCount(class) => {
context.insertion_counts[*class as usize] = value;
}
@@ -786,6 +793,7 @@ mod tests {
output_penalty: 0,
pageno: 1,
deadcycles: 0,
prev_graf: 0,
..OutputContext::default()
}
}
@@ -852,6 +860,21 @@ mod tests {
assert!(!plain.assigns_vsize());
}
#[test]
fn output_routine_sees_prevgraf() {
let mut context = context(0);
context.prev_graf = 4;
let program = OutputProgram::parse(r"\ifnum\prevgraf=4\pageno=9\fi").unwrap();
assert_eq!(program.shipout_count(&mut context).unwrap(), 0);
assert_eq!(context.pageno, 9);
let copy = OutputProgram::parse(r"\pageno=\prevgraf").unwrap();
let mut context = self::context(0);
context.prev_graf = 6;
copy.shipout_count(&mut context).unwrap();
assert_eq!(context.pageno, 6);
}
#[test]
fn output_routine_can_assign_registers() {
let mut context = context(0);