diff --git a/src/hb-cairo-utils.hh b/src/hb-cairo-utils.hh index 2db172047..cad61ca3d 100644 --- a/src/hb-cairo-utils.hh +++ b/src/hb-cairo-utils.hh @@ -34,7 +34,7 @@ struct hb_cairo_budget_t { /* Cairo serializes glyph-cache population for a scaled font. Keeping the - * owner there lets recursive color-glyph callbacks share one session. */ + * owner there lets the draw and paint callbacks share one session. */ int64_t budget = HB_BUDGET_DEFAULT; int64_t budget_remaining = HB_BUDGET_GLYPH; unsigned int active = 0; diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index e1579fdf4..5076e54cb 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -227,31 +227,18 @@ hb_cairo_fill_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, static hb_bool_t hb_cairo_paint_color_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, - void *paint_data, - hb_codepoint_t glyph, - hb_font_t *font, + void *paint_data HB_UNUSED, + hb_codepoint_t glyph HB_UNUSED, + hb_font_t *font HB_UNUSED, void *user_data HB_UNUSED) { - hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data; - cairo_t *cr = c->cr; - - if (unlikely (!c->spend (HB_BUDGET_1))) - return true; - - cairo_save (cr); - - hb_position_t x_scale, y_scale; - hb_font_get_scale (font, &x_scale, &y_scale); - cairo_scale (cr, x_scale, -y_scale); - - cairo_glyph_t cairo_glyph = { glyph, 0, 0 }; - cairo_set_scaled_font (cr, c->scaled_font); - cairo_set_font_size (cr, 1); - cairo_show_glyphs (cr, &cairo_glyph, 1); - - cairo_restore (cr); - - return true; + /* Showing the nested glyph through cairo_show_glyphs() would re-enter + * hb_font_paint_glyph() on a scaled font of its own, with a fresh paint + * context: the glyph decycler, nesting depth and work limits of the + * walk in progress do not carry over, so two glyphs painting each other + * recurse until the stack is exhausted. Decline, and let the caller + * paint the nested glyph inline under the limits of the current walk. */ + return false; } static void diff --git a/test/api/test-cairo.c b/test/api/test-cairo.c index c4a4e289d..b4dd079cb 100644 --- a/test/api/test-cairo.c +++ b/test/api/test-cairo.c @@ -67,11 +67,53 @@ test_hb_cairo_budget (void) hb_face_destroy (face); } +static void +test_hb_cairo_color_glyph_cycle (void) +{ +#ifdef HB_TEST_CAIRO_TSAN + g_test_skip ("Cairo user-font rendering has a pre-existing lock-order inversion under TSan"); + return; +#endif + + /* Two COLRv1 glyphs that paint each other with PaintColrGlyph. The + * color_glyph callback used to show the nested glyph through cairo, + * re-entering hb_font_paint_glyph() without the cycle and depth + * guards of the paint walk in progress. */ + hb_face_t *face = hb_test_open_font_file ("fonts/test_glyphs-glyf_colr_1.ttf"); + hb_font_t *font = hb_font_create (face); + cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_font (font); + cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 64, 64); + cairo_t *cr = cairo_create (surface); + + cairo_set_font_face (cr, cairo_face); + cairo_set_font_size (cr, 30); + + /* paintcolrglyph_cycle_first, paintcolrglyph_cycle_second */ + for (hb_codepoint_t u = 0xF1100; u <= 0xF1101; u++) + { + hb_codepoint_t gid; + g_assert_true (hb_font_get_nominal_glyph (font, u, &gid)); + + cairo_glyph_t glyph = {gid, 0, 30}; + cairo_show_glyphs (cr, &glyph, 1); + } + + g_assert_cmpint (cairo_status (cr), ==, CAIRO_STATUS_SUCCESS); + g_assert_cmpint (cairo_surface_status (surface), ==, CAIRO_STATUS_SUCCESS); + + cairo_destroy (cr); + cairo_surface_destroy (surface); + cairo_font_face_destroy (cairo_face); + hb_font_destroy (font); + hb_face_destroy (face); +} + int main (int argc, char **argv) { hb_test_init (&argc, &argv); hb_test_add (test_hb_cairo_budget); + hb_test_add (test_hb_cairo_color_glyph_cycle); return hb_test_run (); }