Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_ink.rs

6.4 KiB, 3 runs

created by r1870400018:63592, 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//! Ink against the `typst` 0.15.1 oracle (U6b-S2): what a page draws, compared on the rendered file. Each
2//! fixture under `tests/fixtures/ink/`, outside the evaluator corpus whose level 4 reads text lines only, is compiled to a PDF by Austenite's pipeline and by Typst, both are
3//! rasterised by `pdftoppm`, and the two pictures are compared: the box the ink spans must agree, and few
4//! pixels may differ. Text under a transform, a clip and a stroke are geometry that the line reader of level 4
5//! cannot see.
6
7#![allow(dead_code)]
8
9#[path = "eval_oracle/mod.rs"]
10mod harness;
11
12use harness::oracle::Oracle;
13use harness::pdf::{
14 austenite_pdf,
15 tool,
16 typst_pdf,
17 work_dir,
18};
19
20use oxedyne_fe2o3_core::prelude::*;
21
22use std::path::{
23 Path,
24 PathBuf,
25};
26
27const DPI: &str = "100";
28const INK: u8 = 200; // a pixel darker than this is ink
29const DIFF: i32 = 100; // grey levels two pixels must differ by to count
30const SLACK_PX: i64 = 2; // the ink box may move this many pixels
31const FRACTION: f64 = 0.04; // of Typst's ink pixels that may differ
32const MIN_COMPARED: usize = 7;
33
34/// A grey picture, a row at a time.
35struct Grey {
36 w: usize,
37 h: usize,
38 data: Vec<u8>,
39}
40
41/// Reads the binary PGM `pdftoppm -gray` writes.
42fn read_pgm(path: &Path) -> Outcome<Grey> {
43 let bytes = res!(std::fs::read(path));
44 let mut at = 0usize;
45 let mut tokens: Vec<String> = Vec::new();
46 while tokens.len() < 4 && at < bytes.len() {
47 while at < bytes.len() && bytes[at].is_ascii_whitespace() {
48 at += 1;
49 }
50 if at < bytes.len() && bytes[at] == b'#' {
51 while at < bytes.len() && bytes[at] != b'\n' {
52 at += 1;
53 }
54 continue;
55 }
56 let from = at;
57 while at < bytes.len() && !bytes[at].is_ascii_whitespace() {
58 at += 1;
59 }
60 tokens.push(String::from_utf8_lossy(&bytes[from..at]).to_string());
61 }
62 at += 1; // the one whitespace byte after the maximum value
63 if tokens.len() < 4 || tokens[0] != "P5" {
64 return Err(err!("{} is not a binary PGM", path.display(); Test));
65 }
66 let w: usize = res!(tokens[1].parse::<usize>().map_err(|e| err!("PGM width: {}", e; Test)));
67 let h: usize = res!(tokens[2].parse::<usize>().map_err(|e| err!("PGM height: {}", e; Test)));
68 if bytes.len() < at + w * h {
69 return Err(err!("{} holds {} pixel bytes, {} expected", path.display(), bytes.len() - at, w * h; Test));
70 }
71 Ok(Grey { w, h, data: bytes[at..at + w * h].to_vec() })
72}
73
74impl Grey {
75 /// The box the ink spans, left, top, right, bottom, and how many pixels are ink.
76 fn ink(&self) -> (i64, i64, i64, i64, usize) {
77 let (mut x0, mut y0, mut x1, mut y1, mut n) = (i64::MAX, i64::MAX, -1i64, -1i64, 0usize);
78 for y in 0..self.h {
79 for x in 0..self.w {
80 if self.data[y * self.w + x] < INK {
81 x0 = x0.min(x as i64);
82 y0 = y0.min(y as i64);
83 x1 = x1.max(x as i64);
84 y1 = y1.max(y as i64);
85 n += 1;
86 }
87 }
88 }
89 (x0, y0, x1, y1, n)
90 }
91}
92
93/// The pages of `pdf` as pictures, in order.
94fn pages(pdf: &Path, dir: &Path, tag: &str) -> Outcome<Vec<Grey>> {
95 let prefix = dir.join(tag);
96 for entry in res!(std::fs::read_dir(dir)) {
97 let p = res!(entry).path();
98 let name = p.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
99 if name.starts_with(&fmt!("{}-", tag)) && name.ends_with(".pgm") {
100 res!(std::fs::remove_file(&p));
101 }
102 }
103 let prefix_s = prefix.display().to_string();
104 res!(tool("pdftoppm", &["-r", DPI, "-gray"], pdf, &[&prefix_s]));
105 let mut files: Vec<std::path::PathBuf> = Vec::new();
106 for entry in res!(std::fs::read_dir(dir)) {
107 let p = res!(entry).path();
108 let name = p.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
109 if name.starts_with(&fmt!("{}-", tag)) && name.ends_with(".pgm") {
110 files.push(p);
111 }
112 }
113 files.sort();
114 let mut out = Vec::new();
115 for f in files {
116 out.push(res!(read_pgm(&f)));
117 }
118 Ok(out)
119}
120
121/// Where Austenite's pictures differ from Typst's.
122fn differences(want: &[Grey], got: &[Grey]) -> Vec<String> {
123 let mut out = Vec::new();
124 if want.len() != got.len() {
125 out.push(fmt!("page count: typst {}, austenite {}", want.len(), got.len()));
126 }
127 for (i, (w, g)) in want.iter().zip(got.iter()).enumerate() {
128 if (w.w, w.h) != (g.w, g.h) {
129 out.push(fmt!("page {}: size typst {}x{}, austenite {}x{}", i + 1, w.w, w.h, g.w, g.h));
130 continue;
131 }
132 let (a, b) = (w.ink(), g.ink());
133 let moved = [a.0 - b.0, a.1 - b.1, a.2 - b.2, a.3 - b.3].iter().any(|d| d.abs() > SLACK_PX);
134 if moved {
135 out.push(fmt!("page {}: ink box typst {:?}, austenite {:?}", i + 1, (a.0, a.1, a.2, a.3), (b.0, b.1, b.2, b.3)));
136 }
137 let differ = w.data.iter().zip(g.data.iter()).filter(|(p, q)| (**p as i32 - **q as i32).abs() > DIFF).count();
138 let allowed = ((a.4 as f64) * FRACTION).max(40.0) as usize;
139 if differ > allowed {
140 out.push(fmt!("page {}: {} pixel(s) differ (typst has {} ink, {} are allowed)", i + 1, differ, a.4, allowed));
141 }
142 }
143 out
144}
145
146#[test]
147fn ink_matches_the_typst_oracle_as_rendered() -> Outcome<()> {
148 if res!(Oracle::find()).is_none() {
149 return Ok(());
150 }
151 let only = std::env::var("EVAL_INK_FIXTURE").ok().filter(|s| !s.trim().is_empty());
152 let root = res!(work_dir("eval-ink"));
153 let (mut compared, mut failures) = (0usize, Vec::new());
154 let mut files: Vec<PathBuf> = Vec::new();
155 let base = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/ink");
156 for entry in res!(std::fs::read_dir(&base)) {
157 let p = res!(entry).path();
158 if p.extension().map(|e| e == "typ").unwrap_or(false) {
159 files.push(p);
160 }
161 }
162 files.sort();
163 for path in files {
164 let name = path.file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
165 if let Some(o) = &only {
166 if !name.contains(o.as_str()) {
167 continue;
168 }
169 }
170 let dir = root.join(&name);
171 res!(std::fs::create_dir_all(&dir));
172 let ours = dir.join("austenite.pdf");
173 let theirs = dir.join("typst.pdf");
174 let made = match austenite_pdf(&path) {
175 Ok(m) => m,
176 Err(e) => {
177 failures.push(fmt!("{}: {}", name, e));
178 continue;
179 },
180 };
181 res!(std::fs::write(&ours, &made.bytes));
182 res!(typst_pdf(&path, &theirs));
183 let want = res!(pages(&theirs, &dir, "typst"));
184 let got = res!(pages(&ours, &dir, "aus"));
185 let d = differences(&want, &got);
186 compared += 1;
187 if !d.is_empty() {
188 failures.push(fmt!("{}: {}", name, d.join(" | ")));
189 }
190 }
191 println!("[ink] {} fixture(s) compared, {} failure(s)", compared, failures.len());
192 assert!(failures.is_empty(), "{} failure(s):\n{}", failures.len(), failures.join("\n"));
193 if only.is_none() {
194 assert!(compared >= MIN_COMPARED, "only {} fixture(s) compared; {} are expected", compared, MIN_COMPARED);
195 }
196 Ok(())
197}