* M2: a JPX image on a rotated page draws instead of crashing (#3884)
The decode-size hint `image_draw_size` hands hayro-jpeg2000 mapped the
image's corners (w, 0) and (0, h) as points, so it took in the
translation. On a /Rotate 90 page (Acrobat Paper Capture scans) one
corner lands on the device origin, the hint gets a side of 0, and
hayro-jpeg2000 0.3.5 divides the image size by it: "renderer crashed on
page N: attempt to divide by zero" on every rotated JPX page.
The hint now comes from the transform's linear part (the axis lengths
the preview tiny-image check already used, now computed once), and each
side is at least 1. As a side effect, translated JPX images no longer get
an overestimated hint, so they can decode at a reduced resolution level
when drawn small, which is what the hint is for.
Regression test: a 1x1 JPX image filling a 40x40 page at /Rotate 0, 90,
180 and 270, rendered at 0.5x, 1x and 1.5x. It fails before the fix
(/Rotate 90 at 0.5x: attempt to divide by zero) and passes after.
Supersedes the guard in #1883 and does not depend on the hayro-jpeg2000
bump in #2309; both are open and conflicting with main.
Fixes#3884
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2
* M2: decode an offset JPX image area at full size (#3884 review)
hayro-jpeg2000 0.3.5 places a reduced resolution's samples using the
image area's reference-grid offset (XOsiz, YOsiz) unscaled. When a size
hint skips a level, an offset image either loses rows (offset 7, 7: no
reduced row is copied) or is sliced past a reduced row and panics
(offset 7, 0: "range start index 3 out of range for slice of length 2"
at decode.rs:486). The accurate hint from the previous commit makes
reduced decoding more common, so this matters more now.
filter::jpx::decode now passes the hint only when both offsets are 0,
read from the SIZ segment of a raw codestream or a JP2 jp2c box;
codestreams without an offset keep reduced decoding.
Tests: jpx_size_hint_is_dropped_only_for_an_offset_image_area fails
before the fix (the panic above, and 2x2 instead of 4x4 for the other
offsets) and passes after; the (0, 0) case pins that reduced decoding
is kept. image_area_offset_reads_raw_codestreams_and_jp2_files covers
raw, JP2 and truncated inputs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2
* M2: pin invalid /Rotate values in the rotated JPX test (#3884 review)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2
* M2: restore the /UserUnit test's rotations (#3884 review)
7928399 widened the rotation list in the rotated-JPX test with a sed that
also matched the identical loop in user_unit_scales_the_page_and_everything_on_it,
which then failed on /Rotate 45. Only the JPX test was meant to change.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2
* M2: decode a colour-keyed JPX image at full size (#3884 review)
A colour-key /Mask (ISO 32000-2 8.9.6.4) is matched against the decoded
samples in resolve_alpha. A JPX image decoded at a reduced resolution for
the size hint has wavelet low-pass averages instead of its samples, so
keyed pixels next to unkeyed ones turned opaque, and unkeyed averages
could fall in the key range. The accurate hint from the first commit
makes reduced decodes more common.
ImageXObject::decoded_raster now drops the hint when a colour key is the
image's mask (no /SMask stream and no /SMaskInData 1, the precedence
resolve_alpha uses), so it is decoded at full size and resampled with its
mask like an image under any other filter. A /SMask keeps the reduced
decode. The decode cache key follows, since it is built from the same
hint.
Test: jpx_with_a_colour_key_mask_is_decoded_at_full_size decodes a 4x4
one-level JPX image with a 2x2 hint: without a mask, with /Mask [0 0],
and with /Mask [0 0] plus /SMask. The /Mask case gave 2x2 before the fix
and gives 4x4 after; the other two stay 2x2.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2
* M2: pin a colour-keyed JPX image's transparency as drawn (#3884 review)
The colour-key test checked the size the image was decoded at. This one
checks the page: a 4x2 JPX checkerboard of samples 0 and 1 with
/Mask [0 0], drawn over red at 2x1 pixels, where the size hint skips a
resolution level. Half of each pixel's samples are keyed, so both pixels
come out half red (128, 1, 1).
With the fix in decoded_raster reverted, the reduced decode's averaged
samples no longer match the key and the image covers the red: both
pixels are (1, 1, 1) and the test fails.
The codestream is the one inside the 4x2 JP2 fixture in hayro-syntax's
filter::jpx tests.
---------
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
## What this changes
A JPEG 2000 image is now refused when its codestream's SIZ marker declares more than 2^28 pixels, the same cap `image_size_ok` applies to `/Width` x `/Height`. The check runs in `vendor/hayro-syntax/src/filter/jpx.rs` on the header read at full resolution, before `Image::decode` (which makes the infallible allocation), and fails the same way as other undecodable images. It uses the full-resolution size because a `target_resolution` shrinks the reported `width()`/`height()` but each tile's coefficient buffers are still allocated at full size (review on this PR); the header is then re-read at the target resolution only when one is set. Logged as hayro-syntax patch (36) in `vendor/README.md` (main has since taken 29-35).
Fixes#5078
## How you verified it
`cargo fmt --check` and `cargo clippy --all-targets -- -D warnings` are clean on `pdfcraft-render` (which compiles the vendored hayro-syntax). Regression tests in `jpx.rs`'s `tests` module, adopted from the reviewers' patches:
- `a_codestream_over_the_pixel_cap_is_refused` (croquete1): a `/Width 1 /Height 1` image whose single-tile codestream declares 65536 x 65536 (2^32 pixels) returns `None` from `jpx::decode`, with and without a 1x1 target resolution (the target case has four decomposition levels, so the target really shrinks the reported size to 4096 x 4096, under the cap); the same builder at 1x1 decodes, so the `None` is the cap, not a parse error.
- `jpx_size_guard_uses_full_resolution_before_target_resolution` (Harrison-Booth): `decode` delegates to `decode_with_pixel_limit`, so a 1024-pixel limit can show that a 64x64 image with four decomposition levels, which a 1x1 target decodes under 1024 pixels, is still refused at that target. The first version used `PALETTE_JP2`, which a target can't shrink (its JP2 palette makes hayro-jpeg2000 drop the target, and it has no decomposition levels), so it passed with the check measuring the target size; the current one was seen failing at that assertion with the header read at the target resolution before the check, and passing with the fix.
The vendored crates are excluded from the workspace, so CI's `cargo test --workspace` doesn't run these unit tests. They were run on a copy of `vendor/hayro-syntax` (with the vendored `hayro-jbig2` patched in): `cargo test --lib filter::jpx`, 12 passed.
- [x] `cargo fmt --check`
- [x] clippy with `-D warnings` on the crates I changed
- [x] tests pass on the crates I changed
## Anything a reviewer should look at first
- `Image::new` only parses the header (croquete1 measured the 65536 x 65536 single-tile case at about 0 ms), so the cap runs before any coefficient buffer is allocated.
- `u64::from(width) * u64::from(height)` can't overflow (two u32 values). Component and tile counts aren't bounded here; that's a separate allocation path, outside #5078.
- `MAX_PIXELS` is a local constant. The shared `MAX_IMAGE_PIXELS` lives in `hayro`, which hayro-syntax can't depend on.
- rustfmt wasn't run over `jpx.rs` (the vendored crate is outside the workspace, and the workspace config would reformat older blocks). The new lines are formatted by hand.
vendor/README.md conflicted: main took hayro-interpret patch 104 (RGB
colours in isolated DeviceCMYK groups). This branch's viewer overrides
become patches 105 (annotation_overrides) and 106 (appearance_overrides)
in the README, in vendor/hayro-interpret/src/interpret/mod.rs, in the
raster test and in docs/acroform-script-workflows.md.
* fix(render): an RGB colour in an isolated DeviceCMYK group blends in CMYK, as its ink
Part of #4887. An isolated DeviceCMYK group blended in CMYK only when every
colour in it had process tints (DeviceCMYK, DeviceGray); one RGB fill sent
the whole group back to RGB blending.
Vendored hayro-interpret patch (104): Color::ink_tints gives DeviceRGB,
CalRGB, Lab and three-component ICCBased colours the ink that draws back to
them through the bundled CMYK profile (new cmyk_inverse module: black
0.4*k0^2 + 0.6*k0^8 with k0 = 1 - max(r, g, b), matched to Acrobat on
synthetic pages; then cyan, magenta and yellow by at most 12 Newton steps
against the profile; one-entry per-thread cache). ICCBased CMYK still has no
tints.
Test an_rgb_colour_in_an_isolated_cmyk_group_blends_as_its_ink: 50% grey
under Difference with black within 10 levels of Acrobat's 137 112 106;
in-gamut colours draw as themselves; red within 8 of Acrobat's 238 32 37.
With the additive arms reverted the grey draws 93 97 96 and it fails.
#5228's control now uses an RGB image, which still has no tints.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(render): an RGB colour with a non-finite component has no CMYK ink
`to_rgba` turns a non-finite component into some finite colour, so the
inverse could not see it and gave that colour's ink. `ink_from_srgb` now
declines such a colour and the group draws it as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: KenM76 <KenM76>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
FlexArrayIter::next::<T> stepped over a lone-sign slot before finding that
null is not a T, so a caller that then read the slot as another type got
the item after it. The slot is now passed only when null is a T, as a value
of another type is left in place. The sign-token tests also require the
lone-sign fixtures to parse and check raw_iter's null slots directly.
A lone + or ++ passes the first pass, which skips it as a number, but no
number is read from it (ISO 32000-2 7.3.3). Dict::entries and Dict's Debug
unwrapped the failed read and panicked, Array::raw_iter did too, and
Array::iter stopped there and dropped the items after it.
Vendor patch (46): a dictionary leaves such an entry out, as Dict::get
already did (7.3.7), and an array reads it as null in its slot (7.3.9). A
typed read still ends at it, as at any value of another type.
- A page action or timer whose edits are refused shows nothing it did:
view changes go in only after apply_script_outcome accepts them.
- js_advance runs at most its 6,000-run cap; the last call only moves
the clock (Session::run_timers_at_most).
- Field callbacks see the document's layers (getOCGs), also after a
batch rebuilds the runner.
- The viewer's hit test uses what scripts show (shown_by_scripts): a
frame they showed takes presses, one they hid doesn't.
- Print previews draw without the live annotation and appearance
overrides.
- An appearance override naming no form (a cleared icon) draws nothing
instead of the button's own face (hayro patch 105).
- Page actions and timers wait while a dialog or prompt is open.
- A timer editing the focused field refreshes its untouched draft.
## What this changes
lopdf's fallback loader (used by `inspect` when the lazy reader fails, and always by `attachment_data`) parsed every object stream eagerly, capping only decoded bytes. A stream of `0 0 0 …` inside the 256 MiB limit built ~120M `Object`s (10+ GB) and aborted. This bounds what it builds:
- **Per top-level object:** at most 4,194,304 objects (`parser::MAX_OBJECT_NODES`, the same as cos's `MAX_NODES`), counting every array element and dictionary value at any depth. One budget is threaded through `array` / `_dictionary` / `_direct_object`. An object is charged once it is built, so failed `alt` branches cost nothing, and an exhausted budget is a nom `Failure`.
- **Per object stream:** at most 1,000,000 index entries (cos's `/N` clamp), and the index list is no longer allocated from the whole block.
- **Per load:** at most 8,388,608 objects across all object streams, in an atomic `NodeBudget` shared by rayon workers. A member reserves up to its own byte length (every object takes at least one byte) and refunds what it doesn't use, so parallel members can't hold the budget between them. Once the budget is spent, the remaining members are skipped with a warning.
- **`convert` in `crates/render/src/structure.rs`:** the depth limit is now 128, one level per array or dictionary, matching cos's parser. Before, it was 100 with two levels per dictionary, so ~51 nested dictionaries alone forced the lopdf fallback.
Logged as lopdf patch (7) in `vendor/README.md`.
Fixes#5083
## How you verified it
`cargo fmt --check` on `pdfcraft-render` and `cargo clippy -p pdfcraft-render --all-targets -- -D warnings` (which compiles the vendored lopdf). Regression tests in `crates/render/src/inspect/tests.rs`, where lopdf patches are tested in-tree (the vendored lopdf's own tests need its missing `assets/example.pdf`):
- `object_stream_members_over_the_node_cap_are_skipped` (croquete1): a member that is one array of 4.3M elements is skipped at the per-object cap; a small member is kept.
- `object_stream_index_entries_past_the_clamp_are_ignored`: of 1,000,001 distinct members, the one past the 1,000,000-entry clamp is never built.
- `object_streams_of_one_load_share_their_node_budget`, `object_streams_loaded_in_parallel_share_their_node_budget` and `a_malformed_object_stream_member_refunds_its_reservation` (Harrison-Booth): a small shared `NodeBudget` bounds the members of several streams, serially and across threads, and a malformed member refunds what it didn't build. `NodeBudget` and `ObjectStream::new_within` are now public for this.
- [x] `cargo fmt --check`
- [x] clippy with `-D warnings` on the crates I changed
- [x] tests pass on the crates I changed (`cargo test -p pdfcraft-render --lib object_stream`: 9 passed, including the five new tests)
## Never-crash
- [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests
- [x] No indexing or slicing with input-derived positions; the budget uses `checked_sub` and a `min`-bounded reservation; index entries and objects built are capped; nesting stays bounded (128 in `convert`)
- [x] No `unsafe`
- [x] The abort this fixes has synthetic regression tests (above)
## Provenance
- [x] No assets, fonts or copied code; the limits follow `pdfcraft_cos`'s own caps
## Security
- [x] No change to `--root`, MCP, the control channel or network access
## Parity and reach
- Not applicable: an internal robustness fix to the fallback loader, with no user-facing feature or parity entry. The vendored-patch log (`vendor/README.md`, lopdf (7)) is updated.
## Anything a reviewer should look at first
- `vendor/README.md` now lists the tests above for patch (7).
- Ordinary (not compressed) indirect objects are capped at 4M objects each but aren't charged to the per-load budget; their total is bounded only by file size.
- `get_compressed_object` still decodes and re-parses the whole stream per lookup (pre-existing); it now gets one object's budget.
- Objects nested 101–128 deep now reach the Inspector through the lazy reader rather than being skipped by the fallback. The Inspector's own walkers cap depth at 32 or less.
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Added limits to PDF object parsing and object-stream processing. Loading now parses only selected members, while lookups parse only the requested member. Members that exceed available limits are skipped in a deterministic order.
* Increased the supported nesting depth for dictionary and object conversion, allowing more deeply nested content to be processed.
* Documented that conversion failures can trigger eager loading as a fallback.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Binds Properties dialog drafts to the vendored edit-session crate (byte-identical to PerchAnton/edit-session@033f7c3a, ledger in vendor/README) and adds replayable UI scenarios. Includes a merge of main and a test covering the clean Properties inspection-tab live refresh.
* Render: an isolated CMYK group of any size blends in CMYK, in bands
hayro patch (27) blended an isolated DeviceCMYK transparency group in CMYK only up to 2^24
device pixels (a 4096 x 4096 square); a larger one was blended in RGB. Print as image renders a
whole page at once, up to 8192 px on the long side, so a page enlarged to 11 x 17 in at 600 dpi
(about 43 MP) blended any group covering most of it in RGB, and a print test file's CMYK blend
patches drew their X's. The same applied to exporting a large page as an image.
A larger group is now blended in bands of whole rows, each within the old limit: both ink passes
and the tint conversion run per band, so their memory stays as before, and only the finished
group (4 bytes a pixel) is kept whole. Every band is blended before any is drawn, so a group a
pass has no tints for is still drawn as before, whole.
The regression test lowers the band to 7 rows (set_cmyk_band_pixels, hidden, per thread like
set_skip_offscreen_paths) and checks the banded group matches the whole one across an
anti-aliased edge, and is CMYK's green where magenta is under black with Difference.
* Test: CMYK green through the profile is (0, 166, 81); check green against RGB's magenta
Moves the egui crates from 6d518fc7 to the tip of the fork's
migration/egui-037-20261024 line. It adds two patches:
- E22: the fallback JavaScript highlighter no longer colours
TypeScript-only keywords (storytold/egui#24).
- E23: dragging a Resize corner keeps a direction that isn't
resizable (storytold/egui#25).
The rest is lint and PATCHES.md only. Default fonts are unchanged,
so the egui-icons provenance link only moves to the new rev. The
vendor graph test and vendor/README.md follow the pin.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main took hayro-interpret patch (76) for non-embedded Arial and Times New Roman
CIDs, so the View auto state becomes patch (103) in the ledger and the code
comments. The view.layers-panel note no longer lists the Print event's auto
state as missing: main applies it when printing (print.print-ready-pdf).
Main's #6663 added InterpreterWarning/RenderWarning::Type3WidthsMissing (patch
90) and a decoded-image cache that records warnings as flags (patch 91). Both
warning variants are kept in every match (renderer, automation, CLI), and
MalformedIndexedImage takes flag 32 in warning_bit/warnings_in. The ledger
keeps main's rows with (80) added; both sides' raster test modules are kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
Main's #6569 (hayro-interpret patch 101) reads a symbolic TrueType font that
names no glyphs and keeps the Macintosh order as Mac OS Roman; this branch's
patch (79) reads one that names its glyphs by those names. Both are kept: the
renderer struct holds both fields, and the editor's unicode_map applies the
glyph-name reading only to a font whose post table names glyphs, then the Mac
OS Roman one, so each font takes the reading the renderer gives it. The ledger
keeps main's rows with (79) added; the editor reuses descriptor_flags and
MAX_SFNT.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
Keeps main's vendor ledger with hayro-interpret patch (61) added, main's
BlendMode import beside the patch's Point and Cow in path.rs, and both sides'
stroke test modules (main's stroke_degenerate_cubic and stroke_zero_end_tangent
fix vello_common's stroker; this patch shapes subpaths in hayro-interpret).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
crates/engine/src/print_raster.rs: the print raster keeps `for_print` (the View
auto state is not applied when printing) and main's Print-event layer states.
vendor/README.md: hayro-interpret patch (76) and its tests follow main's new
patch (89); main's hayro (31) and hayro-jbig2 (17) entries are kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
Patch 82 sent every soft mask through the decoded-image cache, so a mask that patch 91 keeps failed inside its image's entry got a second entry and counted as another decode. Only a mask averaged down needs an entry of its own; any other decodes as it did before patch 82.