From 2214326a9669af950593da08c98505c62e27974a Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 18 Mar 2021 00:09:58 -0400 Subject: context: Cache metrics for the current font GTK calls pango_context_get_metrics() frequently to determine character widths and alignments, and does not expect this to involve expensive layout operations. --- pango/pango-context.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pango/pango-context.c b/pango/pango-context.c index 1db11a1c..ccaa1bac 100644 --- a/pango/pango-context.c +++ b/pango/pango-context.c @@ -63,6 +63,8 @@ struct _PangoContext PangoFontMap *font_map; + PangoFontMetrics *metrics; + gboolean round_glyph_positions; }; @@ -121,6 +123,9 @@ pango_context_finalize (GObject *object) if (context->matrix) pango_matrix_free (context->matrix); + if (context->metrics) + pango_font_metrics_unref (context->metrics); + G_OBJECT_CLASS (pango_context_parent_class)->finalize (object); } @@ -1783,6 +1788,11 @@ pango_context_get_metrics (PangoContext *context, if (!language) language = context->language; + if (desc == context->font_desc && + language == context->language && + context->metrics != NULL) + return pango_font_metrics_ref (context->metrics); + current_fonts = pango_font_map_load_fontset (context->font_map, context, desc, language); metrics = get_base_metrics (current_fonts); @@ -1797,6 +1807,10 @@ pango_context_get_metrics (PangoContext *context, g_object_unref (current_fonts); + if (desc == context->font_desc && + language == context->language) + context->metrics = pango_font_metrics_ref (metrics); + return metrics; } @@ -1806,6 +1820,8 @@ context_changed (PangoContext *context) context->serial++; if (context->serial == 0) context->serial++; + + g_clear_pointer (&context->metrics, pango_font_metrics_unref); } /** -- cgit v1.2.1