feat(domain): R4.5b/c serialiser, export driver, self-check, from_envelope; O2 parity suite

objects.rs (own minimal writer: numbered objects, streams, classic xref, pdf-lib's string
forms; pdf-writer not used: every number spelt through numberToString, no f32), serialize.rs
(catalog with names tree and AF, pages, 3 Type1 WinAnsi fonts, image XObjects with the
DeviceGray SMask, Link annotations with hex ASCII URIs, Info with Producer/Creator
Faunosteo, the Flate attachment with Params and Filespec), export.rs (checks → envelope
as JSON.stringify(_, null, 2) → layout → file → self-check through the R4 reader;
export_from_envelope). cargo pdf_serialize against the 14 R3 PDFs: content streams equal
after resource-name resolution, image planes and SMasks equal, links equal, Info equal
but Producer/Creator, attachment bytes equal (store path: values equal, D-R11 order);
qpdf --check clean, pdfdetach lists the attachment, poppler 150 dpi 0 pixel differs on
14 runs; R4 PDF → Rust import = R3 PDF import, re-export from the envelope byte-identical;
R4 output digests frozen in goldens/r4/pdf.*.sha256.
This commit is contained in:
bchanot committed 2026-10-18 23:10:18 +02:00
1 parent d7984449ed
commit 4d45de5745
21 files changed
+1381 -2

No files matched your search

@@ -0,0 +1 @@
ecc0d4aea358620e160cbb22d6bf4e8a426dab3efac9f3806dd657b3dea516e7
@@ -0,0 +1 @@
4f70895ae318f8f595c9978dd654cefda8f9efe10b35ea8e50d37617a9ea4332
@@ -0,0 +1 @@
b8c5492b6a09bf7caeef7a299d410e1b193272ece57268d672e55da21f204d73
@@ -0,0 +1 @@
7afcca9d929fc3b0d04e7e3cfda0aaa3999f4dd784910b91d2aafdcae925a778
@@ -0,0 +1 @@
0540d6a281bbec25478cc9b956b8ba9f5ae9f8f6c2561a869b0fed0b0ddb0e38
@@ -0,0 +1 @@
8b42a89904f2b5544be5ecb834d8c15203560264388f95fab159bb5ec9039e5b
@@ -0,0 +1 @@
c1710db98a3e3265dc5097bbea02996f489c81ed1f29e2bd6608ca4010d328b7
@@ -0,0 +1 @@
6a77c18ca8d00ea1d063cd5f7e3367d76654b765ad1cbfedddba3d038c5aba08
@@ -0,0 +1 @@
ace38ed895694f4f86011f28892846ac41829d15bce485f82cbf29b4cd586715
@@ -0,0 +1 @@
57bcac416cce264a09e221d8ece8dd8ebfcb7244b3feae6659a4c95a99549383
@@ -0,0 +1 @@
14dc9f70873b23ec53ad0081a33942c029298879d14b761c4b53190298e1cb81
@@ -0,0 +1 @@
5b96a835158b9783c84d2c71457da32a79102202c1956ce958610d6b54304b58
@@ -0,0 +1 @@
82c3e716e275bc3930249f8f7363ea135efe8f35f8deeffd090d9de1ca6dbb6b
@@ -0,0 +1 @@
300d514382483b9cf485a02643d6fd95bfc4dc5e515386ccd8e07a4edbb149a9
+128
View File
@@ -0,0 +1,128 @@
//! `exportReportArchive` and `exportFromEnvelope` of `packages/pdf/src/export.ts` (R4.5):
//! the checks, the envelope, the layout, the file, then the self-check: the file re-read by
//! the R4 reader must carry the envelope it was written with, else `self_check_failed`.
use serde_json::Value;
use super::input::{
CheckedInput, ExportError, ExportErrorKind, ExportInput, ExportWarning, Rasters, check_input,
};
use super::layout::layout;
use super::serialize::{Document, Metadata, export_date, serialize};
use crate::domain::archive::{EnvelopeFormat, EnvelopeVersion1, Locale, ReportArchiveEnvelope};
use crate::json::to_js_pretty;
use crate::pdf::read::MAX_ATTACHMENT_BYTES;
use crate::pdf::read::probe::{ProbeLimits, read_attached_document};
/// What an export gives back: the file, the envelope text it carries and the warnings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportResult {
pub pdf: Vec<u8>,
pub envelope: String,
pub warnings: Vec<ExportWarning>,
}
/// The envelope exactly as the TypeScript built and stringified it (`buildEnvelope`,
/// `JSON.stringify(envelope, null, 2)`), under the attachment cap.
fn envelope_text(input: &CheckedInput) -> Result<String, ExportError> {
let envelope = ReportArchiveEnvelope {
format: EnvelopeFormat,
envelope_version: EnvelopeVersion1,
exported_at: input.exported_at.clone(),
locale: input.locale,
report: input.report.clone(),
sheet_snapshot: input.snapshot.clone(),
branding: input.branding.clone(),
labels: input.labels.clone(),
};
let text = to_js_pretty(&envelope).map_err(|error| ExportError {
kind: ExportErrorKind::SelfCheckFailed,
detail: error.to_string(),
})?;
if text.len() > MAX_ATTACHMENT_BYTES {
return Err(ExportError {
kind: ExportErrorKind::SnapshotTooLarge,
detail: "attachment".to_owned(),
});
}
Ok(text)
}
/// `finish()` re-reads its own output: one scan and one inflation, the attachment's text
/// against the envelope written.
fn self_check(pdf: &[u8], envelope: &str) -> Result<(), ExportError> {
let located =
read_attached_document(pdf, ProbeLimits::PRODUCTION).map_err(|kind| ExportError {
kind: ExportErrorKind::SelfCheckFailed,
detail: kind.as_str().to_owned(),
})?;
if located.text != envelope {
return Err(ExportError {
kind: ExportErrorKind::SelfCheckFailed,
detail: "attachment differs from the envelope".to_owned(),
});
}
Ok(())
}
/// The export, from the checked input and the rasters the caller made.
pub fn export_checked(
input: &CheckedInput,
rasters: &Rasters,
) -> Result<ExportResult, ExportError> {
let envelope = envelope_text(input)?;
let date = export_date(&input.exported_at)?;
let drawn = layout(input, rasters)?;
let labels = input.labels();
let title = format!(
"{}{}",
labels.pdf_title_suffix_bilan, input.report.animal.name
);
let document = Document {
log: &drawn.log,
envelope: envelope.as_bytes(),
metadata: Metadata {
title: &title,
author: &input.report.practitioner.name,
subject: &labels.pdf_subject,
date: &date,
},
};
let pdf = serialize(&document)?;
self_check(&pdf, &envelope)?;
Ok(ExportResult {
pdf,
envelope,
warnings: drawn.warnings,
})
}
/// `exportReportArchive`: every check, then the file.
pub fn export_pdf(input: ExportInput, rasters: &Rasters) -> Result<ExportResult, ExportError> {
let checked = check_input(input)?;
export_checked(&checked, rasters)
}
/// `exportFromEnvelope`: an archive re-exported from its own snapshot, branding and labels,
/// with the caller's locale and export time; no sheet, no network, no configuration.
pub fn export_from_envelope(
envelope: &Value,
locale: Locale,
exported_at: &str,
rasters: &Rasters,
) -> Result<ExportResult, ExportError> {
let field = |key: &str| envelope.get(key).cloned().unwrap_or(Value::Null);
let report = serde_json::from_value(field("report")).map_err(|error| ExportError {
kind: ExportErrorKind::InvalidInput,
detail: format!("report: {error}"),
})?;
let input = ExportInput {
report,
sheet_snapshot: field("sheetSnapshot"),
branding: field("branding"),
labels: field("labels"),
locale,
exported_at: exported_at.to_owned(),
};
export_pdf(input, rasters)
}
+7
View File
@@ -51,6 +51,11 @@ pub enum ExportErrorKind {
TooManyPages,
/// The branding or the label set fails its schema (URLs included).
BrandingInvalid,
/// R4: the file re-read does not carry the envelope it was written with, or an image
/// the layout sized could not be decoded (today's export alert, `self_check_failed`).
SelfCheckFailed,
/// R4: `exportedAt` is not an ISO UTC timestamp (the application always sends one).
InvalidInput,
}
impl ExportErrorKind {
@@ -61,6 +66,8 @@ impl ExportErrorKind {
Self::SheetMismatch => "sheet_mismatch",
Self::TooManyPages => "too_many_pages",
Self::BrandingInvalid => "branding_invalid",
Self::SelfCheckFailed => "self_check_failed",
Self::InvalidInput => "invalid_input",
}
}
}
+4 -1
View File
@@ -90,7 +90,10 @@ impl<'a> Layout<'a> {
) -> Option<Image> {
let asset = asset?;
let image = match asset {
ImageAsset::Png(png) => png::base64_bytes(&png.base64).and_then(|b| self.embed(&b)),
// `embedImage`: the base64 PNG passes the gate (`pngIssue`) before any decode.
ImageAsset::Png(png) => png::base64_bytes(&png.base64)
.filter(|bytes| super::png::png_issue(bytes).is_none())
.and_then(|b| self.embed(&b)),
ImageAsset::Svg(_) => raster.and_then(|bytes| self.embed(bytes)),
};
if image.is_none() {
+3
View File
@@ -10,7 +10,10 @@ pub mod afm_data;
pub mod content;
pub mod dates;
pub mod draw_log;
pub mod export;
pub mod input;
pub mod layout;
pub mod objects;
pub mod png;
pub mod serialize;
pub mod text;
+190
View File
@@ -0,0 +1,190 @@
//! A minimal PDF object writer (R4.5): numbered objects, streams, a classic xref table and
//! the trailer, with the string forms pdf-lib used (UTF-16BE hex with a BOM for text,
//! literal dates `D:YYYYMMDDHHmmSSZ`, hex ASCII for URIs). pdf-writer was planned here; a
//! writer of this size (the document holds a dozen object kinds) spells every number through
//! `numberToString` and keeps the dictionaries under our control, so the B5 precision
//! difference the plan allowed never arises and no dependency enters the module.
use super::content::number_to_string;
/// The bytes of one indirect object's body (a dictionary, or a stream with its dictionary).
pub struct Objects {
bodies: Vec<Vec<u8>>,
}
/// `n 0 R`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Ref(pub usize);
impl Ref {
pub fn text(self) -> String {
format!("{} 0 R", self.0)
}
}
impl Objects {
pub fn new() -> Self {
Self { bodies: Vec::new() }
}
/// Reserves the next object number; its body comes later with [`Objects::set`].
pub fn reserve(&mut self) -> Ref {
self.bodies.push(Vec::new());
Ref(self.bodies.len())
}
pub fn set(&mut self, at: Ref, body: Vec<u8>) {
if let Some(slot) = self.bodies.get_mut(at.0 - 1) {
*slot = body;
}
}
pub fn add(&mut self, body: Vec<u8>) -> Ref {
self.bodies.push(body);
Ref(self.bodies.len())
}
/// A stream object: the dictionary entries (without `/Length`) and the data.
pub fn add_stream(&mut self, entries: &[(&str, String)], data: &[u8]) -> Ref {
self.bodies.push(stream_body(entries, data));
Ref(self.bodies.len())
}
/// The whole file: header, objects in number order, xref, trailer.
pub fn finish(self, root: Ref, info: Ref) -> Vec<u8> {
let mut out =
Vec::with_capacity(self.bodies.iter().map(|b| b.len() + 24).sum::<usize>() + 256);
out.extend_from_slice(b"%PDF-1.7\n%\xE2\xE3\xCF\xD3\n\n");
let mut offsets = Vec::with_capacity(self.bodies.len());
for (i, body) in self.bodies.iter().enumerate() {
offsets.push(out.len());
out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
out.extend_from_slice(body);
out.extend_from_slice(b"\nendobj\n\n");
}
let xref = out.len();
out.extend_from_slice(
format!("xref\n0 {}\n0000000000 65535 f \n", offsets.len() + 1).as_bytes(),
);
for offset in offsets {
out.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
}
let trailer = format!(
"trailer\n<< /Size {} /Root {} /Info {} >>\nstartxref\n{xref}\n%%EOF\n",
self.bodies.len() + 1,
root.text(),
info.text()
);
out.extend_from_slice(trailer.as_bytes());
out
}
}
impl Default for Objects {
fn default() -> Self {
Self::new()
}
}
fn stream_body(entries: &[(&str, String)], data: &[u8]) -> Vec<u8> {
let mut body = Vec::with_capacity(data.len() + 128);
let mut head = String::from("<<");
for (key, value) in entries {
head.push_str(&format!(" /{key} {value}"));
}
head.push_str(&format!(" /Length {} >>\nstream\n", data.len()));
body.extend_from_slice(head.as_bytes());
body.extend_from_slice(data);
body.extend_from_slice(b"\nendstream");
body
}
/// `<< /K v … >>` from entries already spelt as PDF syntax.
pub fn dict(entries: &[(&str, String)]) -> String {
let mut out = String::from("<<");
for (key, value) in entries {
out.push_str(&format!(" /{key} {value}"));
}
out.push_str(" >>");
out
}
/// A name object: `#xx` for the bytes a name cannot carry bare (`/application#2fjson`).
pub fn name(text: &str) -> String {
let mut out = String::from("/");
for byte in text.bytes() {
let bare = byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'+');
if bare {
out.push(char::from(byte));
} else {
out.push_str(&format!("#{byte:02x}"));
}
}
out
}
/// `PDFHexString.fromText`: UTF-16BE with a BOM, lowercase hex.
pub fn hex_text(text: &str) -> String {
let mut out = String::from("<feff");
for unit in text.encode_utf16() {
out.push_str(&format!("{unit:04x}"));
}
out.push('>');
out
}
/// The hex form of ASCII bytes, one byte per character (`linkAnnotation.ts` `asciiHexOf`).
pub fn hex_ascii(text: &str) -> String {
let mut out = String::from("<");
for byte in text.bytes() {
out.push_str(&format!("{byte:02x}"));
}
out.push('>');
out
}
/// A literal string, the three delimiters escaped (pdf-lib did not escape; a fixed ASCII
/// name never needs it, the escape only guards the form).
pub fn literal(text: &str) -> String {
let mut out = String::from("(");
for c in text.chars() {
if matches!(c, '(' | ')' | '\\') {
out.push('\\');
}
out.push(c);
}
out.push(')');
out
}
/// An array of numbers in pdf-lib's spelling.
pub fn numbers(values: &[f64]) -> String {
let items: Vec<String> = values.iter().map(|v| number_to_string(*v)).collect();
format!("[{}]", items.join(" "))
}
/// `PDFString.fromDate`: `(D:YYYYMMDDHHmmSSZ)` from the UTC fields of an ISO timestamp
/// (`YYYY-MM-DDTHH:MM:SS[.sss]Z`, what `Date.prototype.toISOString` writes); `None` when
/// the text is not one.
pub fn pdf_date(iso: &str) -> Option<String> {
let bytes = iso.as_bytes();
let digits = |range: std::ops::Range<usize>| {
bytes
.get(range.clone())
.filter(|part| part.iter().all(u8::is_ascii_digit))
.map(|_| &iso[range])
};
let (year, month, day) = (digits(0..4)?, digits(5..7)?, digits(8..10)?);
let (hours, minutes, seconds) = (digits(11..13)?, digits(14..16)?, digits(17..19)?);
let separators = bytes.get(4) == Some(&b'-')
&& bytes.get(7) == Some(&b'-')
&& bytes.get(10) == Some(&b'T')
&& bytes.get(13) == Some(&b':')
&& bytes.get(16) == Some(&b':');
let tail = &iso[19..];
let tail_ok = tail == "Z"
|| (tail.starts_with('.')
&& tail.ends_with('Z')
&& tail[1..tail.len() - 1].bytes().all(|b| b.is_ascii_digit()));
(separators && tail_ok).then(|| format!("(D:{year}{month}{day}{hours}{minutes}{seconds}Z)"))
}
+282
View File
@@ -0,0 +1,282 @@
//! The PDF file from a draw log (R4.5, plan §B.1.3): the objects pdf-lib's `save()` wrote
//! for the Step 6 engine, in our own order and names (B2, B3 of §B.2.3): catalog with the
//! names tree and `/AF`, the page tree, three Type1 fonts with `WinAnsiEncoding`, one image
//! XObject per embed (DeviceRGB 8 bits, a DeviceGray `/SMask` with `/Decode [0 1]` when an
//! alpha is below 255), Link annotations (`/Border [0 0 0]`, a URI action with the URL as hex
//! ASCII), the Info dictionary (Producer and Creator `Faunosteo`, A4; the rest in pdf-lib's
//! forms), the attachment (a Flate `EmbeddedFile` with `Params`, the Filespec, the names
//! tree entry) and a classic xref; no object stream, so the scanner reads the file.
use miniz_oxide::deflate::compress_to_vec_zlib;
use super::content::{font_resource_name, image_resource_name, page_content};
use super::draw_log::{DrawLog, Embed, PAGE_HEIGHT, PAGE_WIDTH};
use super::input::{ExportError, ExportErrorKind};
use super::objects::{Objects, Ref, dict, hex_ascii, hex_text, literal, name, numbers, pdf_date};
use super::png::{DecodedPng, decode_png};
use super::text::Font;
/// pako's default level, what `context.flateStream` used.
const DEFLATE_LEVEL: u8 = 6;
pub const ATTACHMENT_FILENAME: &str = "osteo-report.json";
pub const ATTACHMENT_DESCRIPTION: &str = "Faunosteo report archive v1";
pub const PDF_KEYWORD: &str = "faunosteo-archive/1";
/// A4, Producer and Creator (owner arbitration A4).
pub const PRODUCER: &str = "Faunosteo";
/// The document information the layout does not know.
pub struct Metadata<'a> {
pub title: &'a str,
pub author: &'a str,
pub subject: &'a str,
/// `D:` form of the export time, every date of the file.
pub date: &'a str,
}
/// What `serialize` writes besides the pages: the envelope bytes and the metadata.
pub struct Document<'a> {
pub log: &'a DrawLog,
pub envelope: &'a [u8],
pub metadata: Metadata<'a>,
}
fn deflate(data: &[u8]) -> Vec<u8> {
compress_to_vec_zlib(data, DEFLATE_LEVEL)
}
/// The decoded planes of an embed; a PNG the decoder refuses is an internal inconsistency
/// (the layout read its header), reported as `self_check_failed` rather than drawn wrong.
fn planes(embed: &Embed) -> Result<DecodedPng, ExportError> {
decode_png(&embed.png).map_err(|_| ExportError {
kind: ExportErrorKind::SelfCheckFailed,
detail: "image decode".to_owned(),
})
}
/// One image XObject (and its SMask when the alpha plane is kept), PNG and planes dropped
/// as soon as written.
fn write_image(objects: &mut Objects, embed: &Embed) -> Result<Ref, ExportError> {
let decoded = planes(embed)?;
let size = [
("Width", decoded.width.to_string()),
("Height", decoded.height.to_string()),
];
let smask = decoded.alpha.as_ref().map(|alpha| {
let mut entries = vec![
("Type", "/XObject".to_owned()),
("Subtype", "/Image".to_owned()),
("BitsPerComponent", "8".to_owned()),
];
entries.extend(size.iter().cloned());
entries.push(("ColorSpace", "/DeviceGray".to_owned()));
entries.push(("Decode", "[0 1]".to_owned()));
entries.push(("Filter", "/FlateDecode".to_owned()));
objects.add_stream(&entries, &deflate(alpha))
});
let mut entries = vec![
("Type", "/XObject".to_owned()),
("Subtype", "/Image".to_owned()),
("BitsPerComponent", "8".to_owned()),
];
entries.extend(size.iter().cloned());
entries.push(("ColorSpace", "/DeviceRGB".to_owned()));
if let Some(smask) = smask {
entries.push(("SMask", smask.text()));
}
entries.push(("Filter", "/FlateDecode".to_owned()));
Ok(objects.add_stream(&entries, &deflate(&decoded.rgb)))
}
fn write_fonts(objects: &mut Objects) -> [(Font, Ref); 3] {
let fonts = [Font::Helvetica, Font::HelveticaBold, Font::TimesRoman];
fonts.map(|font| {
let body = dict(&[
("Type", "/Font".to_owned()),
("Subtype", "/Type1".to_owned()),
("BaseFont", format!("/{}", font.as_str())),
("Encoding", "/WinAnsiEncoding".to_owned()),
]);
(font, objects.add(body.into_bytes()))
})
}
fn resources(fonts: &[(Font, Ref); 3], images: &[Ref]) -> String {
let font_entries: Vec<String> = fonts
.iter()
.map(|(font, r)| format!("/{} {}", font_resource_name(*font), r.text()))
.collect();
let image_entries: Vec<String> = images
.iter()
.enumerate()
.map(|(i, r)| format!("/{} {}", image_resource_name(i), r.text()))
.collect();
format!(
"<< /Font << {} >> /XObject << {} >> >>",
font_entries.join(" "),
image_entries.join(" ")
)
}
/// One Link annotation (`linkAnnotation.ts`): the rect as drawn, a URI action in hex ASCII.
fn write_link(objects: &mut Objects, rect: [f64; 4], uri: &str) -> Ref {
let action = dict(&[
("Type", "/Action".to_owned()),
("S", "/URI".to_owned()),
("URI", hex_ascii(uri)),
]);
objects.add(
dict(&[
("Type", "/Annot".to_owned()),
("Subtype", "/Link".to_owned()),
("Rect", numbers(&rect)),
("Border", "[0 0 0]".to_owned()),
("A", action),
])
.into_bytes(),
)
}
struct Shared<'a> {
pages: Ref,
fonts: &'a [(Font, Ref); 3],
images: &'a [Ref],
}
fn write_page(
objects: &mut Objects,
doc: &Document<'_>,
index: usize,
shared: &Shared<'_>,
) -> Option<Ref> {
let page = doc.log.pages.get(index)?;
let content = objects.add_stream(
&[("Filter", "/FlateDecode".to_owned())],
&deflate(&page_content(page)),
);
let annots: Vec<String> = doc
.log
.links
.iter()
.filter(|link| link.page == index)
.map(|link| write_link(objects, link.rect, &link.uri).text())
.collect();
let mut entries = vec![
("Type", "/Page".to_owned()),
("Parent", shared.pages.text()),
("MediaBox", numbers(&[0.0, 0.0, PAGE_WIDTH, PAGE_HEIGHT])),
("Contents", content.text()),
("Resources", resources(shared.fonts, shared.images)),
];
if !annots.is_empty() {
entries.push(("Annots", format!("[{}]", annots.join(" "))));
}
Some(objects.add(dict(&entries).into_bytes()))
}
fn write_info(objects: &mut Objects, metadata: &Metadata<'_>) -> Ref {
objects.add(
dict(&[
("Title", hex_text(metadata.title)),
("Author", hex_text(metadata.author)),
("Subject", hex_text(metadata.subject)),
("Keywords", hex_text(PDF_KEYWORD)),
("Producer", hex_text(PRODUCER)),
("Creator", hex_text(PRODUCER)),
("CreationDate", metadata.date.to_owned()),
("ModDate", metadata.date.to_owned()),
])
.into_bytes(),
)
}
/// The attachment (`attachment/write.ts`, pdf-lib `FileEmbedder`): the Flate stream with
/// its `Params`, then the Filespec in clear with the name in both forms.
fn write_attachment(objects: &mut Objects, doc: &Document<'_>) -> Ref {
let params = dict(&[
("Size", doc.envelope.len().to_string()),
("CreationDate", doc.metadata.date.to_owned()),
("ModDate", doc.metadata.date.to_owned()),
]);
let stream = objects.add_stream(
&[
("Type", "/EmbeddedFile".to_owned()),
("Subtype", name("application/json")),
("Params", params),
("Filter", "/FlateDecode".to_owned()),
],
&deflate(doc.envelope),
);
objects.add(
dict(&[
("Type", "/Filespec".to_owned()),
("F", literal(ATTACHMENT_FILENAME)),
("UF", hex_text(ATTACHMENT_FILENAME)),
("EF", dict(&[("F", stream.text())])),
("Desc", hex_text(ATTACHMENT_DESCRIPTION)),
("AFRelationship", "/Data".to_owned()),
])
.into_bytes(),
)
}
/// The export time in the `D:` form, or the closed refusal of a timestamp that is not ISO.
pub fn export_date(exported_at: &str) -> Result<String, ExportError> {
pdf_date(exported_at).ok_or_else(|| ExportError {
kind: ExportErrorKind::InvalidInput,
detail: "exportedAt".to_owned(),
})
}
/// The whole file. Object order: catalog, pages, info, fonts, then per image its XObject,
/// then per page its content, links and dictionary, then the attachment.
pub fn serialize(doc: &Document<'_>) -> Result<Vec<u8>, ExportError> {
let mut objects = Objects::new();
let catalog = objects.reserve();
let pages = objects.reserve();
let info = write_info(&mut objects, &doc.metadata);
let fonts = write_fonts(&mut objects);
let mut images = Vec::with_capacity(doc.log.embeds.len());
for embed in &doc.log.embeds {
images.push(write_image(&mut objects, embed)?);
}
let shared = Shared {
pages,
fonts: &fonts,
images: &images,
};
let mut kids = Vec::with_capacity(doc.log.pages.len());
for index in 0..doc.log.pages.len() {
if let Some(page) = write_page(&mut objects, doc, index, &shared) {
kids.push(page.text());
}
}
let filespec = write_attachment(&mut objects, doc);
objects.set(
pages,
dict(&[
("Type", "/Pages".to_owned()),
("Count", kids.len().to_string()),
("Kids", format!("[{}]", kids.join(" "))),
])
.into_bytes(),
);
let names = dict(&[(
"EmbeddedFiles",
dict(&[(
"Names",
format!("[{} {}]", hex_text(ATTACHMENT_FILENAME), filespec.text()),
)]),
)]);
objects.set(
catalog,
dict(&[
("Type", "/Catalog".to_owned()),
("Pages", pages.text()),
("Names", names),
("AF", format!("[{}]", filespec.text())),
])
.into_bytes(),
);
Ok(objects.finish(catalog, info))
}
+753 -1
View File
@@ -1 +1,753 @@
//! R4.5 placeholder, written by its track.
//! R4.5: the Rust PDF against the R3 build's PDFs (O2, `apps/app/tests/fixtures/pdf/r3/`),
//! with the difference classes of plan §B.2.3 applied: content streams compared after the
//! resource names are resolved (B3), image planes decompressed, links and Info by value,
//! the attachment byte for byte (the store-path export differs by the D-R11 report order
//! only). Then the digests of the R4 outputs (`goldens/r4/pdf.*.sha256`, written once with
//! `MAKE_R4_ORACLES=1`), the external checks (`qpdf --check`, `pdfdetach -list`, poppler at
//! 150 dpi against the R3 render) when the tools are installed, and the restoration round
//! trip through the R4 reader.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
mod common;
use std::collections::BTreeMap;
use std::process::Command;
use common::r4::{repo_root, sha256_hex};
use faunosteo_core::domain::archive::Locale;
use faunosteo_core::domain::js::parse_json;
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet};
use faunosteo_core::json::to_js_compact;
use faunosteo_core::pdf::import::import_pdf;
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
use faunosteo_core::pdf::write::export::{export_from_envelope, export_pdf};
use faunosteo_core::pdf::write::input::{ExportInput, Rasters};
use lopdf::{Dictionary, Document, Object, ObjectId};
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 an R3 PDF carries, as JSON.
fn envelope_of(pdf: &[u8]) -> Value {
let located = read_attached_document(pdf, ProbeLimits::UNBOUNDED).unwrap();
parse_json(&located.text).unwrap()
}
fn input_from(envelope: &Value) -> ExportInput {
serde_json::from_value(json!({
"report": envelope["report"],
"sheetSnapshot": envelope["sheetSnapshot"],
"branding": envelope["branding"],
"labels": envelope["labels"],
"locale": envelope["locale"],
"exportedAt": envelope["exportedAt"],
}))
.unwrap()
}
fn stub_rasters(input: &ExportInput, png: &[u8]) -> Rasters {
let mut rasters = Rasters {
header_logo: Some(png.to_vec()),
footer_logo: Some(png.to_vec()),
powered_by: Some(png.to_vec()),
partner: Some(png.to_vec()),
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.to_vec());
}
}
rasters
}
/// One O2 run: the R3 PDF and the Rust export of the same input and rasters.
struct Run {
name: String,
r3: Vec<u8>,
r4: Vec<u8>,
envelope: String,
}
fn stub_run(report: &str, locale: &str, mode: &str) -> Run {
let r3 = fixture(&format!("pdf/r3/{report}.{locale}.{mode}.pdf"));
let input = input_from(&envelope_of(&r3));
let png = base64(if mode == "logos" {
TWO_BY_ONE_PNG
} else {
ONE_PX_PNG
});
let rasters = stub_rasters(&input, &png);
let result = export_pdf(input, &rasters).unwrap();
assert!(result.warnings.is_empty());
Run {
name: format!("{report}.{locale}.{mode}"),
r3,
r4: result.pdf,
envelope: result.envelope,
}
}
/// The real-raster run: v2full's first observation, the PNGs the browser made (manifest).
fn real_run() -> Run {
let r3 = fixture("pdf/r3/real/v2full-obs1.fr.logos.pdf");
let input = input_from(&envelope_of(&r3));
let manifest: Value = serde_json::from_slice(&fixture("pdf/r3/real/manifest.json")).unwrap();
let mut rasters = stub_rasters(&input, &base64(TWO_BY_ONE_PNG));
for call in manifest["calls"].as_array().unwrap() {
let png = fixture(&format!("pdf/r3/real/{}", call["png"].as_str().unwrap()));
assert_eq!(sha256_hex(&png), call["pngSha256"]);
let subject = &call["subject"];
if subject["kind"] == "logo" {
assert_eq!(subject["slot"], "partner");
rasters.partner = Some(png);
} else {
let key = (
subject["observationId"].as_str().unwrap().to_owned(),
subject["viewId"].as_str().unwrap().to_owned(),
);
rasters.views.insert(key, png);
}
}
let result = export_pdf(input, &rasters).unwrap();
assert!(result.warnings.is_empty());
Run {
name: "real.v2full-obs1.fr.logos".to_owned(),
r3,
r4: result.pdf,
envelope: result.envelope,
}
}
/// The store-path run: the report as the editor's store ordered it (D-R11: the attachment's
/// report is in contract order in R4, values identical).
fn store_path_run() -> Run {
let r3 = fixture("pdf/r3/store-path.fr.nologos.pdf");
let envelope = envelope_of(&r3);
let mut input = input_from(&envelope);
let report: Value = serde_json::from_slice(&fixture("pdf/r3/store-path.report.json")).unwrap();
input.report = serde_json::from_value(report).unwrap();
let rasters = stub_rasters(&input, &base64(ONE_PX_PNG));
let result = export_pdf(input, &rasters).unwrap();
Run {
name: "store-path.fr.nologos".to_owned(),
r3,
r4: result.pdf,
envelope: result.envelope,
}
}
fn all_runs() -> Vec<Run> {
let mut runs = Vec::new();
for report in REPORTS {
for locale in LOCALES {
for mode in MODES {
runs.push(stub_run(report, locale, mode));
}
}
}
runs.push(real_run());
runs.push(store_path_run());
runs
}
// ---------------------------------------------------------------------------
// Reading both files
fn load(bytes: &[u8]) -> Document {
Document::load_mem(bytes).unwrap()
}
fn pages(doc: &Document) -> Vec<ObjectId> {
doc.get_pages().into_values().collect()
}
/// Every image XObject of the document, by object number: the embed order in both files
/// (pdf-lib registered the images as it embedded them; ours are written in embed order).
fn image_order(doc: &Document) -> BTreeMap<ObjectId, usize> {
let mut ids: Vec<ObjectId> = doc
.objects
.iter()
.filter(|(_, object)| {
object.as_stream().is_ok_and(|s| {
s.dict.get(b"Subtype").and_then(Object::as_name).ok() == Some(b"Image")
&& s.dict.get(b"ColorSpace").and_then(Object::as_name).ok()
== Some(b"DeviceRGB")
})
})
.map(|(id, _)| *id)
.collect();
ids.sort();
ids.into_iter().enumerate().map(|(k, id)| (id, k)).collect()
}
/// `name → canonical token` for a page's fonts (by BaseFont) and images (by embed order).
fn resource_map(doc: &Document, page: ObjectId) -> BTreeMap<Vec<u8>, String> {
let (resources, _) = doc.get_page_resources(page).unwrap();
let resources = resources.unwrap();
let order = image_order(doc);
let mut map = BTreeMap::new();
if let Ok(fonts) = resources.get(b"Font").and_then(Object::as_dict) {
for (name, value) in fonts.iter() {
let font = doc.get_dictionary(value.as_reference().unwrap()).unwrap();
let base = String::from_utf8_lossy(font.get(b"BaseFont").unwrap().as_name().unwrap())
.into_owned();
map.insert(name.clone(), format!("/Font:{base}"));
}
}
if let Ok(xobjects) = resources.get(b"XObject").and_then(Object::as_dict) {
for (name, value) in xobjects.iter() {
let k = order[&value.as_reference().unwrap()];
map.insert(name.clone(), format!("/Image:{k}"));
}
}
map
}
/// The content stream with every resource name replaced by its canonical token.
fn canonical_content(doc: &Document, page: ObjectId) -> Vec<u8> {
let content = doc.get_page_content(page);
let map = resource_map(doc, page);
let mut out = Vec::with_capacity(content.len());
let mut i = 0;
while i < content.len() {
if content[i] == b'/' {
let end = content[i..]
.iter()
.position(|b| b.is_ascii_whitespace())
.map_or(content.len(), |p| i + p);
let name = &content[i + 1..end];
if let Some(token) = map.get(name) {
out.extend_from_slice(token.as_bytes());
i = end;
continue;
}
}
out.push(content[i]);
i += 1;
}
out
}
/// Every image XObject of the document in embed order: dictionary facts and planes.
fn images(doc: &Document) -> Vec<Value> {
let mut xobjects: Vec<(ObjectId, &Dictionary)> = Vec::new();
for (id, object) in doc.objects.iter() {
if let Ok(stream) = object.as_stream()
&& stream.dict.get(b"Subtype").and_then(Object::as_name).ok() == Some(b"Image")
&& stream
.dict
.get(b"ColorSpace")
.and_then(Object::as_name)
.ok()
== Some(b"DeviceRGB")
{
xobjects.push((*id, &stream.dict));
}
}
xobjects.sort_by_key(|(id, _)| *id);
xobjects
.into_iter()
.map(|(id, dict)| {
let stream = doc.get_object(id).unwrap().as_stream().unwrap();
let rgb = stream.decompressed_content().unwrap();
let smask = dict.get(b"SMask").ok().map(|r| {
let mask = doc.get_object(r.as_reference().unwrap()).unwrap().as_stream().unwrap();
json!({
"colorSpace": String::from_utf8_lossy(mask.dict.get(b"ColorSpace").unwrap().as_name().unwrap()),
"decode": format!("{:?}", mask.dict.get(b"Decode").unwrap()),
"bits": mask.dict.get(b"BitsPerComponent").unwrap().as_i64().unwrap(),
"width": mask.dict.get(b"Width").unwrap().as_i64().unwrap(),
"height": mask.dict.get(b"Height").unwrap().as_i64().unwrap(),
"sha256": sha256_hex(&mask.decompressed_content().unwrap()),
})
});
json!({
"width": dict.get(b"Width").unwrap().as_i64().unwrap(),
"height": dict.get(b"Height").unwrap().as_i64().unwrap(),
"bits": dict.get(b"BitsPerComponent").unwrap().as_i64().unwrap(),
"rgbSha256": sha256_hex(&rgb),
"smask": smask,
})
})
.collect()
}
/// `[x1 y1 x2 y2]` and URI of every Link annotation, per page, from the raw bytes (the
/// numbers' spelling is compared, lopdf would read them as f32).
fn links(doc: &Document, bytes: &[u8]) -> Vec<Value> {
let text = String::from_utf8_lossy(bytes);
let mut out = Vec::new();
for (p, page) in pages(doc).iter().enumerate() {
for annot in doc.get_page_annotations(*page).unwrap() {
let uri = annot
.get(b"A")
.unwrap()
.as_dict()
.unwrap()
.get(b"URI")
.unwrap()
.as_str()
.unwrap()
.to_vec();
let rect_f32: Vec<f32> = annot
.get(b"Rect")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|o| o.as_float().unwrap())
.collect();
out.push(
json!({ "page": p, "rect": rect_f32, "uri": String::from_utf8(uri).unwrap() }),
);
}
}
// The spellings: every `/Rect [...]` of the file in order.
let mut rects = Vec::new();
let mut rest = text.as_ref();
while let Some(at) = rest.find("/Rect") {
let after = &rest[at + 5..];
let open = after.find('[').unwrap();
let close = after.find(']').unwrap();
rects.push(
after[open + 1..close]
.split_whitespace()
.collect::<Vec<_>>()
.join(" "),
);
rest = &after[close..];
}
// Object order is free (B2): the spellings are compared as a sorted multiset.
rects.sort();
out.push(json!({ "rectSpellings": rects }));
out
}
fn info(doc: &Document) -> BTreeMap<String, Vec<u8>> {
let id = doc.trailer.get(b"Info").unwrap().as_reference().unwrap();
let dict = doc.get_dictionary(id).unwrap();
dict.iter()
.map(|(k, v)| {
(
String::from_utf8_lossy(k).into_owned(),
v.as_str().unwrap().to_vec(),
)
})
.collect()
}
/// The embedded file stream and the Filespec, by walking the catalog's names tree.
fn attachment(doc: &Document) -> (Dictionary, Vec<u8>, Dictionary) {
let root = doc.trailer.get(b"Root").unwrap().as_reference().unwrap();
let catalog = doc.get_dictionary(root).unwrap();
let names = catalog
.get(b"Names")
.unwrap()
.as_dict()
.unwrap()
.get(b"EmbeddedFiles")
.unwrap()
.as_dict()
.unwrap()
.get(b"Names")
.unwrap()
.as_array()
.unwrap();
let filespec = doc
.get_dictionary(names[1].as_reference().unwrap())
.unwrap()
.clone();
let ef = filespec
.get(b"EF")
.unwrap()
.as_dict()
.unwrap()
.get(b"F")
.unwrap()
.as_reference()
.unwrap();
let stream = doc.get_object(ef).unwrap().as_stream().unwrap();
assert_eq!(
catalog.get(b"AF").unwrap().as_array().unwrap()[0]
.as_reference()
.unwrap(),
names[1].as_reference().unwrap()
);
(
stream.dict.clone(),
stream.decompressed_content().unwrap(),
filespec,
)
}
fn dict_facts(dict: &Dictionary, skip: &[&[u8]]) -> BTreeMap<String, String> {
dict.iter()
.filter(|(k, _)| !skip.contains(&k.as_slice()))
.map(|(k, v)| (String::from_utf8_lossy(k).into_owned(), format!("{v:?}")))
.collect()
}
// ---------------------------------------------------------------------------
// Tests
#[test]
fn o2_streams_images_links_info_and_attachment_match_the_r3_build() {
let mut mismatches = Vec::new();
for run in all_runs() {
let (r3, r4) = (load(&run.r3), load(&run.r4));
let (p3, p4) = (pages(&r3), pages(&r4));
if p3.len() != p4.len() {
mismatches.push(format!(
"{}: {} pages, expected {}",
run.name,
p4.len(),
p3.len()
));
continue;
}
for (i, (a, b)) in p3.iter().zip(&p4).enumerate() {
let (c3, c4) = (canonical_content(&r3, *a), canonical_content(&r4, *b));
if c3 != c4 {
let at = c3
.iter()
.zip(&c4)
.position(|(x, y)| x != y)
.unwrap_or(c3.len().min(c4.len()));
mismatches.push(format!(
"{} page {i}: content differs at byte {at}: r3 …{:?}… r4 …{:?}…",
run.name,
String::from_utf8_lossy(&c3[at.saturating_sub(40)..(at + 40).min(c3.len())]),
String::from_utf8_lossy(&c4[at.saturating_sub(40)..(at + 40).min(c4.len())])
));
}
}
let (i3, i4) = (images(&r3), images(&r4));
if i3 != i4 {
mismatches.push(format!(
"{}: images\n r3 {}\n r4 {}",
run.name,
Value::Array(i3),
Value::Array(i4)
));
}
let (l3, l4) = (links(&r3, &run.r3), links(&r4, &run.r4));
if l3 != l4 {
let at = l3
.iter()
.zip(&l4)
.position(|(a, b)| a != b)
.unwrap_or(l3.len().min(l4.len()));
mismatches.push(format!(
"{}: links differ at entry {at} ({} vs {} entries)\n r3 {}\n r4 {}",
run.name,
l3.len(),
l4.len(),
l3.get(at).cloned().unwrap_or(Value::Null),
l4.get(at).cloned().unwrap_or(Value::Null)
));
}
let (mut n3, mut n4) = (info(&r3), info(&r4));
for key in ["Producer", "Creator"] {
assert!(n3.remove(key).is_some(), "{}: r3 has {key}", run.name);
assert_eq!(
n4.remove(key).unwrap(),
"\u{feff}Faunosteo"
.encode_utf16()
.flat_map(u16::to_be_bytes)
.collect::<Vec<u8>>(),
"{}: {key}",
run.name
);
}
if n3 != n4 {
mismatches.push(format!(
"{}: info differs: r3 {:?} r4 {:?}",
run.name,
n3.keys().collect::<Vec<_>>(),
n4
));
}
let (d3, a3, f3) = attachment(&r3);
let (d4, a4, f4) = attachment(&r4);
if run.name.starts_with("store-path") {
// D-R11: the report in contract order; the values identical.
assert_eq!(
to_js_compact(&parse_json(std::str::from_utf8(&a3).unwrap()).unwrap()["report"])
.unwrap()
.len(),
to_js_compact(&parse_json(&run.envelope).unwrap()["report"])
.unwrap()
.len()
);
let (v3, v4) = (
parse_json(std::str::from_utf8(&a3).unwrap()).unwrap(),
parse_json(std::str::from_utf8(&a4).unwrap()).unwrap(),
);
assert_eq!(v3, v4, "{}: attachment values", run.name);
} else if a3 != a4 {
let at = a3
.iter()
.zip(&a4)
.position(|(x, y)| x != y)
.unwrap_or(a3.len().min(a4.len()));
mismatches.push(format!(
"{}: attachment differs at byte {at} of {} / {}: …{}… vs …{}…",
run.name,
a3.len(),
a4.len(),
String::from_utf8_lossy(&a3[at.saturating_sub(60)..(at + 60).min(a3.len())]),
String::from_utf8_lossy(&a4[at.saturating_sub(60)..(at + 60).min(a4.len())])
));
}
assert_eq!(
a4,
run.envelope.as_bytes(),
"{}: the attachment is the envelope",
run.name
);
let skip: &[&[u8]] = &[b"Length", b"Filter"];
let (mut e3, mut e4) = (dict_facts(&d3, skip), dict_facts(&d4, skip));
// Params/Size: the attachment's own length (equal when the bytes are).
if run.name.starts_with("store-path") {
e3.remove("Params");
e4.remove("Params");
}
if e3 != e4 {
mismatches.push(format!(
"{}: embedded file dict r3 {e3:?} r4 {e4:?}",
run.name
));
}
let (s3, s4) = (dict_facts(&f3, &[b"EF"]), dict_facts(&f4, &[b"EF"]));
if s3 != s4 {
mismatches.push(format!("{}: filespec r3 {s3:?} r4 {s4:?}", run.name));
}
}
assert!(
mismatches.is_empty(),
"{} mismatches:\n{}",
mismatches.len(),
mismatches.join("\n")
);
}
/// The digests of the R4 outputs: written once (`MAKE_R4_ORACLES=1`), then asserted; the
/// wasm build must give the same bytes (`packages/core-wasm/tests`).
#[test]
fn r4_output_digests_are_frozen() {
let dir = repo_root().join("apps/app/tests/fixtures/goldens/r4");
let make = std::env::var("MAKE_R4_ORACLES").is_ok_and(|v| v == "1");
for run in all_runs() {
let path = dir.join(format!("pdf.{}.sha256", run.name));
let digest = sha256_hex(&run.r4);
if make {
std::fs::write(&path, format!("{digest}\n")).unwrap();
} else {
let expected = std::fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("{path:?} missing: MAKE_R4_ORACLES=1"));
assert_eq!(expected.trim(), digest, "{}: output changed", run.name);
}
}
}
fn tool(name: &str) -> bool {
Command::new("which")
.arg(name)
.output()
.is_ok_and(|o| o.status.success())
}
/// `qpdf --check` and `pdfdetach -list` on every R4 output; poppler's render at 150 dpi
/// against the R3 PDF's, pixel for pixel (same renderer and fonts for both files).
#[test]
fn external_tools_accept_the_files_and_render_them_like_the_r3_build() {
if !(tool("qpdf") && tool("pdfdetach") && tool("pdftoppm")) {
println!("qpdf / poppler not installed: external checks skipped here");
return;
}
let dir = std::env::temp_dir().join(format!("faunosteo-r4-serialize-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let mut report = Vec::new();
for run in all_runs() {
let r4 = dir.join(format!("{}.r4.pdf", run.name));
let r3 = dir.join(format!("{}.r3.pdf", run.name));
std::fs::write(&r4, &run.r4).unwrap();
std::fs::write(&r3, &run.r3).unwrap();
let check = Command::new("qpdf")
.arg("--check")
.arg(&r4)
.output()
.unwrap();
assert!(
check.status.success(),
"{}: qpdf --check\n{}{}",
run.name,
String::from_utf8_lossy(&check.stdout),
String::from_utf8_lossy(&check.stderr)
);
let list = Command::new("pdfdetach")
.arg("-list")
.arg(&r4)
.output()
.unwrap();
let listed = String::from_utf8_lossy(&list.stdout);
assert!(
listed.contains("osteo-report.json"),
"{}: pdfdetach -list: {listed}",
run.name
);
let render = |pdf: &std::path::Path, stem: &str| {
let out = dir.join(stem);
let status = Command::new("pdftoppm")
.args(["-r", "150"])
.arg(pdf)
.arg(&out)
.status()
.unwrap();
assert!(status.success());
let mut pages: Vec<_> = std::fs::read_dir(&dir)
.unwrap()
.map(|e| e.unwrap().path())
.filter(|p| {
p.file_name()
.unwrap()
.to_string_lossy()
.starts_with(&format!("{stem}-"))
&& p.extension().is_some_and(|e| e == "ppm")
})
.collect();
pages.sort();
pages
.into_iter()
.map(|p| std::fs::read(p).unwrap())
.collect::<Vec<_>>()
};
let (img3, img4) = (
render(&r3, &format!("{}.r3", run.name)),
render(&r4, &format!("{}.r4", run.name)),
);
assert_eq!(img3.len(), img4.len(), "{}: rendered page count", run.name);
let mut differing = 0usize;
let mut total = 0usize;
for (a, b) in img3.iter().zip(&img4) {
assert_eq!(a.len(), b.len(), "{}: render size", run.name);
total += a.len() / 3;
differing += a.chunks(3).zip(b.chunks(3)).filter(|(x, y)| x != y).count();
}
report.push(format!(
"{}: {} pages rendered, {differing} of {total} pixels differ",
run.name,
img3.len()
));
assert_eq!(differing, 0, "{}: poppler render differs", run.name);
}
println!("{}", report.join("\n"));
let _ = std::fs::remove_dir_all(&dir);
}
fn draft_sheet() -> LoadedSheet {
let seed = repo_root().join("supabase/seed/anatomical-sheets/equine/draft");
let read = |name: &str| std::fs::read_to_string(seed.join(name)).unwrap();
let manifest: Value = serde_json::from_str(&read("assets.json")).unwrap();
let mut assets = serde_json::Map::new();
let mut hashes = serde_json::Map::new();
for asset in manifest.as_array().unwrap() {
let id = asset["svg_asset_id"].as_str().unwrap().to_owned();
assets.insert(
id.clone(),
Value::String(read(&format!("assets/{}", asset["file"].as_str().unwrap()))),
);
hashes.insert(id, asset["sha256"].clone());
}
let payload = json!({
"status": "draft",
"ref": parse_json(&read("sheet-ref.json")).unwrap(),
"document": parse_json(&read("document.json")).unwrap(),
"migrationMap": parse_json(&read("report-migration-map.json")).unwrap(),
"assets": assets, "assetHashes": hashes,
});
LoadedSheet::from_bundle(serde_json::from_value::<BundledSheetData>(payload).unwrap()).unwrap()
}
/// Export → import on the R4 reader gives the report back without loss, and the same
/// outcome as importing the R3 PDF; a re-export from the envelope gives the same file.
#[test]
fn restoration_round_trip_loses_nothing() {
let sheet = draft_sheet();
for run in all_runs() {
let from_r4 = import_pdf(&sheet, &run.r4, ProbeLimits::PRODUCTION).unwrap();
let from_r3 = import_pdf(&sheet, &run.r3, ProbeLimits::PRODUCTION).unwrap();
assert_eq!(from_r4, from_r3, "{}: import outcome", run.name);
let envelope = parse_json(&run.envelope).unwrap();
assert_eq!(
to_js_compact(&from_r4.read.report).unwrap(),
to_js_compact(&envelope["report"]).unwrap(),
"{}: report",
run.name
);
if !run.name.starts_with("store-path") {
let locale = if envelope["locale"] == "en" {
Locale::En
} else {
Locale::Fr
};
let input = input_from(&envelope);
let png = base64(
if run.name.contains("logos") && !run.name.contains("nologos") {
TWO_BY_ONE_PNG
} else {
ONE_PX_PNG
},
);
let rasters = if run.name.starts_with("real") {
continue;
} else {
stub_rasters(&input, &png)
};
let again = export_from_envelope(
&envelope,
locale,
envelope["exportedAt"].as_str().unwrap(),
&rasters,
)
.unwrap();
assert_eq!(
again.pdf, run.r4,
"{}: re-export from the envelope",
run.name
);
}
}
}