gtk-demo: Avoid a segfault
authorMatthias Clasen <mclasen@redhat.com>
Fri, 9 Sep 2022 16:33:08 +0000 (12:33 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 9 Sep 2022 16:34:54 +0000 (12:34 -0400)
The code in the fontrendering demo is a bit sloppy
and assumes that we always get a single run when
appending a sequence of 4 chars and 4 spaces.

That is not in general true, such as for Emoji.

Instead of working harder to handle Emoji here,
just give up and fall back to 'a'.

Fixes: #5166
demos/gtk-demo/fontrendering.c

index 283523f48110f76bb31159ede765552435efd18a..9f85d2c03bcd3ad41f17111e319914378b816fcb 100644 (file)
@@ -217,30 +217,39 @@ update_image (void)
         text = " ";
 
       ch = g_utf8_get_char (text);
-
       str = g_string_new ("");
+      layout = pango_layout_new (context);
 
+retry:
       for (i = 0; i < 4; i++)
         {
           g_string_append_unichar (str, ch);
           g_string_append_unichar (str, 0x200c);
         }
 
-      layout = pango_layout_new (context);
       pango_layout_set_font_description (layout, desc);
       pango_layout_set_text (layout, str->str, -1);
-      g_string_free (str, TRUE);
       pango_layout_get_extents (layout, &ink, &logical);
       pango_extents_to_pixels (&logical, NULL);
 
+      iter = pango_layout_get_iter (layout);
+      run = pango_layout_iter_get_run (iter);
+
+      if (run->glyphs->num_glyphs < 8)
+        {
+          /* not a good char to use */
+          g_string_truncate (str, 0);
+          ch = 'a';
+          goto retry;
+        }
+
+      g_string_free (str, TRUE);
+
       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, logical.width * 3 / 2, 4*logical.height);
       cr = cairo_create (surface);
       cairo_set_source_rgb (cr, 1, 1, 1);
       cairo_paint (cr);
 
-      iter = pango_layout_get_iter (layout);
-      run = pango_layout_iter_get_run (iter);
-
       cairo_set_source_rgb (cr, 0, 0, 0);
       for (i = 0; i < 4; i++)
         {