From c37ea4f93ed726c37739325e3f181b3973182e29 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 23 Jan 2023 08:31:24 -0500 Subject: [PATCH 1/8] hb-cairo: Fix handling of foreground color Use the new cairo_user_scaled_font_get_foreground_color to obtain the foreground color, since the cr's source can't be trusted. Requires https://gitlab.freedesktop.org/cairo/cairo/-/merge_requests/420 --- src/hb-cairo.cc | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index b0bcb4a0f..f3d090fd6 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -585,11 +585,9 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, cairo_font_options_destroy (options); #endif - hb_color_t color = HB_COLOR (0, 0, 0, 255); - cairo_pattern_t *pattern = cairo_get_source (cr); double r, g, b, a; - if (cairo_pattern_get_rgba (pattern, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) - color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * 255.)); + cairo_user_scaled_font_get_foreground_color (scaled_font, &r, &g, &b, &a); + hb_color_t color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * 255.)); hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale); From 4afdbcbad55e5b5a4718c52398663cfd889a92ae Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 23 Jan 2023 15:53:26 -0700 Subject: [PATCH 2/8] [hb-cairo] Don't call get_foreground_color unnecessarily That would invalidate cairo cache on foreground change, even if the glyph doesn't need that. --- src/hb-cairo-utils.cc | 42 +++++++++++++++++++++++++++--------------- src/hb-cairo.cc | 23 +++++++++++++++-------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index 299fdbebf..d0214cb5c 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -274,6 +274,28 @@ _hb_cairo_normalize_color_line (hb_color_stop_t *stops, *omax = max; } +static void +_hb_cairo_get_color_stops (hb_cairo_context_t *c, + hb_color_line_t *color_line, + unsigned *count, + hb_color_stop_t **stops) +{ + unsigned len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); + if (len > *count) + *stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t)); + hb_color_line_get_color_stops (color_line, 0, &len, *stops); + for (unsigned i = 0; i < len; i++) + if ((*stops)[i].is_foreground) + { + double r, g, b, a; + cairo_user_scaled_font_get_foreground_color (c->scaled_font, &r, &g, &b, &a); + (*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), + round (a * hb_color_get_alpha ((*stops)[i].color))); + } + + *count = len; +} + void _hb_cairo_paint_linear_gradient (hb_cairo_context_t *c, hb_color_line_t *color_line, @@ -285,19 +307,16 @@ _hb_cairo_paint_linear_gradient (hb_cairo_context_t *c, hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS]; hb_color_stop_t *stops = stops_; - unsigned int len; + unsigned int len = PREALLOCATED_COLOR_STOPS; float xx0, yy0, xx1, yy1; float xxx0, yyy0, xxx1, yyy1; float min, max; cairo_pattern_t *pattern; - len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); - if (len > PREALLOCATED_COLOR_STOPS) - stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t)); - hb_color_line_get_color_stops (color_line, 0, &len, stops); + _hb_cairo_get_color_stops (c, color_line, &len, &stops); + _hb_cairo_normalize_color_line (stops, len, &min, &max); _hb_cairo_reduce_anchors (x0, y0, x1, y1, x2, y2, &xx0, &yy0, &xx1, &yy1); - _hb_cairo_normalize_color_line (stops, len, &min, &max); xxx0 = xx0 + min * (xx1 - xx0); yyy0 = yy0 + min * (yy1 - yy0); @@ -341,11 +360,7 @@ _hb_cairo_paint_radial_gradient (hb_cairo_context_t *c, float rr0, rr1; cairo_pattern_t *pattern; - len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); - if (len > PREALLOCATED_COLOR_STOPS) - stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t)); - hb_color_line_get_color_stops (color_line, 0, &len, stops); - + _hb_cairo_get_color_stops (c, color_line, &len, &stops); _hb_cairo_normalize_color_line (stops, len, &min, &max); xx0 = x0 + min * (x1 - x0); @@ -808,10 +823,7 @@ _hb_cairo_paint_sweep_gradient (hb_cairo_context_t *c, float max_x, max_y, radius; cairo_pattern_t *pattern; - len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); - if (len > PREALLOCATED_COLOR_STOPS) - stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t)); - hb_color_line_get_color_stops (color_line, 0, &len, stops); + _hb_cairo_get_color_stops (c, color_line, &len, &stops); hb_qsort (stops, len, sizeof (hb_color_stop_t), _hb_cairo_cmp_color_stop); diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index f3d090fd6..e934e7ef2 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -251,11 +251,20 @@ hb_cairo_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data; cairo_t *cr = c->cr; - cairo_set_source_rgba (cr, - hb_color_get_red (color) / 255., - hb_color_get_green (color) / 255., - hb_color_get_blue (color) / 255., - hb_color_get_alpha (color) / 255.); + if (use_foreground) + { + double r, g, b, a; + cairo_user_scaled_font_get_foreground_color (c->scaled_font, + &r, &g, &b, &a); + cairo_set_source_rgba (cr, r, g, b, + a * hb_color_get_alpha (color) / 255.); + } + else + cairo_set_source_rgba (cr, + hb_color_get_red (color) / 255., + hb_color_get_green (color) / 255., + hb_color_get_blue (color) / 255., + hb_color_get_alpha (color) / 255.); cairo_paint (cr); } @@ -585,9 +594,7 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, cairo_font_options_destroy (options); #endif - double r, g, b, a; - cairo_user_scaled_font_get_foreground_color (scaled_font, &r, &g, &b, &a); - hb_color_t color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * 255.)); + hb_color_t color = HB_COLOR (0, 0, 0, 255); hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale); From f9b3c79047aaedbffe4690b3bc6093c241ca5e90 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 23 Jan 2023 18:09:09 -0500 Subject: [PATCH 3/8] Update to different cairo API The cairo will likely end up begin a getter for a cairo_pattern_t instead of a color. --- src/hb-cairo-utils.cc | 11 ++++++++--- src/hb-cairo.cc | 12 +++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index d0214cb5c..dd27ed908 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -288,9 +288,14 @@ _hb_cairo_get_color_stops (hb_cairo_context_t *c, if ((*stops)[i].is_foreground) { double r, g, b, a; - cairo_user_scaled_font_get_foreground_color (c->scaled_font, &r, &g, &b, &a); - (*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), - round (a * hb_color_get_alpha ((*stops)[i].color))); + cairo_pattern_t *foreground; + + foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); + if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) + (*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), + round (a * hb_color_get_alpha ((*stops)[i].color))); + else + (*stops)[i].color = HB_COLOR (0, 0, 0, hb_color_get_alpha ((*stops)[i].color)); } *count = len; diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index e934e7ef2..afc1dd662 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -253,11 +253,14 @@ hb_cairo_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, if (use_foreground) { + cairo_pattern_t *foreground; double r, g, b, a; - cairo_user_scaled_font_get_foreground_color (c->scaled_font, - &r, &g, &b, &a); - cairo_set_source_rgba (cr, r, g, b, - a * hb_color_get_alpha (color) / 255.); + + foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); + if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) != CAIRO_STATUS_SUCCESS) + cairo_set_source_rgba (cr, r, g, b, a * hb_color_get_alpha (color) / 255.); + else + cairo_set_source_rgba (cr, 0, 0, 0, hb_color_get_alpha (color) / 255.); } else cairo_set_source_rgba (cr, @@ -595,7 +598,6 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, #endif hb_color_t color = HB_COLOR (0, 0, 0, 255); - hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale); cairo_scale (cr, +1./x_scale, -1./y_scale); From 26d34392e2dd2ea0e9908c1d53e1223487f021cc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 23 Jan 2023 16:21:13 -0700 Subject: [PATCH 4/8] [hb-cairo] Fix condition --- src/hb-cairo.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index afc1dd662..9d777017d 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -257,7 +257,7 @@ hb_cairo_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, double r, g, b, a; foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); - if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) != CAIRO_STATUS_SUCCESS) + if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) cairo_set_source_rgba (cr, r, g, b, a * hb_color_get_alpha (color) / 255.); else cairo_set_source_rgba (cr, 0, 0, 0, hb_color_get_alpha (color) / 255.); From 034d4d26f2dc31b73db72b94ca265ee45da44ddd Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 23 Jan 2023 16:22:26 -0700 Subject: [PATCH 5/8] [hb-cairo] Minor simplify --- src/hb-cairo-utils.cc | 4 +--- src/hb-cairo.cc | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index dd27ed908..1eeee7a40 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -288,9 +288,7 @@ _hb_cairo_get_color_stops (hb_cairo_context_t *c, if ((*stops)[i].is_foreground) { double r, g, b, a; - cairo_pattern_t *foreground; - - foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); + cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) (*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * hb_color_get_alpha ((*stops)[i].color))); diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 9d777017d..0033ea5be 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -253,10 +253,8 @@ hb_cairo_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, if (use_foreground) { - cairo_pattern_t *foreground; double r, g, b, a; - - foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); + cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) cairo_set_source_rgba (cr, r, g, b, a * hb_color_get_alpha (color) / 255.); else From 30d0d9c56c5f481b93141ca2742f6c992443ac46 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 24 Jan 2023 15:43:17 -0500 Subject: [PATCH 6/8] Adapt to cairo changes Adapt to the api in the cairo MR that will be used, and make the code build with older cairo. --- meson.build | 1 + src/hb-cairo-utils.cc | 4 +++- src/hb-cairo.cc | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 07ed493fb..9819d80c4 100644 --- a/meson.build +++ b/meson.build @@ -211,6 +211,7 @@ if cairo_dep.found() else check_funcs += [['cairo_user_font_face_set_render_color_glyph_func', {'deps': cairo_dep}]] check_funcs += [['cairo_font_options_get_custom_palette_color', {'deps': cairo_dep}]] + check_funcs += [['cairo_user_scaled_font_get_foreground_source', {'deps': cairo_dep}]] endif endif diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index 1eeee7a40..0d6a7e4b6 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -287,12 +287,14 @@ _hb_cairo_get_color_stops (hb_cairo_context_t *c, for (unsigned i = 0; i < len; i++) if ((*stops)[i].is_foreground) { +#ifdef HAVE_CAIRO_USER_SCALED_FONT_GET_FOREGROUND_SOURCE double r, g, b, a; - cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); + cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font, true); if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) (*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * hb_color_get_alpha ((*stops)[i].color))); else +#endif (*stops)[i].color = HB_COLOR (0, 0, 0, hb_color_get_alpha ((*stops)[i].color)); } diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 0033ea5be..64bcc63c6 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -253,11 +253,13 @@ hb_cairo_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, if (use_foreground) { +#ifdef HAVE_CAIRO_USER_SCALED_FONT_GET_FOREGROUND_SOURCE double r, g, b, a; - cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); + cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font, true); if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) cairo_set_source_rgba (cr, r, g, b, a * hb_color_get_alpha (color) / 255.); else +#endif cairo_set_source_rgba (cr, 0, 0, 0, hb_color_get_alpha (color) / 255.); } else From 29a36010a1514c72c207fec8b5ab8361617a0078 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 25 Jan 2023 17:26:33 -0700 Subject: [PATCH 7/8] [cairo] Adapt to cairo foreground API change again --- src/hb-cairo-utils.cc | 2 +- src/hb-cairo.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index 0d6a7e4b6..7b4ccefb6 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -289,7 +289,7 @@ _hb_cairo_get_color_stops (hb_cairo_context_t *c, { #ifdef HAVE_CAIRO_USER_SCALED_FONT_GET_FOREGROUND_SOURCE double r, g, b, a; - cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font, true); + cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) (*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * hb_color_get_alpha ((*stops)[i].color))); diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 64bcc63c6..43e3f05d4 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -255,7 +255,7 @@ hb_cairo_paint_color (hb_paint_funcs_t *pfuncs HB_UNUSED, { #ifdef HAVE_CAIRO_USER_SCALED_FONT_GET_FOREGROUND_SOURCE double r, g, b, a; - cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font, true); + cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font); if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) cairo_set_source_rgba (cr, r, g, b, a * hb_color_get_alpha (color) / 255.); else From e484d6b990171ba28e7ee8811f3a41d32b1d0418 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Jan 2023 12:55:25 -0700 Subject: [PATCH 8/8] [cairo] Handle malloc failure --- src/hb-cairo-utils.cc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc index 7b4ccefb6..77b3cb0e8 100644 --- a/src/hb-cairo-utils.cc +++ b/src/hb-cairo-utils.cc @@ -274,7 +274,7 @@ _hb_cairo_normalize_color_line (hb_color_stop_t *stops, *omax = max; } -static void +static bool _hb_cairo_get_color_stops (hb_cairo_context_t *c, hb_color_line_t *color_line, unsigned *count, @@ -282,7 +282,11 @@ _hb_cairo_get_color_stops (hb_cairo_context_t *c, { unsigned len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); if (len > *count) + { *stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t)); + if (unlikely (!stops)) + return false; + } hb_color_line_get_color_stops (color_line, 0, &len, *stops); for (unsigned i = 0; i < len; i++) if ((*stops)[i].is_foreground) @@ -299,6 +303,7 @@ _hb_cairo_get_color_stops (hb_cairo_context_t *c, } *count = len; + return true; } void @@ -318,7 +323,8 @@ _hb_cairo_paint_linear_gradient (hb_cairo_context_t *c, float min, max; cairo_pattern_t *pattern; - _hb_cairo_get_color_stops (c, color_line, &len, &stops); + if (unlikely (!_hb_cairo_get_color_stops (c, color_line, &len, &stops))) + return; _hb_cairo_normalize_color_line (stops, len, &min, &max); _hb_cairo_reduce_anchors (x0, y0, x1, y1, x2, y2, &xx0, &yy0, &xx1, &yy1); @@ -365,7 +371,8 @@ _hb_cairo_paint_radial_gradient (hb_cairo_context_t *c, float rr0, rr1; cairo_pattern_t *pattern; - _hb_cairo_get_color_stops (c, color_line, &len, &stops); + if (unlikely (!_hb_cairo_get_color_stops (c, color_line, &len, &stops))) + return; _hb_cairo_normalize_color_line (stops, len, &min, &max); xx0 = x0 + min * (x1 - x0); @@ -626,6 +633,12 @@ _hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops, { angles = (float *) hb_malloc (sizeof (float) * n_stops); colors = (hb_cairo_color_t *) hb_malloc (sizeof (hb_cairo_color_t) * n_stops); + if (unlikely (!angles || !colors)) + { + hb_free (angles); + hb_free (colors); + return; + } } for (unsigned i = 0; i < n_stops; i++) @@ -828,7 +841,8 @@ _hb_cairo_paint_sweep_gradient (hb_cairo_context_t *c, float max_x, max_y, radius; cairo_pattern_t *pattern; - _hb_cairo_get_color_stops (c, color_line, &len, &stops); + if (unlikely (!_hb_cairo_get_color_stops (c, color_line, &len, &stops))) + return; hb_qsort (stops, len, sizeof (hb_color_stop_t), _hb_cairo_cmp_color_stop);