Skip to content

stroke: draw thin strokes at their width, not as hairlines - #80

Merged
timzifer merged 2 commits into
mainfrom
feat/thin-strokes
Nov 16, 2026
Merged

timzifer merged 2 commits into
mainfrom
feat/thin-strokes

Conversation

@timzifer

@timzifer timzifer commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

For timzifer/cera#243.

Strokes thinner than one device pixel were drawn one pixel wide at full coverage, as PDFium does. Many thin lines close together then merge into a solid blot: a QR code of 1 pt strokes or a pointing hand outlined at 0.6 pt becomes a black square in a thumbnail, where MuPDF and Ghostscript draw them grey.

  • By default only strokes of width 0 are hairlines (one device pixel, full coverage). A thin stroke of non-zero width is drawn at its width, with the exact area it covers: as light as it is, and dense thin lines sum to the ink they have (the QR code keeps its pattern). This matches the exact rendering in cera's accuracy suite, which treats only width 0 as a hairline.
  • New StrokeStyle.FullHairlines keeps the PDFium convention for every stroke thinner than a pixel; IsHairline follows it (so callers that keep hairlines out of unions stay consistent).
  • The harness's stilus engine sets FullHairlines, so its comparison with PDFium and the reference is unchanged.
  • Speed: 2000 random 0.4 px polylines on 800×800, at their width vs as hairlines: about the same (slightly faster).

The first commit scaled the coverage of a one-pixel hairline by its width instead; that counts lines closer than a pixel only once (the QR code came out an even light grey), so the second replaces it. Squash on merge.

Tests: thin_test.go (coverage via Stroke, FillShape, FillUnion, ClipStroke; FullHairlines; width 0; scaled transforms; dense thin hatch at half coverage), a union case of thin mirrored strokes, a thin style in FuzzStroke (60 s clean). Existing tests asserting the PDFium convention now set FullHairlines.

Found by the fuzzer on the way, pre-existing on main and not touched here: a hairline (width 0) under a singular matrix with huge coordinates panics in the sweep on the analytic path (caught by Canvas.guard, reported as ErrInternal). Filed separately.

Strokes thinner than one device pixel are drawn one pixel wide, as PDFium
does. At full coverage, many thin lines close together merge into a solid
blot: a QR code of 1 pt strokes or a pointing hand outlined at 0.6 pt
becomes a black square in a thumbnail, where MuPDF and Ghostscript draw
them grey.

By default a hairline of non-zero width is now drawn at a coverage scaled
by its device width (along the widest direction of the transform), through
the mechanism dense dash patterns already use: the scale blitter, no
analytic fast path, drawn on its own in a union. Width 0 stays at full
coverage. StrokeStyle.FullHairlines keeps the PDFium convention; the
harness sets it, so its comparison with PDFium is unchanged.
Scaling a one-pixel hairline by its width counts thin lines closer than a
pixel only once: a QR code of 0.31 px strokes came out an even light grey
instead of its pattern. By default only strokes of width 0 are hairlines
now; a thin stroke of non-zero width is drawn at its width, with the
exact area it covers, so that dense thin lines sum to the ink they have
(and may join unions like other strokes). StrokeStyle.FullHairlines keeps
the PDFium convention for every stroke thinner than a pixel; IsHairline
follows it.
@timzifer timzifer changed the title stroke: scale the coverage of thin hairlines by their width stroke: draw thin strokes at their width, not as hairlines Nov 16, 2026
@timzifer
timzifer merged commit dc4f923 into main Nov 16, 2026
14 checks passed
@timzifer
timzifer deleted the feat/thin-strokes branch November 16, 2026 06:46
timzifer added a commit that referenced this pull request Nov 16, 2026
v0.20.0 draws thin strokes at their width (#80); a module that requires
stilus/gpu alone then gets it.
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.

1 participant