pdf.js.mirror/test/pdfs/knockout_nested_group_alpha.pdf
calixteman d1e9194e1f
Render knockout transparency groups
In a knockout (KO) group each painting operator ("element") composites against
the group's initial backdrop instead of accumulating onto prior elements
of the same group. The backend renders each element to a per-group pooled
temp canvas (keyed off `#groupStackMeta`), builds a binary alpha mask via
a new `feFuncA` filter (`addKnockoutFilter`), `destination-out`s the
group canvas through that mask, restores the initial backdrop into the
cleared footprint for non-isolated groups (cropped to the same mask so
sparse groups don't bleed the whole rectangle), and finally paints the
element on top with the parent's blend mode. Path / clip / transform ops
are mirrored back to the group canvas via `mirrorContextOperations` so
graphics state stays in sync between elements; only the raster pixels
land on the temp canvas.

The temp canvas is forced to source-over for the element raster (`multiply`
on a transparent backdrop would zero the color) and the original GCO is
restored before `copyCtxState` writes back, so the parent's blend mode
survives for the final composite.

Also handled:
  - Nested KO groups (the level is incremented for KO, reset to 0 for
    non-KO subgroups so an ancestor KO doesn't leak in).
  - Non-isolated non-KO subgroups inside a KO parent (`hasInnerBackdrop`
    path: blend the elements against the subgroup's running backdrop for
    color, mask with the elements-only canvas).
  - Soft masks installed inside a KO element (`applySMaskInPlace` in
    `compose`, which runs the SMask destination-in directly on the temp
    canvas; the existing blit-to-suspended step is gated by `if (!ctx)`).
  - Type-3 text, shading fills, image-mask groups, inline images and the
    solid-color mask path: each is wrapped in `#begin/#endKnockoutElement`.
  - `endDrawing` cleanup so cancelled rendering doesn't leak pooled
    canvases or stale knockout state.
2026-05-11 23:08:06 +02:00

1.1 KiB