Repository navigation
M11: measure every use of an image drawn through deeply nested forms (#1811) - #1933
Merged
Merged
Conversation
…torytold#1811) The effective-resolution walk skipped any form XObject already visited once when it was nested more than four deep, with one seen-set for the whole document. A later, larger use of the same form was never measured, so Reduce File Size and Optimize PDF resampled the image for its smallest use: a 1200 px image drawn 1 in and 8 in wide through six nested forms came out 150 px wide, about 19 ppi on the large copy. - Cycles are detected on the current path of forms, not document-wide. - A form with its own resources drawn again at the same scale (only moved) is skipped, as that can't change any image's resolution; forms without resources draw their caller's images and are always walked. - Repeat walks are capped at 100,000. If the cap or the 12-level depth limit leaves a use unmeasured, every image gets INFINITY: recompressed as the settings say, never resampled. Fixes storytold#1811 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mlkonrad
force-pushed
the
fix/optimize-deep-form-uses
branch
from
October 14, 2026 16:54
a9012f3 to
963fcb0
Compare
# Conflicts: # crates/optimize/tests/optimize.rs # parity/acrobat-features.toml
Member
|
Thank you @mlkonrad for fixing optimize downsampling images that deeply nested forms draw larger (#1811)! It's merged into main now. If you haven't already, please come and join us on Discord: https://discord.gg/artcraft |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Fixes #1811. The findings and reproduction are in the issue comment.
effective_resolutionsskipped a form XObject it had already visited once (anywhere in the document) when it was nested more than four deep. A later, larger use of that form was never measured, so Reduce File Size and Optimize PDF resampled the image for its smallest use. Onmain, a 1200 px image drawn 1 in and 8 in wide through six nested forms is resampled to 150 px, about 19 ppi on the large copy. With the fix it measures 150 ppi and is left at 1200 px.The walk (
crates/optimize/src/images.rs) is now a smallWalkstruct:/Resources, same linear part of the CTM (ppi doesn't depend on translation). A form tiled many times at one size is still walked once. That's also cheaper than before, since shallow forms used to be re-walked on every page. A form without/Resourcesdraws its caller's images, so it is always walked.INFINITY: recompressed as the settings say, never resampled. Before, those uses were silently ignored.How you verified it
cargo fmt --check,cargo clippy -p pdfcraft-optimize --all-targets -- -D warningscargo test -p pdfcraft-optimize(17 passed), plus the optimize/reduce tests inpdfcraft-engineandpdfcraft-automationcargo xtask layers,assets,parity,wasmNew tests in
crates/optimize/tests/optimize.rs. All four fail onmain(fix reverted) and pass with it:a_deeply_nested_form_is_measured_at_its_largest_use: 1, 6 and 12 levels, both uses on one page and on two pages. On main: 1200 ppi instead of 150 at 6 levels.a_form_drawing_itself_is_walked_once_per_use: a self-drawing form. On main it was followed four more levels at 2× each and recorded 75 ppi for draws no renderer makes.a_shared_form_without_resources_draws_each_callers_image: one resource-less form reached through two chains naming different images. On main the second image wasn't measured at all.images_are_not_resampled_when_some_uses_could_not_be_measured: 13 levels (depth limit), and a 12-deep 3-way fan-out of resource-less forms (≈265k walks, budget). On main: measured as 1200 ppi and resampled.Never-crash
unwrap/expect/panic!/unreachable!/todo!outside testsunsafeProvenance
Parity and reach
optimize.downsample-imagesnotes and evidence updated;crates/optimize/README.mdupdateddoc_optimizeanddoc_reduceuse the same codeAnything a reviewer should look at first
process/run, this changes only the walk.🤖 Generated with Claude Code