diff --git a/AGENTS.md b/AGENTS.md index 297105c4c6..cc9ba84bb3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -202,3 +202,7 @@ PdfCraft opens files from strangers, and people trust it with their work. A malf - Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here. - Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time. - Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it. + +## Shared-library reuse + +Before implementing file formats, codecs, protocols or reusable UI components, check [storytold/craft-libs](https://github.com/storytold/craft-libs) and relevant open PRs; reuse or extend shared APIs instead of duplicating app-local implementations. Keep app-specific policy and adapters local, preserve behavior and format fidelity, security and resource limits, repository layering and regression tests, and document any shared-API gap rather than silently reducing functionality. diff --git a/Cargo.lock b/Cargo.lock index d4574f9694..a0b620e350 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1231,7 +1231,17 @@ name = "craft-flow" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ - "craft-wordmodel", + "craft-wordmodel 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", + "image", + "log", +] + +[[package]] +name = "craft-flow" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33" +dependencies = [ + "craft-wordmodel 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33)", "image", "log", ] @@ -1241,6 +1251,11 @@ name = "craft-fonts-build" version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +[[package]] +name = "craft-html" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33" + [[package]] name = "craft-i18n" version = "0.1.0" @@ -1304,6 +1319,16 @@ dependencies = [ "serde_json", ] +[[package]] +name = "craft-markdown" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33" +dependencies = [ + "craft-flow 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33)", + "craft-html", + "craft-wordmodel 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33)", +] + [[package]] name = "craft-math" version = "0.1.0" @@ -1313,6 +1338,15 @@ dependencies = [ "serde", ] +[[package]] +name = "craft-math" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33)", + "serde", +] + [[package]] name = "craft-menu" version = "0.1.0" @@ -1380,7 +1414,7 @@ dependencies = [ [[package]] name = "craft-pkcs7" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f#2284a6bf76b3d629b5e5bb2ca71e1c319d8dae5f" +source = "git+https://github.com/storytold/craft-libs?rev=bc4aae37bb219049596327fca971046dab0b78be#bc4aae37bb219049596327fca971046dab0b78be" dependencies = [ "aes", "aws-lc-rs", @@ -1449,9 +1483,9 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "craft-codepage", - "craft-flow", + "craft-flow 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "craft-metafile", - "craft-wordmodel", + "craft-wordmodel 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "encoding_rs", "image", ] @@ -1558,7 +1592,7 @@ version = "0.1.0" source = "git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3#b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3" dependencies = [ "base64 0.23.1", - "craft-math", + "craft-math 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "log", "serde", "serde_json", @@ -1566,6 +1600,47 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "craft-wordmodel" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33" +dependencies = [ + "base64 0.23.1", + "craft-math 0.1.0 (git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33)", + "log", + "serde", + "serde_json", + "thiserror 2.0.21", + "unicode-segmentation", +] + +[[package]] +name = "craft-xfa" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3#23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3)", +] + +[[package]] +name = "craft-xfdf" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72#e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72" +dependencies = [ + "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72)", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3#23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-xml" version = "0.1.0" @@ -1585,6 +1660,26 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72#e5f00f5bf658ef2cce7bf96fbbd769a6fff5bc72" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=fbb3f47f7ae712192586984af11ca8f6c0f64d33#fbb3f47f7ae712192586984af11ca8f6c0f64d33" +dependencies = [ + "quick-xml 0.41.0", + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -4640,6 +4735,7 @@ dependencies = [ "pdfcraft-fonts", "pdfcraft-geom", "pdfcraft-model", + "proptest", "thiserror 2.0.21", ] @@ -4818,10 +4914,11 @@ dependencies = [ name = "pdfcraft-export" version = "0.21.1" dependencies = [ + "craft-markdown", "craft-mime", "craft-opc", "craft-rtf", - "craft-wordmodel", + "craft-wordmodel 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b8eaa8e2f3561f9fa27fcf3b7f67ce915e93caa3)", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "craft-zip", "flate2", @@ -5171,6 +5268,7 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-image-codecs", + "craft-xfa", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-cos", "pdfcraft-fonts", @@ -5184,6 +5282,7 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-csv", + "craft-xfdf", "craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2dc681a3)", "pdfcraft-annot", "pdfcraft-cos", diff --git a/Cargo.toml b/Cargo.toml index 5d6c178550..b16ead7a1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/pdfcraft" [workspace.dependencies] +craft-markdown = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "fbb3f47f7ae712192586984af11ca8f6c0f64d33" } # Small, revision-bound UI drafts; the existing engine remains the owner of PDF edits/history. edit-session = { path = "vendor/edit-session", version = "=0.1.0-alpha.2" } # Shared frontend components; engines remain independent of egui. diff --git a/ROADMAP.md b/ROADMAP.md index 56bbcd4c74..adb9fcbe10 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -353,6 +353,7 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md - **2026-10-26 (M3, view.text-select):** Text selection runs across pages: dragging onto another page (also a quick flick off the page, and the edge and wheel scrolling of #721/#1670) or ⇧-clicking there selects the rest of the first page, the pages between and the last page up to the pointer. Copy has each page's part on a line of its own, reading pages not yet read off the UI thread; highlight, underline, strikethrough and Redact put one mark on each page in one undo step (a tool that finishes first marks once the pages are read; Redact refuses a selection over a page whose text could not be read); Add comment and Replace text stay one-page. A drag from one page's text that is already over the next when it starts no longer begins a Redact or area-highlight box there. Estimates unchanged. - **2026-10-25 (M5, comment replies):** One reply can be deleted from the Comments panel without its thread. On the selected card each reply has its own "…" menu (Edit, Copy text, Delete); Delete takes that reply with its pop-up and the replies to it, as `comment_delete` already did headlessly, and the comment and its other replies stay. The card stays selected, and the panel's editor stays open, on what they were on though the delete moved it in `/Annots`. Undo and Redo close the editor, since either can move what it was open on. A locked reply can't be deleted. Estimates unchanged. - **2026-10-25 (M7, #2750):** Single-style Keep source fonts edits retain source glyphs and batch only missing glyphs in fallback fonts. Per-rewrite character eligibility and widths are cached; source and fallback edge spaces stay extractable. Estimates unchanged. +- **2026-10-25 (M10, #4399):** The printer picker has a wider searchable list, wrapped device names, stable selection while filtering, and Search and Save as PDF kept visible. Synthetic fleet UI regressions cover long lists and a smaller window. Estimates unchanged. - **2026-10-25 (web, same-document host mode):** A page that loads the PdfCraft web module into its own document (no iframe) can opt into the host bridge with `data-host="page"` on the app's canvas — the exact value `page`; any other value keeps the normal app with downloads, unless a same-origin framed `?host=parent` selects the parent bridge (unframed or cross-origin, `?host=parent` also keeps the normal app). It is the `?host=parent` bridge with the transport swapped: the same five messages travel as non-bubbling `CustomEvent`s on the canvas (`detail` holds the parent messages' fields without `type`; the host answers with a `pdfcraft:open` event), so saves reach the host instead of downloading and dirty and start-up-failure reporting are unchanged. Unit tests cover the opt-in rule; no parity or estimate changes. - **2026-10-25 (M10):** Vector printing keeps field and comment artwork whose rectangle coordinates are indirect, requiring exactly four finite resolved values. - **2026-10-26 (M10):** Vector printing follows up to 64 indirect references for annotation rectangles, coordinates and appearance-state names, rejecting cycles and longer chains without changing source objects. diff --git a/contributors/people.toml b/contributors/people.toml index 06c98f6464..a150fe5276 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -194,3 +194,6 @@ sync_github_name = false [people.enriquemelzer-bit] sync_github_name = true + +[people.Yousef-Dev921] +sync_github_name = true diff --git a/crates/annot/Cargo.toml b/crates/annot/Cargo.toml index e91c9ed896..1d09f0f8bc 100644 --- a/crates/annot/Cargo.toml +++ b/crates/annot/Cargo.toml @@ -18,3 +18,8 @@ thiserror = { workspace = true } [dev-dependencies] hayro-syntax = { workspace = true } +# Property tests over generated annotation geometry and metadata (`tests/properties.rs`). Same +# workspace dependency the sibling test suites already carry: `crates/cos/Cargo.toml` declares it as +# `proptest = { workspace = true }`, and `crates/crypt`/`crates/filters` use the dotted form +# `proptest.workspace = true`. Nothing new enters the dependency graph. +proptest = { workspace = true } diff --git a/crates/annot/src/links.rs b/crates/annot/src/links.rs index 88ac097b74..570bb7f7d3 100644 --- a/crates/annot/src/links.rs +++ b/crates/annot/src/links.rs @@ -517,6 +517,12 @@ const MAX_URL_CHARS: usize = 2048; /// it would open with a scheme the text doesn't say. Brackets inside a web address belong to it /// when they pair up (`…/wiki/Mercury_(planet)`); an unpaired one ends it. pub fn find_urls(chars: &[char]) -> Vec<(std::ops::Range, String)> { + find_urls_with_scan_observer(chars, |_| {}) +} + +/// Observe each candidate scan offset before reading it. The production caller is a no-op; +/// regression tests assert the work bound directly, independent of machine speed. +pub(crate) fn find_urls_with_scan_observer(chars: &[char], mut observe: impl FnMut(usize)) -> Vec<(std::ops::Range, String)> { let url_char = |c: char| !c.is_whitespace() && !"<>\"'()[]{}".contains(c); // Characters found inside an address but not between words: a `www.` after one is part of // a longer address, and so is an e-mail address after a slash. @@ -542,6 +548,7 @@ pub fn find_urls(chars: &[char]) -> Vec<(std::ops::Range, String)> { too_long = true; break; } + observe(j.saturating_sub(i)); match chars.get(j) { Some('(') => { if depth == 0 { diff --git a/crates/annot/src/tests.rs b/crates/annot/src/tests.rs index 980959d4d9..2a84263cdd 100644 --- a/crates/annot/src/tests.rs +++ b/crates/annot/src/tests.rs @@ -1459,9 +1459,18 @@ fn urls_keep_their_written_scheme_and_paired_brackets() { fn unclosed_brackets_in_text_without_spaces_are_read_once_per_address() { let found = |s: &str| -> Vec { crate::links::find_urls(&s.chars().collect::>()).into_iter().map(|(_, u)| u).collect() }; let text = format!("www.before.org {} www.after.org", "https://a/(".repeat(60_000)); - let started = std::time::Instant::now(); - assert_eq!(found(&text), ["http://www.before.org", "http://www.after.org"]); - assert!(started.elapsed().as_secs() < 5, "{:?}", started.elapsed()); + let chars: Vec = text.chars().collect(); + let mut reads = 0usize; + let mut furthest = 0usize; + let urls = crate::links::find_urls_with_scan_observer(&chars, |offset| { + // Fail on the first over-budget read, rather than waiting for a quadratic scan. + assert!(offset < 2048, "candidate read beyond its 2048-character bound: {offset}"); + reads += 1; + furthest = furthest.max(offset); + }); + assert_eq!(urls.into_iter().map(|(_, u)| u).collect::>(), ["http://www.before.org", "http://www.after.org"]); + assert_eq!(furthest, 2047, "the hostile fixture must exercise the scan boundary"); + assert!(reads <= (60_000 + 2) * 2048, "aggregate candidate work: {reads}"); let long = format!("https://example.com/{} www.after.org", "a".repeat(3000)); assert_eq!(found(&long), ["http://www.after.org"]); let fits = format!("https://example.com/{}", "a".repeat(2000)); diff --git a/crates/annot/tests/malformed.proptest-regressions b/crates/annot/tests/malformed.proptest-regressions new file mode 100644 index 0000000000..341ba3ff3d --- /dev/null +++ b/crates/annot/tests/malformed.proptest-regressions @@ -0,0 +1,6 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. diff --git a/crates/annot/tests/malformed.rs b/crates/annot/tests/malformed.rs new file mode 100644 index 0000000000..6b8f007de7 --- /dev/null +++ b/crates/annot/tests/malformed.rs @@ -0,0 +1,872 @@ +//! Damaged annotation dictionaries: what the reader does, and what it must never do. +//! +//! **These are never-crash tests.** They assert that a stranger's broken annotation data produces an +//! error or a defined degradation instead of a panic. Where the current behaviour is questionable, +//! that is said out loud and pinned as it is: an expected value below is *what PdfCraft does today*, +//! not a claim that it is right. Each such place carries a `NOTE` comment naming the alternative a +//! reader could reasonably choose, so a later fix shows up as a changed expectation here rather than a +//! silent edit. (`AGENTS.md` §4 and `CLAUDE.md` Fidelity: no crash, and never repair silently.) +//! +//! The shapes come from two directions: bytes written by hand (a file we did not make, including +//! tokens our typed API cannot even express — a three-number quadrilateral or an unknown keyword), and +//! values handed to the builder (`add_annotation` with NaN in a style or geometry). + +mod support { + pub mod bytes; + pub mod fixture; +} + +use pdfcraft_annot::{ + AnnotError, AttachIcon, Meta, NewAnnotation, NoteIcon, Shape, Style, add_annotation, delete_annotation, has_visible_fill_sign, links, page_refs, + picture_rotation, props, set_annots, signature_image, summaries, +}; +use pdfcraft_cos::{Dict, Document, ObjRef, Object}; +use proptest::prelude::*; +use std::sync::Arc; + +use support::bytes; +use support::fixture::{Save, annot_entries, blank, dict_of, name_of, nums, page_object_numbers, pdf, save_and_reopen}; + +/// Open a hand-written file that must parse well enough to be examined. +fn open(objs: &[&[u8]]) -> Document { + Document::open(Arc::new(pdf(objs))).expect("a damaged file still opens") +} + +/// A one-page file whose single annotation is `body`. +fn one_annot(body: &[u8]) -> Document { + open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [4 0 R] >>", + body, + ]) +} + +/// Run every annotation reader over a document. The point is that none of them panics; the counts +/// are checked by the callers, which know what the damage was. +fn read_everything(doc: &Document) { + let listed = summaries(doc); + let pages = page_refs(doc).map(|p| p.len()).unwrap_or(0); + for page in 0..pages { + let entries = annot_entries(doc, page); + for index in 0..entries.len() + 2 { + let _ = props(doc, page, index); + } + } + let _ = links::list(doc); + let _ = has_visible_fill_sign(doc); + for page in 0..pages { + let entries = annot_entries(doc, page); + for index in 0..entries.len() { + let _ = signature_image(doc, page, index); + let _ = picture_rotation(doc, page, index); + } + } + let _ = listed; +} + +// ── wrong-shaped numbers ─────────────────────────────────────────────────────────────────────── + +/// A `/Rect` that is not four numbers cannot place a comment. Today it reads as the empty rectangle +/// at the origin and the comment is still listed, so a viewer would draw it in the page corner. +/// NOTE: refusing the entry, or listing it at no position, are both defensible; pinning whichever +/// is chosen keeps the change honest. The invariant held here is "listed, with a finite rect, no +/// panic" — not that the position is meaningful. +#[test] +fn a_malformed_rectangle_reads_as_the_origin_instead_of_panicking() { + let bodies: Vec<&[u8]> = vec![ + b"<< /Type /Annot /Subtype /Square /Rect [10 20] >>", + b"<< /Type /Annot /Subtype /Square /Rect [1 2 3] >>", + b"<< /Type /Annot /Subtype /Square /Rect [1 2 3 4 5] >>", + b"<< /Type /Annot /Subtype /Square /Rect [/a /b /c /d] >>", + b"<< /Type /Annot /Subtype /Square /Rect (a string) >>", + b"<< /Type /Annot /Subtype /Square /Rect null >>", + // Colour components outside [0,1]: clamped on read rather than refused. + b"<< /Type /Annot /Subtype /Square /Rect [10 20 30 40] /IC [0.5 2 -0.5] >>", + ]; + for (n, body) in bodies.into_iter().enumerate() { + let doc = one_annot(body); + read_everything(&doc); + let listed = summaries(&doc); + assert_eq!(listed.len(), 1, "case {n}: the comment is still listed ({body:?})"); + assert!(listed[0].rect.iter().all(|v| v.is_finite()), "case {n}: a finite rectangle, got {:?}", listed[0].rect); + assert_eq!( + name_of(&doc, &dict_of(&doc, &annot_entries(&doc, 0)[0]).expect("a dictionary"), b"Subtype").as_deref(), + Some("Square"), + "case {n}" + ); + // Save it on and the damage must not turn into a crash or a file we cannot reopen. + for path in [Save::Incremental, Save::FullTable] { + let (_, reopened) = save_and_reopen(&doc, path); + read_everything(&reopened); + assert_eq!(summaries(&reopened).len(), 1, "case {n} after {path:?}"); + } + } +} + +/// `/QuadPoints` must come in eights (§12.5.6.10). A list that does not divide evenly is truncated +/// to whole quadrilaterals: seven numbers give none, twelve give one, and the leftover numbers are +/// dropped without a word. NOTE: "drop silently" is what happens today; reporting the odd tail would +/// be kinder. What is asserted here is that the chunking never over-reads and never panics. +#[test] +fn a_quad_list_that_is_not_a_multiple_of_eight_keeps_whole_quads_only() { + let cases: Vec<(&[u8], usize)> = vec![ + (b"[1 2 3 4 5 6 7]", 0), + (b"[1 2 3 4 5 6 7 8]", 1), + (b"[1 2 3 4 5 6 7 8 9]", 1), + (b"[1 2 3 4 5 6 7 8 9 10 11 12]", 1), + (b"[16 numbers follow: this token is not a number at all]", 0), + (b"[]", 0), + ]; + for (n, (values, want_quads)) in cases.into_iter().enumerate() { + let body = format!("<< /Type /Annot /Subtype /Highlight /Rect [10 20 60 30] /QuadPoints {} >>", String::from_utf8_lossy(values)); + let doc = one_annot(body.as_bytes()); + read_everything(&doc); + if String::from_utf8_lossy(values).contains("numbers follow") { + // An unparseable token inside the array loses the whole annotation object: see the note + // on `an_unparseable_token_...` below. It is listed in /Annots but reads as nothing. + assert!(summaries(&doc).is_empty(), "case {n}"); + continue; + } + let listed = summaries(&doc); + assert_eq!(listed.len(), 1, "case {n}: the markup is still listed"); + assert_eq!(listed[0].quads.len(), want_quads, "case {n}: {}", String::from_utf8_lossy(values)); + // The stored array is preserved verbatim: reading truncates, saving must not rewrite. + let d = dict_of(&doc, &annot_entries(&doc, 0)[0]).expect("a dictionary"); + let stored = nums(&doc, &d, b"QuadPoints").unwrap_or_default(); + let written = values.iter().filter(|c| c.is_ascii_digit() || **c == b' ').count(); + assert!(stored.len() <= written, "case {n}: we read at most what is there ({} vs {written})", stored.len()); + } +} + +/// A token the object parser cannot interpret (a bare word that is not a number, name, +/// reference, string, `true`, `false` or `null`) loses the object it sits in. Inside a page's +/// `/Annots` that costs the *whole page*: `page_refs()` comes back `Ok([])` and every annotation +/// on that page disappears — with an empty repair log. Precision matters here: `/Pages` still +/// shows `Count 1, Kids [3 0 R]`, so it is not that the annotation list alone became +/// unreachable; the page object itself fails to parse, and nothing downstream sees it. +/// NOTE: this is `pdfcraft-cos` parser behaviour, reached through the annotation readers. The +/// bytes below are HAND-BUILT by this test's `open` helper — no writer path emits a bare +/// keyword there; they model damage an outside editor or a truncated edit leaves behind. +/// +/// Cross-checked against qpdf core 12.2.0 via pikepdf 9.11 on these exact bytes (sha256 +/// 582e0eb0e8a25e6975c0727d405372d7df6c8dfcb06e0289817f5b28e546b94e, 447 B — hand-built and +/// not committed; re-create it from the object bodies below, in order). Running qpdf's check, +/// `pikepdf.Job(["--check", , "/dev/null"])`, exits 3 with warnings on process stderr, +/// verbatim: +/// `WARNING: (object 3 0, offset 191): unknown token while reading object; treating as string` +/// — note `object 3 0` and the offset; there is no `/Annots` in qpdf's wording. qpdf KEEPS the +/// page, so this loss is ours alone — and qpdf's warning shows it was detectable. The word +/// 'silently' below refers only to our empty `repair_log()`, never to qpdf, which complains. +/// +/// The honest claim here is limited to: no panic, an error or an empty page list, and +/// `add_annotation` refusing cleanly rather than writing onto a page it cannot see. Fixing the +/// parser should make this test fail, and that is exactly what it is for. +#[test] +fn an_unparseable_token_inside_annots_loses_the_page_and_says_nothing() { + let doc = open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [4 0 R notanumber] >>", + b"<< /Type /Annot /Subtype /Square /Rect [10 20 60 30] >>", + ]); + read_everything(&doc); + assert_eq!(page_refs(&doc).map(|p| p.len()), Ok(0), "the page object could not be read"); + assert!(doc.repair_log().is_empty(), "and nothing was recorded about losing it"); + assert!(page_object_numbers(&doc).is_empty(), "this file's own page walk agrees there is nothing to read"); + assert!(summaries(&doc).is_empty()); + + let mut try_add = doc.clone(); + let e = add_annotation( + &mut try_add, + &NewAnnotation { + page: 0, + shape: Shape::Rectangle { rect: [10.0, 10.0, 40.0, 30.0] }, + style: Style::default(), + contents: "x".into(), + author: "A".into(), + }, + &Meta { date: None, id: "i".into() }, + ) + .err(); + assert_eq!(e, Some(AnnotError::NoSuchPage(0)), "adding to the vanished page refuses rather than inventing a page"); + assert_eq!(links::list(&doc).len(), 0); +} + +// ── broken references ────────────────────────────────────────────────────────────────────────── + +/// Dangling, duplicated and wrong-typed entries in one `/Annots` array: each is skipped by the +/// readers, none panics, and deleting one leaves the others alone. A duplicated reference really is +/// listed twice — the two comments share a `Rect` because they *are* the same object. +#[test] +fn dangling_duplicate_and_wrong_typed_entries_are_skipped() { + let doc = open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [5 0 R 4 0 R 4 0 R (a string) null] >>", + b"<< /Type /Annot /Subtype /Square /Rect [10 20 60 30] >>", + b"[7 0 R]", + ]); + read_everything(&doc); + let entries = annot_entries(&doc, 0); + assert_eq!(entries.len(), 5, "the array keeps all five slots"); + let listed = summaries(&doc); + assert_eq!(listed.iter().map(|s| s.index).collect::>(), vec![1, 2], "only the two real dictionaries are comments"); + assert_eq!(listed[0].rect, listed[1].rect, "the duplicate is the same object"); + assert!(props(&doc, 0, 0).is_none(), "an entry naming an array is not a comment"); + assert!(props(&doc, 0, 3).is_none(), "a string entry is not a comment"); + assert_eq!(signature_image(&doc, 0, 1).ok().flatten(), None); + + // Deleting one of the duplicates removes **both** slots: `delete_annotation` drops every entry + // that names a doomed object, not just the index asked for (that is how it takes pop-ups and + // replies with their parent). So the shared dictionary leaves the page entirely. + let mut cut = doc.clone(); + delete_annotation(&mut cut, 0, 1).expect("deletes the comment"); + assert_eq!(annot_entries(&cut, 0).len(), 3, "both slots naming that object are gone"); + assert!(summaries(&cut).is_empty()); + let (_, reopened) = save_and_reopen(&cut, Save::FullTable); + assert!(summaries(&reopened).is_empty(), "and the file agrees"); + + // An `/Annots` that is not an array at all reads as "no annotations". + let weird = open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots (hello) >>", + ]); + read_everything(&weird); + assert!(annot_entries(&weird, 0).is_empty()); + let mut d = weird.clone(); + assert_eq!(delete_annotation(&mut d, 0, 0).err(), Some(AnnotError::NoSuchAnnotation { page: 0, index: 0 })); +} + +/// Reference loops must terminate. These are the shapes a generated or half-repaired file produces: +/// an annotation replying to itself, two replies replying to each other, and a pop-up chain whose +/// back-references point at the next annotation instead of the previous one. +#[test] +fn reference_loops_terminate_and_a_delete_takes_the_cycle_with_it() { + // Self-reference. + let self_irt = one_annot(b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /IRT 4 0 R >>"); + read_everything(&self_irt); + let mut cut = self_irt.clone(); + delete_annotation(&mut cut, 0, 0).expect("deletes the self-replying comment"); + assert!(annot_entries(&cut, 0).is_empty()); + + // Mutual references: deleting either must take both and stop. + let shared = open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [4 0 R 5 0 R] >>", + b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /IRT 5 0 R >>", + b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /IRT 4 0 R >>", + ]); + read_everything(&shared); + assert_eq!(summaries(&shared).len(), 2, "both halves of the pair are listed"); + let mut cut = shared.clone(); + delete_annotation(&mut cut, 0, 0).expect("deleting one of a mutual pair returns"); + assert!(annot_entries(&cut, 0).is_empty(), "the cycle is taken out together"); + + // A pop-up whose /Parent names a third annotation, which in turn names the first. + let chained = open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [4 0 R 5 0 R 6 0 R] >>", + b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /Popup 5 0 R >>", + b"<< /Type /Annot /Subtype /Popup /Rect [40 20 90 60] /Parent 6 0 R >>", + b"<< /Type /Annot /Subtype /Popup /Rect [40 20 90 60] /Parent 4 0 R >>", + ]); + read_everything(&chained); + assert_eq!(summaries(&chained).len(), 1, "only the text comment is a comment"); + let mut cut = chained.clone(); + delete_annotation(&mut cut, 0, 0).expect("deleting the owner of a pop-up chain returns"); + assert!(annot_entries(&cut, 0).is_empty(), "and the whole chain goes with it"); + + // A `/Popup` that names a missing object must not stop the comment being used. + let dangling_popup = one_annot(b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /Popup 99 0 R >>"); + read_everything(&dangling_popup); + assert_eq!(summaries(&dangling_popup).len(), 1); + let mut reply = dangling_popup.clone(); + assert!( + pdfcraft_annot::add_reply(&mut reply, 0, 0, "to you", "Ben", &Meta { date: None, id: "r".into() }).is_ok(), + "a missing pop-up does not block replying" + ); +} + +// ── wrong types where the reader wants a number or a name ───────────────────────────────────── + +/// Values of the wrong type in flag, opacity and colour entries: the reader falls back to its +/// defaults instead of panicking. Opacity is clamped on read to the supported 0-1 range; +/// the original malformed dictionary remains unchanged. +#[test] +fn wrong_typed_flag_opacity_and_colour_entries_fall_back_without_panicking() { + let doc = one_annot(b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /CA 7.5 /F /NotAnInt /C [1 2] >>"); + read_everything(&doc); + let p = props(&doc, 0, 0).expect("still a comment"); + assert_eq!(p.opacity, 1.0, "the out-of-range opacity is clamped on read"); + // Exercise both clamp edges and an unchanged interior value, not just a constant fallback. + for (raw, expected) in [("-0.5", 0.0), ("0.4", 0.4), ("1.5", 1.0)] { + let bytes = format!("<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /CA {raw} >>"); + let probe = one_annot(bytes.as_bytes()); + assert_eq!(props(&probe, 0, 0).expect("still a comment").opacity, expected, "opacity {raw}"); + } + assert!(!p.locked, "a name where /F belongs reads as no flags at all"); + assert_eq!(p.color, None, "/C with two components is not a colour"); + let listed = summaries(&doc); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].color, None); + + // And the other direction: /F as a real number is read by its integer value. + let flags_real = one_annot(b"<< /Type /Annot /Subtype /Text /Rect [10 20 30 40] /F 132.0 >>"); + let p = props(&flags_real, 0, 0).expect("still a comment"); + assert!(p.locked, "132 sets ReadOnly (bit 8) whatever the number's form"); + + // An annotation with no /Subtype at all: nothing to name, so it is not a comment and has no + // properties, but the entry survives untouched (preserve unknown data). + let no_subtype = one_annot(b"<< /Type /Annot /Rect [10 20 30 40] >>"); + read_everything(&no_subtype); + assert!(summaries(&no_subtype).is_empty()); + assert!(props(&no_subtype, 0, 0).is_none()); + assert_eq!(annot_entries(&no_subtype, 0).len(), 1); + + // A `/P` pointing at a page that does not exist is cosmetic: the comment still lists. + let bad_parent_page = one_annot(b"<< /Type /Annot /Subtype /Square /Rect [10 20 30 40] /P 77 0 R >>"); + read_everything(&bad_parent_page); + assert_eq!(summaries(&bad_parent_page).len(), 1); +} + +// ── guards that only bite on damaged data, pinned so they stay visible ──────────────────────── + +/// `move_annotation` shifts the numbers of every geometry key, but only the first four of an array +/// it treats as a rectangle (`every || i < 4`, `crates/annot/src/modify.rs:302`). A `/Rect` with +/// more than four numbers is not something PdfCraft writes, so the guard only shows up on damaged +/// data — which is precisely why it is pinned here: without this test the rule can be deleted +/// silently. What must hold is that the four rectangle numbers move together and whatever strays +/// the file carries are left exactly as they were, rather than being dragged along by half a point. +#[test] +fn moving_a_damaged_rectangle_shifts_its_four_numbers_and_leaves_the_strays_alone() { + let mut doc = one_annot(b"<< /Type /Annot /Subtype /Square /Rect [10 20 60 30] >>"); + let r = annot_entries(&doc, 0)[0].as_ref().expect("an indirect entry"); + doc.update_dict(r, |d| { + d.set( + b"Rect".to_vec(), + Object::Array(vec![ + Object::Real(10.0), + Object::Real(20.0), + Object::Real(60.0), + Object::Real(30.0), + Object::Real(9.0), + Object::Real(-9.5), + ]), + ); + }) + .expect("the entry is a dictionary"); + + pdfcraft_annot::move_annotation(&mut doc, 0, 0, 5.0, 7.0, &Meta { date: None, id: "m".into() }).expect("the comment is not locked"); + let d = dict_of(&doc, &annot_entries(&doc, 0)[0]).expect("a dictionary"); + let stored = nums(&doc, &d, b"Rect").expect("six numbers"); + assert_eq!(stored, vec![15.0, 27.0, 65.0, 37.0, 9.0, -9.5], "four shift, two strays do not"); + + // It survives a save that way, and the comment list still cannot use such a rectangle. + let (file, reopened) = save_and_reopen(&doc, Save::FullTable); + let stored_again = bytes::numbers_of(&file, "Rect"); + assert_eq!(stored_again.first(), Some(&vec![15.0, 27.0, 65.0, 37.0, 9.0, -9.5]), "the file keeps all six"); + let listed = summaries(&reopened); + assert_eq!(listed.len(), 1, "still a comment"); + assert_eq!(listed[0].rect, [0.0, 0.0, 0.0, 0.0], "and still not a usable position"); +} + +/// A link area is checked before anything is written: it needs finite corners whose span is at +/// least two points in both directions. Two different reasons, two different messages — and in +/// every case the page gains no entry. +#[test] +fn a_link_area_that_is_too_small_or_broken_is_refused_before_it_is_written() { + let cases = [ + ("one point wide", [10.0, 20.0, 11.0, 40.0], "too small"), + ("zero height", [10.0, 20.0, 50.0, 20.0], "too small"), + ("a NaN corner", [f64::NAN, 1.0, 50.0, 60.0], "finite coordinates"), + ("corners whose span overflows", [-1e308, 1.0, 1e308, 60.0], "too small or too large"), + ]; + for (label, rect, want_in_message) in cases { + let mut doc = blank(2); + let e = + links::add(&mut doc, 0, rect, &links::LinkAction::Page { page: 1, zoom: links::LinkZoom::FitPage }, &links::LinkStyle::default()).err(); + let msg = match &e { + Some(AnnotError::Invalid(m)) => m.clone(), + other => panic!("{label}: expected Invalid, got {other:?}"), + }; + assert!(msg.contains(want_in_message), "{label}: message {msg:?} should mention {want_in_message:?}"); + assert!(annot_entries(&doc, 0).is_empty(), "{label}: nothing was written"); + assert!(links::list(&doc).is_empty(), "{label}: no link to list"); + } +} + +// ── hostile numbers handed to the builder ───────────────────────────────────────────────────── + +/// Non-finite style and geometry must be refused before anything is written: no partial object, no +/// `NaN` or exponent token in the file. (`AGENTS.md` §4: guard NaN/inf casts.) +#[test] +fn non_finite_builder_input_is_refused_and_leaves_the_document_untouched() { + let shapes = [ + ("nan rect", Shape::Rectangle { rect: [f64::NAN, 10.0, 40.0, 30.0] }), + ("infinite rect", Shape::Rectangle { rect: [f64::NEG_INFINITY, 10.0, 40.0, 30.0] }), + ("nan quad", Shape::TextMarkup { kind: pdfcraft_annot::Markup::Highlight, quads: vec![[10.0, 10.0, f64::NAN, 20.0, 10.0, 5.0, 30.0, 5.0]] }), + ("far away note", Shape::Note { at: [1e308, 2.0], icon: NoteIcon::Note }), + ("nan ink", Shape::Ink { strokes: vec![vec![[f64::NAN, 1.0], [2.0, 3.0]]] }), + ("empty ink", Shape::Ink { strokes: vec![vec![]] }), + ( + "degenerate line", + Shape::Line { from: [10.0, 10.0], to: [10.0, 10.5], start: pdfcraft_annot::LineEnding::None, end: pdfcraft_annot::LineEnding::None }, + ), + ("nan polygon", Shape::Polygon { vertices: vec![[0.0, 0.0], [f64::INFINITY, 10.0], [5.0, 5.0]], cloud: false }), + ("two point polygon", Shape::Polygon { vertices: vec![[0.0, 0.0], [10.0, 10.0]], cloud: false }), + ( + "nan callout", + Shape::Callout { + rect: [10.0, 10.0, 40.0, 30.0], + knee: [f64::NAN, 0.0], + point: [0.0, 0.0], + font_size: 12.0, + ending: pdfcraft_annot::LineEnding::None, + }, + ), + ("no quads", Shape::TextMarkup { kind: pdfcraft_annot::Markup::StrikeOut, quads: vec![] }), + ]; + for (label, shape) in shapes { + let mut doc = blank(1); + let e = add_annotation( + &mut doc, + &NewAnnotation { page: 0, shape, style: Style::default(), contents: "x".into(), author: "A".into() }, + &Meta { date: None, id: "i".into() }, + ) + .err(); + assert!(matches!(e, Some(AnnotError::Invalid(_))), "{label}: refused with Invalid, got {e:?}"); + assert!(annot_entries(&doc, 0).is_empty(), "{label}: nothing was written"); + // The refusal must not corrupt the document either: it still saves and reloads empty. + let (_, reopened) = save_and_reopen(&doc, Save::Incremental); + assert!(summaries(&reopened).is_empty(), "{label}: the file is still clean"); + } + + for (label, style) in [ + ("nan opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::NAN, width: 1.0, fill: None, ..Style::default() }), + ("infinite opacity", Style { color: [0.0, 0.0, 0.0], opacity: f64::INFINITY, width: 1.0, fill: None, ..Style::default() }), + ("nan colour", Style { color: [f64::NAN, 0.0, 0.0], opacity: 1.0, width: 1.0, fill: None, ..Style::default() }), + ("nan width", Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width: f64::NAN, fill: None, ..Style::default() }), + ] { + let mut doc = blank(1); + let e = add_annotation( + &mut doc, + &NewAnnotation { page: 0, shape: Shape::Rectangle { rect: [10.0, 10.0, 40.0, 30.0] }, style, contents: "x".into(), author: "A".into() }, + &Meta { date: None, id: "i".into() }, + ) + .err(); + assert_eq!(e.as_ref(), Some(&AnnotError::Invalid("invalid style".into())), "{label}"); + assert!(annot_entries(&doc, 0).is_empty(), "{label}: nothing was written"); + } + + // Edits take the same care: moving by a non-finite offset is refused and leaves geometry alone. + let mut doc = blank(1); + add_annotation( + &mut doc, + &NewAnnotation { + page: 0, + shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 45.0] }, + style: Style::default(), + contents: "keep".into(), + author: "A".into(), + }, + &Meta { date: None, id: "ok".into() }, + ) + .expect("adds"); + let before = summaries(&doc)[0].rect; + assert_eq!( + pdfcraft_annot::move_annotation(&mut doc, 0, 0, f64::NAN, 1.0, &Meta { date: None, id: "m".into() }).err(), + Some(AnnotError::Invalid("invalid offset".into())) + ); + assert_eq!(summaries(&doc)[0].rect, before, "a refused move does not shift the comment"); + assert_eq!( + pdfcraft_annot::set_rect(&mut doc, 0, 0, [f64::NAN, 0.0, 1e308, 1e308], &Meta { date: None, id: "m".into() }).err().as_ref(), + Some(&AnnotError::Invalid("invalid rectangle (too small)".into())) + ); + assert_eq!(summaries(&doc)[0].rect, before, "a refused resize does not shift the comment"); +} + +/// Attachment input is checked too: a blank file name is refused with an actionable message and +/// writes nothing, so no half-built file specification is left in the document. +#[test] +fn an_attachment_needs_a_name_and_leaves_nothing_behind_when_it_has_none() { + for name in ["", " ", "\t"] { + let mut doc = blank(1); + let e = add_annotation( + &mut doc, + &NewAnnotation { + page: 0, + shape: Shape::Attachment { at: [10.0, 700.0], icon: AttachIcon::Paperclip, file: name.into(), data: b"x".to_vec() }, + style: Style::default(), + contents: String::new(), + author: "A".into(), + }, + &Meta { date: None, id: "a".into() }, + ) + .err(); + assert_eq!(e.as_ref(), Some(&AnnotError::Invalid("the attached file needs a name".into())), "{name:?}"); + assert!(annot_entries(&doc, 0).is_empty(), "no orphan file specification for {name:?}"); + } +} + +// ── fields that carry no magnitude screen (recorded, not endorsed) ──────────────────────────── + +/// This is a MAGNITUDE story, not a one-field story. `rect_for` checks the rectangle of every +/// variant but passes `Shape::CustomStamp { size }` and `Shape::TypedSignature { contours }` +/// straight into `/PCPictureSize` and `/PCOutline` (`create.rs:62-82`, `create.rs:278`, +/// `create.rs:285`) with no +/// magnitude screen whatsoever — and width gets none either. The only style guard on the create +/// path (`create.rs:416`) reads `!finite(&style.color) || !style.opacity.is_finite() +/// || !style.width.is_finite()`: `finite()` (`create.rs:17-19`, i.e. `is_finite() && abs() < 1e7`) +/// magnitude-screens `color` ALONE, while `opacity` and `width` are checked for non-finiteness +/// only. So even the one cap that exists sits at ten million, far below what a reader can turn +/// into an integer. Measured with qpdf's check (`pikepdf.Job(["--check", , "/dev/null"])`, +/// warnings on process stderr, exit code 3). Read that exit code carefully: **3 is qpdf's warning +/// status**, printed together with "operation succeeded with warnings", so the check still completes +/// and writes its output — it is not a hard failure (libqpdf: 0 = no errors or warnings, +/// 2 = errors detected, 3 = warnings detected; a clean file returns 0 with no stderr at all). +/// Every warning probe in this file returns 3 — the three overflow cases below and the +/// unknown-token probe documented at `:155-157` above — so the exit code alone does not separate +/// them from each other; the warning text does. The 9,999,999 control below is the only probe +/// that returns 0. Re-measurement trap: `Job.stderr` stays `None` even when qpdf warns, because +/// the warnings go to the inherited process stderr — redirect at shell level or you will report +/// "no warnings" from a file that produced them. Measured per field, each file written by THIS +/// crate's own writer path: +/// +/// `/BS /W` = 1e299 on an Oval: qpdf reports `object 4/0: error reading object: +/// overflow/underflow converting <1 followed by 299 digits> to 64-bit integer`. +/// +/// `/PCPictureSize` = 1e300 plus NaN: same message. File 1198 B, sha256 +/// 357946a6580e7c13fbcf3c80c396cd80f961a53e55aea9e11dff5e284f759e07, 301 digits confirmed. +/// +/// `/PCOutline` = 1e300: same message. +/// +/// `/BS /W` = 9999999: clean, exit code 0 — because that value has few enough digits for a +/// reader's 64-bit integer conversion (which tops out near 9.2e18), NOT because this crate caps +/// it: width never passes through `finite()`, so the ten-million figure plays no part here. The +/// boundary that matters is the reader's integer range, and it sits far above anything a real +/// border width could be. Measured here too: `/BS /W` = 1e9 is accepted and written verbatim as +/// `1000000000`, which is well past any sane border yet still parseable — the failure only +/// begins when the digits outrun a 64-bit conversion. +/// +/// So ANY annotation number of sufficient magnitude can make the whole object unreadable to a +/// third-party reader — width included, not just stamp payload. Two measurement notes that +/// matter when re-running: `pikepdf.open()` alone prints nothing for these files and still +/// reports `/Annots` length 1, while resolving `/Annots[0]` yields **null** and stays null after +/// qpdf re-saves — a listed-but-null entry is exactly what silent corruption looks like. qpdf +/// classifies it as a WARNING and its open still reports success; pikepdf puts `WARNING:` on a +/// line of its own, so grepping raw stderr for one combined line misses it. What we did NOT +/// measure is what any particular viewer then shows a user — the null entry was observed, not a +/// renderer's behaviour. +/// Meanwhile our own `repair_log()` stays empty, which contradicts this project's rule that +/// dropped or repaired data must be recorded (`CLAUDE.md`, Fidelity). None of these probe files +/// is committed: any sample PDF would need its own `ATTRIBUTION.toml` entry (`AGENTS.md` §1), +/// which this change avoids — regenerate them from the `file` bytes bound below and re-measure. +/// +/// How far a caller can actually get there, checked in this workspace rather than assumed: only +/// `/BS /W` is reachable through a shipped path. The automation tools read `width` as any finite +/// number (`crates/automation/src/comments.rs:427-429` assigns `style.width = w.max(0.0)` after +/// `opt_num`, whose only filter is `is_finite()` at `crates/automation/src/json.rs:112-113`), +/// and the published schema sets `"minimum": 0` with no upper bound +/// (`crates/automation/src/tools.rs:774`) — so a script or MCP agent can write a file whose +/// annotation object qpdf refuses to parse. The desktop UI caps the same value at 0.5..=12.0 +/// (`crates/ui-egui/src/dialogs/comment_props.rs:240-243`, `0.5..=12.0`, or a 0.0 floor when a +/// font size is set), and `/PCPictureSize` / `/PCOutline` are computed +/// inside the engine — `crates/engine/src/edit/run.rs:331` takes the source image's pixel size, +/// scales it to a 200 pt natural box at `:335-339` and hands that size to the stamp shape at +/// `:343`, while contour sizing lives at `crates/engine/src/fill_sign.rs:14-20` (which does reject a +/// non-positive width) — so reaching those two needs a caller using the crates directly +/// rather than an app or tool action. The defect is therefore automation-reachable today, and not +/// reachable by clicking in the desktop app. +/// +/// Non-finite values are rewritten on the way out: `write_real` (`crates/cos/src/writer.rs:48`) +/// emits a literal `0` for any non-finite REAL that reaches it, which is the mechanism for +/// `size` and contour points. Colours are a different route — `rgb()` (`create.rs:13-15`) clamps +/// each component to `0.0..=1.0` BEFORE the writer sees it, so infinity becomes `1.0`, not `0` +/// (see the create-vs-edit case below). The assertions that follow pin only what we write. +/// +/// NOTE: these tests record current behaviour so a fix becomes visible. They assert nothing about +/// the result being correct — the values are wrong on the way in and quietly rewritten on the +/// way out. +#[test] +fn unvalidated_stamp_size_and_signature_contours_are_rewritten_silently_in_the_file() { + let mut doc = blank(1); + let picture = doc.add(Object::Dict(Dict::new())); + add_annotation( + &mut doc, + &NewAnnotation { + page: 0, + shape: Shape::CustomStamp { + rect: [10.0, 20.0, 60.0, 50.0], + name: "Sig".into(), + picture, + image: true, + size: (1e300, f64::NAN), + line: None, + }, + style: Style::default(), + contents: String::new(), + author: "A".into(), + }, + &Meta { date: None, id: "cs".into() }, + ) + .expect("the size is not validated, so the comment is accepted"); + + let (file, reopened) = save_and_reopen(&doc, Save::FullTable); + let stored = bytes::numbers_of(&file, "PCPictureSize"); + assert_eq!(stored.len(), 1, "one /PCPictureSize array in the file"); + assert_eq!(stored[0][1], 0.0, "NaN was written as 0 ({:?})", stored[0]); + assert!(stored[0][0] > 1e299, "the huge size was written rather than refused ({:?})", stored[0][0]); + assert!(doc.repair_log().is_empty() && reopened.repair_log().is_empty(), "nothing recorded about either substitution"); + // The digits are really there: 301 of them, no exponent form. + let pos = file.windows(b"/PCPictureSize".len()).position(|w| w == b"/PCPictureSize").expect("the key is in the file"); + let run = &file[pos..]; + let digits = run.iter().take(400).filter(|b| b.is_ascii_digit()).count(); + assert!(digits > 300, "a 301-digit decimal was written to the file ({digits} digits seen)"); + + let mut doc2 = blank(1); + add_annotation( + &mut doc2, + &NewAnnotation { + page: 0, + shape: Shape::TypedSignature { rect: [10.0, 20.0, 60.0, 50.0], contours: vec![vec![[f64::NAN, 1.0], [2.0, 3.0], [4.0, f64::INFINITY]]] }, + style: Style::default(), + contents: String::new(), + author: "A".into(), + }, + &Meta { date: None, id: "ts".into() }, + ) + .expect("the contours are not validated either"); + let (file2, _) = save_and_reopen(&doc2, Save::FullTable); + let outline = bytes::numbers_of(&file2, "PCOutline"); + assert_eq!(outline.as_slice(), &[vec![0.0, 1.0, 2.0, 3.0, 4.0, 0.0]], "both non-finite contour points became 0"); +} + +/// The fill colour is screened differently on the two paths. `add_annotation` magnitude-screens +/// only `style.color`, through `finite()` (`create.rs:416`; the helper at `create.rs:17-19` is +/// `is_finite() && abs() < 1e7`), gives `opacity` and `width` plain finiteness checks, and never +/// screens `fill` at all; `set_fill` refuses the same value (`modify.rs:626-627`, "invalid fill +/// colour") — and that refusal calls `finite()` too, which is why it trips on anything from ten +/// million up as well as on NaN or infinity. The accepted value is then clamped component-wise by +/// `rgb()` (`create.rs:13-15`, `clamp(0.0, 1.0)`), so what lands in the file is the top of the +/// range: NaN survives clamping as NaN and `write_real` (`crates/cos/src/writer.rs:48`) then +/// emits a literal `0`, while infinity becomes `1.0`. Measured across magnitudes +/// in `the_same_huge_fill_is_accepted_on_create_and_refused_on_edit`, which pins +/// 1e7 / 1e8 / infinity as well. Border width and stroke colour behave symmetrically on both +/// paths — width is finiteness-checked only, so a huge width gets written rather than refused (see +/// the magnitude note above), and both paths clamp it at zero — while stamp `size` and signature +/// `contours` have no edit path at all, so only create can diverge for them. Pinned as current +/// behaviour, not as desired behaviour. +#[test] +fn a_nan_fill_is_accepted_on_create_and_refused_on_edit() { + let nan = [f64::NAN, 0.0, 0.0]; + let mut doc = blank(1); + add_annotation( + &mut doc, + &NewAnnotation { + page: 0, + shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 40.0] }, + style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(nan), ..Style::default() }, + contents: String::new(), + author: "A".into(), + }, + &Meta { date: None, id: "nf".into() }, + ) + .expect("create does not screen the fill colour"); + let (file, reopened) = save_and_reopen(&doc, Save::FullTable); + let ic = bytes::numbers_of(&file, "IC"); + assert_eq!(ic.first(), Some(&vec![0.0, 0.0, 0.0]), "the NaN components were written as zeros"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.fill), Some([0.0, 0.0, 0.0])); + + let e = pdfcraft_annot::set_fill(&mut reopened.clone(), 0, 0, Some(nan), &Meta { date: None, id: "nf2".into() }).err(); + assert_eq!(e.as_ref(), Some(&AnnotError::Invalid("invalid fill colour".into())), "the same value is refused two lines later in the same crate"); +} + +// ── generated damage, same rule: refuse or degrade, never panic ─────────────────────────────── + +/// proptest's assert macros cannot capture local names in their format strings, so the writer path +/// is formatted here and passed in explicitly. +fn tag(path: Save) -> String { + format!("{path:?}") +} + +/// The kinds of damage a stranger's annotation data can carry. Twelve wreck the dictionary of a +/// comment PdfCraft wrote, so the file stays structurally valid around one wrong entry; the +/// duplicate-entry kind instead repeats that comment's entry in the page's `/Annots`. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +enum Damage { + RectTooShort, + RectWrongType, + QuadPointsOddLength, + QuadPointsWrongType, + FlagsAsName, + OpacityOutOfRange, + SubtypeMissing, + PopupDangling, + ParentToMissingObject, + IrtSelfReference, + DuplicateEntryInAnnots, + InkListWrongShape, + VerticesOddLength, +} + +const DAMAGES: [Damage; 13] = [ + Damage::RectTooShort, + Damage::RectWrongType, + Damage::QuadPointsOddLength, + Damage::QuadPointsWrongType, + Damage::FlagsAsName, + Damage::OpacityOutOfRange, + Damage::SubtypeMissing, + Damage::PopupDangling, + Damage::ParentToMissingObject, + Damage::IrtSelfReference, + Damage::DuplicateEntryInAnnots, + Damage::InkListWrongShape, + Damage::VerticesOddLength, +]; + +/// Apply one damage. Twelve of the kinds wreck the annotation dictionary at `/Annots[index]` +/// itself; the duplicate-entry kind instead lists that entry twice in the page's `/Annots`. +fn damage(doc: &mut Document, index: usize, kind: Damage) { + let entries = annot_entries(doc, 0); + let Some(r) = entries.get(index).and_then(|e| e.as_ref()) else { return }; + if kind == Damage::DuplicateEntryInAnnots { + // The damage lives on the page, not the dictionary: the same entry object appears twice in + // `/Annots`, so the page lists one annotation dictionary through two slots. Routed through + // `set_annots`, the same writer the crate itself uses, so both storage shapes (array + // inline in the page, array behind a reference) are rewritten the way mainline rewrites. + let mut list = annot_entries(doc, 0); + if let Some(slot) = list.get(index).cloned() { + list.insert(index, slot); + let page = ObjRef::new(*page_object_numbers(doc).first().expect("the page exists"), 0); + set_annots(doc, page, list).expect("the page dictionary is editable"); + } + return; + } + doc.update_dict(r, |d| match kind { + Damage::RectTooShort => d.set(b"Rect".to_vec(), Object::Array(vec![Object::Real(10.0), Object::Real(20.0)])), + Damage::RectWrongType => { + d.set(b"Rect".to_vec(), Object::Array(vec![Object::name("a"), Object::name("b"), Object::name("c"), Object::name("d")])) + } + Damage::QuadPointsOddLength => d.set(b"QuadPoints".to_vec(), Object::Array((0..7).map(|i| Object::Real(i as f64)).collect())), + Damage::QuadPointsWrongType => d.set(b"QuadPoints".to_vec(), Object::Array(vec![Object::Real(1.0), Object::name("notanumber")])), + Damage::FlagsAsName => d.set(b"F".to_vec(), Object::name("Locked")), + Damage::OpacityOutOfRange => d.set(b"CA".to_vec(), Object::Real(4.25)), + Damage::SubtypeMissing => { + d.remove(b"Subtype"); + } + Damage::PopupDangling => d.set(b"Popup".to_vec(), Object::Ref(ObjRef::new(9997, 0))), + Damage::ParentToMissingObject => d.set(b"Parent".to_vec(), Object::Ref(ObjRef::new(9998, 0))), + Damage::IrtSelfReference => d.set(b"IRT".to_vec(), Object::Ref(r)), + Damage::DuplicateEntryInAnnots => unreachable!("this damage edits the page, not the dictionary"), + Damage::InkListWrongShape => d.set(b"InkList".to_vec(), Object::Array(vec![Object::Real(5.0), Object::name("notastroke")])), + Damage::VerticesOddLength => d.set(b"Vertices".to_vec(), Object::Array(vec![Object::Real(1.0), Object::Real(2.0), Object::Real(3.0)])), + }) + .expect("the entry is an annotation dictionary"); +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(64))] + + /// For any single damage, and any of the three writer paths: opening, reading every annotation + /// entry point, re-saving and reopening must all complete without a panic. Alongside that the + /// test insists on something positive, so it cannot pass by reading nothing at all — an undamaged + /// second comment on the same page keeps its exact rectangle, and the damaged comment is either + /// listed or skipped but never half-parsed into a crash. + #[test] + fn damaged_annotations_never_panic(kind in prop::sample::select(DAMAGES.to_vec()), extra_annot in 0usize..=2) { + // Build a page holding one comment that will be damaged plus `extra_annot` clean ones. + let mut doc = blank(1); + for i in 0..=extra_annot { + add_annotation(&mut doc, &NewAnnotation { page: 0, shape: Shape::TextMarkup { kind: pdfcraft_annot::Markup::Highlight, quads: vec![[20.0 + 10.0 * i as f64, 500.0, 80.0 + 10.0 * i as f64, 500.0, 20.0 + 10.0 * i as f64, 486.0, 80.0 + 10.0 * i as f64, 486.0]] }, style: Style::default(), contents: format!("ok{i}"), author: "Ada".into() }, &Meta { date: None, id: format!("ok{i}") }).expect("adds"); + } + let clean_rect = summaries(&doc).iter().map(|s| s.rect).collect::>(); + damage(&mut doc, 0, kind); + + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let (_, reopened) = save_and_reopen(&doc, path); + read_everything(&reopened); + // The undamaged comments are untouched by the damage and its save. + let after = summaries(&reopened); + // A comment stops being listed only when the damage removes what makes it a comment — + // its `/Subtype` (measured, not assumed). The other damages all leave it listed; the + // duplicate-entry damage lists one comment through two slots, so it adds a listing. + let want_listed = clean_rect.len() - usize::from(kind == Damage::SubtypeMissing) + usize::from(kind == Damage::DuplicateEntryInAnnots); + prop_assert_eq!(after.len(), want_listed, "damage {k} on {p}: listed comment count", k = format!("{kind:?}"), p = tag(path)); + // Every undamaged comment keeps its exact rectangle through the damage and the rewrite. + for r in &clean_rect[1..] { + prop_assert!(after.iter().any(|s| s.rect == *r), "damage {k} on {p}: the clean comment at {r:?} is gone", k = format!("{kind:?}"), p = tag(path)); + } + // And deleting by index never panics, whatever the damage was. + let mut cut = reopened.clone(); + for i in 0..clean_rect.len() + 1 { + let _ = delete_annotation(&mut cut, 0, i); + } + } + } +} + +/// Read side, measured: a negative border width arriving from another tool is reported unchanged +/// by the public API. Our own writer clamps negatives to 0 (`create.rs:47`, `create.rs:216`), and +/// the helper `border_width_of` (`modify.rs:498-500`) does apply `.max(0.0)` — but the property +/// read path used here (`props()` -> `info.rs:227-231`) takes `/BS /W` raw for non-FreeText +/// subtypes, so a foreign file +/// declaring -3 comes back as -3.0. Pinned because the asymmetry is visible in the public API: +/// what we refuse to keep, we are willing to report. (Compare `/CA`, likewise unclamped on read.) +#[test] +fn foreign_negative_border_width_is_reported_as_written() { + let doc = open(&[ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [4 0 R] >>", + b"<< /Type /Annot /Subtype /Square /Rect [10 20 60 30] /BS << /S /S /W -3 >> >>", + ]); + let p = props(&doc, 0, 0).expect("the annotation is readable"); + assert_eq!( + p.width, + Some(-3.0), + "props() reports /BS /W as written; note border_width_of (modify.rs:498-500) does clamp at zero, so this is path-specific" + ); + assert!(doc.repair_log().is_empty(), "and that neither is recorded as a repair"); +} + +/// The create-vs-edit divergence is wider than the NaN case: the edit-side refusal begins at `1e7` +/// **in magnitude**, because `finite()` (`create.rs:17-19`, called by the edit path at `modify.rs:626`) +/// tests `abs() < 1e7`, so a component of magnitude exactly ten million is refused (positive or +/// negative) while 9,999,999 passes — what THIS test pins is the positive side (1e7, 1e8, infinity); +/// a negative-magnitude row is not pinned here, only reasoned from `abs()` (independently measured +/// in review round 5, which found `-1e7` refused on the edit path) +/// while create never screens `fill` at all, so any value whatsoever is accepted +/// there. Measured, not assumed — the +/// expected `/IC` components below are what this crate's writer actually produced on re-run +/// (clamping happens in `rgb()`, `create.rs:13-15`, so an accepted huge component lands at 1.0). +#[test] +fn the_same_huge_fill_is_accepted_on_create_and_refused_on_edit() { + let cases: [([f64; 3], &[f64; 3]); 3] = + [([1e7, 0.0, 0.0], &[1.0, 0.0, 0.0]), ([1e8, 0.0, 0.0], &[1.0, 0.0, 0.0]), ([f64::INFINITY; 3], &[1.0, 1.0, 1.0])]; + for (probe, want) in cases { + let mut doc = blank(1); + add_annotation( + &mut doc, + &NewAnnotation { + page: 0, + shape: Shape::Rectangle { rect: [10.0, 20.0, 60.0, 40.0] }, + style: Style { color: [0.9, 0.1, 0.1], opacity: 1.0, width: 2.0, fill: Some(probe), ..Style::default() }, + contents: String::new(), + author: "A".into(), + }, + &Meta { date: None, id: "huge-fill".into() }, + ) + .expect("create does not screen the fill colour"); + let (file, reopened) = save_and_reopen(&doc, Save::FullTable); + let ic = bytes::numbers_of(&file, "IC"); + assert_eq!(ic.first().map(Vec::as_slice), Some(want.as_slice()), "{probe:?}: create stores the clamped colour"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.fill), Some([want[0], want[1], want[2]]), "{probe:?}: read-back matches what was written",); + let e = pdfcraft_annot::set_fill(&mut reopened.clone(), 0, 0, Some(probe), &Meta { date: None, id: "huge-fill-2".into() }).err(); + assert_eq!(e.as_ref(), Some(&AnnotError::Invalid("invalid fill colour".into())), "{probe:?}: the same value is refused on the edit path",); + } +} diff --git a/crates/annot/tests/properties.proptest-regressions b/crates/annot/tests/properties.proptest-regressions new file mode 100644 index 0000000000..341ba3ff3d --- /dev/null +++ b/crates/annot/tests/properties.proptest-regressions @@ -0,0 +1,6 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. diff --git a/crates/annot/tests/properties.rs b/crates/annot/tests/properties.rs new file mode 100644 index 0000000000..53a37cf067 --- /dev/null +++ b/crates/annot/tests/properties.rs @@ -0,0 +1,417 @@ +//! Property tests for annotation structural fidelity, stated over the **saved bytes**. +//! +//! What is proven here is a rule about files, not about structs: for every generated comment the +//! writer produces bytes that (a) hold the geometry as fixed-decimal number arrays, and (b) parse +//! back — with our reader *and* with vendored hayro-syntax — into dictionaries whose numbers, +//! names, flags and opacity equal values computed in this file from the input plus ISO 32000-2. +//! Two things make that more than "compare an object to itself": the expectations never read the +//! pre-save document, and the number tokens are re-parsed from raw `Vec` by `support::bytes`, +//! a strict scanner that is not `pdfcraft-cos`. +//! +//! Geometry comes from a half-point grid (`x = k / 2`, |k| ≤ 450). Every such value has an exact +//! decimal form and survives Flate, `f64` arithmetic and the writer's shortest-round-trip +//! formatting, so a mismatch means data changed shape on disk rather than drifting by an ulp. +//! Text is generated from an alphabet spanning ASCII punctuation, Latin-1 letters and CJK — the +//! cases where PDF's two string encodings disagree. +//! +//! Not covered, deliberately: appearance-stream content (the five open appearance PRs named in +//! `tests/round_trip.rs`), and the comment-list ordering rule of `summaries`, which is +//! deterministic and checked there instead. + +mod support { + pub mod bytes; + pub mod fixture; +} + +use pdfcraft_annot::{LineEnding, Markup, Meta, NewAnnotation, NoteIcon, Shape, Style, add_annotation, props, summaries}; +use pdfcraft_cos::Document; +use proptest::prelude::*; + +use support::bytes; +use support::fixture::{ + FLAG_NO_ROTATE, FLAG_NO_ZOOM, FLAG_PRINT, Save, annot_entries, blank, dict_of, name_of, num, nums, page_object_numbers, save_and_reopen, str_of, +}; + +/// proptest's assert macros cannot capture local names inside their format strings (the message is +/// built in proptest's own expansion scope), so every message here passes what it names explicitly. +fn tag(path: Save) -> String { + format!("{path:?}") +} + +/// A coordinate on the half-point grid. +fn coord() -> impl Strategy { + any::().prop_map(|k| f64::from(k.rem_euclid(901) - 450) / 2.0) +} + +/// A rectangle as four grid coordinates, corners in any order (the writer normalises). +fn rect4() -> impl Strategy { + (coord(), coord(), coord(), coord()).prop_map(|(x0, y0, x1, y1)| [x0, y0, x1.max(x0 + 1.0), y1.max(y0 + 1.0)]) +} + +/// A quadrilateral: eight grid numbers, in Acrobat's corner order (§12.5.6.10). +fn quad8() -> impl Strategy { + prop::array::uniform8(coord()) +} + +/// Text from an alphabet where PDF's two string encodings disagree: printable ASCII (including the +/// punctuation a literal string has to escape around), Latin-1 letters beyond 0x7F, and CJK. +const TEXT_CHARS: &str = "abZq07 .,:;!?-_%$#*+=~()[]<>&@^|\n\t\u{e9}\u{fc}\u{f1}\u{df}\u{a7}\u{c0}\u{f6}\u{301}\u{65e5}\u{672c}"; + +fn text_of(max_len: usize) -> impl Strategy { + prop::collection::vec(prop::sample::select(TEXT_CHARS.chars().collect::>()), 0..=max_len).prop_map(|c| c.into_iter().collect()) +} + +/// ASCII-only identity strings: `/NM` is a thread key other readers match literally, so keep it +/// free of characters that would make a byte-level comparison ambiguous. +fn id_of(max_len: usize) -> impl Strategy { + prop::collection::vec( + prop::sample::select("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._".chars().collect::>()), + 0..=max_len, + ) + .prop_map(|c| c.into_iter().collect()) +} + +/// A grid coordinate's PDF decimal form, computed here rather than by the writer: integers without +/// a fraction, half-points with one digit (§7.3.3). +fn token(v: f64) -> String { + if v.fract() == 0.0 { format!("{}", v as i64) } else { format!("{v:.1}") } +} + +fn rect_token(r: &[f64]) -> String { + format!("/Rect [{}]", r.iter().map(|v| token(*v)).collect::>().join(" ")) +} + +/// Normalise as §12.5.4.2 requires a rectangle to be stored: lower-left then upper-right. +fn norm(r: [f64; 4]) -> Vec { + vec![r[0].min(r[2]), r[1].min(r[3]), r[0].max(r[2]), r[1].max(r[3])] +} + +/// Bounds of a point list, computed here rather than by the crate. +fn bounds_of(points: &[[f64; 2]]) -> Vec { + let mut out = [f64::MAX, f64::MAX, f64::MIN, f64::MIN]; + for p in points { + out[0] = out[0].min(p[0]); + out[1] = out[1].min(p[1]); + out[2] = out[2].max(p[0]); + out[3] = out[3].max(p[1]); + } + out.to_vec() +} + +fn meta(id: String) -> Meta { + Meta { date: Some("D:20261001120000Z".into()), id } +} + +fn comment(page: usize, shape: Shape, style: Style, contents: String, author: String) -> NewAnnotation { + NewAnnotation { page, shape, style, contents, author } +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(128))] + + /// A rectangle comment's `/Rect` reaches the file as four fixed-decimal numbers equal to the + /// normalised input, and parses back to exactly those numbers on all three writer paths. The + /// raw-token claim runs where the bytes are uncompressed, so it is a claim about the file. + #[test] + fn a_rectangles_stored_rectangle_is_the_input_normalised(rect in rect4(), id in id_of(12)) { + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let mut doc = blank(1); + add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect }, Style::default(), String::new(), "Ada".into()), &meta(id.clone())) + .expect("a normalised box is at least a point wide"); + let (file, reopened) = save_and_reopen(&doc, path); + let want = norm(rect); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), 1, "{}: exactly the one comment", tag(path)); + let d = dict_of(&reopened, &entries[0]).expect("one annotation"); + let stored = nums(&reopened, &d, b"Rect"); + prop_assert_eq!(stored.as_deref(), Some(want.as_slice()), "{}: /Rect must be the normalised input {:?}", tag(path), rect); + if path != Save::FullStreams { + let t = rect_token(&want); + prop_assert!(file.windows(t.len()).any(|w| w == t.as_bytes()), "{}: {:?} must be written exactly as {}", tag(path), rect, t); + prop_assert!(bytes::numbers_of(&file, "Rect").contains(&want), "{}: the file itself must hold {:?}", tag(path), want); + } + } + } + + /// Markup keeps every quadrilateral, in order: `/QuadPoints` holds all 8·n numbers and `/Rect` + /// is the bounds of all of them, not just the first quad's. + #[test] + fn markup_keeps_every_quadrilateral(quads in prop::collection::vec(quad8(), 1..=3)) { + let flat: Vec = quads.concat(); + let points: Vec<[f64; 2]> = quads.iter().flat_map(|q| q.as_chunks::<2>().0.iter().copied()).collect(); + let want_rect = bounds_of(&points); + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let mut doc = blank(1); + add_annotation(&mut doc, &comment(0, Shape::TextMarkup { kind: Markup::Highlight, quads: quads.clone() }, Style::default(), String::new(), "Ada".into()), &meta("q".into())) + .expect("adds"); + let (file, reopened) = save_and_reopen(&doc, path); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), 1, "{}: exactly the one comment", tag(path)); + let d = dict_of(&reopened, &entries[0]).expect("one annotation"); + let stored = nums(&reopened, &d, b"QuadPoints"); + prop_assert_eq!(stored.as_deref(), Some(flat.as_slice()), "{}: all {} quad numbers, in order", tag(path), flat.len()); + let stored_rect = nums(&reopened, &d, b"Rect"); + prop_assert_eq!(stored_rect.as_deref(), Some(want_rect.as_slice()), "{}: /Rect bounds every quad", tag(path)); + prop_assert_eq!(summaries(&reopened).first().map(|s| s.quads.len()), Some(quads.len()), "{}: the comment list chunks them the same way", tag(path)); + if path != Save::FullStreams { + prop_assert!(bytes::numbers_of(&file, "QuadPoints").contains(&flat), "{}: {} numbers must be in the file", tag(path), flat.len()); + } + } + } + + /// `/CA` obeys its rule whatever the caller asked for: written only when clamped opacity is + /// below 1, and equal to the clamped value. A non-finite opacity is refused before any object + /// is created, so the file never holds a number PDF cannot write. + #[test] + fn opacity_and_flags_follow_their_rules(opacity in any::()) { + let mut doc = blank(1); + let added = add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: [10.0, 10.0, 60.0, 40.0] }, Style { color: [0.5, 0.25, 0.75], opacity, width: 1.0, fill: None, ..Style::default() }, String::new(), "Ada".into()), &meta("c".into())); + if !opacity.is_finite() { + prop_assert!(added.is_err(), "a non-finite opacity ({:?}) must be refused, not written", opacity); + return Ok(()); + } + let _ = added.expect("adds"); + let want = opacity.clamp(0.0, 1.0); + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let (file, reopened) = save_and_reopen(&doc, path); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), 1, "{}: exactly the one comment", tag(path)); + let d = dict_of(&reopened, &entries[0]).expect("one annotation"); + if want < 1.0 { + prop_assert_eq!(num(&reopened, &d, b"CA"), Some(want), "{}: opacity {:?} clamps to {:?}", tag(path), opacity, want); + prop_assert_eq!(props(&reopened, 0, 0).map(|p| p.opacity), Some(want), "{}: reported opacity", tag(path)); + if path != Save::FullStreams { + prop_assert_eq!(bytes::scalars_of(&file, "CA").first().copied(), Some(want), "{}: /CA must be in the file as {}", tag(path), token(want)); + } + } else { + prop_assert!(d.get(b"CA").is_none(), "{}: full opacity writes no /CA (it is the default)", tag(path)); + } + let flags = d.int(b"F").unwrap_or_default(); + prop_assert_ne!(flags & FLAG_PRINT, 0, "{}: a comment keeps its Print bit (§12.5.3)", tag(path)); + let colour = nums(&reopened, &d, b"C"); + prop_assert_eq!(colour.as_deref(), Some(&[0.5, 0.25, 0.75][..]), "{}: colours pass through unchanged", tag(path)); + } + } + + /// Text, author and id survive exactly — including the empty string. `/Contents` is stored even + /// when empty (an empty PDF string), while `/T` and `/NM` follow the crate's emptiness rule: + /// no author or id means the key is not written at all. + #[test] + fn text_author_and_id_survive_exactly(contents in text_of(40), author in text_of(16), id in id_of(12)) { + for path in [Save::Incremental, Save::FullTable] { + let mut doc = blank(1); + add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: [10.0, 20.0, 70.0, 45.0] }, Style::default(), contents.clone(), author.clone()), &meta(id.clone())) + .expect("adds"); + let (file, reopened) = save_and_reopen(&doc, path); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), 1, "{}: exactly the one comment", tag(path)); + let d = dict_of(&reopened, &entries[0]).expect("one annotation"); + let storedcontents = str_of(&reopened, &d, b"Contents"); + prop_assert_eq!(storedcontents.as_deref(), Some(contents.as_str()), "{}: /Contents round trip for {:?}", tag(path), contents); + let storedid = str_of(&reopened, &d, b"NM"); + if id.is_empty() { + prop_assert!(storedid.is_none(), "{}: an empty id writes no /NM, got {storedid:?}", tag(path)); + } else { + prop_assert_eq!(storedid.as_deref(), Some(id.as_str()), "{}: /NM round trip", tag(path)); + } + prop_assert!(bytes::texts_of(&file, "Contents").contains(&contents), "{:?} must appear in the file as a PDF string", contents); + if id.is_empty() { + prop_assert!(bytes::texts_of(&file, "NM").is_empty(), "{}: no /NM in the file for an empty id", tag(path)); + } else { + prop_assert!(bytes::texts_of(&file, "NM").contains(&id), "{:?} must appear in the file as a PDF string", id); + } + if author.is_empty() { + prop_assert!(d.get(b"T").is_none(), "{}: an empty author writes no /T", tag(path)); + } else { + let storedauthor = str_of(&reopened, &d, b"T"); + prop_assert_eq!(storedauthor.as_deref(), Some(author.as_str()), "{}: /T round trip for {:?}", tag(path), author); + } + } + } + + /// Several comments on one page keep their count, their order and all of their rectangles — + /// including the raw-file claim that the list of `/Rect` arrays is exactly what was added. + #[test] + fn several_rectangles_keep_count_order_and_geometry(rects in prop::collection::vec(rect4(), 1..=6), opacity_bits in any::()) { + let mut doc = blank(1); + let mut want_rects = Vec::new(); + for (i, r) in rects.iter().enumerate() { + // Two bits of the byte give opacities 1.0, 0.5, -0.5 and 0.0: written, clamped or absent. + let opacity = match opacity_bits & 3 { + 0 => 1.0, + 1 => 0.5, + 2 => -0.5, + _ => 0.0, + }; + add_annotation(&mut doc, &comment(0, Shape::Rectangle { rect: *r }, Style { color: [0.9, 0.1, 0.1], opacity, width: 2.0, fill: None, ..Style::default() }, format!("c{i}"), "Ada".into()), &meta(format!("k{i}"))) + .expect("adds"); + want_rects.push(norm(*r)); + } + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let (file, reopened) = save_and_reopen(&doc, path); + let want = want_rects.clone(); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), want.len(), "{}: one object per comment", tag(path)); + let got: Vec> = entries.iter().filter_map(|e| dict_of(&reopened, e)).flat_map(|d| nums(&reopened, &d, b"Rect")).collect(); + prop_assert_eq!(got, want.clone(), "{}: the same rectangles in the same order", tag(path)); + prop_assert_eq!(summaries(&reopened).len(), want.len(), "{}: every comment is listed", tag(path)); + if path != Save::FullStreams { + prop_assert_eq!(bytes::numbers_of(&file, "Rect"), want, "{}: every rectangle is in the file", tag(path)); + } + } + } + + /// Sticky notes arrive with a pop-up partner, and the pairing is two references: so that means + /// they still point at each other after a save, that the count is always two per note, and + /// that the icon box stays 20 pt square at the position asked for. + #[test] + fn notes_always_bring_a_popup_that_points_back(at in prop::array::uniform2(coord()), n in 1usize..=3) { + let at = [at[0], at[1].max(25.0)]; + let mut doc = blank(n); + for p in 0..n { + add_annotation(&mut doc, &comment(p, Shape::Note { at, icon: NoteIcon::Comment }, Style::default(), String::new(), "Ada".into()), &meta(format!("n{p}"))) + .expect("adds"); + } + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let (_, reopened) = save_and_reopen(&doc, path); + let pages = page_object_numbers(&reopened); + prop_assert_eq!(pages.len(), n, "{}", tag(path)); + for p in 0..n { + let entries = annot_entries(&reopened, p); + prop_assert_eq!(entries.len(), 2, "{}: note and pop-up on page {}", tag(path), p); + let note = dict_of(&reopened, &entries[0]).expect("note dictionary"); + let pop = dict_of(&reopened, &entries[1]).expect("pop-up dictionary"); + let notekind = name_of(&reopened, ¬e, b"Subtype"); + prop_assert_eq!(notekind.as_deref(), Some("Text"), "{}", tag(path)); + let popupkind = name_of(&reopened, &pop, b"Subtype"); + prop_assert_eq!(popupkind.as_deref(), Some("Popup"), "{}", tag(path)); + prop_assert_eq!(pop.reference(b"Parent"), entries[0].as_ref(), "{}: /Parent back-reference", tag(path)); + prop_assert_eq!(note.reference(b"Popup"), entries[1].as_ref(), "{}: /Popup forward reference", tag(path)); + let iconbox = nums(&reopened, ¬e, b"Rect"); + prop_assert_eq!(iconbox.as_deref(), Some(&[at[0], at[1] - 20.0, at[0] + 20.0, at[1]][..]), "{}: the icon box for {:?}", tag(path), at); + let flags = note.int(b"F").unwrap_or_default(); + prop_assert_ne!(flags & (FLAG_NO_ZOOM | FLAG_NO_ROTATE), 0, "{}: a note icon neither scales nor rotates", tag(path)); + } + } + } + + /// An incremental save and a garbage-collecting full rewrite must describe the same comments. + /// Their bytes differ by design; what may not differ is what a reader learns from them. + #[test] + fn every_writer_path_reports_the_same_comments(rect in rect4(), contents in text_of(20), id in id_of(8)) { + let mut doc = blank(2); + let quad = [rect[0], rect[3], rect[2], rect[3], rect[0], rect[1], rect[2], rect[1]]; + add_annotation(&mut doc, &comment(1, Shape::TextMarkup { kind: Markup::Underline, quads: vec![quad] }, Style::default(), contents, "Ada".into()), &meta(id)) + .expect("adds"); + + let (_, inc) = save_and_reopen(&doc, Save::Incremental); + let (_, full) = save_and_reopen(&doc, Save::FullTable); + let (_, packed) = save_and_reopen(&doc, Save::FullStreams); + let describe = |d: &Document| -> (Vec, Vec>, Option) { + let dicts: Vec<_> = annot_entries(d, 1).iter().filter_map(|x| dict_of(d, x)).collect(); + ( + dicts.iter().map(|x| name_of(d, x, b"Subtype").unwrap_or_default()).collect(), + dicts.iter().flat_map(|x| nums(d, x, b"QuadPoints")).collect(), + dicts.first().and_then(|x| str_of(d, x, b"Contents")), + ) + }; + prop_assert_eq!(describe(&inc), describe(&full)); + prop_assert_eq!(describe(&full), describe(&packed)); + } + + /// Border width and interior colour reach the file as given, and a `None` fill leaves no `/IC` + /// behind — "no fill" is stored as absence, not as black. + /// + /// The range includes NEGATIVE widths on purpose: `add_annotation` screens width for + /// non-finiteness only (`crates/annot/src/create.rs:416` tests `style.width.is_finite()`; the + /// `abs() < 1e7` cap inside `finite()` at `create.rs:17-19` guards `color`, never width), so + /// negatives are accepted and then clamped by `max(0.0)` on the way to `/BS /W` (`create.rs:216`). + /// Sampling `0.0..` alone left both clamps untested while this test's name implied coverage. + /// The upper end stays modest deliberately: magnitude is not screened here either, so a huge + /// width would be written verbatim — that case is pinned in `malformed.rs`, not by this + /// property, whose job is the sign clamp and the fill/absence rules. + #[test] + fn width_and_fill_are_written_as_they_were_given(width in -4.0f64..12.0, fill in prop::option::of(prop::array::uniform3(0.0f64..1.0))) { + let mut doc = blank(1); + add_annotation(&mut doc, &comment(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, Style { color: [1.0, 0.0, 0.0], opacity: 1.0, width, fill, ..Style::default() }, String::new(), "Ada".into()), &meta("f".into())) + .expect("adds"); + for path in [Save::Incremental, Save::FullTable] { + let (file, reopened) = save_and_reopen(&doc, path); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), 1, "{}: exactly the one comment", tag(path)); + let d = dict_of(&reopened, &entries[0]).expect("one annotation"); + let bs = d.get(b"BS").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).expect("/BS"); + prop_assert_eq!(num(&reopened, &bs, b"W"), Some(width.max(0.0)), "{}: /BS /W for width {:?}", tag(path), width); + prop_assert_eq!(d.contains(b"IC"), fill.is_some(), "{}: /IC is present exactly when a fill was given", tag(path)); + prop_assert_eq!(props(&reopened, 0, 0).and_then(|p| p.width), Some(width.max(0.0)), "{}: reported width {:?}", tag(path), width); + prop_assert_eq!(props(&reopened, 0, 0).and_then(|p| p.fill), fill, "{}: /IC for {:?}", tag(path), fill); + let ic = bytes::numbers_of(&file, "IC"); + match fill { + Some(c) => prop_assert_eq!(ic.first(), Some(&vec![c[0], c[1], c[2]]), "{}: /IC must be in the file", tag(path)), + None => prop_assert!(ic.is_empty(), "{}: no /IC when unfilled, got {:?}", tag(path), ic), + } + } + } + + /// A line keeps its endpoints verbatim in `/L`, and the annotation's rectangle always spans + /// both of them — the one geometry rule a line must obey however wide its stroke or arrowheads + /// are drawn (§12.5.4.2). The padding itself lives in `appearance`, so it is not pinned here. + #[test] + fn a_line_keeps_its_endpoints_and_its_rectangle_spans_them(from in prop::array::uniform2(coord()), dx in 1.0f64..300.0, dy in -300.0f64..300.0, width in 0.5f64..8.0) { + let to = [(from[0] + dx).min(450.0), from[1] + dy]; + let mut doc = blank(1); + add_annotation(&mut doc, &comment(0, Shape::Line { from, to, start: LineEnding::None, end: LineEnding::OpenArrow }, Style { color: [0.0, 0.0, 0.0], opacity: 1.0, width, fill: None, ..Style::default() }, String::new(), "Ada".into()), &meta("l".into())) + .expect("adds"); + let span = bounds_of(&[from, to]); + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let (file, reopened) = save_and_reopen(&doc, path); + let entries = annot_entries(&reopened, 0); + prop_assert_eq!(entries.len(), 1, "{}: exactly the one comment", tag(path)); + let d = dict_of(&reopened, &entries[0]).expect("one annotation"); + let r = nums(&reopened, &d, b"Rect").expect("/Rect"); + prop_assert!(r[0] <= span[0] && r[1] <= span[1] && r[2] >= span[2] && r[3] >= span[3], "{}: line {:?} to {:?} must lie inside {:?}", tag(path), from, to, r); + let storedline = nums(&reopened, &d, b"L"); + prop_assert_eq!(storedline.as_deref(), Some(&[from[0], from[1], to[0], to[1]][..]), "{}: /L verbatim", tag(path)); + if path != Save::FullStreams { + prop_assert!(bytes::numbers_of(&file, "L").contains(&vec![from[0], from[1], to[0], to[1]]), "{}: /L must be in the file", tag(path)); + } + } + } +} + +/// A comment is addressed as `(page, index)`, and `lib.rs` documents `index` as its position in +/// that page's `/Annots`. Fixed rather than generated, because the interesting part is not the +/// arithmetic but the interaction: a note adds its pop-up to the same array, so an index claimed +/// before a save has to still name the same comment afterwards. +#[test] +fn indices_claimed_before_a_save_still_name_the_same_comment_after_it() { + let mut doc = blank(1); + let first = add_annotation( + &mut doc, + &comment(0, Shape::Rectangle { rect: [10.0, 700.0, 90.0, 730.0] }, Style::default(), "one".into(), "A".into()), + &meta("i1".into()), + ) + .expect("adds"); + let second = add_annotation( + &mut doc, + &comment(0, Shape::Note { at: [10.0, 600.0], icon: NoteIcon::Note }, Style::default(), "two".into(), "A".into()), + &meta("i2".into()), + ) + .expect("adds"); + let third = add_annotation( + &mut doc, + &comment(0, Shape::Rectangle { rect: [10.0, 400.0, 90.0, 430.0] }, Style::default(), "three".into(), "A".into()), + &meta("i3".into()), + ) + .expect("adds"); + assert_eq!((first, second, third), (0, 1, 3), "the pop-up takes index 2"); + + for path in [Save::Incremental, Save::FullTable, Save::FullStreams] { + let (_, reopened) = save_and_reopen(&doc, path); + for (idx, want_id) in [(first, "i1"), (second, "i2"), (third, "i3")] { + let entries = annot_entries(&reopened, 0); + let d = dict_of(&reopened, &entries[idx]).unwrap_or_else(|| panic!("{}: index {idx} exists", tag(path))); + assert_eq!(str_of(&reopened, &d, b"NM").as_deref(), Some(want_id), "{}: index {idx} is still the same comment", tag(path)); + } + } +} diff --git a/crates/annot/tests/round_trip.rs b/crates/annot/tests/round_trip.rs new file mode 100644 index 0000000000..dde7f8ebc1 --- /dev/null +++ b/crates/annot/tests/round_trip.rs @@ -0,0 +1,1094 @@ +//! Structural fidelity of annotations across save and reopen (ISO 32000-2 §12.5). +//! +//! `src/tests.rs` checks comments against the document *in memory*: it edits a comment and +//! asserts what the dictionary now holds, with `reopen` used mainly to prove the bytes parse. +//! These tests take the other half — a comment is added, the file is written and re-read from +//! scratch, and what survives is checked entry by entry against values worked out here from the +//! standard rather than read back from the crate that wrote them. +//! +//! Each test replays its assertions over all three writer paths in `support::fixture` +//! (incremental, full save with a cross-reference table, full save into compressed object +//! streams). Object numbers change on the third path, so `/P`, `/Popup`, `/IRT` and `/Dest` +//! targets have to be re-pointed rather than copied — that is where reference-carrying data +//! actually breaks. +//! +//! Scope, deliberately: **structure only**. Nothing here asserts what an appearance stream draws, +//! how wide a stamp label is, or where glyphs land. Appearance generation is being rewritten by +//! several open PRs at once (#2345 stamps, #2137 pop-up windows, #2066 Vietnamese text, #1775 +//! FreeText fonts, #1513 Turkish), and a test that pinned today's drawing would be noise for +//! them. Where stored geometry is defined in terms of an appearance measurement — a line's +//! arrowhead padding, a cloud's bulge, a callout box's re-fit — these tests assert the +//! containment and internal consistency that must hold whatever that measurement is. + +mod support { + pub mod bytes; + pub mod fixture; +} + +use pdfcraft_annot::{ + AnnotError, AttachIcon, LineEnding, Markup, Meta, NewAnnotation, NoteIcon, OverlayFont, OverlayLook, ReviewState, Shape, StampKind, Style, + StyleChange, add_annotation, add_reply, delete_annotation, links, page_refs, props, set_locked, set_review_state, set_style, summaries, +}; +use pdfcraft_cos::{Dict, Document, ObjRef, Object, SaveOptions}; + +use support::bytes; +use support::fixture::{ + ALL_SAVES, FLAG_HIDDEN, FLAG_NO_ROTATE, FLAG_NO_ZOOM, FLAG_PRINT, FLAG_READONLY, Save, annot_entries, blank, blank_with_indirect_annots, dict_of, + name_of, num, nums, page_object_numbers, pdf, save_and_reopen, str_of, +}; + +/// A deterministic `/M` date and `/NM` id, so nothing here depends on the clock. +fn meta(id: &str) -> Meta { + Meta { date: Some("D:20261001120000Z".into()), id: id.into() } +} + +/// A comment with plain styling unless a test asks otherwise. +fn ann(page: usize, shape: Shape, contents: &str) -> NewAnnotation { + NewAnnotation { page, shape, style: Style::default(), contents: contents.into(), author: "Ada".into() } +} + +fn styled(page: usize, shape: Shape, contents: &str, color: [f64; 3], opacity: f64, width: f64, fill: Option<[f64; 3]>) -> NewAnnotation { + NewAnnotation { page, shape, style: Style { color, opacity, width, fill, ..Style::default() }, contents: contents.into(), author: "Ada".into() } +} + +/// The dictionary of the `k`-th `/Annots` entry of a page. +fn annot_at(doc: &Document, page: usize, k: usize, path: Save) -> Dict { + let entries = annot_entries(doc, page); + let entry = entries.get(k).unwrap_or_else(|| panic!("{path:?}: page {page} has {} /Annots entries, wanted index {k}", entries.len())); + dict_of(doc, entry).unwrap_or_else(|| panic!("{path:?}: /Annots[{k}] is not a dictionary")) +} + +fn ref_at(doc: &Document, page: usize, k: usize) -> ObjRef { + annot_entries(doc, page)[k].as_ref().expect("an indirect /Annots entry") +} + +/// Comment subtypes on a page, in `/Annots` order. +fn kinds(doc: &Document, page: usize) -> Vec { + annot_entries(doc, page).iter().filter_map(|e| dict_of(doc, e)).map(|d| name_of(doc, &d, b"Subtype").unwrap_or_default()).collect() +} + +/// A `/LE`-style array of names, as strings. +fn name_array(doc: &Document, d: &Dict, key: &[u8]) -> Option> { + let a = doc.resolve(d.get(key)?).as_array()?.clone(); + // Every element must be a name; an array like [/OpenArrow 3] is not a valid /LE. + let mut out = Vec::with_capacity(a.len()); + for x in &a { + out.push(String::from_utf8_lossy(x.as_name()?).into_owned()); + } + Some(out) +} + +// ── the sticky note: geometry, text, flags and its pop-up ───────────────────────────────────── + +/// A sticky note carries more distinct data than any other comment: a position, an icon name, the +/// flag combination that keeps the icon unscaled on zoom and rotation, a closed `Open`, opacity, +/// author, text, date, id — plus a separate pop-up annotation that points back at it. +#[test] +fn sticky_note_and_its_popup_survive_every_save_path() { + let mut doc = blank(2); + // §12.5.6.4: a sticky note is a `Text` annotation; the icon's top-left sits at `at`, and + // NOTE_SIZE (20 pt) gives the box, so /Rect is [100 680 120 700]. + let i = add_annotation(&mut doc, &ann(0, Shape::Note { at: [100.0, 700.0], icon: NoteIcon::Key }, "Left margin note"), &meta("note-1")) + .expect("adds"); + assert_eq!(i, 0); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 0), vec!["Text".to_string(), "Popup".to_string()], "{path:?}"); + let n = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &n, b"Type").as_deref(), Some("Annot"), "{path:?}"); + assert_eq!(nums(&reopened, &n, b"Rect").as_deref(), Some(&[100.0, 680.0, 120.0, 700.0][..]), "{path:?} /Rect"); + assert_eq!(name_of(&reopened, &n, b"Name").as_deref(), Some("Key"), "{path:?}: the icon (§12.5.6.4 Table 175)"); + assert_eq!(str_of(&reopened, &n, b"Contents").as_deref(), Some("Left margin note"), "{path:?}"); + assert_eq!(str_of(&reopened, &n, b"T").as_deref(), Some("Ada"), "{path:?}"); + assert_eq!(str_of(&reopened, &n, b"NM").as_deref(), Some("note-1"), "{path:?}"); + assert_eq!(str_of(&reopened, &n, b"M").as_deref(), Some("D:20261001120000Z"), "{path:?}"); + assert_eq!(str_of(&reopened, &n, b"Subj").as_deref(), Some("Sticky Note"), "{path:?}"); + // §12.5.3: /CA defaults to 1, so full opacity writes no /CA at all. + assert!(n.get(b"CA").is_none(), "{path:?}: /CA must be absent at full opacity"); + // Notes print, and stay unscaled under zoom and rotation: Print|NoZoom|NoRotate. + assert_eq!(n.int(b"F"), Some(FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?} /F"); + assert_eq!(n.get(b"Open").map(|o| matches!(o, Object::Bool(false))), Some(true), "{path:?}: /Open false"); + assert_eq!(nums(&reopened, &n, b"C").as_deref(), Some(&[1.0, 0.82, 0.0][..]), "{path:?} /C"); + // /P names the page it belongs to — the same object, after any renumbering. + assert_eq!(n.reference(b"P").map(|r| r.num), Some(page_object_numbers(&reopened)[0]), "{path:?}: /P resolves to page 1"); + + // The pop-up names its parent and the parent names it back. + let pop = annot_at(&reopened, 0, 1, path); + assert_eq!(name_of(&reopened, &pop, b"Subtype").as_deref(), Some("Popup"), "{path:?}"); + assert_eq!(pop.reference(b"Parent"), Some(ref_at(&reopened, 0, 0)), "{path:?}: /Popup and /Parent agree both ways"); + assert_eq!(n.reference(b"Popup"), Some(ref_at(&reopened, 0, 1)), "{path:?}"); + // Acrobat's pop-up is a 200×120 pt window beside the icon, top aligned with it. + assert_eq!(nums(&reopened, &pop, b"Rect").as_deref(), Some(&[130.0, 580.0, 330.0, 700.0][..]), "{path:?}: pop-up /Rect"); + + if !matches!(path, Save::FullStreams) { + // Claims about the file's own bytes (see `support::bytes`), not via our reader. + assert_eq!(bytes::numbers_of(&file, "Rect").first(), Some(&vec![100.0, 680.0, 120.0, 700.0]), "{path:?}"); + assert!(bytes::texts_of(&file, "NM").contains(&"note-1".to_string()), "{path:?}"); + assert!(bytes::texts_of(&file, "Contents").contains(&"Left margin note".to_string()), "{path:?}"); + assert!(bytes::has_name(&file, "Key"), "{path:?}: the icon name is in the file"); + } + } +} + +/// Opacity below 1 must reach the file as `/CA`; an out-of-range value is clamped, and a clamp +/// to 1.0 means the key disappears rather than being written as `1`. +#[test] +fn opacity_is_written_only_when_it_differs_from_one() { + let cases = [(0.65, Some(0.65)), (1.0, None), (1.75, None), (0.0, Some(0.0)), (-0.25, Some(0.0))]; + for (opacity, expected) in cases { + let mut doc = blank(1); + add_annotation( + &mut doc, + &styled(0, Shape::Rectangle { rect: [10.0, 10.0, 60.0, 40.0] }, "x", [0.9, 0.1, 0.1], opacity, 2.0, None), + &meta("o"), + ) + .expect("adds"); + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + assert_eq!(num(&reopened, &d, b"CA"), expected, "{path:?}: /CA at the given opacity {opacity}"); + assert_eq!(props(&reopened, 0, 0).map(|p| p.opacity), Some(expected.unwrap_or(1.0)), "{path:?}: reported opacity {opacity}"); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::scalars_of(&file, "CA").first().copied(), expected, "{path:?}: /CA written at opacity {opacity}"); + } + } + } +} + +// ── text markup: quadrilaterals are the geometry (§12.5.6.10) ───────────────────────────────── + +/// The four markup kinds differ in `/Subtype` alone; each keeps its quadrilateral list and derived +/// rectangle. The two-quad case shows the array is stored whole (16 numbers) rather than +/// truncated to the first quad, and that `/Rect` spans both. +#[test] +fn markup_kinds_keep_quads_and_rectangles() { + let kinds = + [("Highlight", Markup::Highlight), ("Underline", Markup::Underline), ("StrikeOut", Markup::StrikeOut), ("Squiggly", Markup::Squiggly)]; + let q1 = [72.0, 712.0, 200.0, 712.0, 72.0, 700.0, 200.0, 700.0]; + let q2 = [72.0, 692.0, 320.0, 692.0, 72.0, 680.0, 320.0, 680.0]; + let mut doc = blank(1); + for (k, name) in kinds.iter() { + add_annotation(&mut doc, &ann(0, Shape::TextMarkup { kind: *name, quads: vec![q1] }, "word"), &meta(&format!("m-{k}"))).expect("adds"); + } + // A two-quad highlight last, so the multi-array case sits at a known index. + let two = add_annotation(&mut doc, &ann(0, Shape::TextMarkup { kind: Markup::Highlight, quads: vec![q1, q2] }, "two lines"), &meta("m-two")) + .expect("adds"); + assert_eq!(two, kinds.len()); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let mut want = kinds.iter().map(|(label, _)| (*label).to_string()).collect::>(); + want.push("Highlight".into()); // the two-quad comment added last + assert_eq!(kinds_of(&reopened, 0), want, "{path:?}"); + for (n, label) in kinds.iter().map(|(label, _)| *label).enumerate() { + let d = annot_at(&reopened, 0, n, path); + assert_eq!(nums(&reopened, &d, b"QuadPoints").as_deref(), Some(&q1[..]), "{path:?}: /QuadPoints verbatim"); // attribution dropped: nothing here tests any specific reader's ordering + assert_eq!(nums(&reopened, &d, b"Rect").as_deref(), Some(&[72.0, 700.0, 200.0, 712.0][..]), "{path:?}: /Rect bounds the quad"); + assert!(d.get(b"BS").is_none(), "{path:?}: markup writes no border style"); + assert_eq!(str_of(&reopened, &d, b"NM").as_deref(), Some(format!("m-{label}").as_str()), "{path:?}"); + } + let d = annot_at(&reopened, 0, two, path); + assert_eq!( + nums(&reopened, &d, b"QuadPoints").as_deref(), + Some(&[q1.to_vec(), q2.to_vec()].concat()[..]), + "{path:?}: two quads are 16 numbers" + ); + assert_eq!(nums(&reopened, &d, b"Rect").as_deref(), Some(&[72.0, 680.0, 320.0, 712.0][..]), "{path:?}: /Rect spans both quads"); + if !matches!(path, Save::FullStreams) { + let sixteen = [q1.to_vec(), q2.to_vec()].concat(); + assert_eq!(bytes::numbers_of(&file, "QuadPoints").last(), Some(&sixteen), "{path:?}: 16 fixed-decimal numbers in the file"); + } + } +} + +fn kinds_of(doc: &Document, page: usize) -> Vec { + kinds(doc, page) +} + +/// `Summary` reports quadrilaterals as 8-number chunks; the same list must chunk identically +/// after a save, and its rectangle must be the bounds those chunks imply. +#[test] +fn summary_quads_and_rect_agree_after_reopen() { + let q1 = [72.0, 300.0, 160.0, 300.0, 72.0, 286.0, 160.0, 286.0]; + let q2 = [72.0, 280.0, 500.0, 280.0, 72.0, 266.0, 500.0, 266.0]; + let mut doc = blank(1); + add_annotation(&mut doc, &ann(0, Shape::TextMarkup { kind: Markup::Squiggly, quads: vec![q1, q2] }, "s"), &meta("sq")).expect("adds"); + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + let sum = summaries(&reopened).into_iter().find(|s| s.subtype == "Squiggly").expect("the squiggly is listed"); + assert_eq!(sum.quads, vec![q1.map(|v| v as f32), q2.map(|v| v as f32)], "{path:?}"); + assert_eq!(sum.rect, [72.0f32, 266.0, 500.0, 300.0], "{path:?}"); + assert_eq!(sum.contents.as_deref(), Some("s"), "{path:?}"); + assert_eq!(sum.author.as_deref(), Some("Ada"), "{path:?}"); + } +} + +// ── shapes, lines and drawings ──────────────────────────────────────────────────────────────── + +/// Squares, ovals and stamps keep an exact `/Rect` (reversed corners normalised as §12.5.4.2 +/// requires), interior colour, border width and stamp identity. +#[test] +fn shapes_keep_rectangles_fills_borders_and_stamp_identity() { + let mut doc = blank(1); + add_annotation( + &mut doc, + &styled(0, Shape::Rectangle { rect: [300.0, 300.0, 200.0, 200.0] }, "box", [0.9, 0.1, 0.1], 1.0, 2.0, Some([0.0, 0.5, 0.0])), + &meta("sq"), + ) + .expect("adds square"); + add_annotation(&mut doc, &styled(0, Shape::Oval { rect: [50.0, 60.0, 90.0, 120.0] }, "oval", [0.0, 0.4, 0.8], 1.0, 3.0, None), &meta("ov")) + .expect("adds oval"); + add_annotation( + &mut doc, + &styled( + 0, + Shape::Stamp { rect: [40.0, 600.0, 180.0, 650.0], stamp: StampKind::Approved, by: Some("Ada".into()) }, + "stamped", + [0.13, 0.55, 0.13], + 1.0, + 2.0, + None, + ), + &meta("st"), + ) + .expect("adds stamp"); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let sq = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &sq, b"Subtype").as_deref(), Some("Square"), "{path:?}"); + // Corners were given as [300 300 200 200]; a rectangle is stored lower-left then upper-right. + assert_eq!(nums(&reopened, &sq, b"Rect").as_deref(), Some(&[200.0, 200.0, 300.0, 300.0][..]), "{path:?}"); + assert_eq!(nums(&reopened, &sq, b"IC").as_deref(), Some(&[0.0, 0.5, 0.0][..]), "{path:?}: /IC (§12.5.6.8)"); + assert_eq!(nums(&reopened, &sq, b"C").as_deref(), Some(&[0.9, 0.1, 0.1][..]), "{path:?}"); + let p = props(&reopened, 0, 0).expect("properties"); + assert_eq!(p.width, Some(2.0), "{path:?}: /BS /W"); + assert_eq!(p.fill, Some([0.0, 0.5, 0.0]), "{path:?}"); + assert!(p.fillable, "{path:?}: rectangles can be filled"); + + let ov = annot_at(&reopened, 0, 1, path); + assert_eq!(name_of(&reopened, &ov, b"Subtype").as_deref(), Some("Circle"), "{path:?}: an oval is /Circle (§12.5.6.9)"); + assert_eq!(nums(&reopened, &ov, b"Rect").as_deref(), Some(&[50.0, 60.0, 90.0, 120.0][..]), "{path:?}"); + assert!(ov.get(b"IC").is_none(), "{path:?}: no interior colour when unfilled"); + assert_eq!(props(&reopened, 0, 1).and_then(|q| q.width), Some(3.0), "{path:?}"); + + let st = annot_at(&reopened, 0, 2, path); + assert_eq!(name_of(&reopened, &st, b"Subtype").as_deref(), Some("Stamp"), "{path:?}"); + assert_eq!(name_of(&reopened, &st, b"Name").as_deref(), Some("Approved"), "{path:?}: a standard stamp keeps its Table 184 name"); + assert_eq!(nums(&reopened, &st, b"Rect").as_deref(), Some(&[40.0, 600.0, 180.0, 650.0][..]), "{path:?}"); + assert!(st.contains(b"PCStamp"), "{path:?}: PdfCraft's own artwork is marked as such"); + assert_eq!(str_of(&reopened, &st, b"PCByLine").as_deref(), Some("Ada"), "{path:?}"); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::numbers_of(&file, "IC").first(), Some(&vec![0.0, 0.5, 0.0]), "{path:?}"); + assert!(bytes::has_name(&file, "Approved"), "{path:?}"); + } + } +} + +/// Lines, connected lines and polygons keep their endpoint arrays (`/L`, `/Vertices`), their +/// ending pair (`/LE`) and a cloud's intent. Their `/Rect` is grown by arrowhead and bulge +/// measurements that live in `appearance`, so this checks containment rather than exact padding. +#[test] +fn lines_and_polygons_keep_endpoints_endings_and_cloud_intent() { + let mut doc = blank(1); + add_annotation( + &mut doc, + &ann(0, Shape::Line { from: [50.0, 50.0], to: [150.0, 100.0], start: LineEnding::None, end: LineEnding::OpenArrow }, "arrow"), + &meta("l1"), + ) + .expect("adds arrow"); + add_annotation( + &mut doc, + &ann( + 0, + Shape::PolyLine { vertices: vec![[10.0, 20.0], [40.0, 60.0], [70.0, 25.0]], start: LineEnding::ClosedArrow, end: LineEnding::Butt }, + "path", + ), + &meta("pl"), + ) + .expect("adds polyline"); + add_annotation( + &mut doc, + &ann(0, Shape::Polygon { vertices: vec![[200.0, 100.0], [260.0, 110.0], [230.0, 160.0]], cloud: true }, "cloud"), + &meta("pg"), + ) + .expect("adds cloud"); + add_annotation( + &mut doc, + &ann(0, Shape::Ink { strokes: vec![vec![[0.0, 0.0], [10.0, 10.0], [20.0, 0.0]], vec![[40.0, 40.0], [50.0, 50.0]]] }, "ink"), + &meta("ink"), + ) + .expect("adds ink"); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + + let l = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &l, b"Subtype").as_deref(), Some("Line"), "{path:?}"); + assert_eq!(nums(&reopened, &l, b"L").as_deref(), Some(&[50.0, 50.0, 150.0, 100.0][..]), "{path:?}: /L (§12.5.6.7)"); + assert_eq!( + name_array(&reopened, &l, b"LE").as_deref(), + Some(&["None".to_string(), "OpenArrow".to_string()][..]), + "{path:?}: /LE (§12.5.6.7 Table 217)" + ); + let r = nums(&reopened, &l, b"Rect").expect("/Rect"); + assert!(r[0] <= 50.0 && r[1] <= 50.0 && r[2] >= 150.0 && r[3] >= 100.0, "{path:?}: /Rect must contain both endpoints, got {r:?}"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.endings), Some(vec![LineEnding::None, LineEnding::OpenArrow]), "{path:?}"); + + let pl = annot_at(&reopened, 0, 1, path); + assert_eq!(name_of(&reopened, &pl, b"Subtype").as_deref(), Some("PolyLine"), "{path:?}"); + assert_eq!(nums(&reopened, &pl, b"Vertices").as_deref(), Some(&[10.0, 20.0, 40.0, 60.0, 70.0, 25.0][..]), "{path:?}: /Vertices (§12.5.6.9)"); + assert_eq!(props(&reopened, 0, 1).and_then(|p| p.endings), Some(vec![LineEnding::ClosedArrow, LineEnding::Butt]), "{path:?}"); + + let pg = annot_at(&reopened, 0, 2, path); + assert_eq!(name_of(&reopened, &pg, b"Subtype").as_deref(), Some("Polygon"), "{path:?}"); + assert_eq!(name_of(&reopened, &pg, b"IT").as_deref(), Some("PolygonCloud"), "{path:?}: a cloud is `/IT /PolygonCloud` (§12.5.6.9)"); + let be = pg.get(b"BE").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).expect("/BE"); + assert_eq!(name_of(&reopened, &be, b"S").as_deref(), Some("C"), "{path:?}: cloudy border"); + assert_eq!(be.int(b"I"), Some(1), "{path:?}"); + assert_eq!(nums(&reopened, &pg, b"Vertices").as_deref(), Some(&[200.0, 100.0, 260.0, 110.0, 230.0, 160.0][..]), "{path:?}"); + + let ink = annot_at(&reopened, 0, 3, path); + assert_eq!(name_of(&reopened, &ink, b"Subtype").as_deref(), Some("Ink"), "{path:?}"); + let strokes: Vec> = ink + .get(b"InkList") + .map(|o| reopened.resolve(o)) + .and_then(|o| o.as_array().cloned()) + .expect("/InkList") + .iter() + .map(|s| reopened.resolve(s).as_array().expect("a stroke array").iter().filter_map(|x| x.as_f64()).collect()) + .collect(); + assert_eq!(strokes, vec![vec![0.0, 0.0, 10.0, 10.0, 20.0, 0.0], vec![40.0, 40.0, 50.0, 50.0]], "{path:?}: both strokes, all their points"); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::numbers_of(&file, "L").first(), Some(&vec![50.0, 50.0, 150.0, 100.0]), "{path:?}"); + assert_eq!(bytes::numbers_of(&file, "Vertices").first(), Some(&vec![10.0, 20.0, 40.0, 60.0, 70.0, 25.0]), "{path:?}"); + } + } +} + +// ── FreeText: the box, its font size and a callout's leader line ─────────────────────────────── + +/// A text box stores its rectangle, colour and size in `/DA`/`Q`, and typewriter text marks its +/// intent (§12.5.6.6). The stored box is the requested one: re-fitting happens on later edits, +/// not at creation. +#[test] +fn text_boxes_keep_rectangles_font_size_and_intent() { + let mut doc = blank(1); + add_annotation( + &mut doc, + &styled(0, Shape::TextBox { rect: [300.0, 600.0, 500.0, 650.0], font_size: 14.0 }, "Type here", [0.1, 0.2, 0.3], 1.0, 0.0, None), + &meta("tb"), + ) + .expect("adds text box"); + add_annotation( + &mut doc, + &styled(0, Shape::Typewriter { rect: [72.0, 100.0, 300.0, 124.0], font_size: 12.0 }, "Typed words", [0.0, 0.0, 0.0], 1.0, 0.0, None), + &meta("tw"), + ) + .expect("adds typewriter"); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let tb = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &tb, b"Subtype").as_deref(), Some("FreeText"), "{path:?}"); + assert_eq!(nums(&reopened, &tb, b"Rect").as_deref(), Some(&[300.0, 600.0, 500.0, 650.0][..]), "{path:?}"); + assert_eq!(str_of(&reopened, &tb, b"DA").as_deref(), Some("0.1 0.2 0.3 rg /Helv 14 Tf"), "{path:?}: /DA (§12.5.6.6, §12.7.2.3)"); + assert_eq!(tb.int(b"Q"), Some(0), "{path:?}: left aligned"); + assert!(tb.get(b"IT").is_none(), "{path:?}: a plain text box sets no intent"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.font_size), Some(14.0), "{path:?}"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.color), Some([0.1, 0.2, 0.3]), "{path:?}: FreeText colour comes from /DA"); + + let tw = annot_at(&reopened, 0, 1, path); + assert_eq!(name_of(&reopened, &tw, b"IT").as_deref(), Some("FreeTextTypeWriter"), "{path:?}"); + assert_eq!(nums(&reopened, &tw, b"Rect").as_deref(), Some(&[72.0, 100.0, 300.0, 124.0][..]), "{path:?}"); + assert_eq!(props(&reopened, 0, 1).and_then(|p| p.font_size), Some(12.0), "{path:?}"); + assert!(tw.contains(b"PCFillSign"), "{path:?}: typewriter text is a Fill & Sign item"); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::texts_of(&file, "DA").first().map(String::as_str), Some("0.1 0.2 0.3 rg /Helv 14 Tf"), "{path:?}"); + } + } +} + +/// A callout keeps six numbers in `/CL` (arrow point, knee, box attach) plus the inset `/RD`; +/// together those must rebuild exactly the text box that was asked for. That is a consistency +/// claim, so it does not depend on how wide the arrowhead is drawn. +#[test] +fn callout_keeps_its_leader_line_and_rebuilds_the_same_text_box() { + let mut doc = blank(1); + add_annotation( + &mut doc, + &ann( + 0, + Shape::Callout { + rect: [300.0, 400.0, 420.0, 460.0], + knee: [250.0, 380.0], + point: [200.0, 350.0], + font_size: 16.0, + ending: LineEnding::OpenArrow, + }, + "why", + ), + &meta("co"), + ) + .expect("adds callout"); + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &d, b"IT").as_deref(), Some("FreeTextCallout"), "{path:?}"); + // The knee is left of the box, so the leader attaches at the box's left edge midpoint. + assert_eq!(nums(&reopened, &d, b"CL").as_deref(), Some(&[200.0, 350.0, 250.0, 380.0, 300.0, 430.0][..]), "{path:?}: /CL point→knee→attach"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.endings), Some(vec![LineEnding::OpenArrow]), "{path:?}"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.font_size), Some(16.0), "{path:?}"); + let rect = nums(&reopened, &d, b"Rect").expect("/Rect"); + let rd = nums(&reopened, &d, b"RD").expect("/RD"); + assert!(rd.iter().all(|v| *v >= 0.0), "{path:?}: /RD insets are non-negative (§12.5.3)"); + assert_eq!( + [rect[0] + rd[0], rect[1] + rd[1], rect[2] - rd[2], rect[3] - rd[3]], + [300.0, 400.0, 420.0, 460.0], + "{path:?}: /Rect inset by /RD is the text box" + ); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::numbers_of(&file, "CL").first(), Some(&vec![200.0, 350.0, 250.0, 380.0, 300.0, 430.0]), "{path:?}"); + } + } +} + +// ── redaction and file attachment: payload data, not just dictionary keys ───────────────────── + +/// A redaction stores the area it will cover, the colour it will paint and its overlay text with +/// style. Applying a redaction is other crates' business; here only the data has to survive. +#[test] +fn redaction_keeps_quads_fill_and_overlay_text() { + let quads = vec![[72.0, 712.0, 300.0, 712.0, 72.0, 690.0, 300.0, 690.0], [72.0, 680.0, 240.0, 680.0, 72.0, 660.0, 240.0, 660.0]]; + let mut doc = blank(1); + add_annotation( + &mut doc, + &styled( + 0, + Shape::Redact { + quads: quads.clone(), + overlay: "SECRET".into(), + look: OverlayLook { font: OverlayFont::Courier, size: 0.0, color: [1.0, 0.0, 0.0], align: 2, repeat: true }, + }, + "remove this", + [0.89, 0.13, 0.13], + 1.0, + 1.0, + Some([0.0, 0.0, 0.0]), + ), + &meta("red"), + ) + .expect("adds redaction"); + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &d, b"Subtype").as_deref(), Some("Redact"), "{path:?}"); + assert_eq!(nums(&reopened, &d, b"QuadPoints").as_deref(), Some(&quads.concat()[..]), "{path:?}"); + assert_eq!(nums(&reopened, &d, b"IC").as_deref(), Some(&[0.0, 0.0, 0.0][..]), "{path:?}: /IC is the colour applied (§12.5.6.23)"); + assert_eq!(str_of(&reopened, &d, b"OverlayText").as_deref(), Some("SECRET"), "{path:?}"); + assert_eq!(str_of(&reopened, &d, b"DA").as_deref(), Some("1 0 0 rg /Cour 0 Tf"), "{path:?}: the overlay font resource"); + assert_eq!(d.int(b"Q"), Some(2), "{path:?}: right aligned"); + assert_eq!(d.get(b"Repeat").map(|o| matches!(o, Object::Bool(true))), Some(true), "{path:?}"); + assert_eq!(nums(&reopened, &d, b"Rect").as_deref(), Some(&[72.0, 660.0, 300.0, 712.0][..]), "{path:?}"); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::texts_of(&file, "OverlayText").first().map(String::as_str), Some("SECRET"), "{path:?}"); + } + } +} + +/// An attached file is the heaviest annotation there is: a file specification, an embedded file +/// stream with its own parameters, and the bytes inside it. A full rewrite renumbers all three +/// objects and may move them into object streams, so this is the strongest reference test here. +#[test] +fn attached_file_specification_and_bytes_survive_every_save_path() { + let body = b"first line\nsecond line, 27 bytes more\n".to_vec(); + let mut doc = blank(1); + add_annotation( + &mut doc, + &ann(0, Shape::Attachment { at: [300.0, 600.0], icon: AttachIcon::Paperclip, file: "notes.txt".into(), data: body.clone() }, ""), + &meta("att"), + ) + .expect("attaches"); + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + assert_eq!(name_of(&reopened, &d, b"Subtype").as_deref(), Some("FileAttachment"), "{path:?}"); + assert_eq!(name_of(&reopened, &d, b"Name").as_deref(), Some("Paperclip"), "{path:?}"); + // With no comment text the file's name is shown as the contents (§12.5.6.15). + assert_eq!(str_of(&reopened, &d, b"Contents").as_deref(), Some("notes.txt"), "{path:?}"); + assert_eq!(nums(&reopened, &d, b"Rect").as_deref(), Some(&[300.0, 580.0, 320.0, 600.0][..]), "{path:?}"); + + let fs_ref = d.reference(b"FS").expect("/FS must reference a file specification"); + let fs = reopened.get(fs_ref).as_dict().cloned().expect("/FS resolves to a dictionary"); + assert_eq!(name_of(&reopened, &fs, b"Type").as_deref(), Some("Filespec"), "{path:?}"); + assert_eq!(str_of(&reopened, &fs, b"F").as_deref(), Some("notes.txt"), "{path:?}"); + let ef = fs.get(b"EF").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).expect("/EF"); + let file_ref = ef.reference(b"F").expect("/EF /F references the embedded stream"); + let Object::Stream(s) = &*reopened.get(file_ref) else { panic!("the embedded file is a stream") }; + assert_eq!(s.decoded().expect("decodes").as_slice(), &body[..], "{path:?}: the file's bytes survive"); + let params = s.dict.get(b"Params").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).expect("/Params"); + assert_eq!(params.int(b"Size").map(|v| v as usize), Some(body.len()), "{path:?}"); + } +} + +// ── order, indices and page ownership across several comments ───────────────────────────────── + +/// Five comments on two pages keep their per-page order, the `(page, index)` addressing documented +/// at the top of `lib.rs`, and each one's `/P`. `summaries` additionally promises page order then +/// top edge first; both are checked, because `index` refers to `/Annots` position while the list +/// order does not. +/// COVERAGE HONESTY (ten single-site mutations, run independently by two reviewers plus the +/// author; run at base 99941daa, before main split `lib.rs` into modules - the anchors below +/// are the merged layout, re-derived line by line from the split files). FIVE are also killed by pre-existing inline tests in `crates/annot/src/tests.rs`, and +/// they share a family — the clamp is INVERTED, or a key/order changed rather than removed: +/// `create.rs:47` inverted (`ink_strokes_are_smooth_curves_through_their_points`), `create.rs:216` inverted +/// (six inline tests), `modify.rs:547` inverted (`restyle_and_resize_redraw_the_appearance`), +/// `info.rs:151` tie-break swapped (`stamps_draw_their_label_and_by_line`, the same tie-break as +/// here) and `create.rs:450` `/Parent` renamed (`every_tool_creates_a_drawable_comment`). This file shares that +/// credit and claims none of it alone. The other FIVE are killed only by tests added here, and +/// they share a different family — the clamp is REMOVED outright, or introduced where the code +/// does not have one: `create.rs:216` deleted, `create.rs:47` deleted, the opacity clamp at +/// `create.rs:212` removed, the colour clamp in `rgb()` at `create.rs:14` removed (killed only by +/// `malformed::the_same_huge_fill_is_accepted_on_create_and_refused_on_edit`), and a clamp ADDED +/// to the read path at `info.rs:231`. Note that `create.rs:47` appears on both sides, and +/// deliberately so: +/// inverting that clamp trips an inline test as well, while deleting it trips nothing but +/// `a_negative_and_a_zero_border_pad_a_line_identically`. Confusing those two mutations is easy +/// enough to produce a false refutation, so the distinction is recorded here rather than left to +/// whoever re-runs them. +/// Two further notes: the stored `/Annots` order checked below goes through all three save paths, +/// which no inline test does; and `properties.rs::several_rectangles_keep_count_order_and_geometry` +/// constrains stored array order only — it does NOT exercise `summaries()` ordering. +#[test] +fn several_comments_keep_order_indices_and_page_ownership() { + let mut doc = blank(4); + add_annotation(&mut doc, &ann(0, Shape::Rectangle { rect: [10.0, 100.0, 60.0, 140.0] }, "low"), &meta("a")).expect("adds"); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [10.0, 700.0], icon: NoteIcon::Note }, "high note"), &meta("b")).expect("adds"); + add_annotation( + &mut doc, + &ann(0, Shape::TextMarkup { kind: Markup::Highlight, quads: vec![[10.0, 450.0, 90.0, 450.0, 10.0, 436.0, 90.0, 436.0]] }, "mid"), + &meta("c"), + ) + .expect("adds"); + add_annotation(&mut doc, &ann(2, Shape::Oval { rect: [20.0, 30.0, 80.0, 90.0] }, "p3 oval"), &meta("d")).expect("adds"); + add_annotation(&mut doc, &ann(2, Shape::Caret { rect: [300.0, 400.0, 312.0, 424.0] }, "p3 caret"), &meta("e")).expect("adds"); + + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 0), ["Square", "Text", "Popup", "Highlight"].map(str::to_owned), "{path:?}: page 1 order"); + assert_eq!(kinds(&reopened, 2), ["Circle", "Caret"].map(str::to_owned), "{path:?}: page 3 order"); + assert_eq!(annot_entries(&reopened, 1).len(), 0, "{path:?}: page 2 stays comment-free"); + assert_eq!(annot_entries(&reopened, 3).len(), 0, "{path:?}"); + + // Every comment's /P names its own page object (pop-ups carry no /P). + let pages = page_object_numbers(&reopened); + for (page, want) in [(0usize, pages[0]), (2, pages[2])] { + for e in annot_entries(&reopened, page) { + let d = dict_of(&reopened, &e).expect("a dictionary"); + if name_of(&reopened, &d, b"Subtype").as_deref() == Some("Popup") { + continue; + } + assert_eq!(d.reference(b"P").map(|r| r.num), Some(want), "{path:?}: /P of {:?}", name_of(&reopened, &d, b"Subtype")); + } + } + + let sums = summaries(&reopened); + assert_eq!(sums.len(), 5, "{path:?}: pop-ups are not comments"); + let mut got: Vec<(usize, usize, String)> = sums.iter().map(|s| (s.page, s.index, s.subtype.clone())).collect(); + got.sort(); + assert_eq!( + got, + vec![ + (0, 0, "Square".to_string()), + (0, 1, "Text".to_string()), + (0, 3, "Highlight".to_string()), + (2, 0, "Circle".to_string()), + (2, 1, "Caret".to_string()) + ], + "{path:?}: (page, index in /Annots, subtype)" + ); + for w in sums.windows(2) { + if w[0].page == w[1].page { + assert!(w[0].rect[3] >= w[1].rect[3], "{path:?}: top edge first, got {:?} then {:?}", w[0].rect, w[1].rect); + } else { + assert!(w[0].page < w[1].page, "{path:?}: page by page"); + } + } + } +} + +/// `annot::page_refs` — the crate's own page walk, cycle guard and all — must agree with this +/// file's independent walk over the same reopened bytes. +#[test] +fn page_refs_agrees_with_an_independent_walk() { + let mut doc = blank(3); + add_annotation(&mut doc, &ann(1, Shape::Rectangle { rect: [0.0, 0.0, 20.0, 20.0] }, "x"), &meta("p")).expect("adds"); + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + let mine = page_object_numbers(&reopened); + let theirs: Vec = page_refs(&reopened).expect("a page tree").iter().map(|r| r.num).collect(); + assert_eq!(mine, theirs, "{path:?}"); + } +} + +// ── threads: replies and review states keep their edges ──────────────────────────────────────── + +/// A reply is a text annotation whose `/IRT` points at its parent; setting a status adds another +/// one. Deleting a comment takes its pop-up and its replies with it. Every edge there is a +/// reference, so a full rewrite has to re-point them all. +#[test] +fn reply_threads_and_the_delete_cascade_survive_every_save_path() { + let mut doc = blank(1); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [100.0, 700.0], icon: NoteIcon::Comment }, "parent"), &meta("p1")).expect("adds parent"); + add_reply(&mut doc, 0, 0, "child one", "Ben", &meta("r1")).expect("replies"); + let status = set_review_state(&mut doc, 0, 0, ReviewState::Accepted, "Ben", &meta("r2")).expect("sets status"); + assert_eq!(status, 3); + + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 0), vec!["Text", "Popup", "Text", "Text"], "{path:?}: parent, pop-up, reply, status reply"); + let parent = ref_at(&reopened, 0, 0); + for k in [2usize, 3] { + let d = annot_at(&reopened, 0, k, path); + assert_eq!(d.reference(b"IRT"), Some(parent), "{path:?}: /IRT points at the parent after renumbering"); + assert_eq!(str_of(&reopened, &d, b"T").as_deref(), Some("Ben"), "{path:?}"); + } + let st = annot_at(&reopened, 0, status, path); + assert_eq!(str_of(&reopened, &st, b"State").as_deref(), Some("Accepted"), "{path:?}"); + assert_eq!(str_of(&reopened, &st, b"StateModel").as_deref(), Some("Review"), "{path:?}"); + assert_eq!(str_of(&reopened, &st, b"Subj").as_deref(), Some("Status"), "{path:?}"); + // Review-state records are hidden; ordinary text replies retain their visible comment flags. + assert_eq!(st.int(b"F"), Some(FLAG_HIDDEN | FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}"); + assert_eq!(annot_at(&reopened, 0, 2, path).int(b"F"), Some(FLAG_PRINT | FLAG_NO_ZOOM | FLAG_NO_ROTATE), "{path:?}: ordinary reply"); + let listed = summaries(&reopened); + assert_eq!(listed.iter().filter(|s| s.in_reply_to.as_deref() == Some("p1")).count(), 2, "{path:?}: both replies name the parent's /NM"); + assert_eq!(listed.iter().filter(|s| s.state.as_deref() == Some("Accepted")).count(), 1, "{path:?}"); + + // Deleting the comment takes its pop-up and both replies with it. + let mut cut = reopened.clone(); + delete_annotation(&mut cut, 0, 0).expect("deletes the thread"); + assert_eq!(annot_entries(&cut, 0).len(), 0, "{path:?}: nothing of the thread is left in memory"); + let (_, cut) = save_and_reopen(&cut, path); + assert_eq!(annot_entries(&cut, 0).len(), 0, "{path:?}: and the file agrees"); + assert!(summaries(&cut).is_empty(), "{path:?}"); + } +} + +/// The Locked flag is not decoration: a comment that reads back locked must refuse the edits the +/// crate says it refuses. Checking after a save means the flag survived on disk, not merely in +/// memory that happened to still hold it. +#[test] +fn locked_flag_survives_and_still_refuses_edits_after_reopen() { + let mut doc = blank(1); + add_annotation(&mut doc, &ann(0, Shape::Rectangle { rect: [10.0, 20.0, 60.0, 50.0] }, "locked box"), &meta("lk")).expect("adds"); + set_locked(&mut doc, 0, 0, true).expect("locks"); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + assert_eq!(d.int(b"F"), Some(FLAG_PRINT | FLAG_READONLY), "{path:?}: Print plus ReadOnly (bit 8)"); + assert_eq!(props(&reopened, 0, 0).map(|p| p.locked), Some(true), "{path:?}"); + let mut try_move = reopened.clone(); + let e = pdfcraft_annot::move_annotation(&mut try_move, 0, 0, 5.0, 5.0, &meta("nope")).err(); + assert_eq!(e, Some(AnnotError::Invalid("the comment is locked".into())), "{path:?}: moving a locked comment refuses"); + let mut try_delete = reopened.clone(); + assert!(delete_annotation(&mut try_delete, 0, 0).is_err(), "{path:?}: deleting a locked comment refuses"); + if !matches!(path, Save::FullStreams) { + assert_eq!(bytes::scalars_of(&file, "F").first(), Some(&132.0), "{path:?}: the flag value is in the file"); + } + } +} + +// ── links (§12.5.6.5) ──────────────────────────────────────────────────────────────────────── + +/// A link's action is a destination array naming a page object, or a URI action. Page targets are +/// the interesting case across a full rewrite: the link must land on the same page after every +/// object number changes. NOTE (naming): this covers link structure — `/A`, `/Dest`, `/URI` and the +/// dash pattern. It deliberately says nothing about appearance streams, which are out of scope here +/// because #2345/#2137/#2066/#1775/#1513 are rewriting appearance generation. +#[test] +fn links_keep_page_targets_uri_and_dash_structure_across_renumbering() { + let mut doc = blank(3); + add_annotation(&mut doc, &ann(0, Shape::Rectangle { rect: [10.0, 700.0, 90.0, 720.0] }, "shape"), &meta("s")).expect("adds a comment too"); + let i = links::add( + &mut doc, + 0, + [120.0, 700.0, 240.0, 720.0], + &links::LinkAction::Page { page: 2, zoom: links::LinkZoom::FitPage }, + &links::LinkStyle { visible: true, width: 2.0, dashed: true, ..Default::default() }, + ) + .expect("links to page 3"); + assert_eq!(i, 1); + links::add( + &mut doc, + 1, + [50.0, 600.0, 200.0, 620.0], + &links::LinkAction::Uri("https://example.com/a?b=c&d=1".into()), + &links::LinkStyle::default(), + ) + .expect("links out"); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let pages = page_object_numbers(&reopened); + let got: Vec<(usize, usize, [f64; 4], String, bool)> = links::list(&reopened) + .into_iter() + .map(|l| { + let a = match l.action { + links::LinkAction::Page { page: p, zoom: _ } => format!("page {p}"), + links::LinkAction::Uri(u) => u, + links::LinkAction::Other(o) => format!("other:{o}"), + }; + (l.page, l.index, l.rect, a, l.style.visible) + }) + .collect(); + assert_eq!(got.len(), 2, "{path:?}"); + assert_eq!(got[0], (0, 1, [120.0, 700.0, 240.0, 720.0], "page 2".into(), true), "{path:?}: the target is still page 3"); + assert_eq!(got[1], (1, 0, [50.0, 600.0, 200.0, 620.0], "https://example.com/a?b=c&d=1".into(), false), "{path:?}"); + + let d = annot_at(&reopened, 0, 1, path); + let dest = d.get(b"Dest").map(|o| reopened.resolve(o)).and_then(|o| o.as_array().cloned()).expect("/Dest is an array"); + assert_eq!(dest.first().and_then(|x| x.as_ref()).map(|r| r.num), Some(pages[2]), "{path:?}: /Dest names the page object"); + assert_eq!(name_of(&reopened, &d, b"H").as_deref(), Some("I"), "{path:?}: highlight /H (invert, §12.5.3)"); + let bs = d.get(b"BS").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).expect("/BS"); + assert_eq!(name_of(&reopened, &bs, b"S").as_deref(), Some("D"), "{path:?}: dashed"); + assert_eq!(bs.get(b"W").and_then(|w| w.as_f64()), Some(2.0), "{path:?}"); + assert_eq!(summaries(&reopened).iter().filter(|s| s.page == 0).count(), 1, "{path:?}: only the rectangle is a comment"); + if !matches!(path, Save::FullStreams) { + assert_eq!( + bytes::texts_of(&file, "URI").first().map(String::as_str), + Some("https://example.com/a?b=c&d=1"), + "{path:?}: the URI is in the file" + ); + } + } +} + +/// Removing every link leaves comments alone. Both directions are index arithmetic over one shared +/// array, so a mistake shows up as a lost comment or an orphaned link. +#[test] +fn removing_all_links_keeps_comments() { + let mut doc = blank(2); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [50.0, 700.0], icon: NoteIcon::Help }, "note"), &meta("n")).expect("adds"); + links::add( + &mut doc, + 0, + [200.0, 700.0, 300.0, 715.0], + &links::LinkAction::Page { page: 0, zoom: links::LinkZoom::FitPage }, + &links::LinkStyle::default(), + ) + .expect("links"); + let removed = links::remove_all(&mut doc, None).expect("removes all links"); + assert_eq!(removed, 1); + + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 0), vec!["Text", "Popup"], "{path:?}: the note and its pop-up stay"); + assert!(links::list(&reopened).is_empty(), "{path:?}"); + } +} + +// ── data the crate does not understand, and nothing at all ───────────────────────────────────── + +/// A stranger's file can hold inline annotation dictionaries and keys PdfCraft has never heard of. +/// Fidelity says they are preserved rather than dropped (`CLAUDE.md`: preserve unknown data). +#[test] +fn inline_annotations_and_unknown_keys_survive_every_save_path() { + let objs: Vec<&[u8]> = vec![ + b"<< /Type /Catalog /Pages 2 0 R >>", + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [<< /Type /Annot /Subtype /Square /Rect [10 10 50 50] /C [1 0 0] /OddKey (kept) /AnotherLevel << /Deep 42 >> >>] >>", + ]; + let mut doc = Document::open(std::sync::Arc::new(pdf(&objs))).expect("the stranger's file opens"); + add_annotation(&mut doc, &ann(0, Shape::Rectangle { rect: [100.0, 100.0, 150.0, 130.0] }, "ours"), &meta("mine")).expect("adds ours"); + + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 0), vec!["Square", "Square"], "{path:?}: theirs first, ours second"); + let foreign = annot_at(&reopened, 0, 0, path); + assert_eq!(str_of(&reopened, &foreign, b"OddKey").as_deref(), Some("kept"), "{path:?}: an unknown string key"); + let deep = foreign.get(b"AnotherLevel").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).expect("an unknown dictionary"); + assert_eq!(deep.int(b"Deep"), Some(42), "{path:?}"); + assert_eq!(nums(&reopened, &foreign, b"Rect").as_deref(), Some(&[10.0, 10.0, 50.0, 50.0][..]), "{path:?}"); + assert_eq!(str_of(&reopened, &annot_at(&reopened, 0, 1, path), b"NM").as_deref(), Some("mine"), "{path:?}: our own comment is still there"); + } +} + +/// A document with nothing on it round trips without acquiring stray keys: no `/Annots` at all, +/// and an empty comment list. Cheap to check, and it is the case where a writer bug would add +/// noise that no other assertion in this file would notice. +#[test] +fn an_empty_document_stays_empty() { + let doc = blank(2); + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let pages = page_object_numbers(&reopened); + assert_eq!(pages.len(), 2, "{path:?}"); + for (page, &number) in pages.iter().enumerate() { + assert!(annot_entries(&reopened, page).is_empty(), "{path:?}: no entries on page {}", page + 1); + let p = reopened.get(ObjRef::new(number, 0)); + assert!(p.as_dict().is_some_and(|d| !d.contains(b"Annots")), "{path:?}: page {} has no /Annots key", page + 1); + } + assert!(summaries(&reopened).is_empty(), "{path:?}"); + assert!(file.starts_with(b"%PDF-"), "{path:?}"); + } +} + +/// An empty `/Annots` array written by someone else and referenced indirectly: adding the first +/// comment must write through that shared object rather than replacing the page's reference with a +/// fresh array, or a viewer holding the old reference would still see no annotations. +#[test] +fn first_comment_into_an_indirect_annotss_array_writes_through_it() { + let mut doc = blank_with_indirect_annots(2, 1); + add_annotation(&mut doc, &ann(1, Shape::Oval { rect: [20.0, 30.0, 60.0, 70.0] }, "o"), &meta("w")).expect("adds"); + let shared = { + let pages = page_object_numbers(&doc); + doc.get(ObjRef::new(pages[1], 0)).as_dict().and_then(|d| d.reference(b"Annots")).expect("the skeleton's shared /Annots object") + }; + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 1), vec!["Circle"], "{path:?}"); + if path == Save::Incremental { + // The original object is still the page's /Annots on an incremental save. + let pages = page_object_numbers(&reopened); + let now = reopened.get(ObjRef::new(pages[1], 0)).as_dict().and_then(|d| d.reference(b"Annots")); + assert_eq!(now, Some(shared), "{path:?}: the page still points at the same array object"); + assert_eq!(reopened.get(shared).as_array().map(|a| a.len()), Some(1), "{path:?}"); + } + } +} + +/// Text is the payload a comment exists to carry. Acrobat separates lines with CR, PDF strings may +/// hold parentheses and backslashes, and anything outside printable ASCII goes out as UTF-16BE. All +/// of it must come back identical — checked against the file's bytes, not only through our reader. +#[test] +fn awkward_comment_text_survives_to_the_bytes() { + let cases = [ + "plain", + "(parenthesised) \\backslash\\", + "line one\r\nline two", + "Annà", // non-ASCII → UTF-16BE with a BOM + "emoji: \u{1F642}", // a surrogate pair in UTF-16BE + "tabs\tand\nbreaks", + ]; + for text in cases { + let mut doc = blank(1); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [10.0, 700.0], icon: NoteIcon::Note }, text), &meta("t")).expect("adds"); + for path in [Save::Incremental, Save::FullTable] { + let (file, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + assert_eq!(str_of(&reopened, &d, b"Contents").as_deref(), Some(text), "{path:?}: {text:?} through our reader"); + let found = bytes::texts_of(&file, "Contents"); + assert!(found.contains(&text.to_string()), "{path:?}: {text:?} is in the file as a PDF string, got {found:?}"); + assert_eq!(props(&reopened, 0, 0).map(|p| p.subtype), Some("Text".into()), "{path:?}"); + } + } +} + +/// Enum names reach the file as the strings the standard defines, which is what other programs +/// read. `/H`'s one-letter codes are checked through the reader instead: a bare `/P` in raw bytes +/// is ambiguous with `/Pages`, `/Parent` and every page object's `/P`. +#[test] +fn enum_names_are_written_as_the_standard_name_strings() { + let mut doc = blank(1); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [10.0, 700.0], icon: NoteIcon::NewParagraph }, "n"), &meta("e1")).expect("adds"); + links::add( + &mut doc, + 0, + [200.0, 700.0, 300.0, 715.0], + &links::LinkAction::Uri("https://example.com".into()), + &links::LinkStyle { highlight: links::Highlight::Inset, visible: true, ..Default::default() }, + ) + .expect("links"); + add_annotation( + &mut doc, + &ann(0, Shape::Line { from: [10.0, 200.0], to: [80.0, 240.0], start: LineEnding::RClosedArrow, end: LineEnding::Slash }, "l"), + &meta("e2"), + ) + .expect("adds line"); + + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + // The page holds the note, its pop-up, then the link and the line. + assert_eq!(name_of(&reopened, &annot_at(&reopened, 0, 0, path), b"Name").as_deref(), Some("NewParagraph"), "{path:?}"); + assert_eq!(name_of(&reopened, &annot_at(&reopened, 0, 2, path), b"H").as_deref(), Some("P"), "{path:?}: Inset highlight is /H /P (§12.5.3)"); + assert_eq!( + name_array(&reopened, &annot_at(&reopened, 0, 3, path), b"LE").as_deref(), + Some(&["RClosedArrow".to_string(), "Slash".to_string()][..]), + "{path:?}: both line endings" + ); + assert_eq!(props(&reopened, 0, 3).and_then(|p| p.endings), Some(vec![LineEnding::RClosedArrow, LineEnding::Slash]), "{path:?}"); + if !matches!(path, Save::FullStreams) { + assert!(bytes::has_name(&file, "NewParagraph"), "{path:?}"); + assert!(bytes::has_name(&file, "RClosedArrow") && bytes::has_name(&file, "Slash"), "{path:?}"); + } + } +} + +/// Which subtypes count as a comment is a reading rule, and it must not shift when the file is +/// rewritten: pop-ups and links stay out of `summaries`, everything else stays in. +#[test] +fn comment_and_non_comment_subtypes_stay_separated_after_saving() { + let mut doc = blank(1); + add_annotation( + &mut doc, + &ann(0, Shape::TextMarkup { kind: Markup::StrikeOut, quads: vec![[10.0, 700.0, 90.0, 700.0, 10.0, 686.0, 90.0, 686.0]] }, "strike"), + &meta("c1"), + ) + .expect("adds"); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [10.0, 600.0], icon: NoteIcon::Comment }, "note"), &meta("c2")).expect("adds"); + links::add(&mut doc, 0, [10.0, 500.0, 90.0, 520.0], &links::LinkAction::Uri("https://example.com".into()), &links::LinkStyle::default()) + .expect("adds link"); + for path in ALL_SAVES { + let (_, reopened) = save_and_reopen(&doc, path); + assert_eq!(kinds(&reopened, 0), vec!["StrikeOut", "Text", "Popup", "Link"], "{path:?}"); + let mut listed: Vec = summaries(&reopened).iter().map(|s| s.subtype.clone()).collect(); + listed.sort(); + assert_eq!(listed, vec!["StrikeOut".to_string(), "Text".to_string()], "{path:?}: pop-ups and links are not comments"); + } +} + +/// A full save with object streams and one without must describe the same annotations: same +/// count, same subtypes, same rectangles. That makes the two writer paths interchangeable for +/// annotation data, which is what "the object graph is the model" promises. +#[test] +fn a_full_rewrite_into_object_streams_describes_the_same_comments_as_a_plain_table() { + let mut doc = blank(2); + add_annotation(&mut doc, &ann(0, Shape::Note { at: [10.0, 700.0], icon: NoteIcon::Paragraph }, "both paths"), &meta("cmp1")).expect("adds"); + add_annotation(&mut doc, &ann(0, Shape::Rectangle { rect: [10.0, 20.0, 90.0, 60.0] }, "box"), &meta("cmp2")).expect("adds"); + add_annotation( + &mut doc, + &ann(1, Shape::TextMarkup { kind: Markup::Underline, quads: vec![[50.0, 600.0, 150.0, 600.0, 50.0, 586.0, 150.0, 586.0]] }, "u"), + &meta("cmp3"), + ) + .expect("adds"); + + let (_, plain) = save_and_reopen(&doc, Save::FullTable); + let (_, packed) = save_and_reopen(&doc, Save::FullStreams); + let shape_of = |d: &Document| -> Vec<(usize, Vec, Vec<[f64; 4]>)> { + (0..page_object_numbers(d).len()) + .map(|p| { + let e = annot_entries(d, p); + ( + e.len(), + kinds(d, p), + e.iter() + .filter_map(|x| dict_of(d, x)) + .filter_map(|x| nums(d, &x, b"Rect")) + .filter_map(|v| <[f64; 4]>::try_from(v).ok()) + .collect(), + ) + }) + .filter(|t: &(usize, Vec, Vec<[f64; 4]>)| t.0 > 0) + .collect() + }; + assert_eq!(shape_of(&plain), shape_of(&packed), "the two full-save paths must agree"); + assert_eq!(shape_of(&plain).len(), 2, "and both must find the two pages that have comments"); + assert_eq!(summaries(&plain).len(), summaries(&packed).len()); + + // The plain-table output is bigger here: object streams compress, which is the point of them. + let opts = SaveOptions { mod_date: None, id_seed: 0x5052_494E_5443_5241, object_streams: true, drop_unlinked: false, id_entropy: None }; + let packed_bytes = pdfcraft_cos::write_full(&doc, &opts).expect("writes"); + assert!( + packed_bytes.len() < pdfcraft_cos::write_full(&doc, &SaveOptions { object_streams: false, ..opts }).expect("writes").len(), + "the packed file should be smaller" + ); +} + +// ── measured clamps, pinned so a change is visible (§12.5.3 border width) ────────────────── + +/// Negative border width — CURRENT behaviour, measured on this branch, not an endorsement. +/// +/// `add_annotation` screens width for NON-FINITENESS only. The guard at +/// `crates/annot/src/create.rs:416` reads +/// `!finite(&style.color) || !style.opacity.is_finite() || !style.width.is_finite()`: the +/// `abs() < 1e7` cap belongs to `finite()` (`create.rs:17-19`) and is applied to `color` alone, so width +/// carries no magnitude limit at all. Measured on this branch: `width = 1e9` and `width = 1e299` +/// are both ACCEPTED and written to `/BS /W` verbatim (a 10-digit number, then a 300-digit one), +/// while `color = [1e9, 0, 0]` is refused with `Invalid("invalid style")`. +/// So `width = -4.0` is ACCEPTED too; the value reaching `/BS /W` comes from `style.width.max(0.0)` +/// (`create.rs:216`) and the `/Rect` growth from `style.width.max(0.0) / 2.0` (`create.rs:47`). The file then holds +/// `0`, and neither document's repair log records it. (`finite()` magnitude-screens `color`; +/// `opacity` and `width` get finiteness checks only, and the interior `fill` is not screened by it.) +/// +/// The edit path behaves identically for width: `set_style` applies the same kind of screen +/// (`modify.rs:513`) and `width = Some(-2.5)` is accepted, storing `0`. There is therefore NO +/// create-vs-edit asymmetry for negative widths — unlike a huge or non-finite fill colour, which +/// creation never screens (`create.rs:416` magnitude-screens `color`, finiteness-checks `opacity` and +/// `width`, and takes no look at `fill`, which is merely clamped component-wise by `rgb()` at +/// `create.rs:13-15`) while the edit path refuses the same +/// value explicitly near `modify.rs:626`. Flagged for maintainers rather than asserted as +/// correct: a refusal, or at minimum a repair-log entry naming the substitution, would be the +/// stricter design. If either clamp changes or goes away, this test fails on purpose so that +/// decision is made in the open. +#[test] +fn negative_border_width_is_accepted_and_stored_as_zero() { + for (label, width) in [("minus-four", -4.0f64), ("minus-half", -0.5)] { + let mut doc = blank(1); + add_annotation(&mut doc, &styled(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, "w", [1.0, 0.0, 0.0], 1.0, width, None), &meta("neg")) + .unwrap_or_else(|e| panic!("{label}: negative width {width} should be accepted today, got {e:?}")); + for path in ALL_SAVES { + let (file, reopened) = save_and_reopen(&doc, path); + let d = annot_at(&reopened, 0, 0, path); + let w_stored = d.get(b"BS").map(|o| reopened.resolve(o)).and_then(|o| o.as_dict().cloned()).and_then(|bs| num(&reopened, &bs, b"W")); + assert_eq!(w_stored, Some(0.0), "{label}: {path:?} stores the clamped zero, not {width}"); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.width), Some(0.0), "{label}: {path:?} reports the clamped zero"); + assert!(!file.windows(4).any(|x| x == b"-4.0" || x == b"-0.5"), "{label}: {path:?} must not write the negative literal it was given"); + assert!(doc.repair_log().is_empty() && reopened.repair_log().is_empty(), "{label}: {path:?} records nothing about the substitution"); + } + } + + let mut doc = blank(1); + add_annotation(&mut doc, &styled(0, Shape::Oval { rect: [5.0, 5.0, 65.0, 35.0] }, "w", [1.0, 0.0, 0.0], 1.0, 2.0, None), &meta("neg2")) + .expect("adds"); + set_style(&mut doc, 0, 0, StyleChange::new(None, None, Some(-2.5), None), &meta("neg2")) + .expect("the edit path screens non-finite and magnitude, not sign"); + let (file, reopened) = save_and_reopen(&doc, Save::FullTable); + assert_eq!(props(&reopened, 0, 0).and_then(|p| p.width), Some(0.0), "an edited negative width is stored as zero too"); + assert!(!file.windows(4).any(|x| x == b"-2.5"), "the negative literal never reaches the file"); + assert!(doc.repair_log().is_empty() && reopened.repair_log().is_empty(), "silently, with nothing recorded"); +} + +/// The `/Rect` half of the same guard (`create.rs:62-82` and `:47`). Measured on this branch: `Rectangle` and +/// `Oval` store the given `/Rect` verbatim, so they cannot detect this guard at all — a first +/// version of this test compared rectangles and survived the mutation, which is exactly how an +/// empty test looks green. `Shape::Line` IS padded by it (`create.rs:87`, `pad = half + arrow`), so the +/// comparison is made there: with `-9pt` and `0pt` borders a line's `/Rect` must grow identically, +/// because `max(0.0)` collapses both before the padding is applied. +#[test] +fn a_negative_and_a_zero_border_pad_a_line_identically() { + let line = |w: f64| { + let mut doc = blank(1); + add_annotation( + &mut doc, + &styled( + 0, + Shape::Line { from: [20.0, 700.0], to: [120.0, 740.0], start: LineEnding::None, end: LineEnding::None }, + "l", + [0.0, 0.0, 0.0], + 1.0, + w, + None, + ), + &meta("ln"), + ) + .expect("adds a line"); + save_and_reopen(&doc, Save::FullTable).0 + }; + let minus = line(-9.0); + let zero = line(0.0); + let rect_minus = bytes::numbers_of(&minus, "Rect"); + let rect_zero = bytes::numbers_of(&zero, "Rect"); + assert!(!rect_minus.is_empty(), "the -9pt file must contain a /Rect array of numbers"); + assert!(!rect_zero.is_empty(), "the 0pt file must contain a /Rect array of numbers"); + assert_eq!(rect_minus, rect_zero, "-9pt and 0pt borders must pad a line's /Rect identically — max(0.0) at create.rs:47 is what guarantees it"); + // `/BS /W` is a SCALAR real, never an array, so `numbers_of` (which only accepts `[...]`) + // compared two empty lists here and passed whatever the file contained. `scalars_of` is the + // right helper, and both sides are guarded non-empty so this can never go green on missing data. + let w_minus = bytes::scalars_of(&minus, "W"); + let w_zero = bytes::scalars_of(&zero, "W"); + assert!(!w_minus.is_empty() && !w_zero.is_empty(), "both files must actually write a /BS /W, got {w_minus:?} vs {w_zero:?}"); + assert_eq!(w_minus, w_zero, "and both store the same /BS /W"); + assert_eq!(w_minus.as_slice(), &[0.0], "the clamp at create.rs:47 stores a negative border width as 0"); +} diff --git a/crates/annot/tests/support/bytes.rs b/crates/annot/tests/support/bytes.rs new file mode 100644 index 0000000000..b06f92f6da --- /dev/null +++ b/crates/annot/tests/support/bytes.rs @@ -0,0 +1,336 @@ +//! Shared helpers compile into every integration-test binary of this crate, and not every +//! binary uses every one of them; `dead_code` would then fail `clippy -D warnings` for reasons +//! that have nothing to do with the test being written. +#![allow(dead_code)] + +//! A deliberately strict reader over the *bytes* a save produced, independent of `pdfcraft-cos`. +//! +//! Reopening saved bytes with `Document::open` and comparing dictionaries proves that our writer +//! and our reader agree. It does not prove what is actually *in the file*: a lenient parser can +//! forgive output that another implementation refuses (an exponent in what should be a fixed +//! decimal number, an unterminated string, a key written twice). These helpers look at the bytes +//! themselves and reject anything outside the narrow grammar ISO 32000-2 §7.3 allows for the +//! object types annotations use, so an assertion here is a claim about the file rather than about +//! a round trip through our own pair of implementations. +//! +//! Scope: dictionary keys whose values are number arrays (`/Rect`, `/QuadPoints`, `/L`, +//! `/Vertices`, `/CL`, `/RD`), number arrays of arrays (`/InkList`), text strings (`/Contents`, +//! `/T`, `/NM`, `/M`) and names (`/Subtype`). That covers the entries these tests care about; it +//! is not a general PDF parser and does not try to be. + +/// Every value token written for `/key` in the raw bytes, in file order. +/// +/// A name-keyed dictionary entry is `/key` preceded and followed by delimiters, so searching for +/// `Rect` does not match inside `/MyRectangle`. The returned spans include the enclosing brackets +/// or string delimiters, which the decoders below strip. +pub fn value_tokens(bytes: &[u8], key: &[u8]) -> Vec> { + let mut out = Vec::new(); + let mut i = 0; + while i + key.len() + 1 < bytes.len() { + if bytes[i] != b'/' || &bytes[i + 1..i + 1 + key.len()] != key { + i += 1; + continue; + } + let after = i + 1 + key.len(); + // The key must end here: a longer name is a different key. + if after < bytes.len() && is_name_byte(bytes[after]) { + i += 1; + continue; + } + let Some((end, token)) = capture(bytes, after) else { + i += 1; + continue; + }; + // A `/Key /Other` hit is ambiguous: it may be a key whose value is a name, or the *name* + // that is some earlier key's value (`/S /URI`). Re-scan from the next byte so a later real + // occurrence of `/Key` is still found; an array, string or number value is unambiguous. + let name_valued = token.starts_with(b"/"); + out.push(token); + i = if name_valued { i + 1 } else { end }; + } + out +} + +fn is_name_byte(b: u8) -> bool { + b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_' | b'#' | b'%') +} + +/// Capture the object that starts at `at`, with bracket/paren balance respected. A literal +/// string's brackets do not count; an array containing a string does nest. +fn capture(bytes: &[u8], mut at: usize) -> Option<(usize, Vec)> { + while bytes.get(at).is_some_and(|b| b.is_ascii_whitespace()) { + at += 1; + } + let first = *bytes.get(at)?; + match first { + b'[' => balanced(bytes, at, b'[', b']'), + b'(' => literal_string(bytes, at), + b'<' if bytes.get(at + 1) == Some(&b'<') => balanced(bytes, at, b'<', b'>'), + // A hex string: `<...>`, closed by the first `>` that is not part of a `>>`. + b'<' => { + let close = bytes.get(at + 1..)?.iter().position(|&b| b == b'>')?; + Some((at + close + 2, bytes[at..at + close + 2].to_vec())) + } + b'/' => { + let mut j = at + 1; + while bytes.get(j).is_some_and(|b| !is_delimiter(*b)) { + j += 1; + } + Some((j, bytes[at..j].to_vec())) + } + _ => { + let mut j = at; + while bytes.get(j).is_some_and(|b| !is_delimiter(*b)) { + j += 1; + } + Some((j, bytes[at..j].to_vec())) + } + } +} + +fn is_delimiter(b: u8) -> bool { + b.is_ascii_whitespace() || matches!(b, b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%') +} + +/// `open` at `at`, closed by the matching `close`, skipping over literal strings so that a +/// delimiter inside `(…)` does not end the span. +fn balanced(bytes: &[u8], at: usize, open: u8, close: u8) -> Option<(usize, Vec)> { + let mut depth = 0usize; + let mut i = at; + while i < bytes.len() { + match bytes[i] { + b'(' => { + let (end, _) = literal_string(bytes, i)?; + i = end; + continue; + } + b if b == open => depth += 1, + b if b == close => { + depth -= 1; + if depth == 0 { + return Some((i + 1, bytes[at..=i].to_vec())); + } + } + _ => {} + } + i += 1; + } + None +} + +fn literal_string(bytes: &[u8], at: usize) -> Option<(usize, Vec)> { + let mut i = at + 1; + while i < bytes.len() { + match bytes[i] { + b'\\' => i += 2, + b')' => return Some((i + 1, bytes[at..=i].to_vec())), + _ => i += 1, + } + } + None +} + +/// The numbers of an array token, written in fixed decimal notation (§7.3.3: no exponent form). +/// `None` when the token is not an array, holds a non-number, or uses `e`. +pub fn numbers(token: &[u8]) -> Option> { + let inner = token.strip_prefix(b"[")?.strip_suffix(b"]")?.to_vec(); + let mut out = Vec::new(); + for word in split_words(&inner) { + if word.first() == Some(&b'[') { + // A nested array (`/InkList`): its numbers count as this one's, flattened. + out.extend(numbers(&word)?); + } else { + out.push(fixed_decimal(&word)?); + } + } + Some(out) +} + +/// A single number written for `/key` (`/CA`, `/F`, a stream's `/Length`), strictly decimal. +pub fn scalars_of(bytes: &[u8], key: &str) -> Vec { + value_tokens(bytes, key.as_bytes()).iter().filter_map(|t| fixed_decimal(t)).collect() +} + +/// All numbers of an ARRAY token written for `/key`, with the strict fixed-decimal rule. +/// +/// ONLY valid for keys whose value is an array (`/Rect`, `/QuadPoints`, `/L`, `/IC`, +/// `/PCPictureSize`). For a SCALAR key (`/W`, `/CA`, `/F`) every token fails `numbers` and the +/// result is an empty vector — so comparing two such calls against each other is vacuous: it +/// passes whether or not the file holds anything. Use [`scalars_of`] for scalars, and always +/// assert non-emptiness when a value is expected. A scalar misuse was found and fixed in +/// `round_trip.rs` (a `/BS /W` comparison that silently compared `[] == []`). +pub fn numbers_of(bytes: &[u8], key: &str) -> Vec> { + value_tokens(bytes, key.as_bytes()).iter().filter_map(|t| numbers(t)).collect() +} + +/// A number written as `[+-]?digits[.]?` or `[+-]?digits[.]digits`, with no exponent and no +/// stray characters. `str::parse::` alone would accept `1e7`, which PDF does not use. +fn fixed_decimal(word: &[u8]) -> Option { + if word.is_empty() || word.iter().any(|b| *b == b'e' || *b == b'E') { + return None; + } + let body = match word[0] { + b'+' | b'-' => &word[1..], + _ => word, + }; + if body.is_empty() { + return None; + } + let mut digits = 0; + let mut dots = 0; + for &b in body { + match b { + b'0'..=b'9' => digits += 1, + b'.' => dots += 1, + _ => return None, + } + } + if digits == 0 || dots > 1 { + return None; + } + std::str::from_utf8(word).ok()?.parse::().ok() +} + +/// The decoded bytes of a string token (literal with escapes, or hex), `None` if malformed. +pub fn raw_string(token: &[u8]) -> Option> { + match (token.first(), token.last()) { + (Some(b'('), Some(b')')) => { + let mut out = Vec::new(); + let body = &token[1..token.len() - 1]; + let mut i = 0; + while i < body.len() { + if body[i] != b'\\' { + out.push(body[i]); + i += 1; + continue; + } + let e = *body.get(i + 1)?; + match e { + b'n' => out.push(b'\n'), + b'r' => out.push(b'\r'), + b't' => out.push(b'\t'), + b'b' => out.push(0x08), + b'f' => out.push(0x0C), + b'(' | b')' | b'\\' => out.push(e), + b'0'..=b'7' => { + let mut v = 0u32; + let mut j = i + 1; + let mut n = 0; + while j < body.len() && n < 3 && (b'0'..=b'7').contains(&body[j]) { + v = v * 8 + u32::from(body[j] - b'0'); + j += 1; + n += 1; + } + out.push(u8::try_from(v).ok()?); + i = j; + continue; + } + b'\n' => {} + _ => return None, + } + i += 2; + } + Some(out) + } + (Some(b'<'), Some(b'>')) => { + let hex: Vec = token[1..token.len() - 1].iter().copied().filter(|b| !b.is_ascii_whitespace()).collect(); + if !hex.len().is_multiple_of(2) { + return None; + } + let mut out = Vec::with_capacity(hex.len() / 2); + for pair in hex.as_chunks::<2>().0 { + let s = std::str::from_utf8(pair).ok()?; + out.push(u8::from_str_radix(s, 16).ok()?); + } + Some(out) + } + _ => None, + } +} + +/// Text as the file holds it: UTF-16BE behind a BOM, PDF 2.0's UTF-8 BOM, or PDFDocEncoding — +/// approximated by Latin-1 above 0x7F. The two differ only at a handful of bytes between 0x18 +/// and 0x9F, none of which these tests assert on; ASCII and the UTF-16 cases are exact. +pub fn text_of_raw(bytes: &[u8]) -> String { + match bytes { + [0xFE, 0xFF, rest @ ..] => String::from_utf16_lossy(&rest.as_chunks::<2>().0.iter().map(|p| u16::from_be_bytes(*p)).collect::>()), + [0xEF, 0xBB, 0xBF, rest @ ..] => String::from_utf8_lossy(rest).into_owned(), + b => b.iter().map(|&c| char::from(c)).collect(), + } +} + +/// Every text value written for `/key`, decoded from the bytes. +pub fn texts_of(bytes: &[u8], key: &str) -> Vec { + value_tokens(bytes, key.as_bytes()).iter().filter_map(|t| raw_string(t)).map(|b| text_of_raw(&b)).collect() +} + +/// A name token's bytes (`/Foo` with `#hh` escapes decoded), or the bare name of a token. +pub fn name_of_token(token: &[u8]) -> Option> { + if *token.first()? != b'/' { + return None; + } + let body = &token[1..]; + let mut out = Vec::new(); + let mut i = 0; + while i < body.len() { + if body[i] == b'#' { + let s = std::str::from_utf8(body.get(i + 1..i + 3)?).ok()?; + out.push(u8::from_str_radix(s, 16).ok()?); + i += 3; + } else { + out.push(body[i]); + i += 1; + } + } + Some(out) +} + +/// Every name written for `/key`. +pub fn names_of(bytes: &[u8], key: &str) -> Vec { + value_tokens(bytes, key.as_bytes()).iter().filter_map(|t| name_of_token(t)).map(|n| String::from_utf8_lossy(&n).into_owned()).collect() +} + +/// Whether the file holds the name `/name` as its own token, so searching `Rect` cannot match +/// inside `/Rectangle`. It does not know about string objects, so a name-looking sequence inside +/// literal text would also match: use it only for names distinctive enough to be unambiguous. +pub fn has_name(bytes: &[u8], name: &str) -> bool { + let n = name.as_bytes(); + if n.is_empty() { + return false; + } + let mut i = 0; + while i + n.len() < bytes.len() { + if bytes[i] == b'/' && &bytes[i + 1..i + 1 + n.len()] == n { + let after = i + 1 + n.len(); + if after >= bytes.len() || !is_name_byte(bytes[after]) { + return true; + } + } + i += 1; + } + false +} + +/// Tokens split on whitespace, with `[` … `]` groups kept whole. +fn split_words(bytes: &[u8]) -> Vec> { + let mut out = Vec::new(); + let mut i = 0; + while i < bytes.len() { + if bytes[i].is_ascii_whitespace() { + i += 1; + continue; + } + if bytes[i] == b'[' { + let (end, tok) = balanced(bytes, i, b'[', b']').expect("an opened array closes in a token we captured"); + out.push(tok); + i = end; + continue; + } + let start = i; + while bytes.get(i).is_some_and(|b| !b.is_ascii_whitespace() && *b != b'[') { + i += 1; + } + out.push(bytes[start..i].to_vec()); + } + out +} diff --git a/crates/annot/tests/support/fixture.rs b/crates/annot/tests/support/fixture.rs new file mode 100644 index 0000000000..1b466c4a54 --- /dev/null +++ b/crates/annot/tests/support/fixture.rs @@ -0,0 +1,213 @@ +//! Shared helpers compile into every integration-test binary of this crate, and not every +//! binary uses every one of them; `dead_code` would then fail `clippy -D warnings` for reasons +//! that have nothing to do with the test being written. +#![allow(dead_code)] + +//! Hand-written PDF skeletons and the save/reopen helpers these tests share. +//! +//! The shape follows `crates/annot/src/tests.rs::fixture`: build the bytes of a small +//! cross-referenced PDF by hand, open it with `pdfcraft_cos::Document::open`, and let +//! `pdfcraft-annot` add comments through its public API. Doing it this way (rather than +//! constructing a `Document` in memory) matters for these tests: the file has a real +//! cross-reference chain, so `write_incremental` takes its genuine incremental path instead of +//! silently falling back to a full rewrite. +//! +//! Every save here goes through one of three writer paths, and the tests replay their assertions +//! over all three: +//! +//! * `Save::Incremental` — the original bytes plus an appended revision and `/Prev`; +//! * `Save::FullTable` — a full rewrite with a classic cross-reference table; +//! * `Save::FullStreams` — a full rewrite that packs objects into compressed object streams and +//! renumbers everything from 1. +//! +//! The third path is the interesting one for annotation *identity*: object numbers change, so a +//! `/P`, `/Popup` or `/IRT` that survives it has really been re-pointed, not copied. + +use std::sync::Arc; + +use pdfcraft_cos::{Dict, Document, Object, SaveOptions, write_full, write_incremental}; + +/// Which writer path a round trip should take. +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub enum Save { + Incremental, + FullTable, + FullStreams, +} + +/// Every path, in the order tests report them. +pub const ALL_SAVES: [Save; 3] = [Save::Incremental, Save::FullTable, Save::FullStreams]; + +/// The paths whose output holds plain (uncompressed) objects, so raw-byte assertions can look at +/// them. Object streams are Flate-compressed by `write_full`, so their bytes need decoding +/// first; those cases are checked through the reopened document instead. +pub const UNCOMPRESSED_SAVES: [Save; 2] = [Save::Incremental, Save::FullTable]; + +/// Objects written as indirect objects plus a classic cross-reference table and trailer. The +/// catalog is object 1, so `objs` starts with it. +pub fn pdf(objs: &[&[u8]]) -> Vec { + let mut out = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::with_capacity(objs.len()); + for (i, o) in objs.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes()); + out.extend_from_slice(o); + out.extend_from_slice(b"\nendobj\n"); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes()); + for o in offsets { + out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes()); + out +} + +/// A document of `pages` blank pages (612×792, no `/Annots` anywhere). +pub fn blank(pages: usize) -> Document { + let mut kids = String::new(); + for i in 0..pages { + kids.push_str(&format!("{} 0 R ", 3 + i)); + } + let mut objs: Vec> = + vec![b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), format!("<>").into_bytes()]; + for _ in 0..pages { + objs.push(b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>".to_vec()); + } + open(&pdf_refs(&objs)) +} + +/// Like [`blank`], but page `page` starts with an indirect (and initially empty) `/Annots` +/// array, so writes to it go through `set_annots`' shared-array branch. +pub fn blank_with_indirect_annots(pages: usize, page: usize) -> Document { + let mut objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), + format!("<< /Type /Pages /Kids [{}] /Count {pages} >>", (0..pages).map(|i| format!("{} 0 R", 3 + i)).collect::>().join(" ")) + .into_bytes(), + ]; + for i in 0..pages { + objs.push(if i == page { + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots {} 0 R >>", 3 + pages).into_bytes() + } else { + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>".to_vec() + }); + } + objs.push(b"[]".to_vec()); + open(&pdf_refs(&objs)) +} + +/// [`pdf`] over already-owned object bodies. +fn pdf_refs(objs: &[Vec]) -> Vec { + let borrowed: Vec<&[u8]> = objs.iter().map(Vec::as_slice).collect(); + pdf(&borrowed) +} + +fn open(bytes: &[u8]) -> Document { + Document::open(Arc::new(bytes.to_vec())).expect("the skeleton parses") +} + +/// Save one document, then reopen the bytes with our reader. A second, independent parser must +/// accept the same bytes first — that is what makes "it reloads" mean more than "our own writer +/// and reader agree with each other". +pub fn save_and_reopen(doc: &Document, path: Save) -> (Vec, Document) { + let bytes = save(doc, path); + hayro_syntax::Pdf::new(bytes.clone()).unwrap_or_else(|e| panic!("{path:?} output rejected by hayro-syntax: {e:?}")); + (bytes.clone(), Document::open(Arc::new(bytes)).unwrap_or_else(|e| panic!("{path:?} output rejected by our reader: {e:?}"))) +} + +/// Save with the path's options. `/Info /ModDate` is left unset so a full save adds nothing that +/// would make byte comparisons depend on the clock. +pub fn save(doc: &Document, path: Save) -> Vec { + let opts = SaveOptions { + mod_date: None, + id_seed: 0x5052_494E_5443_5241, + object_streams: path == Save::FullStreams, + drop_unlinked: false, + id_entropy: None, + }; + match path { + Save::Incremental => write_incremental(doc, &opts), + Save::FullTable | Save::FullStreams => write_full(doc, &opts), + } + .expect("saves") +} + +/// The leaf pages, in `/Kids` order. Local so these tests do not depend on `annot::page_refs` +/// about a page tree they are only incidentally exercising. +pub fn page_object_numbers(doc: &Document) -> Vec { + let root = doc.root().expect("a catalog"); + let pages = doc.get(root).as_dict().and_then(|d| d.reference(b"Pages")).expect("/Pages"); + let mut out = Vec::new(); + let mut seen = std::collections::HashSet::new(); + collect_pages(doc, pages, &mut seen, &mut out); + out +} + +fn collect_pages(doc: &Document, node: pdfcraft_cos::ObjRef, seen: &mut std::collections::HashSet, out: &mut Vec) { + // `seen` bounds the walk, so a cyclic page tree visits each node once and cannot recurse forever. + if !seen.insert(node) || seen.len() > 10_000 { + return; + } + let obj = doc.get(node); + let Some(d) = obj.as_dict() else { return }; + if d.name(b"Type") == Some(b"Page") { + out.push(node.num); + return; + } + let Some(kids) = d.get(b"Kids").map(|k| doc.resolve(k)).and_then(|k| k.as_array().cloned()) else { return }; + for k in kids { + // The array element itself is the reference; resolving it would give the page dictionary. + if let Object::Ref(r) = k { + collect_pages(doc, r, seen, out); + } + } +} + +/// The raw entries of page `page`'s `/Annots`, in array order (references or inline dictionaries). +pub fn annot_entries(doc: &Document, page: usize) -> Vec { + let num = *page_object_numbers(doc).get(page).expect("the page exists"); + let d = doc.get(pdfcraft_cos::ObjRef::new(num, 0)); + let Some(a) = d.as_dict().and_then(|d| d.get(b"Annots").map(|a| doc.resolve(a))) else { return Vec::new() }; + a.as_array().cloned().unwrap_or_default() +} + +/// The dictionary an `/Annots` entry names (a stream's dictionary counts as one). +pub fn dict_of(doc: &Document, entry: &Object) -> Option { + doc.resolve(entry).as_dict().cloned() +} + +/// A dictionary's number array with every element resolved, as PDF numbers. An entry that is not +/// a number (a name, a string, a dictionary) yields `None` here rather than 0.0, so tests can +/// tell "absent or wrong type" apart from "present and zero". +pub fn nums(doc: &Document, d: &Dict, key: &[u8]) -> Option> { + let a = doc.resolve(d.get(key)?).as_array()?.clone(); + a.iter().map(|x| doc.resolve(x).as_f64()).collect() +} + +/// A single number entry (`/CA`, `/BS /W`), resolving an indirect value. +pub fn num(doc: &Document, d: &Dict, key: &[u8]) -> Option { + doc.resolve(d.get(key)?).as_f64() +} + +/// A dictionary's text entry, decoded with our reader (`None` when absent or not a string). +pub fn str_of(doc: &Document, d: &Dict, key: &[u8]) -> Option { + doc.resolve(d.get(key)?).as_string().map(|s| s.to_text()) +} + +/// A dictionary's name entry as a `&str` where it is ASCII, for readable assertion messages. +pub fn name_of(doc: &Document, d: &Dict, key: &[u8]) -> Option { + let o = doc.resolve(d.get(key)?); + Some(String::from_utf8_lossy(o.as_name()?).into_owned()) +} + +/// Annotation flag bit masks, ISO 32000-2 §12.5.3 Table 165 (the spec numbers bits from 1). +/// `pdfcraft-annot` keeps these private, so they are restated here from the standard: this is +/// deliberately *not* read back from the crate, otherwise a wrong bit would be tested against +/// itself. +pub const FLAG_INVALID: i64 = 1 << 0; // bit 1 +pub const FLAG_HIDDEN: i64 = 1 << 1; // bit 2 +pub const FLAG_PRINT: i64 = 1 << 2; // bit 3 +pub const FLAG_NO_ZOOM: i64 = 1 << 3; // bit 4 +pub const FLAG_NO_ROTATE: i64 = 1 << 4; // bit 5 +pub const FLAG_NO_VIEW: i64 = 1 << 5; // bit 6 +pub const FLAG_READONLY: i64 = 1 << 7; // bit 8 (`/ReadOnly`, PdfCraft's Locked) diff --git a/crates/automation/src/a11y/form_tools.rs b/crates/automation/src/a11y/form_tools.rs index 7ff433af63..10cc9d2078 100644 --- a/crates/automation/src/a11y/form_tools.rs +++ b/crates/automation/src/a11y/form_tools.rs @@ -19,6 +19,11 @@ fn request_json(r: &pdfcraft_engine::js::Request) -> Value { R::OpenDoc(path) => json!({ "open_doc": path }), R::OpenAttachment { name, open } => json!({ "attachment": name, "open": open }), R::ShowPanel(p) => json!({ "panel": p.id() }), + // Applied by the engine with the script's field changes; listed only if one is left over. + R::SpawnTemplate { name, page, rename, overlay } => { + json!({ "spawn_template": name, "page": page.saturating_add(1), "rename": rename, "overlay": overlay }) + } + R::SetTemplateHidden { name, hidden } => json!({ "template": name, "hidden": hidden }), } } diff --git a/crates/automation/src/dispatch.rs b/crates/automation/src/dispatch.rs index f79f585a31..212664a37a 100644 --- a/crates/automation/src/dispatch.rs +++ b/crates/automation/src/dispatch.rs @@ -315,6 +315,22 @@ impl Automation { let at = self.opt_position(&a, "at")?; self.apply(&a, Edit::DuplicatePages { pages, at })? } + "page_templates" => { + let templates: Vec = self.doc(&a)?.templates().into_iter().map(|t| json!({ "name": t.name, "hidden": t.hidden })).collect(); + json!({ "templates": templates }) + } + "page_template_spawn" => { + let at = self.opt_position(&a, "at")?.unwrap_or(self.doc(&a)?.info.pages.len()); + let edit = Edit::SpawnTemplate { name: a.str("name")?.into(), at, rename: a.opt_bool("rename")?.unwrap_or(true) }; + self.apply(&a, edit)? + } + "page_template_set_hidden" => self.apply( + &a, + Edit::SetTemplateHidden { + name: a.str("name")?.into(), + hidden: a.opt_bool("hidden")?.ok_or_else(|| ToolError::InvalidArgs("hidden is required".into()))?, + }, + )?, "page_set_box" => self.page_set_box(&a)?, "page_transition_set" => self.page_transition_set(&a)?, "doc_create" => self.doc_create(&a)?, @@ -614,6 +630,7 @@ impl Automation { size: a.opt_num("size")?, underline: a.opt_bool("underline")?, line_spacing: a.opt_num("line_spacing")?, + paragraph_spacing: a.opt_num("paragraph_spacing")?, char_spacing: a.opt_num("char_spacing")?, scale: a.opt_num("scale")?, width, diff --git a/crates/automation/src/forms.rs b/crates/automation/src/forms.rs index f1f34836ad..035aa49d33 100644 --- a/crates/automation/src/forms.rs +++ b/crates/automation/src/forms.rs @@ -901,8 +901,12 @@ impl Automation { let bytes = crate::read_input(&path)?; let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); let before = (self.doc(a)?.info.annotations.len(), self.doc(a)?.form.iter().filter(|f| !f.value.is_empty()).count()); + let repairs_before = self.doc(a)?.repair_log().len(); let mut out = self.apply(a, Edit::ImportData { name, bytes: std::sync::Arc::new(bytes) })?; let doc = self.doc(a)?; + // What this import added to the repair log (values the form refused, comments taken + // without their appearance): the log is otherwise only in `doc_info`. + out["import_repairs"] = json!(doc.repair_log().into_iter().skip(repairs_before).collect::>()); out["comments"] = json!(doc.info.annotations.len() as i64 - before.0 as i64); out["filled_fields"] = json!(doc.form.iter().filter(|f| !f.value.is_empty()).count()); Ok(out) diff --git a/crates/automation/src/tools.rs b/crates/automation/src/tools.rs index e4301f2f23..d058c18309 100644 --- a/crates/automation/src/tools.rs +++ b/crates/automation/src/tools.rs @@ -681,7 +681,7 @@ pub fn tools() -> Vec { t("doc_export_data", "Export comments or form data", "Write comments and/or form data to path; the extension picks the format: .xfdf or .fdf (comments and/or fields), .xml, .csv or .txt (form data). For an XFA form, .xml is its data alone (rooted at the form's root data node, as Acrobat exports it) and .xdp an XML Data Package holding the whole datasets. what: all (default), comments, fields.") .destructive() .with(schema(json!({ "doc": doc(), "path": { "type": "string" }, "what": { "type": "string", "enum": ["all", "comments", "fields"] } }), &["doc", "path"])), - t("doc_import_data", "Import comments or form data", "Import comments and/or field values from an XFDF, FDF, XML, CSV or tab-delimited text file (detected from its content), or the comments of another PDF, each onto the page with the same number (comments on pages past this document's last page are left out). Comments with the same name are replaced; values go through the form's formats and validation. For an XFA form, an XDP, datasets or data XML file replaces the form's data: a laid-out form is laid out again from it (rows included) and its calculations run. Undoable.") + t("doc_import_data", "Import comments or form data", "Import comments and/or field values from an XFDF, FDF, XML, CSV or tab-delimited text file (detected from its content), or the comments of another PDF, each onto the page with the same number (comments on pages past this document's last page are left out). Comments with the same name are replaced; values go through the form's formats and validation. For an XFA form, an XDP, datasets or data XML file replaces the form's data: a laid-out form is laid out again from it (rows included) and its calculations run. Undoable. The result lists in `import_repairs` what this import left out (values the form refused, comments taken without their appearance); the import fails instead only when the form refuses every value and no comment is taken.") .with(schema(json!({ "doc": doc(), "path": { "type": "string" } }), &["doc", "path"])), t("form_delete_field", "Delete a field", "Delete a form field and all its widgets, or with widget only that one: one radio button of a group (widget n is the nth of the radio group's options in form_fields), or one copy of a duplicated field. A field's last widget takes the field with it. Undoable.") .destructive() @@ -1184,6 +1184,13 @@ pub fn tools() -> Vec { t("page_duplicate", "Duplicate pages", "Insert copies of pages so the first copy becomes page `at`, or after the last of them when `at` is left out (fonts and images are shared, not copied). Undoable.") .cmd("page.duplicate") .with(schema(json!({ "doc": doc(), "pages": pages("to duplicate"), "at": { "type": "integer", "minimum": 1 } }), &["doc", "pages"])), + t("page_templates", "List page templates", "The document's page templates (ISO 32000-2 §7.7.4: the name dictionary's /Templates, pages kept out of the document, then /Pages, named visible pages): each name, and whether it is hidden. A form's JavaScript adds pages from them (Template.spawn) or shows them (Template.hidden = false).") + .ro() + .with(schema(json!({ "doc": doc() }), &["doc"])), + t("page_template_spawn", "Add a page from a template", "Insert a copy of page template `name` so it becomes page `at` (default: after the last page), as JavaScript's Template.spawn does. With rename (the default, as in Acrobat) the copy's fields are its own, named P..; with rename false they are more widgets of the template's fields and share their values. Undoable.") + .with(schema(json!({ "doc": doc(), "name": { "type": "string" }, "at": { "type": "integer", "minimum": 1 }, "rename": { "type": "boolean" } }), &["doc", "name"])), + t("page_template_set_hidden", "Show or hide a page template", "Show page template `name` (hidden false: its own page goes at the end of the document, with its fields) or hide it again (hidden true: the page leaves the document and is kept as a template), as JavaScript's Template.hidden does. Undoable.") + .with(schema(json!({ "doc": doc(), "name": { "type": "string" }, "hidden": { "type": "boolean" } }), &["doc", "name", "hidden"])), t( "page_transition_set", "Set page transitions", @@ -1207,7 +1214,7 @@ pub fn tools() -> Vec { t( "page_set_box", "Set page boxes / crop", - "Set a page box (crop by default; also trim, bleed, art, media) on pages: either margins in points from the media box [left, bottom, right, top], or an absolute rect in points from the top-left of the displayed page. Omit both to reset the box to its default. Undoable.", + "Set a page box (crop by default; also trim, bleed, art, media) on pages: either margins from the media box [left, bottom, right, top] in default user-space units of the unrotated page (not points when the page has /UserUnit), or an absolute rect in points from the top-left of the displayed page. Omit both to reset the box to its default. Undoable.", ) .cmd("page.boxes") .with(schema( @@ -1755,6 +1762,7 @@ pub fn tools() -> Vec { "aligns": { "type": "array", "maxItems": 4096, "items": { "type": "string", "enum": ["left", "center", "right", "justify", "inherit"] }, "description": "Each paragraph's (line's) own alignment, by index; inherit (or legacy null) follows align." }, "underline": { "type": "boolean" }, "line_spacing": { "type": "number", "description": "Multiple of the font size (1.2 is ordinary)." }, + "paragraph_spacing": { "type": "number", "description": "Points between paragraphs (a line break in text starts one), beyond the line spacing; unset, half the size for a paragraph the page did not have (rows it had keep their space); 0 is none." }, "dx": { "type": "number", "description": "Paragraph only: move right by this many points (negative: left)." }, "dy": { "type": "number", "description": "Paragraph only: move up by this many points (negative: down)." }, "width": { "type": "number", "description": "Paragraph only: rewrap to this width in points (dragging the box's edge)." }, diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs index 86f60b1f7f..4674667382 100644 --- a/crates/automation/tests/it/automation.rs +++ b/crates/automation/tests/it/automation.rs @@ -7080,6 +7080,32 @@ fn tab_order_and_field_appearance_through_tools() { )); } +/// #7429: an import that took some values and had the form refuse another says which one in its own +/// response (a CLI or MCP caller need not call `doc_info`); one the form refused whole still fails; +/// one the form took whole carries an empty list. +#[test] +fn form_data_import_response_names_the_fields_the_form_refused() { + let dir = workdir("import-refused"); + let mut a = auto(&dir); + let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap(); + ok( + &mut a, + "form_add_field", + json!({ "doc": doc, "page": 1, "type": "combo", "rect": [20, 60, 180, 82], "name": "Choice", "options": ["A", "B"] }), + ); + ok(&mut a, "form_add_field", json!({ "doc": doc, "page": 1, "type": "text", "rect": [20, 20, 180, 42], "name": "Notes" })); + std::fs::write(dir.join("whole.xml"), "hello").unwrap(); + std::fs::write(dir.join("mixed.xml"), "INVALIDagain").unwrap(); + std::fs::write(dir.join("refused.xml"), "INVALID").unwrap(); + let r = ok(&mut a, "doc_import_data", json!({ "doc": doc, "path": "whole.xml" })); + assert_eq!(r["import_repairs"], json!([]), "{r}"); + let r = ok(&mut a, "doc_import_data", json!({ "doc": doc, "path": "mixed.xml" })); + let repairs = r["import_repairs"].as_array().unwrap(); + assert!(repairs.len() == 1 && repairs[0].as_str().unwrap().contains("\"Choice\"") && !repairs[0].as_str().unwrap().contains("Notes"), "{r}"); + let e = err(&mut a, "doc_import_data", json!({ "doc": doc, "path": "refused.xml" })); + assert!(e.to_string().contains("nothing was imported") && e.to_string().contains("\"Choice\""), "{e}"); +} + #[test] fn comments_and_form_data_travel_as_xfdf_fdf_and_text() { let dir = workdir("xfdf"); diff --git a/crates/automation/tests/it/main.rs b/crates/automation/tests/it/main.rs index 2e828f2643..c8caff969f 100644 --- a/crates/automation/tests/it/main.rs +++ b/crates/automation/tests/it/main.rs @@ -20,6 +20,7 @@ mod model_render; mod office; mod optimize_content_boundaries; mod page_actions; +mod page_templates; mod portfolio_info; mod redact_after_stray_q_form; mod redact_text_after_q; diff --git a/crates/automation/tests/it/page_templates.rs b/crates/automation/tests/it/page_templates.rs new file mode 100644 index 0000000000..f8206fbd32 --- /dev/null +++ b/crates/automation/tests/it/page_templates.rs @@ -0,0 +1,95 @@ +//! Page templates through the tools (#1429): list them, add a page from one, show or hide one. + +use std::path::PathBuf; + +use pdfcraft_automation::{Automation, Content, ToolError}; +use serde_json::{Value, json}; + +/// One visible page and a hidden template `More` holding the text field `Extra`. +fn form() -> Vec { + let bodies = [ + "<< /Type /Catalog /Pages 2 0 R /Names << /Templates 6 0 R >> /AcroForm << /Fields [7 0 R] /DA (/Helv 0 Tf 0 g) >> >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 200 300] >>", + "<< /Type /Page /Parent 2 0 R /Contents 4 0 R /Resources << /Font << /F1 9 0 R >> >> >>", + "<< /Length 40 >>\nstream\nBT /F1 12 Tf 20 20 Td (Visible) Tj ET\nendstream", + "<< /Type /Template /Contents 8 0 R /Resources << /Font << /F1 9 0 R >> >> /Annots [7 0 R] >>", + "<< /Names [(More) 5 0 R] >>", + "<< /FT /Tx /T (Extra) /Subtype /Widget /Rect [20 100 180 120] /P 5 0 R >>", + "<< /Length 41 >>\nstream\nBT /F1 12 Tf 20 20 Td (Template) Tj ET\nendstream", + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + ]; + let mut out = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::new(); + for (i, b) in bodies.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n{b}\nendobj\n", i + 1).as_bytes()); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes()); + for o in offsets { + out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", bodies.len() + 1).as_bytes()); + out +} + +fn setup(test: &str) -> (PathBuf, Automation) { + let dir = std::env::temp_dir().join(format!("pdfcraft-page-templates-{test}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("form.pdf"), form()).unwrap(); + let a = Automation::new().with_root(&dir).unwrap(); + (dir, a) +} + +fn ok(a: &mut Automation, tool: &str, args: Value) -> Value { + match a.call(tool, &args).unwrap_or_else(|e| panic!("{tool}: {e:?}")).remove(0) { + Content::Json(v) => v, + other => panic!("{tool}: expected JSON: {other:?}"), + } +} + +fn field_names(a: &mut Automation, doc: u64) -> Vec { + let fields = ok(a, "form_fields", json!({ "doc": doc })); + let mut names: Vec = fields["fields"].as_array().unwrap().iter().map(|f| f["name"].as_str().unwrap().to_string()).collect(); + names.sort(); + names +} + +#[test] +fn templates_are_listed_spawned_shown_and_hidden_through_tools() { + let (dir, mut a) = setup("tools"); + let doc = ok(&mut a, "doc_open", json!({ "path": "form.pdf" }))["doc"].as_u64().unwrap(); + assert_eq!(ok(&mut a, "page_templates", json!({ "doc": doc })), json!({ "templates": [{ "name": "More", "hidden": true }] })); + + // Without `at`, after the last page; renamed unless asked otherwise. + let spawned = ok(&mut a, "page_template_spawn", json!({ "doc": doc, "name": "More" })); + assert_eq!(spawned["pages"], 2, "{spawned}"); + assert_eq!(field_names(&mut a, doc), ["Extra", "P1.More.Extra"]); + ok(&mut a, "page_template_spawn", json!({ "doc": doc, "name": "More", "at": 1, "rename": false })); + assert_eq!(field_names(&mut a, doc), ["Extra", "P1.More.Extra"], "an unrenamed copy shares Extra"); + + let shown = ok(&mut a, "page_template_set_hidden", json!({ "doc": doc, "name": "More", "hidden": false })); + assert_eq!(shown["pages"], 4, "{shown}"); + assert_eq!(ok(&mut a, "page_templates", json!({ "doc": doc })), json!({ "templates": [{ "name": "More", "hidden": false }] })); + let hidden = ok(&mut a, "page_template_set_hidden", json!({ "doc": doc, "name": "More", "hidden": true })); + assert_eq!(hidden["pages"], 3, "{hidden}"); + + assert!(matches!(a.call("page_template_spawn", &json!({ "doc": doc, "name": "Nope" })), Err(ToolError::Failed(_)))); + assert!(matches!(a.call("page_template_spawn", &json!({ "doc": doc, "name": "More", "at": 0 })), Err(ToolError::InvalidArgs(_)))); + assert!(matches!(a.call("page_template_set_hidden", &json!({ "doc": doc, "name": "More" })), Err(ToolError::InvalidArgs(_)))); + let _ = std::fs::remove_dir_all(dir); +} + +/// `js_run` applies a script's spawn with its other changes, and reports nothing left over. +#[test] +fn a_script_run_through_the_tools_adds_the_page() { + let (dir, mut a) = setup("js"); + let doc = ok(&mut a, "doc_open", json!({ "path": "form.pdf" }))["doc"].as_u64().unwrap(); + let out = ok(&mut a, "js_run", json!({ "doc": doc, "script": "getTemplate('More').spawn(1, false, false);" })); + assert_eq!(out["error"], Value::Null, "{out}"); + assert_eq!(out["requests"], json!([]), "{out}"); + let info = ok(&mut a, "doc_info", json!({ "doc": doc })); + assert_eq!(info["pages"].as_array().map(Vec::len), Some(2), "{info}"); + let _ = std::fs::remove_dir_all(dir); +} diff --git a/crates/edit/src/added/draw.rs b/crates/edit/src/added/draw.rs index 8e0ec75791..917b386702 100644 --- a/crates/edit/src/added/draw.rs +++ b/crates/edit/src/added/draw.rs @@ -69,6 +69,7 @@ pub(super) fn draw_laid_shaped( ShapedScript::Arabic => arabic_error(e, &c.text), ShapedScript::Hebrew => hebrew_error(e, &c.text), ShapedScript::Thai => thai_error(e, &c.text), + ShapedScript::Latin => EditError::Invalid(pdfcraft_fonts::shaped_text::latin_message(e, &c.text)), })?; out.extend(format!("BT {}\n", t.fill_op()).bytes()); if t.char_spacing != 0.0 { diff --git a/crates/edit/src/added/layout.rs b/crates/edit/src/added/layout.rs index a6a55224d9..b4a805a20a 100644 --- a/crates/edit/src/added/layout.rs +++ b/crates/edit/src/added/layout.rs @@ -299,6 +299,10 @@ pub(super) fn shaped_layout(t: &AddedText, line: &str, rtl: bool) -> Result<(Vec ShapedScript::Arabic => shape_arabic(&s, run_rtl).map_err(|e| arabic_error(e, &s))?, ShapedScript::Hebrew => shape_hebrew(&s, run_rtl).map_err(|e| hebrew_error(e, &s))?, ShapedScript::Thai => shape_thai(&s).map_err(|e| thai_error(e, &s))?, + // Not a script this layout splits out; shaped as such all the same. + ShapedScript::Latin => { + pdfcraft_fonts::shape_latin(&s).map_err(|e| EditError::Invalid(pdfcraft_fonts::shaped_text::latin_message(e, &s)))? + } }; Piece::Shaped(shaped) } else if run_rtl { diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs index f02930a8e5..886bfc3522 100644 --- a/crates/edit/src/lib.rs +++ b/crates/edit/src/lib.rs @@ -99,4 +99,8 @@ mod find_replace_rows_tests; #[cfg(test)] mod marked_isolation_tests; #[cfg(test)] +mod paragraph_spacing_break_tests; +#[cfg(test)] +mod paragraph_spacing_tests; +#[cfg(test)] mod tests; diff --git a/crates/edit/src/paragraph_spacing_break_tests.rs b/crates/edit/src/paragraph_spacing_break_tests.rs new file mode 100644 index 0000000000..cc50030b6f --- /dev/null +++ b/crates/edit/src/paragraph_spacing_break_tests.rs @@ -0,0 +1,176 @@ +//! The paragraph spacing goes before a row whose line break the edit inserted, and the rows +//! below it keep their own source steps. + +use crate::paragraph_spacing_tests::{close, page, steps}; +use crate::text; + +/// Source rows `One`, `TwoThree` and `Four` of 10 pt text at baselines 700, 688 and 676: a +/// 12 pt row step and no paragraph gap. The draft breaks `TwoThree` into `Two` and `Three` and +/// sets a 10 pt spacing. `Three` starts the only new paragraph: it goes 12 + 10 below `Two`, +/// at 688 - 22 = 666, and `Four` keeps its 12 pt step below it, at 654. Steps 12, 22, 12. +/// Before, draft rows were paired with source rows by position: `Three` took `Four`'s row +/// (676, no gap there), and `Four`, past the source's rows, got the spacing: 12, 12, 22. +#[test] +fn spacing_goes_before_an_inserted_break() { + let mut doc = page("BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (TwoThree) Tj 0 -12 Td (Four) Tj ET"); + let style = text::BlockStyle { paragraph_spacing: Some(10.0), ..Default::default() }; + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree\nFour"), &style).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 22.0, 12.0]), "{s:?}"); +} + +/// A range style takes the styled path (`text_regions`), whose rows are placed by source row. +fn styled(spacing: f64) -> text::BlockStyle { + let range = + text::RangeStyle { range: 0..3, family: None, resource: None, size: None, color: None, underline: None, char_spacing: None, scale: None }; + text::BlockStyle { paragraph_spacing: Some(spacing), ranges: vec![range], ..Default::default() } +} + +/// The styled path: `TwoThree` is split into `Two` and `Three`. `Three` starts the new paragraph, +/// 12 + 10 below `Two`, and `Four` keeps its 12 pt step below it: 12, 22, 12. The source's own +/// breaks, with no extra space above them, get none. +#[test] +fn styled_rows_take_the_spacing_before_an_inserted_break() { + let source = "BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (TwoThree) Tj 0 -12 Td (Four) Tj ET"; + let mut doc = page(source); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree\nFour"), &styled(10.0)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 22.0, 12.0]), "{s:?}"); + let mut doc = page(source); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwoThree\nFour"), &styled(10.0)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 12.0]), "{s:?}"); +} + +/// The styled path: a source row set apart by a 4 pt gap (step 16) has that gap replaced by a +/// 10 pt spacing (step 22), and the row below keeps its step. +#[test] +fn styled_rows_replace_the_gap_a_row_already_had() { + let mut doc = page("BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (Two) Tj 0 -16 Td (Three) Tj ET"); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &styled(10.0)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 22.0]), "{s:?}"); +} + +fn rows(names: &[&str]) -> String { + let body: String = + names.iter().enumerate().map(|(i, n)| format!("{}({n}) Tj ", if i == 0 { "".to_string() } else { "0 -12 Td ".to_string() })).collect(); + format!("BT /F1 10 Tf 72 700 Td {body}ET") +} + +fn steps_after(source: &[&str], draft: &str) -> Vec { + let mut doc = page(&rows(source)); + text::rewrite_block(&mut doc, 0, 0, Some(draft), &styled(10.0)).unwrap(); + steps(&doc) +} + +/// The styled path: a whole row inserted exactly between two source rows takes the spacing, +/// not the row after it. Source `One`, `Two`, `Three`; the draft adds `New` after `Two`. +/// Aligned by character prefix and suffix the added line could not be told from the break +/// after `Two` and the spacing went before `Three`: 12, 12, 22. +#[test] +fn styled_spacing_goes_before_a_row_inserted_at_a_row_boundary() { + let s = steps_after(&["One", "Two", "Three"], "One\nTwo\nNew\nThree"); + assert!(close(&s, &[12.0, 22.0, 12.0]), "{s:?}"); +} + +/// A row inserted after the last row, and one before the first. +#[test] +fn styled_spacing_for_a_row_inserted_at_the_end_and_at_the_start() { + let s = steps_after(&["One", "Two", "Three"], "One\nTwo\nThree\nNew"); + assert!(close(&s, &[12.0, 12.0, 22.0]), "{s:?}"); + let s = steps_after(&["One", "Two", "Three"], "New\nOne\nTwo\nThree"); + assert!(close(&s, &[22.0, 12.0, 12.0]), "{s:?}"); +} + +/// Rows that repeat: a third `B` among `A`, `B`, `B`, `C` is one more paragraph, set apart once, +/// and the rows below it keep their steps. +#[test] +fn styled_spacing_for_a_row_inserted_among_identical_rows() { + let s = steps_after(&["A", "B", "B", "C"], "A\nB\nB\nB\nC"); + assert!(close(&s, &[12.0, 12.0, 22.0, 12.0]), "{s:?}"); + // Only one paragraph is added, whichever of the identical rows is the new one. + let total: f64 = s.iter().sum(); + assert!((total - 58.0).abs() < 0.5, "{s:?}"); +} + +/// The default is half the size, for a paragraph the page did not have: a 10 pt row broken in two +/// is a 12 pt row step and 5 pt apart, 17. Rows the page already had stay one 12 pt step apart +/// whatever the draft does around them (below). +#[test] +fn a_new_styled_paragraph_gets_half_the_size_when_no_spacing_is_set() { + let range = + text::RangeStyle { range: 0..3, family: None, resource: None, size: None, color: None, underline: None, char_spacing: None, scale: None }; + let style = text::BlockStyle { ranges: vec![range], ..Default::default() }; + let mut doc = page(&rows(&["One", "Three"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &style).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[17.0, 12.0]), "{s:?}"); + // Asking for none takes it away again. + let none = text::BlockStyle { paragraph_spacing: Some(0.0), ..style }; + let mut doc = page(&rows(&["One", "Three"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &none).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 12.0]), "{s:?}"); +} + +/// Rows the page had keep their steps when no spacing is set: two paragraphs of 10 pt text stay +/// one 12 pt row step apart. +#[test] +fn styled_source_rows_keep_their_steps_when_no_spacing_is_set() { + let range = + text::RangeStyle { range: 0..3, family: None, resource: None, size: None, color: None, underline: None, char_spacing: None, scale: None }; + let style = text::BlockStyle { ranges: vec![range], ..Default::default() }; + let mut doc = page(&rows(&["One", "Two"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo"), &style).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0]), "{s:?}"); +} + +/// Choosing a family takes the single-style path, which lays every row out afresh. +fn single(spacing: Option) -> text::BlockStyle { + text::BlockStyle { family: Some((crate::Family::Times, false, false)), paragraph_spacing: spacing, ..Default::default() } +} + +/// The single-style path, like the styled one: unset, a paragraph the draft's break adds gets half +/// the size (a 12 pt step and 5 pt apart, 17), and asking for none takes it away again. +#[test] +fn a_new_single_style_paragraph_gets_half_the_size_when_no_spacing_is_set() { + let mut doc = page(&rows(&["One", "Three"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &single(None)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[17.0, 12.0]), "{s:?}"); + let mut doc = page(&rows(&["One", "Three"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &single(Some(0.0))).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 12.0]), "{s:?}"); +} + +/// Rows the page had keep their steps when no spacing is set, whichever way the draft breaks +/// around them, and a gap a source row already had is kept, not added to. +#[test] +fn single_style_source_rows_keep_their_steps_when_no_spacing_is_set() { + let mut doc = page(&rows(&["One", "Two"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo"), &single(None)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0]), "{s:?}"); + // `Three` sits 16 pt below `Two`: a 4 pt gap, kept. + let mut doc = page("BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (Two) Tj 0 -16 Td (Three) Tj ET"); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &single(None)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 16.0]), "{s:?}"); + // A row added at the end is the only one set apart. + let mut doc = page(&rows(&["One", "Two"])); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nNew"), &single(None)).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 17.0]), "{s:?}"); +} + +/// A set spacing replaces the gap a single-style row already had (never adds to it). +#[test] +fn single_style_spacing_that_is_set_replaces_the_gap_a_row_had() { + let mut doc = page("BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (Two) Tj 0 -16 Td (Three) Tj ET"); + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &single(Some(10.0))).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[22.0, 22.0]), "{s:?}"); +} diff --git a/crates/edit/src/paragraph_spacing_tests.rs b/crates/edit/src/paragraph_spacing_tests.rs new file mode 100644 index 0000000000..a59d7dbde4 --- /dev/null +++ b/crates/edit/src/paragraph_spacing_tests.rs @@ -0,0 +1,88 @@ +//! The paragraph spacing: no default space between source rows, and a set spacing that +//! replaces the space a paragraph already had instead of adding to it. + +use std::sync::Arc; + +use pdfcraft_cos::{Document, SaveOptions, write_incremental}; + +use super::*; + +/// One page drawing `content` with Helvetica as /F1. +pub(crate) fn page(content: &str) -> Document { + let objs: Vec = vec![ + "<< /Type /Catalog /Pages 2 0 R >>".into(), + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".into(), + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 600 800] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>".into(), + format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".into(), + ]; + let mut out = b"%PDF-1.7\n".to_vec(); + let mut offs = Vec::new(); + for (i, o) in objs.iter().enumerate() { + offs.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes()); + } + let x = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes()); + for o in offs { + out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{x}\n%%EOF\n", objs.len() + 1).as_bytes()); + Document::open(Arc::new(out)).unwrap() +} + +fn reopen(doc: &Document) -> Document { + let bytes = write_incremental(doc, &SaveOptions::default()).unwrap(); + Document::open(Arc::new(bytes)).unwrap() +} + +/// The steps from each row down to the next, in points. +pub(crate) fn steps(doc: &Document) -> Vec { + let b: Vec = text::text_lines(&reopen(doc), 0).unwrap().iter().map(|l| l.rect[1]).collect(); + b.windows(2).map(|w| w[0] - w[1]).collect() +} + +pub(crate) fn close(got: &[f64], want: &[f64]) -> bool { + got.len() == want.len() && got.iter().zip(want).all(|(g, w)| (g - w).abs() < 0.5) +} + +/// Choosing a family takes the single-style path. Three rows of 10 pt text 12 pt apart (the +/// default 1.2 line step) stay 12 pt apart: no spacing is set, so none is added. Half the size +/// before every row, as before, made the steps 17 and 17. +#[test] +fn choosing_a_family_keeps_row_steps() { + let mut doc = page("BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (Two) Tj 0 -12 Td (Three) Tj ET"); + let style = text::BlockStyle { family: Some((Family::Times, false, false)), ..Default::default() }; + text::rewrite_block(&mut doc, 0, 0, Some("One\nTwo\nThree"), &style).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 12.0]), "{s:?}"); +} + +/// Rows of 10 pt text at baselines 700, 688, 672 and 660: a 12 pt row step, and a 4 pt gap +/// before the third row (step 16, still one paragraph: a gap of 1.5 rows is the next one). A +/// 10 pt spacing replaces that gap: the third row moves +/// down 6 pt to 666 and the fourth with it to 654, so the steps are 12, 22, 12. Applied again +/// to the result the gap is already 10, so nothing moves. Before, the spacing was added on top +/// of the gap and before every row: 22, 30, 22 the first time, and growing each time after. +/// A 0 pt spacing takes the gap away: 12, 12, 12. +#[test] +fn reapplying_paragraph_spacing_replaces_the_gap() { + let source = "BT /F1 10 Tf 72 700 Td (One) Tj 0 -12 Td (Two) Tj 0 -16 Td (Three) Tj 0 -12 Td (Four) Tj ET"; + let draft = Some("One\nTwo\nThree\nFour"); + let ten = text::BlockStyle { paragraph_spacing: Some(10.0), ..Default::default() }; + let mut doc = page(source); + text::rewrite_block(&mut doc, 0, 0, draft, &ten).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 22.0, 12.0]), "first time: {s:?}"); + // The 22 pt step is wider than a row step and a half, so the reopened page reads the third + // row as the next paragraph: the spacing applied again to the first one changes nothing. + let mut doc = reopen(&doc); + let first = text::text_blocks(&doc, 0).unwrap()[0].editor_text.clone(); + text::rewrite_block(&mut doc, 0, 0, Some(&first), &ten).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 22.0, 12.0]), "again: {s:?}"); + let mut doc = page(source); + text::rewrite_block(&mut doc, 0, 0, draft, &text::BlockStyle { paragraph_spacing: Some(0.0), ..Default::default() }).unwrap(); + let s = steps(&doc); + assert!(close(&s, &[12.0, 12.0, 12.0]), "none: {s:?}"); +} diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs index 7a59a238a1..dd6ba09c11 100644 --- a/crates/edit/src/tests.rs +++ b/crates/edit/src/tests.rs @@ -3826,7 +3826,8 @@ fn growing_into_a_blank_line_keeps_the_neighbour_after_save() { let bases: Vec = lines.iter().map(|l| l.origin_baseline()).collect(); assert_eq!(bases.len(), 4, "{bases:?}"); let grown = bases[2] - bases[3]; - assert!((grown - tl).abs() < 0.05, "the new row is {grown} pt above the neighbour, not one leading"); + // The new paragraph takes half the size (6 pt) of the blank line's room: no spacing is set. + assert!((grown - (tl - 6.0)).abs() < 0.05, "the new row is {grown} pt above the neighbour, not one leading less half the size"); assert!(grown < tl * 1.5, "this gap is outside the join window, so the test would pass without the marker"); let texts = |doc: &Document| text::text_blocks(doc, 0).unwrap().into_iter().map(|b| b.text).collect::>(); assert_eq!(texts(&doc), ["One Two Three", "Neighbour"]); @@ -4197,7 +4198,8 @@ fn resizing_a_grown_paragraph_keeps_the_neighbour() { let lines = text::text_lines(&doc, 0).unwrap(); let added = lines.iter().find(|l| l.text.contains("Alpha next row.")).unwrap(); let neighbour = lines.iter().find(|l| l.text.contains("Neighbor")).unwrap(); - assert!((added.origin_baseline() - neighbour.origin_baseline()).abs() < 0.5, "the new row is not on the neighbour"); + // The two rows the edit added are new paragraphs, half the size (6 pt) each below the blank line. + assert!((neighbour.origin_baseline() - added.origin_baseline() - 12.0).abs() < 0.5, "the new row is not 12 pt below the neighbour"); let width = text::text_blocks(&doc, 0).unwrap()[0].rect[2] - text::text_blocks(&doc, 0).unwrap()[0].rect[0]; for factor in [0.6_f64, 1.6] { let mut copy = doc.clone(); @@ -9909,6 +9911,41 @@ fn removing_marks_strips_wrappers_that_closed_open_state() { assert_eq!(streams(&doc, 0), ["q 2 0 0 2 0 0 cm"]); } +/// A line break in the edited text keeps a row of its own (the editor's rows); with a paragraph +/// spacing set, that much more space goes before it, and unset, half the size does. A row the +/// page already had stays where it was, shifted by the spacing added above it. +#[test] +fn paragraph_breaks_get_their_own_spacing() { + let baselines = |doc: &Document| -> Vec { text::text_lines(&reopen(doc), 0).unwrap().iter().map(|l| l.rect[1]).collect() }; + let gap_after_first = |b: &[f64]| b[0] - b[1]; + // A one-line source has no row step of its own: 1.5 × the size stands in. + let mut doc = text_page("BT /F1 10 Tf 72 700 Td (The quick brown fox) Tj ET"); + text::rewrite_block(&mut doc, 0, 0, Some("First one\nSecond one"), &text::BlockStyle::default()).unwrap(); + let b = baselines(&doc); + assert_eq!(b.len(), 2, "{b:?}"); + assert!((gap_after_first(&b) - 20.0).abs() < 0.5, "the row step and half the size: {b:?}"); + let mut doc = text_page("BT /F1 10 Tf 72 700 Td (The quick brown fox) Tj ET"); + text::rewrite_block(&mut doc, 0, 0, Some("First one\nSecond one"), &text::BlockStyle { paragraph_spacing: Some(20.0), ..Default::default() }) + .unwrap(); + assert!((gap_after_first(&baselines(&doc)) - 35.0).abs() < 0.5, "{:?}", baselines(&doc)); + let mut doc = text_page("BT /F1 10 Tf 72 700 Td (The quick brown fox) Tj ET"); + text::rewrite_block(&mut doc, 0, 0, Some("First one\nSecond one"), &text::BlockStyle { paragraph_spacing: Some(0.0), ..Default::default() }) + .unwrap(); + assert!((gap_after_first(&baselines(&doc)) - 15.0).abs() < 0.5); + // Without a break, nothing changes: one paragraph, the ordinary line step. + let mut doc = text_page("BT /F1 10 Tf 72 700 Td (The quick brown fox) Tj ET"); + text::rewrite_block( + &mut doc, + 0, + 0, + Some("First one Second one and then some more words to wrap"), + &text::BlockStyle { width: Some(90.0), ..Default::default() }, + ) + .unwrap(); + let b = baselines(&doc); + assert!(b.len() >= 2 && (gap_after_first(&b) - 12.0).abs() < 0.5, "{b:?}"); +} + /// Printer marks: drawn outside the trim box as a removable page mark; the page /// is widened to make room when asked and the trim box stays; running again replaces them. #[test] @@ -12632,6 +12669,33 @@ fn q_inside_a_text_object_restores_the_text_matrices() { } } +/// A supported letter plus selectors must not become an empty fallback request. The implicit +/// fallback and the explicitly chosen family agree on layout, including character spacing, +/// for selectors in the supported U+FE00..U+FE0F range. +#[test] +fn variation_source_letters_and_selectors_survive_paragraph_save() { + for font in ["F1", "F2"] { + for replacement in ["a\u{FE0F}", "ab\u{FE00}", "a\u{FE0F}b\u{FE00}"] { + let mut doc = text_page(&format!("BT /{font} 12 Tf 72 700 Td (ab) Tj ET")); + let fonts = page_fonts(&doc, &pdfcraft_model::pages(&doc)[0]); + let original_font = doc.resolve(fonts.get(font.as_bytes()).unwrap()).as_dict().unwrap().clone(); + let mut explicit = doc.clone(); + let style = text::BlockStyle { width: Some(100.0), align: Some(added::Align::Center), char_spacing: Some(2.0), ..Default::default() }; + text::rewrite_block(&mut doc, 0, 0, Some(replacement), &style).unwrap(); + let explicit_style = text::BlockStyle { family: Some((added::Family::Helvetica, false, false)), ..style }; + text::rewrite_block(&mut explicit, 0, 0, Some(replacement), &explicit_style).unwrap(); + let doc = reopen(&doc); + let lines = text::text_lines(&doc, 0).unwrap(); + let expected = text::text_lines(&reopen(&explicit), 0).unwrap(); + assert_eq!(lines.len(), 1, "{font}: {replacement:?}"); + assert_eq!(lines[0].text, replacement, "{font}"); + assert!(close(lines[0].rect, expected[0].rect), "{font}: {:?} != {:?}", lines[0].rect, expected[0].rect); + let fonts = page_fonts(&doc, &pdfcraft_model::pages(&doc)[0]); + assert_eq!(doc.resolve(fonts.get(font.as_bytes()).unwrap()).as_dict(), Some(&original_font), "the source font is unchanged"); + } + } +} + #[test] fn variation_public_line_and_block_edits_preserve_selectors_after_reopen() { let replacement = "1\u{FE0F}"; diff --git a/crates/edit/src/text.rs b/crates/edit/src/text.rs index f55cea4866..7f8013c00c 100644 --- a/crates/edit/src/text.rs +++ b/crates/edit/src/text.rs @@ -26,7 +26,7 @@ mod regions; #[path = "text_session.rs"] mod session; pub use fallback::strip_subset_tag; -use regions::{alignment_frame, group_blocks, is_centred, rewrite_changed_words, rewrite_styled_block}; +use regions::{alignment_frame, existing_gap, group_blocks, is_centred, rewrite_changed_words, rewrite_styled_block, row_sources, source_row_step}; pub use session::TextSession; mod blocks; diff --git a/crates/edit/src/text/blocks.rs b/crates/edit/src/text/blocks.rs index db4fa4f2db..9593b184c7 100644 --- a/crates/edit/src/text/blocks.rs +++ b/crates/edit/src/text/blocks.rs @@ -517,6 +517,14 @@ pub struct BlockStyle { pub underline: Option, /// Line spacing as a multiple of the font size (1.2 is ordinary). pub line_spacing: Option, + /// Space between paragraphs, in points, beyond the line spacing; `Some(0.0)` is none. Unset, a + /// paragraph the page did not have (a line break the edit inserted) gets half the size and a + /// row the page had keeps the space it had above it, on both paths. Where the text keeps the + /// page's rows in place (styled), a line break starts a paragraph only on a new row or on a + /// row the page had already set apart, and a set spacing replaces the space it had. Where the + /// text is laid out afresh (a chosen family or weight), a set spacing goes before every line + /// break, replacing what was there. + pub paragraph_spacing: Option, /// Character spacing (points) and horizontal scaling (percent). pub char_spacing: Option, pub scale: Option, diff --git a/crates/edit/src/text/fallback.rs b/crates/edit/src/text/fallback.rs index 1a3d918102..41ca730595 100644 --- a/crates/edit/src/text/fallback.rs +++ b/crates/edit/src/text/fallback.rs @@ -741,6 +741,7 @@ thread_local! { } #[cfg(test)] +/// Returns the number of source-character measurements on this thread and resets the counter. pub(crate) fn take_source_character_measurements() -> usize { SOURCE_CHARACTER_MEASUREMENTS.with(|count| count.replace(0)) } diff --git a/crates/edit/src/text/rewrite.rs b/crates/edit/src/text/rewrite.rs index 68699e4a4f..b9c7f21c8a 100644 --- a/crates/edit/src/text/rewrite.rs +++ b/crates/edit/src/text/rewrite.rs @@ -10,8 +10,8 @@ use super::lines::{ text_lines, }; use super::{ - Dict, Document, EditError, HashMap, HashSet, Matrix, Metrics, Object, Op, PdfString, TextLine, TextSession, alignment_frame, group_blocks, - is_centred, parse, rewrite_changed_words, rewrite_styled_block, + Dict, Document, EditError, HashMap, HashSet, Matrix, Metrics, Object, Op, PdfString, TextLine, TextSession, alignment_frame, existing_gap, + group_blocks, is_centred, parse, rewrite_changed_words, rewrite_styled_block, row_sources, source_row_step, }; /// What replacing a line did. @@ -377,7 +377,8 @@ fn choose_rewrite_path( /// The paragraph's text for writing back: explicit line breaks stay (#1443), and so does each typed /// U+0020 space: a run of them is not folded into one (#4199, #3797). Every [`row_break`] is a break -/// too; other white space is written as one space. +/// too; other white space is written as one space. A line break also starts a paragraph, with the +/// paragraph spacing before it. fn paragraph_text(text: &str) -> String { text.replace("\r\n", "\n") .chars() @@ -389,6 +390,68 @@ fn paragraph_text(text: &str) -> String { .collect() } +/// The wrapped lines, and for each the paragraph it starts (see [`wrap_paragraphs`]). +type Wrapped = (Vec<(String, bool, usize)>, Vec>); + +/// Every paragraph (text between line breaks) wrapped on its own, with, per line, the number of +/// the paragraph it starts when it is a paragraph's first line after the first paragraph. +fn wrap_paragraphs(text: &str, width: f64, advance: impl Fn(&str) -> f64) -> Wrapped { + let (mut wrapped, mut starts) = (Vec::new(), Vec::new()); + for (paragraph, text) in text.split('\n').enumerate() { + for (line, wrapped_line) in wrap(text, width, &advance).into_iter().enumerate() { + wrapped.push(wrapped_line); + starts.push((paragraph > 0 && line == 0).then_some(paragraph)); + } + } + (wrapped, starts) +} + +/// The gap before each wrapped line, in text space, beyond the line step: none except before the +/// first line of a paragraph after the first (`starts` says which paragraph that is). +/// +/// A spacing that is set (`Some(0.0)` is none) goes before every such line: every line is laid out +/// afresh, and it replaces whatever space the page had there. Unset, the paragraphs follow the same +/// rule as the styled path (`RowSpacing`): a paragraph the draft's line break started anew gets +/// half the size, and one the page already had keeps the space it had above it, so choosing a +/// family or applying the draft again moves no source row. Which paragraph is which comes from +/// aligning the draft to the source's rows ([`row_sources`]). +fn paragraph_gaps( + (b, lines): (&TextBlock, &[TextLine]), + text: &str, + starts: &[Option], + style: &BlockStyle, + (k, size): (f64, f64), +) -> Result, EditError> { + match style.paragraph_spacing { + Some(p) if p.is_finite() => { + let gap = (p / k).clamp(0.0, 14_400.0); + return Ok(starts.iter().map(|s| if s.is_some() { gap } else { 0.0 }).collect()); + } + Some(_) => return Err(EditError::Invalid("the paragraph spacing must be a number".into())), + None => {} + } + let mut at = 0; + let ranges: Vec> = text + .split('\n') + .map(|paragraph| { + let range = at..at + paragraph.chars().count(); + at = range.end + 1; + range + }) + .collect(); + let explicit: Vec = (0..ranges.len()).map(|i| i > 0).collect(); + let sources = row_sources(b, text, &ranges, &explicit); + let row_step = source_row_step(b, lines, size * k * 1.5); + Ok(starts + .iter() + .map(|s| match s.and_then(|paragraph| sources.get(paragraph)) { + Some(Some(source)) => existing_gap(b, lines, row_step, *source) / k, + Some(None) => size / 2.0, + None => 0.0, + }) + .collect()) +} + /// Where a paragraph sits on its page, for choosing the width it is laid out in. struct ParaGeometry { rect: [f64; 4], @@ -468,6 +531,7 @@ struct Advance<'a> { } impl Advance<'_> { + /// Measures the advance in user space, combining source and fallback glyph widths for mixed runs. fn of(&self, s: &str) -> f64 { // A variation selector draws nothing and adds no spacing: it takes no room. let extra = s.chars().filter(|ch| !is_variation_selector(*ch)).count() as f64 * self.char_spacing; @@ -683,9 +747,21 @@ struct LineWriter<'a> { lead: f64, } +/// Where a wrapped line stands in its paragraph. +#[derive(Clone, Copy)] +struct LineRow { + /// The last line of the text (not justified). + last: bool, + /// The gap before the line, beyond the line step: set only on a paragraph's first line after + /// the first (see [`paragraph_gaps`]). + gap: f64, +} + /// What carries over from one line to the next. struct LineCursor { x: f64, + /// How far the lines have moved down from the first, in text space. + y: f64, tw_set: f64, shown_font: String, } @@ -731,7 +807,7 @@ impl LineWriter<'_> { index: usize, line: &str, wrap: (usize, bool, usize), - last: bool, + pos: LineRow, cursor: &mut LineCursor, ops: &mut Vec, ) -> Result<(f64, f64, f64), EditError> { @@ -740,9 +816,11 @@ impl LineWriter<'_> { let line_align = self.align_of(row); let body = line.trim_matches(' '); let interior = body.matches(' ').count(); - let tw = self.stretch(body, last, line_align == Some(crate::added::Align::Justify)); + let tw = self.stretch(body, pos.last, line_align == Some(crate::added::Align::Justify)); let (shown, visible) = self.shown(line, body, tw); let dx = if tw > 0.0 { 0.0 } else { self.alignment.offset(visible, line_align) }; + // The line step, plus the gap before a paragraph's first line. + let step = if index > 0 { self.lead + pos.gap } else { 0.0 }; if index > 0 { // Inside this text object, so the marker starts and ends here. `/PCHard` is a newline // that was already in the text. `/PCSoft` rejoins when the box widens, with `/N` the @@ -759,9 +837,10 @@ impl LineWriter<'_> { ops.push(Op::new("EMC", vec![])); } if index > 0 || dx != 0.0 { - ops.push(Op::new("Td", vec![n(dx - cursor.x), n(if index > 0 { -self.lead } else { 0.0 })])); + ops.push(Op::new("Td", vec![n(dx - cursor.x), n(-step)])); } cursor.x = dx; + cursor.y -= step; // Word spacing reaches only single-byte code 32 (ISO 32000-1 §9.3.3), so where a space is // shown with another code (a composite font, a generated Type 3 font, a subset without a // space) the extra space follows each space as a TJ adjustment instead. @@ -790,9 +869,10 @@ impl LineWriter<'_> { let tw = tw + stretch; // Underlined to the line's last word, over its stretched gaps. let w = (visible + tw * interior as f64 * self.scale * self.k) / self.k; - Ok((dx, dx + w, -(index as f64) * self.lead - self.size * 0.12)) + Ok((dx, dx + w, cursor.y - self.size * 0.12)) } + /// Emits text-showing operators for each source or fallback run and tracks the selected font across pieces. fn show_piece(&self, piece: &str, line: &str, tw: f64, cursor: &mut LineCursor, ops: &mut Vec) -> Result<(), EditError> { let n = pdfcraft_content::num; for (source, run) in source_fallback_runs(piece, self.partial_chars) { @@ -825,7 +905,13 @@ impl LineWriter<'_> { } /// Writes every wrapped line, returning their underlines. - fn write_all(&self, wrapped: &[(String, bool, usize)], cursor: &mut LineCursor, ops: &mut Vec) -> Result, EditError> { + fn write_all( + &self, + wrapped: &[(String, bool, usize)], + gaps: &[f64], + cursor: &mut LineCursor, + ops: &mut Vec, + ) -> Result, EditError> { let mut row = 0usize; wrapped .iter() @@ -834,7 +920,11 @@ impl LineWriter<'_> { if i > 0 && !soft { row = row.saturating_add(1); } - self.write_line(i, line, (row, *soft, *gap), i + 1 == wrapped.len(), cursor, ops) + // Every line but the last is justified: a line break in the draft separates the rows + // of one paragraph, as the paragraph editor sends them, not paragraphs. + let last = i + 1 >= wrapped.len(); + let pos = LineRow { last, gap: gaps.get(i).copied().unwrap_or(0.0) }; + self.write_line(i, line, (row, *soft, *gap), pos, cursor, ops) }) .collect() } @@ -855,7 +945,13 @@ struct BlockPlan<'a> { underline: bool, } -fn block_ops(plan: &BlockPlan<'_>, writer: &LineWriter<'_>, wrapped: &[(String, bool, usize)], show_font: String) -> Result, EditError> { +fn block_ops( + plan: &BlockPlan<'_>, + writer: &LineWriter<'_>, + wrapped: &[(String, bool, usize)], + gaps: &[f64], + show_font: String, +) -> Result, EditError> { let n = pdfcraft_content::num; let mut ops = vec![Op::new("q", vec![])]; ops.extend(reveal_ops(plan.block, plan.lines, plan.style, plan.emit.ctm)?); @@ -871,14 +967,14 @@ fn block_ops(plan: &BlockPlan<'_>, writer: &LineWriter<'_>, wrapped: &[(String, let mut state = plan.state.clone(); state.word_spacing = 0.0; // The font the next run is shown in: the one the BT sets, until a run of another fallback subset. - let mut cursor = LineCursor { x: 0.0, tw_set: 0.0, shown_font: show_font.clone() }; + let mut cursor = LineCursor { x: 0.0, y: 0.0, tw_set: 0.0, shown_font: show_font.clone() }; state.font = Some((show_font, plan.size)); if let Some([r, g, bl]) = plan.style.color { state.fill = vec![Op::new("rg", vec![n(r), n(g), n(bl)])]; } ops.extend(state.ops()); ops.push(Op::new("Tm", plan.tm.iter().map(|v| n(*v)).collect())); - let underlines = writer.write_all(wrapped, &mut cursor, &mut ops)?; + let underlines = writer.write_all(wrapped, gaps, &mut cursor, &mut ops)?; ops.push(Op::new("ET", vec![])); if plan.underline { ops.extend(underline_ops(&state.fill, &plan.tm, plan.size, &underlines)); @@ -1015,7 +1111,7 @@ pub(crate) fn rewrite_block_from( // Preserve every drawable source glyph; cache missing-glyph lookups as well. let visual_indic = source.metrics.as_ref().is_some_and(|m| m.visual_order) || first.visual_order; let hebrew = flat.chars().any(crate::bidi::is_hebrew); - let source_advances: HashMap> = source + let mut source_advances: HashMap> = source .metrics .as_ref() .filter(|_| !reuse && upright && !hebrew && !visual_indic && style.family.is_none() && style.bold.is_none() && needs_type3(&flat)) @@ -1027,6 +1123,11 @@ pub(crate) fn rewrite_block_from( advances }) .unwrap_or_default(); + // A fallback face needs a visible glyph. If only selectors are missing, use the whole + // paragraph as before; clear the advances too so measuring and drawing use the same font. + if !source_advances.is_empty() && flat.chars().filter(|ch| source_advances.get(ch).copied().flatten().is_none()).all(is_variation_selector) { + source_advances.clear(); + } let source_chars: HashSet = source_advances.iter().filter_map(|(&ch, width)| width.map(|_| ch)).collect(); let partial_reuse = !source_chars.is_empty(); // The chosen style also determines the real Japanese fallback outlines and advances. @@ -1085,7 +1186,8 @@ pub(crate) fn rewrite_block_from( width }; let right_edge = if anchored_right { right_edge } else { width }; - let wrapped = wrap(&text, width + 0.5, |s| advance.of(s)); + // Every paragraph wrapped on its own; `starts` marks a paragraph's first line (but the first's). + let (wrapped, starts) = wrap_paragraphs(&text, width + 0.5, |s| advance.of(s)); let font = choose_font( &FontInputs { source: source_metrics, @@ -1101,6 +1203,7 @@ pub(crate) fn rewrite_block_from( &mut fonts_res, )?; let lead = line_lead(&b, &lines, style, &LeadScale { k, size, old_size, leading: o_state.leading }); + let gaps = paragraph_gaps((&b, &lines), &text, &starts, style, (k, size))?; let start = last.ops.first().copied().unwrap_or(emit.bt_op); let split = shows_before_paragraph(&ops, emit.bt_op, start, &dropped); // What the text after the paragraph expects, and what is in force where the paragraph is @@ -1149,7 +1252,7 @@ pub(crate) fn rewrite_block_from( size, underline: style.underline == Some(true), }; - let mut block_ops = block_ops(&plan, &writer, &wrapped, font.show_font.clone())?; + let mut block_ops = block_ops(&plan, &writer, &wrapped, &gaps, font.show_font.clone())?; let on_top = crate::lift_on_top(over, &mut block_ops); block_ops.extend(after.ops().into_iter().filter(|op| !op.is("Tf") || after.font.is_some())); if split { diff --git a/crates/edit/src/text_regions.rs b/crates/edit/src/text_regions.rs index ee808a8dec..c879cb1d92 100644 --- a/crates/edit/src/text_regions.rs +++ b/crates/edit/src/text_regions.rs @@ -17,4 +17,4 @@ mod styled; pub(super) use grouping::{alignment_frame, group_blocks, group_reading_blocks, is_centred}; pub(super) use ocr_words::rewrite_changed_words; -pub(super) use styled::rewrite_styled_block; +pub(super) use styled::{existing_gap, rewrite_styled_block, row_sources, source_row_step}; diff --git a/crates/edit/src/text_regions/styled.rs b/crates/edit/src/text_regions/styled.rs index a1beb291f9..63d32c4755 100644 --- a/crates/edit/src/text_regions/styled.rs +++ b/crates/edit/src/text_regions/styled.rs @@ -370,6 +370,8 @@ fn styled_run_end(items: &[StyledChar], start: usize) -> usize { type StyledRows = Vec>; type RowRanges = Vec>; +/// Which rows a line break in the draft started, as against wrapping. +type ExplicitRows = Vec; enum ShapedFont { Existing(Vec<(String, Vec)>), Program { bytes: Vec, family: String }, @@ -672,7 +674,10 @@ fn page_font_metrics( } fn paragraph_geometry(doc: &Document, page: &pdfcraft_model::Page, block: &TextBlock, style: &BlockStyle) -> Result<(f64, f64, f64), EditError> { - if [style.size, style.line_spacing, style.char_spacing, style.scale, style.width].iter().flatten().any(|value| !value.is_finite()) + if [style.size, style.line_spacing, style.paragraph_spacing, style.char_spacing, style.scale, style.width] + .iter() + .flatten() + .any(|value| !value.is_finite()) || style.color.is_some_and(|color| color.iter().any(|value| !value.is_finite())) { return Err(EditError::Invalid("the paragraph's formatting must use finite numbers".into())); @@ -699,7 +704,7 @@ fn row_step(style: &BlockStyle, size: f64, source_leading: f64) -> f64 { } } -type WrappedStyledRows = (StyledRows, RowRanges, Vec, Vec, Vec); +type WrappedStyledRows = (StyledRows, RowRanges, Vec, Vec, Vec, ExplicitRows); /// For each of `chars`, whether a row may end just before it inside a run of non-space characters /// (Chinese, Japanese and Thai), worked out once per run: Thai words need the whole run, not just @@ -743,6 +748,9 @@ fn wrap_styled_rows( let mut row_lines: Vec = Vec::new(); let mut draft_line = 0usize; let mut row_start = 0; + // Which rows a line break in the draft started, as against wrapping: the paragraph spacing, + // when one is set, goes before those. + let mut explicit: ExplicitRows = vec![false]; let mut row = Vec::new(); let mut row_width = 0.0; let mut at = 0; @@ -761,6 +769,7 @@ fn wrap_styled_rows( row_lines.push(draft_line); draft_line = draft_line.saturating_add(1); rows.push(std::mem::take(&mut row)); + explicit.push(true); row_width = 0.0; at += 1; row_start = at; @@ -788,6 +797,7 @@ fn wrap_styled_rows( row_ranges.push(row_start..row_start + row.len()); row_lines.push(draft_line); rows.push(std::mem::take(&mut row)); + explicit.push(false); row_width = 0.0; row_start = at; } @@ -802,7 +812,7 @@ fn wrap_styled_rows( row_lines.push(draft_line); rows.push(row); } - Ok((rows, row_ranges, row_lines, row_soft, row_gap)) + Ok((rows, row_ranges, row_lines, row_soft, row_gap, explicit)) } fn apply_range_styles<'a>( @@ -1282,23 +1292,104 @@ fn shape_source(doc: &Document, fonts: &Dict, thai: &ThaiShaping, name: &str, te ))) } +/// Where a paragraph spacing puts each row. The space before a paragraph, when a paragraph +/// spacing is set: the draft breaks every source row, so a break starts a paragraph only where +/// the edit inserted it, or at a source break whose row was already set apart from the row above +/// by more than the ordinary row step; there the set spacing replaces the space it had. A row +/// that starts a source row keeps that row's step from the row drawn above it, so the rows below +/// an inserted break or spacing move down with it instead of being drawn over it. +struct RowSpacing<'a> { + block: &'a TextBlock, + lines: &'a [TextLine], + first: &'a TextLine, + gap: f64, + /// Whether the spacing was asked for. Unset, the default gap goes only before a paragraph the + /// page did not have: a row it already had, whether set apart or not, keeps where it was, so + /// choosing a family or applying the draft again moves no source row. + set: bool, + row_step: f64, + /// The source row each output row starts, if any. + sources: Vec>, + explicit: ExplicitRows, + /// Rows keep their source baselines unless the size or the line spacing is changed. + follow_source: bool, + /// The baseline the row above was drawn on. + previous: Option, +} + +impl<'a> RowSpacing<'a> { + fn new( + block: &'a TextBlock, + lines: &'a [TextLine], + first: &'a TextLine, + style: &BlockStyle, + row_ranges: &[std::ops::Range], + draft: &str, + explicit: ExplicitRows, + ) -> Result { + let (gap, set) = match style.paragraph_spacing { + Some(p) if p.is_finite() => (p.clamp(0.0, 14_400.0), true), + Some(_) => return Err(EditError::Invalid("the paragraph spacing must be a number".into())), + // Half the size, for a paragraph the page did not have (see `set`). + None => (style.size.unwrap_or(first.size) / 2.0, false), + }; + let row_step = source_row_step(block, lines, first.size * 1.5); + let sources = row_sources(block, draft, row_ranges, &explicit); + Ok(Self { + block, + lines, + first, + gap, + set, + row_step, + sources, + explicit, + follow_source: style.line_spacing.is_none() && style.size.is_none(), + previous: None, + }) + } + + fn run_of(&self, row: usize) -> Option<&'a TextLine> { + self.block.rows.get(row).and_then(|r| r.first()).and_then(|i| self.lines.get(*i)) + } + + /// The baseline of output row `row_index`, given the one the row above ended on (`baseline`), + /// and the source run the row is styled from. + fn place(&mut self, row_index: usize, mut baseline: f64) -> (f64, &'a TextLine) { + let source_row = self.sources.get(row_index).copied().flatten(); + let anchor = source_row.and_then(|j| self.run_of(j)); + // The baseline of the source row above the source row this row starts. + let above = source_row.and_then(|j| self.run_of(j.checked_sub(1)?)).map(|l| l.origin.baseline); + // The space this row already had above it, beyond the ordinary row step. + let existing = source_row.map_or(0.0, |j| existing_gap(self.block, self.lines, self.row_step, j)); + let starts_paragraph = + row_index > 0 && self.explicit.get(row_index).copied().unwrap_or(false) && (anchor.is_none() || (self.set && existing > 0.5)); + let paragraph = starts_paragraph.then_some(self.gap); + if self.follow_source { + match (anchor, above, self.previous) { + // The source's step down from the row above, kept from where that row was drawn; + // written `here - (above - previous)` so a row below unmoved rows lands exactly + // on its source baseline. + (Some(a), Some(above), Some(previous)) => { + baseline = a.origin.baseline - (above - previous) - paragraph.map_or(0.0, |gap| gap - existing); + } + (Some(a), _, None) => baseline = a.origin.baseline, + _ => baseline -= paragraph.unwrap_or(0.0), + } + } else { + baseline -= paragraph.unwrap_or(0.0); + } + self.previous = Some(baseline); + (baseline, anchor.or_else(|| self.run_of(row_index)).unwrap_or(self.first)) + } +} + /// For each output row (a range of the draft's characters), whether it is exactly the source row /// at the same position: the same characters, none of them inserted or changed by the edit. fn untouched_rows(block: &TextBlock, draft: &str, rows: &[std::ops::Range]) -> Vec { let old: Vec = block.editor_text.chars().collect(); let new: Vec = draft.chars().collect(); - let prefix = old.iter().zip(&new).take_while(|(a, b)| a == b).count(); - let suffix = old.iter().rev().zip(new.iter().rev()).take(old.len().min(new.len()).saturating_sub(prefix)).take_while(|(a, b)| a == b).count(); - // The source character a draft character is, if the edit left it alone. - let origin = |i: usize| { - if i < prefix { - Some(i) - } else if i >= new.len().saturating_sub(suffix) { - (old.len() + i).checked_sub(new.len()) - } else { - None - } - }; + let origins = Origins::new(&old, &new); let mut source_rows = Vec::new(); let mut start = 0; for (i, ch) in old.iter().enumerate() { @@ -1316,7 +1407,7 @@ fn untouched_rows(block: &TextBlock, draft: &str, rows: &[std::ops::Range .enumerate() .map(|(r, range)| { source_rows.get(r).is_some_and(|source| { - !range.is_empty() && range.len() == source.len() && range.clone().zip(source.clone()).all(|(i, j)| origin(i) == Some(j)) + !range.is_empty() && range.len() == source.len() && range.clone().zip(source.clone()).all(|(i, j)| origins.of(i) == Some(j)) }) }) .collect() @@ -1514,6 +1605,159 @@ fn rewrite_anchor<'a>( RewriteAnchor { lead, drop, start, split } } +/// Which source character each draft character is, where the edit left it alone. The text is +/// aligned a line at a time first: the lines the source and the draft share at their start and +/// at their end are the same lines, so a whole line inserted exactly at a line boundary is +/// attributed to the lines between them, whatever the characters around it are (a character +/// prefix and suffix cannot tell `Two\nNew\nThree` from `Two\nThree` where the break is). Between +/// them, what is left of each is aligned by its common character prefix and suffix. +struct Origins { + of: Vec>, +} + +/// The lines of `text` as character ranges, each with its line break (the last has none). +fn line_ranges(text: &[char]) -> Vec> { + let mut lines = Vec::new(); + let mut start = 0; + for (i, ch) in text.iter().enumerate() { + if *ch == '\n' { + lines.push(start..i + 1); + start = i + 1; + } + } + lines.push(start..text.len()); + lines +} + +impl Origins { + fn new(old: &[char], new: &[char]) -> Self { + let (old_lines, new_lines) = (line_ranges(old), line_ranges(new)); + // A line's text without its break, and whether it has one: lines are the same when the + // text is, and a break is shared only when both have it. + let text = |chars: &'_ [char], r: &std::ops::Range| -> (usize, usize) { + let end = if r.end > r.start && chars[r.end - 1] == '\n' { r.end - 1 } else { r.end }; + (r.start, end) + }; + let same = |o: &std::ops::Range, n: &std::ops::Range| { + let ((os, oe), (ns, ne)) = (text(old, o), text(new, n)); + old[os..oe] == new[ns..ne] + }; + let mut of = vec![None; new.len()]; + let mut pair = |o: &std::ops::Range, n: &std::ops::Range, with_break: bool| { + let ((os, oe), (ns, ne)) = (text(old, o), text(new, n)); + for k in 0..oe - os { + of[ns + k] = Some(os + k); + } + if with_break && o.end > oe && n.end > ne { + of[ne] = Some(oe); + } + }; + let (ol, nl) = (old_lines.len(), new_lines.len()); + let limit = ol.min(nl); + let head = (0..limit).take_while(|&k| same(&old_lines[k], &new_lines[k])).count(); + let tail = (0..limit - head).take_while(|&k| same(&old_lines[ol - 1 - k], &new_lines[nl - 1 - k])).count(); + let (old_gap, new_gap) = (ol - head - tail, nl - head - tail); + for k in 0..head { + // The break after the last of them is the source's only when nothing lies between. + let last = k + 1 == head; + pair(&old_lines[k], &new_lines[k], !last || (old_gap == 0 && new_gap == 0)); + } + for k in 1..=tail { + pair(&old_lines[ol - k], &new_lines[nl - k], true); + } + let from_here = |lines: &[std::ops::Range], at: usize, len: usize| lines.get(at).map_or(len, |r| r.start); + if old_gap > 0 && new_gap > 0 { + // Lines the edit changed: aligned by the characters they share at their start and + // at their end, from the break before them to the break after. + let before = |lines: &[std::ops::Range]| if head > 0 { lines[head - 1].end - 1 } else { 0 }; + let old_mid = before(&old_lines)..from_here(&old_lines, ol - tail, old.len()); + let new_mid = before(&new_lines)..from_here(&new_lines, nl - tail, new.len()); + let (om, nm) = (&old[old_mid.clone()], &new[new_mid.clone()]); + let prefix = om.iter().zip(nm).take_while(|(a, b)| a == b).count(); + let suffix = om.iter().rev().zip(nm.iter().rev()).take(om.len().min(nm.len()) - prefix).take_while(|(a, b)| a == b).count(); + for k in 0..prefix { + of[new_mid.start + k] = Some(old_mid.start + k); + } + for k in 1..=suffix { + of[new_mid.end - k] = Some(old_mid.end - k); + } + // The same number of lines on both sides: each is the line at its own position, so + // the breaks between them are the source's (an edited row is still that row). + if old_gap == new_gap { + for k in 0..old_gap { + let (o, n) = (&old_lines[head + k], &new_lines[head + k]); + if old[o.end - 1] == '\n' && new[n.end - 1] == '\n' { + of[n.end - 1] = Some(o.end - 1); + } + } + } + } else if tail > 0 { + // A whole line (or lines) inserted or removed: the break before the first line that + // follows is the same break in both, and no other break in between is the source's. + if let (Some(n), Some(o)) = (new_lines[nl - tail].start.checked_sub(1), old_lines[ol - tail].start.checked_sub(1)) { + of[n] = Some(o); + } + } + Self { of } + } + + /// The source character the draft character `i` is, if the edit left it alone. + fn of(&self, i: usize) -> Option { + self.of.get(i).copied().flatten() + } +} + +/// The baseline of source row `row`'s first run. +fn source_baseline(block: &TextBlock, lines: &[TextLine], row: usize) -> Option { + block.rows.get(row).and_then(|r| r.first()).and_then(|i| lines.get(*i)).map(|l| l.origin.baseline) +} + +/// The ordinary step between the block's source rows: the smallest, or `fallback` for one row. +pub(crate) fn source_row_step(block: &TextBlock, lines: &[TextLine], fallback: f64) -> f64 { + (1..block.rows.len()) + .filter_map(|r| Some(source_baseline(block, lines, r - 1)? - source_baseline(block, lines, r)?)) + .filter(|v| *v > 0.0) + .fold(None, |min: Option, v| Some(min.map_or(v, |m| m.min(v)))) + .unwrap_or(fallback) +} + +/// The space source row `row` already had above it, beyond the ordinary `row_step`. +pub(crate) fn existing_gap(block: &TextBlock, lines: &[TextLine], row_step: f64, row: usize) -> f64 { + match (source_baseline(block, lines, row), row.checked_sub(1).and_then(|r| source_baseline(block, lines, r))) { + (Some(here), Some(above)) => (above - here - row_step).max(0.0), + _ => 0.0, + } +} + +/// For each output row (a range of the draft's characters), the source row it starts: row 0 +/// starts row 0, and a row the draft's line break started (`explicit`) starts the source row +/// after that break when the break is one of the source's, left alone by the edit. A wrapped +/// row, or one after a break the edit inserted, starts none. When the source rows can't be told +/// apart by their line breaks, each row is paired with the source row at its own position. +pub(crate) fn row_sources(block: &TextBlock, draft: &str, rows: &[std::ops::Range], explicit: &[bool]) -> Vec> { + let old: Vec = block.editor_text.chars().collect(); + let breaks: Vec = old.iter().enumerate().filter(|(_, c)| **c == '\n').map(|(i, _)| i).collect(); + if breaks.len().saturating_add(1) != block.rows.len() { + return (0..rows.len()).map(Some).collect(); + } + let new: Vec = draft.chars().collect(); + let origins = Origins::new(&old, &new); + rows.iter() + .enumerate() + .map(|(r, range)| { + if r == 0 { + return Some(0); + } + if !explicit.get(r).copied().unwrap_or(false) { + return None; + } + // The draft's line break is the character just before the row. + let source = origins.of(range.start.checked_sub(1)?)?; + breaks.binary_search(&source).ok().map(|k| k + 1) + }) + .collect() +} + /// Open (`op` is `"BMC"`) or close (`"EMC"`) the `/P` marker that wraps the whole rewrite when /// `marked` (every row is in this rewrite, see [`rewrite_styled_block`]), so the paragraph /// cannot be mistaken for a neighbour by geometry alone (#2517). @@ -1722,7 +1966,7 @@ pub(crate) fn rewrite_styled_block( } Ok(width) }; - let (mut rows, row_ranges, row_lines, row_soft, row_gap) = wrap_styled_rows(&chars, width, measure)?; + let (mut rows, row_ranges, row_lines, row_soft, row_gap, explicit) = wrap_styled_rows(&chars, width, measure)?; let space_styles: Vec<_> = rows.iter_mut().map(|row| assign_subset_space_fonts(row, &metrics)).collect(); let streams = content_streams(doc, &p.dict)?; let pieces = crate::text::joined(&streams); @@ -1783,6 +2027,7 @@ pub(crate) fn rewrite_styled_block( }) .rfind(|v| *v > 0.0) .unwrap_or(first.size * 1.5); + let mut spacing = RowSpacing::new(block, lines, first, style, &row_ranges, draft, explicit)?; // Revealed OCR text that something is painted over goes on top of the page, where it starts // from the initial state. let largest = @@ -1798,14 +2043,8 @@ pub(crate) fn rewrite_styled_block( let drawn_from = if over { &initial } else { after }; let mut baseline = first.origin.baseline; for (row_index, row) in rows.iter().enumerate() { - let anchor = block.rows.get(row_index).and_then(|r| r.first()).and_then(|i| lines.get(*i)); - if style.line_spacing.is_none() - && style.size.is_none() - && let Some(anchor) = anchor - { - baseline = anchor.origin.baseline; - } - let anchor = anchor.unwrap_or(first); + let anchor; + (baseline, anchor) = spacing.place(row_index, baseline); if kept.get(row_index).copied().unwrap_or(false) { // Kept rows only occur without spacing or size changes, so this is the step below. baseline -= source_leading; diff --git a/crates/engine/src/document.rs b/crates/engine/src/document.rs index 8a166330b9..98a7e45009 100644 --- a/crates/engine/src/document.rs +++ b/crates/engine/src/document.rs @@ -728,6 +728,17 @@ impl Document { self.editor.as_ref().and_then(|e| pdfcraft_organize::page_boxes(&e.cos).ok()).unwrap_or_default() } + /// Every page's `/UserUnit`: points per user-space unit, read as Acrobat reads it (1 when absent or invalid). + pub fn page_user_units(&self) -> Vec { + self.editor.as_ref().map(|e| pdfcraft_model::pages(&e.cos).iter().map(|p| p.user_unit(&e.cos)).collect()).unwrap_or_default() + } + + /// Page `page`'s `/UserUnit` (see [`Self::page_user_units`]); 1 when there is no such page. + pub fn page_user_unit(&self, page: usize) -> f64 { + let Some(e) = self.editor.as_ref() else { return 1.0 }; + pdfcraft_model::page_at(&e.cos, page).map_or(1.0, |p| p.user_unit(&e.cos)) + } + /// Current value of a document-information entry (Title, Author, …). pub fn info_value(&self, key: &str) -> Option { self.editor.as_ref().and_then(|e| pdfcraft_organize::info(&e.cos, key)) @@ -743,6 +754,11 @@ impl Document { self.editor.as_ref().map(|e| pdfcraft_organize::custom_info(&e.cos)).unwrap_or_default() } + /// The page templates as they are now: the hidden ones, then the named visible pages (#1429). + pub fn templates(&self) -> Vec { + self.editor.as_ref().map(|e| pdfcraft_organize::templates(&e.cos)).unwrap_or_default() + } + /// The name shown on the tab and window: the document title when the document asks for it /// (Initial View ▸ Show: Document Title) and has a real one, else the file name. pub fn display_name(&self) -> String { diff --git a/crates/engine/src/edit.rs b/crates/engine/src/edit.rs index 80c51658e4..bd65f1fa5f 100644 --- a/crates/engine/src/edit.rs +++ b/crates/engine/src/edit.rs @@ -45,6 +45,20 @@ pub enum Edit { pages: Vec, at: Option, }, + /// A copy of page template `name` inserted at `at` (clamped to the end), as JavaScript's + /// `Template.spawn` adds it (#1429). `rename` puts the copy's fields under `P.`; + /// without it they are more widgets of the template's fields. + SpawnTemplate { + name: String, + at: usize, + rename: bool, + }, + /// Show page template `name` at the end of the document, or take its page out of the + /// document again (JavaScript's `Template.hidden`). + SetTemplateHidden { + name: String, + hidden: bool, + }, /// Replace Pages: the content of `pages` is replaced by `src_pages` of another PDF; the /// pages' links, comments and bookmarks stay. ReplacePages { diff --git a/crates/engine/src/edit/labels.rs b/crates/engine/src/edit/labels.rs index 93045146ae..829afea516 100644 --- a/crates/engine/src/edit/labels.rs +++ b/crates/engine/src/edit/labels.rs @@ -15,6 +15,9 @@ pub const EDIT_LABELS: &[&str] = &[ "Insert blank page", "Duplicate page", "Duplicate pages", + "Add a page from template {name}", + "Show template {name}", + "Hide template {name}", "Replace page", "Replace pages", "Crop page", @@ -188,6 +191,9 @@ impl Edit { Edit::ReversePages { .. } => Label::of("Reverse pages"), Edit::InsertBlankPage { .. } => Label::of("Insert blank page"), Edit::DuplicatePages { pages, .. } => plural("Duplicate page", "Duplicate pages", pages.len()), + Edit::SpawnTemplate { name, .. } => Label::with("Add a page from template {name}", name.as_str()), + Edit::SetTemplateHidden { name, hidden: false } => Label::with("Show template {name}", name.as_str()), + Edit::SetTemplateHidden { name, hidden: true } => Label::with("Hide template {name}", name.as_str()), Edit::ReplacePages { pages, .. } => plural("Replace page", "Replace pages", pages.len()), Edit::SetPageBox { pages, which: PageBox::Crop, .. } => plural("Crop page", "Crop pages", pages.len()), Edit::SetPageBox { .. } => Label::of("Set page boxes"), diff --git a/crates/engine/src/edit/run.rs b/crates/engine/src/edit/run.rs index 79a99a5684..58a2609db6 100644 --- a/crates/engine/src/edit/run.rs +++ b/crates/engine/src/edit/run.rs @@ -15,6 +15,8 @@ pub(crate) fn check_permission(edit: &Edit, p: &pdfcraft_cos::Permissions) -> Re | Edit::ReversePages { .. } | Edit::InsertBlankPage { .. } | Edit::DuplicatePages { .. } + | Edit::SpawnTemplate { .. } + | Edit::SetTemplateHidden { .. } | Edit::ReplacePages { .. } | Edit::SetPageBox { .. } | Edit::SetTransition { .. } @@ -196,6 +198,30 @@ fn note_skipped(doc: &mut pdfcraft_cos::Document, report: &pdfcraft_xfdf::Report )); } +/// Up to the first five of `names`, quoted and joined, then how many more there are. +fn listed_names(names: &[String]) -> String { + let shown: Vec = names.iter().take(5).map(|n| format!("\"{}\"", skipped_text(n))).collect(); + match names.len().saturating_sub(shown.len()) { + 0 => shown.join(", "), + more => format!("{}; and {more} more", shown.join(", ")), + } +} + +/// What a form-data import took from a file that also gave values the form refused (a value its +/// field can't hold, such as an option it doesn't list): the repair log names the fields, so a +/// partial import is said, not slipped in. +fn note_rejected(doc: &mut pdfcraft_cos::Document, report: &pdfcraft_xfdf::Report) { + if report.rejected.is_empty() { + return; + } + let count = report.rejected.len(); + doc.note_repair(format!( + "imported form data without the value{} the form refused for {}", + if count == 1 { "" } else { "s" }, + listed_names(&report.rejected) + )); +} + /// At most this many characters of one skipped comment's description in the repair log. const SKIPPED_CHARS: usize = 200; @@ -546,6 +572,10 @@ pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut E } pdfcraft_organize::duplicate_pages(doc, pages, *at)? } + Edit::SpawnTemplate { name, at, rename } => { + pdfcraft_organize::spawn_template(doc, name, *at, *rename)?; + } + Edit::SetTemplateHidden { name, hidden } => pdfcraft_organize::set_template_hidden(doc, name, *hidden)?, Edit::ReplacePages { pages, name, bytes, src_pages } => { let src = crate::sources::open_source_into(name, bytes, doc)?; pdfcraft_organize::replace_pages(doc, pages, &src, src_pages)?; @@ -581,7 +611,7 @@ pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut E Edit::BookmarksFromStructure => { let entries: Vec<_> = pdfcraft_a11y::headings(doc) .into_iter() - .map(|h| pdfcraft_organize::OutlineEntry { level: h.level, title: h.title, page: h.page, element: h.obj, top: None }) + .map(|h| pdfcraft_organize::OutlineEntry { level: h.level, title: h.title, page: h.page, element: h.obj, left: None, top: None }) .collect(); pdfcraft_organize::add_bookmark_tree(doc, "Untitled", &entries)?; } @@ -1063,7 +1093,14 @@ pub(crate) fn run_edit(doc: &mut pdfcraft_cos::Document, edit: &Edit, cx: &mut E let mut data = doc.clone(); match pdfcraft_xfdf::import(&mut data, bytes) { Ok(report) => { + // The importer hands back what the form refused. When that is all the + // file gave, nothing was imported: say so, and leave the document + // (and its Undo and Redo) as it was. + if report.fields == 0 && report.comments == 0 && !report.rejected.is_empty() { + return Err(pdfcraft_xfdf::DataError::Refused(listed_names(&report.rejected)).into()); + } note_skipped(&mut data, &report); + note_rejected(&mut data, &report); *doc = data; } Err(e) if bytes[..bytes.len().min(1024)].windows(5).any(|w| w == b"%PDF-") => match comment_source(bytes) { diff --git a/crates/engine/src/js.rs b/crates/engine/src/js.rs index 301f9ce25e..6772e9828d 100644 --- a/crates/engine/src/js.rs +++ b/crates/engine/src/js.rs @@ -11,6 +11,17 @@ pub use pdfcraft_js::{Limits, Message, MessageButtons, MessageIcon, Outcome, Pan use crate::{DocId, Edit, EditError, Session}; +/// At most this many pages a script adds or shows from templates in one run: each one copies +/// the document, so a script looping over `spawn` must not hold the app on a large file (#1429). +const MAX_TEMPLATE_PAGES: usize = 100; + +/// Whether a script run for `target` may add or show pages from templates (#1429): only a push +/// button's may, as for field scripts (`JsOutput`), and a document-level script with no target; +/// a target naming another field is refused that (CodeRabbit on #5332). +fn may_change_pages(target: Option<&str>, fields: &[pdfcraft_forms::Field]) -> bool { + target.is_none_or(|t| fields.iter().any(|f| f.name == t && f.kind == pdfcraft_forms::FieldKind::PushButton)) +} + /// Reset writeback does two full passes (preview and commit); reject amplification before either. const MAX_SCRIPT_RESETS: usize = pdfcraft_js::MAX_RESET_CALLS; const MAX_SCRIPT_RESET_SELECTORS: usize = 4096; @@ -172,6 +183,9 @@ impl JsOutput { Request::OpenAttachment { name, .. } => { notes.push(format!("A field script asked to open the attached file {name}; only a button may open an attached file")) } + Request::SpawnTemplate { .. } | Request::SetTemplateHidden { .. } => { + self.console.push("A field script asked to add or show a page from a template; only a button may change the pages".into()) + } Request::OpenDoc(path) => notes.push(format!( "A field script asked to open the document {path}; only a button, a link or the console may open another document" )), @@ -557,6 +571,8 @@ impl JsRunner { c.set((g + 1, p)); }); let pages = pdfcraft_model::pages(cos); + // The page templates a script can show or add a page from (#1429). + let templates = pdfcraft_organize::templates(cos).into_iter().map(|t| (t.name, t.hidden)).collect(); let doc = pdfcraft_js::DocInfo { file_name: file_name.to_string(), num_pages: pages.len(), @@ -565,6 +581,7 @@ impl JsRunner { page_boxes: pdfcraft_organize::page_boxes(cos).unwrap_or_default(), page_rotations: pages.iter().map(|page| page.rotation(cos)).collect(), xfa, + templates, zoom: 100.0, runtime_highlight: highlight, icon_epoch: 0, @@ -1220,7 +1237,7 @@ impl Session { answers, ); let discard = o.execution_refused && !o.realm_untouched; - let result = self.apply_script_outcome(id, &states, o, opening); + let result = self.apply_script_outcome(id, &states, o, opening, may_change_pages(target, &fields)); if discard || result.is_err() { self.invalidate_javascript(id); } @@ -1234,6 +1251,7 @@ impl Session { states: &[pdfcraft_js::FieldState], mut o: Outcome, opening: bool, + may_change_pages: bool, ) -> Result { if o.execution_refused { o.changed.clear(); @@ -1269,6 +1287,32 @@ impl Session { let editor = doc.editor.as_ref().ok_or_else(|| EditError::ReadOnly(doc.read_only_reason.clone().unwrap_or_default()))?; Some(editor.cos.clone()) }; + // Pages from templates go in the same undo step, after the field changes (#1429). Only a + // push button may change the pages, as for field scripts (`JsOutput`); a target that + // names another field is refused that (CodeRabbit on #5332, [`may_change_pages`]). + let button = may_change_pages; + let mut pages = Vec::new(); + let (mut over, mut overlaid, mut not_button) = (false, None, false); + for r in std::mem::take(&mut o.requests) { + match r { + Request::SpawnTemplate { .. } | Request::SetTemplateHidden { .. } if !button => not_button = true, + Request::SpawnTemplate { .. } | Request::SetTemplateHidden { .. } if pages.len() >= MAX_TEMPLATE_PAGES => over = true, + Request::SpawnTemplate { overlay: true, name, .. } => overlaid = overlaid.or(Some(name)), + Request::SpawnTemplate { name, page, rename, overlay: false } => pages.push(Edit::SpawnTemplate { name, at: page, rename }), + Request::SetTemplateHidden { name, hidden } => pages.push(Edit::SetTemplateHidden { name, hidden }), + r => o.requests.push(r), + } + } + // Said once, however many overlays the script asked for. + if let Some(name) = overlaid { + o.console.push(format!("Laying template {name} over an existing page isn't supported yet; ask for a new page (bOverlay false)")); + } + if not_button { + o.console.push("A field script asked to add or show a page from a template; only a button may change the pages".into()); + } + if over { + o.console.push(format!("A script may add or show at most {MAX_TEMPLATE_PAGES} pages from templates at a time; the rest were left out")); + } // The calculations the script's changes set off read the field highlight it asked for. if let Some(on) = last_highlight(&o.requests) { self.field_highlight = on; @@ -1301,6 +1345,7 @@ impl Session { if !changes.is_empty() { edits.push(Edit::ApplyScriptChanges { changes }); } + edits.extend(pages); if !edits.is_empty() { self.apply(id, Edit::Batch { label: "Run JavaScript".into(), edits })?; } @@ -1729,7 +1774,8 @@ impl Session { drop_failed_link_effects(&mut o); self.keep_read_only_link_requests(id, &states, &mut o); let discard = o.execution_refused && !o.realm_untouched; - let result = self.apply_script_outcome(id, &states, o, false); + // A link's script has the document as its target, as a document script does. + let result = self.apply_script_outcome(id, &states, o, false, may_change_pages(None, &[])); if discard || result.is_err() { self.invalidate_javascript(id); } @@ -1764,6 +1810,8 @@ pub struct PendingScript { generation: u64, runtime: Runtime, states: std::sync::Arc>, + /// Whether the script may add or show pages from templates ([`may_change_pages`]). + may_change_pages: bool, pub receiver: std::sync::mpsc::Receiver, } #[cfg(not(target_arch = "wasm32"))] @@ -1810,7 +1858,8 @@ impl Session { }; let receiver = doc.js_runtime.lock().map_err(|_| EditError::Invalid("script runtime unavailable".into()))?.start(job).map_err(EditError::Invalid)?; - Ok(PendingScript { id, link: false, generation: doc.generation, runtime: doc.js_runtime.clone(), states, receiver }) + let may_change_pages = may_change_pages(target, &fields); + Ok(PendingScript { id, link: false, generation: doc.generation, runtime: doc.js_runtime.clone(), states, may_change_pages, receiver }) } /// Start link `index` on page `page` as [`Session::run_link_javascript`] runs it, without /// blocking on its prompts; [`Session::finish_javascript`] commits nothing if it failed. @@ -1819,7 +1868,9 @@ impl Session { let doc = self.get(id).ok_or(EditError::NoDocument)?; let receiver = doc.js_runtime.lock().map_err(|_| EditError::Invalid("script runtime unavailable".into()))?.start(job).map_err(EditError::Invalid)?; - Ok(PendingScript { id, link: true, generation: doc.generation, runtime: doc.js_runtime.clone(), states, receiver }) + // A link's script has the document as its target, as a document script does. + let may_change_pages = may_change_pages(None, &[]); + Ok(PendingScript { id, link: true, generation: doc.generation, runtime: doc.js_runtime.clone(), states, may_change_pages, receiver }) } /// Discard only the captured runtime, preserving a newer realm created by an intervening edit. pub fn discard_javascript(&mut self, p: &PendingScript) { @@ -1854,7 +1905,7 @@ impl Session { self.keep_read_only_link_requests(p.id, &p.states, &mut out); } let discard = out.execution_refused && !out.realm_untouched; - let result = self.apply_script_outcome(p.id, &p.states, out, false); + let result = self.apply_script_outcome(p.id, &p.states, out, false, p.may_change_pages); if discard || result.is_err() { self.invalidate_javascript(p.id); } diff --git a/crates/engine/src/live_scripts.rs b/crates/engine/src/live_scripts.rs index b69f7d85d2..e35cf9c7ce 100644 --- a/crates/engine/src/live_scripts.rs +++ b/crates/engine/src/live_scripts.rs @@ -280,7 +280,9 @@ impl Session { let generation = self.get(id).map(Document::edit_generation); let layers = |s: &Session| s.get(id).map(|d| d.info.layers.iter().map(|l| l.visible).collect::>()); let layers_before = layers(self); - match self.apply_script_outcome(id, &states, o, false) { + // Page actions and timers run while the document is read: they add or show no template + // pages (#1429), which would change the document as the reader moves through it. + match self.apply_script_outcome(id, &states, o, false, false) { Ok(o) => { run.output.absorb(&o); // Only now: a script whose edits are refused shows nothing it did either. diff --git a/crates/engine/src/printcheck.rs b/crates/engine/src/printcheck.rs index 744e52d4e4..f236370b42 100644 --- a/crates/engine/src/printcheck.rs +++ b/crates/engine/src/printcheck.rs @@ -256,15 +256,18 @@ fn page_reports(doc: &Document, pages: &[pdfcraft_model::Page]) -> Vec Result<(), EditError> { + let renumbered = + !doc.info.layers.is_empty() && doc.editor.as_ref().is_some_and(|e| layer_ids(&e.cos).iter().ne(doc.info.layers.iter().map(|l| &l.id))); + if renumbered { Self::refresh_renumbered(doc) } else { Self::refresh_scoped(doc, scope) } + } + /// Rebuild the working bytes and the view data that `scope` may have changed. Metadata edits /// keep the renderer; other scopes also replace it. /// Comment and form edits skip the full re-inspection (seconds on very large files): the @@ -672,15 +682,21 @@ impl Session { /// Rebuild working bytes, inspection and renderer from the current edit state. pub(crate) fn refresh(doc: &mut Document) -> Result<(), EditError> { - Self::refresh_with_indexes(doc, true, true) + Self::refresh_with_indexes(doc, true, true, false) + } + + /// [`Self::refresh`] after a full rewrite renumbered the objects: the user's layer choices + /// follow their layers to the new numbers instead of being looked up by the old ones. + pub(super) fn refresh_renumbered(doc: &mut Document) -> Result<(), EditError> { + Self::refresh_with_indexes(doc, true, true, true) } /// Reinspect a link edit while preserving the page-content marker scan cache. fn refresh_links(doc: &mut Document) -> Result<(), EditError> { - Self::refresh_with_indexes(doc, false, true) + Self::refresh_with_indexes(doc, false, true, false) } - fn refresh_with_indexes(doc: &mut Document, invalidate_markers: bool, invalidate_links: bool) -> Result<(), EditError> { + fn refresh_with_indexes(doc: &mut Document, invalidate_markers: bool, invalidate_links: bool, renumbered: bool) -> Result<(), EditError> { #[cfg(test)] crate::record_full_refresh(); // A full refresh may follow structural or annotation changes: cached appearances @@ -699,9 +715,17 @@ impl Session { let info = inspect(bytes.clone(), doc.password.as_deref()).map_err(|e| EditError::Reopen(e.to_string()))?; // Keep the user's layer choices where the layers still exist. let mut info = info; - for l in &mut info.layers { - if let Some(old) = doc.info.layers.iter().find(|o| o.id == l.id) { - l.visible = old.visible; + if renumbered { + carry_renumbered_layer_choices(&doc.info.layers, &mut info.layers); + // The renderer's overrides name the layers by their numbers too. + if !doc.config.layers.is_empty() || info.layers.iter().any(|l| l.visible != l.initial) { + doc.config.layers = Arc::new(info.layers.iter().map(|l| (l.id.0 as i32, l.id.1 as i32, l.visible)).collect()); + } + } else { + for l in &mut info.layers { + if let Some(old) = doc.info.layers.iter().find(|o| o.id == l.id) { + l.visible = old.visible; + } } } note_warnings(&mut info.warnings, &doc.xfa_warnings); @@ -733,6 +757,36 @@ impl Session { } } +/// The optional content groups `cos` lists as layers, in order (what the inspection reads: a group +/// listed twice is one layer, and one that is not a dictionary is none). +fn layer_ids(cos: &pdfcraft_cos::Document) -> Vec<(u32, u16)> { + let props = + cos.trailer().get(b"Root").map(|r| cos.resolve(r)).and_then(|r| r.as_dict().and_then(|r| r.get(b"OCProperties").map(|p| cos.resolve(p)))); + let groups = props.as_deref().and_then(|p| p.as_dict()).and_then(|p| p.get(b"OCGs").map(|g| cos.resolve(g))); + let mut ids: Vec<(u32, u16)> = Vec::new(); + for group in groups.as_deref().and_then(|g| g.as_array()).into_iter().flatten() { + let Some(id) = group.as_ref().map(|id| (id.num, id.generation)) else { continue }; + if cos.resolve(group).as_dict().is_some() && !ids.contains(&id) { + ids.push(id); + } + } + ids +} + +/// Give `new`, the layers of a file whose objects were renumbered, the visibility the user chose on +/// `old`. The numbers say nothing across a rewrite (one may now belong to another layer), so each +/// layer is matched by name: the nth layer called so in `new` is the nth one called so in `old` +/// (the writer keeps the `/OCGs` order). A layer with no counterpart keeps the file's own default. +fn carry_renumbered_layer_choices(old: &[pdfcraft_render::Layer], new: &mut [pdfcraft_render::Layer]) { + let mut seen: std::collections::HashMap<&str, usize> = std::collections::HashMap::new(); + for layer in new { + let nth = seen.entry(layer.name.as_str()).or_insert(0); + let counterpart = old.iter().filter(|o| o.name == layer.name).nth(*nth); + *nth += 1; + layer.visible = counterpart.map_or(layer.initial, |o| o.visible); + } +} + /// Hand on what an edit's XFA scripts showed or asked for (`out`), and when one of them ran away, /// turn the form's scripts off and say so. fn take_xfa_output(doc: &mut Document, out: js::JsOutput, ran_away: bool) { diff --git a/crates/engine/src/session/save.rs b/crates/engine/src/session/save.rs index ec0033d318..d23e159e20 100644 --- a/crates/engine/src/session/save.rs +++ b/crates/engine/src/session/save.rs @@ -65,6 +65,7 @@ impl Session { /// so the next save appends only newer edits. Undo history is kept. pub fn mark_saved(&mut self, id: DocId, bytes: Arc>, path: Option) -> Result<(), EditError> { let doc = self.doc_mut(id)?; + let mut renumbered = false; if let Some(editor) = doc.editor.as_mut() { // An encrypted file needs the password it is protected with now (the owner // password when known, so saving never downgrades this session's rights). @@ -77,6 +78,7 @@ impl Session { let reopened = pdfcraft_cos::Document::open_with_password(bytes.clone(), keys.reopen.as_deref()).map_err(|e| EditError::Reopen(e.to_string()))?; if !preserves_objects { + renumbered = true; crate::js::replace_runtime(&mut doc.js_runtime); } editor.cos = reopened; @@ -95,7 +97,8 @@ impl Session { } doc.dirty = false; doc.generation += 1; - Self::refresh(doc).inspect_err(|_| Self::show_released(doc)) + let refreshed = if renumbered { Self::refresh_renumbered(doc) } else { Self::refresh(doc) }; + refreshed.inspect_err(|_| Self::show_released(doc)) } /// Enter the permissions (owner) password of a document opened with its user password, diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index faab8af4de..980f92d647 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1201,6 +1201,98 @@ fn reset_layers_returns_to_the_files_visibility() { assert!(!s.reset_layers(DocId(999))); } +/// #7411: a full Save As renumbers the objects (the layer groups move from 9 and 10 to the lowest +/// free numbers); the open document keeps the layer the user hid hidden, and the other shown. +#[test] +fn a_full_save_as_keeps_the_layer_choices_of_the_open_document() { + let content = b"/OC /R BDC 1 0 0 rg 10 10 40 80 re f EMC /OC /B BDC 0 0 1 rg 110 10 40 80 re f EMC"; + let mut objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [9 0 R 10 0 R] /D << /BaseState /ON /Order [9 0 R 10 0 R] >> >> >>".to_vec(), + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Contents 4 0 R /Resources << /Properties << /R 9 0 R /B 10 0 R >> >> >>".to_vec(), + [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), + ]; + // Objects 5 to 8 are not used, so a rewrite closes the gap. + objs.extend((5..=8).map(|_| b"null".to_vec())); + objs.push(b"<< /Type /OCG /Name (Red rectangle) >>".to_vec()); + objs.push(b"<< /Type /OCG /Name (Blue rectangle) >>".to_vec()); + let mut s = Session::new(); + let id = s.open("layers.pdf", None, Arc::new(pdf_of_objects(&objs)), None).unwrap(); + let colours = |s: &Session| { + let page = render_page(s, id); + let px = page.rgba.as_chunks::<4>().0; + let red = px.iter().filter(|p| p[0] > 200 && p[1] < 60 && p[2] < 60).count(); + let blue = px.iter().filter(|p| p[2] > 200 && p[0] < 60 && p[1] < 60).count(); + (red, blue) + }; + let hidden = |s: &Session| s.get(id).unwrap().info.layers.iter().map(|l| (l.name.clone(), l.visible)).collect::>(); + let (red, blue) = colours(&s); + assert!(red > 100 && blue > 100, "both layers show as the file opens: {red} red, {blue} blue"); + assert!(s.set_layer_visible(id, 0, false)); + assert_eq!(colours(&s).0, 0, "the red rectangle is hidden"); + let before = s.get(id).unwrap().info.layers.iter().map(|l| l.id).collect::>(); + + let saved = s.save_full_bytes(id).unwrap(); + s.mark_saved(id, saved, Some("/tmp/out/layers.pdf".into())).unwrap(); + + let after = s.get(id).unwrap().info.layers.iter().map(|l| l.id).collect::>(); + assert_ne!(before, after, "the rewrite renumbered the layer groups"); + assert_eq!(hidden(&s), [("Red rectangle".to_string(), false), ("Blue rectangle".to_string(), true)]); + let (red, blue) = colours(&s); + assert_eq!(red, 0, "the page is still drawn without the red rectangle"); + assert!(blue > 100, "and with the blue one: {blue}"); +} + +/// #7411: undo history survives the full Save As, whose renumbering the restored graph does not +/// share. Undo and redo carry the user's layer choices over by name, so the layer hidden before the +/// save stays hidden in the layer list and in the pixels, and the renderer's overrides name the +/// numbers the restored graph has. +#[test] +fn undo_and_redo_after_a_full_save_as_keep_the_hidden_layer_hidden() { + let content = b"/OC /R BDC 1 0 0 rg 10 10 40 80 re f EMC /OC /B BDC 0 0 1 rg 110 10 40 80 re f EMC"; + let mut objs: Vec> = vec![ + b"<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [9 0 R 10 0 R] /D << /BaseState /ON /Order [9 0 R 10 0 R] >> >> >>".to_vec(), + b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), + b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Contents 4 0 R /Resources << /Properties << /R 9 0 R /B 10 0 R >> >> >>".to_vec(), + [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), + ]; + objs.extend((5..=8).map(|_| b"null".to_vec())); + objs.push(b"<< /Type /OCG /Name (Red rectangle) >>".to_vec()); + objs.push(b"<< /Type /OCG /Name (Blue rectangle) >>".to_vec()); + let mut s = Session::new(); + let id = s.open("layers.pdf", None, Arc::new(pdf_of_objects(&objs)), None).unwrap(); + let colours = |s: &Session| { + let page = render_page(s, id); + let px = page.rgba.as_chunks::<4>().0; + let red = px.iter().filter(|p| p[0] > 200 && p[1] < 60 && p[2] < 60).count(); + let blue = px.iter().filter(|p| p[2] > 200 && p[0] < 60 && p[1] < 60).count(); + (red, blue) + }; + let layers = |s: &Session| s.get(id).unwrap().info.layers.iter().map(|l| (l.name.clone(), l.visible)).collect::>(); + let ids = |s: &Session| s.get(id).unwrap().info.layers.iter().map(|l| l.id).collect::>(); + s.apply(id, Edit::RotatePages { pages: vec![0], degrees: 180 }).unwrap(); + assert!(s.set_layer_visible(id, 0, false)); + let saved = s.save_full_bytes(id).unwrap(); + s.mark_saved(id, saved, Some("/tmp/out/layers.pdf".into())).unwrap(); + let saved_ids = ids(&s); + let expect_red_hidden = |s: &Session, step: &str| { + assert_eq!(layers(s), [("Red rectangle".to_string(), false), ("Blue rectangle".to_string(), true)], "{step}: the layer list"); + let (red, blue) = colours(s); + assert_eq!((red, blue > 100), (0, true), "{step}: the pixels show the blue rectangle only"); + }; + expect_red_hidden(&s, "after the save"); + + s.undo(id).unwrap(); + assert_ne!(ids(&s), saved_ids, "undo brings back the numbers from before the save"); + expect_red_hidden(&s, "after undo"); + s.redo(id).unwrap(); + assert_eq!(ids(&s), saved_ids, "redo brings back the numbers the save wrote"); + expect_red_hidden(&s, "after redo"); + // Choices made after the trip still apply to the numbers now in use. + assert!(s.set_layer_visible(id, 0, true)); + assert!(colours(&s).0 > 100, "the red rectangle shows again when asked for"); +} + #[test] fn edits_to_unknown_documents_are_rejected() { let (mut s, _) = session_with(1); @@ -2472,6 +2564,165 @@ fn headings_in_two_columns_are_two_bookmarks() { assert_eq!(outline_titles(&s.get(id).unwrap().info.outline), ["Untitled→0[Left heading→1,Right heading→1]"]); } +/// #7415: a heading's bookmark goes to the top of the heading as displayed. The same heading is +/// shown at the same place on four pages turned 0, 90, 180 and 270 degrees; on a quarter turn the +/// displayed top is a user-space x, so it is the destination's `left` and `top` stays unspecified. +#[test] +fn style_bookmarks_go_to_the_displayed_heading_top_on_turned_pages() { + // (rotation, media box, text matrix): each shows "Birch Survey" at 90 pt from the left and + // 130 pt (baseline) from the top of a 400 x 600 displayed page. + let pages = [ + (0, "0 0 400 600", "1 0 0 1 90 470"), + (90, "0 0 600 400", "0 1 -1 0 130 90"), + (180, "0 0 400 600", "-1 0 0 -1 310 130"), + (270, "0 0 600 400", "0 -1 1 0 470 310"), + ]; + let mut pdf = + pdfcraft_testkit::Pdf::new().obj("<< /Type /Catalog /Pages 2 0 R >>").obj("<< /Type /Pages /Kids [3 0 R 5 0 R 7 0 R 9 0 R] /Count 4 >>"); + for (i, (rotate, media, tm)) in pages.iter().enumerate() { + let content = format!("BT /F1 20 Tf {tm} Tm (Birch Survey) Tj ET"); + pdf = pdf + .obj(format!( + "<< /Type /Page /Parent 2 0 R /MediaBox [{media}] /Rotate {rotate} /Resources << /Font << /F1 11 0 R >> >> /Contents {} 0 R >>", + 4 + 2 * i + )) + .stream("", &content); + } + let bytes = pdf.obj("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>").build(); + let mut s = Session::new(); + let id = s.open("turned.pdf", None, Arc::new(bytes), None).unwrap(); + let style = s.get(id).unwrap().text_styles()[0].style.clone(); + s.apply(id, Edit::BookmarksFromTextStyles { levels: vec![style] }).unwrap(); + let doc = s.get(id).unwrap(); + let kids = &doc.info.outline[0].children; + assert_eq!(kids.len(), 4); + // The displayed top of the heading, from the upright page. + let top_of_heading = { + let cos = pdfcraft_cos::Document::open(doc.bytes.clone()).unwrap(); + let line = &pdfcraft_edit::text_lines(&cos, 0).unwrap()[0]; + 600.0 - line.rect[1].max(line.rect[3]) as f32 + }; + assert!((110.0..115.0).contains(&top_of_heading), "{top_of_heading}"); + for (i, kid) in kids.iter().enumerate() { + let page = &doc.info.pages[i]; + assert_eq!(page.rotation, pages[i].0); + let [want_left, want_top] = page.xyz_for_view(None, Some(top_of_heading)); + match kid.view { + pdfcraft_render::DestView::Xyz { left, top, zoom: None } => { + let near = |got: Option, want: Option| match (got, want) { + (None, None) => true, + (Some(g), Some(w)) => (g - w).abs() < 1.0, + _ => false, + }; + assert!(near(left, want_left) && near(top, want_top), "rotate {}: got {left:?} {top:?}, want {want_left:?} {want_top:?}", pages[i].0); + } + ref v => panic!("{v:?}"), + } + } +} + +/// #7415: on a turned page the headings are read in displayed order (top to bottom), a heading +/// set on two lines is one bookmark, and each goes to the top of its first displayed line. One +/// layout (two headings and a two-line heading, written to the file out of order) on pages turned +/// 0, 90, 180 and 270 degrees must give the same bookmarks at the same displayed tops, with and +/// without a crop box whose origin is not the page's. +#[test] +fn style_bookmarks_read_and_join_headings_as_displayed_on_turned_pages() { + for cropped in [false, true] { + let (mut s, id) = turned_headings_session(cropped); + let style = s.get(id).unwrap().text_styles()[0].style.clone(); + s.apply(id, Edit::BookmarksFromTextStyles { levels: vec![style] }).unwrap(); + check_turned_headings(&s, id, cropped); + // The destinations survive a save and reopen (`/XYZ left null` on the quarter turns). + let bytes = s.save_bytes(id).unwrap(); + let mut s2 = Session::new(); + let id2 = s2.open("saved.pdf", None, bytes, None).unwrap(); + check_turned_headings(&s2, id2, cropped); + } +} + +/// A document of four pages turned 0, 90, 180 and 270, each showing the same three headings at the +/// same displayed places on a displayed page of 400 x 600 (or 600 x 600 with `cropped`, where +/// the crop box does not start at the page's origin), written in an order that is not reading order. +fn turned_headings_session(cropped: bool) -> (Session, DocId) { + // Displayed baselines (x, y from the top). + let lines = [("Gamma Three", 50.0, 450.0), ("Beta Cont", 50.0, 274.0), ("Alpha One", 50.0, 100.0), ("Beta Two", 50.0, 250.0)]; + // (rotation, page boxes, text matrix prefix, user position of a displayed (x, y)). + type Place = fn(f64, f64) -> (f64, f64); + let plain: [(i32, &str, &str, Place); 4] = [ + (0, "/MediaBox [0 0 400 600]", "1 0 0 1", |x, y| (x, 600.0 - y)), + (90, "/MediaBox [0 0 600 400]", "0 1 -1 0", |x, y| (y, x)), + (180, "/MediaBox [0 0 400 600]", "-1 0 0 -1", |x, y| (400.0 - x, y)), + (270, "/MediaBox [0 0 600 400]", "0 -1 1 0", |x, y| (600.0 - y, 400.0 - x)), + ]; + let boxes = "/MediaBox [0 0 700 700] /CropBox [50 40 650 640]"; + let crop: [(i32, &str, &str, Place); 4] = [ + (0, boxes, "1 0 0 1", |x, y| (50.0 + x, 640.0 - y)), + (90, boxes, "0 1 -1 0", |x, y| (50.0 + y, 40.0 + x)), + (180, boxes, "-1 0 0 -1", |x, y| (650.0 - x, 40.0 + y)), + (270, boxes, "0 -1 1 0", |x, y| (650.0 - y, 640.0 - x)), + ]; + let mut pdf = + pdfcraft_testkit::Pdf::new().obj("<< /Type /Catalog /Pages 2 0 R >>").obj("<< /Type /Pages /Kids [3 0 R 5 0 R 7 0 R 9 0 R] /Count 4 >>"); + for (i, (rotate, boxes, prefix, place)) in (if cropped { crop } else { plain }).iter().enumerate() { + let content: String = lines + .iter() + .map(|(text, x, y)| { + let (ux, uy) = place(*x, *y); + format!("BT /F1 20 Tf {prefix} {ux} {uy} Tm ({text}) Tj ET\n") + }) + .collect(); + pdf = pdf + .obj(format!( + "<< /Type /Page /Parent 2 0 R {boxes} /Rotate {rotate} /Resources << /Font << /F1 11 0 R >> >> /Contents {} 0 R >>", + 4 + 2 * i + )) + .stream("", &content); + } + let bytes = pdf.obj("<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>").build(); + let mut s = Session::new(); + let id = s.open("turned.pdf", None, Arc::new(bytes), None).unwrap(); + (s, id) +} + +/// The bookmarks of [`turned_headings_session`] after "from text styles": displayed order, the +/// two-line heading joined, each at the displayed top of its first line. +fn check_turned_headings(s: &Session, id: DocId, cropped: bool) { + let doc = s.get(id).unwrap(); + let kids = &doc.info.outline[0].children; + let titles: Vec<_> = kids.iter().map(|k| k.title.as_str()).collect(); + let per_page = ["Alpha One", "Beta Two Beta Cont", "Gamma Three"]; + assert_eq!(titles, per_page.repeat(4), "displayed order, the two-line heading joined (cropped: {cropped})"); + // The displayed tops of the three headings, from the text of the upright page. + let cos = pdfcraft_cos::Document::open(doc.bytes.clone()).unwrap(); + let upright = &doc.info.pages[0]; + let mut found: Vec<_> = + pdfcraft_edit::text_lines(&cos, 0).unwrap().into_iter().map(|l| upright.user_to_view(0.0, l.rect[3].max(l.rect[1]) as f32)[1]).collect(); + found.sort_by(f32::total_cmp); + // Alpha, Beta Two (the upper line of its heading), Gamma: three of the four lines. + let tops = [found[0], found[1], found[3]]; + for (i, kid) in kids.iter().enumerate() { + let (page, n) = (&doc.info.pages[i / 3], i % 3); + let [want_left, want_top] = page.xyz_for_view(None, Some(tops[n])); + let near = |got: Option, want: Option| match (got, want) { + (None, None) => true, + (Some(g), Some(w)) => (g - w).abs() < 1.0, + _ => false, + }; + match kid.view { + pdfcraft_render::DestView::Xyz { left, top, .. } => { + assert!( + near(left, want_left) && near(top, want_top), + "rotate {} {} (cropped: {cropped}): got {left:?} {top:?}, want {want_left:?} {want_top:?}", + page.rotation, + kid.title + ) + } + ref v => panic!("{v:?}"), + } + } +} + /// `style` at another size. fn pdfcraft_engine_style(style: &text_style_bookmarks::TextStyle, size: f64) -> text_style_bookmarks::TextStyle { text_style_bookmarks::TextStyle { size, ..style.clone() } @@ -3031,16 +3282,16 @@ fn need_appearances_form() -> Vec { "<< /Type /Page /Parent 2 0 R /Annots [4 0 R 5 0 R 6 0 R 11 0 R 12 0 R 15 0 R] >>".into(), "<< /Type /Annot /Subtype /Widget /FT /Tx /T (name) /V (Ada Lovelace) /Rect [20 250 180 270] /P 3 0 R /AP << /N 8 0 R >> >>".into(), "<< /Type /Annot /Subtype /Widget /FT /Tx /T (town) /V /Rect [20 200 180 220] /P 3 0 R /AP << /N 9 0 R >> >>".into(), - "<< /Type /Annot /Subtype /Widget /FT /Tx /T (city) /V /Rect [20 150 180 170] /P 3 0 R /AP << /N 10 0 R >> >>".into(), + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (city) /V /Rect [20 150 180 170] /P 3 0 R /AP << /N 10 0 R >> >>".into(), "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".into(), stream("/Tx BMC EMC"), stream("/Tx BMC EMC"), stream("/Tx BMC BT /Helv 10 Tf 2 5 Td (Brno r) Tj ET EMC"), "<< /Type /Annot /Subtype /Widget /FT /Tx /T (street) /V (Main Street) /Rect [20 100 180 120] /P 3 0 R >>".into(), - "<< /Type /Annot /Subtype /Widget /FT /Tx /T (river) /V /Rect [20 50 180 70] /P 3 0 R /AP << /N 13 0 R >> >>".into(), + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (river) /V /Rect [20 50 180 70] /P 3 0 R /AP << /N 13 0 R >> >>".into(), "<< /Type /XObject /Subtype /Form /BBox [0 0 160 20] /Resources << /XObject << /Bg 14 0 R >> >> /Length 10 >>\nstream\nq /Bg Do Q\nendstream".into(), stream("0.9 g 0 0 160 20 re f"), - "<< /Type /Annot /Subtype /Widget /FT /Tx /T (lake) /V /Rect [20 20 180 40] /P 3 0 R /AP << /N 16 0 R >> >>".into(), + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (lake) /V /Rect [20 20 180 40] /P 3 0 R /AP << /N 16 0 R >> >>".into(), "<< /Type /XObject /Subtype /Form /BBox [0 0 160 20] /Resources << /XObject << /Fm 17 0 R >> >> /Length 10 >>\nstream\nq /Fm Do Q\nendstream".into(), stream("BT /Helv 10 Tf 2 5 Td (Orech) Tj ET"), ]; @@ -3070,7 +3321,8 @@ fn normal_appearance(doc: &pdfcraft_cos::Document, num: u32) -> String { /// #5939 review: the first signature removes `/NeedAppearances`, so the fields are drawn from /// their values first; without that, `name` and `town` would show their empty placeholders. A -/// field whose own appearance draws its value keeps it rather than get a `?` for `ř`. +/// field whose own appearance draws its value keeps it rather than get a `?` for `北`, which no +/// font PdfCraft draws with has; `ň` is drawn from the bundled Inter face (#6822). #[test] fn the_first_signature_draws_the_fields_need_appearances_left_to_the_viewer() { let p12 = std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/../sign/tests/data/ec-p256.p12")).unwrap(); @@ -3082,7 +3334,8 @@ fn the_first_signature_draws_the_fields_need_appearances_left_to_the_viewer() { let doc = pdfcraft_cos::Document::open(signed.clone()).unwrap(); assert!(!pdfcraft_forms::need_appearances(&doc)); assert!(normal_appearance(&doc, 4).contains("(Ada Lovelace) Tj"), "{}", normal_appearance(&doc, 4)); - assert!(normal_appearance(&doc, 5).contains("(Plze?) Tj"), "{}", normal_appearance(&doc, 5)); + let town = normal_appearance(&doc, 5); + assert!(town.contains("(Plze) Tj") && town.contains("/PCLa") && !town.contains('?'), "{town}"); assert!(normal_appearance(&doc, 6).contains("(Brno r) Tj"), "{}", normal_appearance(&doc, 6)); assert!(normal_appearance(&doc, 11).contains("(Main Street) Tj"), "{}", normal_appearance(&doc, 11)); // Its own appearance only paints a background, so `river` gets the drawing, `?` and all. @@ -7442,6 +7695,17 @@ fn detecting_fields_on_a_printed_form() { let f = &d.form[0]; assert!(f.widgets[0].rect[0] > 72.0 + 20.0, "{:?}", f.widgets[0].rect); assert!(s.detect_fields(id, &[]).is_empty(), "nothing left to detect"); + + // #2943: a "label, box, label, box" line names each check box after the label before it. + let labels_pdf = paper_form_with( + 595, + 842, + "BT /F1 10 Tf 100 534 Td (Monsieur) Tj ET 146 532 11 10 re S BT /F1 10 Tf 180 534 Td (Madame) Tj ET 222 532 11 10 re S", + ); + let labels_id = s.open("labels.pdf", None, Arc::new(labels_pdf), None).unwrap(); + let found = s.detect_fields(labels_id, &[]); + let names: Vec<&str> = found.iter().map(|(_, c)| c.name.as_str()).collect(); + assert_eq!(names, ["Monsieur", "Madame"], "{found:?}"); } /// #294, #2831: a detected field sits on a line or box the page already draws, so it gets no border @@ -7466,18 +7730,11 @@ fn detected_fields_have_no_border_or_fill() { assert!(look.border.is_some() && look.fill.is_some(), "{look:?}"); } -/// A printed form, 420 × 300 pt: a title, a label with a rule to write on, a box with a small -/// label in its corner, an empty box with its label above, and two check boxes. -fn paper_form() -> Vec { - let content = "BT /F1 14 Tf 30 270 Td (Gewerbe-Ummeldung) Tj ET \ - BT /F1 10 Tf 30 230 Td (Name:) Tj ET 0.75 w 80 228 m 380 228 l S \ - 1 w 30 150 350 50 re S BT /F1 7 Tf 34 191 Td (Anschrift) Tj ET \ - BT /F1 8 Tf 30 124 Td (Telefon) Tj ET 30 90 350 30 re S \ - 30 50 10 10 re S BT /F1 9 Tf 46 51 Td (Haupt) Tj ET 150 50 10 10 re S BT /F1 9 Tf 166 51 Td (Zweig) Tj ET"; +fn paper_form_with(w: u32, h: u32, content: &str) -> Vec { let objs = [ b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), - b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 420 300] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>".to_vec(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {w} {h}] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> >>").into_bytes(), format!("<< /Length {} >>\nstream\n{content}\nendstream", content.len()).into_bytes(), b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_vec(), ]; @@ -7498,6 +7755,20 @@ fn paper_form() -> Vec { out } +/// A printed form, 420 × 300 pt: a title, a label with a rule to write on, a box with a small +/// label in its corner, an empty box with its label above, and two check boxes. +fn paper_form() -> Vec { + paper_form_with( + 420, + 300, + "BT /F1 14 Tf 30 270 Td (Gewerbe-Ummeldung) Tj ET \ + BT /F1 10 Tf 30 230 Td (Name:) Tj ET 0.75 w 80 228 m 380 228 l S \ + 1 w 30 150 350 50 re S BT /F1 7 Tf 34 191 Td (Anschrift) Tj ET \ + BT /F1 8 Tf 30 124 Td (Telefon) Tj ET 30 90 350 30 re S \ + 30 50 10 10 re S BT /F1 9 Tf 46 51 Td (Haupt) Tj ET 150 50 10 10 re S BT /F1 9 Tf 166 51 Td (Zweig) Tj ET", + ) +} + /// How the paper form was scanned. #[derive(Clone, Copy, Debug)] enum Scan { @@ -15183,6 +15454,39 @@ fn mixed_widget_icons_are_normalized_with_history_and_save() { } } } +/// #7409: a form-data file whose only known value the form refuses imports nothing, and says which +/// field refused it; the document, its Undo and its Redo stay as they were. A file with other +/// values that took still imports them, and the repair log names the field that refused. +#[test] +fn form_data_the_form_refuses_is_reported_and_leaves_the_document_alone() { + let (mut s, id) = session_with(1); + let combo = NewField::Combo { options: vec!["A".into(), "B".into()], editable: false }; + s.apply(id, Edit::AddField { page: 0, rect: [20.0, 250.0, 180.0, 272.0], kind: combo, name: Some("Choice".into()) }).unwrap(); + let notes = NewField::Text { multiline: false }; + s.apply(id, Edit::AddField { page: 0, rect: [20.0, 210.0, 180.0, 232.0], kind: notes, name: Some("Notes".into()) }).unwrap(); + let choose = + |s: &mut Session, v: &str| s.apply(id, Edit::SetFieldValue { name: "Choice".into(), value: FieldValue::Choice(vec![v.into()]) }).unwrap(); + choose(&mut s, "A"); + choose(&mut s, "B"); + s.undo(id).unwrap(); + let value = |s: &Session, n: &str| s.get(id).unwrap().form.iter().find(|f| f.name == n).map(|f| f.value.clone()).unwrap_or_default(); + let import = |s: &mut Session, xml: &str| s.apply(id, Edit::ImportData { name: "data.xml".into(), bytes: Arc::new(xml.as_bytes().to_vec()) }); + let (undo, redo) = (s.get(id).unwrap().can_undo().map(String::from), s.get(id).unwrap().can_redo().map(String::from)); + assert!(redo.is_some()); + + let err = import(&mut s, "INVALID").unwrap_err(); + assert!(err.to_string().contains("nothing was imported") && err.to_string().contains("\"Choice\""), "{err}"); + let doc = s.get(id).unwrap(); + assert_eq!((doc.can_undo().map(String::from), doc.can_redo().map(String::from)), (undo, redo), "no Import step, Redo kept"); + assert_eq!(value(&s, "Choice"), vec!["A".to_string()]); + + // Some values took: the file imports, and the refused field is named in the repair log. + import(&mut s, "INVALIDhello").unwrap(); + assert_eq!((value(&s, "Choice"), value(&s, "Notes")), (vec!["A".to_string()], vec!["hello".to_string()])); + let log = s.get(id).unwrap().repair_log().to_vec(); + assert!(log.iter().any(|l| l.contains("refused") && l.contains("\"Choice\"") && !l.contains("Notes")), "{log:?}"); +} + /// A comment imported or pasted without the appearance its file carried (unreadable there) is /// said in the repair log, which the interface shows and the tools return. #[test] diff --git a/crates/engine/src/text_style_bookmarks.rs b/crates/engine/src/text_style_bookmarks.rs index 5969188d67..05ca9223f0 100644 --- a/crates/engine/src/text_style_bookmarks.rs +++ b/crates/engine/src/text_style_bookmarks.rs @@ -60,11 +60,30 @@ struct Line { page: usize, text: String, style: TextStyle, - /// Top, bottom, left and right of its box in user space. + /// Top, bottom, left and right of its box as displayed (after the page's `/Rotate`, y up), so + /// reading order and the join of a heading's lines are right on a turned page. Only + /// differences between these values are used: they are offset from points on the page. top: f64, bottom: f64, left: f64, right: f64, + /// The `/XYZ` position `[left, top]` (user space) that puts the top of the line as displayed + /// at the top of the window. The box edge displayed on top depends on the rotation: the top + /// edge, the left edge (90), the bottom edge (180) or the right edge (270). On a page turned + /// a quarter it is a user-space x, so it is `left`, and `top` stays unspecified. + anchor: [Option; 2], +} + +/// A box in user space `[x0, y0, x1, y1]` on a page turned `rotation` degrees clockwise → its +/// displayed top, bottom, left and right (y up), and the `/XYZ` anchor of its displayed top. +fn displayed(rotation: i64, [x0, y0, x1, y1]: [f64; 4]) -> ([f64; 4], [Option; 2]) { + let (xl, xh, yl, yh) = (x0.min(x1), x0.max(x1), y0.min(y1), y0.max(y1)); + match rotation { + 90 => ([-xl, -xh, yl, yh], [Some(xl), None]), + 180 => ([-yl, -yh, -xh, -xl], [None, Some(yl)]), + 270 => ([xh, xl, -yh, -yl], [Some(xh), None]), + _ => ([yh, yl, xl, xh], [None, Some(yh)]), + } } /// `name` without a subset tag: six capital letters and a `+` (ISO 32000-2 §9.9.2). @@ -79,8 +98,10 @@ fn without_subset_tag(name: &str) -> &str { /// is only whitespace, or whose size can't be read, is left out. fn lines(doc: &Document) -> Vec { let pages = pdfcraft_organize::page_count(doc).unwrap_or(0).min(MAX_PAGES); + let rotations = pdfcraft_organize::page_rotations(doc).unwrap_or_default(); let mut out = Vec::new(); for page in 0..pages { + let rotation = rotations.get(page).copied().unwrap_or(0); let Ok(found) = pdfcraft_edit::text_lines(doc, page) else { continue }; let mut on_page: Vec = found .into_iter() @@ -91,6 +112,7 @@ fn lines(doc: &Document) -> Vec { let size = l.size; let [x0, y0, x1, y1] = l.rect; let ok = !text.is_empty() && size.is_finite() && size > 0.0 && [x0, y0, x1, y1].iter().all(|v| v.is_finite()); + let ([top, bottom, left, right], anchor) = displayed(rotation, l.rect); ok.then(|| Line { page, text, @@ -100,10 +122,11 @@ fn lines(doc: &Document) -> Vec { bold: l.bold, italic: l.italic, }, - top: y0.max(y1), - bottom: y0.min(y1), - left: x0.min(x1), - right: x0.max(x1), + top, + bottom, + left, + right, + anchor, }) }) .collect(); @@ -157,7 +180,7 @@ pub fn styles(doc: &Document) -> Vec { /// The bookmarks for the lines in `levels`' styles: a line in `levels[0]`'s style is a level-1 /// bookmark, `levels[1]`'s a level-2 one under the level-1 bookmark before it, and so on. A /// heading set on several lines (one after another in the same style, close below each other) -/// is one bookmark. Each goes to the top of its heading. +/// is one bookmark. Each goes to the top of its heading as displayed, also on a turned page. pub fn headings(doc: &Document, levels: &[TextStyle]) -> Vec { let mut entries: Vec = Vec::new(); // The heading the previous line belonged to: its level, and the top, bottom, left and right @@ -191,13 +214,8 @@ pub fn headings(doc: &Document, levels: &[TextStyle]) -> Vec { // The level counts from 1; the position in `levels` is at most its length, which a // caller keeps small (a few levels), so it fits a u8. let level = u8::try_from(level.saturating_add(1)).unwrap_or(u8::MAX); - entries.push(OutlineEntry { - level, - title: line.text.chars().take(MAX_TITLE_CHARS).collect(), - page: line.page, - element: None, - top: Some(line.top), - }); + let [left, top] = line.anchor; + entries.push(OutlineEntry { level, title: line.text.chars().take(MAX_TITLE_CHARS).collect(), page: line.page, element: None, left, top }); } open = Some((level, line.top, line.bottom, line.left, line.right)); } diff --git a/crates/engine/tests/form_date_language.rs b/crates/engine/tests/form_date_language.rs index ba4193522e..9184f71fca 100644 --- a/crates/engine/tests/form_date_language.rs +++ b/crates/engine/tests/form_date_language.rs @@ -246,7 +246,9 @@ fn fill_with(doc: &mut Document, name: &str, value: &str) { } /// Month names in the date language that no font of the field can draw are drawn in English, -/// never as `?` ("18-????.-26" in Russian); a font that holds them keeps them (#5303). +/// never as `?` ("18-??-26" in Japanese); a font that holds them keeps them (#5303). Russian, +/// Czech and Greek names keep theirs in a Helvetica field too: the bundled Inter face draws the +/// letters Helvetica lacks (#6822). #[test] fn date_names_no_field_font_can_draw_are_drawn_in_english() { let _names = names_lock(); @@ -258,7 +260,12 @@ fn date_names_no_field_font_can_draw_are_drawn_in_english() { fill(&mut doc, "helv"); fill(&mut doc, "song"); let helv = appearance(&doc, "helv"); - assert!(contains(&helv, b"(18-Oct-26) Tj"), "{language}: {}", String::from_utf8_lossy(&helv)); + if language == "ja" { + assert!(contains(&helv, b"(18-Oct-26) Tj"), "{language}: {}", String::from_utf8_lossy(&helv)); + } else { + assert!(contains(&helv, b"(18-) Tj") && contains(&helv, b"/PCLa"), "{language}: {}", String::from_utf8_lossy(&helv)); + assert!(!contains(&helv, b"Oct"), "{language} keeps its name: {}", String::from_utf8_lossy(&helv)); + } assert!(!helv.contains(&b'?'), "{language}: {}", String::from_utf8_lossy(&helv)); assert!(pdfcraft_forms::undrawn_fields(&doc).is_empty(), "{language}"); let song = appearance(&doc, "song"); @@ -279,7 +286,8 @@ fn format_scripts_print_dates_in_english_where_the_field_cannot_draw_them() { let _names = names_lock(); let script = "event.value = event.value ? util.printd(\"dd-mmm-yy\", util.scand(\"dd-mm-yy\", event.value)) + \" !\" : \"\";"; let fields = [("helv", "Helv", script), ("song", "Song", script)]; - use_form_date_language("ru"); + // Japanese: no font of a Helvetica field draws 月, the bundled Inter face included. + use_form_date_language("ja"); let mut doc = form(&fields); fill(&mut doc, "helv"); fill(&mut doc, "song"); @@ -287,14 +295,21 @@ fn format_scripts_print_dates_in_english_where_the_field_cannot_draw_them() { assert!(contains(&helv, b"(18-Oct-26 !) Tj"), "{}", String::from_utf8_lossy(&helv)); assert!(!helv.contains(&b'?'), "{}", String::from_utf8_lossy(&helv)); assert!(pdfcraft_forms::undrawn_fields(&doc).is_empty()); - assert!(contains(&appearance(&doc, "song"), &ucs2("18-окт.-26 !"))); + assert!(contains(&appearance(&doc, "song"), &ucs2("18-10月-26 !"))); // A script whose text has no English either keeps it, reported as undrawn. - let greeting = [("hi", "Helv", "event.value = \"Привет \" + util.printd(\"mmm\", new Date(2026, 9, 18));")]; + let greeting = [("hi", "Helv", "event.value = \"你好 \" + util.printd(\"mmm\", new Date(2026, 9, 18));")]; let mut doc = form(&greeting); fill(&mut doc, "hi"); - assert_eq!(pdfcraft_forms::undrawn_fields(&doc), [("hi".to_string(), 'П')]); + assert_eq!(pdfcraft_forms::undrawn_fields(&doc), [("hi".to_string(), '你')]); // The English run is the appearance's alone: scripts go on printing the date language. - assert_eq!(printd("dd-mmm-yy", "18-окт.-26"), "18-окт.-26|18-окт.-26"); + assert_eq!(printd("dd-mmm-yy", "18-10月-26"), "18-10月-26|18-10月-26"); + // Russian names a Helvetica field draws from the bundled face need no English run (#6822). + use_form_date_language("ru"); + let mut doc = form(&fields); + fill(&mut doc, "helv"); + let helv = appearance(&doc, "helv"); + assert!(contains(&helv, b"(18-) Tj") && contains(&helv, b"/PCLa") && !contains(&helv, b"Oct"), "{}", String::from_utf8_lossy(&helv)); + assert!(pdfcraft_forms::undrawn_fields(&doc).is_empty()); use_form_date_language("en"); } @@ -305,15 +320,16 @@ fn format_scripts_print_dates_in_english_where_the_field_cannot_draw_them() { fn an_english_run_that_prints_the_stored_value_is_drawn() { let _names = names_lock(); let script = "event.value = util.printd(\"dd-mmm-yy\", util.scand(\"dd-mmm-yy\", event.value));"; - use_form_date_language("ru"); + // Japanese, whose 月 no font of a Helvetica field draws (#6822 draws Russian there). + use_form_date_language("ja"); let mut doc = form(&[("helv", "Helv", script), ("fails", "Helv", "throw new Error(\"no\");")]); fill_with(&mut doc, "helv", "18-Oct-26"); let helv = appearance(&doc, "helv"); assert!(contains(&helv, b"(18-Oct-26) Tj"), "{}", String::from_utf8_lossy(&helv)); assert!(!helv.contains(&b'?'), "{}", String::from_utf8_lossy(&helv)); assert!(pdfcraft_forms::undrawn_fields(&doc).is_empty()); - fill_with(&mut doc, "fails", "18-окт.-26"); - assert_eq!(pdfcraft_forms::undrawn_fields(&doc), [("fails".to_string(), 'о')]); + fill_with(&mut doc, "fails", "18-10月-26"); + assert_eq!(pdfcraft_forms::undrawn_fields(&doc), [("fails".to_string(), '月')]); use_form_date_language("en"); } diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index c4cf560b9e..a94c5914d2 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -10,6 +10,7 @@ mod form_rotation; mod marked_content_isolation; mod ocr_edit; mod office_export_images; +mod page_templates; mod private_image_resize; mod private_text_glyphs; mod xfa_repeat; diff --git a/crates/engine/tests/it/page_templates.rs b/crates/engine/tests/it/page_templates.rs new file mode 100644 index 0000000000..a28d8543b7 --- /dev/null +++ b/crates/engine/tests/it/page_templates.rs @@ -0,0 +1,133 @@ +//! Page templates through the session (#1429): a button's script shows a template or adds a page +//! from one as one undo step with its other changes; a field's own scripts may not. + +use std::sync::Arc; + +use pdfcraft_engine::{DocId, Edit, FieldValue, Session}; + +/// One visible page with a text field `Name` (whose validate script tries to show the template), +/// and a hidden template `More` holding the text field `Extra`. +fn form() -> Vec { + let bodies = [ + "<< /Type /Catalog /Pages 2 0 R /Names << /Templates 6 0 R >> /AcroForm << /Fields [7 0 R 10 0 R] /DA (/Helv 0 Tf 0 g) >> >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 200 300] >>", + "<< /Type /Page /Parent 2 0 R /Contents 4 0 R /Resources << /Font << /F1 9 0 R >> >> /Annots [10 0 R] >>", + "<< /Length 40 >>\nstream\nBT /F1 12 Tf 20 20 Td (Visible) Tj ET\nendstream", + "<< /Type /Template /Contents 8 0 R /Resources << /Font << /F1 9 0 R >> >> /Annots [7 0 R] >>", + "<< /Names [(More) 5 0 R] >>", + "<< /FT /Tx /T (Extra) /Subtype /Widget /Rect [20 100 180 120] /P 5 0 R >>", + "<< /Length 41 >>\nstream\nBT /F1 12 Tf 20 20 Td (Template) Tj ET\nendstream", + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + "<< /FT /Tx /T (Name) /Subtype /Widget /Rect [20 200 180 220] /P 3 0 R /AA << /V << /S /JavaScript /JS (getTemplate('More').hidden = false;) >> >> >>", + ]; + let mut out = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::new(); + for (i, b) in bodies.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n{b}\nendobj\n", i + 1).as_bytes()); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes()); + for o in offsets { + out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", bodies.len() + 1).as_bytes()); + out +} + +fn open() -> (Session, DocId) { + let mut s = Session::new(); + let id = s.open("form.pdf", None, Arc::new(form()), None).expect("opens"); + (s, id) +} + +fn pages(s: &Session, id: DocId) -> usize { + s.get(id).unwrap().info.pages.len() +} + +fn fields(s: &Session, id: DocId) -> Vec { + let mut names: Vec = s.get(id).unwrap().form.iter().map(|f| f.name.clone()).collect(); + names.sort(); + names +} + +#[test] +fn a_script_shows_a_template_as_one_undo_step() { + let (mut s, id) = open(); + let names: Vec<(String, bool)> = s.get(id).unwrap().templates().into_iter().map(|t| (t.name, t.hidden)).collect(); + assert_eq!(names, [("More".to_string(), true)]); + let o = s.run_javascript(id, "console.println(this.templates.length); getTemplate('More').hidden = false;", None).unwrap(); + assert_eq!((o.error, o.console), (None, vec!["1".to_string()])); + assert_eq!(pages(&s, id), 2, "the template's page is shown"); + assert!(!s.get(id).unwrap().templates()[0].hidden); + assert_eq!(s.get(id).unwrap().can_undo(), Some("Run JavaScript")); + s.undo(id).unwrap(); + assert_eq!(pages(&s, id), 1, "one undo takes it out again"); +} + +#[test] +fn a_script_adds_pages_from_a_template_renamed_or_shared() { + let (mut s, id) = open(); + let o = s.run_javascript(id, "this.spawnPageFromTemplate('More', 1, false, false); getTemplate('More').spawn(2, true, false);", None).unwrap(); + assert_eq!(o.error, None); + assert_eq!(pages(&s, id), 3); + assert_eq!(fields(&s, id), ["Extra", "Name", "P2.More.Extra"], "the renamed copy has a field of its own; the other shares Extra"); + // Saved and opened again, the pages and fields are still there. + let bytes = s.save_bytes(id).unwrap(); + let mut again = Session::new(); + let reopened = again.open("again.pdf", None, bytes, None).unwrap(); + assert_eq!(pages(&again, reopened), 3); + assert_eq!(fields(&again, reopened), ["Extra", "Name", "P2.More.Extra"]); +} + +/// Laying a template over a page isn't done yet: the script is told on the console, and the +/// pages stay as they were. +#[test] +fn an_overlaid_spawn_is_noted_and_changes_nothing() { + let (mut s, id) = open(); + let o = s.run_javascript(id, "getTemplate('More').spawn();", None).unwrap(); + assert_eq!(pages(&s, id), 1); + assert!(o.console.iter().any(|l| l.contains("More") && l.contains("bOverlay")), "{:?}", o.console); + assert!(o.requests.is_empty(), "nothing is left for the viewer: {:?}", o.requests); +} + +/// A field's own scripts run unasked, so they may not change the pages; they are told so. +#[test] +fn a_field_script_may_not_change_the_pages() { + let (mut s, id) = open(); + s.apply(id, Edit::SetFieldValue { name: "Name".into(), value: FieldValue::Text("Ada".into()) }).unwrap(); + assert_eq!(pages(&s, id), 1); + let out = s.take_js_output(id); + assert!(out.console.iter().any(|l| l.contains("only a button may change the pages")), "{:?}", out.console); +} + +/// A script looping over `spawn` adds at most 100 pages in one run, says so once, and doesn't +/// hold the app: each page copies the document. +#[test] +fn a_spawning_loop_stops_at_its_page_budget() { + let (mut s, id) = open(); + let o = s + .run_javascript(id, "for (var i = 0; i < 150; i++) getTemplate('More').spawn(this.numPages, false, false); for (var j = 0; j < 50; j++) getTemplate('More').spawn();", None) + .unwrap(); + assert_eq!(pages(&s, id), 101, "the visible page and 100 copies"); + let budget = o.console.iter().filter(|l| l.contains("at most 100 pages")).count(); + let overlays = o.console.iter().filter(|l| l.contains("bOverlay")).count(); + assert_eq!((budget, overlays), (1, 0), "said once; the overlays came after the budget was spent: {:?}", o.console); + assert_eq!(fields(&s, id), ["Extra", "Name"], "every unrenamed copy shares Extra"); +} + +/// Run as another field's script (`js_run`'s `field`), a script may not change the pages +/// either: only a push button may (CodeRabbit on #5332). A name no field has is no button. +#[test] +fn a_script_run_for_a_field_that_is_no_button_may_not_change_the_pages() { + let (mut s, id) = open(); + for target in ["Name", "Nope"] { + let o = + s.run_javascript(id, "getTemplate('More').hidden = false; this.spawnPageFromTemplate('More', 1, false, false);", Some(target)).unwrap(); + assert_eq!(pages(&s, id), 1, "{target}"); + let said = o.console.iter().filter(|l| l.contains("only a button may change the pages")).count(); + assert_eq!(said, 1, "{target}: said once: {:?}", o.console); + assert!(o.requests.is_empty(), "{target}: nothing is left for the viewer: {:?}", o.requests); + } + assert_eq!(s.get(id).unwrap().can_undo(), None, "nothing was changed"); +} diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 388a5c5309..a2e15e0e09 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -11,6 +11,7 @@ repository.workspace = true workspace = true [dependencies] +craft-markdown.workspace = true craft-mime.workspace = true craft-opc.workspace = true craft-rtf = { workspace = true, features = ["doc"] } diff --git a/crates/export/src/as_docx.rs b/crates/export/src/as_docx.rs index 0dd395d08a..24b1c5eb98 100644 --- a/crates/export/src/as_docx.rs +++ b/crates/export/src/as_docx.rs @@ -150,7 +150,7 @@ pub(crate) const MAX_MEDIA: usize = 1000; const MAX_MEDIA_BYTES: usize = 512 << 20; /// Whether `im` can still be embedded: it has data, and the package stays within its limits. -fn room_for(media: &[(String, &Image)], im: &Image) -> bool { +pub(crate) fn room_for(media: &[(String, &Image)], im: &Image) -> bool { !im.bytes.is_empty() && media.len() < MAX_MEDIA && media.iter().map(|(_, m)| m.bytes.len()).fold(im.bytes.len(), usize::saturating_add) <= MAX_MEDIA_BYTES diff --git a/crates/export/src/as_html.rs b/crates/export/src/as_html.rs index 7d70359edd..5d51e3b6fe 100644 --- a/crates/export/src/as_html.rs +++ b/crates/export/src/as_html.rs @@ -1,5 +1,5 @@ -use super::as_docx::{hex, rgb8}; -use super::layout::{Item, Page, Run, Table, items}; +use super::as_docx::{hex, rgb8, room_for}; +use super::layout::{Image, Item, Page, Run, Table, items}; /// Whether a character may appear in the output. XML 1.0 forbids the C0 controls other than tab, /// line feed and carriage return, and U+FFFE/U+FFFF. Text extracted from PDFs often holds them @@ -152,6 +152,7 @@ fn html_with_mode(pages: &[Page], title: &str, simple: bool, language: Option<&s "" } ); + let mut embedded: Vec<(String, &Image)> = Vec::new(); for it in items(pages) { match it { Item::Para(runs, lvl) => { @@ -160,10 +161,13 @@ fn html_with_mode(pages: &[Page], title: &str, simple: bool, language: Option<&s } } Item::Table(t) => s.push_str(&table_html(&t, simple)), - Item::Img(im) => { + // HTML embeds each placement in full, so repeated pictures also consume the budget. + Item::Img(im) if room_for(&embedded, im) => { let mime = if im.ext == "jpg" { "image/jpeg" } else { "image/png" }; s.push_str(&format!("

\"\"

\n", base64(&im.bytes))); + embedded.push((String::new(), im)); } + Item::Img(_) => {} Item::PageBreak => s.push_str("
\n"), } } diff --git a/crates/export/src/as_markdown.rs b/crates/export/src/as_markdown.rs index 949264494d..2abda97c3d 100644 --- a/crates/export/src/as_markdown.rs +++ b/crates/export/src/as_markdown.rs @@ -1,156 +1,42 @@ -use super::as_html::allowed; -use super::layout::{Cell, Item, MAX_COLS, Page, items}; +//! PDF layout mapping into the shared extracted-text Markdown writer. +use super::layout::{Cell, Item, Page, items}; +use craft_markdown::extracted::{TableCell, Writer}; -/// Markdown's deepest heading. -const MAX_HEADING: u8 = 6; +#[cfg(test)] +pub(crate) use craft_markdown::extracted::{emphasis, paragraph}; -/// `s` as Markdown text on one line: line breaks become spaces, control characters are dropped, -/// and every character Markdown would read as markup is escaped with a backslash. -fn md_text(s: &str) -> String { - let mut o = String::with_capacity(s.len()); - for c in s.chars().filter(|c| allowed(*c)) { - match c { - '\n' | '\r' | '\t' => o.push(' '), - '\\' | '`' | '*' | '_' | '[' | ']' | '<' | '>' | '#' | '|' | '~' | '!' | '&' => { - o.push('\\'); - o.push(c); - } - c => o.push(c), - } +impl TableCell for Cell { + fn text(&self) -> &str { + &self.text } - o -} - -/// `line` with its start escaped where Markdown would read a list item there (`- `, `+ `, -/// `1. `, `1) `); the other block markers (`#`, `>`) are escaped by [`md_text`] already. -fn block_start(line: String) -> String { - let digits = line.bytes().take_while(u8::is_ascii_digit).count(); - match line.as_bytes().get(digits) { - Some(b'.' | b')') if digits > 0 => format!("{}\\{}", line.get(..digits).unwrap_or_default(), line.get(digits..).unwrap_or_default()), - Some(b'-' | b'+') if digits == 0 => format!("\\{line}"), - _ => line, + fn bold(&self) -> bool { + self.bold + } + fn italic(&self) -> bool { + self.italic + } + fn span(&self) -> usize { + self.span } } -/// `text` (already escaped) in bold and/or italic, its outer spaces kept outside. `open` and -/// `close` say whether `*` markers would be read as emphasis on that side (see [`marks`]); -/// where they would not, as in `foo` + bold `.` + `bar`, the run is wrapped in HTML tags, which -/// CommonMark takes inline. The text's own outer spaces count as a side that marks. -pub(crate) fn emphasis(text: &str, bold: bool, italic: bool, open: bool, close: bool) -> String { - let core = text.trim(); - if core.is_empty() || !(bold || italic) { - return text.to_string(); - } - let lead = text.get(..text.len().saturating_sub(text.trim_start().len())).unwrap_or_default(); - let trail = text.get(text.trim_end().len()..).unwrap_or_default(); - let markers = (open || !lead.is_empty()) && (close || !trail.is_empty()); - let (before, after) = match (markers, bold, italic) { - (true, true, true) => ("***", "***"), - (true, true, false) => ("**", "**"), - (true, false, _) => ("*", "*"), - (false, true, true) => ("", ""), - (false, true, false) => ("", ""), - (false, false, _) => ("", ""), - }; - format!("{lead}{before}{core}{after}{trail}") -} - -/// A paragraph's runs (text, bold, italic) as one line of Markdown. Neighbouring runs in the -/// same style are joined first, so two bold runs never meet as `****`. -pub(crate) fn paragraph(runs: &[(&str, bool, bool)]) -> String { - let mut merged: Vec<(String, bool, bool)> = Vec::new(); - for &(text, bold, italic) in runs { - let text = md_text(text); - if text.is_empty() { - continue; - } - match merged.last_mut() { - Some(last) if last.1 == bold && last.2 == italic => last.0.push_str(&text), - _ => merged.push((text, bold, italic)), - } - } - let mut out = String::new(); - for (k, (text, bold, italic)) in merged.iter().enumerate() { - let core = text.trim(); - let open = marks(core.chars().next(), out.chars().last()); - // Markers of a styled neighbour right next to these would join them into one run. - let close = match merged.get(k.saturating_add(1)) { - Some((next, b, i)) if (*b || *i) && !next.starts_with(char::is_whitespace) => false, - next => marks(core.chars().last(), next.and_then(|(n, ..)| n.chars().next())), - }; - out.push_str(&emphasis(text, *bold, *italic, open, close)); - } - out -} - -/// Whether a `*` marker between the text's `edge` character and the `outside` one is read as -/// emphasis (CommonMark's flanking rules for `*`): always next to a space or the line's edge, -/// never next to another `*`, and otherwise unless the text's edge is punctuation while the -/// outside is a letter or digit. -fn marks(edge: Option, outside: Option) -> bool { - match (edge, outside) { - (_, None) => true, - (_, Some(o)) if o.is_whitespace() => true, - (_, Some('*')) => false, - (Some(e), Some(o)) => e.is_alphanumeric() || o.is_ascii_punctuation(), - (None, Some(_)) => false, - } -} - -fn cell(c: &Cell) -> String { - // A cell sits between `| ` and ` |`, so its markers always have a space outside. - emphasis(&md_text(&c.text), c.bold, c.italic, true, true) -} - -/// Markdown (CommonMark, with GitHub's pipe tables): headings, paragraphs with bold and italic, -/// tables with their first row as the header, and a rule between pages. Images are left out. +/// Markdown (CommonMark with pipe tables), retaining PDF reading order and heading detection. +/// Image omission is this export's policy; formatting and syntax live in the shared writer. pub fn markdown(pages: &[Page]) -> String { - let mut blocks: Vec = Vec::new(); - for it in items(pages) { - match it { - Item::Para(runs, lvl) => { - // A heading's weight is the heading's own, as in the HTML export. - let runs: Vec<(&str, bool, bool)> = runs.iter().map(|r| (r.text.as_str(), r.block.bold && lvl == 0, r.block.italic)).collect(); - let text = paragraph(&runs); - let text = text.trim(); - if text.is_empty() { - continue; - } - blocks.push(match lvl { - 0 => block_start(text.to_string()), - l => format!("{} {text}", "#".repeat(usize::from(l.min(MAX_HEADING)))), - }); - } - Item::Table(t) => { - // Each row as wide as the widest; a cell spanning columns is followed by empty ones. - // Detected tables have at most MAX_COLS columns, and a span never counts for more. - let span = |c: &Cell| c.span.clamp(1, MAX_COLS); - let width = t.rows.iter().map(|r| r.iter().map(span).fold(0usize, usize::saturating_add)).max().unwrap_or(0); - if width == 0 { - continue; - } - let mut lines = Vec::with_capacity(t.rows.len().saturating_add(1)); - for (i, row) in t.rows.iter().enumerate() { - let mut cells: Vec = Vec::with_capacity(width); - for c in row { - cells.push(cell(c)); - cells.extend(std::iter::repeat_n(String::new(), span(c).saturating_sub(1))); - } - cells.resize(width, String::new()); - lines.push(format!("| {} |", cells.join(" | "))); - if i == 0 { - lines.push(format!("|{}", " --- |".repeat(width))); - } - } - blocks.push(lines.join("\n")); + let mut writer = Writer::new(); + for item in items(pages) { + match item { + Item::Para(runs, level) => { + // Heading weight comes from the heading marker, as in the HTML export. + let runs: Vec<_> = runs.iter().map(|r| (r.text.as_str(), r.block.bold && level == 0, r.block.italic)).collect(); + writer.paragraph(&runs, level); } + Item::Table(table) => writer.table(&table.rows), Item::Img(_) => {} - Item::PageBreak => blocks.push("---".into()), + Item::PageBreak => writer.page_break(), } } - let mut s = blocks.join("\n\n"); - s.push('\n'); - s + writer.finish() } #[cfg(test)] @@ -176,3 +62,9 @@ mod tests { assert_eq!(paragraph(&[("", true, false), ("\u{1}", false, true), ("\u{7f}", false, false)]), ""); } } + +#[cfg(test)] +#[test] +fn shared_table_span_limit_matches_layout_contract() { + assert_eq!(craft_markdown::extracted::MAX_SPAN, super::layout::MAX_COLS); +} diff --git a/crates/export/src/pptx.rs b/crates/export/src/pptx.rs index abfdb833c4..59d9f858d9 100644 --- a/crates/export/src/pptx.rs +++ b/crates/export/src/pptx.rs @@ -1,7 +1,9 @@ //! PowerPoint presentation (.pptx, Office Open XML PresentationML): slides per page, //! largest text as slide title, remaining paragraphs as body text, images and tables between them. -use crate::as_docx::{core_properties_xml, image_mime}; +use std::collections::HashMap; + +use crate::as_docx::{core_properties_xml, image_mime, room_for}; use crate::{Image, Page, Run, Table, hex}; use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut}; use craft_xml::{DECL_STANDALONE, esc}; @@ -127,6 +129,8 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec { let content_w = sw - 2 * mx; let mut global_media: Vec<(String, &Image)> = Vec::new(); + // Shared picture bytes remain alive throughout export; only admitted media enter this map. + let mut media_of: HashMap<*const u8, usize> = HashMap::new(); let mut slides: Vec<(String, RelsOut)> = Vec::new(); // (slide_xml, slide_rels) let body = crate::layout::body_size(pages); @@ -201,9 +205,19 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec { current_y = current_y.saturating_add(box_h).saturating_add(height_emu(12.0)); } SlideBodyItem::Img(im) => { - let global_img_idx = global_media.len() + 1; - let img_name = format!("image{global_img_idx}.{}", im.ext); - global_media.push((img_name, *im)); + let global_img_idx = match media_of.get(&im.bytes.as_ptr()) { + Some(&i) => i, + None if room_for(&global_media, im) => { + let n = global_media.len() + 1; + global_media.push((format!("image{n}.{}", im.ext), *im)); + media_of.insert(im.bytes.as_ptr(), n); + n + } + None => { + i += 1; + continue; + } + }; let r_id = format!("rIdImg{}", slide_media_rels.len() + 1); slide_media_rels.push((r_id.clone(), global_img_idx)); diff --git a/crates/export/src/pptx/tests.rs b/crates/export/src/pptx/tests.rs index 5e619b32f0..d703c682f3 100644 --- a/crates/export/src/pptx/tests.rs +++ b/crates/export/src/pptx/tests.rs @@ -379,9 +379,18 @@ fn pptx_table_cell_with_largest_font_stays_in_table_and_not_in_title() { fn pptx_opens_in_libreoffice() { let p = sample_page(); let bytes = pptx(&[p], "LibreOffice Check"); - let dir = std::env::temp_dir().join(format!("pdfcraft-pptx-lo-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - let _ = std::fs::create_dir_all(&dir); + // A new folder of our own, never one left (or planted) at a guessable name. + let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + let dir = std::env::temp_dir().join(format!("pdfcraft-pptx-lo-{}-{nanos:x}", std::process::id())); + // Only Unix sets a mode on it, so elsewhere `made` is never changed. + #[cfg_attr(not(unix), allow(unused_mut))] + let mut made = std::fs::DirBuilder::new(); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + made.mode(0o700); + } + made.create(&dir).expect("a fresh test folder"); let pptx_path = dir.join("test.pptx"); std::fs::write(&pptx_path, &bytes).unwrap(); @@ -394,3 +403,40 @@ fn pptx_opens_in_libreoffice() { } let _ = std::fs::remove_dir_all(&dir); } + +/// A picture drawn in many places is one media part, referred to from each place; the package +/// stays within the media limits however many places there are. +#[test] +fn pptx_packages_a_repeated_picture_once() { + let mut p = sample_page(); + let one = p.images[0].clone(); + p.images = std::iter::repeat_n(one, 2 * crate::as_docx::MAX_MEDIA).collect(); + let bytes = pptx(&[p], "Deck"); + let rels = part(&bytes, "ppt/slides/_rels/slide1.xml.rels"); + assert_eq!(rels.matches("relationships/image\"").count(), 2 * crate::as_docx::MAX_MEDIA, "every place refers to the picture"); + assert!(rels.contains("Target=\"../media/image1.png\"") && !rels.contains("image2."), "one media part"); + assert_eq!(part(&bytes, "ppt/slides/slide1.xml").matches("").count(), 2 * crate::as_docx::MAX_MEDIA); + assert_eq!(bytes.windows(20).filter(|w| *w == b"ppt/media/image1.png").count(), 2, "its local header and central directory entry"); +} + +/// Distinct allocations remain distinct media, even when their bytes match. Once the media +/// budget is full, an existing picture remains usable on a later slide, but a new one is omitted. +#[test] +fn pptx_bounds_distinct_pictures_and_reuses_media_across_slides() { + let mut first = sample_page(); + let one = first.images[0].clone(); + first.images = (0..crate::as_docx::MAX_MEDIA).map(|_| Image { bytes: one.bytes.as_ref().to_vec().into(), ..one.clone() }).collect(); + // Media names follow reading order, not the input vector. Give the reused picture a + // distinct top edge so it is unambiguously image1 rather than tying every rectangle. + first.images[0].rect[3] += 1.0; + let mut second = sample_page(); + second.images = + vec![first.images[0].clone(), Image { bytes: one.bytes.as_ref().to_vec().into(), ..one.clone() }, Image { bytes: Vec::new().into(), ..one }]; + let bytes = pptx(&[first, second], "Deck"); + assert_eq!(part(&bytes, "ppt/slides/slide1.xml").matches("").count(), crate::as_docx::MAX_MEDIA); + assert_eq!(part(&bytes, "ppt/slides/slide2.xml").matches("").count(), 1); + let rels = part(&bytes, "ppt/slides/_rels/slide2.xml.rels"); + assert_eq!(rels.matches("relationships/image\"").count(), 1); + assert!(rels.contains("Target=\"../media/image1.png\"")); + assert!(part(&bytes, "ppt/slides/_rels/slide1.xml.rels").contains("../media/image1000.png")); +} diff --git a/crates/export/src/tests.rs b/crates/export/src/tests.rs index c37bbdbb62..d438db8899 100644 --- a/crates/export/src/tests.rs +++ b/crates/export/src/tests.rs @@ -1566,3 +1566,15 @@ fn rtf_keeps_hard_line_breaks_in_paragraphs_and_cells() { .collect(); assert!(cells.contains(&"Meter\nnumber".to_string()) && cells.contains(&"kWh\nunit".to_string()), "{cells:?}"); } + +/// Each place a picture is drawn embeds it whole in the HTML, so the places count against the +/// media limits: past them, pictures are left out instead of multiplying one into gigabytes. +#[test] +fn html_bounds_the_pictures_it_embeds() { + let mut p = page(); + let one = p.images[0].clone(); + p.images = std::iter::repeat_n(one, MAX_MEDIA + 10).collect(); + for h in [html(std::slice::from_ref(&p), "Report"), html_simple(std::slice::from_ref(&p), "Report")] { + assert_eq!(h.matches(" Result<(), String> { return Err("this font family is excluded by the project's asset policy".into()); } let f = FontRef::new(bytes).map_err(|e| e.to_string())?; - let attribution: String = - [StringId::COPYRIGHT_NOTICE, StringId::MANUFACTURER].into_iter().flat_map(|id| f.localized_strings(id)).map(|s| s.to_string()).collect(); - if attribution.to_ascii_lowercase().contains("adobe") { + // An Adobe scan that could not finish fails closed, like a match. + if any_name_record(&f, &[StringId::COPYRIGHT_NOTICE, StringId::MANUFACTURER], |t| t.to_ascii_lowercase().contains("adobe")) != Some(false) { return Err("Adobe-authored font assets are excluded by the project's asset policy".into()); } - let license: String = f.localized_strings(StringId::LICENSE_DESCRIPTION).map(|s| s.to_string()).collect(); - if !["SIL OPEN FONT", "SIL Open Font", "Apache License", "Ubuntu Font Licence", "Bitstream Vera", "MIT License"] - .iter() - .any(|l| license.contains(l)) - { + let licences = ["SIL OPEN FONT", "SIL Open Font", "Apache License", "Ubuntu Font Licence", "Bitstream Vera", "MIT License"]; + if any_name_record(&f, &[StringId::LICENSE_DESCRIPTION], |t| licences.iter().any(|l| t.contains(l))) != Some(true) { return Err("select an openly licensed font whose name table declares its licence (OFL, Apache, Ubuntu, Bitstream Vera or MIT)".into()); } Ok(()) } +/// Name-table text one policy check reads in all. +const MAX_NAME_BYTES: usize = 1 << 20; + +/// Whether `found` holds for any record of the name-table strings `ids`, read one at a time +/// within [`MAX_NAME_BYTES`] in all: `Some(true)` for a match, `Some(false)` when every record was +/// read and none matched, `None` when the allowance ran out first (a record that does not fit what +/// is left is not searched). The table can point many records at one large string, so the records +/// are never joined. +fn any_name_record(font: &FontRef, ids: &[skrifa::string::StringId], found: impl Fn(&str) -> bool) -> Option { + let mut left = MAX_NAME_BYTES; + for s in ids.iter().flat_map(|id| font.localized_strings(*id)) { + let text = s.to_string(); + if text.len() > left { + return None; + } + left -= text.len().max(1).min(left); + if found(&text) { + return Some(true); + } + } + Some(false) +} + /// Validate before allocating any PDF objects, so an invalid font cannot partly mutate a doc. pub fn validate(bytes: &[u8]) -> Result<(), String> { if bytes.len() > 16 << 20 { @@ -373,6 +392,17 @@ mod tests { assert_eq!(face_key("ABCDEF+Droid-Sans-Fallback"), face_key("Droid Sans Fallback")); } + #[test] + fn licence_policy_reads_name_records_one_at_a_time() { + use skrifa::string::StringId; + let bytes = include_bytes!("../../../assets/fonts/Inter-Regular.ttf"); + assert!(validate_document_input(bytes).is_ok(), "an OFL font passes"); + let face = FontRef::new(bytes).unwrap(); + assert_eq!(any_name_record(&face, &[StringId::LICENSE_DESCRIPTION], |t| t.contains("SIL Open Font")), Some(true)); + assert_eq!(any_name_record(&face, &[StringId::LICENSE_DESCRIPTION], |_| false), Some(false)); + assert_eq!(any_name_record(&face, &[], |_| true), Some(false), "no ids, no records"); + } + #[test] fn embedding_the_same_text_again_reuses_its_resource_and_program() { let bytes = include_bytes!("../../../assets/fonts/Inter-Regular.ttf"); diff --git a/crates/fonts/src/lib.rs b/crates/fonts/src/lib.rs index 38292245cc..fbc291dbef 100644 --- a/crates/fonts/src/lib.rs +++ b/crates/fonts/src/lib.rs @@ -32,7 +32,8 @@ pub use script::{ script_missing, script_outline, }; pub use shaping::{ - ShapedCluster, ShapedScript, arabic_glyph, arabic_has, face_glyph, hebrew_has, shape_arabic, shape_hebrew, shape_thai, shaped_glyph, thai_has, + ShapedCluster, ShapedScript, arabic_glyph, arabic_has, face_glyph, hebrew_has, latin_has, shape_arabic, shape_hebrew, shape_latin, shape_thai, + shaped_glyph, thai_has, }; pub use std14::{Match as Std14Match, Std14}; diff --git a/crates/fonts/src/pdf.rs b/crates/fonts/src/pdf.rs index 7ae75ddf31..4ff8feb722 100644 --- a/crates/fonts/src/pdf.rs +++ b/crates/fonts/src/pdf.rs @@ -75,6 +75,10 @@ pub struct Metrics { /// read that fails without one, such as an empty stream behind a filter nothing decodes, /// doesn't set it, with or without anything left. pub past_allowance: bool, + /// A codespace range of the embedded CMap was dropped (not one to four bytes, or past the range + /// limit): how the font's codes split into characters is then a guess, and a caller that must + /// know which text a string holds (redaction) refuses to rely on it. + pub codespaces_dropped: bool, } fn nums(doc: &Document, o: Option<&Object>) -> Vec { @@ -838,18 +842,22 @@ pub fn base_font_name(doc: &Document, font: &Dict) -> String { /// Codespace ranges and CID mappings from an embedded CMap stream, at most /// [`MAX_CMAP_RANGES`] of each (every glyph searches them). #[allow(clippy::type_complexity)] -fn parse_cmap(data: &[u8]) -> (Vec<(usize, Vec, Vec)>, Vec<(u32, u32, u32)>) { +fn parse_cmap(data: &[u8]) -> (Vec<(usize, Vec, Vec)>, Vec<(u32, u32, u32)>, bool) { let (mut spaces, mut cids) = (Vec::new(), Vec::new()); + let mut dropped = false; + let mut seen = HashSet::new(); let mut toks = Lexer::new(data); while let Some(t) = toks.next() { match t { Tok::Word(b"begincodespacerange") => { while let Some([lo, hi]) = entry(&mut toks, b"endcodespacerange") { - if let (Tok::Hex(Some(lo)), Tok::Hex(Some(hi))) = (lo, hi) - && lo.len() == hi.len() - && spaces.len() < MAX_CMAP_RANGES - { - spaces.push((lo.len(), lo, hi)); + if let (Tok::Hex(Some(lo)), Tok::Hex(Some(hi))) = (lo, hi) { + // A code is at most four bytes; a repeated range can never match first, so it only costs every glyph a comparison. + if lo.len() != hi.len() || !(1..=4).contains(&lo.len()) || spaces.len() >= MAX_CMAP_RANGES { + dropped = true; + } else if seen.insert((lo.clone(), hi.clone())) { + spaces.push((lo.len(), lo, hi)); + } } } } @@ -875,7 +883,7 @@ fn parse_cmap(data: &[u8]) -> (Vec<(usize, Vec, Vec)>, Vec<(u32, u32, u3 } } spaces.sort_by_key(|s| s.0); - (spaces, cids) + (spaces, cids, dropped) } impl Metrics { @@ -905,6 +913,7 @@ impl Metrics { explicit_reverse: (32..127u8).map(|c| (char::from(c), u32::from(c))).collect(), outlined_cids: Vec::new(), past_allowance: false, + codespaces_dropped: false, } } @@ -935,7 +944,7 @@ impl Metrics { m.code_len = match &m.codes { Codes::One => 1, Codes::Two => 2, - Codes::Ranges(r) => r.last().map_or(2, |x| x.0), + Codes::Ranges(r) => r.last().map_or(2, |x| x.0.clamp(1, 4)), }; m.unicode = unicode_map(doc, font, m.composite, &mut reads); if m.std14.is_some() { @@ -977,7 +986,8 @@ impl Metrics { && let Object::Stream(s) = &*e && let Some(data) = reads.decode(s, MAX_CMAP_BYTES) { - let (spaces, cids) = parse_cmap(&data); + let (spaces, cids, dropped) = parse_cmap(&data); + m.codespaces_dropped = dropped; if !spaces.is_empty() { m.codes = Codes::Ranges(spaces); } diff --git a/crates/fonts/src/pdf/tests.rs b/crates/fonts/src/pdf/tests.rs index 74880c94a4..e85870d4a7 100644 --- a/crates/fonts/src/pdf/tests.rs +++ b/crates/fonts/src/pdf/tests.rs @@ -1557,7 +1557,7 @@ fn cmap_tokens_need_no_space_between_them() { let mut got: Vec<_> = map.iter().map(|(c, u)| (*c, u.as_str())).collect(); got.sort_unstable(); assert_eq!(got, [(3, " "), (0x10, "é"), (0x24, "A"), (0x25, "B"), (0x26, "C"), (0x30, "H"), (0x31, "i")]); - let (spaces, cids) = parse_cmap(b"1 begincodespacerange<00>endcodespacerange 1 begincidrange<20><7E>1 endcidrange"); + let (spaces, cids, _) = parse_cmap(b"1 begincodespacerange<00>endcodespacerange 1 begincidrange<20><7E>1 endcidrange"); assert_eq!(spaces, [(1, vec![0], vec![0xff])]); assert_eq!(cids, [(0x20, 0x7e, 1)]); } @@ -1823,6 +1823,16 @@ fn low_codes_resolve_to_the_covering_range() { assert_eq!((u.covering(0xFFFF).is_some(), u.covering(0x1_0001).is_some(), u.covering(0x1_0002).is_none()), (true, true, true)); } +#[test] +fn codespaces_are_deduplicated_and_at_most_four_bytes() { + let cmap = "3 begincodespacerange <00> <7F> <00> <7F> <0000000000> endcodespacerange"; + let (spaces, ..) = parse_cmap(cmap.as_bytes()); + assert_eq!(spaces, vec![(1, vec![0x00], vec![0x7F])]); + let many: String = (0..2000).map(|_| "<0000> \n").collect(); + let (spaces, ..) = parse_cmap(format!("2000 begincodespacerange\n{many}endcodespacerange").as_bytes()); + assert_eq!(spaces.len(), 1); +} + #[test] fn base_font_name_matches_the_metrics_name() { let doc = Document::new_empty(); @@ -2146,3 +2156,12 @@ fn a_full_cid_map_rebuilds_a_composite_subsets_cmap_behind_two_filters() { let editor = embedded_sfnt(&doc, &source).expect("the map behind two filters is read"); assert_eq!(skrifa::FontRef::new(&editor).unwrap().charmap().map('Ω'), Some(expected)); } + +/// A codespace range of five bytes, or one whose ends differ in length, can't be kept and is said +/// to be dropped; a repeated range is not a loss. +#[test] +fn a_dropped_codespace_range_is_reported() { + assert!(parse_cmap(b"1 begincodespacerange <0000000000> endcodespacerange").2); + assert!(parse_cmap(b"1 begincodespacerange <00> endcodespacerange").2); + assert!(!parse_cmap(b"2 begincodespacerange <0000> <0000> endcodespacerange").2); +} diff --git a/crates/fonts/src/shaped_text.rs b/crates/fonts/src/shaped_text.rs index c85de92f64..2a4625f556 100644 --- a/crates/fonts/src/shaped_text.rs +++ b/crates/fonts/src/shaped_text.rs @@ -9,7 +9,7 @@ use std::collections::{HashMap, HashSet}; use pdfcraft_cos::{Dict, Document, Object, PdfString, Stream}; -use crate::{GlyphError, GlyphOutline, ShapedCluster, ShapedScript, literal, shape_thai, shaped_glyph}; +use crate::{GlyphError, GlyphOutline, ShapedCluster, ShapedScript, latin_has, literal, shape_latin, shape_thai, shaped_glyph}; /// Most glyphs in one Type 3 font: its codes are single bytes, 1–240. Text gets one such font per /// 240 distinct clusters it needs. @@ -37,6 +37,93 @@ pub fn thai_message(e: GlyphError, text: &str) -> String { } } +/// Whether a generated appearance draws `c` from the bundled Inter face rather than the standard +/// font: a Latin, Greek or Cyrillic character WinAnsiEncoding lacks (`Γ`, `ř`, `ж`) that the face +/// has. Line breaks and Thai never are. +pub fn latin_fallback(c: char) -> bool { + !matches!(c, '\n' | '\r') && !is_thai(c) && !crate::win_ansi_encodable(c) && latin_has(c) +} + +/// Why the bundled Inter face can't draw `text`, for a message an agent or user can act on. +pub fn latin_message(e: GlyphError, text: &str) -> String { + match e { + GlyphError::NoFont | GlyphError::Missing => format!("the bundled Inter face can't show \"{text}\""), + GlyphError::TooComplex => format!("the bundled Inter face's glyphs for \"{text}\" are too complex"), + } +} + +/// `line` split as a generated appearance draws it, in typing order: with `thai`, its Thai runs +/// shaped with the Thai face; with `latin`, its runs of [`latin_fallback`] characters shaped with +/// the bundled Inter face; and the text between them, left to the standard font. An error when a +/// run can't be shaped (no Thai face, a missing character). +pub fn fallback_pieces(line: &str, thai: bool, latin: bool) -> Result, GlyphError> { + #[derive(Clone, Copy, PartialEq)] + enum Run { + Thai, + Latin, + Standard, + } + let kind = |c: char| { + if thai && is_thai(c) { + Run::Thai + } else if latin && latin_fallback(c) { + Run::Latin + } else { + Run::Standard + } + }; + let mut runs: Vec<(Run, String)> = Vec::new(); + for c in line.chars() { + let k = kind(c); + // A combining mark the standard font lacks is drawn on its letter by the bundled face: + // the letter moves from the standard font's run into a run of the face's. + if k == Run::Latin + && is_mark(c) + && let Some((Run::Standard, s)) = runs.last_mut() + && let Some(base) = s.chars().last().filter(|b| latin_has(*b) && !matches!(b, '\n' | '\r')) + { + s.pop(); + if s.is_empty() { + runs.pop(); + } + match runs.last_mut() { + Some((Run::Latin, s)) => s.extend([base, c]), + _ => runs.push((Run::Latin, [base, c].into_iter().collect())), + } + continue; + } + match runs.last_mut() { + Some((run, s)) if *run == k => s.push(c), + _ => runs.push((k, c.to_string())), + } + } + runs.into_iter() + .map(|(run, s)| match run { + Run::Thai => shape_thai(&s).map(Piece::Shaped), + Run::Latin => shape_latin(&s).map(Piece::Shaped), + Run::Standard => Ok(Piece::Latin(s)), + }) + .collect() +} + +/// A combining diacritical mark (U+0300–U+036F), drawn on the letter before it. +fn is_mark(c: char) -> bool { + matches!(u32::from(c), 0x0300..=0x036F) +} + +/// The width of `text` at `size` as [`fallback_pieces`] splits it, the standard font's pieces +/// measured by `standard`. Text with nothing for the fallback faces, or that they can't shape, is +/// measured by `standard` as a whole (drawing then reports why). +pub fn fallback_width(text: &str, size: f64, thai: bool, latin: bool, standard: impl Fn(&str, f64) -> f64) -> f64 { + if !text.chars().any(|c| (thai && is_thai(c)) || (latin && latin_fallback(c))) { + return standard(text, size); + } + match fallback_pieces(text, thai, latin) { + Ok(pieces) => pieces_width(&pieces, size, 0.0, standard), + Err(_) => standard(text, size), + } +} + /// A piece of a line of shaped text, in drawing order. #[derive(Clone, Debug, PartialEq)] pub enum Piece { @@ -243,10 +330,21 @@ impl ShapedFonts { /// appearance. Thai the face can't draw, and every line when a glyph can't be read, is left to the /// standard font, whose encoder writes `?` for it; [`first_undrawable`] reports that. pub fn appearance_lines(lines: &[String], fonts: &mut Dict) -> (Vec>, ShapedFonts) { + fallback_appearance_lines(lines, true, false, fonts) +} + +/// [`appearance_lines`] with the pieces [`fallback_pieces`] splits each line into: its Thai with +/// `thai`, and with `latin` the Latin, Greek and Cyrillic characters the standard fonts lack, drawn +/// from the bundled Inter face. Their Type 3 fonts are named `PCTh…` when Thai is drawn, else +/// `PCLa…`. A line whose runs can't be shaped, and every line when a glyph can't be read, is left +/// to the standard font, which writes `?` for what it lacks; the caller reports that. +pub fn fallback_appearance_lines(lines: &[String], thai: bool, latin: bool, fonts: &mut Dict) -> (Vec>, ShapedFonts) { let plain = |l: &String| vec![Piece::Latin(l.clone())]; + let needs = |c: char| (thai && is_thai(c)) || (latin && latin_fallback(c)); let pieces: Vec> = - lines.iter().map(|l| if l.chars().any(is_thai) { thai_pieces(l).unwrap_or_else(|_| plain(l)) } else { plain(l) }).collect(); - match ShapedFonts::new(clusters(pieces.iter().map(Vec::as_slice)), shaped_glyph, &Dict::new(), fonts, "PCTh", &mut |o| o).map_err(|(e, _)| e) { + lines.iter().map(|l| if l.chars().any(needs) { fallback_pieces(l, thai, latin).unwrap_or_else(|_| plain(l)) } else { plain(l) }).collect(); + let prefix = if thai { "PCTh" } else { "PCLa" }; + match ShapedFonts::new(clusters(pieces.iter().map(Vec::as_slice)), shaped_glyph, &Dict::new(), fonts, prefix, &mut |o| o).map_err(|(e, _)| e) { Ok(shaped) => (pieces, shaped), Err(_) => (lines.iter().map(plain).collect(), ShapedFonts::default()), } @@ -361,6 +459,7 @@ fn type3_font(clusters: &[&ShapedCluster], outlines: &HashMap<(ShapedScript, u32 ShapedScript::Arabic => "Arabic", ShapedScript::Hebrew => "Hebrew", ShapedScript::Thai => "Thai", + ShapedScript::Latin => "Inter", }, |f| f.family, ), @@ -487,6 +586,44 @@ mod tests { assert_eq!((stored, fonts.iter().count()), (0, 0)); } + /// #6822: Greek, and the Latin and Cyrillic letters WinAnsiEncoding lacks, are shaped with the + /// bundled Inter face, which every build has; what WinAnsi holds stays the standard font's. + #[test] + fn letters_the_standard_fonts_lack_are_shaped_with_the_bundled_face() { + assert!(latin_fallback('Γ') && latin_fallback('ř') && latin_fallback('ж')); + assert!(!latin_fallback('é') && !latin_fallback('a') && !latin_fallback('\n') && !latin_fallback('北') && !latin_fallback('ก')); + // Text WinAnsi holds is one piece for the standard font, measured by it. + assert_eq!(fallback_pieces("Café (±)", false, true).unwrap(), [Piece::Latin("Café (±)".into())]); + assert_eq!(fallback_width("Café", 10.0, false, true, |s, size| s.chars().count() as f64 * size), 40.0); + // Greek words, with the standard font's space between them. + let pieces = fallback_pieces("Γειά σου", false, true).unwrap(); + assert!( + matches!(&pieces[..], [Piece::Shaped(a), Piece::Latin(sp), Piece::Shaped(b)] if sp == " " && a.len() == 4 && b.len() == 3), + "{pieces:?}" + ); + let text: String = clusters([pieces.as_slice()]).map(|c| c.text.as_str()).collect(); + assert_eq!(text, "Γειάσου", "each character copies once"); + assert!(clusters([pieces.as_slice()]).all(|c| c.script == ShapedScript::Latin && c.advance > 0.0)); + assert!(fallback_width("Γειά", 10.0, false, true, |_, _| 0.0) > 0.0); + // Mixed: the Latin stays the standard font's. + let mixed = fallback_pieces("Name: Γειά ok", false, true).unwrap(); + assert!(matches!(&mixed[..], [Piece::Latin(a), Piece::Shaped(_), Piece::Latin(b)] if a == "Name: " && b == " ok"), "{mixed:?}"); + // A combining accent the standard font lacks takes its letter with it. + let accent = fallback_pieces("Cafe\u{301}!", false, true).unwrap(); + assert!( + matches!(&accent[..], [Piece::Latin(a), Piece::Shaped(c), Piece::Latin(b)] if a == "Caf" && b == "!" && c.iter().map(|c| c.text.as_str()).collect::() == "e\u{301}"), + "{accent:?}" + ); + // Without `latin`, nothing changes. + assert_eq!(fallback_pieces("Γειά", false, false).unwrap(), [Piece::Latin("Γειά".into())]); + // The fonts are built under PCLa names, and a character no face here has stays `?`. + let mut fonts = Dict::new(); + let (lines, shaped) = fallback_appearance_lines(&["Γειά 北".to_string()], false, true, &mut fonts); + assert!(fonts.iter().any(|(k, _)| k.starts_with(b"PCLa"))); + assert!(matches!(&lines[0][..], [Piece::Shaped(c), Piece::Latin(rest)] if shaped.code(&c[0]).is_some() && rest == " 北"), "{lines:?}"); + assert_eq!(first_undrawable_in_line(" 北"), Some('北')); + } + #[test] fn thai_runs_are_shaped_and_the_rest_left_to_the_standard_font() { assert_eq!(thai_pieces("Hello").unwrap(), [Piece::Latin("Hello".into())]); diff --git a/crates/fonts/src/shaping.rs b/crates/fonts/src/shaping.rs index 54a401875f..c90af6bd67 100644 --- a/crates/fonts/src/shaping.rs +++ b/crates/fonts/src/shaping.rs @@ -28,6 +28,10 @@ pub enum ShapedScript { Arabic, Hebrew, Thai, + /// Latin, Greek and Cyrillic letters in the bundled Inter face + /// ([`crate::document_latin_fonts`]), which every build has: for the ones WinAnsiEncoding + /// lacks, beside the standard font that draws the rest. + Latin, } impl ShapedScript { @@ -36,6 +40,7 @@ impl ShapedScript { Self::Arabic => crate::document_arabic_font(), Self::Hebrew => crate::document_hebrew_font(false), Self::Thai => crate::document_thai_font(), + Self::Latin => crate::document_latin_fonts(false).into_iter().next(), } } } @@ -44,10 +49,12 @@ fn face(script: ShapedScript) -> Option<&'static (FontRef<'static>, ShaperData)> static FACE: OnceLock, ShaperData)>> = OnceLock::new(); static HEBREW: OnceLock, ShaperData)>> = OnceLock::new(); static THAI: OnceLock, ShaperData)>> = OnceLock::new(); + static LATIN: OnceLock, ShaperData)>> = OnceLock::new(); let cache = match script { ShapedScript::Arabic => &FACE, ShapedScript::Hebrew => &HEBREW, ShapedScript::Thai => &THAI, + ShapedScript::Latin => &LATIN, }; cache .get_or_init(|| { @@ -91,6 +98,38 @@ pub fn shape_thai(text: &str) -> Result, GlyphError> { shape_script(text, false, ShapedScript::Thai) } +/// Whether the bundled Inter face has a glyph for `c`. +pub fn latin_has(c: char) -> bool { + face(ShapedScript::Latin).is_some_and(|(font, _)| font.charmap().map(c).is_some_and(|g| g.to_u32() != 0)) +} + +/// Shape a left-to-right run of Latin, Greek or Cyrillic `text` with the bundled Inter face, +/// accents and other marks kept with their letter. [`GlyphError::Missing`] when the face lacks a +/// character. +pub fn shape_latin(text: &str) -> Result, GlyphError> { + shape_script(text, false, ShapedScript::Latin) +} + +/// The scripts the bundled face's runs are shaped as. +#[derive(Clone, Copy, PartialEq)] +enum LatinRun { + Latin, + Greek, + Cyrillic, +} + +/// The script of a run for the bundled face: that of its first Greek or Cyrillic letter, else +/// Latin. +fn latin_run(text: &str) -> LatinRun { + text.chars() + .find_map(|c| match u32::from(c) { + 0x0370..=0x03FF | 0x1F00..=0x1FFF => Some(LatinRun::Greek), + 0x0400..=0x052F | 0x1C80..=0x1C8F | 0x2DE0..=0x2DFF | 0xA640..=0xA69F => Some(LatinRun::Cyrillic), + _ => None, + }) + .unwrap_or(LatinRun::Latin) +} + fn shape_script(text: &str, rtl: bool, kind: ShapedScript) -> Result, GlyphError> { let (font, data) = face(kind).ok_or(GlyphError::NoFont)?; let shaper = data.shaper(font).build(); @@ -102,11 +141,16 @@ fn shape_script(text: &str, rtl: bool, kind: ShapedScript) -> Result script::ARABIC, ShapedScript::Hebrew => script::HEBREW, ShapedScript::Thai => script::THAI, + ShapedScript::Latin => match latin_run(text) { + LatinRun::Greek => script::GREEK, + LatinRun::Cyrillic => script::CYRILLIC, + LatinRun::Latin => script::LATIN, + }, }); - // Preserve the historical Arabic per-character mapping; Hebrew and Thai marks stay with their base. + // Preserve the historical Arabic per-character mapping; Hebrew, Thai and Latin marks stay with their base. buffer.set_cluster_level(match kind { ShapedScript::Arabic => BufferClusterLevel::Characters, - ShapedScript::Hebrew | ShapedScript::Thai => BufferClusterLevel::MonotoneGraphemes, + ShapedScript::Hebrew | ShapedScript::Thai | ShapedScript::Latin => BufferClusterLevel::MonotoneGraphemes, }); let shaped = shaper.shape(buffer, ShapeOptions::new()); // Clusters are byte offsets into `text`; a cluster shows the characters up to the next one. diff --git a/crates/forms/README.md b/crates/forms/README.md index 73a25c503b..4d8a9b64d0 100644 --- a/crates/forms/README.md +++ b/crates/forms/README.md @@ -45,6 +45,14 @@ let cells: Vec = grid.cells_for(names)?; // "Table1_1", "Table several steps (filling, recalculating, the XFA data, the view), most with nothing changed in between, so it walks a form once (twice on a laid-out XFA form, whose data is written in between); `form_reads_on_this_thread` counts the walks, for tests. +- Automatic field detection considers the first 2,000 drawn rectangles in input order for + combs, check boxes and empty text boxes. Check-box label detection considers at most 2,000 + drawn/existing widget rectangles combined, before filtering or deduplication. Unusually + complex pages may therefore have partial proposals, returned in reading order; existing + widgets always exclude overlapping proposals, even beyond the label budget. A balanced + spatial index avoids repeated full scans, with a shared 128,000-step query budget. If + adversarial geometry exhausts it, unverified proposals are omitted, not accepted. As the input + supplies widget geometry only, any existing small square widget can reserve a nearby label. - Inheritable attributes (`/FT`, `/Ff`, `/V`, `/DV`, `/DA`, `/Q`, `/MaxLen`) are resolved down the field tree; the form's `/DA` and `/Q` are the last fallback. - Text and choice fields get new appearance streams (`/Tx BMC … EMC`): auto-size (`0 Tf`), comb diff --git a/crates/forms/src/appearance.rs b/crates/forms/src/appearance.rs index 91792a59c3..304ddecb9e 100644 --- a/crates/forms/src/appearance.rs +++ b/crates/forms/src/appearance.rs @@ -137,10 +137,17 @@ impl TextBox { return true; } let shown: String = if self.password { "*".repeat(text.chars().count()) } else { text.to_string() }; - // The font the appearance would choose for this text. - let can_show = |enc: &FontText| if self.multiline { shown.split(['\n', '\r']).all(|l| enc.can_encode(l)) } else { enc.can_encode(&shown) }; - let font = self.font.as_ref().filter(|enc| can_show(enc)).unwrap_or(&FontText::WinAnsi); - let width_of = |t: &str, size: f64| font.width(t, size); + // The font the appearance would choose for this text, and whether the bundled Inter face + // draws what neither it nor Helvetica holds (as `field_appearance_fit` chooses). + let holds = |enc: &FontText, s: &str, skip_latin: bool| { + if skip_latin { enc.can_encode(&s.chars().filter(|c| !shaped_text::latin_fallback(*c)).collect::()) } else { enc.can_encode(s) } + }; + let can_show = |enc: &FontText, skip_latin: bool| { + if self.multiline { shown.split(['\n', '\r']).all(|l| holds(enc, l, skip_latin)) } else { holds(enc, &shown, skip_latin) } + }; + let latin = !self.font.as_ref().is_some_and(|enc| can_show(enc, false)); + let font = self.font.as_ref().filter(|enc| can_show(enc, latin)).unwrap_or(&FontText::WinAnsi); + let width_of = |t: &str, size: f64| shaped_text::fallback_width(t, size, false, latin, |s, size| font.width(s, size)); // The smallest an automatic size goes ([`auto_text_size_by`]). let size = self.size.unwrap_or(4.0); if self.multiline { @@ -703,8 +710,10 @@ pub fn list_rows(doc: &Document, f: &Field, w: &Widget) -> ListRows { list_rows_of(doc, &f.da, fobj.as_dict(), &wd) } -/// The appearance of a text or choice field's widget showing `values`. Characters its font can't -/// hold come out as `?`; [`field_appearance_drawn`] says which. +/// The appearance of a text or choice field's widget showing `values`. Latin, Greek and Cyrillic +/// characters that neither its font nor Helvetica holds are drawn from the bundled Inter face +/// (Type 3 glyphs beside its font); characters no font here holds come out as `?`, and +/// [`field_appearance_drawn`] says which. pub fn field_appearance(doc: &Document, f: &Field, w: &Widget, values: &[String]) -> Stream { field_appearance_as(doc, f, w, values, true) } @@ -715,9 +724,11 @@ pub fn field_appearance_as(doc: &Document, f: &Field, w: &Widget, values: &[Stri field_appearance_drawn(doc, f, w, values, format).0 } -/// [`field_appearance_as`] with the first character it drew as `?` because the field's font can't -/// hold it: the form's font is a simple (WinAnsi) font, Helvetica stands in for one PdfCraft can't -/// write with (an embedded subset, `Identity-H`), or a UCS-2 CMap meets a character beyond U+FFFF. +/// [`field_appearance_as`] with the first character it drew as `?` because no font it draws with +/// holds it: the form's font is a simple (WinAnsi) font, or Helvetica stands in for one PdfCraft +/// can't write with (an embedded subset, `Identity-H`), and the character is not one the bundled +/// Inter face draws either (CJK, emoji, Thai without a Thai face); or a UCS-2 CMap meets a +/// character beyond U+FFFF. Greek such as "Γειά σου" is drawn, not reported (#6822). pub fn field_appearance_drawn(doc: &Document, f: &Field, w: &Widget, values: &[String], format: bool) -> (Stream, Option) { let (stream, unencodable, _) = field_appearance_fit(doc, f, w, values, format); (stream, unencodable) @@ -756,12 +767,18 @@ pub fn value_overflow(doc: &Document, f: &Field, w: &Widget) -> Option field_appearance_fit(doc, f, w, &f.value, true).2 } -/// Whether `text` draws in widget `wd` of `f` with no `?`: in the field's `/DR` font when that -/// holds it, as [`field_appearance_fit`] picks, or else in the Helvetica (WinAnsi) stand-in. +/// Whether `text` draws in widget `wd` of `f` with no `?`, as [`field_appearance_fit`] picks: in +/// the field's `/DR` font when that holds it, or else in that font or the Helvetica (WinAnsi) +/// stand-in with the bundled Inter face drawing the Latin, Greek and Cyrillic they lack. fn draws(doc: &Document, f: &Field, wd: &Dict, text: &str) -> bool { let da = parse_da(wd.get(b"DA").and_then(|o| doc.resolve(o).as_string().map(|s| s.to_text())).as_deref().unwrap_or(&f.da)); - let enc = dr_font(doc, &da.font).map(|(_, enc)| enc).filter(|enc| enc.can_encode(text)).unwrap_or(FontText::WinAnsi); - enc.first_unencodable(text).is_none() + let dr = dr_font(doc, &da.font).map(|(_, enc)| enc); + if let Some(enc) = dr.as_ref().filter(|enc| enc.can_encode(text)) { + return enc.first_unencodable(text).is_none(); + } + let rest: String = text.chars().filter(|c| !shaped_text::latin_fallback(*c)).collect(); + let enc = dr.filter(|enc| enc.can_encode(&rest)).unwrap_or(FontText::WinAnsi); + enc.first_unencodable(&rest).is_none() } /// Why text field `f` can't take `text`: it has Thai that a widget's font can't hold and this @@ -810,13 +827,17 @@ fn field_appearance_fit(doc: &Document, f: &Field, w: &Widget, values: &[String] values.first().cloned().unwrap_or_default() }; let text = if f.has(flags::PASSWORD) { "*".repeat(text.chars().count()) } else { text }; - let can_show = |enc: &FontText, text: &str| { + // Whether `enc` holds what is shown; with `skip_latin`, leaving out what the bundled face draws. + let can_show = |enc: &FontText, text: &str, skip_latin: bool| { + let holds = |s: &str| { + if skip_latin { enc.can_encode(&s.chars().filter(|c| !shaped_text::latin_fallback(*c)).collect::()) } else { enc.can_encode(s) } + }; if f.kind == FieldKind::List { - f.options.iter().all(|(_, display)| enc.can_encode(display)) + f.options.iter().all(|(_, display)| holds(display)) } else if f.kind == FieldKind::Text && f.has(flags::MULTILINE) { - text.split(['\n', '\r']).all(|line| enc.can_encode(line)) + text.split(['\n', '\r']).all(holds) } else { - enc.can_encode(text) + holds(text) } }; let dr = dr_font(doc, &da.font); @@ -826,15 +847,20 @@ fn field_appearance_fit(doc: &Document, f: &Field, w: &Widget, values: &[String] let thai = matches!(f.kind, FieldKind::Text | FieldKind::Combo) && !f.is_comb() && text.chars().any(shaped_text::is_thai) - && !dr.as_ref().is_some_and(|(_, enc)| matches!(enc, FontText::Simple(_)) && can_show(enc, &text)) + && !dr.as_ref().is_some_and(|(_, enc)| matches!(enc, FontText::Simple(_)) && can_show(enc, &text, false)) && shaped_text::thai_refusal(&text).is_none(); let latin_text: String = if thai { text.chars().filter(|c| !shaped_text::is_thai(*c)).collect() } else { text.clone() }; - let (font_name, font_obj, enc) = match dr.filter(|(_, enc)| can_show(enc, &latin_text)) { + // Latin, Greek and Cyrillic the form's font can't hold, nor Helvetica, are drawn from the + // bundled Inter face in the same way, beside the font that draws the rest (#6822): the value is + // stored as typed either way, and this keeps its appearance from showing `?`. + let latin = !dr.as_ref().is_some_and(|(_, enc)| can_show(enc, &latin_text, false)); + let (font_name, font_obj, enc) = match dr.filter(|(_, enc)| can_show(enc, &latin_text, latin)) { Some((o, enc)) => (da.font.clone(), o, enc), None => ("Helv".to_string(), helvetica(), FontText::WinAnsi), }; - let width_of = - |text: &str, size: f64| if thai { shaped_text::thai_width(text, size, |s, size| enc.width(s, size)) } else { enc.width(text, size) }; + let width_of = |text: &str, size: f64| { + if thai || latin { shaped_text::fallback_width(text, size, thai, latin, |s, size| enc.width(s, size)) } else { enc.width(text, size) } + }; // How far a line's ink reaches below and above its baseline (em), where Thai stacks or // descenders reach past the form font's (Helvetica's) -0.207 and 0.718; `None` otherwise. let thai_ink = |line: &str| thai.then(|| shaped_text::thai_reach(line)).flatten().map(|[lo, hi]| [lo.min(-0.207), hi.max(0.718)]); @@ -952,7 +978,7 @@ fn field_appearance_fit(doc: &Document, f: &Field, w: &Widget, values: &[String] } } } - let (body, fonts, unencodable) = draw_lines(&shown, size, &font_name, font_obj, &enc, thai); + let (body, fonts, unencodable) = draw_lines(&shown, size, &font_name, font_obj, &enc, thai, latin); // Acrobat regenerates only what is inside /Tx BMC when it redraws a field (e.g. on focus // changes), discarding what came before it (issues #1613, #633). The background and border // therefore go inside the marked-content block too, so a redraw keeps them with the text. @@ -982,15 +1008,24 @@ fn field_appearance_fit(doc: &Document, f: &Field, w: &Widget, values: &[String] } /// The text operators for the `shown` lines (where each starts, and what it says) at `size`, in -/// the field's font `font_name` (`font`, written with `enc`) and, with `thai`, their Thai from the -/// Thai face; the font resources they use, and the first character drawn as `?` because the -/// font can't hold it, for the caller to report. -fn draw_lines(shown: &[(f64, f64, String)], size: f64, font_name: &str, font: Object, enc: &FontText, thai: bool) -> (Vec, Dict, Option) { +/// the field's font `font_name` (`font`, written with `enc`); with `thai`, their Thai from the +/// Thai face, and with `latin`, the Latin, Greek and Cyrillic characters the standard fonts lack +/// from the bundled Inter face. Returns the font resources they use, and the first character +/// drawn as `?` because no font here holds it, for the caller to report. +fn draw_lines( + shown: &[(f64, f64, String)], + size: f64, + font_name: &str, + font: Object, + enc: &FontText, + thai: bool, + latin: bool, +) -> (Vec, Dict, Option) { let mut fonts = Dict::new(); fonts.set(font_name.as_bytes().to_vec(), font); let lines: Vec = shown.iter().map(|(_, _, text)| text.clone()).collect(); - let (pieces, shaped) = if thai { - shaped_text::appearance_lines(&lines, &mut fonts) + let (pieces, shaped) = if thai || latin { + shaped_text::fallback_appearance_lines(&lines, thai, latin, &mut fonts) } else { (lines.into_iter().map(|l| vec![Piece::Latin(l)]).collect(), ShapedFonts::default()) }; diff --git a/crates/forms/src/detect.rs b/crates/forms/src/detect.rs index 99f2447c02..4859793e94 100644 --- a/crates/forms/src/detect.rs +++ b/crates/forms/src/detect.rs @@ -49,6 +49,11 @@ pub struct Candidate { /// points along its sides (#2942: a cell drawn with a midpoint on each vertical side). const MAX_RECT_POINTS: usize = 16; +/// Maximum rectangle inputs examined for combs and boxes, and maximum combined drawn/existing +/// rectangle inputs examined for check-box labels. Count inputs before filtering or deduplication: +/// repeated or irrelevant rectangles must not defeat the bound on pairwise comparisons. +pub const MAX_DETECTION_BOXES: usize = 2000; + /// Graphics states saved (`q`) and kept at once while detecting; deeper saves are counted, not kept. const MAX_SAVES: usize = 256; @@ -335,30 +340,13 @@ fn clean(label: &str) -> String { s.chars().take(40).collect::().trim().to_string() } -/// The label for a field at `rect`: the words to its left on the same line, else the words +/// The label for a text field at `rect`: the words to its left on the same line, else the words /// just above it. -fn label_for(words: &[Word], rect: [f64; 4], right_of: bool) -> String { +fn label_for(words: &[Word], rect: [f64; 4]) -> String { let h = (rect[3] - rect[1]).max(8.0); let same_line = |w: &Word| (mid(w.rect) - mid(rect)).abs() < h.max(10.0) * 0.7; - let mut left: Vec<&Word> = if right_of { - words.iter().filter(|w| same_line(w) && w.rect[0] >= rect[2] - 1.0 && w.rect[0] - rect[2] < 200.0).collect() - } else { - words.iter().filter(|w| same_line(w) && w.rect[2] <= rect[0] + 1.0 && rect[0] - w.rect[2] < 250.0).collect() - }; + let mut left: Vec<&Word> = words.iter().filter(|w| same_line(w) && w.rect[2] <= rect[0] + 1.0 && rect[0] - w.rect[2] < 250.0).collect(); left.sort_by(|a, b| a.rect[0].total_cmp(&b.rect[0])); - if right_of { - // The words that follow the box, up to the next gap wider than a few spaces. - let mut picked = Vec::new(); - let mut x = rect[2]; - for w in left { - if w.rect[0] - x > 24.0 { - break; - } - picked.push(w.text.as_str()); - x = w.rect[2]; - } - return clean(&picked.join(" ")); - } // The words that end nearest the field, back to a wide gap. let mut picked: Vec<&str> = Vec::new(); let mut x = rect[0]; @@ -387,6 +375,119 @@ fn label_for(words: &[Word], rect: [f64; 4], right_of: bool) -> String { clean(&on.iter().map(|w| w.text.as_str()).collect::>().join(" ")) } +/// Sort items into reading order: top to bottom (rows within 8 pt of the row's first), left to right. +fn sort_reading_order(mut items: Vec, rect_of: impl Fn(&T) -> [f64; 4]) -> Vec { + items.sort_by(|a, b| mid(rect_of(b)).total_cmp(&mid(rect_of(a)))); + let mut rows = Vec::with_capacity(items.len()); + let (mut row, mut start) = (0usize, f64::INFINITY); + for item in &items { + if start - mid(rect_of(item)) > 8.0 { + row += 1; + start = mid(rect_of(item)); + } + rows.push(row); + } + let mut keyed: Vec<(usize, T)> = rows.into_iter().zip(items).collect(); + keyed.sort_by(|a, b| a.0.cmp(&b.0).then(rect_of(&a.1)[0].total_cmp(&rect_of(&b.1)[0]))); + keyed.into_iter().map(|(_, item)| item).collect() +} + +/// The label for a check box at `rect`: the nearer unclaimed phrase on the same line (to its +/// right or to its left), without crossing or stealing a phrase that sits closer to an adjacent +/// check box on the line. +fn checkbox_label(words: &[Word], rect: [f64; 4], boxes: &[[f64; 4]], used: &mut std::collections::HashSet) -> String { + let h = (rect[3] - rect[1]).max(8.0); + let same_line_word = |r: [f64; 4]| (mid(r) - mid(rect)).abs() < h.max(10.0) * 0.7; + let same_line_box = |b: [f64; 4]| { + #[cfg(test)] + work_bound_tests::record_comparison(); + (mid(b) - mid(rect)).abs() < h.max(b[3] - b[1]).max(10.0) * 0.7 + }; + let has_content = |w: &Word| !clean(&w.text).is_empty(); + let prev_box_x2 = + boxes.iter().copied().filter(|b| *b != rect && same_line_box(*b) && b[2] <= rect[0] + 1.0).map(|b| b[2]).fold(f64::NEG_INFINITY, f64::max); + let next_box_x0 = + boxes.iter().copied().filter(|b| *b != rect && same_line_box(*b) && b[0] >= rect[2] - 1.0).map(|b| b[0]).fold(f64::INFINITY, f64::min); + + let mut left_words: Vec<(usize, &Word)> = words + .iter() + .enumerate() + .filter(|(i, w)| { + !used.contains(i) && same_line_word(w.rect) && w.rect[2] <= rect[0] + 1.0 && rect[0] - w.rect[2] < 200.0 && w.rect[0] >= prev_box_x2 - 1.0 + }) + .collect(); + left_words.sort_by(|a, b| a.1.rect[0].total_cmp(&b.1.rect[0])); + let mut left_picked: Vec<(usize, &Word)> = Vec::new(); + let mut x = rect[0]; + let mut has_word = false; + for (i, w) in left_words.into_iter().rev() { + if x - w.rect[2] > 24.0 || w.text.chars().all(|c| c == '_') || (has_word && w.text.trim_end().ends_with(':')) { + break; + } + has_word |= has_content(w); + left_picked.push((i, w)); + x = w.rect[0]; + } + left_picked.reverse(); + let left_candidate = match (left_picked.iter().find(|(_, w)| has_content(w)), left_picked.iter().rev().find(|(_, w)| has_content(w))) { + (Some((_, first)), Some((_, last))) => { + let text = clean(&left_picked.iter().map(|(_, w)| w.text.as_str()).collect::>().join(" ")); + let gap = (rect[0] - last.rect[2]).max(0.0); + let prev_gap = (first.rect[0] - prev_box_x2).max(0.0); + let colon = left_picked.last().is_some_and(|(_, w)| w.text.trim_end().ends_with(':')) || last.text.trim_end().ends_with(':'); + (!text.is_empty() && gap <= prev_gap).then_some((text, gap, colon, left_picked)) + } + _ => None, + }; + + let mut right_words: Vec<(usize, &Word)> = words + .iter() + .enumerate() + .filter(|(i, w)| { + !used.contains(i) && same_line_word(w.rect) && w.rect[0] >= rect[2] - 1.0 && w.rect[0] - rect[2] < 200.0 && w.rect[2] <= next_box_x0 + 1.0 + }) + .collect(); + right_words.sort_by(|a, b| a.1.rect[0].total_cmp(&b.1.rect[0])); + let mut right_picked: Vec<(usize, &Word)> = Vec::new(); + let mut x = rect[2]; + let mut has_word = false; + for (i, w) in right_words { + if w.rect[0] - x > 24.0 || w.text.chars().all(|c| c == '_') || (has_word && (next_box_x0 - w.rect[2]).max(0.0) < w.rect[0] - x) { + break; + } + has_word |= has_content(w); + right_picked.push((i, w)); + x = w.rect[2]; + } + let right_candidate = match (right_picked.iter().find(|(_, w)| has_content(w)), right_picked.iter().rev().find(|(_, w)| has_content(w))) { + (Some((_, first)), Some((_, last))) => { + let text = clean(&right_picked.iter().map(|(_, w)| w.text.as_str()).collect::>().join(" ")); + let gap = (first.rect[0] - rect[2]).max(0.0); + let next_gap = (next_box_x0 - last.rect[2]).max(0.0); + (!text.is_empty() && gap <= next_gap).then_some((text, gap, next_gap, right_picked)) + } + _ => None, + }; + + let chosen = match (left_candidate, right_candidate) { + (Some((lt, l_gap, colon, l_words)), Some((rt, r_gap, next_gap, r_words))) => { + let pick_left = !colon && (l_gap < r_gap || (next_gap <= 24.0 && l_gap <= r_gap)); + if pick_left { Some((lt, l_words)) } else { Some((rt, r_words)) } + } + (Some((lt, _, _, l_words)), None) => Some((lt, l_words)), + (None, Some((rt, _, _, r_words))) => Some((rt, r_words)), + (None, None) => None, + }; + if let Some((text, picked)) = chosen { + for (i, _) in picked { + used.insert(i); + } + text + } else { + String::new() + } +} + /// Rows of two or more touching cells of one size, as (the row's rect, cell count). Two is /// enough: dates split into day and month groups of two cells. Cells are square or taller /// than wide; an empty table of wider cells is not a comb. @@ -404,6 +505,8 @@ fn combs(boxes: &[[f64; 4]]) -> Vec<([f64; 4], usize)> { .collect(); // `b` comes right after `a` in a row: same size, its left edge on a's right edge. let follows = |a: &[f64; 4], b: &[f64; 4]| { + #[cfg(test)] + work_bound_tests::record_comparison(); (b[0] - a[2]).abs() <= 1.0 && (b[1] - a[1]).abs() <= 0.5 && (b[3] - a[3]).abs() <= 0.5 && ((b[2] - b[0]) - (a[2] - a[0])).abs() <= 0.5 }; let mut out = Vec::new(); @@ -426,13 +529,95 @@ fn combs(boxes: &[[f64; 4]]) -> Vec<([f64; 4], usize)> { out } +/// A balanced bounding-volume tree indexes *all* existing widgets, independently of the +/// proposal/label budget. Far-away widgets can be excluded together instead of rescanning +/// the entire page for each proposal. Construction is O(n log n), with logarithmic depth. +struct WidgetIndex { + bounds: [f64; 4], + contents: WidgetContents, +} + +enum WidgetContents { + Leaf(Box<[[f64; 4]]>), + Branch(Box, Box), +} + +fn rectangles_overlap(a: [f64; 4], b: [f64; 4]) -> bool { + #[cfg(test)] + work_bound_tests::record_comparison(); + a[0] < b[2] - 1.0 && b[0] < a[2] - 1.0 && a[1] < b[3] - 1.0 && b[1] < a[3] - 1.0 +} + +impl WidgetIndex { + fn new(rects: &[[f64; 4]]) -> Option { + // A NaN makes the exact overlap predicate false. Infinities and inverted boxes are + // retained: pruning uses normalized bounds, but leaves preserve original geometry. + let mut rects: Vec<_> = rects.iter().copied().filter(|r| !r.iter().any(|x| x.is_nan())).collect(); + Self::build(&mut rects, 0) + } + + fn build(rects: &mut [[f64; 4]], depth: usize) -> Option { + let first = rects.first()?; + let mut bounds = [first[0].min(first[2]), first[1].min(first[3]), first[0].max(first[2]), first[1].max(first[3])]; + for r in rects.iter().skip(1) { + bounds = [bounds[0].min(r[0]).min(r[2]), bounds[1].min(r[1]).min(r[3]), bounds[2].max(r[0]).max(r[2]), bounds[3].max(r[1]).max(r[3])]; + } + let contents = if rects.len() <= 8 { + WidgetContents::Leaf(rects.to_vec().into_boxed_slice()) + } else { + let axis = depth % 2; + let mid = rects.len() / 2; + rects.select_nth_unstable_by(mid, |a, b| a[axis].total_cmp(&b[axis])); + let (left, right) = rects.split_at_mut(mid); + WidgetContents::Branch(Box::new(Self::build(left, depth + 1)?), Box::new(Self::build(right, depth + 1)?)) + }; + Some(Self { bounds, contents }) + } + + fn overlaps(&self, rect: [f64; 4], remaining: &mut usize) -> bool { + // Spatial indexes can degenerate on adversarial geometry. Exhaustion suppresses + // the unverified proposal, rather than overlooking any existing widget exclusion. + if *remaining == 0 { + return true; + } + *remaining -= 1; + #[cfg(test)] + work_bound_tests::record_index_visit(); + if !rectangles_overlap(self.bounds, rect) { + return false; + } + match &self.contents { + WidgetContents::Leaf(rects) => rects.iter().any(|r| { + if *remaining == 0 { + return true; + } + *remaining -= 1; + rectangles_overlap(*r, rect) + }), + WidgetContents::Branch(left, right) => left.overlaps(rect, remaining) || right.overlaps(rect, remaining), + } + } +} + /// Propose fields for a page. `existing` are the page's widget rectangles (no candidate /// overlaps them); names are unique against `taken`. +/// +/// Box detection considers only the first [`MAX_DETECTION_BOXES`] drawn rectangles in input +/// order, and check-box labelling considers at most that many drawn/existing rectangles combined. +/// Limits apply before pairwise comb/overlap/label work, not just to accepted candidates. This +/// heuristic can return a partial set on unusually complex pages, like raster shape detection; +/// the retained candidates are still returned in reading order. Every existing widget remains +/// an overlap exclusion, even when it falls outside the label-detection budget. pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[String]) -> Vec { + // Bound the comb pass too: small squares qualify as comb cells, so limiting only the new + // check-box loop would still leave quadratic work on all of a hostile page's rectangles. + let boxes = shapes.boxes.get(..shapes.boxes.len().min(MAX_DETECTION_BOXES)).unwrap_or_default(); let mut out: Vec = Vec::new(); - let overlaps = |a: [f64; 4], b: [f64; 4]| a[0] < b[2] - 1.0 && b[0] < a[2] - 1.0 && a[1] < b[3] - 1.0 && b[1] < a[3] - 1.0; - let push = |kind: Kind, rect: [f64; 4], label: String, out: &mut Vec| { - if existing.iter().chain(out.iter().map(|c| &c.rect)).any(|e| overlaps(*e, rect)) { + let overlaps = rectangles_overlap; + let existing_index = WidgetIndex::new(existing); + let mut existing_work = 64 * MAX_DETECTION_BOXES; + let mut push = |kind: Kind, rect: [f64; 4], label: String, out: &mut Vec| { + if existing_index.as_ref().is_some_and(|index| index.overlaps(rect, &mut existing_work)) || out.iter().any(|c| overlaps(c.rect, rect)) { return; } out.push(Candidate { kind, rect, name: label }); @@ -443,33 +628,45 @@ pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[S for b in blanks { let h = (b.rect[3] - b.rect[1]).max(10.0); let rect = [b.rect[0], b.rect[1], b.rect[2], b.rect[1] + (h * 1.4).clamp(12.0, 22.0)]; - let l = label_for(&labels, rect, false); + let l = label_for(&labels, rect); push(Kind::Text, rect, l, &mut out); } // Comb boxes (postcodes, dates, IBANs): one text field per row of cells, found before // check boxes so the cells don't each become one. - for (rect, cells) in combs(&shapes.boxes) { + for (rect, cells) in combs(boxes) { if labels.iter().any(|x| overlaps(x.rect, rect)) { continue; } - let l = label_for(&labels, rect, false); + let l = label_for(&labels, rect); push(Kind::Comb { cells }, rect, l, &mut out); } - // Small squares: check boxes, labelled by the text to their right. - for r in &shapes.boxes { + // Small squares: check boxes, labelled by the nearer text to their right or left. + // `existing` contains geometry only: a small square widget of any type reserves its label. + // Cap examined inputs, not accepted boxes, so duplicates cannot keep this pass running. + let mut checkboxes: Vec<[f64; 4]> = Vec::new(); + for r in boxes.iter().chain(existing.iter()).take(MAX_DETECTION_BOXES) { let (w, h) = (r[2] - r[0], r[3] - r[1]); - if (5.0..=20.0).contains(&w) && (5.0..=20.0).contains(&h) && (w / h - 1.0).abs() < 0.25 { - let l = label_for(&labels, *r, true); - push(Kind::CheckBox, *r, l, &mut out); + if (5.0..=20.0).contains(&w) + && (5.0..=20.0).contains(&h) + && (w / h - 1.0).abs() < 0.25 + && !out.iter().map(|c| &c.rect).chain(checkboxes.iter()).any(|e| overlaps(*e, *r)) + { + checkboxes.push(*r); } } + let checkboxes = sort_reading_order(checkboxes, |r| *r); + let mut used_words = std::collections::HashSet::new(); + for r in &checkboxes { + let l = checkbox_label(&labels, *r, &checkboxes, &mut used_words); + push(Kind::CheckBox, *r, l, &mut out); + } // Empty boxes big enough to write in: text fields inside them. - for r in &shapes.boxes { + for r in boxes { let (w, h) = (r[2] - r[0], r[3] - r[1]); let has_text = labels.iter().any(|x| overlaps(x.rect, *r)); if w >= 40.0 && (12.0..=60.0).contains(&h) && !has_text { let inner = [r[0] + 1.0, r[1] + 1.0, r[2] - 1.0, r[3] - 1.0]; - let l = label_for(&labels, *r, false); + let l = label_for(&labels, *r); push(Kind::Text, inner, l, &mut out); } } @@ -484,26 +681,14 @@ pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[S if labels.iter().any(|x| overlaps(x.rect, rect)) { continue; } - let l = label_for(&labels, rect, false); + let l = label_for(&labels, rect); push(Kind::Text, rect, l, &mut out); } - // Reading order: top to bottom, left to right. - // Rows: fields whose middles are within 8 pt of the row's first. - out.sort_by(|a, b| mid(b.rect).total_cmp(&mid(a.rect))); - let mut rows = Vec::with_capacity(out.len()); - let (mut row, mut start) = (0usize, f64::INFINITY); - for c in &out { - if start - mid(c.rect) > 8.0 { - row += 1; - start = mid(c.rect); - } - rows.push(row); - } - let mut keyed: Vec<(usize, Candidate)> = rows.into_iter().zip(out).collect(); - keyed.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.rect[0].total_cmp(&b.1.rect[0]))); - let mut out: Vec = keyed.into_iter().map(|(_, c)| c).collect(); + let mut out = sort_reading_order(out, |c| c.rect); // Unique names (Acrobat numbers repeats); unlabelled fields get Text1, Check Box1, … - let mut used: Vec = taken.to_vec(); + // A linear membership scan inside each suffix retry makes a page of unnamed squares + // cubic even after bounding the geometric passes. + let mut used: std::collections::HashSet = taken.iter().cloned().collect(); for c in &mut out { let base = if c.name.is_empty() { match c.kind { @@ -519,15 +704,135 @@ pub fn detect(words: &[Word], shapes: &Shapes, existing: &[[f64; 4]], taken: &[S n += 1; name = format!("{base}{n}"); } - used.push(name.clone()); + used.insert(name.clone()); c.name = name; } out } #[cfg(test)] -mod comb_tests { - use super::combs; +mod work_bound_tests { + use super::*; + use std::cell::Cell; + + // Per-thread so the other detection tests can run concurrently. Count work rather than + // wall time: appending more hostile rectangles must not add pairwise comparisons. + thread_local! { + static COMPARISONS: Cell = const { Cell::new(0) }; + static INDEX_VISITS: Cell = const { Cell::new(0) }; + } + + pub(super) fn record_comparison() { + COMPARISONS.set(COMPARISONS.get() + 1); + } + + pub(super) fn record_index_visit() { + INDEX_VISITS.set(INDEX_VISITS.get() + 1); + } + + #[test] + fn rectangle_budget_bounds_comparisons_before_filtering() { + for duplicates in [false, true] { + for existing_only in [false, true] { + let boxes = (0..MAX_DETECTION_BOXES) + .map(|i| { + let x = if duplicates { 0.0 } else { i as f64 * 30.0 }; + [x, 0.0, x + 10.0, 10.0] + }) + .collect(); + let mut shapes = Shapes { boxes, rules: Vec::new() }; + let run = |shapes: &Shapes| { + if existing_only { detect(&[], &Shapes::default(), &shapes.boxes, &[]) } else { detect(&[], shapes, &[], &[]) } + }; + COMPARISONS.set(0); + let expected = run(&shapes); + let bounded = COMPARISONS.get(); + assert!(bounded > 0 && bounded <= 5 * MAX_DETECTION_BOXES * MAX_DETECTION_BOXES); + shapes.boxes.extend_from_within(..); + COMPARISONS.set(0); + assert_eq!(run(&shapes), expected); + if existing_only { + // Existing widgets are never discarded: doubling them may add tree levels, + // but must not restore the full pairwise scan. + assert!(COMPARISONS.get() <= bounded + 16 * MAX_DETECTION_BOXES); + } else { + assert_eq!(COMPARISONS.get(), bounded, "discarded inputs add no pairwise work"); + } + } + } + } + + #[test] + fn many_existing_widgets_do_not_multiply_proposal_work() { + let mut existing: Vec<_> = (0..100_000) + .map(|i| { + let x = i as f64 * 30.0; + [x, 100.0, x + 10.0, 110.0] + }) + .collect(); + let shapes = Shapes { + boxes: (0..MAX_DETECTION_BOXES) + .map(|i| { + let x = i as f64 * 30.0; + [x, 0.0, x + 10.0, 10.0] + }) + .collect(), + rules: Vec::new(), + }; + // An exclusion beyond both budgets must still suppress the last drawn candidate. + let exclusion = [59_970.0, 0.0, 59_980.0, 10.0]; + existing.push(exclusion); + COMPARISONS.set(0); + let expected = detect(&[], &shapes, &[exclusion], &[]); + let baseline_work = COMPARISONS.get(); + COMPARISONS.set(0); + INDEX_VISITS.set(0); + let candidates = detect(&[], &shapes, &existing, &[]); + assert_eq!(candidates.len(), MAX_DETECTION_BOXES - 1); + assert_eq!(candidates, expected); + assert!(!candidates.iter().any(|c| c.rect == [59_970.0, 0.0, 59_980.0, 10.0])); + assert!(COMPARISONS.get() <= baseline_work + 64 * MAX_DETECTION_BOXES, "existing-widget comparisons must not be candidates times widgets"); + assert!(INDEX_VISITS.get() < 100 * MAX_DETECTION_BOXES); + } + + #[test] + fn widget_index_matches_linear_overlap_semantics() { + let mut rects = + vec![[0.0, 0.0, 10.0, 10.0], [30.0, 20.0, 20.0, 10.0], [f64::NEG_INFINITY, 50.0, f64::INFINITY, 60.0], [f64::NAN, 0.0, 10.0, 10.0]]; + rects.extend((0..500).map(|i| { + let x = (i % 25) as f64 * 3.0; + let y = (i / 25) as f64 * 4.0; + [x, y, x + 2.0, y + 2.0] + })); + let index = WidgetIndex::new(&rects).expect("nonempty index"); + for x in -5..40 { + for y in -5..70 { + let query = [x as f64, y as f64, x as f64 + 12.0, y as f64 + 12.0]; + let mut remaining = usize::MAX; + assert_eq!(index.overlaps(query, &mut remaining), rects.iter().any(|r| rectangles_overlap(*r, query))); + } + } + } + + #[test] + fn adversarial_widget_geometry_exhausts_budget_without_accepting_unchecked_fields() { + let existing = vec![[1e9, 1e9, -1e9, -1e9]; 100_000]; + let shapes = Shapes { + boxes: (0..MAX_DETECTION_BOXES) + .map(|i| { + let x = i as f64 * 30.0; + [x, 0.0, x + 10.0, 10.0] + }) + .collect(), + rules: Vec::new(), + }; + COMPARISONS.set(0); + INDEX_VISITS.set(0); + let candidates = detect(&[], &shapes, &existing, &[]); + assert!(candidates.is_empty(), "an exhausted search cannot verify any proposal"); + assert!(INDEX_VISITS.get() <= 64 * MAX_DETECTION_BOXES); + assert!(COMPARISONS.get() < 5 * MAX_DETECTION_BOXES * MAX_DETECTION_BOXES); + } #[test] fn repeated_boxes_do_not_change_or_multiply_a_comb() { diff --git a/crates/forms/src/tests.rs b/crates/forms/src/tests.rs index 1e6a18582c..4d859417a0 100644 --- a/crates/forms/src/tests.rs +++ b/crates/forms/src/tests.rs @@ -1260,6 +1260,83 @@ fn thai_a_field_font_cannot_hold_is_drawn_from_the_thai_face() { } } +/// #6822: Greek typed into a field whose font (Helvetica, WinAnsi) can't hold it is stored as typed +/// (a UTF-16BE text string) and drawn from the bundled Inter face, beside the field's font, rather +/// than as `?`: nothing is marked and other viewers aren't asked to redraw. Latin beside it stays +/// in the field's font, and text WinAnsi holds is drawn in it alone, as before. +#[test] +fn greek_a_field_font_cannot_hold_is_drawn_from_the_bundled_face() { + let objs: Vec = vec![ + "<< /Type /Catalog /Pages 2 0 R /AcroForm 4 0 R >>".into(), + "<< /Type /Pages /Kids [3 0 R] /Count 1 /MediaBox [0 0 600 800] >>".into(), + "<< /Type /Page /Parent 2 0 R /Annots [6 0 R 7 0 R 8 0 R] >>".into(), + "<< /Fields [6 0 R 7 0 R 8 0 R] /DA (/Helv 0 Tf 0 g) /DR << /Font << /Helv 5 0 R >> >> >>".into(), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".into(), + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (greek) /DA (/Helv 10 Tf 0 g) /Rect [50 700 250 720] /P 3 0 R >>".into(), + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (mixed) /Ff 4096 /DA (/Helv 10 Tf 0 g) /Rect [50 600 350 660] /P 3 0 R >>".into(), + "<< /Type /Annot /Subtype /Widget /FT /Tx /T (western) /DA (/Helv 10 Tf 0 g) /Rect [50 500 250 520] /P 3 0 R >>".into(), + ]; + let mut doc = document(&objs); + let greek = "Γειά σου"; + let mixed = "Name: Γειά\nσου ok"; + let western = "Café (±)"; + set_value(&mut doc, "greek", &FieldValue::Text(greek.into())).unwrap(); + set_value(&mut doc, "mixed", &FieldValue::Text(mixed.into())).unwrap(); + set_value(&mut doc, "western", &FieldValue::Text(western.into())).unwrap(); + let doc = reopen(&doc); + // The values, as typed: /V is UTF-16BE behind its byte order mark. + for (name, typed) in [("greek", greek), ("mixed", mixed), ("western", western)] { + let all = fields(&doc); + let f = field(&all, name); + assert_eq!(f.value, [typed]); + let v = doc.get(f.obj).as_dict().unwrap().get(b"V").unwrap().as_string().unwrap().bytes.clone(); + let utf16: Vec = [0xFE, 0xFF].into_iter().chain(typed.encode_utf16().flat_map(u16::to_be_bytes)).collect(); + assert_eq!(v, utf16, "{name}"); + } + assert!(undrawn_fields(&doc).is_empty(), "{:?}", undrawn_fields(&doc)); + assert!(!need_appearances(&doc), "no viewer needs to redraw what is drawn"); + // The normal appearance's content and its font resources. + let drawn = |name: &str| { + let all = fields(&doc); + let w = &field(&all, name).widgets[0]; + let wd = doc.get(w.obj).as_dict().unwrap().clone(); + assert_eq!(pdfcraft_fonts::undrawn_mark(&wd), None, "{name}"); + assert!(wd.get(b"DA").and_then(|o| o.as_string()).is_some_and(|s| s.to_text().starts_with("/Helv ")), "the /DA is kept"); + let n = wd.get(b"AP").unwrap().as_dict().unwrap().reference(b"N").unwrap(); + let Object::Stream(s) = &*doc.get(n) else { panic!() }; + let fonts = s.dict.get(b"Resources").and_then(|r| r.as_dict()).and_then(|r| r.get(b"Font")).and_then(|f| f.as_dict()).unwrap().clone(); + (s.decoded().unwrap(), fonts) + }; + fn has(hay: &[u8], needle: &[u8]) -> bool { + hay.windows(needle.len()).any(|w| w == needle) + } + for name in ["greek", "mixed"] { + let (ap, fonts) = drawn(name); + assert!(!ap.contains(&b'?'), "{name}: {}", String::from_utf8_lossy(&ap)); + let (key, font) = fonts.iter().find(|(k, _)| k.starts_with(b"PCLa")).expect("a font from the bundled face"); + assert!(has(&ap, &[&b"/"[..], key.as_slice(), &b" "[..]].concat()), "{name} shows it"); + let font = font.as_dict().unwrap(); + assert_eq!(font.name(b"Subtype"), Some(&b"Type3"[..])); + let Object::Stream(cmap) = &*doc.get(font.reference(b"ToUnicode").unwrap()) else { panic!("ToUnicode is a stream object") }; + let cmap = String::from_utf8_lossy(&cmap.decoded().unwrap()).into_owned(); + assert!(cmap.contains("<03C3>") && cmap.contains("<03BF>") && cmap.contains("<03C5>"), "σου copies as typed: {cmap}"); + } + let (greek_ap, _) = drawn("greek"); + assert!(has(&greek_ap, b"( ) Tj"), "the space between the words is Helvetica's"); + let (mixed_ap, _) = drawn("mixed"); + assert!(has(&mixed_ap, b"(Name: ) Tj") && has(&mixed_ap, b"( ok) Tj"), "{}", String::from_utf8_lossy(&mixed_ap)); + // Text WinAnsi holds: Helvetica alone, with the bytes it always had. + let (ap, fonts) = drawn("western"); + assert_eq!(fonts.iter().map(|(k, _)| k.clone()).collect::>(), [b"Helv".to_vec()]); + let shown = [pdfcraft_fonts::literal(&pdfcraft_fonts::win_ansi(western)), b" Tj".to_vec()].concat(); + assert!(has(&ap, &shown), "{}", String::from_utf8_lossy(&ap)); + // A character the bundled face lacks is still drawn as `?` and reported, beside drawn Greek. + let mut doc = doc; + set_value(&mut doc, "greek", &FieldValue::Text("Γειά 北".into())).unwrap(); + assert_eq!(undrawn_fields(&doc), [("greek".to_string(), '北')]); + assert!(need_appearances(&doc)); +} + /// A document that declares PDF/A can't ask other viewers to redraw its fields (PDF/A forbids /// `/NeedAppearances`): the value is still kept and its widget marked, and the flag stays off. #[test] @@ -4316,6 +4393,132 @@ fn document_scripts_read_each_name_tree_node_once() { assert_eq!(document_scripts_named(&doc), [("init".to_string(), "var ready = true;".to_string())]); } +#[test] +fn detection_names_check_boxes_from_the_nearer_neighbouring_label() { + use crate::detect::*; + let w = |t: &str, x0: f64, y0: f64, x1: f64| Word { text: t.into(), rect: [x0, y0, x1, y0 + 10.0] }; + let names_of = |words: &[Word], boxes: &[[f64; 4]]| -> Vec { + detect(words, &Shapes { boxes: boxes.to_vec(), rules: Vec::new() }, &[], &[]) + .into_iter() + .map(|c| { + assert_eq!(c.kind, Kind::CheckBox); + c.name + }) + .collect() + }; + + // #2943: "Monsieur [box] Madame [box]" — each label ends ~4-5 pt before its box, while + // the next label begins 23 pt after the first box. Drawing order does not change the names. + let french_words = [w("Monsieur", 100.0, 532.0, 141.0), w("Madame", 180.0, 532.0, 218.0)]; + let french_boxes = [[146.0, 532.0, 157.0, 542.0], [222.0, 532.0, 233.0, 542.0]]; + assert_eq!(names_of(&french_words, &french_boxes), ["Monsieur", "Madame"]); + assert_eq!(names_of(&french_words, &[french_boxes[1], french_boxes[0]]), ["Monsieur", "Madame"]); + + // Two filers on the same form ("Monsieur [ ] Madame [ ]" repeated) number the second pair. + let two_filers_words = + [w("Monsieur", 100.0, 532.0, 141.0), w("Madame", 180.0, 532.0, 218.0), w("Monsieur", 320.0, 532.0, 361.0), w("Madame", 400.0, 532.0, 438.0)]; + let two_filers_boxes = [[146.0, 532.0, 157.0, 542.0], [222.0, 532.0, 233.0, 542.0], [366.0, 532.0, 377.0, 542.0], [442.0, 532.0, 453.0, 542.0]]; + assert_eq!(names_of(&two_filers_words, &two_filers_boxes), ["Monsieur", "Madame", "Monsieur2", "Madame2"]); + + // Even with equal 5 pt gaps ("Label [ ] Label [ ]" vs "[ ] Label [ ] Label"), each box pairs + // with its own neighbour rather than leaving an outer box or label orphaned. + let tight_left_words = [w("Monsieur", 100.0, 400.0, 140.0), w("Madame", 160.0, 400.0, 200.0)]; + let tight_left_boxes = [[145.0, 400.0, 155.0, 410.0], [205.0, 400.0, 215.0, 410.0]]; + assert_eq!(names_of(&tight_left_words, &tight_left_boxes), ["Monsieur", "Madame"]); + + let tight_right_words = [w("Monsieur", 115.0, 400.0, 155.0), w("Madame", 175.0, 400.0, 215.0)]; + let tight_right_boxes = [[100.0, 400.0, 110.0, 410.0], [160.0, 400.0, 170.0, 410.0]]; + assert_eq!(names_of(&tight_right_words, &tight_right_boxes), ["Monsieur", "Madame"]); + + // An unlabelled box does not steal a label that sits closer to the adjacent box. + assert_eq!(names_of(&[w("Madame", 180.0, 532.0, 218.0)], &french_boxes), ["Check Box1", "Madame"]); + assert_eq!( + names_of(&[w("Monsieur", 115.0, 400.0, 155.0)], &[[100.0, 400.0, 110.0, 410.0], [178.0, 400.0, 188.0, 410.0]]), + ["Monsieur", "Check Box1"] + ); + + // Deliberate ambiguity: "[ ] 4pt Yes 3pt [ ] No" gives Yes to the closer box. + assert_eq!( + names_of(&[w("Yes", 114.0, 400.0, 134.0), w("No", 151.0, 400.0, 166.0)], &[[100.0, 400.0, 110.0, 410.0], [137.0, 400.0, 147.0, 410.0]]), + ["Check Box1", "Yes"] + ); + + // A row heading ending with a colon ("Status: [ ] Single [ ] Married" or French + // "Civilité : Monsieur [ ] Madame [ ]") does not beat or merge into the option label, + // while a standalone "Married: [ ]" or "Date of birth : [ ]" still uses its prompt. + let prompt_words = [w("Status:", 50.0, 300.0, 96.0), w("Single", 115.0, 300.0, 145.0), w("Married", 185.0, 300.0, 225.0)]; + let prompt_boxes = [[100.0, 300.0, 110.0, 310.0], [170.0, 300.0, 180.0, 310.0]]; + assert_eq!(names_of(&prompt_words, &prompt_boxes), ["Single", "Married"]); + let civilite_words = + [w("Civilité", 30.0, 532.0, 72.0), w(":", 75.0, 532.0, 78.0), w("Monsieur", 100.0, 532.0, 141.0), w("Madame", 180.0, 532.0, 218.0)]; + assert_eq!(names_of(&civilite_words, &french_boxes), ["Monsieur", "Madame"]); + assert_eq!(names_of(&[w("Married:", 50.0, 300.0, 95.0)], &[[100.0, 300.0, 110.0, 310.0]]), ["Married"]); + assert_eq!( + names_of( + &[w("Date", 30.0, 300.0, 52.0), w("of", 55.0, 300.0, 65.0), w("birth", 68.0, 300.0, 90.0), w(":", 92.0, 300.0, 95.0)], + &[[100.0, 300.0, 110.0, 310.0]] + ), + ["Date of birth"] + ); + + // "[ ] Oui Non [ ]" with an 18 pt gap between "Oui" and "Non" splits the two labels + // between their respective boxes instead of merging "Oui Non". + let mixed_words = [w("Oui", 115.0, 300.0, 135.0), w("Non", 153.0, 300.0, 175.0)]; + let mixed_boxes = [[100.0, 300.0, 110.0, 310.0], [180.0, 300.0, 190.0, 310.0]]; + assert_eq!(names_of(&mixed_words, &mixed_boxes), ["Oui", "Non"]); + + // An existing check-box widget still claims its own label so a neighbouring unlabelled box + // does not steal it. + let existing = [[100.0, 400.0, 110.0, 410.0]]; + let remaining = + detect(&[w("Monsieur", 115.0, 400.0, 155.0)], &Shapes { boxes: vec![[178.0, 400.0, 188.0, 410.0]], rules: Vec::new() }, &existing, &[]); + assert_eq!(remaining.iter().map(|c| c.name.as_str()).collect::>(), ["Check Box1"]); + + // A stray punctuation token right after the first box does not pull the next box's label in. + assert_eq!(names_of(&[w(".", 158.0, 532.0, 160.0), w("Madame", 180.0, 532.0, 218.0)], &french_boxes), ["Check Box1", "Madame"]); +} + +/// A hostile vector page can contain arbitrarily many small squares; detection must not run +/// pairwise comb/overlap/neighbor work on all of them, nor cubic default-name lookup. +#[test] +fn detection_bounds_many_small_squares_before_pairwise_work() { + use crate::detect::*; + let boxes = (0..MAX_DETECTION_BOXES * 50) + .map(|i| { + let x = (i % 100) as f64 * 30.0; + let y = (i / 100) as f64 * 30.0; + [x, y, x + 10.0, y + 10.0] + }) + .collect(); + let shapes = Shapes { boxes, rules: Vec::new() }; + let found = detect(&[], &shapes, &[], &["Check Box1".into(), "Check Box3".into()]); + assert_eq!(found.len(), MAX_DETECTION_BOXES); + assert!(found.iter().all(|c| c.kind == Kind::CheckBox)); + assert_eq!(found.first().unwrap().name, "Check Box2"); + assert_eq!(found.get(1).unwrap().name, "Check Box4"); + assert_eq!(found.last().unwrap().name, format!("Check Box{}", MAX_DETECTION_BOXES + 2)); + assert_eq!(found.iter().map(|c| &c.name).collect::>().len(), found.len()); + let prefix = Shapes { boxes: shapes.boxes.iter().take(MAX_DETECTION_BOXES).copied().collect(), rules: Vec::new() }; + assert_eq!(found, detect(&[], &prefix, &[], &["Check Box1".into(), "Check Box3".into()])); +} + +#[test] +fn detection_box_budget_counts_rejected_inputs_and_keeps_existing_exclusions() { + use crate::detect::*; + let first = [0.0, 0.0, 10.0, 10.0]; + let last = [30.0, 0.0, 40.0, 10.0]; + // The final input at the limit is included; the next is not, even if earlier inputs are + // duplicates. A later input higher on the page stays outside the budget despite reading first. + let mut boxes = vec![first; MAX_DETECTION_BOXES - 1]; + boxes.push(last); + boxes.push([60.0, 30.0, 70.0, 40.0]); + let shapes = Shapes { boxes, rules: Vec::new() }; + assert_eq!(detect(&[], &shapes, &[], &[]).iter().map(|c| c.rect).collect::>(), [first, last]); + let mut existing = vec![[90.0, 0.0, 100.0, 10.0]; MAX_DETECTION_BOXES]; + existing.push(last); + assert_eq!(detect(&[], &shapes, &existing, &[]).iter().map(|c| c.rect).collect::>(), [first]); +} + #[test] fn name_tree_nodes_sharing_one_long_kids_array_queue_it_a_bounded_number_of_times() { // 10,000 nodes share one /Kids array of 210,001 entries: the leaf, every node, and the leaf diff --git a/crates/js/src/document.rs b/crates/js/src/document.rs index cccf89a7a2..3e754761c8 100644 --- a/crates/js/src/document.rs +++ b/crates/js/src/document.rs @@ -488,3 +488,94 @@ pub(crate) fn page_num_set(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> h.request(Request::GoToPage(p)); Ok(JsValue::undefined()) } + +// ── page templates (#1429) ────────────────────────────────────────────────────────────────── + +/// A Template object (`this.getTemplate(name)`): its `name`, whether it is `hidden` (setting it +/// shows or hides the template's page), and `spawn` to add a page from it. +pub(crate) fn template_object(ctx: &mut Context, name: &str) -> JsObject { + let n = JsString::from(name); + fn get_name(_: &JsValue, _: &[JsValue], name: &JsString, _c: &mut Context) -> JsResult { + Ok(JsValue::from(name.clone())) + } + fn get_hidden(_: &JsValue, _: &[JsValue], name: &JsString, c: &mut Context) -> JsResult { + let name = name.to_std_string_escaped(); + let hidden = host(c)?.borrow().doc.templates.iter().find(|(n, _)| *n == name).is_some_and(|(_, h)| *h); + Ok(JsValue::from(hidden)) + } + fn set_hidden(_: &JsValue, args: &[JsValue], name: &JsString, c: &mut Context) -> JsResult { + let hidden = arg(args, 0).to_boolean(); + let name = name.to_std_string_escaped(); + let mut h = host(c)?.borrow_mut(); + // The script reads back what it set. + if let Some(t) = h.doc.templates.iter_mut().find(|(n, _)| *n == name) { + t.1 = hidden; + } + h.request(Request::SetTemplateHidden { name, hidden }); + Ok(JsValue::undefined()) + } + fn spawn(_: &JsValue, args: &[JsValue], name: &JsString, c: &mut Context) -> JsResult { + let request = spawn_request(name.to_std_string_escaped(), args, 0, c)?; + host(c)?.borrow_mut().request(request); + Ok(JsValue::null()) + } + let rw = Attribute::CONFIGURABLE | Attribute::ENUMERABLE; + let get_name = function(ctx, NativeFunction::from_copy_closure_with_captures(get_name, n.clone())); + let get_hidden = function(ctx, NativeFunction::from_copy_closure_with_captures(get_hidden, n.clone())); + let set_hidden = function(ctx, NativeFunction::from_copy_closure_with_captures(set_hidden, n.clone())); + let spawn = NativeFunction::from_copy_closure_with_captures(spawn, n); + ObjectInitializer::new(ctx) + .accessor(js_string!("name"), Some(get_name), None, rw) + .accessor(js_string!("hidden"), Some(get_hidden), Some(set_hidden), rw) + .function(spawn, js_string!("spawn"), 4) + .build() +} + +/// The request for a page from template `name`, from `spawn`'s arguments starting at `first`: +/// `(nPage, bRename, bOverlay)` or one object holding them. As in Acrobat, the page is 0 unless +/// given, and the fields are renamed and the page laid over unless asked otherwise. +fn spawn_request(name: String, args: &[JsValue], first: usize, ctx: &mut Context) -> JsResult { + let given = arg(args, first); + let named = given.as_object().filter(|o| !o.is_array()); + let mut value = |key: &str, index: usize| -> JsResult { + match &named { + Some(o) => o.get(JsString::from(key), ctx), + None => Ok(arg(args, first.saturating_add(index))), + } + }; + let (page, rename, overlay) = (value("nPage", 0)?, value("bRename", 1)?, value("bOverlay", 2)?); + let page = if page.is_undefined() { 0.0 } else { page.to_number(ctx)? }; + // NaN, negative and huge numbers land on the first page and past the last, where the + // document clamps them. + let page = if page.is_finite() { + page.max(0.0).min(usize::MAX as f64) as usize + } else if page > 0.0 { + usize::MAX + } else { + 0 + }; + let flag = |v: &JsValue| if v.is_undefined() { true } else { v.to_boolean() }; + Ok(Request::SpawnTemplate { name, page, rename: flag(&rename), overlay: flag(&overlay) }) +} + +/// `this.getTemplate(cName)`: the Template object, or `null` when there is none of that name. +pub(crate) fn get_template(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + let name = text(&arg(args, 0), ctx)?; + let exists = host(ctx)?.borrow().doc.templates.iter().any(|(n, _)| *n == name); + Ok(if exists { template_object(ctx, &name).into() } else { JsValue::null() }) +} + +/// `this.spawnPageFromTemplate(cTemplate, nPage, bRename, bOverlay)`: the older way to spawn. +pub(crate) fn spawn_page_from_template(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult { + // `({ cTemplate, nPage, bRename, bOverlay })` names its arguments, the template included. + let (name, first) = match arg(args, 0).as_object().filter(|o| !o.is_array()) { + Some(o) => (text(&o.get(js_string!("cTemplate"), ctx)?, ctx)?, 0), + None => (text(&arg(args, 0), ctx)?, 1), + }; + if !host(ctx)?.borrow().doc.templates.iter().any(|(n, _)| *n == name) { + return Err(error(format!("there is no template named {name}"))); + } + let request = spawn_request(name, args, first, ctx)?; + host(ctx)?.borrow_mut().request(request); + Ok(JsValue::null()) +} diff --git a/crates/js/src/globals.rs b/crates/js/src/globals.rs index 22f3bb7799..07e5c5d22a 100644 --- a/crates/js/src/globals.rs +++ b/crates/js/src/globals.rs @@ -1,4 +1,7 @@ -use super::document::{get_field, get_nth_field_name, noop, num_fields, page_num_get, page_num_set, print, reset_form, submit_form}; +use super::document::{ + get_field, get_nth_field_name, get_template, noop, num_fields, page_num_get, page_num_set, print, reset_form, spawn_page_from_template, + submit_form, template_object, +}; use super::format::{printd, printf, printx, scand}; use super::host::{arg, error, function, host, s, strings, text, text_line}; use super::model::{DISPLAY_HIDDEN, DISPLAY_NO_PRINT, DISPLAY_NO_VIEW, DISPLAY_VISIBLE, Event, Panel, Request}; @@ -527,7 +530,7 @@ fn document_info_get(_: &JsValue, _: &[JsValue], ctx: &mut Context) -> JsResult< /// Install the host API once; live document properties continue reading each job's snapshot. pub(crate) fn install(ctx: &mut Context) -> JsResult<()> { - let globals: [(&str, usize, NativeFunction); 13] = [ + let globals: [(&str, usize, NativeFunction); 15] = [ ("getField", 1, NativeFunction::from_fn_ptr(get_field)), ("getNthFieldName", 1, NativeFunction::from_fn_ptr(get_nth_field_name)), ("resetForm", 1, NativeFunction::from_fn_ptr(reset_form)), @@ -539,6 +542,8 @@ pub(crate) fn install(ctx: &mut Context) -> JsResult<()> { ("AFSimple_Calculate", 2, NativeFunction::from_fn_ptr(af_simple_calculate)), ("syncAnnotScan", 0, NativeFunction::from_fn_ptr(noop)), ("dirty", 0, NativeFunction::from_fn_ptr(noop)), + ("getTemplate", 1, NativeFunction::from_fn_ptr(get_template)), + ("spawnPageFromTemplate", 5, NativeFunction::from_fn_ptr(spawn_page_from_template)), ("exportDataObject", 4, NativeFunction::from_fn_ptr(export_data_object)), ("getOCGs", 1, NativeFunction::from_fn_ptr(crate::ocg::get_ocgs)), ]; @@ -578,6 +583,11 @@ pub(crate) fn install(ctx: &mut Context) -> JsResult<()> { let getter = function(ctx, NativeFunction::from_fn_ptr(getter)); global.define_property_or_throw(JsString::from(name), PropertyDescriptor::builder().get(getter).configurable(true), ctx)?; } + // `this.templates`: every page template, as Template objects (#1429). + let names: Vec = host(ctx)?.borrow().doc.templates.iter().map(|(n, _)| n.clone()).collect(); + let templates: Vec = names.iter().map(|n| template_object(ctx, n).into()).collect(); + let templates = JsArray::from_iter(templates, ctx); + ctx.register_global_property(js_string!("templates"), templates, rw)?; // XFA forms check both before they run: every document sees the XFA version, and only an // XFA form sees `xfa_installed`. ctx.register_global_property(js_string!("xfa_version"), JsValue::from(XFA_VERSION), rw)?; diff --git a/crates/js/src/host.rs b/crates/js/src/host.rs index f4da3d7a31..6e41a514d5 100644 --- a/crates/js/src/host.rs +++ b/crates/js/src/host.rs @@ -83,14 +83,16 @@ impl Host { Request::Submit(u) => Request::Submit(xfa::clip(u)), Request::Focus(n) => Request::Focus(xfa::clip(n)), Request::OpenDoc(p) => Request::OpenDoc(xfa::clip(p)), + // Template names are looked up exactly, so they are never clipped: a cut name could + // name another template. Their whole length is charged below. Request::FocusField { name, object_id } => Request::FocusField { name: xfa::clip(name), object_id }, other => other, }; let bytes = match &r { Request::Reset(names) => names.iter().fold(0usize, |n, s| n.saturating_add(s.len()).saturating_add(std::mem::size_of::())), Request::LaunchUrl(u) | Request::Submit(u) | Request::Focus(u) | Request::OpenDoc(u) => u.len(), + Request::SpawnTemplate { name, .. } | Request::SetTemplateHidden { name, .. } | Request::OpenAttachment { name, .. } => name.len(), Request::FocusField { name, .. } => name.len(), - Request::OpenAttachment { name, .. } => name.len(), _ => 0, }; let kept = self.room(bytes); diff --git a/crates/js/src/lib.rs b/crates/js/src/lib.rs index c5e0d6b391..6d9f3f2829 100644 --- a/crates/js/src/lib.rs +++ b/crates/js/src/lib.rs @@ -8,7 +8,9 @@ //! - `event`: `value`, `change`, `willCommit`, `rc`, `name`, `type`, `target`, `targetName`. //! - the document (`this`, also the global object): `getField`, `numFields`, //! `getNthFieldName`, `numPages`, `pageNum`, `documentFileName`, `info`, `resetForm`, -//! `calculateNow`, `getPageBox`, `print`, `submitForm`, `mailDoc`, `exportDataObject`. +//! `calculateNow`, `getPageBox`, `print`, `submitForm`, `mailDoc`, `exportDataObject`, `templates`, +//! `getTemplate`, `spawnPageFromTemplate`. +//! - Template objects: `name`, `hidden` (setting it shows or hides the template's page), `spawn`. //! - Field objects: `name`, `type`, `value`, `valueAsString`, `defaultValue`, `readonly`, //! `required`, `display`, `hidden`, `numItems`, `getItemAt`, `currentValueIndices`, //! `isBoxChecked`, `checkThisBox`, `exportValues`, `charLimit`, `textColor`, `fillColor`, diff --git a/crates/js/src/model.rs b/crates/js/src/model.rs index 0b2744ec0b..43587067be 100644 --- a/crates/js/src/model.rs +++ b/crates/js/src/model.rs @@ -185,6 +185,8 @@ pub struct DocInfo { pub page_rotations: Vec, /// The document has an XFA form, so scripts see `xfa_installed`. pub xfa: bool, + /// The page templates, with whether each is hidden (`this.templates`, #1429). + pub templates: Vec<(String, bool)>, pub zoom: f64, pub runtime_highlight: bool, /// The document's layers (optional content groups), for `getOCGs`, in the viewer's order. @@ -330,6 +332,19 @@ pub enum Request { }, /// `app.execMenuItem("ShowHide…")`: show the side panel, or hide it when it is showing. ShowPanel(Panel), + /// `template.spawn(nPage, bRename, bOverlay)` or `this.spawnPageFromTemplate(…)`: a page from + /// template `name` at `page` (0-based), with its fields renamed or not, or laid over `page`. + SpawnTemplate { + name: String, + page: usize, + rename: bool, + overlay: bool, + }, + /// `template.hidden = hidden`. + SetTemplateHidden { + name: String, + hidden: bool, + }, } /// A side panel a script can show or hide (`app.execMenuItem("ShowHideFileAttachment")`, …). diff --git a/crates/js/src/tests.rs b/crates/js/src/tests.rs index 0c876688bd..2a1d6935a0 100644 --- a/crates/js/src/tests.rs +++ b/crates/js/src/tests.rs @@ -2676,6 +2676,83 @@ fn a_plain_form_sees_the_xfa_version_but_not_installed() { assert_eq!(o.alerts, [Message::plain("old viewer")]); } +/// A document with a hidden template `More` and a visible named page `Cover` (#1429). +fn templated() -> DocInfo { + DocInfo { templates: vec![("More".into(), true), ("Cover".into(), false)], ..doc() } +} + +fn run_templated(script: &str) -> Outcome { + run(script, &Event::field("Mouse Up", "agree", ""), &templated(), &fields(), &[], Limits::default()) +} + +#[test] +fn templates_are_listed_and_found_by_name() { + let o = run_templated( + "var t = this.getTemplate('More'); \ + console.println(this.templates.length + ' ' + this.templates[1].name + ' ' + t.name + ' ' + t.hidden + ' ' + this.getTemplate('Cover').hidden + ' ' + this.getTemplate('Nope'));", + ); + assert_eq!(o.error, None); + assert_eq!(o.console, ["2 Cover More true false null"]); +} + +#[test] +fn spawn_asks_for_a_page_from_the_template() { + let o = run_templated("getTemplate('More').spawn(2, false, false);"); + assert_eq!(o.requests, [Request::SpawnTemplate { name: "More".into(), page: 2, rename: false, overlay: false }]); + // As in Acrobat: page 0, renamed and laid over unless asked otherwise. + let o = run_templated("getTemplate('More').spawn();"); + assert_eq!(o.requests, [Request::SpawnTemplate { name: "More".into(), page: 0, rename: true, overlay: true }]); + // The arguments by name. + let o = run_templated("getTemplate('More').spawn({ nPage: 1, bOverlay: false });"); + assert_eq!(o.requests, [Request::SpawnTemplate { name: "More".into(), page: 1, rename: true, overlay: false }]); +} + +#[test] +fn spawn_page_from_template_takes_its_arguments_both_ways() { + let o = run_templated("this.spawnPageFromTemplate('More', 3, false, false);"); + assert_eq!(o.requests, [Request::SpawnTemplate { name: "More".into(), page: 3, rename: false, overlay: false }]); + let o = run_templated("this.spawnPageFromTemplate({ cTemplate: 'More', nPage: 1, bRename: false, bOverlay: false });"); + assert_eq!(o.requests, [Request::SpawnTemplate { name: "More".into(), page: 1, rename: false, overlay: false }]); + let o = run_templated("this.spawnPageFromTemplate('Nope', 1);"); + assert!(o.error.as_deref().is_some_and(|e| e.contains("Nope")), "{o:?}"); + assert!(o.requests.is_empty()); +} + +#[test] +fn hiding_and_showing_a_template_is_asked_for_and_read_back() { + let o = run_templated("var t = getTemplate('More'); t.hidden = false; console.println(t.hidden);"); + assert_eq!(o.requests, [Request::SetTemplateHidden { name: "More".into(), hidden: false }]); + assert_eq!(o.console, ["false"]); +} + +/// Page numbers that aren't one land on the first page or past the last, where the document +/// clamps them. +#[test] +fn spawn_pages_that_are_not_numbers_are_clamped() { + for (arg, page) in [("NaN", 0), ("-5", 0), ("Infinity", usize::MAX), ("-Infinity", 0), ("1e300", usize::MAX), ("'2'", 2)] { + let o = run_templated(&format!("getTemplate('More').spawn({arg}, true, false);")); + assert_eq!(o.requests, [Request::SpawnTemplate { name: "More".into(), page, rename: true, overlay: false }], "{arg}"); + } +} + +/// A template name longer than a console line is asked for whole, never clipped: a cut name +/// could name another template. +#[test] +fn long_template_names_are_asked_for_whole() { + let long = "T".repeat(5000); + let doc = DocInfo { templates: vec![(long.clone(), true)], ..doc() }; + let script = "var t = this.templates[0]; t.spawn(0, false, false); t.hidden = false;"; + let o = run(script, &Event::field("Mouse Up", "agree", ""), &doc, &fields(), &[], Limits::default()); + assert_eq!(o.error, None); + assert_eq!( + o.requests, + [ + Request::SpawnTemplate { name: long.clone(), page: 0, rename: false, overlay: false }, + Request::SetTemplateHidden { name: long, hidden: false }, + ] + ); +} + /// Arrays assigned to several fields are held to one run-wide budget, and replacing a field's /// array frees its charge. #[test] diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 23eede9b45..276547006e 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -100,6 +100,11 @@ pub fn pages(doc: &Document) -> Vec { PageTree::new(doc).collect() } +/// Leaf page `index` (0-based), walking the page tree only as far as it. +pub fn page_at(doc: &Document, index: usize) -> Option { + PageTree::new(doc).nth(index) +} + /// Approximate bytes of inherited values copied into leaf dictionaries by one page walk. pub const MAX_INHERITED_COPIES: usize = 1 << 30; diff --git a/crates/ocr/src/lib.rs b/crates/ocr/src/lib.rs index aac95f18c6..d702bca055 100644 --- a/crates/ocr/src/lib.rs +++ b/crates/ocr/src/lib.rs @@ -23,9 +23,11 @@ pub mod confidence; pub mod merge; mod ocrs; +mod pitch; pub mod preprocess; #[cfg(not(target_arch = "wasm32"))] pub mod tesseract; +mod tiles; pub use confidence::{ConfidenceBand, band_for, is_suspect, low_confidence}; pub use merge::{MergeStrategy, StrategyOutcome, mean_confidence, recognize_with_strategy, rover_merge}; @@ -867,6 +869,38 @@ mod tests { assert!(word.rect[0] >= 10.0 && word.rect[0] < 60.0, "{word:?}"); } + /// A large page with small print: shrunk whole to the detector's input, its letters are too + /// small to find; in tiles they are found. The words come back in page pixels, once each, + /// also where they lie in the overlap of two tiles or across a cut. + #[test] + fn finds_small_print_on_a_large_page() { + let Some(models) = Models::find() else { + eprintln!("skipped: OCR models not installed"); + return; + }; + let ocr = Ocr::load(&models).unwrap(); + assert_eq!(ocr.detector_input, Some((600, 800)), "the tiling rule assumes the ocrs detector"); + let (w, h, dot) = (2480u32, 3508u32, DOT); + // An A4 page at 300 dpi is cut at x = 1240 and y = 1754, its tiles overlapping by 99 and + // 140 pixels around the cuts. The lines lie: in one tile only; across the vertical cut; + // half in the overlap left of it; across the horizontal cut. + let at = [(300, 400), (1170, 1000), (1100, 2500), (300, 1748)]; + let px = test_image::hello_on(w, h, dot, &at); + let lines = ocr.recognize(&px, w, h).unwrap(); + let words: Vec<&Word> = lines.iter().flat_map(|l| &l.words).collect(); + // Two words per line, each once: none lost, none doubled where tiles overlap. The letters + // are tiny and blocky, so the text itself may be a letter off ("HFLLO"). + assert_eq!(words.len(), 2 * at.len(), "{lines:?}"); + for (x, y) in at { + let near = |w: &&&Word| (w.rect[0] - x as f32).abs() < 4.0 * dot as f32 && (w.rect[1] - y as f32).abs() < 4.0 * dot as f32; + assert_eq!(words.iter().filter(near).count(), 1, "no first word at ({x}, {y}): {lines:?}"); + } + assert!(words.iter().any(|w| w.text == "HELLO"), "{lines:?}"); + } + + /// The letter size of [`finds_small_print_on_a_large_page`], in pixels per dot of a 5×7 letter. + const DOT: u32 = 2; + /// The recognised words name their engine. #[test] fn ocrs_recognizer_names_its_words() { @@ -994,6 +1028,14 @@ mod tests { mod test_image { /// "HELLO WORLD" drawn with blocky 5×7 letters, 4 pixels per dot, black on white. pub fn hello() -> (u32, u32, Vec) { + let (dot, x0, y0) = (4u32, 40u32, 40u32); + let (w, h) = (x0 * 2 + 11 * 6 * dot, y0 * 2 + 7 * dot); + (w, h, hello_on(w, h, dot, &[(x0, y0)])) + } + + /// A white `w` × `h` page with "HELLO WORLD" at each of `at` (top left corner), `dot` + /// pixels per dot of the letters. + pub fn hello_on(w: u32, h: u32, dot: u32, at: &[(u32, u32)]) -> Vec { const GLYPHS: &[(char, [&str; 7])] = &[ ('H', ["X...X", "X...X", "X...X", "XXXXX", "X...X", "X...X", "X...X"]), ('E', ["XXXXX", "X....", "X....", "XXXX.", "X....", "X....", "XXXXX"]), @@ -1004,10 +1046,8 @@ mod tests { ('D', ["XXXX.", "X...X", "X...X", "X...X", "X...X", "X...X", "XXXX."]), ]; let text = "HELLO WORLD"; - let (dot, x0, y0) = (4u32, 40u32, 40u32); - let (w, h) = (x0 * 2 + text.len() as u32 * 6 * dot, y0 * 2 + 7 * dot); let mut px = vec![255u8; (w * h * 4) as usize]; - for (i, c) in text.chars().enumerate() { + for ((i, c), &(x0, y0)) in at.iter().flat_map(|p| text.chars().enumerate().map(move |c| (c, p))) { let Some((_, rows)) = GLYPHS.iter().find(|g| g.0 == c) else { continue }; for (ry, row) in rows.iter().enumerate() { for (rx, b) in row.bytes().enumerate() { @@ -1025,7 +1065,7 @@ mod tests { } } } - (w, h, px) + px } } } diff --git a/crates/ocr/src/ocrs.rs b/crates/ocr/src/ocrs.rs index 53070d90c5..03bbfef808 100644 --- a/crates/ocr/src/ocrs.rs +++ b/crates/ocr/src/ocrs.rs @@ -131,6 +131,9 @@ fn clip(rect: [f32; 4], to: Option) -> [f32; 4] { /// A loaded recogniser. Loading takes a moment; keep one and reuse it. pub struct Ocr { engine: ocrs::OcrEngine, + /// The detection model's fixed input size, (width, height) pixels, when it declares one: a + /// page much larger than that is detected in tiles. + pub(crate) detector_input: Option<(u32, u32)>, } impl Ocr { @@ -152,6 +155,7 @@ impl Ocr { } fn with_models(detection: rten::Model, recognition: rten::Model) -> Result { + let detector_input = detector_input(&detection); let params = ocrs::OcrEngineParams { detection_model: Some(detection), recognition_model: Some(recognition), @@ -159,7 +163,7 @@ impl Ocr { ..Default::default() }; let engine = ocrs::OcrEngine::new(params).map_err(|e| OcrError::Load("ocr engine".into(), e.to_string()))?; - Ok(Ocr { engine }) + Ok(Ocr { engine, detector_input }) } /// Load the models found by [`Models::find`]. @@ -182,7 +186,7 @@ impl Ocr { let err = |e: &dyn std::fmt::Display| OcrError::Recognize(e.to_string()); let source = ocrs::ImageSource::from_bytes(&rgb, (width, height)).map_err(|e| err(&e))?; let input = self.engine.prepare_input(source).map_err(|e| err(&e))?; - let found = self.engine.detect_words(&input).map_err(|e| err(&e))?; + let found = self.detect_words(&rgb, width, height, &input)?; let mut lines = self.engine.find_text_lines(&input, &found); let before: Vec> = lines.iter_mut().map(|l| pad_line(l)).collect(); let read = self.engine.recognize_text(&input, &lines).map_err(|e| err(&e))?; @@ -204,6 +208,51 @@ impl Ocr { .filter(|l| !l.words.is_empty()) .collect()) } + + /// The words in a page, `rgb` (1 or 3 bytes per pixel) prepared as `page`. A page at least + /// twice the detector's input along an axis whose text is small once shrunk to that input + /// ([`crate::pitch`]) is detected in overlapping tiles ([`crate::tiles`]), each word kept by the + /// tile whose core holds its centre and moved back to page pixels. + fn detect_words(&self, rgb: &[u8], width: u32, height: u32, page: &ocrs::OcrInput) -> Result, OcrError> { + let err = |e: &dyn std::fmt::Display| OcrError::Recognize(e.to_string()); + let tiles = self.detector_input.map(|input| crate::tiles::tiles(width, height, input)).unwrap_or_default(); + let area = (width as usize).checked_mul(height as usize).filter(|&a| a > 0); + let channels = area.and_then(|a| rgb.len().is_multiple_of(a).then(|| rgb.len() / a)).filter(|c| matches!(c, 1 | 3)); + let (Some(channels), Some(input), true) = (channels, self.detector_input, tiles.len() > 1) else { + return self.engine.detect_words(page).map_err(|e| err(&e)); + }; + // Large print reads as well whole, at a quarter of the detection time. + if !crate::pitch::lines(rgb, width, height, channels, input).wants_tiles() { + return self.engine.detect_words(page).map_err(|e| err(&e)); + } + let mut words = Vec::new(); + for tile in &tiles { + let pixels = crate::tiles::crop(rgb, width, channels, tile.crop) + .ok_or_else(|| OcrError::Recognize("a detection tile lies outside the page".into()))?; + let source = ocrs::ImageSource::from_bytes(&pixels, (tile.width(), tile.height())).map_err(|e| err(&e))?; + let input = self.engine.prepare_input(source).map_err(|e| err(&e))?; + let (dx, dy) = (tile.crop[0] as f32, tile.crop[1] as f32); + for word in self.engine.detect_words(&input).map_err(|e| err(&e))? { + let centre = word.center(); + let (x, y) = (centre.x + dx, centre.y + dy); + if tile.owns(x, y) { + words.push(RotatedRect::new(rten_imageproc::PointF::from_yx(y, x), word.up_axis(), word.width(), word.height())); + } + } + } + Ok(words) + } +} + +/// The detection model's input size, (width, height) pixels, if its input is an NCHW image of a +/// fixed size. +fn detector_input(model: &rten::Model) -> Option<(u32, u32)> { + let id = *model.input_ids().first()?; + let shape = model.node_info(id)?.shape()?; + match shape.as_slice() { + [_, _, rten::Dimension::Fixed(h), rten::Dimension::Fixed(w)] => Some((u32::try_from(*w).ok()?, u32::try_from(*h).ok()?)), + _ => None, + } } /// The ocrs engine as a [`Recognizer`] (the default engine). diff --git a/crates/ocr/src/pitch.rs b/crates/ocr/src/pitch.rs new file mode 100644 index 0000000000..316d914e06 --- /dev/null +++ b/crates/ocr/src/pitch.rs @@ -0,0 +1,363 @@ +//! How tall the text on a page is, measured cheaply from its pixels, to decide whether detecting +//! it in tiles is worth the cost. +//! +//! What decides whether tiling helps is the height of the text once the page is shrunk to the +//! detector's input, not how full the page is. Lines of text repeat at a regular distance, the +//! line pitch, so the ink summed per pixel row rises and falls with that period. The page is +//! shrunk to a grey copy of at most [`WORK`] pixels a side and cut into a [`GRID`] × [`GRID`] +//! grid of blocks. In each block the ink is summed per row, the slow trend (shading, pictures) is +//! taken out with a moving average, and the autocorrelation of what remains is taken. The +//! autocorrelations of the blocks are averaged: columns of text whose lines do not line up still +//! agree on the period, and blocks of blank paper are left out. The period is the first strong +//! peak of the average. The same is done with the ink summed per pixel column, for pages that lie +//! sideways, and the axis with the stronger peak wins. +//! +//! The constants were chosen on MDPBench (de/es/fr, 480 pages; see the PR that added this file): +//! tiling a page whose lines repeat every fewer than [`SMALL_PITCH`] detector pixels, or whose +//! lines show no regular spacing at all, scored better than tiling every page, at about half the +//! cost. + +/// The largest side of the grey copy the pitch is measured on, in pixels. +const WORK: u32 = 1600; +/// The page is cut into `GRID` × `GRID` blocks. +const GRID: u32 = 6; +/// The longest line pitch looked for, in detector pixels. +const MAX_PITCH: f64 = 30.0; +/// A peak of the averaged, normalised autocorrelation weaker than this is noise. +const PEAK_FLOOR: f64 = 0.05; +/// The first peak at least this share of the strongest one is the pitch; the later ones are its +/// multiples (every second or third line). +const PEAK_SHARE: f64 = 0.6; +/// Lines closer together than this, in detector pixels, are small print. +pub(crate) const SMALL_PITCH: f64 = 9.0; + +/// What the pixels of a page say about its lines of text. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) enum Lines { + /// Nothing to measure: blank or uniform paper, a page too narrow to show a pitch, or pixels + /// that do not form the image they claim to. + Blank, + /// Ink without a regular line spacing: pictures, blur, a skewed or curved photo, a few + /// scattered lines. + Unclear, + /// Lines repeat every so many pixels of the detector's input. + Pitch(f64), +} + +impl Lines { + /// Whether the page is worth detecting in tiles: its lines are close together once shrunk to + /// the detector's input, or the pixels cannot tell. A blank page is not. + pub fn wants_tiles(self) -> bool { + match self { + Lines::Blank => false, + Lines::Unclear => true, + Lines::Pitch(p) => p < SMALL_PITCH, + } + } +} + +/// The lines of a `width` × `height` image of `channels` bytes per pixel (grey, grey and alpha, +/// RGB or RGBA), for a detector that reads `input` = (width, height) pixels. Reads every pixel +/// once; the rest works on at most [`WORK`] × [`WORK`] values. +pub(crate) fn lines(pixels: &[u8], width: u32, height: u32, channels: usize, input: (u32, u32)) -> Lines { + let size = (width as usize).checked_mul(height as usize).and_then(|a| a.checked_mul(channels)); + if !(1..=4).contains(&channels) || size != Some(pixels.len()) || pixels.is_empty() || input.0 == 0 || input.1 == 0 { + return Lines::Blank; + } + let Some(grey) = Grey::shrink(pixels, width, height, channels) else { return Lines::Blank }; + // The longest pitch looked for, in pixels of the grey copy, along each axis. + let reach = |len: u32, input: u32| ((MAX_PITCH * f64::from(len) / f64::from(input)) as usize).max(4); + let down = grey.curve(Axis::Down, reach(grey.height, input.1)); + let across = grey.curve(Axis::Across, reach(grey.width, input.0)); + if !down.inked && !across.inked { + return Lines::Blank; + } + let to_input = |lag: usize, len: u32, input: u32| lag as f64 * f64::from(grey.step) * f64::from(input) / f64::from(len); + match (peak(&down.correlation), peak(&across.correlation)) { + (None, None) => Lines::Unclear, + (Some((lag, d)), Some((_, a))) if d >= a => Lines::Pitch(to_input(lag, height, input.1)), + (Some((lag, _)), None) => Lines::Pitch(to_input(lag, height, input.1)), + (_, Some((lag, _))) => Lines::Pitch(to_input(lag, width, input.0)), + } +} + +/// A shrunk copy of a page as ink: 0 for white, 255 for black. +struct Grey { + ink: Vec, + width: u32, + height: u32, + /// Page pixels per pixel of the copy, along each axis. + step: u32, +} + +#[derive(Clone, Copy)] +enum Axis { + /// Ink summed per row: lines of text lying the normal way. + Down, + /// Ink summed per column: lines lying sideways. + Across, +} + +/// The averaged autocorrelation of one axis, and whether any block had ink. +struct Curve { + correlation: Vec, + inked: bool, +} + +impl Grey { + /// Box-average the page down by a whole factor until neither side exceeds [`WORK`]. The + /// caller has checked that `pixels` holds `width` × `height` pixels of `channels` bytes. + fn shrink(pixels: &[u8], width: u32, height: u32, channels: usize) -> Option { + let step = width.max(height).div_ceil(WORK).max(1); + let (w, h) = (width / step, height / step); + if w == 0 || h == 0 { + return None; + } + // At most WORK × WORK values: `step` is chosen so that each side fits. + let mut sum = vec![0f32; (w as usize).checked_mul(h as usize)?]; + let row_bytes = (width as usize).checked_mul(channels)?; + let (step_px, w_px) = (step as usize, w as usize); + for (y, row) in pixels.chunks_exact(row_bytes).take(h as usize * step_px).enumerate() { + let start = (y / step_px).checked_mul(w_px)?; + let Some(out) = sum.get_mut(start..start.checked_add(w_px)?) else { continue }; + for (x, px) in row.chunks_exact(channels).take(w_px * step_px).enumerate() { + if let Some(o) = out.get_mut(x / step_px) { + *o += luma(px); + } + } + } + let area = (step as f32) * (step as f32); + let ink = sum.into_iter().map(|s| 255.0 - s / area).collect(); + Some(Grey { ink, width: w, height: h, step }) + } + + /// The ink of the copy at `(x, y)`, 0 outside it. + fn at(&self, x: usize, y: usize) -> f32 { + if x >= self.width as usize { + return 0.0; + } + y.checked_mul(self.width as usize).and_then(|i| i.checked_add(x)).and_then(|i| self.ink.get(i)).copied().unwrap_or(0.0) + } + + /// The autocorrelation of the ink profile along `axis`, for lags `0..=reach`, averaged over + /// the blocks that have ink. Empty when no block has, or the blocks are too short to show + /// `reach`. + fn curve(&self, axis: Axis, reach: usize) -> Curve { + let (along, across) = match axis { + Axis::Down => (self.height as usize, self.width as usize), + Axis::Across => (self.width as usize, self.height as usize), + }; + let (block, side) = (along / GRID as usize, across / GRID as usize); + let mut total = vec![0f64; reach + 1]; + let (mut blocks, mut inked) = (0usize, false); + if side == 0 || block < 2 * reach + 2 { + return Curve { correlation: Vec::new(), inked: false }; + } + let mut profile = vec![0f64; block]; + for i in 0..GRID as usize { + for j in 0..GRID as usize { + // The block's ink per row (or column), averaged across the block. + for (k, p) in profile.iter_mut().enumerate() { + let a = i * block + k; + let sum: f32 = (j * side..(j + 1) * side) + .map(|b| match axis { + Axis::Down => self.at(b, a), + Axis::Across => self.at(a, b), + }) + .sum(); + *p = f64::from(sum) / side as f64; + } + let detrended = detrend(&profile, reach); + let energy: f64 = detrended.iter().map(|d| d * d).sum(); + // Blank paper: next to no variation along the block. + if energy <= 1e-3 * block as f64 { + continue; + } + inked = true; + blocks += 1; + for (lag, t) in total.iter_mut().enumerate() { + let c: f64 = detrended.iter().zip(detrended.iter().skip(lag)).map(|(a, b)| a * b).sum(); + *t += c / energy; + } + } + } + if blocks == 0 { + return Curve { correlation: Vec::new(), inked }; + } + total.iter_mut().for_each(|t| *t /= blocks as f64); + Curve { correlation: total, inked } + } +} + +/// The grey value of a pixel of 1 to 4 bytes (grey, grey and alpha, RGB, RGBA), 0 to 255. +fn luma(px: &[u8]) -> f32 { + match *px { + [r, g, b, ..] => 0.299 * f32::from(r) + 0.587 * f32::from(g) + 0.114 * f32::from(b), + [v, ..] => f32::from(v), + [] => 255.0, + } +} + +/// `profile` minus its moving average over `window` samples, the ends held at the edge values. +fn detrend(profile: &[f64], window: usize) -> Vec { + let n = profile.len(); + let window = window.max(1); + let half = window / 2; + (0..n) + .map(|i| { + let sum: f64 = (0..window).filter_map(|j| profile.get((i + j).saturating_sub(half).min(n.saturating_sub(1)))).sum(); + profile.get(i).copied().unwrap_or(0.0) - sum / window as f64 + }) + .collect() +} + +/// The line pitch in a normalised autocorrelation, as `(lag, strength)`: past the first zero +/// crossing, the first local maximum at least [`PEAK_SHARE`] of the strongest one, counting only +/// maxima above [`PEAK_FLOOR`]. `None` without such a peak. +fn peak(correlation: &[f64]) -> Option<(usize, f64)> { + let zero = correlation.iter().position(|&c| c < 0.0)?; + let peaks: Vec<(usize, f64)> = correlation + .windows(3) + .enumerate() + .filter_map(|(i, w)| match *w { + [a, b, c] if i + 1 >= zero.max(2) && b >= a && b >= c && b >= PEAK_FLOOR => Some((i + 1, b)), + _ => None, + }) + .collect(); + let strongest = peaks.iter().map(|p| p.1).fold(f64::NEG_INFINITY, f64::max); + peaks.into_iter().find(|p| p.1 >= PEAK_SHARE * strongest) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The detector of the ocrs models reads 600 × 800 pixels. + const INPUT: (u32, u32) = (600, 800); + /// A4 at 300 dpi. + const A4: (u32, u32) = (2480, 3508); + + /// A white grey page with lines of "words" (dark bars) every `pitch` pixels, 40 % of the + /// pitch tall, within 200-pixel margins; `sideways` turns the lines a quarter turn. + fn print(pitch: u32, sideways: bool) -> Vec { + let (w, h) = A4; + let mut px = vec![255u8; (w * h) as usize]; + for y in 200..h - 200 { + for x in 200..w - 200 { + let (along, line) = if sideways { (y, x) } else { (x, y) }; + // Words of 4 × 15 pixels with a space of 15 between them. + if line % pitch < pitch * 2 / 5 && (along / 15) % 5 != 4 { + px[(y * w + x) as usize] = 0; + } + } + } + px + } + + fn pitch_of(lines: Lines) -> f64 { + match lines { + Lines::Pitch(p) => p, + other => panic!("no pitch: {other:?}"), + } + } + + #[test] + fn a_blank_page_is_not_tiled() { + let (w, h) = A4; + let lines = lines(&vec![255u8; (w * h) as usize], w, h, 1, INPUT); + assert_eq!(lines, Lines::Blank); + assert!(!lines.wants_tiles()); + } + + #[test] + fn large_print_is_not_tiled() { + // Lines every 100 pixels of an A4 page at 300 dpi (8 mm, a 20-point font) repeat every + // 100 × 800 / 3508 = 22.8 detector pixels. + let (w, h) = A4; + let lines = lines(&print(100, false), w, h, 1, INPUT); + let p = pitch_of(lines); + assert!((21.0..24.0).contains(&p), "{p}"); + assert!(!lines.wants_tiles()); + } + + #[test] + fn small_print_is_tiled() { + // Lines every 30 pixels (2.5 mm, a 7-point font): 6.8 detector pixels. + let (w, h) = A4; + let lines = lines(&print(30, false), w, h, 1, INPUT); + let p = pitch_of(lines); + assert!((6.0..7.5).contains(&p), "{p}"); + assert!(lines.wants_tiles()); + // The same page in RGB and RGBA. + for channels in [3, 4] { + let px: Vec = print(30, false).into_iter().flat_map(|v| std::iter::repeat_n(v, channels)).collect(); + assert_eq!(super::lines(&px, w, h, channels, INPUT), lines, "{channels} channels"); + } + } + + #[test] + fn sideways_small_print_is_tiled() { + // Lines every 30 pixels across a 2480-pixel width: 30 × 600 / 2480 = 7.3 detector pixels. + let (w, h) = A4; + let lines = lines(&print(30, true), w, h, 1, INPUT); + let p = pitch_of(lines); + assert!((6.5..8.0).contains(&p), "{p}"); + assert!(lines.wants_tiles()); + } + + #[test] + fn ink_without_lines_is_tiled() { + // Noise (a picture, a blurred photo): the pixels cannot tell how tall the text is. + let (w, h) = A4; + let mut seed = 0x2545_f491_4f6c_dd1du64; + let px: Vec = (0..w * h) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + if seed & 1 == 0 { 0 } else { 255 } + }) + .collect(); + let lines = lines(&px, w, h, 1, INPUT); + assert_eq!(lines, Lines::Unclear); + assert!(lines.wants_tiles()); + } + + #[test] + fn odd_inputs_give_an_answer_without_panicking() { + assert_eq!(lines(&[], 0, 0, 1, INPUT), Lines::Blank); + assert_eq!(lines(&[0], 1, 1, 1, INPUT), Lines::Blank); + assert_eq!(lines(&[0, 0, 0], 1, 1, 3, INPUT), Lines::Blank); + // Fewer or more bytes than the size claims, no channels, too many, a detector of no size. + assert_eq!(lines(&[0; 7], 2, 2, 2, INPUT), Lines::Blank); + assert_eq!(lines(&[0; 4], 2, 2, 0, INPUT), Lines::Blank); + assert_eq!(lines(&[0; 20], 2, 2, 5, INPUT), Lines::Blank); + assert_eq!(lines(&[0; 4], u32::MAX, u32::MAX, 4, INPUT), Lines::Blank); + assert_eq!(lines(&print(30, false), A4.0, A4.1, 1, (0, 0)), Lines::Blank); + // A sliver: one side shrinks to nothing, or too short to show a pitch. + assert_eq!(lines(&[0; 5000], 1, 5000, 1, INPUT), Lines::Blank); + let tall = (0..3 * 40_000u32).map(|i| if (i / 3) % 30 < 12 { 0 } else { 255 }).collect::>(); + let _ = lines(&tall, 3, 40_000, 1, INPUT); + // A black page: ink everywhere, but nothing that varies. + assert_eq!(lines(&vec![0u8; 2000 * 2000], 2000, 2000, 1, INPUT), Lines::Blank); + } + + #[test] + fn the_first_strong_peak_is_the_pitch() { + // Peaks at 5 (0.5), 10 (0.8), 15 (0.3): 5 is past the share of the strongest, so it is + // the pitch and 10 its double. + let mut c = vec![0.0; 18]; + c[0] = 1.0; + c[2] = -0.2; + c[5] = 0.5; + c[10] = 0.8; + c[15] = 0.3; + assert_eq!(peak(&c), Some((5, 0.5))); + c[5] = 0.4; + assert_eq!(peak(&c), Some((10, 0.8))); + // No zero crossing, or nothing above the floor: no pitch. + assert_eq!(peak(&[1.0, 0.5, 0.4, 0.5, 0.3]), None); + assert_eq!(peak(&[1.0, -0.1, 0.01, 0.0, 0.02, 0.0]), None); + assert_eq!(peak(&[]), None); + } +} diff --git a/crates/ocr/src/tiles.rs b/crates/ocr/src/tiles.rs new file mode 100644 index 0000000000..a20f5d5f12 --- /dev/null +++ b/crates/ocr/src/tiles.rs @@ -0,0 +1,196 @@ +//! Tiled text detection for large pages. +//! +//! The detection model reads a fixed-size image, so ocrs shrinks a larger page to that size first. +//! On a dense page (a newspaper, small print, a blurred photo) the shrunk letters become too small +//! to find. A page at least twice the model's input along an axis is therefore cut in two along it +//! (2 × 2 tiles for a full page at 300 dpi), each tile reaching a little into its neighbours so +//! that a word on a cut is whole in one of them. Words are detected tile by tile; a word is kept +//! only by the tile whose core cell holds its centre, so a word seen twice in an overlap counts +//! once. Lines are then formed and recognised on the whole page, as without tiles. Only a page +//! whose text is small once shrunk is tiled; [`crate::pitch`] decides. + +/// How far a tile reaches past its core cell into each neighbour, as a fraction of the page along +/// that axis. MDPBench (de/es/fr) was measured with 4 %. +const OVERLAP: f64 = 0.04; + +/// One tile of a page. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct Tile { + /// The pixels the tile crops from the page, `[left, top, right, bottom]`, right and bottom + /// exclusive. Always within the page and never empty. + pub crop: [u32; 4], + /// The cell the tile owns, in page pixels, `[left, top, right, bottom]`, right and bottom + /// exclusive. The cells of the page's edge tiles reach to infinity, so the cells cover the + /// plane without gaps or overlaps: every finite point lies in exactly one. + pub core: [f32; 4], +} + +impl Tile { + /// Whether a word centred at `(x, y)` (page pixels) belongs to this tile. Never true for a + /// non-finite point. + pub fn owns(&self, x: f32, y: f32) -> bool { + let [l, t, r, b] = self.core; + x.is_finite() && y.is_finite() && l <= x && x < r && t <= y && y < b + } + + pub fn width(&self) -> u32 { + self.crop[2].saturating_sub(self.crop[0]) + } + + pub fn height(&self) -> u32 { + self.crop[3].saturating_sub(self.crop[1]) + } +} + +/// How many pieces an axis of `len` pixels is cut into for a detector that reads `input` pixels +/// along it: two once each half still fills the detector's input (the detector would shrink the +/// page by at least half), otherwise one, so a smaller page is read as before at no extra cost. +fn pieces(len: u32, input: u32) -> u32 { + if input > 0 && len / 2 >= input { 2 } else { 1 } +} + +/// The cells and crops of one axis of `len` pixels cut into `n` pieces: `(crop start, crop end, +/// core start, core end)` for each piece. +fn axis(len: u32, n: u32) -> Vec<(u32, u32, f32, f32)> { + let total = f64::from(len); + let margin = (OVERLAP * total).max(0.0); + (0..n) + .map(|i| { + let c0 = total * f64::from(i) / f64::from(n); + let c1 = total * f64::from(i + 1) / f64::from(n); + // `as` saturates for floats; both values lie in 0..=len. + let from = (c0 - margin).max(0.0).floor() as u32; + let to = ((c1 + margin).min(total).ceil() as u32).min(len); + let core0 = if i == 0 { f32::NEG_INFINITY } else { c0 as f32 }; + let core1 = if i + 1 == n { f32::INFINITY } else { c1 as f32 }; + (from, to, core0, core1) + }) + .collect() +} + +/// The tiles to detect text in on a `width` × `height` page, for a detector whose input is +/// `input` = (width, height) pixels. One tile covering the whole page when no axis is worth +/// cutting. +pub(crate) fn tiles(width: u32, height: u32, input: (u32, u32)) -> Vec { + let columns = axis(width, pieces(width, input.0)); + let rows = axis(height, pieces(height, input.1)); + rows.iter() + .flat_map(|&(top, bottom, core_top, core_bottom)| { + columns.iter().map(move |&(left, right, core_left, core_right)| Tile { + crop: [left, top, right, bottom], + core: [core_left, core_top, core_right, core_bottom], + }) + }) + .filter(|t| t.width() > 0 && t.height() > 0) + .collect() +} + +/// The pixels of `crop` from an image `width` pixels wide with `channels` bytes per pixel, row +/// by row. `None` if the crop reaches outside `pixels`. +pub(crate) fn crop(pixels: &[u8], width: u32, channels: usize, crop: [u32; 4]) -> Option> { + let [left, top, right, bottom] = crop.map(|v| v as usize); + let width = width as usize; + if right > width || left >= right || top >= bottom { + return None; + } + let row_len = (right - left).checked_mul(channels)?; + let mut out = Vec::with_capacity(row_len.checked_mul(bottom - top)?); + for y in top..bottom { + let start = y.checked_mul(width)?.checked_add(left)?.checked_mul(channels)?; + out.extend_from_slice(pixels.get(start..start.checked_add(row_len)?)?); + } + Some(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The detector of the ocrs models reads 600 × 800 pixels. + const INPUT: (u32, u32) = (600, 800); + + #[test] + fn small_pages_are_read_whole() { + // US Letter at 72 and 150 dpi, a screenshot, a page twice as wide as the input but not + // twice as tall: no axis is cut, and the one tile is the page. + for (w, h) in [(612, 792), (1275, 1599), (1199, 3000), (1, 1)] { + let t = tiles(w, h, INPUT); + let expect_cols = if w >= 1200 { 2 } else { 1 }; + let expect_rows = if h >= 1600 { 2 } else { 1 }; + assert_eq!(t.len(), expect_cols * expect_rows, "{w}x{h}: {t:?}"); + } + let one = tiles(612, 792, INPUT); + assert_eq!(one, vec![Tile { crop: [0, 0, 612, 792], core: [f32::NEG_INFINITY, f32::NEG_INFINITY, f32::INFINITY, f32::INFINITY] }]); + } + + #[test] + fn a_page_at_300_dpi_is_cut_into_four_overlapping_tiles() { + // A4 at 300 dpi. + let (w, h) = (2480, 3508); + let t = tiles(w, h, INPUT); + assert_eq!(t.len(), 4, "{t:?}"); + // 4 % of 2480 is 99.2 pixels, of 3508 140.32. + assert_eq!(t[0].crop, [0, 0, 1340, 1895]); + assert_eq!(t[1].crop, [1140, 0, 2480, 1895]); + assert_eq!(t[2].crop, [0, 1613, 1340, 3508]); + assert_eq!(t[3].crop, [1140, 1613, 2480, 3508]); + // Each tile still fills the detector's input. + assert!(t.iter().all(|t| t.width() >= INPUT.0 && t.height() >= INPUT.1), "{t:?}"); + // The cores meet at the middle of the page. + assert_eq!(t[0].core[2], 1240.0); + assert_eq!(t[3].core[0], 1240.0); + assert_eq!(t[0].core[3], 1754.0); + // Each core lies inside its crop, wherever it does not run off the page. + for tile in &t { + let [l, tp, r, b] = tile.core.map(|v| v.clamp(0.0, 4000.0)); + let [cl, ct, cr, cb] = tile.crop.map(|v| v as f32); + assert!(cl <= l && ct <= tp && r.min(w as f32) <= cr && b.min(h as f32) <= cb, "{tile:?}"); + } + } + + /// Each axis is cut at most once, so no page costs more than four detector runs: A0 at + /// 300 dpi, a long receipt and the largest page the types allow are cut like A4. + #[test] + fn no_page_is_cut_into_more_than_four_tiles() { + for (w, h, expect) in [(9933, 14043, 4), (14043, 9933, 4), (800, 60_000, 2), (u32::MAX, u32::MAX, 4)] { + let t = tiles(w, h, INPUT); + assert_eq!(t.len(), expect, "{w}x{h}: {t:?}"); + assert!(t.iter().all(|t| t.width() > 0 && t.height() > 0 && t.crop[2] <= w && t.crop[3] <= h), "{w}x{h}: {t:?}"); + } + } + + /// A word in an overlap is seen by two tiles; exactly one keeps it. Every point of the page, + /// on the cuts and on the edges included, belongs to exactly one tile. + #[test] + fn every_word_centre_belongs_to_exactly_one_tile() { + let (w, h) = (2480, 3508); + let t = tiles(w, h, INPUT); + let points = [ + (0.0, 0.0), + (1239.9, 1753.9), + (1240.0, 1754.0), + (1240.0, 0.0), + (1200.0, 1700.0), // in the overlap of all four crops + (w as f32, h as f32), + (-3.0, 5000.0), // a box that runs off the page + ]; + for (x, y) in points { + let owners = t.iter().filter(|tile| tile.owns(x, y)).count(); + assert_eq!(owners, 1, "({x}, {y})"); + } + assert!(t.iter().all(|tile| !tile.owns(f32::NAN, 10.0) && !tile.owns(10.0, f32::INFINITY))); + } + + #[test] + fn crops_rows_of_pixels() { + // 3 × 2 pixels, 2 bytes each. + let px: Vec = (0..12).collect(); + assert_eq!(crop(&px, 3, 2, [1, 0, 3, 2]), Some(vec![2, 3, 4, 5, 8, 9, 10, 11])); + assert_eq!(crop(&px, 3, 2, [0, 1, 1, 2]), Some(vec![6, 7])); + // Outside the image, empty, or more rows than there are: no crop, no panic. + assert_eq!(crop(&px, 3, 2, [2, 0, 4, 1]), None); + assert_eq!(crop(&px, 3, 2, [1, 1, 1, 2]), None); + assert_eq!(crop(&px, 3, 2, [0, 1, 3, 3]), None); + assert_eq!(crop(&px, u32::MAX, usize::MAX, [0, 1, u32::MAX, 2]), None); + } +} diff --git a/crates/organize/src/boxes.rs b/crates/organize/src/boxes.rs index ff56c73f67..f947d2abee 100644 --- a/crates/organize/src/boxes.rs +++ b/crates/organize/src/boxes.rs @@ -48,7 +48,8 @@ pub enum BoxSpec { Remove, /// An absolute rectangle in default user space. Rect([f64; 4]), - /// Margins inward from the media box: left, bottom, right, top (points). + /// Margins inward from the media box: left, bottom, right, top (default user-space units of the + /// unrotated page; points only on a page without `/UserUnit`). Margins([f64; 4]), } diff --git a/crates/organize/src/import.rs b/crates/organize/src/import.rs index 3d4791c83d..f6991308af 100644 --- a/crates/organize/src/import.rs +++ b/crates/organize/src/import.rs @@ -41,6 +41,7 @@ pub(crate) use copy::{Copier, copy_object}; use copy::{MAX_COPY_DEPTH, SKIP_ON_PAGE}; use fields::{add_kid, field_and_widget, register_fields, resolve_field_conflicts, split_own_field}; pub use fields::{combine_field_conflicts, field_name_conflicts}; +pub(crate) use fields::{listed_fields, share_fields_with_originals}; /// How colliding fully-qualified field names are resolved when pages are imported. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] diff --git a/crates/organize/src/import/fields.rs b/crates/organize/src/import/fields.rs index daeaecd3f6..94b2e39020 100644 --- a/crates/organize/src/import/fields.rs +++ b/crates/organize/src/import/fields.rs @@ -224,6 +224,26 @@ pub(super) fn add_kid(dst: &mut Document, field: ObjRef, kid: ObjRef) -> Result< Ok(()) } +/// Take `kid` out of field `field`'s `/Kids` (direct or held in an array object of its own). +fn remove_kid(dst: &mut Document, field: ObjRef, kid: ObjRef) -> Result<(), OrganizeError> { + match dst.get(field).as_dict().and_then(|f| f.get(b"Kids").cloned()) { + // Only an array is rewritten: `/Kids` pointing at anything else (a font, in a hostile + // file) is left as it is rather than overwritten. + Some(Object::Ref(list)) => { + if let Some(mut kids) = dst.get(list).as_array().cloned() { + kids.retain(|k| k.as_ref() != Some(kid)); + dst.set(list, Object::Array(kids)); + } + } + Some(Object::Array(mut kids)) => { + kids.retain(|k| k.as_ref() != Some(kid)); + dst.update_dict(field, |f| f.set(b"Kids".to_vec(), Object::Array(kids)))?; + } + _ => {} + } + Ok(()) +} + /// PdfCraft patch (#1810): split a widget that doubles as its own field into a parent field /// holding the field keys (name, value, flags…) and a widget child holding the annotation keys /// (rectangle, appearance, page). The parent takes the widget's place among the top-level fields; @@ -305,6 +325,218 @@ pub(super) fn register_fields(dst: &mut Document, fields: &[ObjRef]) -> Result<( Ok(()) } +/// At most this many field nodes are read when matching copied widgets to their originals. +const MAX_FIELD_NODES: usize = 100_000; + +/// Make the widgets on `page`, copied from a page of the same document, more widgets of the +/// fields they were copied from, so that each field stays one field, with one value shown on +/// every page (#1429: a page template spawned without renaming). A copied field is matched to +/// the field of the same fully-qualified name under the top-level fields the form listed before +/// the copy (`before`); the copies of the matched fields leave the form. +pub(crate) fn share_fields_with_originals(dst: &mut Document, page: ObjRef, before: &HashSet) -> Result<(), OrganizeError> { + let catalog = dst.root().ok_or(OrganizeError::NoPageTree)?; + let form_ref = dst.get(catalog).as_dict().and_then(|c| c.reference(b"AcroForm")); + let form = dst.get(catalog).as_dict().and_then(|c| c.get(b"AcroForm").cloned()).map(|f| dst.resolve(&f).as_ref().clone()); + let mut list: Vec = form + .as_ref() + .and_then(Object::as_dict) + .and_then(|f| f.get(b"Fields")) + .map(|l| dst.resolve(l).as_ref().clone()) + .and_then(|l| l.as_array().cloned()) + .unwrap_or_default(); + // The fields that were there before the copy, by fully-qualified name. + let mut originals: HashMap = HashMap::new(); + let mut seen = HashSet::new(); + let mut pending: Vec<(ObjRef, String, u8)> = + list.iter().filter_map(Object::as_ref).filter(|r| before.contains(r)).map(|r| (r, String::new(), 0)).collect(); + while let Some((node, prefix, depth)) = pending.pop() { + if depth > 32 || seen.len() >= MAX_FIELD_NODES || !seen.insert(node) { + continue; + } + let Some(d) = dst.get(node).as_dict().cloned() else { continue }; + let name = match d.get(b"T").and_then(|t| dst.resolve(t).as_string().map(|s| s.to_text())) { + Some(t) => qualified_optional(&prefix, &t), + None => prefix.clone(), + }; + if d.contains(b"T") { + originals.entry(name.clone()).or_insert(node); + } + for kid in d.get(b"Kids").map(|k| dst.resolve(k).as_ref().clone()).and_then(|k| k.as_array().cloned()).unwrap_or_default() { + if let Some(kid) = kid.as_ref() { + pending.push((kid, name.clone(), depth.saturating_add(1))); + } + } + } + let annots = dst.get(page).as_dict().and_then(|d| d.get(b"Annots").cloned()).map(|a| dst.resolve(&a).as_ref().clone()); + let widgets: Vec = annots + .as_ref() + .and_then(Object::as_array) + .into_iter() + .flatten() + .filter_map(Object::as_ref) + .filter(|w| dst.get(*w).as_dict().is_some_and(|d| d.name(b"Subtype") == Some(b"Widget"))) + .collect(); + let (mut shared, mut kept): (HashSet, HashSet) = (HashSet::new(), HashSet::new()); + for widget in widgets { + let Some(d) = dst.get(widget).as_dict().cloned() else { continue }; + // The copied field holding the widget: the widget itself when it is its own field. + let field = if d.contains(b"T") { Some(widget) } else { d.reference(b"Parent") }; + let Some((name, top)) = field.and_then(|f| name_and_top(dst, f)) else { continue }; + let Some(&original) = originals.get(&name) else { + kept.insert(top); + continue; + }; + // An original that is its own widget becomes a field with that widget as its child. + let original = if dst.get(original).as_dict().is_some_and(|o| o.name(b"Subtype") == Some(b"Widget")) { + let mut slot = [original]; + split_own_field(dst, &mut slot, original); + let [parent] = slot; + replace_kid(dst, &mut list, original, parent)?; + originals.insert(name, parent); + parent + } else { + original + }; + // The copied field it leaves no longer lists it, so no widget has two parents. + if let Some(former) = d.reference(b"Parent") { + remove_kid(dst, former, widget)?; + } + let (_, mut child) = if d.contains(b"T") { field_and_widget(dst, &d) } else { (Dict::new(), d) }; + child.set(b"Parent".to_vec(), Object::Ref(original)); + dst.set(widget, Object::Dict(child)); + add_kid(dst, original, widget)?; + shared.insert(top); + } + list.retain(|f| f.as_ref().is_none_or(|r| !shared.contains(&r) || kept.contains(&r))); + match (form_ref, form) { + (Some(r), _) => dst.update_dict(r, |f| f.set(b"Fields".to_vec(), Object::Array(list)))?, + (None, Some(_)) => dst.update_dict(catalog, |c| { + if let Some(Object::Dict(f)) = c.get(b"AcroForm").cloned() { + let mut f = f; + f.set(b"Fields".to_vec(), Object::Array(list)); + c.set(b"AcroForm".to_vec(), Object::Dict(f)); + } + })?, + (None, None) => {} + } + Ok(()) +} + +/// The fully-qualified name of field `node` and its top-level field, climbing `/Parent` at most +/// 32 levels and never through a node twice. +fn name_and_top(doc: &Document, node: ObjRef) -> Option<(String, ObjRef)> { + let mut parts = Vec::new(); + let mut seen = HashSet::new(); + let mut at = node; + for _ in 0..32 { + if !seen.insert(at) { + return None; + } + let d = doc.get(at).as_dict()?.clone(); + if let Some(t) = d.get(b"T").and_then(|t| doc.resolve(t).as_string().map(|s| s.to_text())) { + parts.push(t); + } + match d.reference(b"Parent") { + Some(parent) => at = parent, + None => { + parts.reverse(); + return Some((parts.join("."), at)); + } + } + } + None +} + +/// Put `new` where `old` was: among its parent field's `/Kids`, or among the form's top-level +/// `fields` when it has no parent. +fn replace_kid(dst: &mut Document, fields: &mut [Object], old: ObjRef, new: ObjRef) -> Result<(), OrganizeError> { + let swap = |kids: &mut Vec| { + for k in kids.iter_mut() { + if k.as_ref() == Some(old) { + *k = Object::Ref(new); + } + } + }; + match dst.get(new).as_dict().and_then(|d| d.reference(b"Parent")) { + Some(parent) => match dst.get(parent).as_dict().and_then(|p| p.get(b"Kids").cloned()) { + Some(Object::Ref(list)) => { + let mut kids = dst.get(list).as_array().cloned().unwrap_or_default(); + swap(&mut kids); + dst.set(list, Object::Array(kids)); + } + _ => dst.update_dict(parent, |p| { + let mut kids = p.get(b"Kids").and_then(|k| k.as_array()).cloned().unwrap_or_default(); + swap(&mut kids); + p.set(b"Kids".to_vec(), Object::Array(kids)); + })?, + }, + None => { + for f in fields.iter_mut() { + if f.as_ref() == Some(old) { + *f = Object::Ref(new); + } + } + } + } + Ok(()) +} + +/// The top-level fields `doc`'s form lists now, before an import adds its own. +pub(crate) fn listed_fields(doc: &Document) -> HashSet { + doc.root() + .and_then(|r| doc.get(r).as_dict().cloned()) + .and_then(|c| c.get(b"AcroForm").map(|f| doc.resolve(f).as_ref().clone())) + .and_then(|f| f.as_dict().and_then(|f| f.get(b"Fields")).map(|l| doc.resolve(l).as_ref().clone())) + .and_then(|l| l.as_array().cloned()) + .unwrap_or_default() + .iter() + .filter_map(Object::as_ref) + .collect() +} + +#[cfg(test)] +mod template_tests { + use std::sync::Arc; + + use super::*; + + fn doc(bodies: &[&str]) -> Document { + let mut out = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::new(); + for (i, b) in bodies.iter().enumerate() { + offsets.push(out.len()); + out.extend_from_slice(format!("{} 0 obj\n{b}\nendobj\n", i + 1).as_bytes()); + } + let xref = out.len(); + out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", bodies.len() + 1).as_bytes()); + for o in offsets { + out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); + } + out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", bodies.len() + 1).as_bytes()); + Document::open(Arc::new(out)).expect("opens") + } + + /// `/Kids` held in an array object of its own loses the kid; `/Kids` pointing at something + /// that isn't an array (here a font) is never overwritten. + #[test] + fn removing_a_kid_rewrites_only_an_array() { + let mut d = doc(&[ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [] /Count 0 >>", + "<< /T (G) /Kids 4 0 R >>", + "[6 0 R 7 0 R]", + "<< /T (H) /Kids 8 0 R >>", + "<< /T (a) /Parent 3 0 R >>", + "<< /T (b) /Parent 3 0 R >>", + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>", + ]); + remove_kid(&mut d, ObjRef::new(3, 0), ObjRef::new(6, 0)).unwrap(); + assert_eq!(d.get(ObjRef::new(4, 0)).as_array().cloned(), Some(vec![Object::Ref(ObjRef::new(7, 0))])); + remove_kid(&mut d, ObjRef::new(5, 0), ObjRef::new(6, 0)).unwrap(); + assert_eq!(d.get(ObjRef::new(8, 0)).as_dict().and_then(|f| f.name(b"Type")), Some(&b"Font"[..]), "the font is untouched"); + } +} + #[cfg(test)] mod tests { use std::sync::Arc; diff --git a/crates/organize/src/lib.rs b/crates/organize/src/lib.rs index 7996ee8ea6..cf0947d375 100644 --- a/crates/organize/src/lib.rs +++ b/crates/organize/src/lib.rs @@ -23,6 +23,7 @@ mod outline; mod outline_io; mod pdfx; mod prune; +mod templates; mod transition; pub mod view; mod xmp; @@ -42,6 +43,7 @@ pub use outline::{ move_bookmark, rename_bookmark, set_bookmark_color, set_bookmark_open, set_bookmark_page, set_bookmark_style, top_level_bookmarks, }; pub use outline_io::{MAX_IMPORT_BOOKMARKS, OutlineNode, OutlineTree, import_outline, outline_tree, unmapped, view_arity}; +pub use templates::{Template, set_template_hidden, spawn_template, templates}; pub use transition::{TRANSITION_DIRECTIONS, TRANSITION_STYLES, Transition, TransitionSpec, set_transition}; pub use view::{InitialView, displays_doc_title, initial_view, set_initial_view}; @@ -160,6 +162,12 @@ pub fn page_rotation(doc: &Document, index: usize) -> Result Ok(resolved_rotation(doc, attrs).rem_euclid(360)) } +/// Effective inherited rotation of every leaf page, clockwise in degrees, from one walk of the +/// page tree (what [`page_rotation`] gives per page). +pub fn page_rotations(doc: &Document) -> Result, OrganizeError> { + Ok(walk(doc)?.iter().map(|(_, attrs)| resolved_rotation(doc, attrs).rem_euclid(360)).collect()) +} + pub fn page_count(doc: &Document) -> Result { Ok(walk(doc)?.len()) } diff --git a/crates/organize/src/outline.rs b/crates/organize/src/outline.rs index e9e20bd9fc..cb54c7b8f1 100644 --- a/crates/organize/src/outline.rs +++ b/crates/organize/src/outline.rs @@ -584,8 +584,12 @@ pub struct OutlineEntry { pub page: usize, /// The structure element it stands for (`/SE`). pub element: Option, - /// Where on the page it goes: this height (user space) at the top of the window, at the - /// reader's zoom. `None` goes to the page, keeping the reader's position. + /// Where on the page it goes: this x (user space) at the left of the window, at the reader's + /// zoom. `None` keeps the reader's horizontal position. + pub left: Option, + /// This height (user space) at the top of the window. `None` keeps the reader's vertical + /// position. A bookmark to the top of what is displayed on a page turned a quarter gives + /// `left`, not `top`. pub top: Option, } @@ -610,7 +614,7 @@ pub fn add_bookmark_tree(doc: &mut Document, parent_title: &str, entries: &[Outl for (e, page) in entries.iter().zip(targets) { let mut d = Dict::new(); d.set(b"Title".to_vec(), Object::String(PdfString::text(&e.title))); - d.set(b"Dest".to_vec(), go_to_at(page, None, e.top)); + d.set(b"Dest".to_vec(), go_to_at(page, e.left, e.top)); if let Some(se) = e.element { d.set(b"SE".to_vec(), Object::Ref(se)); } diff --git a/crates/organize/src/templates.rs b/crates/organize/src/templates.rs new file mode 100644 index 0000000000..8314aaebca --- /dev/null +++ b/crates/organize/src/templates.rs @@ -0,0 +1,261 @@ +//! Page templates (#1429): the name dictionary's `/Templates` tree names pages kept out of the +//! page tree, and its `/Pages` tree names visible pages (ISO 32000-2 §7.7.4, Table 32). Forms +//! show more pages from them through JavaScript: `Template.spawn` adds a copy of a template's +//! page, and `Template.hidden = false` moves the template's own page into the document. + +use std::collections::HashSet; + +use pdfcraft_cos::{Dict, Document, ObjRef, Object, PdfString}; + +use crate::{INHERITABLE, OrganizeError, page_count, rebuild, walk}; + +/// At most this many names are read from one tree: a form has a handful of templates. +const MAX_NAMES: usize = 4096; + +/// Field levels followed up from a widget to its top-level field. +const MAX_FIELD_DEPTH: usize = 32; + +/// A named page a form can add or show. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Template { + pub name: String, + /// Kept out of the page tree (`/Templates`); a visible named page (`/Pages`) is not hidden. + pub hidden: bool, +} + +/// The document's templates: the hidden ones (`/Templates`), then the named visible pages +/// (`/Pages`), each in its tree's order. +pub fn templates(doc: &Document) -> Vec