Compare route-D text extraction with pdfTeX

This commit is contained in:
gloamxun committed 2026-10-21 09:31:18 +08:00
1 parent c6a58dd646
commit 763ae99e8e
2 files changed
+76 -1

No files matched your search

+75
View File
@@ -410,3 +410,78 @@ fn accented_text_round_trips_through_pdf_extraction() {
"got {text:?}"
);
}
#[test]
fn route_d_text_matches_pdftex_extraction() {
use std::process::Command;
fn available(command: &str, arg: &str) -> bool {
Command::new(command)
.arg(arg)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok_and(|status| status.success())
}
if !available("pdflatex", "--version") || !available("pdftotext", "-v") {
return;
}
fn extract(pdf: &std::path::Path) -> String {
let output = Command::new("pdftotext")
.arg(pdf)
.arg("-")
.output()
.expect("pdftotext");
String::from_utf8_lossy(&output.stdout)
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
let body =
"The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs.";
let document = Document::new(
61,
vec![Unit::paragraph_with_style(
1,
body,
ParagraphStyle::default(),
)],
);
let engine = Engine::layout(&document);
let lists: Vec<_> = engine
.pages()
.iter()
.map(|page| page.display_list())
.collect();
let route_pdf = std::env::temp_dir().join(format!("texrra-words-{}.pdf", std::process::id()));
std::fs::write(&route_pdf, display_lists_to_pdf(&lists, 792)).expect("write route PDF");
let dir = std::env::temp_dir().join(format!("texrra-pdftex-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("temp dir");
let tex = dir.join("control.tex");
let source = [
"\\documentclass[10pt]{article}",
"\\pagestyle{empty}",
"\\begin{document}",
"\\noindent ",
body,
"\\end{document}",
]
.concat();
std::fs::write(&tex, source).expect("write tex");
let status = Command::new("pdflatex")
.current_dir(&dir)
.args(["-interaction=nonstopmode", "control.tex"])
.status()
.expect("pdflatex");
assert!(status.success(), "pdflatex failed");
let route = extract(&route_pdf);
let control = extract(&dir.join("control.pdf"));
let _ = std::fs::remove_file(&route_pdf);
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(route, control);
}