diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 5076e54cb..681229ca5 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -577,7 +577,11 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, hb_font_t *font = (hb_font_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_font_user_data_key); - if (!font) + if (font) + /* Shared font owned by the cairo font face (the font-based + * constructor). Take our own reference to hand to the scaled font. */ + hb_font_reference (font); + else { hb_face_t *face = (hb_face_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_face_user_data_key); @@ -622,16 +626,28 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, { void *user_data = cairo_font_face_get_user_data (font_face, &hb_cairo_font_init_user_data_user_data_key); - font = init_func (font, scaled_font, user_data); + hb_font_t *new_font = init_func (font, scaled_font, user_data); + /* Preserve the historical ownership behavior for a callback that + * returns a different font: reference it, rather than assuming it + * transfers ownership. The font we created is the callback's to + * release, as before. */ + if (new_font != font) + hb_font_reference (new_font); + font = new_font; } hb_font_make_immutable (font); } - cairo_scaled_font_set_user_data (scaled_font, - &hb_cairo_font_user_data_key, - (void *) hb_font_reference (font), - hb_cairo_font_destroy); + /* The scaled font takes over our one reference to font. */ + if (unlikely (CAIRO_STATUS_SUCCESS != cairo_scaled_font_set_user_data (scaled_font, + &hb_cairo_font_user_data_key, + (void *) font, + hb_cairo_font_destroy))) + { + hb_font_destroy (font); + return CAIRO_STATUS_NO_MEMORY; + } hb_cairo_budget_t *budget = (hb_cairo_budget_t *) hb_malloc (sizeof (hb_cairo_budget_t)); if (unlikely (!budget)) diff --git a/test/api/test-cairo.c b/test/api/test-cairo.c index b4dd079cb..8074531d2 100644 --- a/test/api/test-cairo.c +++ b/test/api/test-cairo.c @@ -108,12 +108,56 @@ test_hb_cairo_color_glyph_cycle (void) hb_face_destroy (face); } +static void +face_destroyed (void *user_data) +{ + *(hb_bool_t *) user_data = 1; +} + +static void +test_hb_cairo_face_lifecycle (void) +{ +#ifdef HB_TEST_CAIRO_TSAN + g_test_skip ("Cairo user-font rendering has a pre-existing lock-order inversion under TSan"); + return; +#endif + + static hb_user_data_key_t key; + hb_bool_t destroyed = 0; + + hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf"); + hb_face_set_user_data (face, &key, &destroyed, face_destroyed, 1); + cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_face (face); + hb_face_destroy (face); + + 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); + + cairo_glyph_t glyph = {1, 0, 0}; + cairo_show_glyphs (cr, &glyph, 1); + + g_assert_cmpint (cairo_status (cr), ==, CAIRO_STATUS_SUCCESS); + + cairo_destroy (cr); + cairo_surface_destroy (surface); + cairo_font_face_destroy (cairo_face); + + /* Cairo keeps unreferenced scaled fonts cached; flush them so the + * hb_font_t created for the scaled font, and with it the face, go away. */ + cairo_debug_reset_static_data (); + g_assert_true (destroyed); +} + 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); + hb_test_add (test_hb_cairo_face_lifecycle); return hb_test_run (); }