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

+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()
);
}