From 344c12ca15355a9d760b43525dd106c07d30bed8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 24 Feb 2026 09:19:04 -0300 Subject: [PATCH] Add vector paint glyph convenience API and auto-extents --- src/hb-raster-paint.cc | 18 ++++++++++++++---- src/hb-raster.cc | 8 ++++---- src/hb-vector-svg.cc | 34 ++++++++++++++++++++++++++++++++++ src/hb-vector.h | 9 +++++++++ util/vector-output.hh | 7 +++---- 5 files changed, 64 insertions(+), 12 deletions(-) diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 120800442..a440475db 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -1968,10 +1968,20 @@ hb_raster_paint_glyph (hb_raster_paint_t *paint, float dx = paint->base_transform.x0; float dy = paint->base_transform.y0; - hb_raster_paint_set_transform (paint, - xx, yx, xy, yy, - dx + xx * pen_x + xy * pen_y, - dy + yx * pen_x + yy * pen_y); + float tx = dx + xx * pen_x + xy * pen_y; + float ty = dy + yx * pen_x + yy * pen_y; + + if (!paint->has_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (font, glyph, &ge)) + { + hb_raster_paint_set_transform (paint, xx, yx, xy, yy, tx, ty); + hb_raster_paint_set_glyph_extents (paint, &ge); + } + } + + hb_raster_paint_set_transform (paint, xx, yx, xy, yy, tx, ty); hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, hb_raster_paint_get_funcs (), paint, palette, foreground); diff --git a/src/hb-raster.cc b/src/hb-raster.cc index 889e43325..7c56983b4 100644 --- a/src/hb-raster.cc +++ b/src/hb-raster.cc @@ -41,9 +41,9 @@ * ``` * hb_raster_draw_t *draw = hb_raster_draw_create_or_fail (); * hb_raster_draw_set_scale_factor (draw, 64.f, 64.f); - * hb_raster_draw_set_transform (draw, 1.f, 0.f, 0.f, 1.f, pen_x, pen_y); + * hb_raster_draw_set_transform (draw, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); * hb_raster_draw_set_glyph_extents (draw, &glyph_extents); - * hb_font_draw_glyph (font, gid, hb_raster_draw_get_funcs (), draw); + * hb_raster_draw_glyph (draw, font, gid, pen_x, pen_y); * hb_raster_image_t *mask = hb_raster_draw_render (draw); * ``` * @@ -52,12 +52,12 @@ * ``` * hb_raster_paint_t *paint = hb_raster_paint_create_or_fail (); * hb_raster_paint_set_scale_factor (paint, 64.f, 64.f); - * hb_raster_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, pen_x, pen_y); + * hb_raster_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); * hb_raster_paint_set_foreground (paint, foreground); * hb_glyph_extents_t glyph_extents; * hb_font_get_glyph_extents (font, gid, &glyph_extents); * hb_raster_paint_set_glyph_extents (paint, &glyph_extents); - * hb_font_paint_glyph (font, gid, hb_raster_paint_get_funcs (), paint, 0, foreground); + * hb_raster_paint_glyph (paint, font, gid, pen_x, pen_y, 0, foreground); * hb_raster_image_t *img = hb_raster_paint_render (paint); * ``` * diff --git a/src/hb-vector-svg.cc b/src/hb-vector-svg.cc index 81253def1..586dbf279 100644 --- a/src/hb-vector-svg.cc +++ b/src/hb-vector-svg.cc @@ -1503,6 +1503,40 @@ hb_vector_paint_get_funcs (void) return hb_vector_paint_funcs_singleton (); } +hb_bool_t +hb_vector_paint_glyph (hb_vector_paint_t *paint, + hb_font_t *font, + hb_codepoint_t glyph, + float pen_x, + float pen_y, + unsigned palette, + hb_color_t foreground) +{ + float xx = paint->transform.xx; + float yx = paint->transform.yx; + float xy = paint->transform.xy; + float yy = paint->transform.yy; + float dx = paint->transform.x0; + float dy = paint->transform.y0; + + float tx = dx + xx * pen_x + xy * pen_y; + float ty = dy + yx * pen_x + yy * pen_y; + + hb_vector_paint_set_transform (paint, xx, yx, xy, yy, tx, ty); + if (!paint->has_extents) + { + hb_glyph_extents_t ge; + if (hb_font_get_glyph_extents (font, glyph, &ge)) + hb_vector_paint_set_glyph_extents (paint, &ge); + } + + hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph, + hb_vector_paint_get_funcs (), paint, + palette, foreground); + hb_vector_paint_set_transform (paint, xx, yx, xy, yy, dx, dy); + return ret; +} + void hb_vector_svg_paint_set_flat (hb_vector_paint_t *paint, hb_bool_t flat) diff --git a/src/hb-vector.h b/src/hb-vector.h index c637ab3d5..a87f6021d 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -193,6 +193,15 @@ hb_vector_paint_set_palette (hb_vector_paint_t *paint, HB_EXTERN hb_paint_funcs_t * hb_vector_paint_get_funcs (void); +HB_EXTERN hb_bool_t +hb_vector_paint_glyph (hb_vector_paint_t *paint, + hb_font_t *font, + hb_codepoint_t glyph, + float pen_x, + float pen_y, + unsigned palette, + hb_color_t foreground); + HB_EXTERN void hb_vector_svg_paint_set_flat (hb_vector_paint_t *paint, hb_bool_t flat); diff --git a/util/vector-output.hh b/util/vector-output.hh index 9ce08f758..bb4e43837 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -213,10 +213,9 @@ struct vector_output_t : output_options_t<> float pen_x = g.x + off_x; float pen_y = g.y + off_y; - hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, pen_x, pen_y); - if (hb_font_paint_glyph_or_fail (upem_font, g.gid, - hb_vector_paint_get_funcs (), paint, - palette, foreground)) + hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); + if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, + palette, foreground)) { had_paint = true; continue;