Honour club and widow penalties on paragraph lines

Route-D expanded a paragraph into one node per line without TeX's
\clubpenalty/\widowpenalty, so a page break could orphan the first line or
widow the last. Insert the penalties between line nodes (150 by default, read
from the source registers) and make the page builder reject any penalty of
10000 or more as a breakpoint. Forced -10000 values now break exactly where
pdfLaTeX does; two page oracle fixtures compare per-page text, and unit tests
cover the node order and the forbidden breakpoint.
This commit is contained in:
gloamxun committed 2026-10-26 22:14:01 +08:00
1 parent 1bb28cf141
commit 8ac2e828df
6 files changed
+210 -4

No files matched your search

+21
View File
@@ -229,6 +229,8 @@ struct PageGeometry {
par_fillskip: Option<(i64, i64, bool)>,
top_skip: Option<i64>,
max_depth: Option<i64>,
club_penalty: Option<i64>,
widow_penalty: Option<i64>,
split_top_skip: Option<i64>,
split_max_depth: Option<i64>,
hyphen_penalty: Option<i64>,
@@ -2667,6 +2669,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.line_penalty = Some(value);
} else if let Some(value) = integer_assignment(trimmed, "adjdemerits") {
geometry.adj_demerits = Some(value);
} else if let Some(value) = integer_assignment(trimmed, "clubpenalty") {
geometry.club_penalty = Some(value);
} else if let Some(value) = integer_assignment(trimmed, "widowpenalty") {
geometry.widow_penalty = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "topskip") {
geometry.top_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "maxdepth") {
@@ -10079,6 +10085,8 @@ fn build_document_with_labels(
}
// LaTeX sets \maxdepth=.5\topskip; sources may override it later.
document.max_depth = 5 * SCALE;
document.club_penalty = geometry.club_penalty.unwrap_or(150);
document.widow_penalty = geometry.widow_penalty.unwrap_or(150);
document.text_width = line_width;
document.page_breaks = paragraphs
.iter()
@@ -13430,6 +13438,19 @@ A definition.
assert_eq!(document.max_depth, 5 * SCALE);
assert_eq!(document.split_top_skip, 3 * SCALE);
}
#[test]
fn geometry_reads_club_and_widow_penalties() {
let source = "\\documentclass{article}\n\\clubpenalty=77\n\\widowpenalty=88\n\\begin{document}\n\nSome words here.\n\n\\end{document}\n";
let (body, geometry) = geometry_and_body(source);
assert_eq!(geometry.club_penalty, Some(77));
assert_eq!(geometry.widow_penalty, Some(88));
assert!(!body.contains("clubpenalty"));
let paragraphs = paragraphs(&body).expect("paragraphs");
let document = build_document(&paragraphs, &geometry);
assert_eq!(document.club_penalty, 77);
assert_eq!(document.widow_penalty, 88);
}
#[test]
fn geometry_reads_demerit_registers() {
let source = "\\documentclass{article}\n\\hyphenpenalty=77\n\\exhyphenpenalty=88\n\\doublehyphendemerits=1234\n\\finalhyphendemerits=4321\n\\linepenalty=20\n\\adjdemerits=999\n\\begin{document}\n\nSome words here.\n\n\\end{document}\n";