From 421cc8f5a726d60925b91c992a82885064332ec4 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Wed, 8 Jul 2026 12:20:57 +0300 Subject: [PATCH] [paint] Add hb_paint_fill_glyph() Instead of painting COLRv0 and outline fallback layers with push_clip_glyph + color + pop_clip, add a dedicated callback for this common paint operation. The default (*_nil) implementation uses the decomposed sequence, so existing clients without a fill_glyph implementation continue to work as before. --- docs/harfbuzz-sections.txt | 3 +++ src/OT/Color/COLR/COLR.hh | 4 +--- src/hb-font.cc | 4 +--- src/hb-paint.cc | 38 ++++++++++++++++++++++++++++++ src/hb-paint.h | 47 ++++++++++++++++++++++++++++++++++++++ src/hb-paint.hh | 10 ++++++++ 6 files changed, 100 insertions(+), 6 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 95c179c2f..955e33b81 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -266,6 +266,8 @@ hb_paint_pop_transform_func_t hb_paint_funcs_set_pop_transform_func hb_paint_color_glyph_func_t hb_paint_funcs_set_color_glyph_func +hb_paint_fill_glyph_func_t +hb_paint_funcs_set_fill_glyph_func hb_paint_push_clip_glyph_func_t hb_paint_funcs_set_push_clip_glyph_func hb_paint_push_clip_rectangle_func_t @@ -311,6 +313,7 @@ hb_paint_push_font_transform hb_paint_push_inverse_font_transform hb_paint_pop_transform hb_paint_color_glyph +hb_paint_fill_glyph hb_paint_push_clip_glyph hb_paint_push_clip_rectangle hb_paint_push_clip_path_start diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 39a8231ba..7db97299b 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -2761,9 +2761,7 @@ struct COLR { hb_bool_t is_foreground; hb_color_t color = c.get_color (r.colorIdx, 1., &is_foreground); - c.funcs->push_clip_glyph (c.data, r.glyphId, c.font); - c.funcs->color (c.data, is_foreground, color); - c.funcs->pop_clip (c.data); + c.funcs->fill_glyph (c.data, r.glyphId, c.font, is_foreground, color); } return true; diff --git a/src/hb-font.cc b/src/hb-font.cc index 96fcff501..4b7bce99f 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1670,9 +1670,7 @@ hb_font_t::paint_glyph (hb_codepoint_t glyph, return; /* Fallback for outline glyph. */ - paint_funcs->push_clip_glyph (paint_data, glyph, this); - paint_funcs->color (paint_data, true, foreground); - paint_funcs->pop_clip (paint_data); + paint_funcs->fill_glyph (paint_data, glyph, this, true, foreground); } diff --git a/src/hb-paint.cc b/src/hb-paint.cc index e272ec39b..9e84fbbb9 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -60,6 +60,20 @@ hb_paint_color_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data, hb_font_t *font, void *user_data) { return false; } +static void +hb_paint_fill_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + hb_bool_t is_foreground, + hb_color_t color, + void *user_data) +{ + /* Decompose into the equivalent general sequence. */ + funcs->push_clip_glyph (paint_data, glyph, font); + funcs->color (paint_data, is_foreground, color); + funcs->pop_clip (paint_data); +} + static void hb_paint_push_clip_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data, hb_codepoint_t glyph, @@ -551,6 +565,30 @@ hb_paint_color_glyph (hb_paint_funcs_t *funcs, void *paint_data, return funcs->color_glyph (paint_data, glyph, font); } +/** + * hb_paint_fill_glyph: + * @funcs: paint functions + * @paint_data: associated data passed by the caller + * @glyph: the glyph ID + * @font: the font + * @is_foreground: whether the color is the foreground + * @color: The color to use + * + * Perform a "fill-glyph" paint operation: fill the glyph's shape + * with a solid color. + * + * XSince: REPLACEME + */ +void +hb_paint_fill_glyph (hb_paint_funcs_t *funcs, void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + hb_bool_t is_foreground, + hb_color_t color) +{ + funcs->fill_glyph (paint_data, glyph, font, is_foreground, color); +} + /** * hb_paint_push_clip_glyph: * @funcs: paint functions diff --git a/src/hb-paint.h b/src/hb-paint.h index 9057e3b72..46e83b817 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -157,6 +157,29 @@ typedef hb_bool_t (*hb_paint_color_glyph_func_t) (hb_paint_funcs_t *funcs, hb_font_t *font, void *user_data); +/** + * hb_paint_fill_glyph_func_t: + * @funcs: paint functions object + * @paint_data: The data accompanying the paint functions in hb_font_paint_glyph() + * @glyph: the glyph ID + * @font: the font + * @is_foreground: whether the color is the foreground + * @color: The color to use, unpremultiplied + * @user_data: User data pointer passed to hb_paint_funcs_set_fill_glyph_func() + * + * A virtual method for the #hb_paint_funcs_t to fill a glyph's + * shape with a solid color. + * + * XSince: REPLACEME + */ +typedef void (*hb_paint_fill_glyph_func_t) (hb_paint_funcs_t *funcs, + void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + hb_bool_t is_foreground, + hb_color_t color, + void *user_data); + /** * hb_paint_push_clip_glyph_func_t: * @funcs: paint functions object @@ -854,6 +877,23 @@ hb_paint_funcs_set_color_glyph_func (hb_paint_funcs_t *funcs, void *user_data, hb_destroy_func_t destroy); +/** + * hb_paint_funcs_set_fill_glyph_func: + * @funcs: A paint functions struct + * @func: (closure user_data) (destroy destroy) (scope notified): The fill-glyph callback + * @user_data: Data to pass to @func + * @destroy: (nullable): Function to call when @user_data is no longer needed + * + * Sets the fill-glyph callback on the paint functions struct. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_paint_funcs_set_fill_glyph_func (hb_paint_funcs_t *funcs, + hb_paint_fill_glyph_func_t func, + void *user_data, + hb_destroy_func_t destroy); + /** * hb_paint_funcs_set_push_clip_glyph_func: * @funcs: A paint functions struct @@ -1117,6 +1157,13 @@ hb_paint_color_glyph (hb_paint_funcs_t *funcs, void *paint_data, hb_codepoint_t glyph, hb_font_t *font); +HB_EXTERN void +hb_paint_fill_glyph (hb_paint_funcs_t *funcs, void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + hb_bool_t is_foreground, + hb_color_t color); + HB_EXTERN void hb_paint_push_clip_glyph (hb_paint_funcs_t *funcs, void *paint_data, hb_codepoint_t glyph, diff --git a/src/hb-paint.hh b/src/hb-paint.hh index 148bfc974..f06bce45d 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -48,6 +48,7 @@ HB_PAINT_FUNC_IMPLEMENT (push_group_for) \ HB_PAINT_FUNC_IMPLEMENT (pop_group) \ HB_PAINT_FUNC_IMPLEMENT (custom_palette_color) \ + HB_PAINT_FUNC_IMPLEMENT (fill_glyph) \ /* ^--- Add new callbacks here */ struct hb_paint_funcs_t @@ -121,6 +122,15 @@ struct hb_paint_funcs_t { func.color (this, paint_data, is_foreground, color, !user_data ? nullptr : user_data->color); } + void fill_glyph (void *paint_data, + hb_codepoint_t glyph, + hb_font_t *font, + hb_bool_t is_foreground, + hb_color_t color) + { func.fill_glyph (this, paint_data, + glyph, font, + is_foreground, color, + !user_data ? nullptr : user_data->fill_glyph); } bool image (void *paint_data, hb_blob_t *image, unsigned width, unsigned height,