Compare route-D page breaks with pdfLaTeX

This commit is contained in:
gloamxun committed 2026-10-23 03:40:08 +08:00
1 parent c5b3493998
commit 6c3636c7c5
3 files changed
+198 -3

No files matched your search

+193 -1
View File
@@ -10,7 +10,7 @@ use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
use texrra_tex_core::paragraph::{HorizontalItem, ParagraphStyle};
use texrra_tex_core::{Unit, VerticalItem};
use texrra_tex_core::{Document, Engine, Unit, VerticalItem};
/// Scaled points per TeX point.
const PT: i64 = 65_536;
@@ -212,6 +212,95 @@ fn route_d_lines_with(
lines
}
/// Keep only characters that survive both extractors: pdfTeX splits words at
/// line ends with a hyphen, which this strips as well.
fn alnum(text: &str) -> String {
text.chars()
.filter(|character| character.is_ascii_alphanumeric())
.collect()
}
/// Compile `source` with pdfLaTeX and return the alphanumeric text of each page.
fn pdftex_pages(source: &str) -> Option<Vec<String>> {
let dir = temp_dir("pdftex-pages");
fs::write(dir.join("main.tex"), source).expect("write fixture");
let latex = Command::new("pdflatex")
.args(["-interaction=nonstopmode", "-halt-on-error", "main.tex"])
.current_dir(&dir)
.output();
let latex = match latex {
Ok(output) => output,
Err(error) if tool_missing(&error) => return None,
Err(error) => panic!("run pdflatex: {error}"),
};
assert!(
latex.status.success(),
"pdflatex failed: {}",
String::from_utf8_lossy(&latex.stdout)
);
let text = Command::new("pdftotext")
.arg(dir.join("main.pdf"))
.arg("-")
.output();
let text = match text {
Ok(output) => output,
Err(error) if tool_missing(&error) => return None,
Err(error) => panic!("run pdftotext: {error}"),
};
let text = String::from_utf8_lossy(&text.stdout);
Some(
text.split('\u{c}')
.map(alnum)
.filter(|page| !page.is_empty())
.collect(),
)
}
fn page_alnum_text(page: &texrra_tex_core::Page) -> String {
let mut text = String::new();
for item in &page.items {
let VerticalItem::Paragraph(paragraph) = item else {
continue;
};
for line in &paragraph.lines {
for item in line {
if let HorizontalItem::Box(box_) = item
&& let Some(character) = box_.character
&& character.is_ascii_alphanumeric()
{
text.push(character);
}
}
}
}
text
}
/// Lay `paragraphs` out with the route-D scrolling builder using article
/// metrics that match the pdfTeX fixtures (`\hsize` 345pt, `\topskip` 12pt,
/// `\vsize` 550pt, `\parskip` 0).
fn route_pages_text(paragraphs: &[String], par_skip: i64) -> Vec<String> {
let mut units = Vec::new();
for (index, text) in paragraphs.iter().enumerate() {
let style = ParagraphStyle {
line_width: 345 * PT,
par_indent: 15 * PT,
line_height: 12 * PT,
..ParagraphStyle::default()
};
units.push(Unit::paragraph_with_style(index as u64 + 1, text, style));
}
let mut document = Document::new(550 * PT, units);
document.top_skip = 12 * PT;
document.par_skip = par_skip * PT;
document.text_float_sep = 0;
Engine::layout(&document)
.pages()
.iter()
.map(page_alnum_text)
.collect()
}
fn assert_lines_match(label: &str, ours: &[Line], reference: &[Line]) {
assert_eq!(
ours.len(),
@@ -390,6 +479,109 @@ fn explicit_sfcode_matches_pdftex() {
});
}
#[test]
fn pages_break_like_pdftex_for_single_line_paragraphs() {
let paragraphs: Vec<String> = (0..100)
.map(|index| format!("p{index} alpha beta gamma"))
.collect();
let body: String = paragraphs
.iter()
.map(|paragraph| format!("{paragraph}\\par\n"))
.collect();
let source = format!(
"\\documentclass[10pt]{{article}}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{{empty}}\n\
\\begin{{document}}\n\
\\hsize=345pt \\parindent=15pt \\parskip=0pt \\topskip=12pt \\vsize=550pt\n\
{body}\\end{{document}}\n"
);
let Some(reference) = pdftex_pages(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_pages_text(&paragraphs, 0);
assert_eq!(
ours.len(),
reference.len(),
"route-D produced {} pages, pdfLaTeX {}",
ours.len(),
reference.len()
);
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
assert_eq!(ours, reference, "text on page {}", index + 1);
}
}
#[test]
fn pages_break_like_pdftex_with_paragraph_spacing() {
let paragraphs: Vec<String> = (0..90)
.map(|index| format!("p{index} alpha beta gamma"))
.collect();
let body: String = paragraphs
.iter()
.map(|paragraph| format!("{paragraph}\\par\n"))
.collect();
let source = format!(
"\\documentclass[10pt]{{article}}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{{empty}}\n\
\\begin{{document}}\n\
\\hsize=345pt \\parindent=15pt \\parskip=6pt \\topskip=12pt \\vsize=550pt\n\
{body}\\end{{document}}\n"
);
let Some(reference) = pdftex_pages(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_pages_text(&paragraphs, 6);
assert_eq!(
ours.len(),
reference.len(),
"route-D produced {} pages, pdfLaTeX {}",
ours.len(),
reference.len()
);
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
assert_eq!(ours, reference, "text on page {}", index + 1);
}
}
#[test]
fn pages_split_paragraphs_like_pdftex() {
let sentence = "alpha beta gamma delta epsilon zeta eta theta iota kappa. ";
let paragraphs: Vec<String> = (0..20)
.map(|index| format!("p{index} {}", sentence.repeat(3)))
.collect();
let body: String = paragraphs
.iter()
.map(|paragraph| format!("{paragraph}\\par\n"))
.collect();
let source = format!(
"\\documentclass[10pt]{{article}}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{{empty}}\n\
\\begin{{document}}\n\
\\hsize=345pt \\parindent=15pt \\parskip=0pt \\topskip=12pt \\vsize=550pt\n\
{body}\\end{{document}}\n"
);
let Some(reference) = pdftex_pages(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_pages_text(&paragraphs, 0);
assert_eq!(
ours.len(),
reference.len(),
"route-D produced {} pages, pdfLaTeX {}",
ours.len(),
reference.len()
);
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
assert_eq!(ours, reference, "text on page {}", index + 1);
}
}
#[test]
fn paragraph_breaks_match_pdftex_at_150pt() {
compare_fixture(WORDS, 150);