Verify parindent, parshape and skips against pdfLaTeX

This commit is contained in:
gloamxun committed 2026-10-23 13:10:40 +08:00
1 parent 97b72ac5d9
commit 3df21201c2
3 files changed
+88 -3

No files matched your search

+85
View File
@@ -355,6 +355,19 @@ fn paragraph_fixture_with(text: &str, width_pt: u32, prefix: &str) -> String {
)
}
/// Like `paragraph_fixture_with`, but leaves the first-line indent active so
/// `\parindent` fixtures can exercise it.
fn indented_paragraph_fixture(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 {prefix}{text}\\par\n\
\\end{{document}}\n"
)
}
const WORDS: &str = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu";
fn compare_fixture(text: &str, width_pt: u32) {
@@ -402,6 +415,21 @@ fn ragged_right_matches_pdftex() {
});
}
/// `\leftskip` plus `\rightskip` narrow the measure exactly like pdfLaTeX,
/// so the line breaks and relative word positions must match.
#[test]
fn left_skip_and_right_skip_match_pdftex() {
compare_fixture_with(
WORDS,
150,
"\\leftskip=20pt \\rightskip=30pt\\relax ",
|style| {
style.left_skip = 20 * PT;
style.right_skip = 30 * PT;
},
);
}
/// `\hangindent`/`\hangafter` shift the affected lines and narrow their
/// measure exactly like pdfLaTeX.
#[test]
@@ -452,6 +480,63 @@ fn parfillskip_without_fil_justifies_the_last_line() {
assert_lines_match("parfillskip 120pt", &ours, &reference);
}
/// `\parindent` only narrows the first line; the break positions must match
/// pdfLaTeX with the indent active.
#[test]
fn par_indent_matches_pdftex() {
let width = 120;
let Some(reference) = pdftex_lines(&indented_paragraph_fixture(
WORDS,
width,
"\\parindent=30pt ",
)) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_d_lines_with(WORDS, f64::from(width), |style| {
style.par_indent = 30 * PT;
});
assert_lines_match("parindent 120pt", &ours, &reference);
assert!(ours.len() >= 3, "fixture must span at least three lines");
}
/// `\parshape` gives each line its own indent and measure, repeating the last
/// pair once the list is exhausted.
#[test]
fn par_shape_matches_pdftex() {
let width = 150;
let text = format!("{WORDS} {WORDS}");
let prefix = "\\parshape=3 0pt 120pt 20pt 100pt 40pt 80pt ";
let Some(reference) = pdftex_lines(&paragraph_fixture_with(&text, width, prefix)) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_d_lines_with(&text, f64::from(width), |style| {
style.par_shape = vec![(0, 120 * PT), (20 * PT, 100 * PT), (40 * PT, 80 * PT)];
});
assert_lines_match("parshape 150pt", &ours, &reference);
assert!(ours.len() >= 5, "fixture must span at least five lines");
let steps: Vec<f64> = ours
.windows(2)
.map(|pair| pair[1].start - pair[0].start)
.collect();
assert!(
(steps[0] - 20.0).abs() <= 0.1,
"second line starts at {}",
steps[0]
);
assert!(
(steps[1] - 20.0).abs() <= 0.1,
"third line starts at {}",
steps[1]
);
assert!(
steps[2].abs() <= 0.1,
"later lines repeat the last indent: {}",
steps[2]
);
}
/// Sentence punctuation changes the space factor, so the interword glue and
/// the resulting line breaks must match pdfLaTeX.
#[test]