diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 35dde22b..43cc843c 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -17,6 +17,8 @@ ts = ["dep:ts-rs"] # The PDF reader (R4, plan §B.1.2): byte scan, inflation, attachment probe, import. The wasm # facade enables it; the worker never does. pdf-read = ["dep:miniz_oxide"] +# The PDF writer (R4, plan §B.1.3): layout to a draw log (R4.4), the serialiser (R4.5). +pdf-write = [] [dependencies] serde.workspace = true @@ -55,5 +57,13 @@ required-features = ["pdf-read"] name = "pdf_robustness" required-features = ["pdf-read"] +[[test]] +name = "pdf_text" +required-features = ["pdf-write"] + +[[test]] +name = "pdf_layout" +required-features = ["pdf-write"] + [lints] workspace = true diff --git a/crates/core/src/pdf/mod.rs b/crates/core/src/pdf/mod.rs index 19367431..9a757c0f 100644 --- a/crates/core/src/pdf/mod.rs +++ b/crates/core/src/pdf/mod.rs @@ -11,3 +11,5 @@ pub mod import; pub mod json_js; #[cfg(feature = "pdf-read")] pub mod read; +#[cfg(feature = "pdf-write")] +pub mod write; diff --git a/crates/core/src/pdf/write/afm_data.rs b/crates/core/src/pdf/write/afm_data.rs new file mode 100644 index 00000000..754aa514 --- /dev/null +++ b/crates/core/src/pdf/write/afm_data.rs @@ -0,0 +1,3 @@ +//! R4.4a: the AFM metrics of Helvetica, Helvetica-Bold and Times-Roman over WinAnsi, and +//! the Unicode tables the text rules need, generated from the O4 and O5 oracles by +//! `scripts/r4-afm-data.mjs`. Written by the R4.4a track. diff --git a/crates/core/src/pdf/write/dates.rs b/crates/core/src/pdf/write/dates.rs new file mode 100644 index 00000000..8a1e687e --- /dev/null +++ b/crates/core/src/pdf/write/dates.rs @@ -0,0 +1,8 @@ +//! R4.4a: `formatDate` of `render/dates.ts`. Written by the R4.4a track. + +use crate::domain::archive::Locale; + +/// Long date in the locale for a `YYYY-MM-DD` value; anything else printed unchanged. +pub fn format_date(_iso_date: &str, _locale: Locale) -> String { + unimplemented!("R4.4a") +} diff --git a/crates/core/src/pdf/write/draw_log.rs b/crates/core/src/pdf/write/draw_log.rs new file mode 100644 index 00000000..8b0c7dad --- /dev/null +++ b/crates/core/src/pdf/write/draw_log.rs @@ -0,0 +1 @@ +//! R4.4b: the draw log. Written by the layout track. diff --git a/crates/core/src/pdf/write/input.rs b/crates/core/src/pdf/write/input.rs new file mode 100644 index 00000000..2a925199 --- /dev/null +++ b/crates/core/src/pdf/write/input.rs @@ -0,0 +1 @@ +//! R4.4b: the export input and its checks. Written by the layout track. diff --git a/crates/core/src/pdf/write/layout/mod.rs b/crates/core/src/pdf/write/layout/mod.rs new file mode 100644 index 00000000..f25dc9ac --- /dev/null +++ b/crates/core/src/pdf/write/layout/mod.rs @@ -0,0 +1 @@ +//! R4.4b: the page layouts. Written by the layout track. diff --git a/crates/core/src/pdf/write/mod.rs b/crates/core/src/pdf/write/mod.rs new file mode 100644 index 00000000..dab13c9a --- /dev/null +++ b/crates/core/src/pdf/write/mod.rs @@ -0,0 +1,14 @@ +//! The PDF writer (R4, plan §B.1.3). R4.4 brings the layout: the pages of a report as a +//! [`draw_log::DrawLog`], every drawing call of `packages/pdf/src/render/*` reproduced at +//! full precision in the TypeScript's call order (header, title, images, sections per +//! observation page, the conclusion page, then the footers of every page), pinned by the +//! O1 oracle (`apps/app/tests/fixtures/goldens/r4/draw.*.json`) and the Step 6 goldens. The +//! text rules (`text`, `afm_data`) and the dates (`dates`) are pinned by O4, O5 and O6. The +//! serialiser that turns the log into PDF bytes is R4.5. + +pub mod afm_data; +pub mod dates; +pub mod draw_log; +pub mod input; +pub mod layout; +pub mod text; diff --git a/crates/core/src/pdf/write/text.rs b/crates/core/src/pdf/write/text.rs new file mode 100644 index 00000000..3876a41a --- /dev/null +++ b/crates/core/src/pdf/write/text.rs @@ -0,0 +1,46 @@ +//! R4.4a: the text rules of the engine (`packages/pdf/src/text.ts`, `render/text.ts`). +//! Written by the R4.4a track; the signatures below are the contract the layout uses. + +/// The three standard fonts the engine embeds (`render/fonts.ts`), by their PDF base name. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Font { + Helvetica, + HelveticaBold, + TimesRoman, +} + +impl Font { + /// The base font name, as the draw log and the PDF carry it. + pub fn as_str(self) -> &'static str { + match self { + Self::Helvetica => "Helvetica", + Self::HelveticaBold => "Helvetica-Bold", + Self::TimesRoman => "Times-Roman", + } + } +} + +/// `safeText`: Cc, Cf, Zl and Zp dropped, other non-WinAnsi code points `?`. +pub fn safe_text(_text: &str) -> String { + unimplemented!("R4.4a") +} + +/// JavaScript `String.prototype.toUpperCase()`. +pub fn js_upper(_text: &str) -> String { + unimplemented!("R4.4a") +} + +/// JavaScript `String.prototype.trim()`. +pub fn js_trim(_text: &str) -> &str { + unimplemented!("R4.4a") +} + +/// `measure(font, text, size) = widthOfTextAtSize(safeText(text), size)`. +pub fn measure(_font: Font, _text: &str, _size: f64) -> f64 { + unimplemented!("R4.4a") +} + +/// `wrapLines(text, font, size, max)`: split on JS `\s+`, each word made safe, greedy join. +pub fn wrap_lines(_text: &str, _font: Font, _size: f64, _max: f64) -> Vec { + unimplemented!("R4.4a") +} diff --git a/crates/core/tests/pdf_layout.rs b/crates/core/tests/pdf_layout.rs new file mode 100644 index 00000000..f8ff1a5e --- /dev/null +++ b/crates/core/tests/pdf_layout.rs @@ -0,0 +1 @@ +//! R4.4b placeholder. diff --git a/crates/core/tests/pdf_text.rs b/crates/core/tests/pdf_text.rs new file mode 100644 index 00000000..980258d6 --- /dev/null +++ b/crates/core/tests/pdf_text.rs @@ -0,0 +1 @@ +//! R4.4a placeholder. diff --git a/crates/wasm/Cargo.toml b/crates/wasm/Cargo.toml index 091d40cb..01761ba7 100644 --- a/crates/wasm/Cargo.toml +++ b/crates/wasm/Cargo.toml @@ -18,7 +18,7 @@ default = [] proofs = [] [dependencies] -faunosteo-core = { path = "../core", features = ["pdf-read"] } +faunosteo-core = { path = "../core", features = ["pdf-read", "pdf-write"] } wasm-bindgen.workspace = true [lints] diff --git a/scripts/rust-check.sh b/scripts/rust-check.sh index c4b62b6d..06be5fc6 100755 --- a/scripts/rust-check.sh +++ b/scripts/rust-check.sh @@ -17,11 +17,11 @@ cargo clippy --workspace --all-targets --locked -- -D warnings cargo test --workspace --locked # The PDF reader (R4) is feature-gated in the core: its code and suites compile only here # and in the wasm module, never in the worker. -cargo clippy -p faunosteo-core --all-targets --features pdf-read --locked -- -D warnings -cargo test -p faunosteo-core --features pdf-read --locked +cargo clippy -p faunosteo-core --all-targets --features pdf-read,pdf-write --locked -- -D warnings +cargo test -p faunosteo-core --features pdf-read,pdf-write --locked # Exports the TypeScript types of the contracts (ts-rs) into packages/core-wasm/types. # `git status --porcelain --untracked-files=all -- packages/core-wasm/types` must stay # empty afterwards (scripts/check-all.sh, .gitea/workflows/ci.yml): the committed types # never drift from the Rust structs, and a new type file counts as drift. -cargo test -p faunosteo-core --features ts,pdf-read --locked +cargo test -p faunosteo-core --features ts,pdf-read,pdf-write --locked cargo deny check licenses bans sources