[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.
This commit is contained in:
Behdad Esfahbod 2023-01-23 15:53:26 -07:00 committed by Matthias Clasen
parent c37ea4f93e
commit 4afdbcbad5
2 changed files with 42 additions and 23 deletions

View File

@ -274,6 +274,28 @@ _hb_cairo_normalize_color_line (hb_color_stop_t *stops,
*omax = max; *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 void
_hb_cairo_paint_linear_gradient (hb_cairo_context_t *c, _hb_cairo_paint_linear_gradient (hb_cairo_context_t *c,
hb_color_line_t *color_line, 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_[PREALLOCATED_COLOR_STOPS];
hb_color_stop_t *stops = stops_; hb_color_stop_t *stops = stops_;
unsigned int len; unsigned int len = PREALLOCATED_COLOR_STOPS;
float xx0, yy0, xx1, yy1; float xx0, yy0, xx1, yy1;
float xxx0, yyy0, xxx1, yyy1; float xxx0, yyy0, xxx1, yyy1;
float min, max; float min, max;
cairo_pattern_t *pattern; cairo_pattern_t *pattern;
len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); _hb_cairo_get_color_stops (c, color_line, &len, &stops);
if (len > PREALLOCATED_COLOR_STOPS) _hb_cairo_normalize_color_line (stops, len, &min, &max);
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_reduce_anchors (x0, y0, x1, y1, x2, y2, &xx0, &yy0, &xx1, &yy1); _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); xxx0 = xx0 + min * (xx1 - xx0);
yyy0 = yy0 + min * (yy1 - yy0); yyy0 = yy0 + min * (yy1 - yy0);
@ -341,11 +360,7 @@ _hb_cairo_paint_radial_gradient (hb_cairo_context_t *c,
float rr0, rr1; float rr0, rr1;
cairo_pattern_t *pattern; cairo_pattern_t *pattern;
len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); _hb_cairo_get_color_stops (c, color_line, &len, &stops);
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_normalize_color_line (stops, len, &min, &max); _hb_cairo_normalize_color_line (stops, len, &min, &max);
xx0 = x0 + min * (x1 - x0); 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; float max_x, max_y, radius;
cairo_pattern_t *pattern; cairo_pattern_t *pattern;
len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr); _hb_cairo_get_color_stops (c, color_line, &len, &stops);
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_qsort (stops, len, sizeof (hb_color_stop_t), _hb_cairo_cmp_color_stop); hb_qsort (stops, len, sizeof (hb_color_stop_t), _hb_cairo_cmp_color_stop);

View File

@ -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; hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data;
cairo_t *cr = c->cr; cairo_t *cr = c->cr;
cairo_set_source_rgba (cr, if (use_foreground)
hb_color_get_red (color) / 255., {
hb_color_get_green (color) / 255., double r, g, b, a;
hb_color_get_blue (color) / 255., cairo_user_scaled_font_get_foreground_color (c->scaled_font,
hb_color_get_alpha (color) / 255.); &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); cairo_paint (cr);
} }
@ -585,9 +594,7 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font,
cairo_font_options_destroy (options); cairo_font_options_destroy (options);
#endif #endif
double r, g, b, a; hb_color_t color = HB_COLOR (0, 0, 0, 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_position_t x_scale, y_scale;
hb_font_get_scale (font, &x_scale, &y_scale); hb_font_get_scale (font, &x_scale, &y_scale);