Repository navigation
fix: draw a vector stroke where its alignment puts it - #887
Merged
Merged
Conversation
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]>
Contributor
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
A requested partial-opacity regression test remains unresolved in complex rendering changes.
Review effort: Lite
Findings: 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.
Co-Authored-By: Claude Opus 5 <[email protected]>
kyamagu
force-pushed
the
fix/stroke-line-alignment
branch
from
September 25, 2026 10:22
62a93b2 to
5898f1f
Compare
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]>
This was referenced Sep 28, 2026
kyamagu
commented
Sep 28, 2026
kyamagu
left a comment
Contributor
Author
There was a problem hiding this comment.
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]>
This was referenced Sep 28, 2026
Merged
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.

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, whateverstrokeStyleLineAlignmentsaid.Stroke.line_alignmenthas always parsed the field; nothing read it. 33 of the corpus's 50 stroked layers ask forinnerorouter.#883 —
_get_object()composited the stroke through a sub-Compositorseeded with the layer's own colour and alpha, then read it out withfinish()→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
strokeStyleOpacityentirely: oneffects/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.psdscores exactly 0.0 today only because its 1 px inner stroke runs alongy = 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_edgecompares 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 onshape-layer.psdagainst 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 of1e-15on 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 — oncombine.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 is3.6e-15, and the smallest coverage a path really states is9.5e-07._get_object()exits withresult_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.psdforce=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.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 1hasfillEnabledfalse, 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_alignmentraises 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_canvasmoved 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_doespins that, andtest_a_centred_stroke_survives_negative_document_coordinateskeeps 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 atf1256273), andtest_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
wloses a square hole of side2w - 100oncewpasses 50. Drawn centred the same cancellation started atw = 100and left a hole of side100 - w, so the two cross atw = 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 indraw_strokeand pinned by a test rather than fixed, because the real fix is the silhouette band below; tracked as #890.Not fixed here
_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.inneris therefore fully renderable,centerhalf,outerbarely. That invariant is also why this change stays out of the culling path:_stroke_reach()andstroke_bbox()need nothing.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, sointersect-all,intersect-firstandsubtract-alldraw no stroke at all — 3 of the 12path-operationsfixtures 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 somepath-operationsdocuments (combine.psdforce=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.force=Falsethe 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.strokeStyleLineDashSetis parsed and not drawn, as above._get_maskunderforce:draw_strokegoes 0.15 → 0.86 ms per sided stroked layer, and a forced render ofeffects/stroke-composite.psd60 → 76 ms. A memo keyed on layer and viewport would remove it; left out as a separate concern.🤖 Generated with Claude Code