collective

Browse / github.com/storytold/pdfcraft / patch

M11: preserve image resolution in annotation appearances

Optimizer blurs shared stamp images by ignoring appearance size On current main (`1dd2737b`), Optimize PDF measures image resolution only in page content and nested forms. It misses annotation appearance uses. A shared 720 x 720 grayscale image drawn 36pt wide on the page and 360pt wide in a Stamp appearance is reduced to 75 x 75, even though the stamp is already only 144ppi. The stamp's fine checkerboard becomes a uniform gray block. Reproduced through `pdfcraft-cli run` with ZIP compression, so this is downsampling damage rather than JPEG loss. The original opens and renders correctly. Reproduction (contributor-original synthetic source, Python standar

Status
open
Score
0.00
Adopters
0 other operators
Author
KyleKassen (imported from pull request #5000, open)
Base
1dd2737b47a9c1c9562f0af563053d1bf510e4bf
Size
+616 −46 in 14 files
Review
pending
Shared
2026-10-28
Id
b5e7d8b1191c3898a5bdfef7

How it went

No other operator has reported using it yet.

Diff

raw · ATTRIBUTION.md ATTRIBUTION.toml Cargo.lock Cargo.toml ROADMAP.md crates/ocr/Cargo.toml crates/ocr/src/lib.rs crates/optimize/README.md crates/optimize/src/images.rs crates/optimize/tests/appearances.rs docs/releasing.md docs/roadmap.md packaging/windows/test-msi.ps1 parity/acrobat-features.toml

diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md--- a/ATTRIBUTION.md+++ b/ATTRIBUTION.md@@ -335,8 +335,8 @@ Fonts are fetched by `cargo xtask demo-pdf` into `target/demo-fonts/`, OCR model | `NotoColorEmoji-Regular.ttf` | Noto Color Emoji | Google Inc. | OFL-1.1 | https://github.com/google/fonts/tree/9710da1eacb3be272583c3224dcb70f9da6eadbb/ofl/notocoloremoji | Showcase PDF (cargo xtask demo-pdf) | | `Inter[opsz,wght].ttf` | Inter (variable) | The Inter Project Authors (Rasmus Andersson) | OFL-1.1 | https://github.com/google/fonts/tree/9710da1eacb3be272583c3224dcb70f9da6eadbb/ofl/inter | Showcase PDF (cargo xtask demo-pdf) | | `Inter-Italic[opsz,wght].ttf` | Inter Italic (variable) | The Inter Project Authors (Rasmus Andersson) | OFL-1.1 | https://github.com/google/fonts/tree/9710da1eacb3be272583c3224dcb70f9da6eadbb/ofl/inter | Showcase PDF (cargo xtask demo-pdf) |-| `text-detection.rten` | Ocrs text detection model | Robert Knight (ocrs-models; trained on HierText, CC-BY-SA-4.0) | CC-BY-SA-4.0 | https://github.com/robertknight/ocrs-models (the models ocrs downloads; model card: https://huggingface.co/robertknight/ocrs, cc-by-sa-4.0) | Scan & OCR: finds words on page images (cargo xtask models) |-| `text-recognition.rten` | Ocrs text recognition model | Robert Knight (ocrs-models; trained on HierText, CC-BY-SA-4.0) | CC-BY-SA-4.0 | https://github.com/robertknight/ocrs-models (the models ocrs downloads; model card: https://huggingface.co/robertknight/ocrs, cc-by-sa-4.0) | Scan & OCR: reads the words it finds (cargo xtask models) |+| `text-detection.onnx` | Ocrs text detection model | Robert Knight (ocrs-models; trained on HierText, CC-BY-SA-4.0) | CC-BY-SA-4.0 | https://github.com/robertknight/ocrs/blob/0c826441362a701c55560fefc17bf356b6d1037e/ocrs/examples/download-models.sh (author-published ONNX models; model card: https://huggingface.co/robertknight/ocrs/blob/df0edd170279ab971b53e094c627255a87e1a503/README.md, cc-by-sa-4.0) | Scan & OCR: finds words on page images (cargo xtask models) |+| `text-recognition.onnx` | Ocrs text recognition model | Robert Knight (ocrs-models; trained on HierText, CC-BY-SA-4.0) | CC-BY-SA-4.0 | https://github.com/robertknight/ocrs/blob/0c826441362a701c55560fefc17bf356b6d1037e/ocrs/examples/download-models.sh (author-published ONNX models; model card: https://huggingface.co/robertknight/ocrs/blob/df0edd170279ab971b53e094c627255a87e1a503/README.md, cc-by-sa-4.0) | Scan & OCR: reads the words it finds (cargo xtask models) |  ## Optional build inputs (1) diff --git a/ATTRIBUTION.toml b/ATTRIBUTION.toml--- a/ATTRIBUTION.toml+++ b/ATTRIBUTION.toml@@ -3356,30 +3356,30 @@ usage = "1x + 2x HiDPI 16-colour palette TIFF copied into the macOS DMG by packa sha256 = "f46881724a6621f3686a612a79dbdce4f7dbe9e2025610e44c9a094341334c2c"  [[fetched]]-file = "text-detection.rten"-url = "https://ocrs-models.s3-accelerate.amazonaws.com/text-detection.rten"+file = "text-detection.onnx"+url = "https://ocrs-models.s3-accelerate.amazonaws.com/text-detection.onnx" title = "Ocrs text detection model" author = "Robert Knight (ocrs-models; trained on HierText, CC-BY-SA-4.0)"-source = "https://github.com/robertknight/ocrs-models (the models ocrs downloads; model card: https://huggingface.co/robertknight/ocrs, cc-by-sa-4.0)"+source = "https://github.com/robertknight/ocrs/blob/0c826441362a701c55560fefc17bf356b6d1037e/ocrs/examples/download-models.sh (author-published ONNX models; model card: https://huggingface.co/robertknight/ocrs/blob/df0edd170279ab971b53e094c627255a87e1a503/README.md, cc-by-sa-4.0)" licence = "CC-BY-SA-4.0" licence_url = "https://creativecommons.org/licenses/by-sa/4.0/legalcode.txt" licence_sha256 = "28a9529c7d0bb4dc51f4bf5c116a3d16ef247a052f7591466768ddf563fd1cf5" kind = "model" usage = "Scan & OCR: finds words on page images (cargo xtask models)"-sha256 = "f15cfb56bd02c4bf478a20343986504a1f01e1665c2b3a0ad66340f054b1b5ca"+sha256 = "a917b23dbd9524b465df7e922641b3ff2981623df4ded5a0234004ef2fee7cfe"  [[fetched]]-file = "text-recognition.rten"-url = "https://ocrs-models.s3-accelerate.amazonaws.com/text-recognition.rten"+file = "text-recognition.onnx"+url = "https://ocrs-models.s3-accelerate.amazonaws.com/text-recognition.onnx" title = "Ocrs text recognition model" author = "Robert Knight (ocrs-models; trained on HierText, CC-BY-SA-4.0)"-source = "https://github.com/robertknight/ocrs-models (the models ocrs downloads; model card: https://huggingface.co/robertknight/ocrs, cc-by-sa-4.0)"+source = "https://github.com/robertknight/ocrs/blob/0c826441362a701c55560fefc17bf356b6d1037e/ocrs/examples/download-models.sh (author-published ONNX models; model card: https://huggingface.co/robertknight/ocrs/blob/df0edd170279ab971b53e094c627255a87e1a503/README.md, cc-by-sa-4.0)" licence = "CC-BY-SA-4.0" licence_url = "https://creativecommons.org/licenses/by-sa/4.0/legalcode.txt" licence_sha256 = "28a9529c7d0bb4dc51f4bf5c116a3d16ef247a052f7591466768ddf563fd1cf5" kind = "model" usage = "Scan & OCR: reads the words it finds (cargo xtask models)"-sha256 = "e484866d4cce403175bd8d00b128feb08ab42e208de30e42cd9889d8f1735a6e"+sha256 = "86c145c2edb96c8caed5b1ebb8f44d706408922211451c309c625157dd6061c5"  [[build_input]] name = "craft-fonts"diff --git a/Cargo.lock b/Cargo.lock--- a/Cargo.lock+++ b/Cargo.lock@@ -1174,7 +1174,7 @@ source = "git+https://github.com/storytold/craft-libs?rev=82a24b006eefa036b43c09 [[package]] name = "craft-keymap" version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=edc41f68fa24c46651c3b5117d10f0ead75ec9cb#edc41f68fa24c46651c3b5117d10f0ead75ec9cb"+source = "git+https://github.com/storytold/craft-libs?rev=acab34c0d8d84363ea3d05885f1a15601c233a19#acab34c0d8d84363ea3d05885f1a15601c233a19" dependencies = [  "egui",  "serde",@@ -1184,7 +1184,7 @@ dependencies = [ [[package]] name = "craft-menu" version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=edc41f68fa24c46651c3b5117d10f0ead75ec9cb#edc41f68fa24c46651c3b5117d10f0ead75ec9cb"+source = "git+https://github.com/storytold/craft-libs?rev=acab34c0d8d84363ea3d05885f1a15601c233a19#acab34c0d8d84363ea3d05885f1a15601c233a19" dependencies = [  "craft-keymap",  "craft-quit",@@ -1210,7 +1210,7 @@ dependencies = [ [[package]] name = "craft-quit" version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=edc41f68fa24c46651c3b5117d10f0ead75ec9cb#edc41f68fa24c46651c3b5117d10f0ead75ec9cb"+source = "git+https://github.com/storytold/craft-libs?rev=acab34c0d8d84363ea3d05885f1a15601c233a19#acab34c0d8d84363ea3d05885f1a15601c233a19" dependencies = [  "dispatch2",  "egui",@@ -1221,7 +1221,7 @@ dependencies = [ [[package]] name = "craft-ui" version = "0.1.0"-source = "git+https://github.com/storytold/craft-libs?rev=edc41f68fa24c46651c3b5117d10f0ead75ec9cb#edc41f68fa24c46651c3b5117d10f0ead75ec9cb"+source = "git+https://github.com/storytold/craft-libs?rev=acab34c0d8d84363ea3d05885f1a15601c233a19#acab34c0d8d84363ea3d05885f1a15601c233a19" dependencies = [  "egui",  "log",@@ -4340,6 +4340,7 @@ dependencies = [  "ocrs",  "pdfcraft-fonts",  "rten",+ "tempfile",  "thiserror 2.0.21", ] @@ -5473,6 +5474,7 @@ dependencies = [  "rten-base",  "rten-gemm",  "rten-model-file",+ "rten-onnx",  "rten-parallel",  "rten-shape-inference",  "rten-simd",@@ -5522,6 +5524,12 @@ dependencies = [  "rten-base", ] +[[package]]+name = "rten-onnx"+version = "0.26.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "96944374568990b8d1bbc5bae5957585866f9dd8256130460405730948bfd42e"+ [[package]] name = "rten-parallel" version = "0.26.0"diff --git a/Cargo.toml b/Cargo.toml--- a/Cargo.toml+++ b/Cargo.toml@@ -13,12 +13,12 @@ repository = "https://github.com/storytold/pdfcraft"  [workspace.dependencies] # Shared frontend components; engines remain independent of egui.-craft-ui = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "edc41f68fa24c46651c3b5117d10f0ead75ec9cb", features = ["serde"] }+craft-ui = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "acab34c0d8d84363ea3d05885f1a15601c233a19", features = ["serde"] } # Quit the macOS way (#1136): ⌘Q asks about unsaved changes, and closing quits through `terminate:`.-craft-quit = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "edc41f68fa24c46651c3b5117d10f0ead75ec9cb" }+craft-quit = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "acab34c0d8d84363ea3d05885f1a15601c233a19" } # One menu definition for the native macOS menu bar and the in-window bar elsewhere.-craft-menu = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "edc41f68fa24c46651c3b5117d10f0ead75ec9cb" }-craft-keymap = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "edc41f68fa24c46651c3b5117d10f0ead75ec9cb", default-features = false }+craft-menu = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "acab34c0d8d84363ea3d05885f1a15601c233a19" }+craft-keymap = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "acab34c0d8d84363ea3d05885f1a15601c233a19", default-features = false } # Shared system spooler; document composition remains in pdfcraft-print. craft-print = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "42e30cd5b3f4b114270dd053fccbc33bedc47f68" } # Whether a path stored in a document may be opened (GoToR link targets).diff --git a/ROADMAP.md b/ROADMAP.md--- a/ROADMAP.md+++ b/ROADMAP.md@@ -1,7 +1,10 @@ # PdfCraft roadmap +> **Last reviewed:** 2026-10-27 | **Last updated:** 2026-10-27 | **Change:** minor (M11 #4996: optimization measures all annotation appearance uses; estimates unchanged)+ **Stage: alpha** · next: beta, ~25% (ready for real work 50% → 75%) and ~400–700 h away +> **Last reviewed:** 2026-10-27 | **Last updated:** 2026-10-27 | **Change:** minor (M10: OCR model downloads use verified ONNX pairs with legacy discovery fallback; estimates unchanged) > **Last reviewed:** 2026-10-25 | **Last updated:** 2026-10-25 | **Change:** minor (M14 localization current state: Arabic logical catalog, explicit RTL label direction and mirrored stock chrome; native/browser RTL workflows unqualified. Native release Hans face via LXGW WenKai Lite; estimates unchanged) > **Last reviewed:** 2026-10-23 · **Last updated:** 2026-10-23 · **Change:** minor (M7, #2636 partial: pasting added text boxes and images in the engine and through `content_copy`; estimates unchanged) > **Last reviewed:** 2026-10-23 · **Last updated:** 2026-10-23 · **Change:** minor (M11: image optimization accounts for page-local UserUnit; estimates unchanged)@@ -199,6 +202,11 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md  ## Progress log +- **2026-10-27 (M11, #4996):** Image downsampling includes normal, rollover and pressed annotation appearance states at their physical placement size, preserving images shared with large stamps or widgets.+  Invalid appearance geometry and exhausted traversal budgets prevent downsampling.+  Estimates unchanged.++- **2026-10-27 (M10, OCR models):** Model staging downloads and verifies the ONNX pair after the upstream RTEN URL brownout; runtime discovery prefers complete ONNX pairs per directory, retains complete RTEN fallback and preserves directory precedence. - **2026-10-24 (M10, #4158 partial):** Word, HTML, RTF and PowerPoint export keep a block that continues another on its line (an italic case name, a link, a superscript) as a run in its own style inside that paragraph, with the source's word space, instead of a paragraph of its own. Style changes that wrap onto the next line still split paragraphs (the reading blocks keep style boundaries). Estimates unchanged. - **2026-10-24 (M3, window move on Wayland, #3970 follow-up):** After a move or resize by PdfCraft's own title bar or edges, the button release given back to egui goes before the pointer leaving the window, so hover no longer sticks at the old place until the pointer moves. Found by CodeRabbit in review of #3970; a unit test covers it. Estimates unchanged. - **2026-10-24 (M14, #4253):** The annotation quick bar remembers Keep tool selected across restarts, including unpinning. Old and malformed settings keep safe defaults; the opt-in UI control channel reads and changes the same preference. Estimates unchanged.@@ -682,6 +690,8 @@ Newest first: the date, what moved, and the new overall estimate.  | Date | Change | Summary | |---|---|---|+| 2026-10-27 | minor | M11 #4996: include annotation appearance uses in image resolution, with bounded traversal and conservative fallback. Estimates unchanged. |+| 2026-10-27 | minor | M10: OCR stages SHA-256-verified ONNX models after the upstream RTEN URL brownout; discovery prefers a complete ONNX pair per directory, falls back to a complete RTEN pair, and never mixes formats or directories; language/web limits and estimates unchanged. | | 2026-10-23 | minor | M8, #3205 follow-up: phone-pattern redaction includes the +39 country code on labelled Italian geographic numbers with a slash; dates, protocol identifiers and whole-run guards remain. Estimates unchanged. | | 2026-10-23 | minor | M11: image downsampling measures physical resolution after page-local UserUnit scaling, including shared nested forms; synthetic measurement and optimization regressions. Estimates unchanged. | | 2026-10-23 | minor | M10: Word table cells use the grid columns they span, including cells after a merged cell; synthetic export regression and CLI reproduction. Estimates unchanged. |diff --git a/crates/ocr/Cargo.toml b/crates/ocr/Cargo.toml--- a/crates/ocr/Cargo.toml+++ b/crates/ocr/Cargo.toml@@ -13,5 +13,8 @@ workspace = true [dependencies] pdfcraft-fonts = { workspace = true } ocrs = { version = "0.13.1", default-features = false, features = ["rten"] }-rten = { version = "0.26.0", default-features = false, features = ["rten_format"] }+rten = { version = "0.26.0", default-features = false, features = ["onnx_format", "rten_format"] } thiserror.workspace = true++[dev-dependencies]+tempfile = "3.27"diff --git a/crates/ocr/src/lib.rs b/crates/ocr/src/lib.rs--- a/crates/ocr/src/lib.rs+++ b/crates/ocr/src/lib.rs@@ -15,8 +15,8 @@ use std::path::{Path, PathBuf}; pub use pdfcraft_fonts::helvetica_width;  /// The model files, as named in ATTRIBUTION.toml.-pub const DETECTION_MODEL: &str = "text-detection.rten";-pub const RECOGNITION_MODEL: &str = "text-recognition.rten";+pub const DETECTION_MODEL: &str = "text-detection.onnx";+pub const RECOGNITION_MODEL: &str = "text-recognition.onnx";  /// The recognition model's alphabet, as it was trained (ocrs-models' `DEFAULT_ALPHABET`): its /// output classes follow this order. The ocrs crate's own copy has an `E` where the training had@@ -48,10 +48,13 @@ pub struct Models { }  impl Models {-    /// The models in `dir`, if both files are there.+    /// A complete model pair in `dir`, preferring current ONNX files over legacy RTEN files.+    /// An incomplete update never mixes formats or hides an intact legacy pair.     pub fn in_dir(dir: &Path) -> Option<Models> {-        let m = Models { detection: dir.join(DETECTION_MODEL), recognition: dir.join(RECOGNITION_MODEL) };-        (m.detection.is_file() && m.recognition.is_file()).then_some(m)+        [(DETECTION_MODEL, RECOGNITION_MODEL), ("text-detection.rten", "text-recognition.rten")].into_iter().find_map(|(detection, recognition)| {+            let m = Models { detection: dir.join(detection), recognition: dir.join(recognition) };+            (m.detection.is_file() && m.recognition.is_file()).then_some(m)+        })     }      /// Look for the models: `$PDFCRAFT_MODELS`, then where the release packages install them@@ -321,6 +324,63 @@ mod tests {         assert_eq!(w.up, [-10.0, 0.0]);     } +    /// The current ONNX pair wins when an installation also retains both legacy models.+    #[test]+    fn model_discovery_prefers_the_complete_onnx_pair() {+        let dir = tempfile::tempdir().unwrap();+        for file in ["text-detection.onnx", "text-recognition.onnx"] {+            std::fs::write(dir.path().join(file), b"model").unwrap();+        }+        let expected = Some(Models { detection: dir.path().join("text-detection.onnx"), recognition: dir.path().join("text-recognition.onnx") });+        assert_eq!(Models::in_dir(dir.path()), expected, "a complete ONNX pair is sufficient");+        for file in ["text-detection.rten", "text-recognition.rten"] {+            std::fs::write(dir.path().join(file), b"model").unwrap();+        }+        assert_eq!(Models::in_dir(dir.path()), expected, "the complete ONNX pair takes precedence over legacy models");+    }++    /// An interrupted ONNX update still permits an intact legacy pair in the same directory.+    #[test]+    fn model_discovery_falls_back_to_legacy_when_the_onnx_pair_is_incomplete() {+        for current in ["text-detection.onnx", "text-recognition.onnx"] {+            let dir = tempfile::tempdir().unwrap();+            for file in [current, "text-detection.rten", "text-recognition.rten"] {+                std::fs::write(dir.path().join(file), b"model").unwrap();+            }+            assert_eq!(+                Models::in_dir(dir.path()),+                Some(Models { detection: dir.path().join("text-detection.rten"), recognition: dir.path().join("text-recognition.rten") }),+                "{current} alone must not hide a complete legacy pair"+            );+        }+    }++    /// Detection and recognition must come from one complete format pair, never a mixed update.+    #[test]+    fn model_discovery_refuses_mixed_format_pairs() {+        for pair in [["text-detection.onnx", "text-recognition.rten"], ["text-detection.rten", "text-recognition.onnx"]] {+            let dir = tempfile::tempdir().unwrap();+            for file in pair {+                std::fs::write(dir.path().join(file), b"model").unwrap();+            }+            assert_eq!(Models::in_dir(dir.path()), None, "{pair:?} is not a complete model pair");+        }+    }++    /// Existing source builds and PDFCRAFT_MODELS overrides retain their complete RTEN pair.+    #[test]+    fn model_discovery_keeps_legacy_only_installations() {+        let dir = tempfile::tempdir().unwrap();+        assert_eq!(Models::in_dir(dir.path()), None);+        std::fs::write(dir.path().join("text-detection.rten"), b"model").unwrap();+        assert_eq!(Models::in_dir(dir.path()), None, "one legacy model is incomplete");+        std::fs::write(dir.path().join("text-recognition.rten"), b"model").unwrap();+        assert_eq!(+            Models::in_dir(dir.path()),+            Some(Models { detection: dir.path().join("text-detection.rten"), recognition: dir.path().join("text-recognition.rten") })+        );+    }+     /// #103: every release package's layout puts the models where the installed app looks.     /// Mirrors packaging/{windows/package.ps1,macos/package.sh,linux/package.sh,freebsd/package.sh}.     #[test]diff --git a/crates/optimize/README.md b/crates/optimize/README.md--- a/crates/optimize/README.md+++ b/crates/optimize/README.md@@ -23,6 +23,8 @@ let report = optimize_with_progress(&mut doc, &settings, &mut |stage| keep_going - **Images:** the effective resolution of each image is measured where pages draw it (the CTM   through form XObjects, up to 12 deep; the smallest over all uses).   Page-local `/UserUnit` scaling is included, so large-format pages are measured in physical inches.+  Annotation appearances also count, including every normal, rollover and pressed state, so a large stamp or widget keeps a shared image sharp.+  Appearance placement includes `/Rect`, `/BBox` and `/Matrix`; malformed geometry or exhausted traversal/decode limits disable downsampling conservatively.   If a use can't be   measured (forms nested deeper, or drawn too many times), no image is resampled. Colour and grayscale images above a   threshold are resampled with a bicubic (Catmull-Rom) filter to a target resolution anddiff --git a/crates/optimize/src/images.rs b/crates/optimize/src/images.rs--- a/crates/optimize/src/images.rs+++ b/crates/optimize/src/images.rs@@ -36,6 +36,58 @@ const MAX_FORM_DEPTH: usize = 12; /// Each form's first walk is free: their number is bounded by the file's object count. const MAX_REWALKS: usize = 100_000; +/// Bound annotation and appearance-state entries examined across the document.+const MAX_APPEARANCE_WORK: usize = 100_000;++/// Fixed-size, finite geometry with indirect arrays and numeric entries resolved.+fn geometry<const N: usize>(doc: &Document, value: &Object) -> Option<[f64; N]> {+    let value = doc.resolve(value);+    let values = value.as_array()?;+    if values.len() != N {+        return None;+    }+    let mut out = [0.0; N];+    for (number, value) in out.iter_mut().zip(values) {+        *number = doc.resolve(value).as_f64()?;+        if !number.is_finite() {+            return None;+        }+    }+    Some(out)+}++fn form_matrix(doc: &Document, dict: &Dict) -> Option<Matrix> {+    match dict.get(b"Matrix") {+        None | Some(Object::Null) => Some(Matrix::IDENTITY),+        Some(value) => geometry(doc, value).map(Matrix),+    }+}++/// ISO 32000-2, 12.5.5: fit the transformed appearance BBox to the annotation Rect.+/// The form walker applies /Matrix itself, so return only the placement transform.+fn appearance_placement(doc: &Document, form: &Dict, rect: [f64; 4]) -> Option<Matrix> {+    let bbox = geometry(doc, form.get(b"BBox")?)?;+    let matrix = form_matrix(doc, form)?;+    // Check corners before bbox(): min/max can otherwise hide NaNs from overflow.+    let [x0, y0, x1, y1] = bbox;+    if [matrix.apply(x0, y0), matrix.apply(x1, y0), matrix.apply(x0, y1), matrix.apply(x1, y1)].iter().any(|(x, y)| !x.is_finite() || !y.is_finite())+    {+        return None;+    }+    let bbox = matrix.bbox(bbox);+    if !bbox.iter().all(|value| value.is_finite()) {+        return None;+    }+    let [x0, y0, x1, y1] = rect;+    let (width, height) = (bbox[2] - bbox[0], bbox[3] - bbox[1]);+    if !(width > 0.0 && height > 0.0 && width.is_finite() && height.is_finite()) {+        return None;+    }+    let (sx, sy) = ((x1.max(x0) - x0.min(x1)) / width, (y1.max(y0) - y0.min(y1)) / height);+    let placement = Matrix([sx, 0.0, 0.0, sy, x0.min(x1) - bbox[0] * sx, y0.min(y1) - bbox[1] * sy]);+    (sx > 0.0 && sy > 0.0 && placement.0.iter().all(|value| value.is_finite())).then_some(placement)+}+ /// State of one [`effective_resolutions`] pass. struct Walk<'a> {     doc: &'a Document,@@ -52,6 +104,7 @@ struct Walk<'a> {     /// False once a use may have been missed (depth or work limit).     complete: bool,     out: HashMap<ObjRef, f64>,+    appearance_work: usize, }  impl Walk<'_> {@@ -100,6 +153,95 @@ impl Walk<'_> {         }     } +    fn appearance_step(&mut self) -> bool {+        match self.appearance_work.checked_sub(1) {+            Some(left) => {+                self.appearance_work = left;+                true+            }+            None => {+                self.complete = false;+                false+            }+        }+    }++    /// Every appearance may be used later, including inactive states and rollover/down forms.+    fn annotations(&mut self, page: &Dict, resources: &Dict, ctm: Matrix) {+        let doc = self.doc;+        let Some(annots) = page.get(b"Annots") else { return };+        let annots = doc.resolve(annots);+        if matches!(annots.as_ref(), Object::Null) {+            return;+        }+        let Some(annots) = annots.as_array() else {+            self.complete = false;+            return;+        };+        for annot in annots {+            if !self.appearance_step() {+                return;+            }+            let annot = doc.resolve(annot);+            let Some(annot) = annot.as_dict() else { continue };+            let Some(ap) = annot.get(b"AP") else { continue };+            let ap = doc.resolve(ap);+            if matches!(ap.as_ref(), Object::Null) {+                continue;+            }+            let Some(ap) = ap.as_dict() else {+                self.complete = false;+                continue;+            };+            for kind in [b"N", b"R", b"D"] {+                let Some(value) = ap.get(kind) else { continue };+                if !self.appearance_step() {+                    return;+                }+                let object = doc.resolve(value);+                match object.as_ref() {+                    Object::Null => {}+                    Object::Stream(_) => self.appearance(value, annot, resources, ctm),+                    Object::Dict(states) => {+                        for (_, value) in states.iter() {+                            if !self.appearance_step() {+                                return;+                            }+                            self.appearance(value, annot, resources, ctm);+                        }+                    }+                    _ => self.complete = false,+                }+            }+        }+    }++    fn appearance(&mut self, value: &Object, annot: &Dict, resources: &Dict, ctm: Matrix) {+        let doc = self.doc;+        let object = doc.resolve(value);+        if matches!(object.as_ref(), Object::Null) {+            return;+        }+        let (Some(reference), Object::Stream(form)) = (value.as_ref(), object.as_ref()) else {+            self.complete = false;+            return;+        };+        let Some(rect) = annot.get(b"Rect").and_then(|rect| geometry(doc, rect)) else {+            self.complete = false;+            return;+        };+        // Invisible signature widgets commonly have a zero-area rectangle and BBox.+        // They cannot draw an image at a larger size, so they do not make the walk incomplete.+        if rect[0] == rect[2] || rect[1] == rect[3] {+            return;+        }+        let Some(placement) = appearance_placement(doc, &form.dict, rect) else {+            self.complete = false;+            return;+        };+        self.form(reference, form, resources, placement.then(&ctm));+    }+     /// Descend into form `r` drawn with `ctm` from a stream whose resources are `resources`.     fn form(&mut self, r: ObjRef, s: &Stream, resources: &Dict, ctm: Matrix) {         if self.path.contains(&r) {@@ -110,8 +252,15 @@ impl Walk<'_> {             return;         }         let doc = self.doc;-        let m = s.dict.get(b"Matrix").map(|m| doc.resolve(m)).and_then(|m| m.as_array().map(|a| Matrix::from_operands(a))).flatten();-        let inner = m.unwrap_or(Matrix([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])).then(&ctm);+        let Some(matrix) = form_matrix(doc, &s.dict) else {+            self.complete = false;+            return;+        };+        let inner = matrix.then(&ctm);+        if !inner.0.iter().all(|value| value.is_finite()) {+            self.complete = false;+            return;+        }         let own = s.dict.get(b"Resources").map(|r| doc.resolve(r)).and_then(|r| r.as_dict().cloned());         // A form without resources uses its caller's, so the same form can draw other images.         if own.is_some() {@@ -156,20 +305,23 @@ pub fn effective_resolutions(doc: &Document, pages: &[ObjRef]) -> HashMap<ObjRef         budget: DecodeBudget::page(),         complete: true,         out: HashMap::new(),+        appearance_work: MAX_APPEARANCE_WORK,     };     for p in pages {         let Some(dict) = doc.get(*p).as_dict().cloned() else { continue };         let page = pdfcraft_model::Page { obj: *p, dict };-        let Some(contents) = page.dict.get(b"Contents") else { continue };         let res = page_resources(doc, *p).unwrap_or_default();         walk.budget = DecodeBudget::page();+        // Content and appearances share the page's physical scale and decode budget.+        let unit = page.user_unit(doc);+        let ctm = Matrix([unit, 0.0, 0.0, unit, 0.0, 0.0]);+        walk.annotations(&page.dict, &res, ctm);+        let Some(contents) = page.dict.get(b"Contents") else { continue };         let Some(data) = content_bytes(doc, contents, &mut walk.budget) else {             walk.complete = false;             continue;         };-        // Content coordinates are in page units; image resolution is in physical inches.-        let unit = page.user_unit(doc);-        walk.content(&data, &res, Matrix([unit, 0.0, 0.0, unit, 0.0, 0.0]));+        walk.content(&data, &res, ctm);     }     if !walk.complete {         walk.out.values_mut().for_each(|v| *v = f64::INFINITY);diff --git a/crates/optimize/tests/appearances.rs b/crates/optimize/tests/appearances.rsnew file mode 100644--- /dev/null+++ b/crates/optimize/tests/appearances.rs@@ -0,0 +1,317 @@+//! Contributor-original image shared by page content and annotation appearances.++use pdfcraft_cos::{Dict, Document, ObjRef, Object, SaveOptions, Stream, write_full};+use pdfcraft_optimize::{Compression, Settings, effective_resolutions, optimize};+use std::sync::Arc;++fn numbers(values: &[f64]) -> Object {+    Object::Array(values.iter().copied().map(Object::Real).collect())+}++fn fixture() -> (Document, ObjRef, ObjRef, ObjRef) {+    let mut doc = Document::new_empty();+    let pixels: Vec<u8> = (0..720).flat_map(|y| (0..720).map(move |x| if (x / 2 + y / 2) % 2 == 0 { 20 } else { 235 })).collect();+    let mut image = Dict::new();+    image.set(b"Type".to_vec(), Object::name("XObject"));+    image.set(b"Subtype".to_vec(), Object::name("Image"));+    image.set(b"Width".to_vec(), Object::Int(720));+    image.set(b"Height".to_vec(), Object::Int(720));+    image.set(b"BitsPerComponent".to_vec(), Object::Int(8));+    image.set(b"ColorSpace".to_vec(), Object::name("DeviceGray"));+    let image = doc.add(Stream::from_raw(image, pixels));+    let mut xobjects = Dict::new();+    xobjects.set(b"Im".to_vec(), Object::Ref(image));+    let mut resources = Dict::new();+    resources.set(b"XObject".to_vec(), Object::Dict(xobjects));+    let mut form = Dict::new();+    form.set(b"Type".to_vec(), Object::name("XObject"));+    form.set(b"Subtype".to_vec(), Object::name("Form"));+    form.set(b"BBox".to_vec(), numbers(&[0.0, 0.0, 1.0, 1.0]));+    form.set(b"Resources".to_vec(), Object::Dict(resources.clone()));+    let form = doc.add(Stream::flate(form, b"/Im Do"));+    let mut ap = Dict::new();+    ap.set(b"N".to_vec(), Object::Ref(form));+    let mut annot = Dict::new();+    annot.set(b"Subtype".to_vec(), Object::name("Stamp"));+    annot.set(b"Rect".to_vec(), numbers(&[80.0, 80.0, 440.0, 440.0]));+    annot.set(b"F".to_vec(), Object::Int(4));+    annot.set(b"AP".to_vec(), Object::Dict(ap));+    let annot = doc.add(annot);+    let contents = doc.add(Stream::flate(Dict::new(), b"q 36 0 0 36 20 730 cm /Im Do Q"));+    let root = doc.root().unwrap();+    let pages = doc.get(root).as_dict().unwrap().reference(b"Pages").unwrap();+    let mut page = Dict::new();+    page.set(b"Type".to_vec(), Object::name("Page"));+    page.set(b"Parent".to_vec(), Object::Ref(pages));+    page.set(b"MediaBox".to_vec(), numbers(&[0.0, 0.0, 612.0, 792.0]));+    page.set(b"Contents".to_vec(), Object::Ref(contents));+    page.set(b"Resources".to_vec(), Object::Dict(resources));+    page.set(b"Annots".to_vec(), Object::Array(vec![Object::Ref(annot)]));+    let page = doc.add(page);+    doc.update_dict(pages, |d| {+        d.set(b"Kids".to_vec(), Object::Array(vec![Object::Ref(page)]));+        d.set(b"Count".to_vec(), Object::Int(1));+    })+    .unwrap();+    (doc, image, form, annot)+}++#[test]+fn appearance_use_keeps_shared_image_at_its_largest_size() {+    let (mut doc, image, _, _) = fixture();+    let page = pdfcraft_model::pages(&doc)[0].obj;+    assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&144.0));+    let before = write_full(&doc, &SaveOptions::default()).unwrap();+    let mut settings = Settings::default();+    settings.gray.compression = Compression::Flate;+    let report = optimize(&mut doc, &settings).unwrap();+    assert_eq!(report.images_resampled, 0, "{report:?}");+    let after = write_full(&doc, &SaveOptions::default()).unwrap();+    let reopened = Document::open(Arc::new(after.clone())).unwrap();+    let page = pdfcraft_model::pages(&reopened)[0].obj;+    assert!(effective_resolutions(&reopened, &[page]).values().any(|ppi| *ppi == 144.0));+    let render = |bytes| {+        let mut renderer = pdfcraft_render::PageRenderer::new(Arc::new(bytes), pdfcraft_render::RenderConfig::default());+        let page = renderer.render(pdfcraft_render::RenderRequest { page: 0, scale: 1.0, ..Default::default() });+        assert!(page.error.is_none(), "{:?}", page.error);+        page.rgba+    };+    assert_eq!(render(before), render(after), "lossless optimization preserves the stamp pixels after save/reopen");+}++#[test]+fn all_appearance_states_and_physical_page_units_are_measured() {+    for kind in [b"N", b"R", b"D"] {+        for states in [false, true] {+            let (mut doc, image, form, annot) = fixture();+            let page = pdfcraft_model::pages(&doc)[0].obj;+            let value = if states {+                let mut options = Dict::new();+                let Object::Stream(mut small) = doc.get(form).as_ref().clone() else { panic!() };+                small.dict.set(b"BBox".to_vec(), numbers(&[0.0, 0.0, 10.0, 10.0]));+                let small = doc.add(small);+                options.set(b"A".to_vec(), Object::Ref(small));+                // /AS selects only the small-use A state; the inactive large state must count.+                options.set(b"Z".to_vec(), Object::Ref(form));+                Object::Ref(doc.add(options))+            } else {+                Object::Ref(form)+            };+            let mut ap = Dict::new();+            ap.set(kind.to_vec(), value);+            let ap = doc.add(ap);+            let rect = doc.add(numbers(&[440.0, 440.0, 80.0, 80.0]));+            doc.update_dict(annot, |d| {+                d.set(b"AP".to_vec(), Object::Ref(ap));+                d.set(b"Rect".to_vec(), Object::Ref(rect));+                d.set(b"AS".to_vec(), Object::name("A"));+            })+            .unwrap();+            let annots = doc.add(Object::Array(vec![Object::Ref(annot)]));+            doc.update_dict(page, |d| {+                d.set(b"Annots".to_vec(), Object::Ref(annots));+                d.set(b"UserUnit".to_vec(), Object::Int(2));+                d.remove(b"Contents");+            })+            .unwrap();+            assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&72.0), "{kind:?}, states={states}");+        }+    }+}++#[test]+fn appearance_matrix_is_included_once_and_page_resources_are_available() {+    let (mut doc, image, form, annot) = fixture();+    let page = pdfcraft_model::pages(&doc)[0].obj;+    let Object::Stream(mut appearance) = doc.get(form).as_ref().clone() else { panic!() };+    appearance.dict.set(b"BBox".to_vec(), numbers(&[10.0, 20.0, 11.0, 21.0]));+    // A 90-degree turn and scaling. The transformed bounding box is fitted to /Rect.+    appearance.dict.set(b"Matrix".to_vec(), numbers(&[0.0, 2.0, -3.0, 0.0, 60.0, -20.0]));+    appearance.dict.remove(b"Resources");+    appearance = Stream::flate(appearance.dict, b"q 1 0 0 1 10 20 cm /Im Do Q");+    doc.set(form, appearance);+    doc.update_dict(annot, |d| d.set(b"Rect".to_vec(), numbers(&[80.0, 80.0, 440.0, 260.0]))).unwrap();+    assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&144.0));+}++#[test]+fn unreadable_appearance_prevents_unsafe_downsampling() {+    for bad in [Object::Null, numbers(&[0.0, 0.0, 0.0, 1.0]), numbers(&[0.0, 0.0, 1.0]), numbers(&[0.0, 0.0, f64::INFINITY, 1.0])] {+        let (mut doc, image, form, _) = fixture();+        let page = pdfcraft_model::pages(&doc)[0].obj;+        let Object::Stream(mut appearance) = doc.get(form).as_ref().clone() else { panic!() };+        appearance.dict.set(b"BBox".to_vec(), bad);+        doc.set(form, appearance);+        assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&f64::INFINITY));+    }+}++#[test]+fn indirect_matrix_numbers_and_missing_subtype_keep_the_appearance_scale() {+    let (mut doc, image, form, _) = fixture();+    let page = pdfcraft_model::pages(&doc)[0].obj;+    let scale = doc.add(Object::Int(100));+    let matrix = doc.add(Object::Array(vec![Object::Ref(scale), 0.into(), 0.into(), Object::Ref(scale), 0.into(), 0.into()]));+    let Object::Stream(mut appearance) = doc.get(form).as_ref().clone() else { panic!() };+    appearance.dict.set(b"Matrix".to_vec(), Object::Ref(matrix));+    appearance.dict.remove(b"Subtype");+    doc.set(form, appearance);+    assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&144.0));+}++#[test]+fn resource_less_shared_appearances_use_each_pages_resources_and_units() {+    let (mut doc, first_image, form, _) = fixture();+    let first = pdfcraft_model::pages(&doc)[0].obj;+    let second_image = doc.add(doc.get(first_image).as_ref().clone());+    let Object::Stream(mut appearance) = doc.get(form).as_ref().clone() else { panic!() };+    appearance.dict.remove(b"Resources");+    doc.set(form, appearance);+    let mut second_page = doc.get(first).as_dict().unwrap().clone();+    let mut objects = Dict::new();+    objects.set(b"Im".to_vec(), Object::Ref(second_image));+    let mut resources = Dict::new();+    resources.set(b"XObject".to_vec(), Object::Dict(objects));+    second_page.set(b"Resources".to_vec(), Object::Dict(resources));+    second_page.set(b"UserUnit".to_vec(), Object::Int(2));+    second_page.remove(b"Contents");+    let second = doc.add(second_page);+    for pages in [[first, second], [second, first]] {+        let measured = effective_resolutions(&doc, &pages);+        assert_eq!(measured.get(&first_image), Some(&144.0));+        assert_eq!(measured.get(&second_image), Some(&72.0));+    }+}++#[test]+fn malformed_appearance_containers_and_failed_decodes_disable_downsampling() {+    for case in 0..5 {+        let (mut doc, image, form, annot) = fixture();+        let page = pdfcraft_model::pages(&doc)[0].obj;+        match case {+            0 => doc.update_dict(page, |d| d.set(b"Annots".to_vec(), Object::Int(1))).unwrap(),+            1 => doc.update_dict(annot, |d| d.set(b"AP".to_vec(), Object::name("invalid"))).unwrap(),+            2 => {+                let mut states = Dict::new();+                states.set(b"Bad".to_vec(), Object::Dict(Dict::new()));+                let mut ap = Dict::new();+                ap.set(b"N".to_vec(), Object::Dict(states));+                doc.update_dict(annot, |d| d.set(b"AP".to_vec(), Object::Dict(ap))).unwrap();+            }+            _ => {+                let Object::Stream(mut appearance) = doc.get(form).as_ref().clone() else { panic!() };+                if case == 3 {+                    appearance.dict.set(b"Filter".to_vec(), Object::name("UnknownDecode"));+                } else {+                    appearance.dict.set(b"Matrix".to_vec(), numbers(&[1e308, -1e308, 1e308, 1e308, 0.0, 0.0]));+                    appearance.dict.set(b"BBox".to_vec(), numbers(&[-1e308, -1e308, 1e308, 1e308]));+                }+                doc.set(form, appearance);+            }+        }+        assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&f64::INFINITY), "case {case}");+    }+}++#[test]+fn excessive_appearance_work_and_form_depth_disable_downsampling() {+    for deep in [false, true] {+        let (mut doc, image, form, annot) = fixture();+        let page = pdfcraft_model::pages(&doc)[0].obj;+        if deep {+            let mut inner = form;+            for _ in 0..14 {+                let Object::Stream(mut outer) = doc.get(form).as_ref().clone() else { panic!() };+                let mut objects = Dict::new();+                objects.set(b"Inner".to_vec(), Object::Ref(inner));+                let mut resources = Dict::new();+                resources.set(b"XObject".to_vec(), Object::Dict(objects));+                outer.dict.set(b"Resources".to_vec(), Object::Dict(resources));+                inner = doc.add(Stream::flate(outer.dict, b"/Inner Do"));+            }+            let mut ap = Dict::new();+            ap.set(b"N".to_vec(), Object::Ref(inner));+            doc.update_dict(annot, |d| d.set(b"AP".to_vec(), Object::Dict(ap))).unwrap();+        } else {+            doc.update_dict(page, |d| d.set(b"Annots".to_vec(), Object::Array(vec![Object::Ref(annot); 100_001]))).unwrap();+        }+        assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&f64::INFINITY), "deep={deep}");+    }+}++#[test]+fn small_appearance_only_images_are_still_downsampled() {+    let (mut doc, image, _, annot) = fixture();+    let page = pdfcraft_model::pages(&doc)[0].obj;+    doc.update_dict(page, |d| {+        d.remove(b"Contents");+    })+    .unwrap();+    doc.update_dict(annot, |d| d.set(b"Rect".to_vec(), numbers(&[80.0, 80.0, 116.0, 116.0]))).unwrap();+    let mut settings = Settings::default();+    settings.gray.compression = Compression::Flate;+    let report = optimize(&mut doc, &settings).unwrap();+    assert_eq!(report.images_resampled, 1);+    let Object::Stream(image) = doc.get(image).as_ref().clone() else { panic!() };+    assert_eq!(image.dict.int(b"Width"), Some(75));+}++#[test]+fn annotations_without_appearances_do_not_disable_page_downsampling() {+    let (mut doc, _, _, annot) = fixture();+    doc.update_dict(annot, |d| {+        d.remove(b"AP");+    })+    .unwrap();+    let mut settings = Settings::default();+    settings.gray.compression = Compression::Flate;+    assert_eq!(optimize(&mut doc, &settings).unwrap().images_resampled, 1);+}++#[test]+fn zero_area_signature_appearances_do_not_block_downsampling() {+    for rect in [[0.0, 0.0, 0.0, 0.0], [80.0, 80.0, 80.0, 440.0], [80.0, 80.0, 440.0, 80.0]] {+        for empty_bbox in [false, true] {+            let (mut doc, image, form, annot) = fixture();+            let page = pdfcraft_model::pages(&doc)[0].obj;+            let root = doc.root().unwrap();+            let mut fields = Dict::new();+            fields.set(b"Fields".to_vec(), Object::Array(vec![Object::Ref(annot)]));+            doc.update_dict(root, |d| d.set(b"AcroForm".to_vec(), Object::Dict(fields))).unwrap();+            doc.update_dict(annot, |d| {+                d.set(b"Subtype".to_vec(), Object::name("Widget"));+                d.set(b"FT".to_vec(), Object::name("Sig"));+                d.set(b"Rect".to_vec(), numbers(&rect));+            })+            .unwrap();+            if empty_bbox {+                let Object::Stream(mut appearance) = doc.get(form).as_ref().clone() else { panic!() };+                appearance.dict.set(b"BBox".to_vec(), numbers(&[0.0, 0.0, 0.0, 0.0]));+                doc.set(form, appearance);+            }+            assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&1440.0), "rect={rect:?}, empty_bbox={empty_bbox}");+            let mut settings = Settings::default();+            settings.gray.compression = Compression::Flate;+            assert_eq!(optimize(&mut doc, &settings).unwrap().images_resampled, 1);+            let Object::Stream(image) = doc.get(image).as_ref().clone() else { panic!() };+            assert_eq!(image.dict.int(b"Width"), Some(75));+        }+    }+}++#[test]+fn invalid_appearance_rectangles_still_disable_downsampling() {+    for rect in [Object::Null, numbers(&[0.0, 0.0, 1.0]), numbers(&[0.0, 0.0, f64::INFINITY, 1.0]), numbers(&[0.0, 0.0, f64::NAN, 1.0])] {+        let (mut doc, image, _, annot) = fixture();+        let page = pdfcraft_model::pages(&doc)[0].obj;+        doc.update_dict(annot, |d| d.set(b"Rect".to_vec(), rect)).unwrap();+        assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&f64::INFINITY));+    }+    let (mut doc, image, _, annot) = fixture();+    let page = pdfcraft_model::pages(&doc)[0].obj;+    doc.update_dict(annot, |d| {+        d.remove(b"Rect");+    })+    .unwrap();+    assert_eq!(effective_resolutions(&doc, &[page]).get(&image), Some(&f64::INFINITY));+}diff --git a/docs/releasing.md b/docs/releasing.md--- a/docs/releasing.md+++ b/docs/releasing.md@@ -69,13 +69,20 @@ pinned in `release.yml` and builds with `CRAFT_FONTS_DIR` and `CRAFT_FONTS_REQUI embed its Japanese fonts and fail rather than ship without them (`AGENTS.md` §1.4). Bump the pin deliberately. -**OCR models:** every desktop package ships the Scan & OCR models (#103). The packaging scripts call-`stage_models` (`packaging/env.sh`; `package.ps1` on Windows), which runs `cargo xtask models` to-fetch every `kind = "model"` file in `ATTRIBUTION.toml` (verified by SHA-256) with its licence text-and an `ATTRIBUTION.txt`, straight into the package, so packaging needs the network. They go where-`pdfcraft_ocr::Models::dirs_beside_exe` looks: `models\` beside `pdfcraft.exe` (MSI, portable zip),-`PdfCraft.app/Contents/Resources/models` (macOS), and `share/pdfcraft/models` beside `bin/`-(deb, rpm, tar.gz, AppImage, Flatpak, FreeBSD). The web build doesn't include them yet.+**OCR models:** desktop packaging stages the Scan & OCR models (#103).+The packaging scripts call `stage_models` (`packaging/env.sh`; `package.ps1` on Windows), which runs `cargo xtask models` to fetch every `kind = "model"` file in `ATTRIBUTION.toml` and verify its SHA-256.+The current detection and recognition files are `text-detection.onnx` and `text-recognition.onnx`.+Each model ships with its licence text and the directory's `ATTRIBUTION.txt`, so packaging needs the network.+Upstream announced the legacy RTEN download URLs' brownout in [ocrs #282](https://github.com/robertknight/ocrs/issues/282) on 2026-10-27 and directs consumers to the ONNX models.++Runtime discovery prefers a complete ONNX detection/recognition pair within each directory, then a complete legacy `text-detection.rten` / `text-recognition.rten` pair.+An incomplete ONNX update can therefore use an intact RTEN pair in the same directory.+Discovery never combines model formats or files from different directories.+Directory precedence stays the same: `$PDFCRAFT_MODELS`, the release-package locations beside the executable, then the source tree's `assets/models/`.+The first directory with a complete supported pair wins, even if an earlier legacy pair precedes a later ONNX pair.++The release-package locations are `models\` beside `pdfcraft.exe` (MSI, portable zip), `PdfCraft.app/Contents/Resources/models` (macOS), and `share/pdfcraft/models` beside `bin/` (deb, rpm, tar.gz, AppImage, Flatpak, FreeBSD).+The web build does not include these models, and OCR remains limited to the Latin alphabet without accents.  ### macOS diff --git a/docs/roadmap.md b/docs/roadmap.md--- a/docs/roadmap.md+++ b/docs/roadmap.md@@ -1,5 +1,8 @@ # PdfCraft roadmap: milestones and what's next +> **Last reviewed:** 2026-10-27 | **Last updated:** 2026-10-27 | **Change:** minor (M11 #4996: optimization measures all annotation appearance uses; estimates unchanged)++> **Last reviewed:** 2026-10-27 | **Last updated:** 2026-10-27 | **Change:** minor (M10: OCR model downloads use verified ONNX pairs with legacy discovery fallback; estimates unchanged) > **Last reviewed:** 2026-10-25 | **Last updated:** 2026-10-25 | **Change:** minor (M14 localization current state: Arabic logical catalog, explicit RTL label direction and mirrored stock chrome; native/browser RTL workflows unqualified. Native release Hans face via LXGW WenKai Lite; estimates unchanged) > **Last reviewed:** 2026-10-23 · **Last updated:** 2026-10-23 · **Change:** minor (M7, #2636 partial: pasting added text boxes and images in the engine and through `content_copy`; estimates unchanged) > **Last reviewed:** 2026-10-23 · **Last updated:** 2026-10-23 · **Change:** minor (M11: image optimization accounts for page-local UserUnit; estimates unchanged)@@ -193,8 +196,8 @@ total matches the parity estimate. | M7 | Content editing (text, images, header/footer, watermark) | 250–500 | 20% | 100–180 | Done: header & footer, watermarks, backgrounds, Bates; added text and images that stay editable (move, resize, format, rotate, flip, crop, replace); links (Link tool, Link Properties, create from URLs, remove all). Missing: editing existing text and images in place (the longest pole), image/PDF watermarks | | M8 | Security + redaction | 100–180 | 68% | 15–30 | Done: opening protected documents, permissions, Protect Using Password, Remove security; redaction (mark text/areas/pages, Search & Redact with patterns, apply removing glyphs, image pixels, vectors, XObject content, annotations and fields, verification, full rewrite on save); Remove hidden information and Sanitize.<br>Done since: redaction codes (U.S. FOIA and Privacy Act sets as overlay text, in Redaction properties and `redact_mark`). Redaction cleans indirect and form structure references, preserving surviving form tags. Redaction retires inherited resource dictionaries, so a parent `/Pages` can't keep a removed XObject reachable; unredacted pages are left untouched. Image redaction removes alternate images and unsupported masks only on affected images. Changed marked-content spans lose replacement descriptions; untouched spans keep theirs. Hidden-text cleanup retires unused named descriptions across inherited/shared page and form resources (64 focused tests and scoped Rust 1.96 Clippy/wasm checks pass; publication CI pending). The redaction audit counts a filled cover that hides half of any one word of a long line (#2662), applied or not. Phone-pattern redaction also matches labelled Italian geographic numbers with `+39` and a slash after the area code (#3205 follow-up); unlabelled international slash numbers remain unsupported. Missing: certificate security, custom redaction code sets and pattern locales, DCT re-encoding | | M9 | Signatures (PAdES, validation) | 160–280 | 60% | 40–70 | Done: new `sign` crate (DER, X.509, CMS, PKCS #12 on RustCrypto; aws-lc-rs for RSA private keys); PAdES B-B signing (visible, invisible, existing fields, certification with DocMDP); validation with trust store and changes-after-signing classification; self-signed digital IDs; Signatures panel, message bar, sign dialogs; agent tools. Checked with pdfsig and OpenSSL.<br>Done since: macOS Keychain and Windows Current User Personal store signing (software-backed CNG keys), certificate viewer, signed documents protected from rewrites, page validation overlays separate from original signature artwork (baked Not Verified captions remain; #79 open), snapshot tabs that reuse instead of multiplying. Missing: timestamps (B-T), LTV (DSS, OCSP, CRL), FieldMDP, smart cards, PKCS #11 |-| M10 | OCR, create, export, print | 200–350 | 33% | 120–215 | Done: create from blank/text/PNG/JPEG/TIFF (multi-page)/GIF/BMP; export PNG/JPEG/TIFF and text; Print (Acrobat's sizing, n-up, booklet, poster, comments & forms, preview, the CUPS and Windows spoolers, print-ready PDF).<br>Done since: embedded/72/custom DPI choices for image imports; export all images; OCR (searchable image for pages, ranges and multiple files); single-sided cut-and-stack imposition with cut marks, through Print and doc_print; architectural/ANSI/large ISO sheet presets, Match page at 100% and PDF-scale preview (#599 follow-up); Create PDF from multiple files (PDFs, images and text, edited as pages in the grid); OCR can also read pictures on pages that have text, without adding that text twice (#1028). Done since: Word invoice export uses borderless inferred tables, floating positioned text/artwork only where text overlaps an image or is rotated, and per-page sizes, covered by synthetic invoice automation regressions (#773); printing on the web through the browser's print dialog, sheets drawn by PdfCraft (gap 4). Lossless image export applies per-component Decode, including Indexed mapping (#1201 partial). Existing 16-bit precision and color-management limits remain. #3532 partial: vector printing retains existing appearances with absent Subtype through a print-owned Form copy; source artwork stays unchanged. #3120: Office export reads form XObjects 50 levels deep, as the renderer does, and up to 50 000 a page; pages past that are listed in `truncated_pages`. Word table cells use the grid columns they span, including cells after a merged cell. Done since: text in a table slot covered by another cell's span remains as paragraph text in Word, HTML, RTF and PowerPoint. Missing: OCR languages beyond Latin, editable-text OCR output, full Office layout fidelity/import. Done since: dotted-leader contents entries stay in separate complete paragraphs (#1757) #2156 candidate: last selected print paper persists across sessions; other print preferences unchanged. Image export applies soft-mask Decode opacity intervals before resizing, including indirect entries and inverted ranges. Vector printing gives annotation appearances unoccupied XObject names so existing page artwork survives. |-| M11 | Optimize, preflight, PDF/A/X/UA, print production | 200–350 | 18% | 80–140 | Done: new `optimize` crate: Reduce File Size and the PDF Optimizer (images measured where drawn, bicubic downsampling, JPEG/ZIP recompression only when smaller, discard objects and user data, Flate clean-up, resource merging, object streams). Target-version compatibility is partial: newer content is not detected or converted. Content stream boundaries preserve comments, split operands and graphics state when image fragments merge. Image downsampling includes page-local `/UserUnit` scaling when measuring physical resolution. Done since: reject invalid image and soft-mask dimensions with checked sizing (#2178). Missing: fonts and transparency panels, space audit, preflight, PDF/A/X/UA |+| M10 | OCR, create, export, print | 200–350 | 33% | 120–215 | Done: create from blank/text/PNG/JPEG/TIFF (multi-page)/GIF/BMP; export PNG/JPEG/TIFF and text; Print (Acrobat's sizing, n-up, booklet, poster, comments & forms, preview, the CUPS and Windows spoolers, print-ready PDF).<br>Done since: embedded/72/custom DPI choices for image imports; export all images; OCR (searchable image for pages, ranges and multiple files); single-sided cut-and-stack imposition with cut marks, through Print and doc_print; architectural/ANSI/large ISO sheet presets, Match page at 100% and PDF-scale preview (#599 follow-up); Create PDF from multiple files (PDFs, images and text, edited as pages in the grid); OCR can also read pictures on pages that have text, without adding that text twice (#1028). Model staging fetches SHA-256-verified ONNX detection/recognition files after the upstream RTEN URL brownout; runtime discovery prefers complete ONNX pairs per directory, then complete legacy RTEN pairs, without mixing formats or changing directory precedence. Done since: Word invoice export uses borderless inferred tables, floating positioned text/artwork only where text overlaps an image or is rotated, and per-page sizes, covered by synthetic invoice automation regressions (#773); printing on the web through the browser's print dialog, sheets drawn by PdfCraft (gap 4). Lossless image export applies per-component Decode, including Indexed mapping (#1201 partial). Existing 16-bit precision and color-management limits remain. #3532 partial: vector printing retains existing appearances with absent Subtype through a print-owned Form copy; source artwork stays unchanged. #3120: Office export reads form XObjects 50 levels deep, as the renderer does, and up to 50 000 a page; pages past that are listed in `truncated_pages`. Word table cells use the grid columns they span, including cells after a merged cell. Done since: text in a table slot covered by another cell's span remains as paragraph text in Word, HTML, RTF and PowerPoint. Missing: OCR languages beyond Latin, editable-text OCR output, full Office layout fidelity/import. Done since: dotted-leader contents entries stay in separate complete paragraphs (#1757) #2156 candidate: last selected print paper persists across sessions; other print preferences unchanged. Image export applies soft-mask Decode opacity intervals before resizing, including indirect entries and inverted ranges. Vector printing gives annotation appearances unoccupied XObject names so existing page artwork survives. |+| M11 | Optimize, preflight, PDF/A/X/UA, print production | 200–350 | 18% | 80–140 | Done: new `optimize` crate: Reduce File Size and the PDF Optimizer (images measured where drawn, bicubic downsampling, JPEG/ZIP recompression only when smaller, discard objects and user data, Flate clean-up, resource merging, object streams). Target-version compatibility is partial: newer content is not detected or converted. Content stream boundaries preserve comments, split operands and graphics state when image fragments merge. Image downsampling includes page-local `/UserUnit` scaling and all annotation appearance states when measuring physical resolution. Done since: reject invalid image and soft-mask dimensions with checked sizing (#2178). Missing: fonts and transparency panels, space audit, preflight, PDF/A/X/UA | | M12 | Accessibility, compare, measure, search, XFA | 200–380 | 30% | 90–160 | Done: new `a11y` crate with the Accessibility Checker (all 32 rules, report, Fix/Skip/Explain, options dialog and results panel, agent tools). Locatable figure, annotation and form findings highlight their page bounds. Character encoding accepts Identity-H/V with Adobe CJK collections and predefined CJK CMaps independently (#3673); per-glyph validation and embedded-font extraction remain separate.<br>Done since: 2D distance, perimeter and area measurements with persistent viewport calibration, snapping, live information and CSV export; a bounded, text-only auto-tag proposal tool for one page. Marking a figure decorative rewrites only that stream (#1324). An XFA radio item whose value contains a space is written back unchanged (#1319). Done since: #2569: Growable multiline XFA text reflows after edits, with undo/redo, save/reopen and print coverage; oversized fields still become read-only page text. Structured-data synchronization warnings retain accepted whitespace without relayout from stale data. Done since: #2572: Every repeated page-master button resolves its own field to the shared XFA click script, including print buttons. Done since: #2570: Dynamic XFA rich-text fields carry exData/maxLength into AcroForm MaxLen for reported and enforced Unicode character limits, retained after save/reopen. Done since: #2589: Active XFA skips PDF-container fallback scripts while retaining requested messages and saved scripts; ordinary/static/unlaid-out documents and explicit console/field calls keep their existing behavior. Missing: writing proposed tags and parent tree, field autotag, visual Tags/Order/Content panels, Reading Order tool, compare, geospatial/3D measurement, search index, XFA<br>Reading colours: pending page backgrounds follow the configured colour (#2059). | | M13 | Automation (MCP, Action Wizard, CLI) + AI providers | 60–120 | 45% | 25–45 | Done: MCP resources (document info, text, page images); headless tool table (123 tools incl. signing, optimizing, initial view, links, stamps, data exchange, comment review, forms authoring and scripts, redaction, sanitize, print, add content), opt-in MCP server over stdio, CLI `run`/`tools` (closed stdout pipes exit cleanly), UI control channel with drag. Missing: Action Wizard, AI providers<br>Done since: CLI run and MCP reject missing, empty, repeated and option-valued --root arguments before any tool/script execution or MCP initialization. A single-hyphen directory name remains valid; synthetic CLI regressions preserve a would-be overwritten file. `text_find` also caps the per-page match ranges it materializes at the remaining result limit plus one continuation match. CLI robustness scans visit each directory once through overlapping roots and symlink aliases. | | M12 | Accessibility, compare, measure, search, XFA | 200–380 | 30% | 90–160 | Done: new `a11y` crate with the Accessibility Checker (all 32 rules, report, Fix/Skip/Explain, options dialog and results panel, agent tools). Locatable figure, annotation and form findings highlight their page bounds. Character encoding accepts Identity-H/V with Adobe CJK collections and predefined CJK CMaps independently (#3673); per-glyph validation and embedded-font extraction remain separate.<br>Done since: 2D distance, perimeter and area measurements with persistent viewport calibration, snapping, live information and CSV export; a bounded text-only auto-tag proposal tool; text-only Apply writes marked content, paragraph tags and a parent tree, then opens the Tags panel for review. Missing: mixed-content and field autotag, heading/list/table/figure classification, Order/Content panels, Reading Order tool, compare, geospatial/3D measurement, search index, XFA<br>Done since: CLI info, text and render use dynamic XFA layout pages, with synthetic count/text/pixel and copy-permission regressions (#1645).<br>Reading colours: pending page backgrounds follow the configured colour (#2059). |@@ -242,6 +245,10 @@ M2 (renderer), M7 (content editing), M10 (OCR and Office), M11 (Preflight and st  ## Revision history +**2026-10-27 | minor:** M11 #4996: image optimization includes every normal, rollover and pressed annotation appearance state at its physical placement size.+Malformed geometry and exhausted traversal budgets prevent downsampling.+Estimates unchanged.+ **2026-10-25 · minor:** M14 localization current state: Arabic catalogs stay logical, Arabic/Kurdish labels take an explicit RTL direction, and stock chrome mirrors; native/browser RTL input, IME and complex workflows remain unqualified. Native Simplified Chinese uses LXGW WenKai Lite; the web build has no Hans face. Estimates unchanged.  **2026-10-23 · minor:** M11: image downsampling uses physical resolution after page-local `/UserUnit` scaling, including shared nested forms.@@ -254,6 +261,7 @@ Estimates unchanged.  | Date | Change | Summary | |---|---|---|+| 2026-10-27 | minor | M10: OCR stages SHA-256-verified ONNX models after the upstream RTEN URL brownout; discovery prefers a complete ONNX pair per directory, falls back to a complete RTEN pair, and never mixes formats or directories; language/web limits and estimates unchanged. | | 2026-10-24 | minor | M3, #3970 follow-up: the button release given back after a window move or resize goes before the pointer leaving, so hover doesn't stick. Estimates unchanged. | | 2026-10-23 | minor | M8, #3205 follow-up: phone-pattern redaction includes the +39 country code on labelled Italian geographic numbers with a slash; dates, protocol identifiers and whole-run guards remain. Estimates unchanged. | | 2026-10-23 | minor | M10: Word table cells use the grid columns they span, including cells after a merged cell; synthetic export regression and CLI reproduction. Estimates unchanged. |diff --git a/packaging/windows/test-msi.ps1 b/packaging/windows/test-msi.ps1--- a/packaging/windows/test-msi.ps1+++ b/packaging/windows/test-msi.ps1@@ -288,13 +288,16 @@ $modelsDir = Read-Row 'SELECT `Directory_Parent`, `DefaultDir` FROM `Directory` Assert-Equal $modelsDir[0] 'INSTALLFOLDER' 'OCR models folder parent' # DefaultDir is `models`, or `SHORT|models` with a generated 8.3 name. if ($modelsDir[1] -notmatch '(^|\|)models$') { throw "OCR models folder name: '$($modelsDir[1])'" }-$modelFiles = 0+$modelFiles = @() $view = $Database.OpenView('SELECT `File`.`FileName` FROM `File`, `Component` WHERE `File`.`Component_` = `Component`.`Component` AND `Component`.`Directory_` = ''ModelsFolder''') try {   [void] $view.Execute()-  while ($record = $view.Fetch()) { if ($record.StringData(1) -match '\.rten$') { $modelFiles++ } }+  # FileName can contain a generated 8.3 name before the long name: SHORT|long.+  while ($record = $view.Fetch()) { $modelFiles += ($record.StringData(1) -split '\|')[-1] } } finally { [void] $view.Close() }-if ($modelFiles -lt 2) { throw "expected the OCR models (*.rten) in models\, found $modelFiles" }+foreach ($model in @('text-detection.onnx', 'text-recognition.onnx')) {+  Assert-Equal (@($modelFiles | Where-Object { $_ -ceq $model }).Count) 1 "OCR model $model in models\"+} $rm = Read-Row 'SELECT `Dialog` FROM `Dialog` WHERE `Dialog` = ''MsiRMFilesInUse''' 1 Assert-Equal $rm[0] 'MsiRMFilesInUse' 'Files-in-use dialog' [void] [Runtime.InteropServices.Marshal]::FinalReleaseComObject($Database)diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -6544,8 +6544,8 @@ milestone = "M10" status = "shipped" commands = ["ocr.recognize"] tools = ["ocr_recognize", "ocr_status"]-evidence = ["crates/engine/src/tests.rs::recognize_text_makes_a_scanned_page_searchable", "crates/automation/tests/it/automation.rs::ocr_tools_make_a_scan_searchable", "crates/ui-egui/tests/it/ocr.rs::recognize_text_dialog_adds_searchable_text"]-notes = "ocrs engine (MIT/Apache) with CC-BY-SA models fetched by `cargo xtask models`; the desktop release packages ship them (#103; packaging/env.sh stage_models), the web build does not yet; runs on a worker thread with progress; one undoable step; pages with text are skipped unless asked (ocr.skip-renderable-text)."+evidence = ["crates/ocr/src/lib.rs::model_discovery_prefers_the_complete_onnx_pair", "crates/ocr/src/lib.rs::model_discovery_falls_back_to_legacy_when_the_onnx_pair_is_incomplete", "crates/ocr/src/lib.rs::model_discovery_refuses_mixed_format_pairs", "crates/ocr/src/lib.rs::model_discovery_keeps_legacy_only_installations", "crates/engine/src/tests.rs::recognize_text_makes_a_scanned_page_searchable", "crates/automation/tests/it/automation.rs::ocr_tools_make_a_scan_searchable", "crates/ui-egui/tests/it/ocr.rs::recognize_text_dialog_adds_searchable_text"]+notes = "ocrs engine (MIT/Apache) with CC-BY-SA ONNX models fetched and SHA-256 checked by `cargo xtask models`; within each search directory, a complete ONNX pair takes precedence over a complete legacy RTEN pair, incomplete and mixed-format pairs are not combined, and directory precedence is unchanged; the desktop release packages ship them (#103; packaging/env.sh stage_models), the web build does not yet; runs on a worker thread with progress; one undoable step; pages with text are skipped unless asked (ocr.skip-renderable-text)."  [[feature]] id = "ocr.recognize-range"@@ -7348,8 +7348,8 @@ milestone = "M11" status = "partial" commands = ["optimize.advanced"] tools = ["doc_optimize"]-evidence = ["crates/optimize/tests/optimize.rs::images_are_measured_where_drawn_and_downsampled", "crates/optimize/tests/optimize.rs::settings_choose_what_happens", "crates/automation/tests/it/automation.rs::optimizing_through_tools", "crates/optimize/tests/optimize.rs::a_page_picture_printed_as_one_pixel_strips_is_merged_into_one_image", "crates/optimize/tests/optimize.rs::images_that_do_not_tile_a_picture_are_left_alone", "crates/optimize/tests/optimize.rs::a_deeply_nested_form_is_measured_at_its_largest_use", "crates/optimize/tests/optimize.rs::images_are_not_resampled_when_some_uses_could_not_be_measured", "crates/optimize/tests/optimize.rs::image_resolution_uses_the_pages_physical_unit", "crates/optimize/tests/optimize.rs::optimization_retains_an_image_below_threshold_on_a_scaled_page", "crates/optimize/tests/optimize.rs::shared_forms_are_measured_in_each_pages_physical_units"]-notes = "colour and grayscale, bicubic, effective resolution measured where drawn (every use, with page-local UserUnit scaling, through nested forms up to 12 deep; when a use can't be measured, nothing is resampled); image fragments stacked edge to edge are merged first (side-by-side tiles not yet); monochrome not yet"+evidence = ["crates/optimize/tests/appearances.rs::appearance_use_keeps_shared_image_at_its_largest_size", "crates/optimize/tests/appearances.rs::all_appearance_states_and_physical_page_units_are_measured", "crates/optimize/tests/optimize.rs::images_are_measured_where_drawn_and_downsampled", "crates/optimize/tests/optimize.rs::settings_choose_what_happens", "crates/automation/tests/it/automation.rs::optimizing_through_tools", "crates/optimize/tests/optimize.rs::a_page_picture_printed_as_one_pixel_strips_is_merged_into_one_image", "crates/optimize/tests/optimize.rs::images_that_do_not_tile_a_picture_are_left_alone", "crates/optimize/tests/optimize.rs::a_deeply_nested_form_is_measured_at_its_largest_use", "crates/optimize/tests/optimize.rs::images_are_not_resampled_when_some_uses_could_not_be_measured", "crates/optimize/tests/optimize.rs::image_resolution_uses_the_pages_physical_unit", "crates/optimize/tests/optimize.rs::optimization_retains_an_image_below_threshold_on_a_scaled_page", "crates/optimize/tests/optimize.rs::shared_forms_are_measured_in_each_pages_physical_units"]+notes = "colour and grayscale, bicubic, effective resolution measured where drawn (page content and all N/R/D annotation appearance states, with page-local UserUnit scaling, through nested forms up to 12 deep; when a use can't be measured, nothing is resampled); image fragments stacked edge to edge are merged first (side-by-side tiles not yet); monochrome not yet"  [[feature]] id = "optimize.recompress-images"