PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M2.6: map annotation appearance boxes onto /Rect corner to corner (#277)
cyfung1031118a12a0ad61
diff --git a/crates/render/src/raster.rs b/crates/render/src/raster.rsindex 61fd34807..5cedcf8a1 100644--- a/crates/render/src/raster.rs+++ b/crates/render/src/raster.rs@@ -774,6 +774,55 @@ trailer << /Root 1 0 R >>assert_eq!(px(30, 58), vec![255, 255, 255, 255], "above /Rect");}+ /// ISO 32000-2 §12.5.5: the transformed appearance box is scaled and translated so that its+ /// lower-left and upper-right corners land on those of /Rect. The translation ignored the+ /// scale, so a box that doesn't start at the origin and differs in size from /Rect was drawn+ /// shifted, partly outside /Rect (vendored hayro-interpret patch). MuPDF, Poppler, PDFium and+ /// pdf.js fill /Rect exactly for each of these.+ #[test]+ fn annotation_appearance_box_maps_onto_rect() {+ let filled = |form: &str, content: &str| {+ let pdf = format!(+ "%PDF-1.7+1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj+2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj+3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Annots [4 0 R] >> endobj+4 0 obj << /Type /Annot /Subtype /Square /Rect [10 10 60 40] /AP << /N 5 0 R >> >> endobj+5 0 obj << /Type /XObject /Subtype /Form {form} /Length {} >> stream+{content}+endstream endobj+trailer << /Root 1 0 R >>+%%EOF",+ content.len()+ );+ let mut r = PageRenderer::new(Arc::new(pdf.into_bytes()), RenderConfig::default());+ let p = r.render(RenderRequest { page: 0, kind: RequestKind::Pixels, tile: None, scale: 1.0, tag: 0 });+ assert!(p.error.is_none(), "{form}: {:?}", p.error);+ // The bounding box of everything drawn, in device pixels.+ let (mut x0, mut y0, mut x1, mut y1) = (u32::MAX, u32::MAX, 0, 0);+ for y in 0..p.height {+ for x in 0..p.width {+ if p.rgba[((y * p.width + x) * 4) as usize..][..4] != [255, 255, 255, 255] {+ (x0, y0, x1, y1) = (x0.min(x), y0.min(y), x1.max(x), y1.max(y));+ }+ }+ }+ (x0, y0, x1, y1)+ };+ // /Rect [10 10 60 40] is device x 10..=59, y 60..=89.+ let rect = (10, 60, 59, 89);+ for (form, content) in [+ ("/BBox [10 10 30 30]", "1 0 0 rg 10 10 20 20 re f"),+ ("/BBox [0 0 20 20] /Matrix [1 0 0 1 10 10]", "1 0 0 rg 0 0 20 20 re f"),+ ("/BBox [-20 -5 0 15]", "1 0 0 rg -20 -5 20 20 re f"),+ // Unchanged: a box at the origin, and an offset box as large as /Rect.+ ("/BBox [0 0 20 20]", "1 0 0 rg 0 0 20 20 re f"),+ ("/BBox [100 100 150 130]", "1 0 0 rg 100 100 50 30 re f"),+ ] {+ assert_eq!(filled(form, content), rect, "{form}");+ }+ }+/// A Highlight annotation without an appearance stream (common in older and generated files)/// was not drawn at all. Vendored hayro-interpret patch: its /QuadPoints are filled with /C at/// /CA, blended with Multiply, as PdfCraft draws its own highlights (pdfcraft-annot); MuPDF,diff --git a/vendor/README.md b/vendor/README.mdindex 44553ca7a..1224ec20e 100644--- a/vendor/README.md+++ b/vendor/README.md@@ -7,7 +7,7 @@ the fix. Vendoring copyleft code is never allowed (plan/adr/0001).| 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 | (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` |+| 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) | (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` || hayro | 0.7.1 | Apache-2.0 OR MIT | (1) `RenderSettings::{x_offset, y_offset}` to render one tile of a large page; (2) images over `MAX_IMAGE_PIXELS` (2^28) are skipped (a fuzzed `/W 4294967295` hung in resampling); (3) `tiling_cell_scale` keeps a tiling pattern's cell pixmap within 3000 px a side whatever its `/XStep`/`/YStep` (a fuzzed step far beyond `/BBox` allocated 2 GB); (4) stroke widths clamped to 4 × (canvas width + height) device pixels (a fuzzed `/LW 9223372036854775807` allocated 10 GB in stroke expansion) | (1) `raster::tests::tiles_match_full_render`; (2) `raster::tests::absurd_image_dimensions_are_skipped`; (3) `raster::tests::tiling_cells_stay_small_whatever_the_step`; (4) `raster::tests::absurd_predictor_columns_and_line_widths_render` || hayro-jbig2 | 0.3.0 | Apache-2.0 OR MIT | (1) at most 2^28 pixels per bitmap, page bitmap, halftone grid and grayscale image (each side could be 65535: 4.3 billion pixels decoded one by one); (2) at most 2^26 symbol pixels per symbol dictionary (a fuzzed dictionary decoded for minutes) | (1) `raster::tests::jbig2_regions_of_billions_of_pixels_are_refused`; (2) the nightly `cargo xtask fuzz` finding, which now finishes in about a second |diff --git a/vendor/hayro-interpret/src/interpret/mod.rs b/vendor/hayro-interpret/src/interpret/mod.rsindex fe11dd59d..522012061 100644--- a/vendor/hayro-interpret/src/interpret/mod.rs+++ b/vendor/hayro-interpret/src/interpret/mod.rs@@ -213,13 +213,19 @@ pub fn interpret_page<'a>(// corner with the greatest x and y coordinates) of the// transformed appearance box to the corresponding corners// of the annotation’s rectangle.- let affine = Affine::new([+ // PdfCraft patch: the translation is taken after scaling (it was+ // `/Rect.x0 - box.x0`, right only for a box at the origin or a scale of 1).+ let (sx, sy) = (annot_rect.width() / transformed_rect.width(),+ annot_rect.height() / transformed_rect.height(),+ );+ let affine = Affine::new([+ sx,0.0,0.0,- annot_rect.height() / transformed_rect.height(),- annot_rect.x0 - transformed_rect.x0,- annot_rect.y0 - transformed_rect.y0,+ sy,+ annot_rect.x0 - sx * transformed_rect.x0,+ annot_rect.y0 - sy * transformed_rect.y0,]);// PdfCraft patch: a /BBox or /Matrix that collapses the appearance box to a line