diff --git a/src/hb-cairo-utils.hh b/src/hb-cairo-utils.hh index c9e380120..30e15717e 100644 --- a/src/hb-cairo-utils.hh +++ b/src/hb-cairo-utils.hh @@ -31,12 +31,68 @@ #include "hb-cairo.h" -typedef struct +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. */ + int64_t budget = HB_BUDGET_DEFAULT; + int64_t budget_remaining = HB_BUDGET_CAIRO_PAINT; + unsigned int active = 0; + + void enter () + { + if (!active++) + recharge (); + } + + void leave () + { + assert (active); + active--; + } + + void recharge () + { + budget_remaining = budget == HB_BUDGET_DEFAULT ? + HB_BUDGET_CAIRO_PAINT : budget; + } +}; + +struct hb_cairo_context_t +{ + hb_cairo_context_t (cairo_scaled_font_t *scaled_font_, + cairo_t *cr_, + hb_map_t *color_cache_, + hb_cairo_budget_t *budget_) : + scaled_font (scaled_font_), cr (cr_), color_cache (color_cache_), + budget (budget_) + { + budget->enter (); + } + + ~hb_cairo_context_t () + { + budget->leave (); + } + + bool set_budget (int64_t value) + { + budget->budget = value; + budget->recharge (); + return true; + } + + int64_t get_budget () const { return budget->budget; } + int64_t *get_budget_remaining () { return &budget->budget_remaining; } + + bool spend (unsigned int cost, unsigned int mult = 1) + { return hb_budget_spend (budget->budget_remaining, cost, mult); } + cairo_scaled_font_t *scaled_font; cairo_t *cr; hb_map_t *color_cache; -} hb_cairo_context_t; + hb_cairo_budget_t *budget; +}; static inline cairo_operator_t _hb_paint_composite_mode_to_cairo (hb_paint_composite_mode_t mode) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 04440c19a..e1579fdf4 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -54,9 +54,11 @@ hb_cairo_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, float to_x, float to_y, void *user_data HB_UNUSED) { - cairo_t *cr = (cairo_t *) draw_data; + hb_cairo_context_t *c = (hb_cairo_context_t *) draw_data; + if (unlikely (!c->spend (HB_BUDGET_1))) + return; - cairo_move_to (cr, (double) to_x, (double) to_y); + cairo_move_to (c->cr, (double) to_x, (double) to_y); } static void @@ -66,9 +68,11 @@ hb_cairo_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, float to_x, float to_y, void *user_data HB_UNUSED) { - cairo_t *cr = (cairo_t *) draw_data; + hb_cairo_context_t *c = (hb_cairo_context_t *) draw_data; + if (unlikely (!c->spend (HB_BUDGET_1))) + return; - cairo_line_to (cr, (double) to_x, (double) to_y); + cairo_line_to (c->cr, (double) to_x, (double) to_y); } static void @@ -80,9 +84,11 @@ hb_cairo_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, float to_x, float to_y, void *user_data HB_UNUSED) { - cairo_t *cr = (cairo_t *) draw_data; + hb_cairo_context_t *c = (hb_cairo_context_t *) draw_data; + if (unlikely (!c->spend (HB_BUDGET_1))) + return; - cairo_curve_to (cr, + cairo_curve_to (c->cr, (double) control1_x, (double) control1_y, (double) control2_x, (double) control2_y, (double) to_x, (double) to_y); @@ -94,9 +100,36 @@ hb_cairo_close_path (hb_draw_funcs_t *dfuncs HB_UNUSED, hb_draw_state_t *st HB_UNUSED, void *user_data HB_UNUSED) { - cairo_t *cr = (cairo_t *) draw_data; + hb_cairo_context_t *c = (hb_cairo_context_t *) draw_data; + if (unlikely (!c->spend (HB_BUDGET_1))) + return; - cairo_close_path (cr); + cairo_close_path (c->cr); +} + +static hb_bool_t +hb_cairo_draw_set_budget (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + int64_t budget, + void *user_data HB_UNUSED) +{ + return ((hb_cairo_context_t *) draw_data)->set_budget (budget); +} + +static int64_t +hb_cairo_draw_get_budget (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + void *user_data HB_UNUSED) +{ + return ((hb_cairo_context_t *) draw_data)->get_budget (); +} + +static int64_t * +hb_cairo_draw_get_budget_remaining (hb_draw_funcs_t *dfuncs HB_UNUSED, + void *draw_data, + void *user_data HB_UNUSED) +{ + return ((hb_cairo_context_t *) draw_data)->get_budget_remaining (); } static inline void free_static_cairo_draw_funcs (); @@ -111,6 +144,9 @@ static struct hb_cairo_draw_funcs_lazy_loader_t : hb_draw_funcs_lazy_loader_tcr; + if (unlikely (!c->spend (HB_BUDGET_1))) + return true; + cairo_save (cr); hb_position_t x_scale, y_scale; @@ -228,7 +267,7 @@ hb_cairo_push_clip_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED, cairo_save (cr); cairo_new_path (cr); - hb_font_draw_glyph (font, glyph, hb_cairo_draw_get_funcs (), cr); + hb_font_draw_glyph (font, glyph, hb_cairo_draw_get_funcs (), c); cairo_close_path (cr); cairo_clip (cr); @@ -261,7 +300,7 @@ hb_cairo_push_clip_path_start (hb_paint_funcs_t *pfuncs HB_UNUSED, cairo_save (cr); cairo_new_path (cr); - *draw_data = cr; + *draw_data = c; return hb_cairo_draw_get_funcs (); } @@ -449,6 +488,31 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, return false; } +static hb_bool_t +hb_cairo_paint_set_budget (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + int64_t budget, + void *user_data HB_UNUSED) +{ + return ((hb_cairo_context_t *) paint_data)->set_budget (budget); +} + +static int64_t +hb_cairo_paint_get_budget (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + return ((hb_cairo_context_t *) paint_data)->get_budget (); +} + +static int64_t * +hb_cairo_paint_get_budget_remaining (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + void *user_data HB_UNUSED) +{ + return ((hb_cairo_context_t *) paint_data)->get_budget_remaining (); +} + static inline void free_static_cairo_paint_funcs (); static struct hb_cairo_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t @@ -474,6 +538,9 @@ static struct hb_cairo_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t< hb_paint_funcs_set_radial_gradient_func (funcs, hb_cairo_paint_radial_gradient, nullptr, nullptr); hb_paint_funcs_set_sweep_gradient_func (funcs, hb_cairo_paint_sweep_gradient, nullptr, nullptr); hb_paint_funcs_set_custom_palette_color_func (funcs, hb_cairo_paint_custom_palette_color, nullptr, nullptr); + hb_paint_funcs_set_set_budget_func (funcs, hb_cairo_paint_set_budget, nullptr, nullptr); + hb_paint_funcs_set_get_budget_func (funcs, hb_cairo_paint_get_budget, nullptr, nullptr); + hb_paint_funcs_set_get_budget_remaining_func (funcs, hb_cairo_paint_get_budget_remaining, nullptr, nullptr); hb_paint_funcs_make_immutable (funcs); @@ -501,9 +568,17 @@ static const cairo_user_data_key_t hb_cairo_font_user_data_key = {0}; static const cairo_user_data_key_t hb_cairo_font_init_func_user_data_key = {0}; static const cairo_user_data_key_t hb_cairo_font_init_user_data_user_data_key = {0}; static const cairo_user_data_key_t hb_cairo_scale_factor_user_data_key = {0}; +static const cairo_user_data_key_t hb_cairo_budget_user_data_key = {0}; static void hb_cairo_face_destroy (void *p) { hb_face_destroy ((hb_face_t *) p); } static void hb_cairo_font_destroy (void *p) { hb_font_destroy ((hb_font_t *) p); } +static void +hb_cairo_budget_destroy (void *p) +{ + hb_cairo_budget_t *budget = (hb_cairo_budget_t *) p; + budget->~hb_cairo_budget_t (); + hb_free (budget); +} static cairo_status_t hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, @@ -571,6 +646,20 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, (void *) hb_font_reference (font), hb_cairo_font_destroy); + hb_cairo_budget_t *budget = (hb_cairo_budget_t *) hb_malloc (sizeof (hb_cairo_budget_t)); + if (unlikely (!budget)) + return CAIRO_STATUS_NO_MEMORY; + new (budget) hb_cairo_budget_t (); + cairo_status_t status = cairo_scaled_font_set_user_data (scaled_font, + &hb_cairo_budget_user_data_key, + budget, + hb_cairo_budget_destroy); + if (unlikely (status != CAIRO_STATUS_SUCCESS)) + { + hb_cairo_budget_destroy (budget); + return status; + } + hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale); @@ -636,6 +725,11 @@ hb_cairo_render_glyph (cairo_scaled_font_t *scaled_font, { hb_font_t *font = (hb_font_t *) cairo_scaled_font_get_user_data (scaled_font, &hb_cairo_font_user_data_key); + hb_cairo_budget_t *budget = (hb_cairo_budget_t *) cairo_scaled_font_get_user_data (scaled_font, + &hb_cairo_budget_user_data_key); + if (unlikely (!budget)) + return CAIRO_STATUS_NO_MEMORY; + hb_cairo_context_t c {scaled_font, cr, nullptr, budget}; hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale); @@ -643,7 +737,7 @@ hb_cairo_render_glyph (cairo_scaled_font_t *scaled_font, cairo_scale (cr, +1. / (x_scale ? x_scale : 1), -1. / (y_scale ? y_scale : 1)); - if (hb_font_draw_glyph_or_fail (font, glyph, hb_cairo_draw_get_funcs (), cr)) + if (hb_font_draw_glyph_or_fail (font, glyph, hb_cairo_draw_get_funcs (), &c)) cairo_fill (cr); // If draw fails, we still return SUCCESS, as we want empty drawing, not @@ -661,6 +755,10 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, { hb_font_t *font = (hb_font_t *) cairo_scaled_font_get_user_data (scaled_font, &hb_cairo_font_user_data_key); + hb_cairo_budget_t *budget = (hb_cairo_budget_t *) cairo_scaled_font_get_user_data (scaled_font, + &hb_cairo_budget_user_data_key); + if (unlikely (!budget)) + return CAIRO_STATUS_NO_MEMORY; unsigned int palette = 0; #ifdef CAIRO_COLOR_PALETTE_DEFAULT @@ -677,10 +775,10 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, +1. / (x_scale ? x_scale : 1), -1. / (y_scale ? y_scale : 1)); - hb_cairo_context_t c; - c.scaled_font = scaled_font; - c.cr = cr; - c.color_cache = (hb_map_t *) cairo_scaled_font_get_user_data (scaled_font, &color_cache_key); + hb_cairo_context_t c {scaled_font, + cr, + (hb_map_t *) cairo_scaled_font_get_user_data (scaled_font, &color_cache_key), + budget}; /* Synthesizing variant: mono glyphs render here too via the * fill_glyph foreground fallback inside hb_font_paint_glyph. diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 62fda1ca5..54eef9a7a 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -248,6 +248,12 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_RASTER_DRAW ((int64_t) 1 << 24) #endif +/* One cairo glyph-rendering session, shared by nested color-glyph + * callbacks and outline traversal. */ +#ifndef HB_BUDGET_CAIRO_PAINT +#define HB_BUDGET_CAIRO_PAINT ((int64_t) 16 << 20) +#endif + /* One raster draw session, in accumulated non-horizontal edges. */ #ifndef HB_RASTER_MAX_DRAW_EDGES #define HB_RASTER_MAX_DRAW_EDGES ((int64_t) 1 << 20) diff --git a/test/api/meson.build b/test/api/meson.build index 66b91af2a..68b7c34f9 100644 --- a/test/api/meson.build +++ b/test/api/meson.build @@ -95,6 +95,10 @@ if conf.get('HAVE_DIRECTWRITE', 0) == 1 ] endif +if conf.get('HAVE_CAIRO', 0) == 1 + tests += 'test-cairo.c' +endif + if conf.get('HAVE_PTHREAD', 0) == 1 tests += 'test-multithread.c' endif diff --git a/test/api/test-cairo.c b/test/api/test-cairo.c new file mode 100644 index 000000000..6faad1957 --- /dev/null +++ b/test/api/test-cairo.c @@ -0,0 +1,59 @@ +/* + * Copyright © 2026 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#include "hb-test.h" + +#include + +static void +test_hb_cairo_budget (void) +{ + hb_face_t *face = hb_test_open_font_file ("../fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-4886959609413632"); + cairo_font_face_t *cairo_face = hb_cairo_font_face_create_for_face (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 = {0, 0, 0}; + 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_face_destroy (face); +} + +int +main (int argc, char **argv) +{ + hb_test_init (&argc, &argv); + hb_test_add (test_hb_cairo_budget); + + return hb_test_run (); +}