Skip to content

raster: flatten filled curves without losing area - #77

Merged
timzifer merged 1 commit into
mainfrom
fix/unbiased-flattening
Nov 13, 2026
Merged

timzifer merged 1 commit into
mainfrom
fix/unbiased-flattening

Conversation

@timzifer

Copy link
Copy Markdown
Owner

Summary

Filled curves were flattened into chords that lie inside convex arcs, so the area between chord and arc was always lost: 0.2 % for a circle with r = 50 px, up to 4.6 % at glyph size. Glyphs came out thin (−1.3 % ink on glyphs-30000-uncached at 150 dpi, measured against a 600 dpi rendering).

  • Each flattening interval is now two lines through an apex placed 4/3 of the sagitta out from the chord. For a parabolic arc the two lines enclose its area exactly; for a cubic interval only a higher-order error remains.
  • The apex lies at most sagitta/3 outside the curve, so intervals may use a sagitta of 3 · flattenTol (fillSag). The polyline still stays within flattenTol of the curve, and edge counts stay similar for larger curves.
  • Curves with a sagitta below 1/64 px are still drawn as a single line.
  • raster.go and shape.go (the unclipped recorder) are changed the same way. Strokes are not affected; they flatten in the stroker.

Accuracy

circle r (px) before cubic after cubic before quad after quad
1.3 −4.60 % −0.09 % −2.53 % +0.12 %
3.07 −2.57 % −0.13 % −2.60 % −0.00 %
12.3 −0.83 % −0.00 % −0.64 % +0.01 %
50.13 −0.21 % +0.00 % −0.21 % +0.00 %

glyphs-30000-uncached: 150 dpi ink vs 600 dpi ink went from 0.9869 to 0.9990.

The scene still misses the pdfbench target against PDFium, now from the other side: PDFium's own flattening loses roughly 0.9 % on these curves. The hairline scene misses because PDFium over-inks edge pixels (+0.5 to 0.8 % on diagonal lines). Both are reference bias, not stilus errors.

Performance

BenchmarkScenes (M4, n=6): small curves now take two lines instead of one, so glyphs-30000-uncached is +7 to +10 % slower. Cached text and all other scenes are unchanged within noise; the geomean over all scenes is −1.2 %.

Test plan

  • New TestCurveArea: cubic and quadratic circles, r = 1.3 to 150 px, within 0.4 %. It fails on the old code.
  • go vet ./..., go test ./... (core, harness)
  • Cross-builds for 386 and js/wasm
  • GPU module: left to CI

🤖 Generated with Claude Code

Filled curves were flattened into chords, which lie inside convex arcs:
the segments between chord and arc were lost, 0.2 % of a circle of
radius 50 px and up to 4.6 % of one of glyph size (r = 1.3 px). Glyphs
came out measurably thin (-1.3 % ink on glyphs-30000-uncached at
150 dpi against a 600 dpi rendering).

Each interval is now drawn as two lines through an apex 4/3 of the
sagitta out from the chord. The two lines enclose the arc's area exactly
for a parabolic arc; for a cubic interval the remaining error is of
higher order. The apex lies a third of the sagitta outside the curve, so
intervals may have a sagitta of 3 · flattenTol (fillSag) and the
polyline still stays within flattenTol of the curve. Curves with a
sagitta below 1/64 px stay one line.

Circles of radius 1.3-150 px now enclose their area within 0.16 %
(TestCurveArea); glyphs-30000-uncached is within 0.1 % of the 600 dpi
rendering. Small curves take two lines instead of one: uncached glyphs
are 7-10 % slower, cached text and the other scenes are unchanged
within noise. Strokes are not affected (they flatten in the stroker).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timzifer
timzifer merged commit ec113f6 into main Nov 13, 2026
14 checks passed
@timzifer
timzifer deleted the fix/unbiased-flattening branch November 13, 2026 14:13
@timzifer timzifer mentioned this pull request Nov 13, 2026
2 of 3 tasks
timzifer added a commit that referenced this pull request Nov 13, 2026
gpu/v0.8.0 required v0.18.0, which lacks the core side of the GPU work
merged since (#62, #63, #65, #66, #67): outside this repository, where
the replace directive does not apply, the module got the older core.
v0.19.0 has it, and filled curves flattened without losing area (#77).
gpu/v0.8.1 is tagged on the commit that merges this.

Co-authored-by: Tim Ebbinghaus <tim@ebbinghaus.name>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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