Compare two route-D pages with pdfTeX

This commit is contained in:
gloamxun committed 2026-10-21 08:40:44 +08:00
1 parent 180c05e425
commit 68d2cef346
3 files changed
+166 -11

No files matched your search

+164
View File
@@ -202,3 +202,167 @@ Hello route-D
);
let _ = std::fs::remove_dir_all(&root);
}
/// Compare two route-D pages against a two-page pdfTeX document. This extends
/// the single-page geometry check to page boundaries and repeated page
/// furniture: each page should contain one line at the same vertical band.
#[test]
fn route_d_two_pages_match_pdftex_raster_geometry() {
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 ink_bbox(path: &std::path::Path) -> (usize, usize, 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..];
let (mut x0, mut y0, mut x1, mut y1, mut rows) =
(usize::MAX, usize::MAX, 0usize, 0usize, 0usize);
for y in 0..height {
let mut row_has_ink = false;
for x in 0..width {
if pixels[y * width + x] < 160 {
row_has_ink = true;
x0 = x0.min(x);
y0 = y0.min(y);
x1 = x1.max(x);
y1 = y1.max(y);
}
}
if row_has_ink {
rows += 1;
}
}
(x0, y0, x1, y1, rows, width)
}
let root = std::env::temp_dir().join(format!("texrra-raster-two-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("create raster dir");
let style = ParagraphStyle {
line_width: 480 * 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(
20 * 65_536,
vec![
Unit::paragraph_with_style(1, "First page text", style.clone()),
Unit::paragraph_with_style(2, "Second page text", style.clone()),
],
);
let engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2, "expected two route-D pages");
let pages: Vec<_> = engine
.pages()
.iter()
.map(|page| page.display_list())
.collect();
let route_pdf_path = root.join("route.pdf");
std::fs::write(&route_pdf_path, display_lists_to_pdf(&pages, 792)).expect("write route PDF");
let classic = r"\documentclass[10pt]{article}
\pagestyle{empty}
\hoffset=-1in
\voffset=-1in
\oddsidemargin=0pt
\topmargin=0pt
\headheight=0pt
\headsep=0pt
\pdfpagewidth=612bp
\pdfpageheight=792bp
\textwidth=480pt
\textheight=15pt
\topskip=10pt
\baselineskip=12pt
\parindent=0pt
\parskip=0pt
\begin{document}
\vsize=15pt
First page text\par
Second page text\par
\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, prefix) in [
(route_pdf_path.clone(), "route"),
(root.join("classic.pdf"), "classic"),
] {
let status = Command::new("pdftoppm")
.args(["-r", "72", "-gray"])
.arg(&pdf)
.arg(root.join(prefix))
.status()
.expect("pdftoppm");
assert!(status.success(), "pdftoppm failed for {}", pdf.display());
}
for page in 1..=2 {
let route = ink_bbox(&root.join(format!("route-{page}.pgm")));
let classic = ink_bbox(&root.join(format!("classic-{page}.pgm")));
assert_eq!(
route.4, classic.4,
"page {page} text line count: route={route:?} classic={classic:?}"
);
assert!(
(route.2 as i64 - classic.2 as i64).abs() <= 2,
"page {page} ink left edge: route={route:?} classic={classic:?}"
);
assert!(
route.3 <= classic.3 + 2 && route.3 + 2 >= classic.3,
"page {page} ink bottom edge: route={route:?} classic={classic:?}"
);
}
let _ = std::fs::remove_dir_all(&root);
}
+1 -1
View File
@@ -269,7 +269,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
The crate currently has 154 unit tests plus two route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 251 tests (154 route-D unit, 2 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 1 doctest). v1 remains the live server path.
The crate currently has 154 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 252 tests (154 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo
+1 -10
View File
@@ -51,7 +51,7 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
.lock()
.map_err(|_| "route-D session lock".to_string())?;
let previous = sessions.remove(project_id);
let (mut engine, changed, recomputed) = match previous {
let (engine, changed, recomputed) = match previous {
Some(session)
if session.paragraphs.len() == texts.len()
&& session
@@ -105,15 +105,6 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
Ok(json)
}
/// Build a JSON preview from the document body of a small LaTeX file.
///
/// This is the opt-in M7 bridge for v1 projects: the preamble is discarded and
/// the body is handed to the same route-D paragraph parser used by
/// `/api/route-d/preview`. The v1 semantic pipeline remains the default.
pub fn latex_preview_json(source: &str) -> Result<String, String> {
preview_json(latex_body(source))
}
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
pub fn preview_json(source: &str) -> Result<String, String> {
let paragraphs = paragraphs(source)?;