From d420f67352b02552b0274db1f7058b7cfdcf14dd Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 18 Oct 2026 22:54:04 +0200 Subject: [PATCH] feat(domain): R4.5a content stream emitter: pdf-lib operator sequences and numberToString byte for byte drawText/drawImage/drawLine/drawRectangle as pdf-lib 1.17.1 emits them (cleanText, 24 TL, upper-case hex, the doubled m of drawLine, the h f of a filled rectangle); numberToString with the e-N / e+N branches and V8's Math.pow(10, k) one-ULP quirk at k = 23 and 210; WinAnsi text encoding. The 12 R3 PDFs' 60 content streams (224,534 bytes) equal the emitter's output from the O1 logs with only the resource names renamed; 703 O4 cases. --- crates/core/src/pdf/write/content.rs | 317 ++++++++++++++++++++++- crates/core/tests/pdf_content.rs | 367 ++++++++++++++++++++++++++- 2 files changed, 682 insertions(+), 2 deletions(-) diff --git a/crates/core/src/pdf/write/content.rs b/crates/core/src/pdf/write/content.rs index b165b5c2..3958fee7 100644 --- a/crates/core/src/pdf/write/content.rs +++ b/crates/core/src/pdf/write/content.rs @@ -1 +1,316 @@ -//! R4.5a: the content stream emitter (pdf-lib operator sequences, `numberToString`). Written by the R4.5a track. +//! R4.5a: one page of the draw log as the bytes of its PDF content stream, operator for +//! operator what pdf-lib 1.17.1 wrote for the same calls (`api/PDFPage.js` → `api/operations.js` +//! → `api/operators.js`), number spelling included (`utils/numbers.js` `numberToString`). +//! Pinned against the content streams of the twelve R3 PDFs, whose calls the O1 logs carry. +//! +//! The resource names are ours, the serialiser declares them: fonts `/F1` Helvetica, `/F2` +//! Helvetica-Bold, `/F3` Times-Roman ([`font_resource_name`]), images `/Im` +//! ([`image_resource_name`]). pdf-lib named a fresh `/Helvetica-` or +//! `/Image-` per call; the bytes are otherwise the same. + +use super::afm_data; +use super::draw_log::{DrawOp, ImageOp, LineOp, PageLog, RectangleOp, Rgb, TextOp}; +use super::text::Font; +use crate::json::js_number; + +/// `PDFPage`'s default `lineHeight` (constructor, `api/PDFPage.js`): the `TL` of every text, +/// since the engine never passes one. +const DEFAULT_LINE_HEIGHT: f64 = 24.0; + +/// `PDFPage`'s default `fontSize`: `drawText` uses `options.size || this.fontSize`. +const DEFAULT_FONT_SIZE: f64 = 24.0; + +/// `drawRectangle`'s `options.borderWidth ?? 0` (`api/PDFPage.js`). +const DEFAULT_BORDER_WIDTH: f64 = 0.0; + +/// The upright text matrix of `rotateAndSkewTextRadiansAndTranslate` (`api/operators.js`) for +/// pdf-lib's `degrees(0)` defaults: `cos 0`, `sin 0 + tan 0`, `-sin 0 + tan 0` (= +0), `cos 0`. +const UPRIGHT_TEXT: [f64; 4] = [1.0, 0.0, 0.0, 1.0]; + +/// `rotateRadians(0)` (`api/operators.js`): `cos 0, sin 0, -sin 0, cos 0, 0, 0`; the `-0` +/// prints `0`. +const ROTATE_ZERO: [f64; 6] = [1.0, 0.0, -0.0, 1.0, 0.0, 0.0]; + +/// `skewRadians(0, 0)` (`api/operators.js`): `1, tan 0, tan 0, 1, 0, 0`. +const SKEW_ZERO: [f64; 6] = [1.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + +/// The WinAnsi byte of `?`, what a code point outside the encoding prints. +const QUESTION_BYTE: u8 = b'?'; + +/// The powers of ten where V8's `Math.pow(10, k)` (its fdlibm `ieee754::pow`, Node 24 / +/// V8 13.6) is one ULP above the correctly rounded `1e`: checked for every `k` in -330..=330. +const POW10_ONE_ULP_UP: [i32; 2] = [23, 210]; + +/// The `/Font` resource key of `font` on every page (without the `/` of the name syntax). +pub fn font_resource_name(font: Font) -> &'static str { + match font { + Font::Helvetica => "F1", + Font::HelveticaBold => "F2", + Font::TimesRoman => "F3", + } +} + +/// The `/XObject` resource key of the image of embed index `embed` (without the `/`). +pub fn image_resource_name(embed: usize) -> String { + format!("Im{embed}") +} + +/// The content stream of `page`, uncompressed: every call of the log in order, each as the +/// operators pdf-lib's `PDFPage.drawText` / `drawImage` / `drawLine` / `drawRectangle` pushed +/// with the options the engine passed and the defaults pdf-lib filled in. +pub fn page_content(page: &PageLog) -> Vec { + let mut content = Content::default(); + for op in &page.ops { + match op { + DrawOp::Text(text) => draw_text(&mut content, text), + DrawOp::Image(image) => draw_image(&mut content, image), + DrawOp::Line(line) => draw_line(&mut content, line), + DrawOp::Rectangle(rectangle) => draw_rectangle(&mut content, rectangle), + } + } + content.bytes +} + +/// `numberToString` (`utils/numbers.js`), the printer of every `PDFNumber` operand: the +/// JavaScript `String(num)`, except that an exponent form is spelt out in full, through the +/// same float arithmetic (`num * 10^(e-1)` below 1, `num / 10^(e-20)` above `1e21`). +pub fn number_to_string(value: f64) -> String { + let text = js_to_string(value); + if value.abs() < 1.0 { + spell_small(value, text) + } else { + spell_large(value, text) + } +} + +/// The WinAnsi bytes of `text` (`StandardFontEmbedder.encodeTextAsGlyphs`, +/// `core/embedders/StandardFontEmbedder.js`, one glyph per code point); a code point the +/// encoding lacks, where pdf-lib would throw and `safeText` leaves none, prints `?`. +pub fn encode_text(text: &str) -> Vec { + text.chars() + .map(|c| winansi_byte(c).unwrap_or(QUESTION_BYTE)) + .collect() +} + +/// `Encodings.WinAnsi.encodeUnicodeCodePoint(c).code` from `afm_data::WINANSI`. +fn winansi_byte(c: char) -> Option { + let table = afm_data::WINANSI; + let index = table + .binary_search_by_key(&u32::from(c), |&(cp, _)| cp) + .ok()?; + table.get(index).map(|&(_, byte)| byte) +} + +/// One content stream being written: `PDFContentStream.getUnencodedContents` +/// (`core/structures/PDFContentStream.js`) writes every operator followed by `\n`. +#[derive(Default)] +struct Content { + bytes: Vec, +} + +impl Content { + /// `PDFOperator.copyBytesInto` (`core/operators/PDFOperator.js`): every operand followed by + /// one space, then the operator name. + fn op(&mut self, operands: &[&str], name: &str) { + for operand in operands { + self.bytes.extend_from_slice(operand.as_bytes()); + self.bytes.push(b' '); + } + self.bytes.extend_from_slice(name.as_bytes()); + self.bytes.push(b'\n'); + } + + /// An operator whose operands are all `PDFNumber`s (`asPDFNumber`, `api/objects.js`). + fn numbers(&mut self, values: &[f64], name: &str) { + let printed: Vec = values.iter().map(|&v| number_to_string(v)).collect(); + let operands: Vec<&str> = printed.iter().map(String::as_str).collect(); + self.op(&operands, name); + } +} + +/// `setFillingColor` → `setFillingRgbColor` (`api/colors.js`, `api/operators.js`): `r g b rg`. +fn set_filling_rgb(content: &mut Content, color: Rgb) { + content.numbers(&[color.red, color.green, color.blue], "rg"); +} + +/// `setStrokingColor` → `setStrokingRgbColor`: `r g b RG`. +fn set_stroking_rgb(content: &mut Content, color: Rgb) { + content.numbers(&[color.red, color.green, color.blue], "RG"); +} + +/// `setDashPattern([], 0)` (`api/operators.js`): the array is a pre-printed `"[]"` operand. +fn set_no_dash(content: &mut Content) { + content.op(&["[]", &number_to_string(0.0)], "d"); +} + +/// `translate(x, y)` = `concatTransformationMatrix(1, 0, 0, 1, x, y)`. +fn translate(content: &mut Content, x: f64, y: f64) { + content.numbers(&[1.0, 0.0, 0.0, 1.0, x, y], "cm"); +} + +/// `drawText` of `api/PDFPage.js`: `lineSplit(cleanText(text))`, each line `encodeText`-ed, +/// then `drawLinesOfText` (`api/operations.js`): `q BT rg Tf TL Tm`, `Tj T*` per line, `ET Q`. +fn draw_text(content: &mut Content, op: &TextOp) { + let font = format!("/{}", font_resource_name(op.font)); + let [a, b, c, d] = UPRIGHT_TEXT; + content.op(&[], "q"); + content.op(&[], "BT"); + set_filling_rgb(content, op.color); + content.op(&[&font, &number_to_string(font_size(op.size))], "Tf"); + content.numbers(&[DEFAULT_LINE_HEIGHT], "TL"); + content.numbers(&[a, b, c, d, op.x, op.y], "Tm"); + for line in line_split(&clean_text(&op.text)) { + content.op(&[&hex_string(&encode_text(line))], "Tj"); + content.op(&[], "T*"); + } + content.op(&[], "ET"); + content.op(&[], "Q"); +} + +/// `options.size || this.fontSize`: a falsy size (`0`, `-0`, `NaN`) falls back to 24. +fn font_size(size: f64) -> f64 { + if size == 0.0 || size.is_nan() { + DEFAULT_FONT_SIZE + } else { + size + } +} + +/// `cleanText` (`utils/strings.js`): tab, U+0085, U+2028 and U+2029 become four spaces, then +/// backspace and vertical tab are removed. +fn clean_text(text: &str) -> String { + let mut clean = String::with_capacity(text.len()); + for c in text.chars() { + match c { + '\t' | '\u{85}' | '\u{2028}' | '\u{2029}' => clean.push_str(" "), + '\u{8}' | '\u{B}' => {} + other => clean.push(other), + } + } + clean +} + +/// `lineSplit` (`utils/strings.js`): `text.split(/[\n\f\r\u000B]/)`, empty pieces kept. +fn line_split(text: &str) -> impl Iterator { + text.split(['\n', '\u{C}', '\r', '\u{B}']) +} + +/// `PDFHexString.copyBytesInto` (`core/objects/PDFHexString.js`) of the value `encodeText` +/// builds: `<`, two upper-case hex digits per glyph code (`toHexString`), `>`. +fn hex_string(bytes: &[u8]) -> String { + let mut hex = String::with_capacity(bytes.len() * 2 + 2); + hex.push('<'); + for byte in bytes { + hex.push_str(&format!("{byte:02X}")); + } + hex.push('>'); + hex +} + +/// `drawImage` (`api/PDFPage.js` → `api/operations.js`): `q`, translate, `rotateRadians(0)`, +/// `scale(width, height)`, `skewRadians(0, 0)`, `/Im Do`, `Q`. +fn draw_image(content: &mut Content, op: &ImageOp) { + let name = format!("/{}", image_resource_name(op.embed)); + content.op(&[], "q"); + translate(content, op.x, op.y); + content.numbers(&ROTATE_ZERO, "cm"); + content.numbers(&[op.width, 0.0, 0.0, op.height, 0.0, 0.0], "cm"); + content.numbers(&SKEW_ZERO, "cm"); + content.op(&[&name], "Do"); + content.op(&[], "Q"); +} + +/// `drawLine` (`api/PDFPage.js` → `api/operations.js`): `q RG w`, no dash, the start point +/// moved to twice (pdf-lib's `moveTo` stands on both sides of the absent `lineCap`), `l S Q`. +fn draw_line(content: &mut Content, op: &LineOp) { + content.op(&[], "q"); + set_stroking_rgb(content, op.color); + content.numbers(&[op.thickness], "w"); + set_no_dash(content); + content.numbers(&[op.start.x, op.start.y], "m"); + content.numbers(&[op.start.x, op.start.y], "m"); + content.numbers(&[op.end.x, op.end.y], "l"); + content.op(&[], "S"); + content.op(&[], "Q"); +} + +/// `drawRectangle` (`api/PDFPage.js` → `api/operations.js`): `q rg`, border width 0, no dash, +/// translate, `rotateRadians(0)`, `skewRadians(0, 0)`, the path from the origin, `h`, then `f` +/// (a colour with a falsy border width fills only), `Q`. +fn draw_rectangle(content: &mut Content, op: &RectangleOp) { + content.op(&[], "q"); + set_filling_rgb(content, op.color); + content.numbers(&[DEFAULT_BORDER_WIDTH], "w"); + set_no_dash(content); + translate(content, op.x, op.y); + content.numbers(&ROTATE_ZERO, "cm"); + content.numbers(&SKEW_ZERO, "cm"); + content.numbers(&[0.0, 0.0], "m"); + content.numbers(&[0.0, op.height], "l"); + content.numbers(&[op.width, op.height], "l"); + content.numbers(&[op.width, 0.0], "l"); + content.op(&[], "h"); + content.op(&[], "f"); + content.op(&[], "Q"); +} + +/// JavaScript `String(num)`: [`js_number`] for a finite value, `NaN` / `Infinity` otherwise. +fn js_to_string(value: f64) -> String { + if value.is_nan() { + "NaN".to_owned() + } else if value.is_infinite() { + let sign = if value < 0.0 { "-" } else { "" }; + format!("{sign}Infinity") + } else { + js_number(value) + } +} + +/// `parseInt(text.split(separator)[1])` on a JavaScript number's spelling: the digits after +/// the exponent separator, `None` (NaN) when there is none. +fn exponent_after(text: &str, separator: &str) -> Option { + let (_, rest) = text.split_once(separator)?; + let end = rest + .find(|c: char| !c.is_ascii_digit()) + .unwrap_or(rest.len()); + rest.get(..end)?.parse().ok() +} + +/// The `Math.abs(num) < 1` branch: `e` from `e-`, then `'0.' + (e - 1) zeros + +/// String(|num| * 10^(e-1)).substring(2)`, signed back. +fn spell_small(value: f64, text: String) -> String { + let Some(e) = exponent_after(&text, "e-").filter(|&e| e != 0) else { + return text; + }; + let negative = value < 0.0; + let magnitude = if negative { -value } else { value }; + let scaled = js_to_string(magnitude * js_pow10(e - 1)); + let zeros = "0".repeat(usize::try_from(e - 1).unwrap_or(0)); + let sign = if negative { "-" } else { "" }; + format!("{sign}0.{zeros}{}", scaled.get(2..).unwrap_or("")) +} + +/// The other branch: an exponent `e` above 20 (from `e+`) divides by `10^(e-20)` and appends +/// `e - 20` zeros to the quotient's `String`. +fn spell_large(value: f64, text: String) -> String { + match exponent_after(&text, "+") { + Some(e) if e > 20 => { + let shift = e - 20; + let quotient = js_to_string(value / js_pow10(shift)); + let zeros = "0".repeat(usize::try_from(shift).unwrap_or(0)); + format!("{quotient}{zeros}") + } + _ => text, + } +} + +/// V8's `Math.pow(10, k)`: the correctly rounded `1e` (`Infinity` past the range), one ULP +/// up at the two exponents where V8's pow is ([`POW10_ONE_ULP_UP`]). +fn js_pow10(k: i32) -> f64 { + let rounded: f64 = format!("1e{k}").parse().unwrap_or(f64::INFINITY); + if POW10_ONE_ULP_UP.contains(&k) { + f64::from_bits(rounded.to_bits() + 1) + } else { + rounded + } +} diff --git a/crates/core/tests/pdf_content.rs b/crates/core/tests/pdf_content.rs index df7b0b70..856d0b57 100644 --- a/crates/core/tests/pdf_content.rs +++ b/crates/core/tests/pdf_content.rs @@ -1 +1,366 @@ -//! R4.5 placeholder, written by its track. +//! R4.5a: the content stream emitter (`pdf::write::content`) against pdf-lib 1.17.1 itself. +//! For each of the 12 R3 runs, every page of `pdf/r3/...pdf` (written +//! by pdf-lib from the calls the O1 log `goldens/r4/draw....json` +//! records) is compared byte for byte with `page_content` of the page rebuilt from that log, +//! after renaming pdf-lib's per-call resource names (`/Helvetica-`, `/Image-`) +//! to ours. Then `number_to_string` on the O4 `numberToString` cases and `encode_text` on +//! WinAnsi samples. +#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)] + +mod common; + +use std::collections::{BTreeMap, BTreeSet}; + +use common::r4::{r4_oracle, repo_root}; +use faunosteo_core::pdf::write::content::{ + encode_text, font_resource_name, image_resource_name, number_to_string, page_content, +}; +use faunosteo_core::pdf::write::draw_log::{ + DrawOp, ImageOp, LineOp, PageLog, Point, RectangleOp, Rgb, TextOp, +}; +use faunosteo_core::pdf::write::text::Font; +use lopdf::{Dictionary, Document, Object, ObjectId}; +use serde_json::Value; + +const REPORTS: [&str; 3] = ["demo", "v2full", "v2unresolved"]; +const LOCALES: [&str; 2] = ["fr", "en"]; +const MODES: [&str; 2] = ["nologos", "logos"]; + +fn fixture(path: &str) -> Vec { + std::fs::read(repo_root().join("apps/app/tests/fixtures").join(path)).unwrap() +} + +fn font_named(name: &str) -> Font { + match name { + "Helvetica" => Font::Helvetica, + "Helvetica-Bold" => Font::HelveticaBold, + "Times-Roman" => Font::TimesRoman, + other => panic!("unknown font {other}"), + } +} + +fn num(value: &Value) -> f64 { + value + .as_f64() + .unwrap_or_else(|| panic!("not a number: {value}")) +} + +fn color(value: &Value) -> Rgb { + assert_eq!(value["type"], "RGB"); + Rgb { + red: num(&value["red"]), + green: num(&value["green"]), + blue: num(&value["blue"]), + } +} + +fn point(value: &Value) -> Point { + Point { + x: num(&value["x"]), + y: num(&value["y"]), + } +} + +/// Every option key of a call, so a key the rebuild would drop fails the test. +fn assert_keys(options: &Value, expected: &[&str]) { + let keys: BTreeSet<&str> = options + .as_object() + .unwrap() + .keys() + .map(String::as_str) + .collect(); + let wanted: BTreeSet<&str> = expected.iter().copied().collect(); + assert_eq!(keys, wanted, "option keys of {options}"); +} + +/// One O1 call as the draw log op it records. +fn draw_op(call: &Value) -> DrawOp { + let o = &call["options"]; + match call["op"].as_str().unwrap() { + "drawText" => { + assert_keys(o, &["x", "y", "size", "font", "color"]); + DrawOp::Text(TextOp { + text: call["text"].as_str().unwrap().to_owned(), + x: num(&o["x"]), + y: num(&o["y"]), + size: num(&o["size"]), + font: font_named(o["font"].as_str().unwrap()), + color: color(&o["color"]), + }) + } + "drawImage" => { + assert_keys(o, &["x", "y", "width", "height"]); + DrawOp::Image(ImageOp { + embed: call["image"]["embedIndex"].as_u64().unwrap() as usize, + x: num(&o["x"]), + y: num(&o["y"]), + width: num(&o["width"]), + height: num(&o["height"]), + }) + } + "drawLine" => { + assert_keys(o, &["start", "end", "thickness", "color"]); + DrawOp::Line(LineOp { + start: point(&o["start"]), + end: point(&o["end"]), + thickness: num(&o["thickness"]), + color: color(&o["color"]), + }) + } + "drawRectangle" => { + assert_keys(o, &["x", "y", "width", "height", "color"]); + DrawOp::Rectangle(RectangleOp { + x: num(&o["x"]), + y: num(&o["y"]), + width: num(&o["width"]), + height: num(&o["height"]), + color: color(&o["color"]), + }) + } + other => panic!("unknown op {other}"), + } +} + +/// The pages of an O1 log, their calls in page order (the log lists them so). +fn page_logs(log: &Value) -> Vec { + let pages = log["pages"].as_array().unwrap(); + pages + .iter() + .enumerate() + .map(|(index, page)| { + assert_eq!(page["index"].as_u64().unwrap() as usize, index); + let ops: Vec = page["ops"] + .as_array() + .unwrap() + .iter() + .map(draw_op) + .collect(); + PageLog { + footer_start: ops.len(), + ops, + } + }) + .collect() +} + +fn resources(doc: &Document, page: ObjectId) -> Dictionary { + let (direct, ids) = doc.get_page_resources(page).unwrap(); + let mut merged = Dictionary::new(); + for dict in direct + .into_iter() + .chain(ids.iter().map(|id| doc.get_dictionary(*id).unwrap())) + { + for (key, value) in dict.iter() { + merged.set(key.clone(), value.clone()); + } + } + merged +} + +/// A resource sub-dictionary (`/Font`, `/XObject`): key → referenced object id. +fn named_refs(doc: &Document, res: &Dictionary, kind: &[u8]) -> BTreeMap, ObjectId> { + let Ok(entry) = res.get(kind) else { + return BTreeMap::new(); + }; + let dict = match entry { + Object::Reference(id) => doc.get_dictionary(*id).unwrap(), + Object::Dictionary(dict) => dict, + other => panic!("{other:?}"), + }; + dict.iter() + .map(|(key, value)| (key.clone(), value.as_reference().unwrap())) + .collect() +} + +/// pdf-lib's page content: its one content stream, decompressed. +fn pdf_lib_content(doc: &Document, page: ObjectId) -> Vec { + let streams = doc.get_page_contents(page); + assert_eq!(streams.len(), 1, "one content stream per page"); + let stream = doc.get_object(streams[0]).unwrap().as_stream().unwrap(); + stream.decompressed_content().unwrap() +} + +/// pdf-lib's resource name → ours: fonts by `/BaseFont`, images by embed index (the image +/// objects' numbers grow in embed order, `embedPng` takes `nextRef()`). +fn renames(doc: &Document, page: ObjectId, images: &[ObjectId]) -> Vec<(Vec, String)> { + let res = resources(doc, page); + let mut renames = Vec::new(); + for (key, id) in named_refs(doc, &res, b"Font") { + let base = doc.get_dictionary(id).unwrap().get(b"BaseFont").unwrap(); + let font = font_named(std::str::from_utf8(base.as_name().unwrap()).unwrap()); + renames.push((key, font_resource_name(font).to_owned())); + } + for (key, id) in named_refs(doc, &res, b"XObject") { + let embed = images.iter().position(|image| *image == id).unwrap(); + renames.push((key, image_resource_name(embed))); + } + renames +} + +/// Every occurrence of the name token `/ ` replaced by `/ `. +fn rename(content: &[u8], from: &[u8], to: &str) -> Vec { + let needle = [b"/", from, b" "].concat(); + let replacement = format!("/{to} ").into_bytes(); + let mut out = Vec::with_capacity(content.len()); + let mut rest = content; + while let Some(at) = rest + .windows(needle.len()) + .position(|w| w == needle.as_slice()) + { + out.extend_from_slice(&rest[..at]); + out.extend_from_slice(&replacement); + rest = &rest[at + needle.len()..]; + } + out.extend_from_slice(rest); + out +} + +/// The document's image XObjects (from every page's resources), in object number order. +fn image_objects(doc: &Document) -> Vec { + let mut ids = BTreeSet::new(); + for page in doc.get_pages().values() { + ids.extend(named_refs(doc, &resources(doc, *page), b"XObject").into_values()); + } + ids.into_iter().collect() +} + +fn first_difference(left: &[u8], right: &[u8]) -> String { + let at = left.iter().zip(right).take_while(|(l, r)| l == r).count(); + let window = |bytes: &[u8]| { + String::from_utf8_lossy(&bytes[at.saturating_sub(80)..(at + 80).min(bytes.len())]) + .into_owned() + }; + format!( + "first difference at byte {at}\npdf-lib:\n{}\nours:\n{}", + window(left), + window(right) + ) +} + +/// One run: (pages, bytes compared). +fn compare_run(report: &str, locale: &str, mode: &str) -> (usize, usize) { + let run = format!("{report}.{locale}.{mode}"); + let log: Value = + serde_json::from_slice(&fixture(&format!("goldens/r4/draw.{run}.json"))).unwrap(); + let doc = Document::load_mem(&fixture(&format!("pdf/r3/{run}.pdf"))).unwrap(); + let images = image_objects(&doc); + assert_eq!( + images.len(), + log["embeds"].as_array().unwrap().len(), + "{run}: embeds" + ); + let logs = page_logs(&log); + let pages = doc.get_pages(); + assert_eq!(pages.len(), logs.len(), "{run}: page count"); + let mut compared = 0; + for (page_log, page) in logs.iter().zip(pages.values()) { + let mut theirs = pdf_lib_content(&doc, *page); + for (from, to) in renames(&doc, *page, &images) { + theirs = rename(&theirs, &from, &to); + } + let ours = page_content(page_log); + assert!( + ours == theirs, + "{run} page {page:?}: {}", + first_difference(&theirs, &ours) + ); + compared += ours.len(); + } + (logs.len(), compared) +} + +#[test] +fn content_streams_match_pdf_lib_on_the_twelve_r3_runs() { + let (mut runs, mut pages, mut bytes) = (0, 0, 0); + for report in REPORTS { + for locale in LOCALES { + for mode in MODES { + let (run_pages, run_bytes) = compare_run(report, locale, mode); + println!("{report}.{locale}.{mode}: {run_pages} pages, {run_bytes} bytes equal"); + runs += 1; + pages += run_pages; + bytes += run_bytes; + } + } + } + println!("{runs} runs, {pages} pages, {bytes} content bytes equal"); + assert_eq!(runs, 12); +} + +#[test] +fn number_to_string_reproduces_the_o4_cases() { + let oracle = r4_oracle("text.json"); + let cases = oracle["numberToString"]["cases"].as_array().unwrap(); + assert!(cases.len() > 700); + for case in cases { + let input: f64 = case[0].as_str().unwrap().parse().unwrap(); + assert_eq!( + number_to_string(input), + case[1].as_str().unwrap(), + "{}", + case[0] + ); + } +} + +/// Exponents where V8's `Math.pow(10, k)` is one ULP above `1e` (k = 23, 210), which the +/// O4 cases do not reach; recorded from pdf-lib's `numberToString` on Node v24.21.0. +#[test] +fn number_to_string_follows_v8_math_pow() { + let cases = [ + (1.5e-24, "0.0000000000000000000000015000000000000002"), + (1.2345e-24, "0.0000000000000000000000012345000000000002"), + (7e-24, "0.000000000000000000000007"), + (1.25e43, "12500000000000000000000000000000000000000000"), + (4.4e43, "43999999999999993000000000000000000000000000"), + (f64::NAN, "NaN"), + (f64::INFINITY, "Infinity"), + (f64::NEG_INFINITY, "-Infinity"), + (-2.5e-9, "-0.0000000025"), + ]; + for (input, expected) in cases { + assert_eq!(number_to_string(input), expected, "{input:e}"); + } + let large = number_to_string(9.87e230); + assert_eq!(large, format!("9869999999999999{}", "0".repeat(215))); + let small = number_to_string(3.3e-211); + assert_eq!(small, format!("0.{}33", "0".repeat(210))); +} + +#[test] +fn encode_text_prints_winansi_bytes() { + assert_eq!(encode_text("é"), [0xE9]); + assert_eq!(encode_text("€"), [0x80]); + assert_eq!(encode_text("'"), [0x27]); + assert_eq!(encode_text("\u{2019}"), [0x92]); + assert_eq!(encode_text("œ"), [0x9C]); + assert_eq!(encode_text("\u{A0}"), [0xA0]); + assert_eq!(encode_text("Équi-Ostéo"), b"\xC9qui-Ost\xE9o"); + assert_eq!(encode_text("日\u{1F600}a\u{7}"), b"??a?"); + assert_eq!(encode_text(""), b""); +} + +/// `cleanText` and `lineSplit` of `PDFPage.drawText`, a falsy size and spelt-out numbers, +/// as pdf-lib wrote them (Node v24.21.0, `getContentStream().getUnencodedContents()`). +#[test] +fn draw_text_splits_and_cleans_like_pdf_lib() { + let page = PageLog { + ops: vec![DrawOp::Text(TextOp { + text: "a\tb\nc\u{B}d\re".to_owned(), + x: 0.1 + 0.2, + y: 1e-7, + size: 0.0, + font: Font::TimesRoman, + color: Rgb { + red: 0.5, + green: 1.0, + blue: 0.0, + }, + })], + footer_start: 1, + }; + let expected = "q\nBT\n0.5 1 0 rg\n/F3 24 Tf\n24 TL\n\ + 1 0 0 1 0.30000000000000004 0.00000009999999999999999 Tm\n\ + <612020202062> Tj\nT*\n<6364> Tj\nT*\n<65> Tj\nT*\nET\nQ\n"; + assert_eq!(String::from_utf8(page_content(&page)).unwrap(), expected); +}