From 813ccb637831ebd96e4f9fa29ee09e12ffa484aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Tue, 27 Feb 2018 11:45:34 +0100 Subject: [PATCH] emojichooser: Only measure reference emoji once Doing that once for every emoji is pretty slow and unnecessary as the width does not change. --- gtk/gtkemojichooser.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/gtk/gtkemojichooser.c b/gtk/gtkemojichooser.c index f9a665546e..ba287b1ae4 100644 --- a/gtk/gtkemojichooser.c +++ b/gtk/gtkemojichooser.c @@ -51,6 +51,8 @@ struct _GtkEmojiChooser GtkWidget *stack; GtkWidget *scrolled_window; + int emoji_max_width; + EmojiSection recent; EmojiSection people; EmojiSection body; @@ -346,7 +348,6 @@ add_emoji (GtkWidget *box, int i; PangoLayout *layout; PangoRectangle rect; - int width; codes = g_variant_get_child_value (item, 0); for (i = 0; i < g_variant_n_children (codes); i++) @@ -362,22 +363,17 @@ add_emoji (GtkWidget *box, g_variant_unref (codes); p[0] = 0; - label = gtk_label_new ("🙂"); + label = gtk_label_new (text); attrs = pango_attr_list_new (); pango_attr_list_insert (attrs, pango_attr_scale_new (PANGO_SCALE_X_LARGE)); gtk_label_set_attributes (GTK_LABEL (label), attrs); pango_attr_list_unref (attrs); - layout = gtk_label_get_layout (GTK_LABEL (label)); - pango_layout_get_extents (layout, &rect, NULL); - width = rect.width; - - gtk_label_set_text (GTK_LABEL (label), text); layout = gtk_label_get_layout (GTK_LABEL (label)); pango_layout_get_extents (layout, &rect, NULL); /* Check for fallback rendering that generates too wide items */ - if (rect.width >= 2 * width) + if (rect.width >= 2 * chooser->emoji_max_width) { gtk_widget_destroy (label); return; @@ -617,6 +613,26 @@ gtk_emoji_chooser_init (GtkEmojiChooser *chooser) gtk_widget_init_template (GTK_WIDGET (chooser)); + /* Get a reasonable maximum width for an emoji. We do this to + * skip overly wide fallback rendering for certain emojis the + * font does not contain and therefore end up being rendered + * as multiply glyphs. */ + { + PangoLayout *layout = gtk_widget_create_pango_layout (GTK_WIDGET (chooser), "🙂"); + PangoAttrList *attrs; + PangoRectangle rect; + + attrs = pango_attr_list_new (); + pango_attr_list_insert (attrs, pango_attr_scale_new (PANGO_SCALE_X_LARGE)); + pango_layout_set_attributes (layout, attrs); + pango_attr_list_unref (attrs); + + pango_layout_get_extents (layout, &rect, NULL); + chooser->emoji_max_width = rect.width; + + g_object_unref (layout); + } + chooser->recent_long_press = gtk_gesture_long_press_new (chooser->recent.box); g_signal_connect (chooser->recent_long_press, "pressed", G_CALLBACK (long_pressed_cb), chooser); chooser->recent_multi_press = gtk_gesture_multi_press_new (chooser->recent.box);