Skip to content

fix: draw a vector stroke where its alignment puts it - #887

Merged
kyamagu merged 4 commits into
mainfrom
fix/stroke-line-alignment
Sep 28, 2026
Merged

kyamagu merged 4 commits into
mainfrom
fix/stroke-line-alignment

Conversation

@kyamagu

@kyamagu kyamagu commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #854. Fixes #883.

Two defects that have to land together, because today they cancel.

What was wrong

#854 — vector.draw_stroke() drew every vector stroke as an aggdraw pen centred on the path, whatever strokeStyleLineAlignment said. Stroke.line_alignment has always parsed the field; nothing read it. 33 of the corpus's 50 stroked layers ask for inner or outer.

#883 — _get_object() composited the stroke through a sub-Compositor seeded with the layer's own colour and alpha, then read it out with finish() → result_isolated(), which divides that seed back out. A pixel the stroke covers at 0.498 came out as the stroke's colour at full strength.

That second one is worse than the issue claimed. Dividing the seed out also discarded strokeStyleOpacity entirely: on effects/stroke-composite.psd, a fully covered band pixel reads [1, 0, 0] at 100%, at 50% and at 10% alike, where it should read [0.9, 0.4, 0.4] and [0.82, 0.72, 0.72]. So this is not only about partial pixels.

They cancel. transparency/transparency-group.psd scores exactly 0.0 today only because its 1 px inner stroke runs along y = 0, where the centred pen covers row 0 at 0.498 against the correct band's 1.0, and the isolation inflates 0.498 back to the 1.0 Photoshop paints. Fixing either alone regresses the corpus; test_layer_composite_keeps_a_stroke_past_the_canvas_edge compares that layer against Photoshop's own stored raster pixel for pixel and is the sharpest check on the pair.

What this does

draw_stroke() draws an inner or an outer stroke at twice the width, clipped to the side the alignment names — the workaround SVG and CSS use, a pen being drawable only centred. The clip is by which side of the path a pixel is on, not by how much of it the fill covers: the layer's own alpha already carries the shape's antialiased edge, and multiplying by coverage counts it twice. That is measurable — a coverage clip scores 0.03408 on shape-layer.psd against this clip's 0.01424, worse than doing nothing at all.

The side test is made against 1e-9, not against zero. The fill rasterizer reports coverage of the order of 1e-15 on pixels whole pixels from the path, and an exact comparison reads one of those as a pixel the path clips and hands it the full width of the pen — on combine.psd, 29 such pixels take pen coverage of up to 0.827. The tolerance sits in a measured gap: the largest rounding residue over the whole corpus is 3.6e-15, and the smallest coverage a path really states is 9.5e-07.

_get_object() exits with result_over_backdrop(), as _apply_clip_layers() already does for the same reason.

A centred stroke's raster is unchanged bit for bit, pinned by a test. Its render is not, because of the exit.

Measured

21 stroked fixtures, both force modes, against Photoshop's stored preview, re-measured on top of #884: 26 pairs improve, 15 are unchanged, 1 regresses — stroke.psd force=False, 0.00054 → 0.00059, which is its dashes: strokeStyleLineDashSet = [4.0, 2.0], which psd-tools does not draw. Photoshop's stored band for that layer sums to 212.6 where our solid one sums to 317.4, a ratio of 0.670 against the 4/6 the dashes cut it to. A correct band that is wider is also more wrongly solid.

fixture force before after
layers-minimal/shape-layer.psd both 0.01621 0.01424
effects/stroke-composite.psd True 0.00638 0.00354
vector-mask2.psd False 0.00199 0.00074
descriptors/stroke-color-descriptors-rgb.psd True 0.00728 0.00513
path-operations/combine.psd False 0.01353 0.01038
transparency/transparency-group.psd both 0.00000 0.00000

The document MSE understates it, because it scores colour on pixels the render leaves transparent. On shape-layer.psd, of the 609 pixels both renders leave visible, the RGB error goes 0.01497 → 0.00618 while the alpha channel is unchanged at 0.00591 — a stroke moves colour, not coverage.

Ground truth where it exists: stroke.psd/Rectangle 1 has fillEnabled false, so the channels Photoshop stored for it are its own rasterization of the band. Across its left edge Photoshop reads 1.0 then 0.4706; this draws 1.0 then 0.4667, within a quantization step, where the centred pen read 0.733/0.729 — a column earlier, half of it outside the path.

Tests

Reverting each half separately fails 6 and 3 tests, so both are pinned.

  • test_an_inner_stroke_covers_the_whole_width_inside_the_path — a 3 px inner band covers three columns inside the fill, where the centred pen covered two, one outside the shape.
  • test_a_fractional_stroke_width_matches_photoshops_own_band — 1.47 px against Photoshop's own raster, with the dashes documented.
  • test_a_partly_covered_stroke_pixel_blends_with_the_fill — A partly covered stroke pixel renders as full-strength stroke colour: _get_object() isolates the stroke's sub-compositor #883 on a centred stroke, isolating the exit from the alignment: pen 0.498, Photoshop 0.0667, before 0.129 (the stroke's raw colour), after 0.0643.
  • test_an_outer_stroke_lies_outside_the_path — the branch the corpus barely reaches, forged.
  • test_a_centred_stroke_is_still_the_plain_pen, test_a_stroke_whose_alignment_is_unreadable_is_drawn_centred — the position that did not change, and the degradation for a descriptor stating no alignment (Stroke.line_alignment raises there) or an unknown one.
  • test_an_inner_stroke_wider_than_the_shape_cancels_itself — see the limitation below.
  • test_draw_stroke_reaches_outside_the_canvas moved to a layer whose path leaves the canvas, since an inner stroke no longer hangs off one that does not; test_an_inner_stroke_stops_where_the_path_does pins that, and test_a_centred_stroke_survives_negative_document_coordinates keeps composite: the vector stroke is placed at the wrong offset when the viewport is not at the origin #807 pinned at negative coordinates, where only a centred stroke now reaches.

Two stale comments retired: test_composite.py's bound comment predicted "#854 should fail here" from a 0.0260 that does not reproduce (the render measures 0.01621 at f1256273), and test_vector.py's said a stroke "is still drawn centred whatever alignment it asks for".

Known limitation this introduces

The pen is one outline filled even-odd, so where the shape is thinner than the doubled width the band overlaps itself and cancels. On a 100x100 path an inner stroke of width w loses a square hole of side 2w - 100 once w passes 50. Drawn centred the same cancellation started at w = 100 and left a hole of side 100 - w, so the two cross at w = 2t/3: this is the better of them below that and the worse above, and Photoshop paints solid throughout. The widest stroke in the corpus is 10 px on a far larger shape. Documented in draw_stroke and pinned by a test rather than fixed, because the real fix is the silhouette band below; tracked as #890.

Not fixed here

  • A stroke has no coverage of its own. _get_object() returns the layer's shape and alpha and keeps only the stroke's colour, so nothing outside the layer's coverage can show. inner is therefore fully renderable, center half, outer barely. That invariant is also why this change stays out of the culling path: _stroke_reach() and stroke_bbox() need nothing.
  • A stroke outlines each subpath, not the resulting silhouette. On combine.psd, 322 pixels along the interior seams where the ellipses meet are stroked where Photoshop paints fill, carrying 62% of that document's squared error. Worse, _draw_path() runs pen coverage through the fill's path operations, so intersect-all, intersect-first and subtract-all draw no stroke at all — 3 of the 12 path-operations fixtures are green because the stroke vanished. All pre-existing and unmoved either way by this change. This is why the exit fix alone beats the pair on some path-operations documents (combine.psd force=False: 0.00408 against 0.01038). Filed as composite: a vector stroke outlines every subpath, not the silhouette, and a leading intersect/subtract erases it #889.
  • At force=False the stored raster already carries Photoshop's stroke, so the compositor paints a second one over it; a geometrically correct band double-paints more of the right place. That is the other half of why a few force=False numbers favour the exit alone.
  • Dashes. strokeStyleLineDashSet is parsed and not drawn, as above.
  • Cost. The clip rasterizes the fill a second time, one the compositor computes again in _get_mask under force: draw_stroke goes 0.15 → 0.86 ms per sided stroked layer, and a forced render of effects/stroke-composite.psd 60 → 76 ms. A memo keyed on layer and viewport would remove it; left out as a separate concern.

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings September 25, 2026 10:17
A vector stroke was drawn as an aggdraw pen centred on the path whatever
strokeStyleLineAlignment said, and the stroke's sub-compositor was read
out with finish(), which divides its seed backdrop out. The two defects
cancel: transparency-group.psd scores exactly 0.0 today because a 1 px
inner stroke covers row 0 at 0.498 where the correct band covers 1.0,
and the isolation inflates 0.498 back to the 1.0 Photoshop paints. So
they land together.

draw_stroke() now draws an inner or an outer stroke at twice the width
and clips it to the side the alignment names -- the workaround SVG and
CSS use, since a pen can only be drawn centred. The clip is by which
side of the path a pixel is on, not by how much of it the fill covers:
the layer's own alpha already carries the shape's antialiased edge, and
multiplying by coverage counts it twice, which measures worse than
leaving the bug in. Its raster for a centred stroke is unchanged bit for
bit, though the render is not, because of the exit below.

_get_object() exits the stroke's sub-compositor with
result_over_backdrop(), as the clip-layer path already does: the seed is
the fill being painted onto, not a backdrop to be divided out. Dividing
it out discarded strokeStyleOpacity outright -- a stroke at 10% painted
exactly as one at 100% -- besides turning a partly covered pixel into
full-strength stroke colour.

Measured against Photoshop over 21 stroked fixtures in both force modes:
26 pairs improve, 15 are unchanged, and stroke.psd force=False moves
0.00054 -> 0.00059, which is its unimplemented dashes. On
layers-minimal/shape-layer.psd the RGB error on the pixels both renders
leave visible goes 0.01497 -> 0.00618 with the alpha channel unchanged.

Fixes #854
Fixes #883

Co-Authored-By: Claude Opus 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.

Copilot review overview

🔵 Needs a closer look

A requested partial-opacity regression test remains unresolved in complex rendering changes.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

This PR fixes vector stroke alignment and preserves correct stroke compositing and opacity.

Changes:

  • Supports inner, outer, and centered stroke placement.
  • Preserves partial coverage and stroke opacity.
  • Adds rendering regression tests and changelog documentation.
File Changes
tests/​psd_tools/​composite/​test_vector.py Adds alignment and viewport regression tests.
tests/​psd_tools/​composite/​test_composite.py Adds partial-coverage compositing coverage.
src/​psd_tools/​composite/​vector.py Implements sided stroke rendering.
src/​psd_tools/​composite/​composite.py Composites strokes over the existing backdrop.
docs/​changelog.rst Documents the rendering changes.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/psd_tools/composite/composite.py Resolved
@kyamagu
kyamagu force-pushed the fix/stroke-line-alignment branch from 62a93b2 to 5898f1f Compare September 25, 2026 10:22
The regression test added with the exit fix reads a pixel the stroke
covers in part, which measures the coverage half of #883 and nothing
else. Dividing the seed backdrop out also undid the `shape * opacity`
that `_get_stroke` applies, so a stroke at 50% or 10% painted exactly
what 100% paints on every pixel it covered whole -- and no test failed
if that came back.

`layers/shape-layer.psd`'s `Polygon 1` is a cyan fill under a magenta
1 px stroke, so at a pixel the pen covers whole the red channel reads
the opacity straight back. No fixture ships a stroke below 100%, so the
descriptor is forged. Against the previous exit the 50% case reads the
flat magenta of 100%, so the assertion is not vacuous.

Co-Authored-By: Claude Opus 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.

Copilot review overview

🔵 Needs a closer look

The rendering changes are broad and include documented limitations requiring human review.

Review effort: Lite
Findings: None

Resolved since last review (1)

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.

Copilot review overview

🔵 Needs a closer look

Rendering changes include documented edge cases and substantial compositing behavior changes requiring human review.

Review effort: Lite
Findings: None

@kyamagu kyamagu left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd request documentation updates

The prose added with the alignment and exit fixes recounted what the
renderer used to do and quoted the figures it was measured at. Both date:
the history belongs in #854, #883 and this PR, and a measured MSE moves
under the next rendering change, leaving a comment that reads as fact.

Drops the narrative -- "used to", "was expected to fail at", "the centred
pen it replaces" -- and the measured figures from the two corpus-bound
comments, the `draw_stroke` docstring and the new tests' docstrings. The
numbers that pin behaviour stay where they belong, in the assertions; the
stroke colour and the coverage share are named in code instead of prose.
The changelog loses its corpus statistic for the same reason.

Behaviour, assertions and bounds are unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
@kyamagu
kyamagu merged commit 9d127ca into main Sep 28, 2026
23 checks passed
@kyamagu
kyamagu deleted the fix/stroke-line-alignment branch September 28, 2026 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants