Commit Graph
20076 Commits
Author SHA1 Message Date
Behdad Esfahbod dd4758159a raster: use cubic curve-based chord error bound 2026-02-21 20:06:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 6421457605 hb-raster: introduce HB_RASTER_FLAT_THRESH
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>
2026-02-21 19:49:15 -03:00
Behdad Esfahbod 1a5d3f8c12 hb-raster: Use 6 bits of subpixel for positioning 2026-02-21 19:41:48 -03:00
Behdad Esfahbod c05dcff644 hb-raster: Remove stale comment 2026-02-21 19:33:28 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 811da7bf30 hb-raster: use forward differencing for cubic Bézier flattening
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>
2026-02-21 19:29:26 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 1f873fc632 hb-raster: keep both quadratic flatteners, dispatch to forward-differencing
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>
2026-02-21 19:22:56 -03:00
Behdad EsfahbodandClaude Opus 4.6 f3be7f2ef1 hb-raster: use forward differencing for quadratic Bézier flattening
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>
2026-02-21 19:05:50 -03:00
Behdad Esfahbod 38a3c13ec7 hb-raster: Use 8 bytes subpixel resolution
So we can generate smooth 256 levels of horizontal/vertical lines.
2026-02-21 18:38:04 -03:00
Behdad EsfahbodandClaude Opus 4.6 3ba27ca777 hb-raster: make fixed-point precision configurable
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>
2026-02-21 18:27:24 -03:00
Behdad EsfahbodandClaude Opus 4.6 ecce9de28a hb-raster: pass y_top directly to edge_sweep_row
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>
2026-02-21 18:08:19 -03:00
Behdad EsfahbodandClaude Opus 4.6 f8bcf23d33 hb-raster: defer ×128 scaling from prefix sum to SIMD sweep
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>
2026-02-21 18:03:29 -03:00
Behdad Esfahbod da2f665d6a hb-raster: Use a vector extend() 2026-02-21 17:30:28 -03:00
Behdad Esfahbod 8c987819b7 Revert "hb-raster: process single-row edges without active-edge list"
This reverts commit 541d7dab7bbf1a89103edda43ca58617408eb8bb.
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 e0a19e3543 hb-raster: process single-row edges without active-edge list
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 2b8d16dde0 hb-raster: clamp cover_accum to avoid int16 overflow
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 13a6f251e8 hb-raster: split image API into hb-raster-image.cc
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 a089a19c76 hb-raster: move draw-specific types from .hh to hb-raster-draw.cc
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 65c16d7472 hb-raster: split draw/rasterizer into hb-raster-draw.cc
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 3b34cf93d2 hb-raster: replace edge sort with O(n) bucket-by-row
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 c34428cea2 util: guard hb-ft-raster build on HAVE_FREETYPE
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 3bed6db1d9 Move hb-draw-all, hb-paint-all, hb-raster-all to util/ as uninstalled binaries
Move perf tools from perf/ and test/raster/ to util/ alongside the
other uninstalled utilities.  Remove the now-empty test/raster/ directory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 d92bfc4ca5 util: add hb-ft-raster tool for pixel-level comparison
Same shaping/positioning pipeline as hb-raster but uses FreeType's
rasterizer (FT_Outline_Render) instead of hb_raster.  Useful for
comparing coverage values between the two rasterizers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 7d008bf735 hb-raster: add hb_raster_draw_recycle_image() for zero-malloc rendering
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 14ffc6f874 hb-raster: cache row_area/row_cover in hb_raster_draw_t
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 10a486dcce hb-raster: split render into named functions for profiling
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 967633ef61 hb-raster: use .arrayZ[] instead of operator[] in hot paths
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 2a2fc9a9da hb-raster: reduce sweep memory traffic
- 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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 920c5605bb hb-raster: fix -Wcast-align warnings in SSE2 path
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 0b97882e58 hb-raster: add GTK-Doc comments for all public API
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 914d2c12ed hb-raster: fix sparse sweep when cover doesn't cancel at row end
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 27c500f6be hb-raster: add SSE2 alpha sweep path
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 f5846a9e1b hb-raster: NEON-vectorize the alpha sweep
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 fea61c8ba1 hb-raster: use incremental fy in column-crossing loops
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 06842d1a42 hb-raster: precompute edge slope to avoid per-row divisions
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 5bb6140c25 hb-raster: skip expired edges with advancing start index
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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 68c30811c2 hb-raster: minor rasterizer optimizations
- 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>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 d3c6a807d3 hb-raster: replace tile-based sampled rasterizer with analytic coverage
Replace the multi-sample tile rasterizer (NEON / SSE4.1 / scalar variants)
with an analytic-coverage approach that computes exact area and cover
contributions per pixel cell per edge.  This eliminates the tile bookkeeping
(tile_offsets, tile_edge_indices) and SIMD-specific code paths in favour of
a simpler, portable scanline sweep that yields higher quality output.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 a7377f7678 hb-cairo: render at device-pixel resolution when --raster is active
Instead of rendering the hb-raster image at subpixel resolution
(x_scale = font_size * 64 units/em, causing Cairo to resample 64×),
render at device-pixel resolution using the scale_matrix from the
scaled font.

The rasterizer transform is set to (px_x/x_scale, 0, 0, px_y/y_scale)
where px_x = scale_matrix.xx = font_size in device pixels per em.
The image is then placed with cairo_scale(1/px_x, -1/px_y) to map
raster pixels 1:1 to device pixels with Y-flip.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 1c23fc51b0 hb-cairo: add hb_cairo_font_face_set/get_raster(); wire up hb-view --raster
When enabled, hb_cairo_render_glyph() rasterizes via hb_raster and
blits the A8 coverage image using cairo_mask_surface(), leaving the
existing Cairo source undisturbed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 153c42522e hb-raster: add test/raster/hb-raster-all perf tool
Renders every glyph in a font through the rasterizer.
Args: font-file [font-size-px] [output-dir]
font-size defaults to upem; output-dir writes per-glyph PGMs when given.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 08b76f5db1 hb-raster: add util/hb-raster tool
Renders shaped text into a single auto-sized A8 PGM raster image,
written to stdout or --output-file.  Mirrors util/hb-draw in structure.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandKhaled Hosny 99a84d7f02 Update src/hb-raster.h
Co-authored-by: Khaled Hosny <khaled@aliftype.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandKhaled Hosny 100bdbd244 Update src/hb-raster.h
Co-authored-by: Khaled Hosny <khaled@aliftype.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 35ba082aa2 hb-raster: add test_glyph to test-raster
Renders glyph IDs 2-5 from fonts/adwaita.ttf at upem scale, writes
PGMs to /tmp/ for visual inspection, and asserts non-zero coverage.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 c9d459b8e9 hb-raster: fix tile bounds clipping; move test to test/api/
Fix out-of-bounds pixel write in rasterize_tile: border tiles that
extend past the image edge were writing px-x_origin beyond the row
stride, wrapping into the start of the next row and overwriting
correctly-rasterized pixels with zero.  Pass px1/py1 (exclusive
bounds) to rasterize_tile and clip the pixel loops accordingly.

Also: drop unused ex0 in pass 2 of tile assignment; move test-raster
from src/ to test/api/ and convert it to the GLib g_assert/hb_test_add
framework.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 f622f99ee6 [hb-raster] Add harfbuzz-raster CPU A8 rasterizer library
New library harfbuzz-raster provides a fast CPU coverage rasterizer for
glyph outlines.  Three public types: hb_raster_image_t (ref-counted pixel
buffer), hb_raster_draw_t (outline rasterizer consuming hb_draw_funcs_t
callbacks), and a stub for hb_raster_paint_t (v2).

Key design points:
- Nonzero winding rule
- 26.6 fixed-point edge representation
- 8-sample supersampling per pixel (HB_RASTER_SAMPLES constant)
- 16×16 tile culling with CSR edge lists
- SIMD inner loops: NEON (aarch64) and SSE4.1 (x86); scalar fallback
- Bézier flattening (quadratic + cubic) via de Casteljau, max depth 16
- Auto-size from edge bounding box or caller-supplied extents

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:29:05 -03:00
Behdad EsfahbodandClaude Opus 4.6 a1d9c296d8 hb_vector_t::extend: add exact parameter for tight allocation
Add bool exact=false parameter to all extend() overloads.  When true,
the allocation is exact (no growth headroom), useful for copy
constructors.  Pass exact=true from the Iterable copy constructor.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:26:46 -03:00
Behdad EsfahbodandClaude Opus 4.6 65270b19e8 Fix hb_vector_t::extend() buffer overflow for non-empty vectors
extend(Iterable) called alloc(hb_len(iter)) which only allocated
space for the new elements, ignoring existing length.  When the
vector already had content, push_has_room would write past the
allocated buffer.

Fix: alloc(length + hb_len(iter)).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-21 17:26:32 -03:00
Behdad EsfahbodandClaude Sonnet 4.6 63fb2eec90 Monotone characters fixups (#5752)
* [buffer] Fix a typo and doc string

* [buffer] Add merge_grapheme_clusters / merge_out_grapheme_clusters

Cluster merges serve two distinct purposes that require different level
gates:

- Grapheme grouping: combining marks merged into their base cluster.
  Correct gate: IS_GRAPHEMES (levels 0 and 3).
  New functions: merge_grapheme_clusters / merge_out_grapheme_clusters.

- Monotonicity maintenance: clusters merged to prevent non-monotone
  output after ligature formation, reordering, etc.
  Correct gate: IS_MONOTONE (levels 0 and 1).
  Existing functions: merge_clusters / merge_out_clusters.

Previously both paths shared merge_clusters_impl's single IS_MONOTONE
gate, so GRAPHEMES (level 3) was incorrectly blocked from merging
grapheme clusters.  This also caused a regression in the Thai and
Hangul shapers for MONOTONE_CHARACTERS (level 1): when their explicit
== MONOTONE_GRAPHEMES call-site guards were removed in #5069, the
impls' IS_MONOTONE gate no longer protected them.

Gates are now in the inline wrappers in hb-buffer.hh; the _impl
functions are gate-free.  hb_form_clusters, CoreText grapheme
detection, and the Thai/Hangul shapers are updated to call the
grapheme-semantic variants.  Fixes #5750.

Fixes https://github.com/harfbuzz/harfbuzz/issues/5751
Fixes https://github.com/harfbuzz/harfbuzz/issues/5750

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* [thai] Adjust grapheme merging logic

* [hangul] Tweak grapheme merging

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-20 11:25:24 -07:00
Behdad Esfahbod 821b375331 Fix build without help2man 2026-02-19 12:14:33 -03:00