Merge branch 'fix-fontrendering-demo' into 'main'

gtk-demo: Avoid a segfault

Closes #5166

See merge request GNOME/gtk!5014
This commit is contained in:
Matthias Clasen 2022-09-09 16:57:27 +00:00
commit ae2964c7c6

View File

@ -217,30 +217,39 @@ update_image (void)
text = " "; text = " ";
ch = g_utf8_get_char (text); ch = g_utf8_get_char (text);
str = g_string_new (""); str = g_string_new ("");
layout = pango_layout_new (context);
retry:
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {
g_string_append_unichar (str, ch); g_string_append_unichar (str, ch);
g_string_append_unichar (str, 0x200c); g_string_append_unichar (str, 0x200c);
} }
layout = pango_layout_new (context);
pango_layout_set_font_description (layout, desc); pango_layout_set_font_description (layout, desc);
pango_layout_set_text (layout, str->str, -1); pango_layout_set_text (layout, str->str, -1);
g_string_free (str, TRUE);
pango_layout_get_extents (layout, &ink, &logical); pango_layout_get_extents (layout, &ink, &logical);
pango_extents_to_pixels (&logical, NULL); 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); surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, logical.width * 3 / 2, 4*logical.height);
cr = cairo_create (surface); cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1, 1, 1); cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr); cairo_paint (cr);
iter = pango_layout_get_iter (layout);
run = pango_layout_iter_get_run (iter);
cairo_set_source_rgb (cr, 0, 0, 0); cairo_set_source_rgb (cr, 0, 0, 0);
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {