oxedyne/fe2o3/fe2o3_austenite/tests/eval_oracle/pdf.rs
8.4 KiB, 13 runs
created by r1870400018:63626, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // Austenite's own PDF against Typst's (level 4b, the U12b comparator). A source is compiled through the |
| 2 | // evaluator's pipeline with the streaming `PdfSink`, and the file written is read back by the same |
| 3 | // `pdftotext -bbox-layout` that reads Typst's, so the face-dependent offset of a box from its baseline is |
| 4 | // common to both and only the document's own layout is compared. |
| 5 | |
| 6 | #![allow(dead_code)] |
| 7 | |
| 8 | use crate::harness::layout::{ |
| 9 | read_bbox, |
| 10 | OLine, |
| 11 | OPage, |
| 12 | }; |
| 13 | use crate::harness::oracle::work_dir as oracle_dir; |
| 14 | use crate::harness::typst_command::typst_command; |
| 15 | |
| 16 | use oxedyne_fe2o3_austenite::compile; |
| 17 | use oxedyne_fe2o3_austenite::emit::sinks::PdfSink; |
| 18 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 19 | |
| 20 | use oxedyne_fe2o3_core::prelude::*; |
| 21 | |
| 22 | use std::path::{ |
| 23 | Path, |
| 24 | PathBuf, |
| 25 | }; |
| 26 | use std::process::Command; |
| 27 | |
| 28 | pub const TYPST: &str = "/home/jason/bin/typst"; |
| 29 | pub const TOL: f64 = 1.0; // points: level 4b's tolerance |
| 30 | |
| 31 | /// A scratch directory of the test's own, under the target directory, never `/tmp`. |
| 32 | pub fn work_dir(name: &str) -> Outcome<PathBuf> { |
| 33 | // Under the test target's own scratch directory, so two worktrees testing at once never share it. |
| 34 | let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("austenite-qc").join(name); |
| 35 | res!(std::fs::create_dir_all(&dir)); |
| 36 | Ok(dir) |
| 37 | } |
| 38 | |
| 39 | /// What a compile through the pipeline left: the PDF's bytes, the report, and the page count. |
| 40 | pub struct Made { |
| 41 | pub bytes: Vec<u8>, |
| 42 | pub report: compile::Report, |
| 43 | pub pages: u32, |
| 44 | } |
| 45 | |
| 46 | /// Compiles `src` (its directory the root) to a PDF through `compile::assemble_eval` with a `PdfSink`. |
| 47 | pub fn austenite_pdf(src: &Path) -> Outcome<Made> { |
| 48 | austenite_pdf_with(src, FontStore::default()) |
| 49 | } |
| 50 | |
| 51 | /// As [`austenite_pdf`], with the faces `fonts` holds beside the embedded ones. |
| 52 | pub fn austenite_pdf_with(src: &Path, fonts: FontStore) -> Outcome<Made> { |
| 53 | let mut sink = res!(PdfSink::new()); |
| 54 | let root = src.parent().map(|p| p.to_path_buf()).unwrap_or_default(); |
| 55 | let done = res!(compile::assemble_eval(src, &root, fonts, &mut sink)); |
| 56 | let pages = res!(done.laid.as_ref().map_err(|e| err!("{} did not lay out: {}", src.display(), e.plain(); Test))).pages; |
| 57 | let report = done.report(); |
| 58 | let out = res!(sink.output().ok_or_else(|| err!("The fixpoint finished with no PDF."; Test))); |
| 59 | Ok(Made { bytes: out.to_vec(), report, pages }) |
| 60 | } |
| 61 | |
| 62 | /// The command that compiles `src` to `out`, under the oracle's cap and with the account's fonts hidden. |
| 63 | pub fn typst_pdf_command(src: &Path, out: &Path, extra: &[&str]) -> Outcome<Command> { |
| 64 | let work = res!(oracle_dir()); |
| 65 | let mut c = typst_command(TYPST, Some("3G"), &work); |
| 66 | c.arg("compile").args(extra).arg(src).arg(out); |
| 67 | Ok(c) |
| 68 | } |
| 69 | |
| 70 | /// Typst's PDF of `src`, under the same memory cap as every oracle run. |
| 71 | pub fn typst_pdf(src: &Path, out: &Path) -> Outcome<()> { |
| 72 | typst_pdf_with(src, out, &[]) |
| 73 | } |
| 74 | |
| 75 | /// As [`typst_pdf`], with `extra` arguments to `typst compile` (a `--font-path`, say). |
| 76 | pub fn typst_pdf_with(src: &Path, out: &Path, extra: &[&str]) -> Outcome<()> { |
| 77 | let status = res!(typst_pdf_command(src, out, extra)).status(); |
| 78 | match status { |
| 79 | Ok(s) if s.success() => Ok(()), |
| 80 | other => Err(err!("typst did not compile {}: {:?}", src.display(), other; Test)), |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn tool(name: &str, args: &[&str], file: &Path, tail: &[&str]) -> Outcome<String> { |
| 85 | let out = Command::new(name).args(args).arg(file).args(tail).output(); |
| 86 | match out { |
| 87 | Ok(o) if o.status.success() => Ok(String::from_utf8_lossy(&o.stdout).to_string()), |
| 88 | other => Err(err!("{} failed on {}: {:?}", name, file.display(), other.map(|o| o.status); Test)), |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// The PDF's text, by `pdftotext -layout`, whitespace squashed. |
| 93 | pub fn text_of(pdf: &Path) -> Outcome<String> { |
| 94 | let t = res!(tool("pdftotext", &["-layout"], pdf, &["-"])); |
| 95 | Ok(t.split_whitespace().collect::<Vec<_>>().join(" ")) |
| 96 | } |
| 97 | |
| 98 | /// The PDF's pages and lines, by `pdftotext -bbox-layout`. |
| 99 | pub fn lines_of(pdf: &Path) -> Outcome<Vec<OPage>> { |
| 100 | let xml = res!(tool("pdftotext", &["-bbox-layout"], pdf, &["-"])); |
| 101 | Ok(read_bbox(&xml)) |
| 102 | } |
| 103 | |
| 104 | /// The page size `pdfinfo` reads from a PDF, in points: Typst packs its page objects into compressed streams, |
| 105 | /// so its MediaBox is not in the bytes. |
| 106 | pub fn page_size(pdf: &Path) -> Outcome<(f64, f64)> { |
| 107 | let info = res!(tool("pdfinfo", &[], pdf, &[])); |
| 108 | let line = res!(info.lines().find(|l| l.starts_with("Page size:")).ok_or_else(|| err!("pdfinfo gave no page size"; Test))); |
| 109 | let nums: Vec<f64> = line.split_whitespace().filter_map(|n| n.parse::<f64>().ok()).collect(); |
| 110 | if nums.len() < 2 { |
| 111 | return Err(err!("pdfinfo's page size line has no two numbers: {}", line; Test)); |
| 112 | } |
| 113 | Ok((nums[0], nums[1])) |
| 114 | } |
| 115 | |
| 116 | /// Where Austenite's lines differ from Typst's: a line's text, the bottom of its box and where its first |
| 117 | /// word starts, each within [`TOL`]. |
| 118 | pub fn k3_differences(want: &[OPage], got: &[OPage]) -> Vec<String> { |
| 119 | k3_differences_by(want, got, false) |
| 120 | } |
| 121 | |
| 122 | /// As [`k3_differences`]; with `spaceless`, a line's text is compared without its spaces, for a reader of |
| 123 | /// maths whose word breaks fall on glyph gaps a hair apart (Typst writes a run as one `TJ`, Austenite a |
| 124 | /// glyph at a time). |
| 125 | pub fn k3_differences_by(want: &[OPage], got: &[OPage], spaceless: bool) -> Vec<String> { |
| 126 | let mut out = Vec::new(); |
| 127 | if want.len() != got.len() { |
| 128 | out.push(fmt!("page count: typst {}, austenite {}", want.len(), got.len())); |
| 129 | } |
| 130 | for (pi, (w, g)) in want.iter().zip(got.iter()).enumerate() { |
| 131 | if w.lines.len() != g.lines.len() { |
| 132 | out.push(fmt!("page {}: typst has {} line(s), austenite {}", pi + 1, w.lines.len(), g.lines.len())); |
| 133 | } |
| 134 | for (wl, gl) in w.lines.iter().zip(g.lines.iter()) { |
| 135 | let squash = |l: &OLine| l.text.split_whitespace().collect::<Vec<_>>().join(if spaceless { "" } else { " " }); |
| 136 | if squash(wl) != squash(gl) { |
| 137 | out.push(fmt!("page {}: line text typst {:?}, austenite {:?}", pi + 1, squash(wl), squash(gl))); |
| 138 | } |
| 139 | if (wl.y1 - gl.y1).abs() > TOL { |
| 140 | out.push(fmt!("page {}: {:?}: box bottom typst {:.2}, austenite {:.2}", pi + 1, squash(wl), wl.y1, gl.y1)); |
| 141 | } |
| 142 | if (wl.x0 - gl.x0).abs() > TOL { |
| 143 | out.push(fmt!("page {}: {:?}: starts at x typst {:.2}, austenite {:.2}", pi + 1, squash(wl), wl.x0, gl.x0)); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | out |
| 148 | } |
| 149 | |
| 150 | /// The fonts a PDF embeds, as `pdffonts` lists them, each name without its subset tag. |
| 151 | pub fn font_names(pdf: &Path) -> Outcome<Vec<String>> { |
| 152 | let list = res!(tool("pdffonts", &[], pdf, &[])); |
| 153 | let mut out = Vec::new(); |
| 154 | for line in list.lines().skip(2) { |
| 155 | if let Some(name) = line.split_whitespace().next() { |
| 156 | let bare = name.split_once('+').map(|(_, n)| n).unwrap_or(name); |
| 157 | if !out.iter().any(|o| o == bare) { |
| 158 | out.push(bare.to_string()); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | Ok(out) |
| 163 | } |
| 164 | |
| 165 | /// Compiles `src` both ways into `dir` and returns the level-4b differences of Austenite's own PDF from Typst's. |
| 166 | pub fn pdf_differences(src: &Path, dir: &Path) -> Outcome<Vec<String>> { |
| 167 | Ok(res!(pdf_compare(src, dir, None, false)).differences) |
| 168 | } |
| 169 | |
| 170 | /// What one comparison of the two PDFs found: the level-4b differences and the fonts each file embeds. |
| 171 | pub struct Compared { |
| 172 | pub differences: Vec<String>, |
| 173 | pub ours: Vec<String>, |
| 174 | pub theirs: Vec<String>, |
| 175 | pub report: compile::Report, |
| 176 | } |
| 177 | |
| 178 | /// Compiles `src` both ways into `dir`, the faces under `fonts` supplied to each as a host supplies them |
| 179 | /// (`--font-path`, with Typst's system fonts ignored so the two sides see the same faces). With `spaceless` |
| 180 | /// the text is compared without its spaces ([`k3_differences_by`]). |
| 181 | pub fn pdf_compare(src: &Path, dir: &Path, fonts: Option<&Path>, spaceless: bool) -> Outcome<Compared> { |
| 182 | res!(std::fs::create_dir_all(dir)); |
| 183 | let mut store = FontStore::default(); |
| 184 | let mut extra: Vec<String> = Vec::new(); |
| 185 | if let Some(f) = fonts { |
| 186 | store.add_dir(f.to_path_buf()); |
| 187 | extra.push("--ignore-system-fonts".to_string()); |
| 188 | extra.push("--font-path".to_string()); |
| 189 | extra.push(f.display().to_string()); |
| 190 | } |
| 191 | let made = res!(austenite_pdf_with(src, store)); |
| 192 | let ours = dir.join("austenite.pdf"); |
| 193 | let theirs = dir.join("typst.pdf"); |
| 194 | res!(std::fs::write(&ours, &made.bytes)); |
| 195 | let args: Vec<&str> = extra.iter().map(|a| a.as_str()).collect(); |
| 196 | res!(typst_pdf_with(src, &theirs, &args)); |
| 197 | let mut out = k3_differences_by(&res!(lines_of(&theirs)), &res!(lines_of(&ours)), spaceless); |
| 198 | let (a, t) = (res!(text_of(&ours)), res!(text_of(&theirs))); |
| 199 | let bare = |s: &String| if spaceless { s.split_whitespace().collect::<Vec<_>>().join("") } else { s.clone() }; |
| 200 | if bare(&a) != bare(&t) { |
| 201 | out.push(fmt!("pdftotext differs: typst {:?}, austenite {:?}", t, a)); |
| 202 | } |
| 203 | Ok(Compared { |
| 204 | differences: out, |
| 205 | ours: res!(font_names(&ours)), |
| 206 | theirs: res!(font_names(&theirs)), |
| 207 | report: made.report, |
| 208 | }) |
| 209 | } |