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:
1 parent
1bb28cf141
commit
8ac2e828df
6 files changed
+210
-4
No files matched your search
@@ -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,
|
||||
|
||||
@@ -587,7 +587,9 @@ impl PageBuilder {
|
||||
deferred_floats.clone(),
|
||||
));
|
||||
}
|
||||
if index > start {
|
||||
// TeX treats a penalty of 10000 or more as no break at
|
||||
// all; only the forced (<= -10000) case above is stronger.
|
||||
if index > start && *penalty < 10_000 {
|
||||
candidates.push(self.candidate(
|
||||
height,
|
||||
depth,
|
||||
@@ -928,6 +930,14 @@ mod tests {
|
||||
assert_eq!(pages[1].penalty, 10_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infinite_penalty_forbids_the_break() {
|
||||
let builder = PageBuilder::new(100);
|
||||
let nodes = vec![box_(60, 0), VerticalNode::Penalty(10_000), box_(60, 0)];
|
||||
let pages = builder.build(&nodes);
|
||||
assert_eq!(pages.len(), 1, "a 10000 penalty must not be a breakpoint");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glue_breaks_report_a_ten_thousand_penalty() {
|
||||
let builder = PageBuilder::new(100);
|
||||
|
||||
@@ -323,6 +323,17 @@ fn route_pages_text_with(
|
||||
page_goal_pt: i64,
|
||||
top_skip_pt: i64,
|
||||
) -> Vec<String> {
|
||||
route_pages_text_with_penalties(paragraphs, par_skip, page_goal_pt, top_skip_pt, 150, 150)
|
||||
}
|
||||
|
||||
fn route_document_with_penalties(
|
||||
paragraphs: &[String],
|
||||
par_skip: i64,
|
||||
page_goal_pt: i64,
|
||||
top_skip_pt: i64,
|
||||
club_penalty: i64,
|
||||
widow_penalty: i64,
|
||||
) -> Document {
|
||||
let mut units = Vec::new();
|
||||
let mut page_breaks = Vec::new();
|
||||
for text in paragraphs {
|
||||
@@ -346,9 +357,30 @@ fn route_pages_text_with(
|
||||
document.page_breaks = page_breaks;
|
||||
document.top_skip = top_skip_pt * PT;
|
||||
document.par_skip = par_skip * PT;
|
||||
document.club_penalty = club_penalty;
|
||||
document.widow_penalty = widow_penalty;
|
||||
document.text_float_sep = 0;
|
||||
// LaTeX's default \maxdepth keeps descents out of the page height.
|
||||
document.max_depth = 5 * PT;
|
||||
document
|
||||
}
|
||||
|
||||
fn route_pages_text_with_penalties(
|
||||
paragraphs: &[String],
|
||||
par_skip: i64,
|
||||
page_goal_pt: i64,
|
||||
top_skip_pt: i64,
|
||||
club_penalty: i64,
|
||||
widow_penalty: i64,
|
||||
) -> Vec<String> {
|
||||
let document = route_document_with_penalties(
|
||||
paragraphs,
|
||||
par_skip,
|
||||
page_goal_pt,
|
||||
top_skip_pt,
|
||||
club_penalty,
|
||||
widow_penalty,
|
||||
);
|
||||
Engine::layout(&document)
|
||||
.pages()
|
||||
.iter()
|
||||
@@ -356,6 +388,32 @@ fn route_pages_text_with(
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Lay out through the TeX page builder (`document_nodes`), the path route-D
|
||||
/// previews and PDFs use; club/widow penalties are inserted between lines.
|
||||
fn route_paged_text_with_penalties(
|
||||
paragraphs: &[String],
|
||||
par_skip: i64,
|
||||
page_goal_pt: i64,
|
||||
top_skip_pt: i64,
|
||||
club_penalty: i64,
|
||||
widow_penalty: i64,
|
||||
) -> Vec<String> {
|
||||
let document = route_document_with_penalties(
|
||||
paragraphs,
|
||||
par_skip,
|
||||
page_goal_pt,
|
||||
top_skip_pt,
|
||||
club_penalty,
|
||||
widow_penalty,
|
||||
);
|
||||
Engine::layout_paged(&document)
|
||||
.expect("paged layout")
|
||||
.pages()
|
||||
.iter()
|
||||
.map(page_alnum_text)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn assert_lines_match(label: &str, ours: &[Line], reference: &[Line]) {
|
||||
assert_eq!(
|
||||
ours.len(),
|
||||
@@ -760,6 +818,62 @@ fn newpage_matches_pdftex_page_breaks() {
|
||||
assert_eq!(ours, reference, "newpage page text");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn club_penalty_forces_a_break_after_the_first_line() {
|
||||
let text = format!("{WORDS} {WORDS} {WORDS}");
|
||||
let source = format!(
|
||||
"\\documentclass[10pt]{{article}}\n\
|
||||
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
|
||||
\\pagestyle{{empty}}\n\
|
||||
\\begin{{document}}\n\
|
||||
\\hsize=345pt \\parindent=15pt \\topskip=12pt \\vsize=550pt \\clubpenalty=-10000 \\widowpenalty=0\n\
|
||||
{text}\\par\n\\end{{document}}\n"
|
||||
);
|
||||
let Some(reference) = pdftex_pages(&source) else {
|
||||
eprintln!("skipping: pdflatex/pdftotext are not installed");
|
||||
return;
|
||||
};
|
||||
let ours = route_paged_text_with_penalties(&[text], 0, 550, 12, -10_000, 0);
|
||||
assert_eq!(
|
||||
ours.len(),
|
||||
reference.len(),
|
||||
"route-D produced {} pages, pdfLaTeX {}",
|
||||
ours.len(),
|
||||
reference.len()
|
||||
);
|
||||
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
|
||||
assert_eq!(ours, reference, "text on page {}", index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn widow_penalty_forces_a_break_before_the_last_line() {
|
||||
let text = format!("{WORDS} {WORDS} {WORDS}");
|
||||
let source = format!(
|
||||
"\\documentclass[10pt]{{article}}\n\
|
||||
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
|
||||
\\pagestyle{{empty}}\n\
|
||||
\\begin{{document}}\n\
|
||||
\\hsize=345pt \\parindent=15pt \\topskip=12pt \\vsize=550pt \\clubpenalty=0 \\widowpenalty=-10000\n\
|
||||
{text}\\par\n\\end{{document}}\n"
|
||||
);
|
||||
let Some(reference) = pdftex_pages(&source) else {
|
||||
eprintln!("skipping: pdflatex/pdftotext are not installed");
|
||||
return;
|
||||
};
|
||||
let ours = route_paged_text_with_penalties(&[text], 0, 550, 12, 0, -10_000);
|
||||
assert_eq!(
|
||||
ours.len(),
|
||||
reference.len(),
|
||||
"route-D produced {} pages, pdfLaTeX {}",
|
||||
ours.len(),
|
||||
reference.len()
|
||||
);
|
||||
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
|
||||
assert_eq!(ours, reference, "text on page {}", index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pages_break_like_pdftex_for_single_line_paragraphs() {
|
||||
let paragraphs: Vec<String> = (0..100)
|
||||
|
||||
Reference in new issue
Block a user