Skip to content

feat: render drop shadow layer effect (takes over #679) - #1069

Merged
kyamagu merged 7 commits into
mainfrom
feat/drop-shadow-render
Nov 16, 2026
Merged

kyamagu merged 7 commits into
mainfrom
feat/drop-shadow-render

Conversation

@kyamagu

@kyamagu kyamagu commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Takes over #679, whose author has not replied to review. composite() read every drop shadow parameter but never drew the effect, so the shadow disappeared whenever a document was composited from its layers (force=True, ignore_preview=True, or after an edit). This renders it.

The commit keeps omar16100's authorship. It is rebased onto current main and addresses the open review items.

What the original PR does

  • composite/effects.py: draw_drop_shadow() grows the layer's silhouette by Spread (the drop shadow's Ckmt), blurs it with a gaussian that takes what is left of Size, and casts it away from the light angle. drop_shadow_bbox() sizes the canvas.
  • composite/composite.py: Compositor._apply_drop_shadows() paints each shadow beneath the layer, before the layer is resolved. It fades with layer opacity but not fill opacity, and "Layer Knocks Out Drop Shadow" cuts it by the layer's own alpha. _effect_reach() adds the shadow's reach to the cull and to a group's content box.
  • Unreadable or non-finite parameters drop the effect rather than the document.
  • Fidelity is held against the four fixtures in tests/psd_files that carry a Photoshop-rendered drop shadow (test_drop_shadow.py).

Changes in this PR on top of #679

Addresses the two inline findings from the review there:

  • Unbounded shadow canvas. The canvas is cut to the part that can land in the viewport, and the size is held to Photoshop's 250 px maximum. A forged Blur or Distance no longer allocates a canvas of gigabytes for a shadow that cannot reach the viewport. The cut alone does not bound a huge Blur, which is why the size cap is there too.
  • Unclamped opacity. Opacity is clamped to 0-100%. A huge finite Opct overflowed float32 into NaN across the document, and 400 or -100 left alpha outside [0, 1]. Non-finite values are still dropped.
  • Rebased onto main. Two tests changed with it: _resolve_source now takes clip arguments, and an unreadable Blur reads as 0 (proposal: give psd_tools.api a typed boundary for descriptor access #789) instead of dropping the shadow.
  • The nested-group "resolved once each" test now allows one extra resolve: a box cut to the viewport's reach misses the group cache once at the edge. Cost is still linear in depth.

Limits

Checks

Full suite (5081 passed), ruff check, ruff format --check and mypy pass locally.

Supersedes #679.

🤖 Generated with Claude Code

omar16100 and others added 2 commits November 13, 2026 17:31
composite() read every drop shadow parameter but never drew the effect,
so the shadow vanished whenever a document was composited from its
layers. Draw it: the layer's silhouette, grown by spread, blurred and
cast away from the light, painted beneath the layer before the layer is
resolved.

- draw_drop_shadow() and drop_shadow_bbox() in composite/effects.py.
  For a drop shadow the Ckmt key is Photoshop's Spread, which grows the
  matte before the blur (choke, which shrinks it, is the inner shadow's),
  and the blur takes what is left of the size.
- The shadow fades with the layer's opacity but not its fill opacity,
  and "layer knocks out drop shadow" cuts it away under the silhouette
  it is cast from.
- The silhouette is the layer's alpha before opacity and fill, read on
  the shadow's own box; a group's is its contents composited over
  nothing.
- The cull and a group's content box include the shadow's reach.
- Painting before the layer is resolved puts the shadow under a
  pass-through group's contents, which blend with it.

The blur sigma, 0.45 x size, is fitted against the four fixtures that
carry a drop shadow Photoshop rendered into its preview, and
test_drop_shadow.py holds each to it.

Rebased onto main and extended after review (#679):

- The shadow canvas is cut to the part that can land in the viewport, and
  the size is held to Photoshop's 250 px maximum, so a forged Blur or
  Distance no longer allocates gigabytes.
- Opacity is clamped to 0-100%; a huge finite value overflowed float32
  into NaN across the document.
- Tests follow main: _resolve_source takes clip arguments, and an
  unreadable Blur reads as 0 (#789) instead of dropping the shadow.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Shadow rendering bypasses allocation-budget checks and can allocate multi-gigabyte canvases for offscreen layers, creating critical memory-exhaustion risks.

3 open issues
What changed in this PR

Adds drop-shadow rendering when PSD documents are composited from layers, taking over #679. Shadows were previously omitted from these renders; contour and noise remain unsupported.

Changes:

  • Implements shadow spread, blur, offset, color, and opacity handling.
  • Integrates shadows beneath layers and groups, including effect bounds and cached group alpha.
  • Adds parameter-validation, compositing, and Photoshop-preview regression tests, plus a changelog entry.
File Description
tests/​psd_tools/​composite/​test_drop_shadow.py Adds shadow rendering, edge-case, and fixture-fidelity tests.
src/​psd_tools/​composite/​effects.py Implements shadow canvas sizing, spread, blur, and offset.
src/​psd_tools/​composite/​composite.py Integrates shadows into layer and group compositing.
docs/​changelog.rst Documents drop-shadow support and its limitations.

Review effort: Balanced

Comment thread src/psd_tools/composite/composite.py Resolved
Comment thread src/psd_tools/composite/effects.py Resolved
Comment thread src/psd_tools/composite/composite.py Resolved
Skip a shadow that paints nothing so it cannot widen a group's shape, use is_group() in _cast_shape, and bring the cull and constant comments in line with what the code does. The changelog entry notes the scipy requirement.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The shadow canvas is checked against max_alloc_bytes before it is allocated, as a stroke canvas is, and an over-budget shadow is dropped. A redrawn shape with its fill off casts no shadow of its own, as it draws no fill.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new test module can fail with ImportError when optional scipy is unavailable, and its source comment includes unstable measurement history that should be reduced to stable behavior.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Low severity Remove drifting measurement and tuning details from source comment

tests/​psd_tools/​composite/​test_drop_shadow.py:810

This new source comment embeds measured MSE values and historical tuning details. The repository's prose policy asks source comments to keep current, stable behavior and move measurements/history to the PR or assertions; these figures will drift without changing what the test verifies. Please reduce this block to the stable fact that the cases cover the listed fixtures/regions, leaving the actual thresholds in _PREVIEWS.

3 resolved since last review

Review effort: Lite

kyamagu and others added 3 commits November 13, 2026 18:19
Four fixtures authored in Photoshop lay one shadow per cell: opacity and
knockout, groups, spread, and blend modes. Each cell must remove at least
20x of the error that rendering without shadows leaves. Known departures
are strict xfails tied to #1075 and #1076.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The module draws shadows, which need the optional composite extra, so it
skips like the other composite tests. The preview comment keeps the stable
reasons and drops the measured figures.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ckdrop

A knockout grid authored in Photoshop shows the shadow surviving under a
layer as (1 - alpha) / (1 - alpha * paint opacity), paint opacity being fill
times layer opacity. The old rule cut by alpha alone, which was right at fill
0 and on hard edges but removed shadow Photoshop keeps under a feathered
layer or a group at full fill. The four knockout xfails in the grid tests
now pass and are removed.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported, and the change is supported by comprehensive rendering, robustness, allocation, grouping, viewport, knockout, masking, and fidelity tests.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@kyamagu
kyamagu merged commit 06bfe3d into main Nov 16, 2026
24 checks passed
@kyamagu
kyamagu deleted the feat/drop-shadow-render branch November 16, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants