Justify route-D lines and compare ink bands with pdfTeX

This commit is contained in:
gloamxun committed 2026-10-22 19:45:31 +08:00
1 parent 93330a85ef
commit 4414f27438
4 files changed
+250 -7

No files matched your search

+174
View File
@@ -485,3 +485,177 @@ fn route_d_text_matches_pdftex_extraction() {
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(route, control);
}
/// Compare the vertical ink bands of a wrapped paragraph against pdfTeX. Band
/// boundaries are the pixel rows that actually contain glyphs, so this checks
/// baseline placement rather than just the count of text lines.
#[test]
fn route_d_multiline_bands_track_pdftex() {
use std::process::Command;
fn has(command: &str, version_arg: &str) -> bool {
Command::new(command)
.arg(version_arg)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
if !has("pdflatex", "--version") || !has("pdftoppm", "-v") {
return;
}
fn bands(path: &std::path::Path) -> Vec<(usize, usize, usize, usize)> {
let bytes = std::fs::read(path).expect("read PGM");
assert_eq!(&bytes[..2], b"P5", "expected binary PGM");
let mut fields = Vec::new();
let mut cursor = 2;
while fields.len() < 3 {
while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
cursor += 1;
}
let start = cursor;
while cursor < bytes.len() && bytes[cursor].is_ascii_digit() {
cursor += 1;
}
fields.push(
std::str::from_utf8(&bytes[start..cursor])
.expect("PGM number")
.parse::<usize>()
.expect("PGM number"),
);
}
while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
cursor += 1;
}
let (width, height, _) = (fields[0], fields[1], fields[2]);
let pixels = &bytes[cursor..];
assert_eq!(pixels.len(), width * height, "PGM payload size");
let mut found: Vec<(usize, usize, usize, usize)> = Vec::new();
for y in 0..height {
let inked: Vec<usize> = (0..width).filter(|x| pixels[y * width + x] < 160).collect();
if inked.is_empty() {
continue;
}
let (row_left, row_right) = (inked[0], inked[inked.len() - 1]);
match found.last_mut() {
Some((_, end, left, right)) if y <= *end + 1 => {
*end = y;
*left = (*left).min(row_left);
*right = (*right).max(row_right);
}
_ => found.push((y, y, row_left, row_right)),
}
}
found
}
let text = "The quick brown fox jumps over the lazy dog while a second sentence keeps wrapping across several lines so that the baseline grid can be compared with a real TeX paragraph built from exactly the same words and the same measure.";
let style = ParagraphStyle {
line_width: 360 * 65_536,
left_skip: 0,
right_skip: 0,
centered: false,
right_aligned: false,
par_indent: 0,
line_height: 12 * 65_536,
char_width: 10 * 65_536,
space_width: 4 * 65_536,
space_stretch: 6 * 65_536,
space_shrink: 2 * 65_536,
};
let document = Document::new(
700 * 65_536,
vec![Unit::paragraph_with_style(1, text, style)],
);
let engine = Engine::layout(&document);
let pages: Vec<_> = engine
.pages()
.iter()
.map(|page| page.display_list())
.collect();
let route_pdf = display_lists_to_pdf(&pages, 792);
let root = std::env::temp_dir().join(format!("texrra-bands-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("create band dir");
let route_pdf_path = root.join("route.pdf");
std::fs::write(&route_pdf_path, route_pdf).expect("write route PDF");
let classic = format!(
r"\documentclass[10pt]{{article}}
\pagestyle{{empty}}
\hoffset=-1in
\voffset=-1in
\oddsidemargin=0pt
\topmargin=0pt
\headheight=0pt
\headsep=0pt
\pdfpagewidth=612bp
\pdfpageheight=792bp
\textwidth=360pt
\textheight=700pt
\topskip=10pt
\parindent=0pt
\begin{{document}}
{text}
\end{{document}}
"
);
let classic_path = root.join("classic.tex");
std::fs::write(&classic_path, classic).expect("write classic TeX");
let status = Command::new("pdflatex")
.args(["-interaction=nonstopmode", "-halt-on-error"])
.arg(&classic_path)
.current_dir(&root)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.expect("pdflatex");
assert!(status.success(), "pdflatex failed");
for pdf in [&route_pdf_path, &root.join("classic.pdf")] {
let status = Command::new("pdftoppm")
.args(["-r", "72", "-gray", "-singlefile"])
.arg(pdf)
.arg(pdf.with_extension(""))
.status()
.expect("pdftoppm");
assert!(status.success(), "pdftoppm failed for {}", pdf.display());
}
let route = bands(&root.join("route.pgm"));
let classic = bands(&root.join("classic.pgm"));
assert_eq!(
route.len(),
classic.len(),
"line bands: route={route:?} classic={classic:?}"
);
let last = route.len().saturating_sub(1);
for (index, (route_band, classic_band)) in route.iter().zip(&classic).enumerate() {
assert!(
(route_band.0 as i64 - classic_band.0 as i64).abs() <= 2,
"band {index} top: route={route_band:?} classic={classic_band:?}"
);
assert!(
(route_band.1 as i64 - classic_band.1 as i64).abs() <= 2,
"band {index} bottom: route={route_band:?} classic={classic_band:?}"
);
assert!(
(route_band.2 as i64 - classic_band.2 as i64).abs() <= 2,
"band {index} left: route={route_band:?} classic={classic_band:?}"
);
if index != last {
// Every line but the last is justified to the measure in both
// engines. The final line keeps its natural width and therefore
// reflects where TeX chose its last break, which the simplified
// demerits model does not yet reproduce exactly.
assert!(
(route_band.3 as i64 - classic_band.3 as i64).abs() <= 3,
"band {index} right: route={route_band:?} classic={classic_band:?}"
);
}
}
let _ = std::fs::remove_dir_all(&root);
}