Model TeX tolerance and hyphen penalty registers

This commit is contained in:
texrra committed 2026-10-23 01:05:28 +08:00
1 parent 4fbc43f5ff
commit 3725cca9d7
6 files changed
+148 -23

No files matched your search

+43 -7
View File
@@ -131,10 +131,14 @@ fn parse_bbox(xhtml: &str) -> Vec<Line> {
lines
}
/// Lay out one paragraph with route-D and return its per-line words.
fn route_d_lines(text: &str, width_pt: f64) -> Vec<Line> {
/// Lay out a paragraph after applying paragraph-register overrides.
fn route_d_lines_with(
text: &str,
width_pt: f64,
configure: impl FnOnce(&mut ParagraphStyle),
) -> Vec<Line> {
let width = (width_pt * PT as f64).round() as i64;
let style = ParagraphStyle {
let mut style = ParagraphStyle {
line_width: width,
left_skip: 0,
right_skip: 0,
@@ -144,6 +148,7 @@ fn route_d_lines(text: &str, width_pt: f64) -> Vec<Line> {
line_height: 12 * PT,
..ParagraphStyle::default()
};
configure(&mut style);
let unit = Unit::paragraph_with_style(0, text, style);
let paragraph = match unit.items.first() {
Some(VerticalItem::Paragraph(paragraph)) => paragraph,
@@ -229,12 +234,16 @@ fn assert_lines_match(label: &str, ours: &[Line], reference: &[Line]) {
}
fn paragraph_fixture(text: &str, width_pt: u32) -> String {
paragraph_fixture_with(text, width_pt, "")
}
fn paragraph_fixture_with(text: &str, width_pt: u32, prefix: &str) -> String {
format!(
"\\documentclass[10pt]{{article}}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{{empty}}\n\
\\begin{{document}}\n\
\\hsize={width_pt}pt \\parindent=0pt \\noindent {text}\\par\n\
\\hsize={width_pt}pt \\parindent=0pt {prefix}\\noindent {text}\\par\n\
\\end{{document}}\n"
)
}
@@ -242,12 +251,39 @@ fn paragraph_fixture(text: &str, width_pt: u32) -> String {
const WORDS: &str = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu";
fn compare_fixture(text: &str, width_pt: u32) {
let Some(reference) = pdftex_lines(&paragraph_fixture(text, width_pt)) else {
compare_fixture_with(text, width_pt, "", |_| {});
}
fn compare_fixture_with(
text: &str,
width_pt: u32,
tex_prefix: &str,
configure: impl FnOnce(&mut ParagraphStyle),
) {
let Some(reference) = pdftex_lines(&paragraph_fixture_with(text, width_pt, tex_prefix)) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_d_lines(text, f64::from(width_pt));
assert_lines_match(&format!("{width_pt}pt"), &ours, &reference);
let ours = route_d_lines_with(text, f64::from(width_pt), configure);
assert_lines_match(&format!("{width_pt}pt {tex_prefix}"), &ours, &reference);
}
/// `\pretolerance` lets pass 0 accept very loose lines and stops the breaker
/// before hyphenation is enabled.
#[test]
fn pretolerance_skips_hyphenation_like_tex() {
compare_fixture_with(WORDS, 120, "\\pretolerance=10000 ", |style| {
style.pretolerance = 10_000;
});
}
/// `\hyphenpenalty=10000` makes hyphenation illegal, so the final pass keeps
/// overfull lines instead.
#[test]
fn hyphen_penalty_inhibits_hyphenation() {
compare_fixture_with(WORDS, 120, "\\hyphenpenalty=10000 ", |style| {
style.hyphen_penalty = 10_000;
});
}
#[test]
+16
View File
@@ -17,6 +17,10 @@ fn body_style() -> ParagraphStyle {
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
tolerance: 200,
hyphen_penalty: 50,
ex_hyphen_penalty: 50,
line_height: 12 * 65_536,
char_width: font.width('x'),
space_width: font.space(),
@@ -154,6 +158,10 @@ fn route_d_page_matches_pdftex_raster_geometry() {
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
tolerance: 200,
hyphen_penalty: 50,
ex_hyphen_penalty: 50,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,
@@ -302,6 +310,10 @@ fn route_d_two_pages_match_pdftex_raster_geometry() {
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
tolerance: 200,
hyphen_penalty: 50,
ex_hyphen_penalty: 50,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,
@@ -580,6 +592,10 @@ fn route_d_multiline_bands_track_pdftex() {
right_aligned: false,
par_indent: 0,
emergencystretch: 0,
pretolerance: 100,
tolerance: 200,
hyphen_penalty: 50,
ex_hyphen_penalty: 50,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,