diff --git a/ROADMAP.md b/ROADMAP.md index b1283e775..f8634ce39 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,6 +2,8 @@ **Stage: alpha** · next: beta, ~25% (ready for real work 50% → 75%) and ~400–700 h away +> **Last reviewed:** 2026-10-20 | **Last updated:** 2026-10-20 | **Change:** minor (M2, #3574: named form properties retain page origin and own dictionary boundaries; estimates unchanged) + > **Last reviewed:** 2026-10-18 · **Last updated:** 2026-10-18 · **Change:** minor (M5, PR #1971: bounded nested FDF export excludes source page/page-tree dictionaries; refreshed against upstream 4db6ac4a; estimates unchanged) > **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M7: the Edit text caret is as tall as the row it is on; estimates unchanged) · **Target:** Adobe Acrobat Pro (Acrobat DC, continuous track 26.002.21931, macOS) > **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M6: CSV tab-containing headers; estimates unchanged) @@ -172,6 +174,8 @@ Ranked; detail and the full milestone table in [docs/roadmap.md](docs/roadmap.md ## Progress log +- **2026-10-20 (M2, #3574):** Forms without Resources use page Properties for ActualText and layer visibility. Synthetic tests cover boundaries, nested/shared forms, and save/reopen. Estimates unchanged. + - **2026-10-19 (M14, #3373):** The main menu and its long submenus scroll in short windows, keeping their commands reachable. Estimates unchanged. - **2026-10-19 (M3, #3387):** Save-prompt shortcuts yield to later consent and utility dialogs; a synthetic UI regression checks that an edited document stays open. Estimates unchanged. diff --git a/crates/render/tests/named_form_properties.rs b/crates/render/tests/named_form_properties.rs new file mode 100644 index 000000000..2d36275f5 --- /dev/null +++ b/crates/render/tests/named_form_properties.rs @@ -0,0 +1,294 @@ +//! Contributor-original synthetic forms for named marked-content properties. +use pdfcraft_render::{PageRenderer, RenderConfig, RenderRequest, RequestKind}; +use std::sync::Arc; + +fn fixture(own: bool) -> Vec { + let res1 = "<< /Font << /F1 3 0 R >> /XObject << /Fm0 8 0 R >> /Properties << /P1 << /ActualText (REPLACED) >> >> >>"; + let res2 = "<< /Font << /F1 3 0 R >> /XObject << /Fm0 9 0 R >> /Properties << /oc1 7 0 R >> >>"; + let own1 = if own { format!("/Resources {res1}") } else { String::new() }; + let own2 = if own { format!("/Resources {res2}") } else { String::new() }; + let body1 = "/Span /P1 BDC BT /F1 24 Tf 72 700 Td (glyphs) Tj ET EMC"; + let body2 = "/OC /oc1 BDC BT /F1 24 Tf 72 700 Td (HIDDENTEXT) Tj ET EMC"; + let page1 = "q /Fm0 Do Q"; + let page2 = "q /Fm0 Do Q BT /F1 24 Tf 72 600 Td (visible) Tj ET"; + pdf(&[ + "<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [7 0 R] /D << /OFF [7 0 R] >> >> >>".into(), + "<< /Type /Pages /Kids [4 0 R 5 0 R] /Count 2 >>".into(), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".into(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 6 0 R /Resources {res1} >>"), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 10 0 R /Resources {res2} >>"), + stream("", page1), + "<< /Type /OCG /Name (Hidden) >>".into(), + stream(&format!("/Type /XObject /Subtype /Form /BBox [0 0 612 792] {own1}"), body1), + stream(&format!("/Type /XObject /Subtype /Form /BBox [0 0 612 792] {own2}"), body2), + stream("", page2), + ]) +} + +fn text(renderer: &mut PageRenderer, page: usize) -> Arc { + let result = renderer.render(RenderRequest { page, kind: RequestKind::Text, ..Default::default() }); + assert!(result.error.is_none(), "{:?}", result.error); + result.text.unwrap() +} + +#[test] +fn named_actual_text_in_resource_less_form_replaces_glyphs() { + for own in [true, false] { + let mut renderer = PageRenderer::new(Arc::new(fixture(own)), RenderConfig::default()); + let result = text(&mut renderer, 0); + assert_eq!(result.plain_text(), "REPLACED", "own Resources: {own}"); + assert_eq!(result.find("REPLACED").len(), 1); + assert!(result.find("glyphs").is_empty()); + } +} + +#[test] +fn named_off_layer_in_resource_less_form_stays_hidden() { + for own in [true, false] { + let mut renderer = PageRenderer::new(Arc::new(fixture(own)), RenderConfig::default()); + let image = renderer.render(RenderRequest { page: 1, scale: 1.0, ..Default::default() }); + assert!(image.error.is_none(), "{:?}", image.error); + assert_eq!((image.width, image.height), (612, 792)); + let upper = image + .rgba + .as_chunks::<4>() + .0 + .iter() + .enumerate() + .filter(|(index, _)| (70..105).contains(&(index / 612))) + .filter(|(_, pixel)| **pixel != [255, 255, 255, 255]) + .count(); + let lower = image + .rgba + .as_chunks::<4>() + .0 + .iter() + .enumerate() + .filter(|(index, _)| (170..200).contains(&(index / 612))) + .filter(|(_, pixel)| **pixel != [255, 255, 255, 255]) + .count(); + let result = text(&mut renderer, 1); + println!( + "own={own}: upper ink={upper}; lower ink={lower}; text={:?}; hidden Find={:?}; visible Find={:?}", + result.plain_text(), + result.find("HIDDENTEXT"), + result.find("visible") + ); + assert_eq!(upper, 0, "OFF layer has no upper ink, own Resources: {own}"); + assert!(lower > 0, "ordinary page text stays visible"); + assert_eq!(result.plain_text(), "visible", "own Resources: {own}"); + assert_eq!(result.find("visible").len(), 1); + assert!(result.find("HIDDENTEXT").is_empty()); + } +} + +// Contributor-original synthetic PDFs. No font or document assets are embedded. +fn pdf(objects: &[String]) -> Vec { + let mut bytes = b"%PDF-1.7\n".to_vec(); + let mut offsets = Vec::new(); + for (i, object) in objects.iter().enumerate() { + offsets.push(bytes.len()); + bytes.extend_from_slice(format!("{} 0 obj\n{object}\nendobj\n", i + 1).as_bytes()); + } + let xref = bytes.len(); + bytes.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objects.len() + 1).as_bytes()); + for offset in offsets { + bytes.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes()); + } + bytes.extend_from_slice(format!("trailer << /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objects.len() + 1).as_bytes()); + bytes +} + +fn stream(dict: &str, body: &str) -> String { + format!("<< {dict} /Length {} >>\nstream\n{body}\nendstream", body.len()) +} + +fn resources(properties: &str) -> String { + format!("<< /Font << /F1 3 0 R >> /XObject << /Fm 6 0 R /Inner 7 0 R >> {properties} >>") +} + +fn form(resources: &str, body: &str) -> String { + stream(&format!("/Type /XObject /Subtype /Form /BBox [0 0 200 200] {resources}"), body) +} + +const GLYPHS: &str = "/Span /P BDC BT /F1 12 Tf 10 150 Td (glyphs) Tj ET EMC"; +const OFF_PAINT: &str = "/OC /O BDC 1 0 0 rg 10 100 20 20 re f EMC 0 0 1 rg 50 100 20 20 re f"; + +fn objects(page_resources: &str, form_resources: &str, body: &str) -> Vec { + vec![ + "<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [8 0 R 9 0 R] /D << /OFF [8 0 R] >> >> >>".into(), + "<< /Type /Pages /Kids [4 0 R] /Count 1 >>".into(), + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>".into(), + format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 5 0 R {page_resources} >>"), + stream("", "/Fm Do"), + form(form_resources, body), + form("", GLYPHS), + "<< /Type /OCG /Name (PageOff) >>".into(), + "<< /Type /OCG /Name (FormOn) >>".into(), + ] +} + +fn assert_text(bytes: Vec, page: usize, expected: &str) { + let mut renderer = PageRenderer::new(Arc::new(bytes), RenderConfig::default()); + let result = text(&mut renderer, page); + assert_eq!(result.plain_text(), expected); + assert_eq!(result.find(expected).len(), 1); + if expected != "glyphs" { + assert!(result.find("glyphs").is_empty()); + } +} + +#[test] +fn own_form_dictionary_stops_property_fallback() { + let page = format!("/Resources {}", resources("/Properties << /P << /ActualText (PAGE) >> >>")); + for (own, expected) in [ + ("", "PAGE"), + ("/Resources << /Properties << /P << /ActualText (OWN) >> >> >>", "OWN"), + ("/Resources << >>", "glyphs"), + ("/Resources << /Properties << >> >>", "glyphs"), + ("/Resources << /Properties << /Other << /ActualText (OTHER) >> >> >>", "glyphs"), + ("/Resources << /Properties << /P 42 >> >>", "glyphs"), + ("/Resources << /Properties << /P 99 0 R >> >>", "glyphs"), + ("/Resources << /Properties 42 >>", "glyphs"), + ("/Resources 42", "glyphs"), + ("/Resources 99 0 R", "glyphs"), + // Preserve the pre-fix behavior for an explicitly present null entry. + ("/Resources null", "glyphs"), + ] { + assert_text(pdf(&objects(&page, own, GLYPHS)), 0, expected); + } +} + +#[test] +fn nested_resource_less_form_uses_page_origin_and_own_form_shadows_it() { + let page = format!("/Resources {}", resources("/Properties << /P << /ActualText (PAGE) >> >>")); + let outer = format!("/Resources {}", resources("/Properties << /P << /ActualText (CALLER) >> >>")); + for (inner, expected) in [("", "PAGE"), ("/Resources << /Properties << /P << /ActualText (INNER) >> >> >>", "INNER")] { + let mut fixture = objects(&page, &outer, "/Inner Do"); + fixture[6] = form(inner, GLYPHS); + assert_text(pdf(&fixture), 0, expected); + } +} + +#[test] +fn shared_form_uses_each_pages_properties() { + let first = format!("/Resources {}", resources("/Properties << /P << /ActualText (FIRST) >> >>")); + let second = format!("/Resources {}", resources("/Properties << /P << /ActualText (SECOND) >> >>")); + let mut fixture = objects(&first, "", GLYPHS); + fixture[1] = "<< /Type /Pages /Kids [4 0 R 10 0 R] /Count 2 >>".into(); + fixture.push(format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 5 0 R {second} >>")); + let bytes = pdf(&fixture); + assert_text(bytes.clone(), 0, "FIRST"); + assert_text(bytes, 1, "SECOND"); +} + +#[test] +fn page_tree_properties_inherit_only_until_an_own_resources_dictionary() { + let root = resources("/Properties << /P << /ActualText (ROOT) >> >>"); + let near = resources("/Properties << /P << /ActualText (NEAR) >> >>"); + for (node, page, expected) in [ + ("".to_string(), "".to_string(), "ROOT"), + (format!("/Resources {near}"), "".to_string(), "NEAR"), + ("/Resources << >>".to_string(), "".to_string(), "glyphs"), + ("".to_string(), "/Resources << >>".to_string(), "glyphs"), + ] { + let mut fixture = objects(&page, "", GLYPHS); + fixture[1] = format!("<< /Type /Pages /Resources {root} /Kids [10 0 R] /Count 1 >>"); + fixture[3] = fixture[3].replace("/Parent 2 0 R", "/Parent 10 0 R"); + fixture.push(format!("<< /Type /Pages /Parent 2 0 R /Kids [4 0 R] /Count 1 {node} >>")); + assert_text(pdf(&fixture), 0, expected); + } +} + +fn assert_paint(bytes: Vec, page: usize, layers: Vec<(i32, i32, bool)>, visible: bool) { + let mut renderer = PageRenderer::new(Arc::new(bytes), RenderConfig { layers: Arc::new(layers), ..Default::default() }); + let image = renderer.render(RenderRequest { page, scale: 1.0, ..Default::default() }); + assert!(image.error.is_none(), "{:?}", image.error); + assert_eq!((image.width, image.height), (200, 200)); + let pixel = |x: usize, y: usize| &image.rgba[(y * 200 + x) * 4..(y * 200 + x) * 4 + 4]; + assert_eq!(pixel(60, 90), [0, 0, 255, 255], "unmarked control remains blue"); + assert_eq!(pixel(20, 90), if visible { [255, 0, 0, 255] } else { [255; 4] }); +} + +#[test] +fn indirect_page_properties_preserve_ocg_identity_and_visibility_overrides() { + let page = format!("/Resources {}", resources("/Properties 10 0 R")); + let mut fixture = objects(&page, "", OFF_PAINT); + fixture.push("<< /O 8 0 R >>".into()); + let bytes = pdf(&fixture); + assert_paint(bytes.clone(), 0, vec![], false); + assert_paint(bytes.clone(), 0, vec![(9, 0, true)], false); + assert_paint(bytes.clone(), 0, vec![(8, 0, true)], true); + assert_paint(bytes, 0, vec![(8, 0, false)], false); +} + +#[test] +fn nested_ocg_uses_page_origin_and_own_dictionary_boundaries() { + let page = format!("/Resources {}", resources("/Properties << /O 8 0 R >>")); + let caller = format!("/Resources {}", resources("/Properties << /O 9 0 R >>")); + for (inner, visible) in [ + ("", false), + ("/Resources << /Properties << /O 9 0 R >> >>", true), + ("/Resources << >>", true), + ("/Resources << /Properties << /Other 8 0 R >> >>", true), + ("/Resources << /Properties << /O 42 >> >>", true), + ("/Resources << /Properties << /O 99 0 R >> >>", true), + ] { + let mut fixture = objects(&page, &caller, "/Inner Do"); + fixture[6] = form(inner, OFF_PAINT); + assert_paint(pdf(&fixture), 0, vec![], visible); + } +} + +#[test] +fn direct_and_inline_properties_and_unmarked_content_keep_existing_behavior() { + let page = format!("/Resources {}", resources("/Properties << /P << /ActualText (DIRECT) >> >>")); + for (body, expected) in [ + (GLYPHS, "DIRECT"), + ("/Span << /ActualText (INLINE) >> BDC BT /F1 12 Tf 10 150 Td (glyphs) Tj ET EMC", "INLINE"), + ("/Span BMC BT /F1 12 Tf 10 150 Td (glyphs) Tj ET EMC", "glyphs"), + ("/Span << /ActualText (OUTER) >> BDC /Span /P BDC BT /F1 12 Tf 10 150 Td (glyphs) Tj ET EMC EMC", "OUTER"), + ] { + assert_text(pdf(&objects(&page, "", body)), 0, expected); + } + let inline_ref = OFF_PAINT.replace("/O BDC", "<< /OC 8 0 R >> BDC"); + assert_paint(pdf(&objects(&page, "", &inline_ref)), 0, vec![], false); + assert_paint(pdf(&objects(&page, "", &inline_ref)), 0, vec![(8, 0, true)], true); + let direct_ocg = format!("/Resources {}", resources("/Properties << /O << /Type /OCG /Name (Direct) >> >>")); + assert_paint(pdf(&objects(&direct_ocg, "", OFF_PAINT)), 0, vec![], true); + assert_paint(pdf(&objects(&page, "", &OFF_PAINT.replace("/O BDC", "<< /Type /OCG /Name (Inline) >> BDC"))), 0, vec![], true); +} + +#[test] +fn save_reopen_preserves_missing_resources_and_effective_named_properties() { + let page = format!("/Resources {}", resources("/Properties << /P << /ActualText (PAGE) >> /O 8 0 R >>")); + for own in ["", "/Resources << /Properties << /P << /ActualText (PAGE) >> /O 8 0 R >> >>"] { + let bytes = pdf(&objects(&page, own, GLYPHS)); + let mut document = lopdf::Document::load_mem(&bytes).unwrap(); + let before = document.get_object((6, 0)).unwrap().as_stream().unwrap().dict.clone(); + let mut saved = Vec::new(); + document.save_to(&mut saved).unwrap(); + let reopened = lopdf::Document::load_mem(&saved).unwrap(); + assert_eq!(reopened.get_object((6, 0)).unwrap().as_stream().unwrap().dict, before); + assert_text(saved, 0, "PAGE"); + let painted = objects(&page, own, OFF_PAINT); + let mut document = lopdf::Document::load_mem(&pdf(&painted)).unwrap(); + let mut saved = Vec::new(); + document.save_to(&mut saved).unwrap(); + assert_paint(saved, 0, vec![], false); + } +} + +#[test] +fn shared_form_uses_each_pages_ocg_reference() { + let first = format!("/Resources {}", resources("/Properties << /O 8 0 R >>")); + let second = format!("/Resources {}", resources("/Properties << /O 9 0 R >>")); + let mut fixture = objects(&first, "", OFF_PAINT); + fixture[1] = "<< /Type /Pages /Kids [4 0 R 10 0 R] /Count 2 >>".into(); + fixture.push(format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 5 0 R {second} >>")); + let bytes = pdf(&fixture); + assert_paint(bytes.clone(), 0, vec![], false); + assert_paint(bytes.clone(), 1, vec![], true); + assert_paint(bytes.clone(), 0, vec![(8, 0, true)], true); + assert_paint(bytes, 1, vec![(9, 0, false)], false); +} diff --git a/docs/roadmap.md b/docs/roadmap.md index 5d73511e9..5d2d4ceb5 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -1,5 +1,7 @@ # PdfCraft roadmap: milestones and what's next +> **Last reviewed:** 2026-10-20 | **Last updated:** 2026-10-20 | **Change:** minor (M2, #3574: named form properties retain page origin and own dictionary boundaries; estimates unchanged) + > **Last reviewed:** 2026-10-18 · **Last updated:** 2026-10-18 · **Change:** minor (M5, PR #1971: bounded nested FDF export excludes source page/page-tree dictionaries; refreshed against upstream 4db6ac4a; estimates unchanged) > **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M14/#2383 GPU-limit candidate; estimates unchanged) · **Target:** Adobe Acrobat Pro (Acrobat DC, continuous track 26.002.21931, macOS) > **Last reviewed:** 2026-10-15 · **Last updated:** 2026-10-15 · **Change:** minor (M7: the Edit text caret is as tall as the row it is on; estimates unchanged) · **Target:** Adobe Acrobat Pro (Acrobat DC, continuous track 26.002.21931, macOS) @@ -111,7 +113,7 @@ total matches the parity estimate. |---|---|---|---|---|---| | M0 | Skeleton: workspace, xtask gates, CI | 15–30 | 98% | 1–2 | GitHub workflow, `deny.toml` (licence audit of every dependency), parity checklist (826 features, `xtask parity`) done. Missing: remaining crate stubs, testkit/oracle crates | | M1 | COS: filters, crypt, parser, xref, writer | 120–200 | 80% | 20–40 | Done:
- filters and crypt: every standard-security revision R2–R6 (RC4, AES-128/256), SASLprep, permissions, creating encryption;
- cos: parse and repair (including invalid xref object-number ranges), decoded-stream limits including unfiltered data, decrypt on load, re-encrypt on save, incremental and full writing.
Corpus: open/edit/save passes on 958 files, and all 7 password-protected files open.
Full saves now pack objects into compressed object streams. An unchanged incremental save of a classic table writes a zero-length cross-reference subsection instead of a section with no subsections (#1339). A full rewrite keeps unknown trailer entries, including objects reachable only from them (#1338). Fuzzing runs nightly (`xtask fuzz`). Raw-deflate fallback preserves decoding-limit errors, including through tolerant COS stream decoding. Missing: ≥ 250 tests, own image codecs | -| M2 | Model, render, text | 200–350 | 15% | 130–230 | hayro bootstrap renderer (vendored patches); inspector predictor scratch is bounded by available input, and JPX sample rescaling handles 1–32 bits safely. Text extraction reaches word-F1 0.98 against pdftotext. Short same-line redraws split before placement rather than interleaving. Missing: model crate, fonts, DisplayList, renderer independent of hayro. Inspector page-label traversal and output are resource-bounded, with physical-number fallback warnings. Packed TIFF-predictor RGB rows retain later rows in decoding and rendering. Page marks, added content and print output retain small page-unit transform coefficients when serialized. | +| M2 | Model, render, text | 200–350 | 15% | 130–230 | hayro bootstrap renderer (vendored patches); inspector predictor scratch is bounded by available input, and JPX sample rescaling handles 1–32 bits safely. Text extraction reaches word-F1 0.98 against pdftotext. Short same-line redraws split before placement rather than interleaving. Missing: model crate, fonts, DisplayList, renderer independent of hayro. Inspector page-label traversal and output are resource-bounded, with physical-number fallback warnings. Packed TIFF-predictor RGB rows retain later rows in decoding and rendering. Page marks, added content and print output retain small page-unit transform coefficients when serialized. Forms without Resources use page Properties for ActualText and layer visibility; own dictionaries stop fallback. | | M2 | Model, render, text | 200–350 | 15% | 130–230 | hayro bootstrap renderer (vendored patches); inspector predictor scratch is bounded by available input, and JPX sample rescaling handles 1–32 bits safely. Text extraction reaches word-F1 0.98 against pdftotext. Short same-line redraws split before placement rather than interleaving. Missing: model crate, fonts, DisplayList, renderer independent of hayro. Inspector page-label traversal and output are resource-bounded, with physical-number fallback warnings. Packed TIFF-predictor RGB rows retain later rows in decoding and rendering. The renderer keeps its one-point crop-size floor in physical points after UserUnit scaling, with regression coverage for crop origins, rotations and f32 reciprocal rounding. Existing unit policy and nearly-zero fallback are retained. | | M2 | Model, render, text | 200–350 | 15% | 130–230 | hayro bootstrap renderer (vendored patches); inspector predictor scratch is bounded by available input, and JPX sample rescaling handles 1–32 bits safely. Text extraction reaches word-F1 0.98 against pdftotext. Short same-line redraws split before placement rather than interleaving. Missing: model crate, fonts, DisplayList, renderer independent of hayro. Inspector page-label traversal and output are resource-bounded, with physical-number fallback warnings | | M3 | Viewer app (native + web) | 80–150 | 90% | 10–20 | Acrobat-style shell, find, select, panels, tiles, web build, UI control channel for agents (opt-in). Missing: 60 fps test on a 500-page document, snapshot tests of every panel
Done since: fit visible, document title in the window; Linux middle-button scrolling; shared three-state theme preferences in the toolbar, View menu and control channel; numeric Linux portal events and idle wakeups completing #881, with native appearance inheritance retained (#446); late browser startup downloads preserve the current document and Save target (#171). Selected-text Copy in Edit and the canvas context menu is covered by focused regressions and native clipboard checks (#1225); document copy restrictions apply to the floating action too. #1364 candidate: absolute local GoToR links ask before target access; headless callers explicitly opt in. Native-opening candidate: chosen tools survive preparation and password retries; startup intake waits for pending preparation. Page-label navigation displays logical labels, retains the current page on unchanged submission and refreshes focused same-document replacements; clean file-watcher reload preserves the chosen scrolling mode, preventing a Single Page reader from jumping to page 1 during replacement. Synthetic control regressions cover these behaviors. #3149: page-list changes discard render demand based on old geometry; actual raster dimensions are checked against the GPU limit before upload. Synthetic crash and continued-rendering regressions cover the replacement path. Save-prompt shortcuts yield to dialogs above them; stacked link-consent UI regression covers Escape and Return. | diff --git a/vendor/README.md b/vendor/README.md index 824e84419..2a817453b 100644 --- a/vendor/README.md +++ b/vendor/README.md @@ -9,6 +9,12 @@ A PdfCraft patch in `hayro-syntax::Page::base_dimensions` expresses the existing In `hayro-interpret`, Type 3 glyph bounding boxes are transformed to device space before intersecting clip and shading bounds; `raster::tests::shaded_type3_glyph_draws_at_multiple_positions` covers translated glyphs (#3178). +Named form properties use the effective page dictionary when the form omits `/Resources` (#3574). +`hayro-syntax::Resources` caches the page origin; `hayro-interpret` preserves the form dictionary's absence. +An own dictionary stops fallback, including empty and malformed entries. Explicit null keeps its previous local-empty behavior. +`crates/render/tests/named_form_properties.rs` covers text, Find, OCG identity, visibility overrides, page inheritance, and save/reopen. +Other resource categories retain their existing parent-chain lookup. + | Crate | Version | Licence | Patches | Test | |---|---|---|---|---| | hayro-interpret | 0.7.0 | Apache-2.0 OR MIT | (1) select `/AP /N` appearance states via `/AS` (checkboxes, radios, stateful icons) and honour the NoView flag; (2) Type3 Unicode fallback to `/Encoding` glyph names, then printable ASCII; (3) `Type3Glyph::advance_width`; (4) `InterpreterSettings::ocg_overrides` for viewer layer toggles; (5) soft masks whose group lacks `/CS` were dropped (Chrome gradient text); (6) paint nesting cap `MAX_PAINT_NESTING` for tiling patterns and Type 3 glyphs (self-referencing content overflowed the stack); (7) CID width ranges in `/W` and `/W2` clamped to `MAX_CID` (a `0 4294967295 w` range hung); (8) `InterpreterSettings::hide_comments` skips markup annotations but keeps widgets and links (View ▸ Hide all comments); (9) `image_size_ok`: images and masks over 2^28 pixels are not decoded (a fuzzed `/W 4294967295` stencil mask allocated 4 GiB); (10) `MAX_NESTED_PAINTS`: nested paints (forms, Type 3 glyphs and tiling patterns inside one another) share a per-page budget of 50,000 (a glyph showing eight glyphs of its own font, sixteen deep, hung); (11) depends on skrifa 0.47 instead of 0.42 (upstream read-fonts fixed a Type 1 tokenizer that recursed through runs of numbers: a fuzzed font hung or overflowed the stack), with the one API change (`Charset::glyph_id` returns `Option`); (12) annotation appearances whose `/BBox` or `/Matrix` collapses to a line or a point are skipped (mapping them onto `/Rect` divided by zero and handed the device infinite or NaN matrices); (13) a Highlight annotation without a usable normal appearance is drawn from its `/QuadPoints`, `/C` and `/CA` with Multiply blending, as pdfcraft-annot draws highlights (it was not drawn at all); (14) simple-font `/Widths` (`read_widths`): entries only for codes 0–255 (one entry per code below `/FirstChar` was allocated: `4294967295` asked for 34 GB), an entry that isn't a number leaves its code missing instead of ending the array, and a `/LastChar` below `/FirstChar` gives no widths instead of discarding the font for a standard one; (15) the appearance box is mapped onto `/Rect` corner to corner: the translation now accounts for the scale (an appearance whose box didn't start at the origin and differed in size from `/Rect` was drawn shifted); (16) a clip set by `W`/`W*` takes effect after whichever path-painting operator ends the path (`S`, `f`, `B`, … as well as `n`; ISO 32000-2 §8.5.4): it was applied only on `n`, so `re W* S` clipped nothing and the clip stayed pending for a later, unrelated `n`; (17) `InterpreterSettings::cancelled`: once set, interpretation stops before the next content operator (a render pool sets it for every worker when dropped, after an edit or on closing, and for one worker when the watchdog gives up on its render, so renders whose answers nobody can receive stop instead of running to the end); (18) `MAX_PAGE_CONTENT`: the content a page holds at once (its contents, and the forms, tiling patterns and Type 3 glyph procedures being painted) stays within 512 MiB (`decode_content`; once it runs out, `InterpreterWarning::ContentTruncated` is reported (once per page by pdfcraft-render) so a truncated page is not a clean render; each level gives its bytes back when it finishes): forms nest fifty deep and each level kept its content while the next ran, so a form painting itself around a stream inflating to 256 MiB would have held over 12 GB; and Type 0 sampled functions take at most `MAX_SAMPLE_ENTRIES` (2^21) table points and `MAX_SAMPLE_VALUES` (2^23) samples, read no further than `/Size` asks (256 MiB of 1-bit samples asked for over 8 GB); (19) mesh shadings (types 4–7) are sampled only inside the area being drawn (`ShadingPattern::encode_within`, which the vendored hayro renderer calls with that area plus a pixel), within 8 visits per pixel of it plus 4 per triangle (`TriangleSampler`), and patches are triangulated one at a time: points far off the page were sampled over their whole bounding box, up to 65536 × 65536 entries (a fuzzed 147-byte patch stream; a synthetic page passed 6 GB in six seconds), ten thousand overlapping triangles ran for minutes, and every patch became 722 triangles before any limit; meshes hold at most `MAX_MESH_PATCHES` (2^16) patches and `MAX_MESH_TRIANGLES` (2^20) triangles, a lattice needs two to 2^19 vertices per row (`/VerticesPerRow 0` pushed empty rows for ever; one huge row read the whole stream), and a page samples at most `MAX_PAGE_MESH_TRIANGLES` (2^25) mesh triangles in all (a mesh painted a thousand times was triangulated a thousand times); each cut logs a warning; (20) PDF function construction is limited to 64 levels and 10,000 total nodes with cycle detection; calculator parsing and evaluation share procedure depth/work budgets, integer division/bitshift/rotation/index are panic-free, and fixed-size stack errors propagate; (21) a Type 0 font drawn with a substitute that isn't CID-keyed (it isn't embedded, or its program doesn't load) draws `.notdef` for a CID of an Adobe character collection (Japan1, GB1, CNS1, Korea1) that it can't find by Unicode: hayro took the CID for a glyph index of the substitute, which numbers its glyphs its own way, so the character was drawn as an unrelated one (𠮷 as "Q" in BIZ UDPGothic); (22) a Type 0 font drawn with a substitute finds a character given with a variation selector (`/ToUnicode`, or Adobe-Japan1-UCS2, which maps the JIS X 0208 forms of the kanji JIS X 0213:2004 redrew that way: CID 1247 is 噂 U+E0100) through the substitute's variation sequences, else as the character alone: such a CID found no glyph and was drawn as an unrelated one; (23) backport of hayro 0.8's `gradient` module: `EncodedShadingPattern::as_svg_gradient` turns an axial or radial shading into stops of an sRGB gradient (within 0.01 per channel, a stop at every stitching-function bound via `Function::stitching_bounds`) when a native gradient draws it exactly (extended past the box drawn in; a radial one with no background colour); (24) `DecodedImageCache`: decoded image pixels shared across renders through the interpreter cache within a 64 MiB budget (an image-heavy page decoded its thousands of images again per tile and per scale), keyed by every decode input (`DecodedImageKey`: the stream's place and dictionary bytes, its object, mask/stencil/raster output, interpolation, decode target size, preview or final); images under a transfer function and inline images are not cached, an image over the budget is never kept, and a poisoned lock decodes uncached; (25) `Device::actual_text` hands the device a marked content sequence's `/ActualText` (inline or named in `/Properties`), so text extraction keeps the Thai, Indic and Arabic clusters Chrome marks this way (their shaped tone marks and vowels have no Unicode of their own); (26) non-embedded simple fonts drawn with a standard-font stand-in build the Latin Extended-A letters the stand-in lacks (`/Gbreve`, `/Idotaccent`, `/Scedilla`, `/aogonek`, `/ccaron`…) from the base letter and accent it has (`StandardKind::composite_glyph`, `ACCENTED_LETTERS`): PDFium's Foxit stand-ins hold only the Standard/WinAnsi/Mac Roman glyphs, so iText letters with Times New Roman unembedded showed blanks for Ğ, İ, Ş, ğ and ş; (27) an embedded TrueType font without a base encoding reads its codes as WinAnsi for text extraction (`EmbeddedKind::code_to_unicode_name`), matching pdftotext's output, instead of Standard: OCR text layers of scans write WinAnsi codes with no `/Encoding`, and Standard has no ü, ö or ß and reads é as Ø (glyph selection unchanged); (28) an image whose pixels hold at most 16 bits of samples (an 8-bit Separation or DeviceN image, a 1-, 2- or 4-bit gray or indexed one) is converted once per possible pixel value through a lookup table instead of once per pixel (`rgb_via_lookup`): a spot-colour tint transform is often a PostScript calculator program, run for every pixel, so a fillable character sheet's 3304 × 4105 DeviceN background took four seconds and 430 MB to draw a page; each entry is converted on its own (`convert_each`), so a tint transform giving the wrong number of components for a value the image never uses cannot shift the colours of the values it does; (29) `Device::begin_annotations`/`end_annotations` bracket the page's annotations, so a device can tell comment paints from document paints (reading colours recolour the document, never a comment, #1548); (30) the Annex D.2 alias codes read as the ink they draw: WinAnsiEncoding 0xA0 and 0xAD, and MacRomanEncoding 0xCA, carry the glyph names `space` and `hyphen` -- the names those tables also give 0x20 and 0x2D -- not `nbspace` and `sfthyphen`, whose glyph list values U+00A0 and U+00AD are invisible formatting characters. Extraction hid drawn ink behind them, so search and redaction matched neither (`XiaoBiaoSong.pdf` in the pdf.js corpus drew 104 soft hyphens; across the 989 files the soft hyphens we extract fall from 117 to 13 and the non-breaking spaces from 30 to 26, what remains coming from `/ToUnicode` maps, which say so explicitly). The base table only, in the text direction only: a `/Differences` name keeps its glyph list value and glyph lookup is untouched, both as pdftotext does; (31) a Type 0 sampled function keeps its samples in one flat array indexed by strides (`Type0::entry`) instead of a hash map keyed by each grid point: the map was built again, one hashed insert per sample, every time a shading using the function was painted (a 2,097,152-sample ramp painted 30 times took 38 s; `bug1721218` in the pdf.js corpus spent most of a 1.5 s page on it); the interpolation is unchanged, so results are bit for bit the same, a sample missing from a short stream still gives no value, and the maximum sample value is computed without overflowing at 32 bits per sample; (32) the decoded-image cache (24) keys a decode by the size the image is drawn at only when a JPX stream decodes it or its soft mask or mask (`decode_reads_target`): JPX is the only filter that decodes a reduced resolution for a size, so a JPEG or Flate scan was decoded again at every zoom step and kept once per zoom level; one decode now serves every size; `InterpreterCache::decoded_image_decodes` counts the cache's decodes; (33) Coons and tensor-product patches copy their vertices' colours less: each point of a patch's 20 × 20 triangle grid is interpolated once (`generate_patch_triangles`; each cell interpolated its four corners itself, 1,444 interpolations a patch for 400 points), a cell's second triangle takes the vertices its first copied, and the mesh sampler moves a patch's triangles into device space instead of copying each vertex (`TriangleSampler::sample_owned`); the same values, 10–16% faster on mesh-gradient pages; (34) `color::tint_transform_runs`: how many colours the calling thread has run through a Separation or DeviceN tint transform (counted once per batch in `convert_f32`), so a test counts patch (28)'s once-per-sample-value runs instead of timing them (its 10 s deadline failed on a busy FreeBSD VM in CI); (41) a transparency group that is neither isolated nor knockout, entered at full opacity with no soft mask and Normal blending, is drawn straight onto the page instead of into a layer of its own (§11.4.7: its elements composite with the backdrop; the layer made a `/BM /Multiply` highlight appearance an opaque block, #1968); (42) text in a Type 3 font is drawn filled whatever the text rendering mode, except mode 3 (invisible): only that mode has any effect on Type 3 fonts (ISO 32000-1 §9.3.6). Stroking ran the glyph procedure a second time, so `2 Tr` made bitmap fonts look bold, `1 Tr` painted an uncoloured glyph in the stroking colour, and `7 Tr` hid it; (43) Lab colour spaces honour their own `/WhitePoint`: L\*a\*b\* goes to XYZ relative to that white, is Bradford-adapted to D65 and converted to sRGB (ISO 32000-2 §8.6.5.4) instead of through `LAB.icc` as if every white were D50 (a D65 blue came out 45 levels too red; the profile is removed); components normalised to [0, 1] (`manual_scale`, an `/Indexed` palette over Lab) map onto L\* 0..100 and the a\*/b\* `/Range` | (1) `raster::tests::widget_appearance_states`; (2, 3) `xtask text-oracle` on corpus/pdfjs issue918; (4) `raster::tests::layer_override_hides_content`; (5) `raster::tests::soft_mask_without_group_colour_space_is_applied`; (6) `raster::tests::self_referencing_tiling_pattern_terminates`, `raster::tests::self_referencing_type3_glyph_terminates`; (7) `raster::tests::huge_cid_width_ranges_terminate`; (8) `raster::tests::hiding_comments_keeps_fields`; (9) `raster::tests::absurd_mask_and_fax_sizes_are_refused_before_decoding`; (10) `raster::tests::type3_glyphs_that_fan_out_into_themselves_finish`; (11) `raster::tests::type1_font_programs_with_long_runs_of_numbers_load_quickly`; (12) `raster::tests::degenerate_annotation_appearances_are_skipped`; (13) `raster::tests::highlights_without_an_appearance_are_drawn`; (14) `raster::tests::malformed_simple_font_widths_keep_later_widths_and_the_font`, `raster::tests::huge_simple_font_first_char_terminates`; (15) `raster::tests::annotation_appearance_box_maps_onto_rect`; (16) `raster::tests::clipping_path_applies_after_any_painting_operator`; (17) `raster::tests::dropping_the_pool_cancels_its_renders`, `raster::tests::the_watchdog_stops_the_render_it_gives_up_on`; (18) `raster::tests::page_contents_and_nested_forms_share_one_budget`, `raster::tests::content_painted_again_and_again_keeps_drawing`, `raster::tests::sampled_functions_with_billions_of_samples_are_refused`; (19) `raster::tests::mesh_shadings_far_off_the_page_render`, `raster::tests::many_overlapping_mesh_triangles_finish`, `raster::tests::mesh_shadings_with_many_patches_or_no_rows_finish`; (20) `raster::tests::function_limits_calculator_arithmetic_and_depth`, `raster::tests::function_limits_public_construction_work_and_depth`, `raster::tests::function_limits_stitching_cycles_are_refused_and_shared_children_work`, `raster::tests::function_limits_invalid_calculator_keeps_the_page_renderable`; (21) `raster::tests::cids_a_substitute_lacks_are_not_drawn_as_other_glyphs`; (26) `raster::tests::accented_letters_the_standard_stand_ins_lack_are_built_from_their_parts`; (22) `raster::tests::variation_sequences_draw_their_kanji_in_a_substitute`; (23) `raster::tests::axial_and_radial_shadings_draw_as_native_gradients`; (24) `raster::tests::tiles_match_full_render`, `raster::tests::skipping_paths_off_the_canvas_changes_no_pixel` (both decode through the cache), `raster::tests::decoded_images_with_identical_dictionaries_keep_their_own_pixels`, `raster::tests::decoded_images_under_a_transfer_function_are_not_shared`; (25) `text::tests::actual_text_replaces_the_glyphs_it_marks_in_every_writing_direction`, `text::tests::actual_text_follows_the_pdf_rules_for_marked_content`; (27) `text::tests::truetype_without_an_encoding_reads_its_codes_as_win_ansi`; (28) `raster::tests::spot_colour_images_run_their_tint_transform_once_per_sample_value`, `raster::tests::images_converted_through_a_table_keep_every_samples_colour`, `raster::tests::a_malformed_tint_for_an_unused_sample_keeps_the_used_colours`; (29) `raster::tests::reading_colors_keep_highlight_annotations_readable`; (30) `text::tests::the_annex_d2_alias_codes_read_as_an_ordinary_hyphen_and_space`; (31) `raster::tests::shadings_with_large_sampled_functions_paint_quickly`, `raster::tests::sampled_functions_interpolate_their_grid_as_before`; (32) `raster::tests::decoded_images_serve_every_size_they_are_drawn_at`; (33) `raster::tests::patch_meshes_keep_their_corner_colours`; (34) `raster::tests::spot_colour_images_run_their_tint_transform_once_per_sample_value`; (41) `raster::tests::a_multiply_highlight_inside_a_transparency_group_keeps_the_text`; (42) `raster::tests::text_rendering_modes_other_than_invisible_draw_type3_glyphs_as_filled`; (43) `raster::tests::lab_honours_the_space_white_point`, `raster::tests::indexed_palettes_over_lab_keep_their_colours` | diff --git a/vendor/hayro-interpret/src/x_object.rs b/vendor/hayro-interpret/src/x_object.rs index 21b134ce8..e98e89217 100644 --- a/vendor/hayro-interpret/src/x_object.rs +++ b/vendor/hayro-interpret/src/x_object.rs @@ -64,7 +64,7 @@ pub(crate) struct FormXObject<'a> { is_isolated: bool, is_knockout: bool, pub(crate) dict: Dict<'a>, - resources: Dict<'a>, + resources: Option>, } impl<'a> FormXObject<'a> { @@ -73,7 +73,8 @@ impl<'a> FormXObject<'a> { // PdfCraft patch: see `context::MAX_PAGE_CONTENT`. let decoded = crate::context::decode_content(stream)?; - let resources = dict.get::>(RESOURCES).unwrap_or_default(); + // PdfCraft patch: retain absence; present malformed dictionaries stay local-empty. + let resources = dict.contains_key(RESOURCES).then(|| dict.get::>(RESOURCES).unwrap_or_default()); let matrix = Affine::new( dict.get::<[f64; 6]>(MATRIX) @@ -189,7 +190,7 @@ pub(crate) fn draw_form_xobject<'a, 'b>( interpret( iter, - &Resources::from_parent(x_object.resources.clone(), resources.clone()), + &Resources::for_form(x_object.resources.clone(), resources.clone()), context, device, ); diff --git a/vendor/hayro-syntax/src/page.rs b/vendor/hayro-syntax/src/page.rs index d7f0ec953..bb0b4abc0 100644 --- a/vendor/hayro-syntax/src/page.rs +++ b/vendor/hayro-syntax/src/page.rs @@ -148,8 +148,8 @@ fn resolve_pages<'a>( ctx.rotate = Some(rotate); } - let resources = Resources::from_parent( - pages_dict.get::>(RESOURCES).unwrap_or_default(), + let resources = Resources::for_page( + pages_dict.contains_key(RESOURCES).then(|| pages_dict.get::>(RESOURCES).unwrap_or_default()), resources.clone(), ); @@ -431,8 +431,8 @@ impl<'a> Page<'a> { .map_or(1.0, |u| u.min(MAX_USER_UNIT) as f32); let ctx = resources.ctx.clone(); - let resources = Resources::from_parent( - dict.get::>(RESOURCES).unwrap_or_default(), + let resources = Resources::for_page( + dict.contains_key(RESOURCES).then(|| dict.get::>(RESOURCES).unwrap_or_default()), resources, ); @@ -674,11 +674,13 @@ impl<'a> Page<'a> { pub struct Resources<'a> { parent: Option>, ctx: ReaderContext<'a>, + // PdfCraft patch: legacy forms without Resources use the page, not their caller. + page_properties: Dict<'a>, /// The raw dictionary of external graphics states. pub ext_g_states: Dict<'a>, /// The raw dictionary of fonts. pub fonts: Dict<'a>, - /// The raw dictionary of properties. + /// The effective property dictionary selected at page or form entry. pub properties: Dict<'a>, /// The raw dictionary of color spaces. pub color_spaces: Dict<'a>, @@ -698,6 +700,26 @@ impl<'a> Resources<'a> { Self::new(resources, Some(parent), &ctx) } + // PdfCraft patch: page-tree inheritance stops at any present Resources dictionary. + fn for_page(resources: Option>, parent: Self) -> Self { + let properties = resources.as_ref().map_or_else(|| parent.properties.clone(), |dict| dict.get::>(PROPERTIES).unwrap_or_default()); + let mut result = Self::from_parent(resources.unwrap_or_default(), parent); + result.properties = properties.clone(); + result.page_properties = properties; + result + } + + /// Create form resources, using page Properties only when Resources is absent. + /// Other resource categories retain their existing parent-chain lookup. + // PdfCraft patch: ISO 32000-2, 7.8.3 and Table 93 require the page origin. + pub fn for_form(resources: Option>, parent: Self) -> Self { + let properties = + resources.as_ref().map_or_else(|| parent.page_properties.clone(), |dict| dict.get::>(PROPERTIES).unwrap_or_default()); + let mut result = Self::from_parent(resources.unwrap_or_default(), parent); + result.properties = properties; + result + } + /// Create a new `Resources` object. pub(crate) fn new(resources: Dict<'a>, parent: Option, ctx: &ReaderContext<'a>) -> Self { let ext_g_states = resources.get::>(EXT_G_STATE).unwrap_or_default(); @@ -708,6 +730,8 @@ impl<'a> Resources<'a> { let shadings = resources.get::>(SHADING).unwrap_or_default(); let properties = resources.get::>(PROPERTIES).unwrap_or_default(); + // PdfCraft patch: generic child resources retain the page origin. + let page_properties = parent.as_ref().map_or_else(|| properties.clone(), |parent| parent.page_properties.clone()); let parent = parent.map(Box::new); Self { @@ -716,6 +740,7 @@ impl<'a> Resources<'a> { fonts, color_spaces, properties, + page_properties, x_objects, patterns, shadings,