Pre-compute seam rows during edge bucketing where one edge's yH matches
another edge's yL at a mid-pixel-row y-coordinate. For these rare rows
(typically 2 per glyph), split into two sub-rows and sum absolute alpha
values to avoid winding cancellation. Non-seam rows take the exact same
code path with only a single branch check overhead.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When two contours share a y coordinate that falls mid-pixel-row,
their edges contribute opposite-winding cover values at different
fractional heights. The analytic formula |cover·512 − area|
computes the signed sum, which partially cancels, producing a
visible one-row artifact.
Fix by detecting these seam rows (an edge yH matching another
edge's yL within the same pixel row) and splitting into sub-rows.
Each sub-row's |cover·512 − area| is computed independently, then
the absolute alpha values are summed. This gives correct coverage
because each sub-row has consistent winding direction.
Performance impact is negligible: seam detection piggybacks on the
existing edge-removal loop (zero cost for non-seam rows), and
sub-row processing only runs for the rare seam rows (typically 1-2
per glyph).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Two bugs fixed:
- atan2f returns [-pi,pi] but sweep gradient angles are in [0,2*pi];
normalize pixel angle to [0,2*pi) so the seam matches the OT spec.
- REFLECT extend mode was identical to REPEAT because floorf after
the initial mod made period always 0.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When the current transform has rotation/skew, push_clip_rectangle now
rasterizes the actual transformed quad using edge cross-product tests
instead of approximating with an axis-aligned bounding box.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Track bounding box (min_x/y, max_x/y) on all clip masks. Paint
callbacks (solid fill, image, gradients) now iterate only within
the clip bounding box instead of the full surface.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Move pixel helpers (div255, src_over, alpha_mul, pack_pixel) to
hb-raster.hh as shared inline functions with hb_raster_ prefix.
Move all blend mode implementations and hb_raster_composite_images()
to hb-raster-image.cc for reuse outside of paint.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Implement a software paint backend (hb_raster_paint_t) that renders
COLRv0/v1 color glyphs into BGRA32 pixel buffers with no external
dependencies. Implements all 14 hb_paint_funcs_t callbacks including
solid fills, linear/radial/sweep gradients, glyph clipping, rectangle
clipping, group compositing, and BGRA image blitting.
Update hb-raster utility to detect color fonts and use the paint path,
outputting PPM (RGB) instead of PGM (greyscale).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Define HB_RASTER_FLAT_THRESH 0.25f for the maximum Bézier deviation in
pixels; each flattener squares it locally.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Same approach as the quadratic: control-point deviation also shrinks 4×
per de Casteljau subdivision, so err² shrinks 16× per level, giving the
same upfront subdivision-count formula. The cubic adds a constant third
difference d³f = 6·a·h³ and one extra accumulator in the inner loop.
Keep the recursive de Casteljau version as flatten_cubic_recursive
(static HB_UNUSED inline, depth=0 default). flatten_cubic() dispatches
to flatten_cubic_fd() by default.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Revive the recursive de Casteljau flattener as flatten_quadratic_recursive
(static inline, depth=0 default argument) alongside the forward-differencing
flatten_quadratic_fd. flatten_quadratic() dispatches to the fd variant by
default.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Exploit the property that quadratic Bézier error shrinks exactly 4×
per de Casteljau subdivision. Compute the subdivision count upfront,
then iterate with constant-cost additions (forward differencing)
instead of recursive branching. ~1.5% speedup.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace all hardcoded 64/63/6/128/8192/4096/13 constants with
HB_RASTER_PIXEL_BITS / HB_RASTER_ONE_PIXEL / HB_RASTER_PIXEL_MASK /
HB_RASTER_FULL_COVERAGE macros. Changing HB_RASTER_PIXEL_BITS from
6 (26.6, current) to 8 (24.8, like FreeType's rasterizer) switches
the entire pipeline.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Avoid recomputing y_top = py << 6 inside edge_sweep_row; the caller
already has it.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove the clamp and ×128 multiply from the prefix_sum_cover loop,
leaving just accumulate + store. The ×128 (<<7) is done in the
NEON/SSE2 sweep after widening to int32, where it's a free instruction.
The clamp is no longer needed: raw cover_accum fits int16 for any
reasonable font, and the alpha formula already clamps |val| to 8192.
~20% speedup at size 200.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
New bucket edges are processed inline for the current row. Only edges
that span beyond the current row are added to the active list. This
avoids push/remove_unordered churn for the majority of flattened curve
segments which typically span only 1-3 pixel rows.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Clamp cover_accum to [-64, 64] via hb_clamp before multiplying by 128
in the prefix sum. Without the clamp, overlapping contours with
winding count > 3 would produce cover_accum * 128 > 32767, overflowing
int16. The clamp is semantically correct since the alpha formula
already clamps |val| to 8192 (full coverage).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move hb_raster_image_t implementation into its own file. hb-raster.cc
now contains only the SECTION documentation header. File layout:
hb-raster.cc — SECTION docs
hb-raster-image.cc — hb_raster_image_t API
hb-raster-draw.cc — hb_raster_draw_t API + rasterizer
hb-raster.hh — shared hb_raster_image_t struct definition
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move hb_raster_edge_t and hb_raster_draw_t struct definitions into
hb-raster-draw.cc since they are only used there. hb-raster.hh now
contains only hb_raster_image_t, shared between the two .cc files.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move all hb_raster_draw_t implementation (API, draw callbacks, Bézier
flattening, analytic coverage rasterizer, SIMD sweep) into its own
file. hb-raster.cc keeps only the hb_raster_image_t API and the
SECTION header. hb-raster.hh remains as the shared internal header.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Bucket edges by their starting pixel row during render instead of
sorting. Maintain an active edge list with O(1) removal via
remove_unordered(). Only grow the bucket vector-of-vectors, never
shrink, so inner vectors keep their allocations across renders.
Eliminates the O(n log n) sort that was dominating render time.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add API to recycle a rendered image back to the rasterizer. The draw
object keeps the image and its buffer for reuse by the next render()
call. After the first render+recycle cycle, render() does zero
mallocs (the image struct and pixel buffer are both reused).
Also switch hb_raster_image_t to use hb_vector_t<uint8_t> for its
pixel buffer so resize tracking is automatic.
Update hb-raster-all benchmark to use recycling.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Move the per-row working arrays into the rasterizer object so they
persist across render() calls and only realloc when width grows.
Use resize_dirty() since we memset immediately after. Reduces
per-render mallocs from 5 to 3 (image struct + pixel buffer remain).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Extract rasterize_edges_to_cells(), prefix_sum_cover(), and
sweep_row_to_alpha() from hb_raster_draw_render() so they show up
individually in flamegraphs. Mark cell_add and edge_sweep_row as
HB_ALWAYS_INLINE.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Bypass hb_vector_t bounds checking in the edge iteration, prefix sum,
and scalar sweep loops. Profiling showed 24% of time in operator[].
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Use int16 for the cover array (values max ±8192, fits int16).
Halves cover array footprint; NEON loads 8×int16 and widens.
- When cover doesn't cancel at row end, compute the constant alpha
byte once and memset the output tail instead of filling, sweeping,
and clearing the working arrays.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Cast through void* before __m128i* to silence Clang's -Wcast-align.
The _mm_loadu/storeu intrinsics handle unaligned access correctly;
the warning is spurious.
Also document hb_raster_image_t and hb_raster_draw_t typedefs in
hb-raster.h.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Document all hb_raster_image_t and hb_raster_draw_t functions with
GTK-Doc style comments. Add SECTION header for hb-raster. Fix
hb_cairo_font_face_set/get_raster() doc comments to use XSince and
standard Return value format.
Also add hb-raster section to harfbuzz-sections.txt and include
hb-raster.xml in harfbuzz-docs.xml Integration API chapter.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The prefix sum of cover propagates across the entire row, but the
sparse x_min/x_max tracking only covered cells directly written by
edges. When the right edge falls on the image boundary (out of
bounds), the sweep stopped at the left edge column, leaving interior
pixels at zero.
Fix by extending the sweep to width-1 when cover_accum is non-zero
after the prefix sum, filling the extended range with the constant
accumulated cover value. For well-formed closed shapes where edges
cancel, x_end == x_max and there is no extra work.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Mirror the NEON vectorized sweep for x86-64 using pure SSE2 baseline
intrinsics (no SSSE3/SSE4.1 required). Emulates abs, min, and
multiply-by-255 with basic SSE2 ops.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Split the per-row sweep into a sequential prefix-sum pass followed by
a vectorized alpha computation pass. The NEON path processes 8 pixels
per iteration using vabsq/vminq/vmlaq and narrows to bytes in-place.
Significant speedup at large font sizes where wide scanlines dominate.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Precompute delta_fy = 64 * total_dy / total_dx once per edge per row,
then accumulate instead of dividing per column.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace dx/dy fields with a 16.16 fixed-point slope precomputed at
edge creation time. The two integer divisions per edge per scanline
in edge_sweep_row become multiply+shift. Same struct size.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Maintain a monotonically-advancing start index that skips past edges
whose yH has fallen below the current row. This avoids re-scanning
the dead prefix of the sorted edge array on every scanline.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Use hb_qsort() instead of qsort() to avoid function-pointer overhead
- Use >> 13 instead of / 8192 for alpha computation (unsigned shift)
- Track per-row x_min/x_max of touched cells; only sweep and clear
that range instead of memset'ing the full width every row
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>