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.
367 lines
13 KiB
Rust
367 lines
13 KiB
Rust
//! 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/<report>.<locale>.<mode>.pdf` (written
|
|
//! by pdf-lib from the calls the O1 log `goldens/r4/draw.<report>.<locale>.<mode>.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-<random>`, `/Image-<random>`)
|
|
//! 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<u8> {
|
|
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<PageLog> {
|
|
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<DrawOp> = 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<Vec<u8>, 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<u8> {
|
|
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<u8>, 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 `/<from> ` replaced by `/<to> `.
|
|
fn rename(content: &[u8], from: &[u8], to: &str) -> Vec<u8> {
|
|
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<ObjectId> {
|
|
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>` (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);
|
|
}
|