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

+56
View File
@@ -1720,6 +1720,12 @@ pub struct Document {
/// material moves into the page height, as in TeX's page builder.
#[serde(default)]
pub max_depth: i64,
/// TeX `\clubpenalty`: discouraged break after a paragraph's first line.
#[serde(default = "default_club_penalty")]
pub club_penalty: i64,
/// TeX `\widowpenalty`: discouraged break before a paragraph's last line.
#[serde(default = "default_widow_penalty")]
pub widow_penalty: i64,
/// Minimum percentage of the page that text must occupy before a bottom
/// float is allowed onto the page (TeX's `\textfraction`, in percent).
#[serde(default = "default_text_fraction")]
@@ -1824,6 +1830,14 @@ fn default_split_max_depth() -> i64 {
i64::MAX
}
fn default_club_penalty() -> i64 {
150
}
fn default_widow_penalty() -> i64 {
150
}
fn default_split_top_skip() -> i64 {
10 * 65_536
}
@@ -1875,6 +1889,8 @@ impl Document {
text_width: default_text_width(),
top_skip: 0,
max_depth: 0,
club_penalty: default_club_penalty(),
widow_penalty: default_widow_penalty(),
text_fraction_percent: default_text_fraction(),
float_page_fraction_percent: 50,
bottom_fraction_percent: default_bottom_fraction(),
@@ -2039,6 +2055,8 @@ fn document_nodes(
// one node per line so the page builder is free to break
// inside it; each slice renders as its own single-line
// paragraph and keeps the baselines of its own line.
let total_lines = paragraph.lines.len();
let mut line_index = 0_usize;
let mut remaining = paragraph.clone();
loop {
let tail = if remaining.lines.len() > 1 {
@@ -2062,13 +2080,29 @@ fn document_nodes(
items.push(VerticalItem::Paragraph(remaining.clone()));
match tail {
Some(tail) => {
// TeX inserts \clubpenalty after the first line
// and \widowpenalty before the last, so a page
// break that would leave a single line behind
// pays the paragraph's penalty.
if line_index == 0 && document.club_penalty != 0 {
nodes.push(VerticalNode::Penalty(document.club_penalty));
owners.push(index);
items.push(VerticalItem::Penalty(document.club_penalty));
}
// TeX puts interline glue between lines; a
// zero-width glue node is the legal breakpoint
// the page builder needs to split here.
nodes.push(VerticalNode::Glue(VerticalGlue::new(0, 0, 0)));
owners.push(index);
items.push(VerticalItem::Glue(0));
if line_index + 1 == total_lines - 1 && document.widow_penalty != 0
{
nodes.push(VerticalNode::Penalty(document.widow_penalty));
owners.push(index);
items.push(VerticalItem::Penalty(document.widow_penalty));
}
remaining = tail;
line_index += 1;
}
None => break,
}
@@ -4452,6 +4486,28 @@ mod tests {
assert_eq!(document.paper_height, 792 * 65_536);
}
#[test]
fn paragraph_lines_carry_club_and_widow_penalties() {
let style = crate::paragraph::ParagraphStyle {
line_width: 120 * 65_536,
..Default::default()
};
let text = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu ".repeat(8);
let unit = Unit::paragraph_with_style(1, text, style);
let mut document = Document::new(700 * 65_536, vec![unit]);
document.club_penalty = 77;
document.widow_penalty = 88;
let (nodes, _, _) = document_nodes(&document).expect("page-builder nodes");
let penalties: Vec<i64> = nodes
.iter()
.filter_map(|node| match node {
VerticalNode::Penalty(value) => Some(*value),
_ => None,
})
.collect();
assert_eq!(penalties, vec![77, 88]);
}
fn document() -> Document {
Document::new(
50,