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
@@ -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