From 37f3f0fcc25ed5cc0ea822b1a780705ab842d7bd Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 17 Dec 2022 11:49:18 -0500 Subject: [PATCH] [paint] Change the image callback Instead of passing the glyph ID, give it the image blob, a mimetype, and glyph extents (if available). Update all callers. --- src/hb-ot-color-cbdt-table.hh | 6 +++++- src/hb-ot-color-sbix-table.hh | 6 +++++- src/hb-ot-color-svg-table.hh | 2 +- src/hb-paint.cc | 10 +++++++--- src/hb-paint.h | 18 ++++++++++++++---- src/hb-paint.hh | 6 ++++-- util/hb-test.c | 16 ++++++++++------ 7 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/hb-ot-color-cbdt-table.hh b/src/hb-ot-color-cbdt-table.hh index c5946934b..66c083460 100644 --- a/src/hb-ot-color-cbdt-table.hh +++ b/src/hb-ot-color-cbdt-table.hh @@ -939,11 +939,15 @@ struct CBDT bool paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data) const { + hb_glyph_extents_t extents; hb_blob_t *blob = reference_png (font, glyph); if (unlikely (blob == hb_blob_get_empty ())) return false; - funcs->image (data, glyph); + if (unlikely (!get_extents (font, glyph, &extents))) + return false; + + funcs->image (data, blob, "image/png", &extents); hb_blob_destroy (blob); return true; diff --git a/src/hb-ot-color-sbix-table.hh b/src/hb-ot-color-sbix-table.hh index d874e790b..ef96b226c 100644 --- a/src/hb-ot-color-sbix-table.hh +++ b/src/hb-ot-color-sbix-table.hh @@ -240,11 +240,15 @@ struct sbix int x_offset = 0, y_offset = 0; unsigned int strike_ppem = 0; hb_blob_t *blob = reference_png (font, glyph, &x_offset, &y_offset, &strike_ppem); + hb_glyph_extents_t extents; if (blob == hb_blob_get_empty ()) return false; - funcs->image (data, glyph); + if (!get_extents (font, glyph, &extents)) + return false; + + funcs->image (data, blob, "image/png", &extents); hb_blob_destroy (blob); return true; diff --git a/src/hb-ot-color-svg-table.hh b/src/hb-ot-color-svg-table.hh index 427af9945..b13b485c3 100644 --- a/src/hb-ot-color-svg-table.hh +++ b/src/hb-ot-color-svg-table.hh @@ -103,7 +103,7 @@ struct SVG if (blob == hb_blob_get_empty ()) return false; - funcs->image (data, glyph); + funcs->image (data, blob, "image/svg+xml", nullptr); hb_blob_destroy (blob); return true; diff --git a/src/hb-paint.cc b/src/hb-paint.cc index 0da27af3d..7ef805376 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -70,7 +70,9 @@ hb_paint_color_nil (hb_paint_funcs_t *funcs, void *paint_data, static void hb_paint_image_nil (hb_paint_funcs_t *funcs, void *paint_data, - hb_codepoint_t glyph, + hb_blob_t *image, + const char *mimetype, + hb_glyph_extents_t *extents, void *user_data) {} static void @@ -344,9 +346,11 @@ hb_paint_color (hb_paint_funcs_t *funcs, void *paint_data, void hb_paint_image (hb_paint_funcs_t *funcs, void *paint_data, - hb_codepoint_t glyph) + hb_blob_t *image, + const char *mimetype, + hb_glyph_extents_t *extents) { - funcs->image (paint_data, glyph); + funcs->image (paint_data, image, mimetype, extents); } void diff --git a/src/hb-paint.h b/src/hb-paint.h index e20ca737d..ac990a11e 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -30,6 +30,7 @@ #define HB_PAINT_H #include "hb.h" +#include "hb-font.h" HB_BEGIN_DECLS @@ -225,20 +226,27 @@ typedef void (*hb_paint_color_func_t) (hb_paint_funcs_t *funcs, * hb_paint_image_func_t: * @funcs: paint functions object * @paint_data: The data accompanying the paint functions - * @glyph: the glyph ID + * @image: the image data + * @mimetype: the mime type for the image data + * @extents: (nullable): glyph extents * @user_data: user data passed to the hb_font_paint_glyph() call * * A virtual method for the #hb_paint_funcs_t to paint the * glyph image. * * This method is intended for glyphs with image blobs in the CBDT, - * sbix or SVG tables. + * sbix or SVG tables. The @mimetype identifies the kind of data + * that is contained in @image. Possible values include "image/png" + * and "image/svg+xml". The glyph extents are provided if available, + * and should be used to position the image. * * Since: REPLACEME */ typedef void (*hb_paint_image_func_t) (hb_paint_funcs_t *funcs, void *paint_data, - hb_codepoint_t glyph, + hb_blob_t *image, + const char *mimetype, + hb_glyph_extents_t *extents, void *user_data); /** @@ -684,7 +692,9 @@ hb_paint_color (hb_paint_funcs_t *funcs, void *paint_data, HB_EXTERN void hb_paint_image (hb_paint_funcs_t *funcs, void *paint_data, - hb_codepoint_t glyph); + hb_blob_t *image, + const char *mimetype, + hb_glyph_extents_t *extents); HB_EXTERN void hb_paint_linear_gradient (hb_paint_funcs_t *funcs, void *paint_data, diff --git a/src/hb-paint.hh b/src/hb-paint.hh index adf4c8696..7eaf8592e 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -94,9 +94,11 @@ struct hb_paint_funcs_t color_index, alpha, !user_data ? nullptr : user_data->color); } void image (void *paint_data, - hb_codepoint_t glyph) + hb_blob_t *image, + const char *mimetype, + hb_glyph_extents_t *extents) { func.image (this, paint_data, - glyph, + image, mimetype, extents, !user_data ? nullptr : user_data->image); } void linear_gradient (void *paint_data, hb_color_line_t *color_line, diff --git a/util/hb-test.c b/util/hb-test.c index 666a350dc..6708f87ba 100644 --- a/util/hb-test.c +++ b/util/hb-test.c @@ -324,7 +324,9 @@ read_blob (void *closure, static void paint_image (hb_paint_funcs_t *funcs, void *paint_data, - hb_codepoint_t glyph, + hb_blob_t *blob, + const char *mimetype, + hb_glyph_extents_t *extents, void *user_data) { paint_data_t *data = user_data; @@ -332,17 +334,19 @@ paint_image (hb_paint_funcs_t *funcs, cairo_surface_t *surface; cairo_pattern_t *pattern; cairo_matrix_t m; - hb_glyph_extents_t extents; - hb_font_get_glyph_extents (data->font, glyph, &extents); - r.blob = hb_ot_color_glyph_reference_png (data->font, glyph); + if (strcmp (mimetype, "image/png") != 0) + return; + + r.blob = blob; r.offset = 0; surface = cairo_image_surface_create_from_png_stream (read_blob, &r); - hb_blob_destroy (r.blob); pattern = cairo_pattern_create_for_surface (surface); cairo_matrix_init_scale (&m, 1, -1); - cairo_matrix_translate (&m, extents.x_bearing, - extents.y_bearing); + cairo_matrix_translate (&m, + extents ? extents->x_bearing : 0, + extents ? - extents->y_bearing : 0); cairo_pattern_set_matrix (pattern, &m); cairo_set_source (data->cr, pattern);