[cairo] Protect against infinite loops

Fixes https://github.com/harfbuzz/harfbuzz/issues/4375

Maybe we should do this in hb-ft and COLR instead?
diff --git a/src/hb-cairo-utils.hh b/src/hb-cairo-utils.hh
index a26bf59..5435563 100644
--- a/src/hb-cairo-utils.hh
+++ b/src/hb-cairo-utils.hh
@@ -30,11 +30,14 @@
 #include "hb.hh"
 #include "hb-cairo.h"
 
+#include "hb-set.hh"
+
 
 typedef struct
 {
   cairo_scaled_font_t *scaled_font;
   cairo_t *cr;
+  hb_set_t current_glyphs;
   hb_map_t *color_cache;
 } hb_cairo_context_t;
 
diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc
index f4f9f54..29461be 100644
--- a/src/hb-cairo.cc
+++ b/src/hb-cairo.cc
@@ -176,6 +176,11 @@
   hb_cairo_context_t *c = (hb_cairo_context_t *) paint_data;
   cairo_t *cr = c->cr;
 
+  if (unlikely (c->current_glyphs.has (glyph)))
+    return true;
+
+  c->current_glyphs.add (glyph);
+
   cairo_save (cr);
 
   hb_position_t x_scale, y_scale;
@@ -189,6 +194,8 @@
 
   cairo_restore (cr);
 
+  c->current_glyphs.del (glyph);
+
   return true;
 }
 
@@ -634,6 +641,7 @@
   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);
+  c.current_glyphs.add (glyph);
 
   hb_font_paint_glyph (font, glyph, hb_cairo_paint_get_funcs (), &c, palette, color);