[pango/speed-up-format-filtering: 10/12] fc: Only do filtering by format once




commit 8006b28e265d7020f0bf0addcb5c5ebe289fd4f9
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Aug 16 08:17:50 2020 -0400

    fc: Only do filtering by format once
    
    The fc config changes rarely, so we can avoid
    unnecessary work by only doing the filtering by
    supported font formats once, and keeping the
    result.

 pango/pangofc-fontmap.c | 87 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 72 insertions(+), 15 deletions(-)
---
diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c
index 4c2aa094..b2a6285e 100644
--- a/pango/pangofc-fontmap.c
+++ b/pango/pangofc-fontmap.c
@@ -157,6 +157,8 @@ struct _PangoFcFontMapPrivate
   guint closed : 1;
 
   FcConfig *config;
+  FcFontSet *config_fonts[2];
+  FcFontSet *filtered_fonts[2];
 };
 
 struct _PangoFcFontFaceData
@@ -837,6 +839,9 @@ pango_fc_is_supported_font_format (FcPattern* pattern)
   return FALSE;
 }
 
+static FcFontSet **pango_fc_font_map_get_config_fonts (PangoFcFontMap *fcfontmap,
+                                                       int            *nsets);
+
 static FcFontSet *
 filter_fontset_by_format (FcFontSet *fontset)
 {
@@ -863,10 +868,21 @@ pango_fc_patterns_get_font_pattern (PangoFcPatterns *pats, int i, gboolean *prep
   if (i == 0)
     {
       FcResult result;
+
       if (!pats->match && !pats->fontset)
-       pats->match = FcFontMatch (pats->fontmap->priv->config, pats->pattern, &result);
+        {
+          FcFontSet **sets;
+          int nsets;
+
+          sets = pango_fc_font_map_get_config_fonts (pats->fontmap, &nsets);
 
-      if (pats->match && pango_fc_is_supported_font_format (pats->match))
+         pats->match = FcFontSetMatch (pats->fontmap->priv->config,
+                                        sets, nsets,
+                                        pats->pattern,
+                                        &result);
+        }
+
+      if (pats->match)
        {
          *prepare = FALSE;
          return pats->match;
@@ -876,21 +892,12 @@ pango_fc_patterns_get_font_pattern (PangoFcPatterns *pats, int i, gboolean *prep
   if (!pats->fontset)
     {
       FcResult result;
-      FcFontSet *filtered[2] = { NULL, };
-      int i, n = 0;
+      FcFontSet **sets;
+      int nsets;
 
-      for (i = 0; i < 2; i++)
-        {
-          FcFontSet *fonts = FcConfigGetFonts (pats->fontmap->priv->config, i);
-          if (fonts)
-            filtered[n++] = filter_fontset_by_format (fonts);
-        }
-
-      pats->fontset = FcFontSetSort (pats->fontmap->priv->config, filtered, n, pats->pattern, FcTrue, NULL, 
&result);
-
-      for (i = 0; i < n; i++)
-        FcFontSetDestroy (filtered[i]);
+      sets = pango_fc_font_map_get_config_fonts (pats->fontmap, &nsets);
 
+      pats->fontset = FcFontSetSort (pats->fontmap->priv->config, sets, nsets, pats->pattern, FcTrue, NULL, 
&result);
 
       if (pats->match)
         {
@@ -1214,6 +1221,12 @@ pango_fc_font_map_fini (PangoFcFontMap *fcfontmap)
   PangoFcFontMapPrivate *priv = fcfontmap->priv;
   int i;
 
+  for (i = 0; i < 2; i++)
+    {
+      if (priv->filtered_fonts[i])
+        FcFontSetDestroy (priv->filtered_fonts[i]);
+    }
+
   g_queue_free (priv->fontset_cache);
   priv->fontset_cache = NULL;
 
@@ -2055,6 +2068,7 @@ pango_fc_font_map_set_config (PangoFcFontMap *fcfontmap,
                              FcConfig       *fcconfig)
 {
   FcConfig *oldconfig;
+  int i;
 
   g_return_if_fail (PANGO_IS_FC_FONT_MAP (fcfontmap));
 
@@ -2065,6 +2079,14 @@ pango_fc_font_map_set_config (PangoFcFontMap *fcfontmap,
 
   fcfontmap->priv->config = fcconfig;
 
+  for (i = 0; i < 2; i++)
+    {
+      if (fcfontmap->priv->filtered_fonts[i])
+        FcFontSetDestroy (fcfontmap->priv->filtered_fonts[i]);
+      fcfontmap->priv->filtered_fonts[i] = NULL;
+      fcfontmap->priv->config_fonts[i] = NULL;
+    }
+
   if (oldconfig != fcconfig)
     pango_fc_font_map_config_changed (fcfontmap);
 
@@ -2093,6 +2115,41 @@ pango_fc_font_map_get_config (PangoFcFontMap *fcfontmap)
   return fcfontmap->priv->config;
 }
 
+static FcFontSet **
+pango_fc_font_map_get_config_fonts (PangoFcFontMap *fcfontmap,
+                                    int            *nsets)
+{
+  FcFontSet *fonts;
+  int set;
+  int n;
+
+  n = 0;
+  for (set = 0; set < 2; set++)
+    {
+      fonts = FcConfigGetFonts (fcfontmap->priv->config, set);
+      if (fonts != fcfontmap->priv->config_fonts[set])
+        {
+          if (fcfontmap->priv->filtered_fonts[set])
+            {
+              FcFontSetDestroy (fcfontmap->priv->filtered_fonts[set]);
+              fcfontmap->priv->filtered_fonts[set] = NULL;
+              fcfontmap->priv->config_fonts[set] = NULL;
+            }
+          if (fonts)
+            {
+              fcfontmap->priv->filtered_fonts[set] = filter_fontset_by_format (fonts);
+              fcfontmap->priv->config_fonts[set] = fonts;
+            }
+        }
+      if (fcfontmap->priv->filtered_fonts[set])
+        n++;
+    }
+
+  *nsets = n;
+
+  return fcfontmap->priv->filtered_fonts;
+}
+
 static PangoFcFontFaceData *
 pango_fc_font_map_get_font_face_data (PangoFcFontMap *fcfontmap,
                                      FcPattern      *font_pattern)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]