feat(domain): R4.4 layout to a draw log: text rules, AFM metrics, dates (O4, O5, O6) and the page layouts (O1)

pdf::write: text (safe_text with the frozen V8 classes, js_upper from the O4 tables,
js_trim, measure = Σ(WX || 250 + KPX) × size / 1000 over WinAnsi bytes, wrap_lines on JS
\s), afm_data generated by scripts/r4-afm-data.mjs from O5 and O4, dates (Date.UTC years
0-99 → 19xx, fr-FR / en-US long dates), input checks (branding and labels schemas,
snapshot gate with the R3 SVG reader, sheet mismatch, page caps), the header, footer,
observation and conclusion layouts into a DrawLog (per page, footers last, links by page).
cargo: O4 55,992 widths bit-exact, 1,168 wraps, safeText over every code point, upper
1,580, O6 6,468 cells + 22; O1 12 runs 2,124 ops equal at full precision; the Step 6 text
and image goldens (24) derive from the log. ts-rs: ExportInput, ExportErrorKind,
ExportWarning, LogoSlot.
This commit is contained in:
bchanot committed 2026-10-18 21:47:40 +02:00
1 parent 71bcb742c9
commit 70007e0c93
19 files changed
+4094 -29

No files matched your search

+345 -1
View File
@@ -1 +1,345 @@
//! R4.4b placeholder.
//! R4.4b: the layout against the O1 draw logs (`apps/app/tests/fixtures/goldens/r4/
//! draw.<report>.<locale>.<logos>.json`, `apps/app/tests/helpers/r4-draw.ts`): every call
//! of the TypeScript engine, at full precision, in call order, for the 12 Step 6 runs. The
//! export input of each run is the envelope its own R3 PDF carries (`pdf/r3/*.pdf`, read by
//! the R4 reader), the rasters the stubs the goldens used (1×1 PNG without logos, 2×1 PNG
//! with them: the SVG partner logo and every view; the PNG logos decode their own base64).
//! Then the Step 6 text and image goldens (`goldens/step6/{text,images}.*.json`, two
//! decimals) as the derived check the plan lists.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use common::r4::{repo_root, sha256_hex};
use faunosteo_core::domain::archive::Locale;
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
use faunosteo_core::pdf::write::draw_log::{DrawLog, DrawOp};
use faunosteo_core::pdf::write::input::{ExportInput, Rasters, check_input};
use faunosteo_core::pdf::write::layout::layout;
use serde_json::{Value, json};
const REPORTS: [&str; 3] = ["demo", "v2full", "v2unresolved"];
const LOCALES: [&str; 2] = ["fr", "en"];
const MODES: [&str; 2] = ["nologos", "logos"];
const ONE_PX_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
const TWO_BY_ONE_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAIAAAB7QOjdAAAAD0lEQVR4nGPYEWX///9/AA0PBE/1T7nxAAAAAElFTkSuQmCC";
fn base64(text: &str) -> Vec<u8> {
let table: Vec<u8> = (b'A'..=b'Z')
.chain(b'a'..=b'z')
.chain(b'0'..=b'9')
.chain(*b"+/")
.collect();
let mut out = Vec::new();
let (mut acc, mut bits) = (0u32, 0u32);
for c in text.bytes().filter(|c| *c != b'=') {
acc = (acc << 6) | table.iter().position(|t| *t == c).unwrap() as u32;
bits += 6;
if bits >= 8 {
bits -= 8;
out.push((acc >> bits) as u8);
acc &= (1 << bits) - 1;
}
}
out
}
fn fixture(path: &str) -> Vec<u8> {
std::fs::read(repo_root().join("apps/app/tests/fixtures").join(path)).unwrap()
}
/// The envelope the R3 PDF of a run carries, as the export input of that run.
fn export_input(report: &str, locale: &str, mode: &str) -> ExportInput {
let pdf = fixture(&format!("pdf/r3/{report}.{locale}.{mode}.pdf"));
let located = read_attached_document(&pdf, ProbeLimits::UNBOUNDED).unwrap();
let envelope = parse_json(&located.text).unwrap();
let input = json!({
"report": envelope["report"],
"sheetSnapshot": envelope["sheetSnapshot"],
"branding": envelope["branding"],
"labels": envelope["labels"],
"locale": envelope["locale"],
"exportedAt": envelope["exportedAt"],
});
serde_json::from_value(input).unwrap()
}
/// The stub rasters of the goldens: one PNG for every view and every SVG logo slot.
fn stub_rasters(input: &ExportInput, mode: &str) -> Rasters {
let png = base64(if mode == "logos" {
TWO_BY_ONE_PNG
} else {
ONE_PX_PNG
});
let mut rasters = Rasters {
header_logo: Some(png.clone()),
footer_logo: Some(png.clone()),
powered_by: Some(png.clone()),
partner: Some(png.clone()),
views: Default::default(),
};
for observation in &input.report.observations {
for zone in &observation.zones {
rasters
.views
.entry((observation.id.clone(), zone.view_id.clone()))
.or_insert_with(|| png.clone());
}
}
rasters
}
fn color(c: &faunosteo_core::pdf::write::draw_log::Rgb) -> Value {
json!({ "type": "RGB", "red": c.red, "green": c.green, "blue": c.blue })
}
/// One op in the oracle's shape (without `seq`).
fn op_json(op: &DrawOp, log: &DrawLog) -> Value {
match op {
DrawOp::Text(t) => json!({ "op": "drawText", "text": t.text, "options": {
"x": t.x, "y": t.y, "size": t.size, "font": t.font.as_str(), "color": color(&t.color) } }),
DrawOp::Image(i) => json!({ "op": "drawImage",
"image": { "embedIndex": i.embed, "pngSha256": sha256_hex(&log.embeds[i.embed].png) },
"options": { "x": i.x, "y": i.y, "width": i.width, "height": i.height } }),
DrawOp::Line(l) => json!({ "op": "drawLine", "options": {
"start": { "x": l.start.x, "y": l.start.y }, "end": { "x": l.end.x, "y": l.end.y },
"thickness": l.thickness, "color": color(&l.color) } }),
DrawOp::Rectangle(r) => json!({ "op": "drawRectangle", "options": {
"x": r.x, "y": r.y, "width": r.width, "height": r.height, "color": color(&r.color) } }),
}
}
fn without_seq(op: &Value) -> Value {
let mut copy = op.clone();
copy.as_object_mut().unwrap().remove("seq");
copy
}
/// Objects with their keys sorted, recursively: the oracle's key order is the recorder's.
fn canon(value: &Value) -> Value {
match value {
Value::Object(map) => {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
Value::Object(
keys.into_iter()
.map(|k| (k.clone(), canon(&map[k])))
.collect(),
)
}
Value::Array(items) => Value::Array(items.iter().map(canon).collect()),
other => other.clone(),
}
}
/// Compares two JSON values in their JavaScript spelling (`4` and `4.0` are one number),
/// key order aside.
fn same(a: &Value, b: &Value) -> bool {
let js = |v: &Value| faunosteo_core::json::to_js_compact(&canon(v)).unwrap();
js(a) == js(b)
}
fn compare_run(report: &str, locale: &str, mode: &str, mismatches: &mut Vec<String>) -> usize {
let name = format!("{report}.{locale}.{mode}");
let oracle: Value = serde_json::from_str(
&std::fs::read_to_string(repo_root().join(format!(
"apps/app/tests/fixtures/goldens/r4/draw.{name}.json"
)))
.unwrap(),
)
.unwrap();
let input = export_input(report, locale, mode);
let rasters = stub_rasters(&input, mode);
let checked = check_input(input).unwrap();
let drawn = layout(&checked, &rasters).unwrap();
assert!(drawn.warnings.is_empty(), "{name}: {:?}", drawn.warnings);
let pages = oracle["pages"].as_array().unwrap();
if pages.len() != drawn.log.pages.len() {
mismatches.push(format!(
"{name}: {} pages, expected {}",
drawn.log.pages.len(),
pages.len()
));
return 0;
}
let mut ops = 0;
for (p, page) in pages.iter().enumerate() {
let expected = page["ops"].as_array().unwrap();
let actual = &drawn.log.pages[p].ops;
if expected.len() != actual.len() {
mismatches.push(format!(
"{name} page {p}: {} ops, expected {}",
actual.len(),
expected.len()
));
}
for (i, (e, a)) in expected.iter().zip(actual).enumerate() {
ops += 1;
let a = op_json(a, &drawn.log);
if !same(&without_seq(e), &a) {
mismatches.push(format!(
"{name} page {p} op {i}\n expected {}\n actual {a}",
without_seq(e)
));
}
}
}
let embeds: Vec<Value> = drawn
.log
.embeds
.iter()
.enumerate()
.map(|(i, e)| json!({ "embedIndex": i, "pngSha256": sha256_hex(&e.png) }))
.collect();
if !same(&oracle["embeds"], &Value::Array(embeds.clone())) {
mismatches.push(format!(
"{name} embeds\n expected {}\n actual {}",
oracle["embeds"],
Value::Array(embeds)
));
}
let links: Vec<Value> = drawn
.log
.links
.iter()
.map(|l| json!({ "page": l.page, "rect": l.rect, "uri": l.uri }))
.collect();
if !same(&oracle["links"], &Value::Array(links.clone())) {
mismatches.push(format!(
"{name} links\n expected {}\n actual {}",
oracle["links"],
Value::Array(links)
));
}
ops
}
#[test]
fn o1_draw_logs_match_the_typescript_at_full_precision() {
let mut mismatches = Vec::new();
let mut ops = 0;
for report in REPORTS {
for locale in LOCALES {
for mode in MODES {
ops += compare_run(report, locale, mode, &mut mismatches);
}
}
}
assert!(
mismatches.is_empty(),
"{} mismatches:\n{}",
mismatches.len(),
mismatches
.iter()
.take(12)
.cloned()
.collect::<Vec<_>>()
.join("\n")
);
println!("O1: 12 runs, {ops} ops equal");
}
/// Two decimals as the Step 6 recorder rounded (`Math.round(n * 100) / 100`).
fn round2(n: f64) -> f64 {
(n * 100.0).round() / 100.0
}
#[test]
fn step6_text_and_image_goldens_derive_from_the_log() {
let goldens = repo_root().join("apps/app/tests/fixtures/goldens/step6");
let mut checked = 0;
for report in REPORTS {
for locale in LOCALES {
for mode in MODES {
let name = format!("{report}.{locale}.{mode}");
let input = export_input(report, locale, mode);
let rasters = stub_rasters(&input, mode);
let drawn = layout(&check_input(input).unwrap(), &rasters).unwrap();
let text: Value = serde_json::from_str(
&std::fs::read_to_string(goldens.join(format!("text.{name}.json"))).unwrap(),
)
.unwrap();
let images: Value = serde_json::from_str(
&std::fs::read_to_string(goldens.join(format!("images.{name}.json"))).unwrap(),
)
.unwrap();
let (mut texts, mut imgs) = (Vec::new(), Vec::new());
for (p, op) in drawn.log.calls_in_order() {
{
match op {
DrawOp::Text(t) => texts.push(json!({ "page": p, "text": t.text, "x": round2(t.x), "y": round2(t.y), "size": t.size, "font": t.font.as_str() })),
DrawOp::Image(i) => imgs.push(json!({ "page": p, "x": round2(i.x), "y": round2(i.y), "width": round2(i.width), "height": round2(i.height) })),
_ => {}
}
}
}
assert!(same(&text, &Value::Array(texts)), "{name}: text golden");
assert!(same(&images, &Value::Array(imgs)), "{name}: images golden");
checked += 2;
}
}
}
assert_eq!(checked, 24);
}
#[test]
fn the_checks_refuse_what_export_ts_refused() {
let mut input = export_input("demo", "fr", "nologos");
input.report.sheet.document_sha256 = "0".repeat(64);
let err = check_input(input).err().unwrap();
assert_eq!(err.kind.as_str(), "sheet_mismatch");
let mut input = export_input("demo", "fr", "nologos");
input.branding = json!({ "siteUrl": "not a url" });
assert_eq!(check_input(input).err().unwrap().detail, "branding");
let mut input = export_input("demo", "fr", "nologos");
input.labels = json!({ "fr": {} });
assert_eq!(check_input(input).err().unwrap().detail, "labels");
let mut input = export_input("demo", "fr", "nologos");
input.sheet_snapshot = json!({ "status": "draft" });
assert_eq!(
check_input(input).err().unwrap().kind.as_str(),
"snapshot_invalid"
);
let input = export_input("demo", "fr", "nologos");
let _ = Locale::Fr;
assert!(check_input(input).is_ok());
}
#[test]
fn page_caps_and_missing_rasters_behave_as_export_ts() {
use faunosteo_core::pdf::write::input::{ExportWarning, LogoSlot, MAX_OBSERVATIONS};
// 501 observations: too_many_pages / observations.
let mut input = export_input("demo", "fr", "nologos");
let first = input.report.observations[0].clone();
while input.report.observations.len() <= MAX_OBSERVATIONS {
input.report.observations.push(first.clone());
}
let err = check_input(input).err().unwrap();
assert_eq!(
(err.kind.as_str(), err.detail.as_str()),
("too_many_pages", "observations")
);
// A missing view raster and an unreadable logo: one warning each, the page still drawn.
let input = export_input("demo", "fr", "logos");
let mut rasters = stub_rasters(&input, "logos");
rasters.partner = Some(b"not a png".to_vec());
let first_key = rasters.views.keys().next().cloned().unwrap();
rasters.views.remove(&first_key);
let checked = check_input(input).unwrap();
let drawn = layout(&checked, &rasters).unwrap();
assert_eq!(
drawn.warnings,
vec![
ExportWarning::LogoFailed {
slot: LogoSlot::Partner
},
ExportWarning::ViewRasterFailed {
observation_id: first_key.0,
view_id: first_key.1,
},
]
);
assert_eq!(drawn.log.pages.len(), checked.report.observations.len() + 1);
}
+407 -1
View File
@@ -1 +1,407 @@
//! R4.4a placeholder.
//! R4.4a: the text and date rules of the PDF writer (`pdf::write::{text, dates}`) against the
//! frozen oracles: O4 `oracles/r4/text.json` (safeText, toUpperCase, JS `\s` and trim,
//! measure, wrapLines), O5 `afm.json` (the generated `afm_data` tables) and O6 `dates.json`
//! (formatDate). Widths compare bit for bit, never with a tolerance.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use std::collections::HashMap;
use common::r4::r4_oracle;
use faunosteo_core::domain::archive::Locale;
use faunosteo_core::pdf::write::afm_data::{self, FontMetrics};
use faunosteo_core::pdf::write::dates::format_date;
use faunosteo_core::pdf::write::text::{
Font, is_js_whitespace, js_trim, js_upper, measure, safe_text, width_units, wrap_lines,
};
use serde_json::Value;
const SURROGATES: std::ops::RangeInclusive<u32> = 0xD800..=0xDFFF;
const FONTS: [Font; 3] = [Font::Helvetica, Font::HelveticaBold, Font::TimesRoman];
fn text_oracle() -> Value {
r4_oracle("text.json")
}
fn u32_of(value: &Value) -> u32 {
u32::try_from(value.as_u64().unwrap()).unwrap()
}
fn ranges_of(section: &Value) -> Vec<(u32, u32)> {
section["ranges"]
.as_array()
.unwrap()
.iter()
.map(|r| (u32_of(&r[0]), u32_of(&r[1])))
.collect()
}
fn in_ranges(ranges: &[(u32, u32)], cp: u32) -> bool {
ranges
.iter()
.any(|&(start, end)| (start..=end).contains(&cp))
}
/// Every Rust `char`: the scalar values, 0..=0x10FFFF without the surrogates.
fn all_chars() -> impl Iterator<Item = char> {
(0..=0x10_FFFF).filter_map(char::from_u32)
}
#[test]
fn safe_text_reproduces_every_oracle_range() {
let ranges = text_oracle()["safeText"]["ranges"]
.as_array()
.unwrap()
.clone();
let (mut checked, mut skipped, mut next) = (0u32, Vec::new(), 0u32);
for row in &ranges {
let (start, end, class) = (u32_of(&row[0]), u32_of(&row[1]), row[2].as_str().unwrap());
assert_eq!(start, next, "ranges are contiguous");
next = end + 1;
for cp in start..=end {
let Some(c) = char::from_u32(cp) else {
// A Rust char is never a surrogate: JavaScript saw a lone unit, made it `?`.
assert!(SURROGATES.contains(&cp) && class == "question", "{cp:#X}");
skipped.push(cp);
continue;
};
let expected = match class {
"kept" => c.to_string(),
"dropped" => String::new(),
"question" => "?".to_owned(),
other => panic!("class {other}"),
};
assert_eq!(safe_text(&c.to_string()), expected, "{cp:#X} ({class})");
checked += 1;
}
}
assert_eq!(next, 0x11_0000, "the ranges cover every code point");
assert_eq!(
skipped,
SURROGATES.collect::<Vec<_>>(),
"only the surrogates are skipped"
);
println!(
"safeText: {} ranges, {checked} code points checked, {} surrogates skipped",
ranges.len(),
skipped.len()
);
}
#[test]
fn safe_text_works_on_whole_strings() {
assert_eq!(safe_text("a\u{AD}b\u{2028}c\u{1F600}\u{0}é€"), "abc?é€");
assert_eq!(safe_text("\u{A0}\u{178}\u{FEFF}"), "\u{A0}\u{178}");
assert_eq!(safe_text(""), "");
}
/// O4 `upper`: the expected upper case of every code point that changes.
fn upper_table(upper: &Value) -> HashMap<u32, String> {
let mut table = HashMap::new();
for run in upper["runs"].as_array().unwrap() {
let (start, end, step) = (u32_of(&run[0]), u32_of(&run[1]), u32_of(&run[2]));
let delta = run[3].as_i64().unwrap();
for cp in (start..=end).step_by(step as usize) {
let mapped = char::from_u32(u32::try_from(i64::from(cp) + delta).unwrap()).unwrap();
table.insert(cp, mapped.to_string());
}
}
for special in upper["special"].as_array().unwrap() {
let result = special[1].as_array().unwrap().iter();
let text = result
.map(|cp| char::from_u32(u32_of(cp)).unwrap())
.collect();
assert!(table.insert(u32_of(&special[0]), text).is_none());
}
table
}
#[test]
fn js_upper_is_v8_to_upper_case_for_every_code_point() {
let oracle = text_oracle();
let table = upper_table(&oracle["upper"]);
let single = oracle["upper"]["singleCount"].as_u64().unwrap() as usize;
let special = oracle["upper"]["special"].as_array().unwrap().len();
assert_eq!(table.len(), single + special);
let (mut changed, mut diverging) = (0, Vec::new());
for c in all_chars() {
let expected = table
.get(&u32::from(c))
.cloned()
.unwrap_or_else(|| c.to_string());
changed += usize::from(expected != c.to_string());
let upper = js_upper(&c.to_string());
assert_eq!(upper, expected, "{:#X}", u32::from(c));
let rust: String = c.to_uppercase().collect();
if rust != upper {
diverging.push(format!("U+{:04X} V8 {upper:?} Rust {rust:?}", u32::from(c)));
}
}
assert_eq!(changed, table.len());
assert_eq!(js_upper("Straße fin ʼn"), "STRASSE FIN ʼN");
println!(
"upper: {changed} code points change ({single} one-to-one, {special} one-to-many); \
Rust {:?} to_uppercase differs on {}: {:?}",
char::UNICODE_VERSION,
diverging.len(),
diverging.iter().take(8).collect::<Vec<_>>()
);
}
#[test]
fn js_whitespace_and_trim_follow_the_oracle_sets() {
let oracle = text_oracle();
let whitespace = ranges_of(&oracle["jsWhitespace"]);
let trim = ranges_of(&oracle["jsTrim"]);
let (mut spaces, mut trimmed) = (0, 0);
for c in all_chars() {
let cp = u32::from(c);
assert_eq!(
is_js_whitespace(c),
in_ranges(&whitespace, cp),
"\\s {cp:#X}"
);
spaces += usize::from(in_ranges(&whitespace, cp));
let padded = format!("{c}{c}x{c}y{c}");
let expected = if in_ranges(&trim, cp) {
trimmed += 1;
format!("x{c}y")
} else {
padded.clone()
};
assert_eq!(js_trim(&padded), expected, "trim {cp:#X}");
}
assert_eq!(js_trim(" \t\u{FEFF}\u{3000}"), "");
assert_eq!(
js_trim("\u{85}a\u{85}"),
"\u{85}a\u{85}",
"U+0085 is not JS whitespace"
);
println!("jsWhitespace: {spaces} code points; jsTrim: {trimmed} code points");
}
/// The rows of every O4 `measure` group, in file order (the `widthSample` indexing).
fn measure_rows(measure: &Value) -> Vec<Value> {
let groups = [
"sheetNames",
"pdfLabels",
"reports",
"randomWinAnsi",
"randomUnicode",
];
let rows: Vec<Value> = groups
.iter()
.flat_map(|group| measure["groups"][group].as_array().unwrap().clone())
.collect();
for group in groups {
let count = measure["counts"][group].as_u64().unwrap() as usize;
assert_eq!(
measure["groups"][group].as_array().unwrap().len(),
count,
"{group}"
);
}
rows
}
fn check_measure_row(row: &Value, sizes: &[f64]) -> usize {
let (text, safe) = (row["text"].as_str().unwrap(), row["safe"].as_str().unwrap());
assert_eq!(safe_text(text), safe, "safe of {text:?}");
let mut exact_widths = 0;
for (f, font) in FONTS.into_iter().enumerate() {
let units = row["units"][f].as_f64().unwrap();
assert_eq!(
width_units(font, safe),
units,
"{} units of {text:?}",
font.as_str()
);
for (s, &size) in sizes.iter().enumerate() {
let width = measure(font, text, size);
assert_eq!(
width.to_bits(),
(units * (size / 1000.0)).to_bits(),
"{text:?} {size}"
);
if let Some(recorded) = row["widths"][f][s].as_f64() {
assert_eq!(width.to_bits(), recorded.to_bits(), "{text:?} {f} {size}");
exact_widths += 1;
}
}
}
exact_widths
}
#[test]
fn measure_matches_pdf_lib_bit_for_bit() {
let oracle = text_oracle();
let measure_oracle = &oracle["measure"];
let fonts: Vec<&str> = FONTS.iter().map(|f| f.as_str()).collect();
assert_eq!(measure_oracle["fonts"], serde_json::json!(fonts));
assert_eq!(measure_oracle["widthFormula"], "units * (size / 1000)");
let sizes: Vec<f64> = measure_oracle["sizes"]
.as_array()
.unwrap()
.iter()
.map(|s| s.as_f64().unwrap())
.collect();
let rows = measure_rows(measure_oracle);
assert_eq!(
rows.len() as u64,
measure_oracle["widthSample"]["total"].as_u64().unwrap()
);
let sampled: Vec<usize> = measure_oracle["widthSample"]["indices"]
.as_array()
.unwrap()
.iter()
.map(|i| i.as_u64().unwrap() as usize)
.collect();
let with_widths: Vec<usize> = (0..rows.len())
.filter(|&i| rows[i]["widths"].is_array())
.collect();
assert_eq!(
with_widths, sampled,
"widths are kept on the sample rows only"
);
let exact: usize = rows.iter().map(|row| check_measure_row(row, &sizes)).sum();
assert_eq!(exact, sampled.len() * FONTS.len() * sizes.len());
println!(
"measure: {} rows x {} fonts x {} sizes = {} widths, {exact} bit-exact against the \
recorded widths ({} sample rows)",
rows.len(),
FONTS.len(),
sizes.len(),
rows.len() * FONTS.len() * sizes.len(),
sampled.len()
);
}
/// One font of `afm_data` against O5: the WX of every byte's glyph (0 where the byte has
/// none) and every KPX pair between WinAnsi glyphs, by byte pair in sorted order.
fn check_font_table(oracle: &Value, metrics: &FontMetrics, byte_glyph: &HashMap<u8, &str>) {
let mut pairs = Vec::new();
for left in 0..=255u8 {
let glyph = byte_glyph.get(&left);
let wx = glyph.map_or(0, |g| oracle["glyphs"][g][0].as_u64().unwrap());
assert_eq!(
u64::from(metrics.widths[usize::from(left)]),
wx,
"WX of {left}"
);
for right in 0..=255u8 {
let amount = glyph
.zip(byte_glyph.get(&right))
.and_then(|(l, r)| oracle["kerning"][l][r].as_i64());
if let Some(amount) = amount {
pairs.push((left, right, i16::try_from(amount).unwrap()));
}
}
}
assert_eq!(metrics.kerning, pairs.as_slice());
}
#[test]
fn afm_tables_hold_every_glyph_and_pair_of_o5() {
let afm = r4_oracle("afm.json");
let rows = afm["winAnsi"].as_array().unwrap();
let byte_of = |row: &Value| u8::try_from(row[1].as_u64().unwrap()).unwrap();
let mut winansi: Vec<(u32, u8)> = rows.iter().map(|r| (u32_of(&r[0]), byte_of(r))).collect();
winansi.sort_unstable();
assert_eq!(afm_data::WINANSI, winansi.as_slice());
let byte_glyph: HashMap<u8, &str> = rows
.iter()
.map(|r| (byte_of(r), r[2].as_str().unwrap()))
.collect();
let tables = [
&afm_data::HELVETICA,
&afm_data::HELVETICA_BOLD,
&afm_data::TIMES_ROMAN,
];
for (font, metrics) in FONTS.into_iter().zip(tables) {
let oracle = &afm["fonts"][font.as_str()];
assert_eq!(oracle["winAnsiGlyphsWithoutWidth"], serde_json::json!([]));
check_font_table(oracle, metrics, &byte_glyph);
println!(
"afm {}: {} kerning pairs over WinAnsi",
font.as_str(),
metrics.kerning.len()
);
}
}
#[test]
fn wrap_lines_reproduces_every_case() {
let oracle = text_oracle();
let wrap = &oracle["wrapLines"];
assert_eq!(wrap["font"], "Helvetica");
let numbers = |key: &str| -> Vec<f64> {
wrap[key]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_f64().unwrap())
.collect()
};
let (sizes, max_widths) = (numbers("sizes"), numbers("maxWidths"));
assert_eq!(max_widths[0], 595.28 - 36.0 * 2.0);
let cases = wrap["cases"].as_array().unwrap();
for case in cases {
let text = case["text"].as_str().unwrap();
for (s, &size) in sizes.iter().enumerate() {
for (w, &max) in max_widths.iter().enumerate() {
let expected: Vec<String> =
serde_json::from_value(case["lines"][s][w].clone()).unwrap();
let lines = wrap_lines(text, Font::Helvetica, size, max);
assert_eq!(lines, expected, "{text:?} at {size} pt within {max}");
}
}
}
assert_eq!(
wrap_lines("SIRET ", Font::Helvetica, 10.0, 1000.0),
["SIRET "]
);
println!(
"wrapLines: {} cases x {} sizes x {} widths = {} wraps",
cases.len(),
sizes.len(),
max_widths.len(),
cases.len() * sizes.len() * max_widths.len()
);
}
fn check_date(input: &str, fr: &str, en: &str) {
assert_eq!(format_date(input, Locale::Fr), fr, "fr {input:?}");
assert_eq!(format_date(input, Locale::En), en, "en {input:?}");
}
#[test]
fn format_date_reproduces_the_intl_grid_and_the_others() {
let oracle = r4_oracle("dates.json");
let grid = &oracle["grid"];
let list = |key: &str| -> Vec<String> { serde_json::from_value(grid[key].clone()).unwrap() };
let (years, months, days) = (list("years"), list("months"), list("days"));
let (mut cells, mut refused) = (0, 0);
for year in &years {
for month in &months {
let results = &grid["results"][format!("{year}-{month}")];
for (d, day) in days.iter().enumerate() {
let input = format!("{year}-{month}-{day}");
let fr = results["fr"][d].as_str().unwrap();
check_date(&input, fr, results["en"][d].as_str().unwrap());
cells += 1;
refused += usize::from(fr == input);
}
}
}
let others = oracle["others"].as_array().unwrap();
for row in others {
let [input, fr, en] = [0, 1, 2].map(|i| row[i].as_str().unwrap());
check_date(input, fr, en);
}
check_date("2026-01-05", "5 janvier 2026", "January 5, 2026");
println!(
"dates: {cells} grid cells ({refused} refused) x 2 locales, {} others",
others.len()
);
}