use std::path::Path;
use std::process::Command;
fn unique_suffix() -> usize {
static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
pub struct Oracle {
pub program: String,
pub version: String,
}
pub const EPOCH: &str = "0";
pub fn oracle() -> Option<Oracle> {
let out = Command::new("luatex").arg("--version").output().ok()?;
if !out.status.success() {
return None;
}
let text = String::from_utf8_lossy(&out.stdout).into_owned();
let version = text
.lines()
.next()
.unwrap_or_default()
.split_whitespace()
.nth(4)
.unwrap_or("unknown")
.trim_end_matches(',')
.to_string();
Some(Oracle {
program: "luatex".to_string(),
version,
})
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Rung {
None,
Produced,
Pages,
PageSize,
Text,
Lines,
Fonts,
Bytes,
}
impl Rung {
pub fn name(self) -> &'static str {
match self {
Rung::None => "NONE",
Rung::Produced => "PRODUCED",
Rung::Pages => "PAGES",
Rung::PageSize => "PAGESIZE",
Rung::Text => "TEXT",
Rung::Lines => "LINES",
Rung::Fonts => "FONTS",
Rung::Bytes => "BYTES",
}
}
pub fn parse(s: &str) -> Option<Rung> {
Some(match s {
"NONE" => Rung::None,
"PRODUCED" => Rung::Produced,
"PAGES" => Rung::Pages,
"PAGESIZE" => Rung::PageSize,
"TEXT" => Rung::Text,
"LINES" => Rung::Lines,
"FONTS" => Rung::Fonts,
"BYTES" => Rung::Bytes,
_ => return None,
})
}
}
fn build(case: &Path, engine: &str, program: &str) -> Option<Vec<u8>> {
let dir = std::env::temp_dir().join(format!(
"texrs-pdfparity-{}-{}-{}-{}",
std::process::id(),
unique_suffix(),
engine,
case.file_stem()?.to_string_lossy()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).ok()?;
let src = dir.join("case.tex");
std::fs::copy(case, &src).ok()?;
let mut cmd = Command::new(program);
match engine {
"texrs" => {
cmd.arg("--pdf").arg("case.tex");
}
_ => {
cmd.args(["-interaction=nonstopmode", "case.tex"]);
}
}
let ok = cmd
.env("SOURCE_DATE_EPOCH", EPOCH)
.env("max_print_line", "8000")
.current_dir(&dir)
.output()
.ok()
.is_some();
let pdf = match ok {
true => std::fs::read(dir.join("case.pdf")).ok(),
false => None,
};
let _ = std::fs::remove_dir_all(&dir);
pdf
}
pub fn reference(oracle: &Oracle, case: &Path) -> Option<Vec<u8>> {
build(case, "luatex", &oracle.program)
}
pub fn subject(case: &Path) -> Option<Vec<u8>> {
let src = std::fs::read_to_string(case).ok()?;
let pdf = crate::run_pdf_at(case.parent(), &src).ok()?;
(crate::pdf_page_count(&pdf) > 0).then_some(pdf)
}
pub fn shape(pdf: &[u8]) -> Option<(usize, String)> {
let out = run_tool("pdfinfo", pdf)?;
let mut pages = None;
let mut size = String::new();
for line in out.lines() {
if let Some(v) = line.strip_prefix("Pages:") {
pages = v.trim().parse::<usize>().ok();
}
if let Some(v) = line.strip_prefix("Page size:") {
size = v.trim().to_string();
}
}
Some((pages?, size))
}
pub fn words(pdf: &[u8]) -> Option<Vec<String>> {
let out = run_tool("pdftotext", pdf)?;
Some(
out.split_whitespace()
.map(|w| {
w.chars()
.filter(|c| c.is_alphanumeric())
.collect::<String>()
})
.filter(|w| !w.is_empty())
.collect(),
)
}
pub fn lines(pdf: &[u8]) -> Option<Vec<String>> {
let out = run_tool("pdftotext-layout", pdf)?;
Some(
out.lines()
.map(|l| l.split_whitespace().collect::<Vec<_>>().join(" "))
.filter(|l| !l.is_empty())
.collect(),
)
}
pub fn fonts(pdf: &[u8]) -> Option<Vec<String>> {
let out = run_tool("pdffonts", pdf)?;
Some(
out.lines()
.skip(2)
.filter(|l| !l.trim().is_empty())
.map(|l| {
let f: Vec<&str> = l.split_whitespace().collect();
let name = f.first().copied().unwrap_or("");
let face = name.split_once('+').map(|(_, r)| r).unwrap_or(name);
format!("{face} {}", f.get(1).copied().unwrap_or(""))
})
.collect(),
)
}
fn run_tool(tool: &str, pdf: &[u8]) -> Option<String> {
let path = std::env::temp_dir().join(format!(
"texrs-pdfparity-{}-{}-{}.pdf",
std::process::id(),
unique_suffix(),
tool
));
std::fs::write(&path, pdf).ok()?;
let mut cmd = Command::new(tool);
match tool {
"pdftotext" => {
cmd.arg("-q").arg(&path).arg("-");
}
"pdftotext-layout" => {
cmd = Command::new("pdftotext");
cmd.arg("-q").arg("-layout").arg(&path).arg("-");
}
_ => {
cmd.arg(&path);
}
}
let out = cmd.output().ok();
let _ = std::fs::remove_file(&path);
let out = out?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
pub fn drawn(pdf: &[u8]) -> Vec<u8> {
let mut out = strip_dynamic(pdf);
out = inflate_streams(&out);
out
}
fn strip_dynamic(pdf: &[u8]) -> Vec<u8> {
let mut out = pdf.to_vec();
for key in [
&b"/CreationDate"[..],
&b"/ModDate"[..],
&b"/Producer"[..],
&b"/Creator"[..],
] {
out = blank_between(&out, key, b'(', b')');
}
out = blank_between(&out, b"/ID", b'[', b']');
out
}
fn blank_between(pdf: &[u8], key: &[u8], open: u8, close: u8) -> Vec<u8> {
let mut out = Vec::with_capacity(pdf.len());
let mut i = 0;
while i < pdf.len() {
if pdf[i..].starts_with(key) {
let after = i + key.len();
let start = pdf[after..]
.iter()
.position(|b| !b.is_ascii_whitespace())
.map(|at| after + at);
match start {
Some(at) if pdf.get(at) == Some(&open) => {
let end = pdf[at..].iter().position(|b| *b == close).map(|e| at + e);
if let Some(end) = end {
out.extend_from_slice(key);
out.push(open);
out.push(close);
i = end + 1;
continue;
}
}
_ => {}
}
}
out.push(pdf[i]);
i += 1;
}
out
}
fn inflate_streams(pdf: &[u8]) -> Vec<u8> {
crate::pdf::inflate_streams(pdf)
}
pub fn verdict(reference: Option<&Vec<u8>>, subject: Option<&Vec<u8>>) -> (Rung, String) {
let (r, s) = match (reference, subject) {
(None, None) => {
return (
Rung::Bytes,
"neither engine wrote a PDF: no pages of output".to_string(),
)
}
(Some(_), None) => return (Rung::None, "texrs wrote no PDF, luatex did".to_string()),
(None, Some(s)) => {
return (
Rung::None,
format!("texrs wrote a {}-byte PDF, luatex wrote none", s.len()),
)
}
(Some(r), Some(s)) => (r, s),
};
if drawn(r) == drawn(s) {
return (Rung::Bytes, String::new());
}
let (Some((rp, rm)), Some((sp, sm))) = (shape(r), shape(s)) else {
return (Rung::Produced, "pdfinfo not installed".to_string());
};
if rp != sp {
return (Rung::Produced, format!("pages: luatex {rp}, texrs {sp}"));
}
if rm != sm {
return (Rung::Pages, format!("page size: luatex {rm}, texrs {sm}"));
}
let (Some(rw), Some(sw)) = (words(r), words(s)) else {
return (Rung::PageSize, "pdftotext not installed".to_string());
};
if rw != sw {
let at = rw
.iter()
.zip(sw.iter())
.position(|(a, b)| a != b)
.unwrap_or(rw.len().min(sw.len()));
return (
Rung::PageSize,
format!(
"words: {} vs {}, first difference at {at} ({:?} vs {:?})",
rw.len(),
sw.len(),
rw.get(at),
sw.get(at)
),
);
}
let (Some(rl), Some(sl)) = (lines(r), lines(s)) else {
return (Rung::Text, "pdftotext -layout unavailable".to_string());
};
if rl != sl {
let at = rl
.iter()
.zip(sl.iter())
.position(|(a, b)| a != b)
.unwrap_or(rl.len().min(sl.len()));
return (
Rung::Text,
format!(
"lines: {} vs {}, first difference at {at} ({:?} vs {:?})",
rl.len(),
sl.len(),
rl.get(at),
sl.get(at)
),
);
}
let (Some(rf), Some(sf)) = (fonts(r), fonts(s)) else {
return (Rung::Lines, "pdffonts unavailable".to_string());
};
if rf != sf {
return (Rung::Lines, format!("fonts: luatex {rf:?}, texrs {sf:?}"));
}
(
Rung::Fonts,
format!("bytes: luatex {}, texrs {}", r.len(), s.len()),
)
}