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 {