[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.
diff --git a/src/hb-cairo-utils.cc b/src/hb-cairo-utils.cc
index 299fdbe..d0214cb 100644
--- a/src/hb-cairo-utils.cc
+++ b/src/hb-cairo-utils.cc
@@ -274,6 +274,28 @@
   *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_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 @@
   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 @@
   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 f3d090f..e934e7e 100644
--- a/src/hb-cairo.cc
+++ b/src/hb-cairo.cc
@@ -251,11 +251,20 @@
   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 @@
   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);