From 56ed6e519cd244fa112d4d0ead359860816e6e4a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 13 Apr 2026 01:32:55 -0600 Subject: [PATCH] [paint] Share sweep-gradient tile walker across cairo/vector backends (#5913) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move hb_color_lerp and the sweep-gradient tiling template (previously hb_vector_sweep_gradient_tiles in hb-vector-svg-utils.hh) into hb-paint.hh, renamed to hb_sweep_gradient_tiles. They operate on paint types (hb_color_stop_t, hb_paint_extend_t, hb_color_t), so hb-paint.hh is the natural home; hb-vector-svg-utils.hh is vector-specific but the algorithm isn't. Convert the cairo backend's _hb_cairo_add_sweep_gradient_patches — which re-implemented the same pad/repeat/reflect sector walk in ~200 lines — to call hb_sweep_gradient_tiles with a lambda that converts hb_color_t -> hb_cairo_color_t and emits a cairo patch. The cairo-specific part shrinks to ~30 lines. Net: -210 lines across the tree (-200 from hb-cairo-utils.cc, -200 from hb-vector-svg-utils.hh, +200 added to hb-paint.hh). All 240 tests pass. Assisted-by: Claude Opus 4.6 (1M context) --- src/hb-cairo-utils.cc | 264 ++++--------------------------------- src/hb-paint.hh | 200 ++++++++++++++++++++++++++++ src/hb-vector-paint-pdf.cc | 3 +- src/hb-vector-paint.cc | 3 +- src/hb-vector-svg-utils.hh | 196 --------------------------- 5 files changed, 228 insertions(+), 438 deletions(-) diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index 850739604..0d558df1b 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -30,6 +30,7 @@ #ifdef HAVE_CAIRO #include "hb-cairo-utils.hh" +#include "hb-paint.hh" /* Some routines in this file were ported from BlackRenderer by Black Foundry. * Used by permission to relicense to HarfBuzz license. @@ -567,6 +568,17 @@ _hb_cairo_add_sweep_gradient_patches1 (float cx, float cy, float radius, } } +static inline hb_cairo_color_t +_hb_cairo_color_from_hb_color (hb_color_t c) +{ + return { + hb_color_get_red (c) / 255.f, + hb_color_get_green (c) / 255.f, + hb_color_get_blue (c) / 255.f, + hb_color_get_alpha (c) / 255.f, + }; +} + static void _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, unsigned int n_stops, @@ -577,249 +589,21 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, float end_angle, cairo_pattern_t *pattern) { - float angles_[PREALLOCATED_COLOR_STOPS]; - float *angles = angles_; - hb_cairo_color_t colors_[PREALLOCATED_COLOR_STOPS]; - hb_cairo_color_t *colors = colors_; - hb_cairo_color_t color0, color1; - - if (start_angle == end_angle) - { - if (extend == CAIRO_EXTEND_PAD) - { - hb_cairo_color_t c; - if (start_angle > 0) - { - c.r = hb_color_get_red (stops[0].color) / 255.; - c.g = hb_color_get_green (stops[0].color) / 255.; - c.b = hb_color_get_blue (stops[0].color) / 255.; - c.a = hb_color_get_alpha (stops[0].color) / 255.; - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - 0., &c, - start_angle, &c, - pattern); - } - if (end_angle < HB_2_PI) - { - c.r = hb_color_get_red (stops[n_stops - 1].color) / 255.; - c.g = hb_color_get_green (stops[n_stops - 1].color) / 255.; - c.b = hb_color_get_blue (stops[n_stops - 1].color) / 255.; - c.a = hb_color_get_alpha (stops[n_stops - 1].color) / 255.; - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - end_angle, &c, - HB_2_PI, &c, - pattern); - } - } - return; - } - - assert (start_angle != end_angle); - - /* handle directions */ - if (end_angle < start_angle) - { - hb_swap (start_angle, end_angle); - - for (unsigned i = 0; i < n_stops - 1 - i; i++) - hb_swap (stops[i], stops[n_stops - 1 - i]); - for (unsigned i = 0; i < n_stops; i++) - stops[i].offset = 1 - stops[i].offset; - } - - auto angles_guard = hb_make_scope_guard ([&]() { - if (angles != angles_) hb_free (angles); - }); - auto colors_guard = hb_make_scope_guard ([&]() { - if (colors != colors_) hb_free (colors); - }); - - if (n_stops > PREALLOCATED_COLOR_STOPS) - { - angles = (float *) hb_malloc (sizeof (float) * n_stops); - colors = (hb_cairo_color_t *) hb_malloc (sizeof (hb_cairo_color_t) * n_stops); - if (unlikely (!angles || !colors)) - return; - } - - for (unsigned i = 0; i < n_stops; i++) - { - angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); - colors[i].r = hb_color_get_red (stops[i].color) / 255.; - colors[i].g = hb_color_get_green (stops[i].color) / 255.; - colors[i].b = hb_color_get_blue (stops[i].color) / 255.; - colors[i].a = hb_color_get_alpha (stops[i].color) / 255.; - } - - if (extend == CAIRO_EXTEND_PAD) - { - unsigned pos; - - color0 = colors[0]; - for (pos = 0; pos < n_stops; pos++) - { - if (angles[pos] >= 0) - { - if (pos > 0) - { - float k = (0 - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - _hb_cairo_interpolate_colors (&colors[pos-1], &colors[pos], k, &color0); - } - break; - } - } - if (pos == n_stops) - { - /* everything is below 0 */ - color0 = colors[n_stops-1]; - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - 0., &color0, - HB_2_PI, &color0, - pattern); - return; - } + hb_paint_extend_t hb_extend = + extend == CAIRO_EXTEND_REPEAT ? HB_PAINT_EXTEND_REPEAT : + extend == CAIRO_EXTEND_REFLECT ? HB_PAINT_EXTEND_REFLECT : + HB_PAINT_EXTEND_PAD; + hb_sweep_gradient_tiles (stops, n_stops, hb_extend, + start_angle, end_angle, + [&](float a0, hb_color_t c0, float a1, hb_color_t c1) { + hb_cairo_color_t cc0 = _hb_cairo_color_from_hb_color (c0); + hb_cairo_color_t cc1 = _hb_cairo_color_from_hb_color (c1); _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - 0., &color0, - angles[pos], &colors[pos], + a0, &cc0, + a1, &cc1, pattern); - - for (pos++; pos < n_stops; pos++) - { - if (angles[pos] <= HB_2_PI) - { - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - angles[pos - 1], &colors[pos-1], - angles[pos], &colors[pos], - pattern); - } - else - { - float k = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - _hb_cairo_interpolate_colors (&colors[pos - 1], &colors[pos], k, &color1); - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - angles[pos - 1], &colors[pos - 1], - HB_2_PI, &color1, - pattern); - break; - } - } - - if (pos == n_stops) - { - /* everything is below 2*M_PI */ - color0 = colors[n_stops - 1]; - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - angles[n_stops - 1], &color0, - HB_2_PI, &color0, - pattern); - return; - } - } - else - { - int k; - float span; - - span = angles[n_stops - 1] - angles[0]; - if (!span) - return; - - k = 0; - if (angles[0] >= 0) - { - float ss = angles[0]; - while (ss > 0) - { - if (span > 0) - { - ss -= span; - k--; - } - else - { - ss += span; - k++; - } - } - } - else if (angles[0] < 0) - { - float ee = angles[n_stops - 1]; - while (ee < 0) - { - if (span > 0) - { - ee += span; - k++; - } - else - { - ee -= span; - k--; - } - } - } - - //assert (angles[0] + k * span <= 0 && 0 < angles[n_stops - 1] + k * span); - span = fabsf (span); - - for (signed l = k; l < 1000; l++) - { - for (unsigned i = 1; i < n_stops; i++) - { - float a0, a1; - hb_cairo_color_t *c0, *c1; - - if ((l % 2 != 0) && (extend == CAIRO_EXTEND_REFLECT)) - { - a0 = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - (i-1)] + l * span; - a1 = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; - c0 = &colors[n_stops - 1 - (i - 1)]; - c1 = &colors[n_stops - 1 - i]; - } - else - { - a0 = angles[i-1] + l * span; - a1 = angles[i] + l * span; - c0 = &colors[i-1]; - c1 = &colors[i]; - } - - if (a1 < 0) - continue; - if (a0 < 0) - { - hb_cairo_color_t color; - float f = (0 - a0)/(a1 - a0); - _hb_cairo_interpolate_colors (c0, c1, f, &color); - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - 0, &color, - a1, c1, - pattern); - } - else if (a1 >= HB_2_PI) - { - hb_cairo_color_t color; - float f = (HB_2_PI - a0)/(a1 - a0); - _hb_cairo_interpolate_colors (c0, c1, f, &color); - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - a0, c0, - HB_2_PI, &color, - pattern); - return; - } - else - { - _hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius, - a0, c0, - a1, c1, - pattern); - } - } - } - } - + }); } void diff --git a/src/hb-paint.hh b/src/hb-paint.hh index be4655b7b..f737ab911 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -251,4 +251,204 @@ struct hb_paint_funcs_t DECLARE_NULL_INSTANCE (hb_paint_funcs_t); +/* Linearly interpolate between two hb_color_t values, component-wise, + * in byte-channel space with rounding. */ +static inline hb_color_t +hb_color_lerp (hb_color_t c0, hb_color_t c1, float t) +{ + auto lerp = [&] (unsigned shift) -> unsigned { + unsigned v0 = (c0 >> shift) & 0xFF; + unsigned v1 = (c1 >> shift) & 0xFF; + return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); + }; + return HB_COLOR (lerp (0), lerp (8), lerp (16), lerp (24)); +} + +/* Iterate sweep gradient color stop tiling across the full 0..2π range, + * calling emit_patch (a0, c0, a1, c1) for each sector. + * + * Handles pad, repeat, and reflect extend modes. Stops must be + * pre-sorted by offset. Shared by the vector, raster, GPU and cairo + * sweep-gradient implementations. */ +template +static inline void +hb_sweep_gradient_tiles (hb_color_stop_t *stops, + unsigned n_stops, + hb_paint_extend_t extend, + float start_angle, + float end_angle, + Func emit_patch) +{ + if (!n_stops) return; + + if (start_angle == end_angle) + { + if (extend == HB_PAINT_EXTEND_PAD) + { + if (start_angle > 0.f) + emit_patch (0.f, stops[0].color, start_angle, stops[0].color); + if (end_angle < HB_2_PI) + emit_patch (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); + } + return; + } + + if (end_angle < start_angle) + { + float tmp = start_angle; start_angle = end_angle; end_angle = tmp; + for (unsigned i = 0; i < n_stops - 1 - i; i++) + { + hb_color_stop_t t = stops[i]; + stops[i] = stops[n_stops - 1 - i]; + stops[n_stops - 1 - i] = t; + } + for (unsigned i = 0; i < n_stops; i++) + stops[i].offset = 1.f - stops[i].offset; + } + + /* Map stop offsets to angles. */ + float angles_buf[16]; + hb_color_t colors_buf[16]; + float *angles = angles_buf; + hb_color_t *colors = colors_buf; + bool dynamic = false; + + if (n_stops > 16) + { + angles = (float *) hb_malloc (sizeof (float) * n_stops); + colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops); + if (!angles || !colors) + { + hb_free (angles); + hb_free (colors); + return; + } + dynamic = true; + } + + for (unsigned i = 0; i < n_stops; i++) + { + angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); + colors[i] = stops[i].color; + } + + if (extend == HB_PAINT_EXTEND_PAD) + { + unsigned pos; + hb_color_t color0 = colors[0]; + for (pos = 0; pos < n_stops; pos++) + { + if (angles[pos] >= 0) + { + if (pos > 0) + { + float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + color0 = hb_color_lerp (colors[pos - 1], colors[pos], f); + } + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + emit_patch (0.f, color0, HB_2_PI, color0); + goto done; + } + emit_patch (0.f, color0, angles[pos], colors[pos]); + for (pos++; pos < n_stops; pos++) + { + if (angles[pos] <= HB_2_PI) + emit_patch (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); + else + { + float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); + hb_color_t color1 = hb_color_lerp (colors[pos - 1], colors[pos], f); + emit_patch (angles[pos - 1], colors[pos - 1], HB_2_PI, color1); + break; + } + } + if (pos == n_stops) + { + color0 = colors[n_stops - 1]; + emit_patch (angles[n_stops - 1], color0, HB_2_PI, color0); + goto done; + } + } + else + { + float span = angles[n_stops - 1] - angles[0]; + if (fabsf (span) < 1e-6f) + goto done; + + int k = 0; + if (angles[0] >= 0) + { + float ss = angles[0]; + while (ss > 0) + { + if (span > 0) { ss -= span; k--; } + else { ss += span; k++; } + } + } + else + { + float ee = angles[n_stops - 1]; + while (ee < 0) + { + if (span > 0) { ee += span; k++; } + else { ee -= span; k--; } + } + } + + span = fabsf (span); + for (int l = k; l < 1000; l++) + { + for (unsigned i = 1; i < n_stops; i++) + { + float a0_l, a1_l; + hb_color_t col0, col1; + if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) + { + a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; + a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; + col0 = colors[n_stops - i]; + col1 = colors[n_stops - 1 - i]; + } + else + { + a0_l = angles[i - 1] + l * span; + a1_l = angles[i] + l * span; + col0 = colors[i - 1]; + col1 = colors[i]; + } + + if (a1_l < 0.f) continue; + if (a0_l < 0.f) + { + float f = (0.f - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_color_lerp (col0, col1, f); + emit_patch (0.f, c, a1_l, col1); + } + else if (a1_l >= HB_2_PI) + { + float f = (HB_2_PI - a0_l) / (a1_l - a0_l); + hb_color_t c = hb_color_lerp (col0, col1, f); + emit_patch (a0_l, col0, HB_2_PI, c); + goto done; + } + else + emit_patch (a0_l, col0, a1_l, col1); + } + } + } + +done: + if (dynamic) + { + hb_free (angles); + hb_free (colors); + } +} + + #endif /* HB_PAINT_HH */ diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index b798f1cfe..d956714b3 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -28,6 +28,7 @@ #include "hb-vector-paint.hh" #include "hb-vector-draw.hh" +#include "hb-paint.hh" #include #include @@ -1055,7 +1056,7 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, hb_vector_t mesh; mesh.alloc (256); - hb_vector_sweep_gradient_tiles (stops, n_stops, extend, + hb_sweep_gradient_tiles (stops, n_stops, extend, start_angle, end_angle, [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) { hb_pdf_add_sweep_patch (&mesh, cx, cy, xlo, xhi, ylo, yhi, diff --git a/src/hb-vector-paint.cc b/src/hb-vector-paint.cc index 1e5571abc..20401c144 100644 --- a/src/hb-vector-paint.cc +++ b/src/hb-vector-paint.cc @@ -28,6 +28,7 @@ #include "hb-vector-paint.hh" #include "hb-blob.hh" +#include "hb-paint.hh" #include "hb-vector-svg-path.hh" #include @@ -735,7 +736,7 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, auto *body = &paint->current_body (); unsigned precision = paint->precision; float radius = 32767.f; - hb_vector_sweep_gradient_tiles (stops.arrayZ, stops.length, + hb_sweep_gradient_tiles (stops.arrayZ, stops.length, hb_color_line_get_extend (color_line), start_angle, end_angle, [&] (float a0, hb_color_t c0, float a1, hb_color_t c1) diff --git a/src/hb-vector-svg-utils.hh b/src/hb-vector-svg-utils.hh index 4fe6e0dae..9b46594e2 100644 --- a/src/hb-vector-svg-utils.hh +++ b/src/hb-vector-svg-utils.hh @@ -126,200 +126,4 @@ hb_vector_scale_precision (unsigned precision) return precision < 7 ? 7 : precision; } -static inline hb_color_t -hb_color_lerp (hb_color_t c0, hb_color_t c1, float t) -{ - auto lerp = [&] (unsigned shift) -> unsigned { - unsigned v0 = (c0 >> shift) & 0xFF; - unsigned v1 = (c1 >> shift) & 0xFF; - return (unsigned) (v0 + t * ((float) v1 - (float) v0) + 0.5f); - }; - return HB_COLOR (lerp (0), lerp (8), lerp (16), lerp (24)); -} - -/* Iterate sweep gradient color stop tiling across the full 0..2π range, - * calling emit_patch (a0, c0, a1, c1) for each sector. - * - * Handles pad, repeat, and reflect extend modes. Stops must be - * pre-sorted by offset. */ -template -static inline void -hb_vector_sweep_gradient_tiles (hb_color_stop_t *stops, - unsigned n_stops, - hb_paint_extend_t extend, - float start_angle, - float end_angle, - Func emit_patch) -{ - if (!n_stops) return; - - if (start_angle == end_angle) - { - if (extend == HB_PAINT_EXTEND_PAD) - { - if (start_angle > 0.f) - emit_patch (0.f, stops[0].color, start_angle, stops[0].color); - if (end_angle < HB_2_PI) - emit_patch (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color); - } - return; - } - - if (end_angle < start_angle) - { - float tmp = start_angle; start_angle = end_angle; end_angle = tmp; - for (unsigned i = 0; i < n_stops - 1 - i; i++) - { - hb_color_stop_t t = stops[i]; - stops[i] = stops[n_stops - 1 - i]; - stops[n_stops - 1 - i] = t; - } - for (unsigned i = 0; i < n_stops; i++) - stops[i].offset = 1.f - stops[i].offset; - } - - /* Map stop offsets to angles. */ - float angles_buf[16]; - hb_color_t colors_buf[16]; - float *angles = angles_buf; - hb_color_t *colors = colors_buf; - bool dynamic = false; - - if (n_stops > 16) - { - angles = (float *) hb_malloc (sizeof (float) * n_stops); - colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops); - if (!angles || !colors) - { - hb_free (angles); - hb_free (colors); - return; - } - dynamic = true; - } - - for (unsigned i = 0; i < n_stops; i++) - { - angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); - colors[i] = stops[i].color; - } - - if (extend == HB_PAINT_EXTEND_PAD) - { - unsigned pos; - hb_color_t color0 = colors[0]; - for (pos = 0; pos < n_stops; pos++) - { - if (angles[pos] >= 0) - { - if (pos > 0) - { - float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - color0 = hb_color_lerp (colors[pos - 1], colors[pos], f); - } - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - emit_patch (0.f, color0, HB_2_PI, color0); - goto done; - } - emit_patch (0.f, color0, angles[pos], colors[pos]); - for (pos++; pos < n_stops; pos++) - { - if (angles[pos] <= HB_2_PI) - emit_patch (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]); - else - { - float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]); - hb_color_t color1 = hb_color_lerp (colors[pos - 1], colors[pos], f); - emit_patch (angles[pos - 1], colors[pos - 1], HB_2_PI, color1); - break; - } - } - if (pos == n_stops) - { - color0 = colors[n_stops - 1]; - emit_patch (angles[n_stops - 1], color0, HB_2_PI, color0); - goto done; - } - } - else - { - float span = angles[n_stops - 1] - angles[0]; - if (fabsf (span) < 1e-6f) - goto done; - - int k = 0; - if (angles[0] >= 0) - { - float ss = angles[0]; - while (ss > 0) - { - if (span > 0) { ss -= span; k--; } - else { ss += span; k++; } - } - } - else - { - float ee = angles[n_stops - 1]; - while (ee < 0) - { - if (span > 0) { ee += span; k++; } - else { ee -= span; k--; } - } - } - - span = fabsf (span); - for (int l = k; l < 1000; l++) - { - for (unsigned i = 1; i < n_stops; i++) - { - float a0_l, a1_l; - hb_color_t col0, col1; - if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) - { - a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span; - a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span; - col0 = colors[n_stops - i]; - col1 = colors[n_stops - 1 - i]; - } - else - { - a0_l = angles[i - 1] + l * span; - a1_l = angles[i] + l * span; - col0 = colors[i - 1]; - col1 = colors[i]; - } - - if (a1_l < 0.f) continue; - if (a0_l < 0.f) - { - float f = (0.f - a0_l) / (a1_l - a0_l); - hb_color_t c = hb_color_lerp (col0, col1, f); - emit_patch (0.f, c, a1_l, col1); - } - else if (a1_l >= HB_2_PI) - { - float f = (HB_2_PI - a0_l) / (a1_l - a0_l); - hb_color_t c = hb_color_lerp (col0, col1, f); - emit_patch (a0_l, col0, HB_2_PI, c); - goto done; - } - else - emit_patch (a0_l, col0, a1_l, col1); - } - } - } - -done: - if (dynamic) - { - hb_free (angles); - hb_free (colors); - } -} - #endif /* HB_VECTOR_SVG_UTILS_HH */