[pango/strdup-avoidance: 1/3] fc: Avoid extra copies of family names




commit 94b7c4ae13e3b71ffc283db1989389f22bd66446
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 27 20:39:14 2022 -0400

    fc: Avoid extra copies of family names
    
    When we create a PangoFcFont from an FcPattern,
    we know that the pattern will live as long as
    the font and the font description we create
    at the same time. So there is no need to copy
    the strings we get out of the pattern.

 pango/pangofc-font.c            | 31 ++++++++++++++++---------------
 pango/pangofc-fontmap-private.h |  3 +++
 pango/pangofc-fontmap.c         | 25 +++++++++++++++++++------
 3 files changed, 38 insertions(+), 21 deletions(-)
---
diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c
index 9d555a371..02fb67fc6 100644
--- a/pango/pangofc-font.c
+++ b/pango/pangofc-font.c
@@ -24,6 +24,7 @@
 #include "pango-font-private.h"
 #include "pangofc-font-private.h"
 #include "pangofc-fontmap.h"
+#include "pangofc-fontmap-private.h"
 #include "pangofc-private.h"
 #include "pango-layout.h"
 #include "pango-impl-utils.h"
@@ -172,8 +173,8 @@ pango_fc_font_finalize (GObject *object)
       g_object_unref (fontmap);
     }
 
-  FcPatternDestroy (fcfont->font_pattern);
   pango_font_description_free (fcfont->description);
+  FcPatternDestroy (fcfont->font_pattern);
 
   if (priv->decoder)
     _pango_fc_font_set_decoder (fcfont, NULL);
@@ -209,9 +210,9 @@ pattern_is_transformed (FcPattern *pattern)
 
 static void
 pango_fc_font_set_property (GObject       *object,
-                           guint          prop_id,
-                           const GValue  *value,
-                           GParamSpec    *pspec)
+                            guint          prop_id,
+                            const GValue  *value,
+                            GParamSpec    *pspec)
 {
   PangoFcFont *fcfont = PANGO_FC_FONT (object);
 
@@ -219,25 +220,25 @@ pango_fc_font_set_property (GObject       *object,
     {
     case PROP_PATTERN:
       {
-       FcPattern *pattern = g_value_get_pointer (value);
+        FcPattern *pattern = g_value_get_pointer (value);
 
-       g_return_if_fail (pattern != NULL);
-       g_return_if_fail (fcfont->font_pattern == NULL);
+        g_return_if_fail (pattern != NULL);
+        g_return_if_fail (fcfont->font_pattern == NULL);
 
-       FcPatternReference (pattern);
-       fcfont->font_pattern = pattern;
-       fcfont->description = pango_fc_font_description_from_pattern (pattern, TRUE);
-       fcfont->is_hinted = pattern_is_hinted (pattern);
-       fcfont->is_transformed = pattern_is_transformed (pattern);
+        FcPatternReference (pattern);
+        fcfont->font_pattern = pattern;
+        fcfont->description = font_description_from_pattern (pattern, TRUE, TRUE);
+        fcfont->is_hinted = pattern_is_hinted (pattern);
+        fcfont->is_transformed = pattern_is_transformed (pattern);
       }
       goto set_decoder;
 
     case PROP_FONTMAP:
       {
-       PangoFcFontMap *fcfontmap = PANGO_FC_FONT_MAP (g_value_get_object (value));
+        PangoFcFontMap *fcfontmap = PANGO_FC_FONT_MAP (g_value_get_object (value));
 
-       g_return_if_fail (fcfont->fontmap == NULL);
-       g_weak_ref_set ((GWeakRef *) &fcfont->fontmap, fcfontmap);
+        g_return_if_fail (fcfont->fontmap == NULL);
+        g_weak_ref_set ((GWeakRef *) &fcfont->fontmap, fcfontmap);
       }
       goto set_decoder;
 
diff --git a/pango/pangofc-fontmap-private.h b/pango/pangofc-fontmap-private.h
index 68daea6cb..42fa7e3d8 100644
--- a/pango/pangofc-fontmap-private.h
+++ b/pango/pangofc-fontmap-private.h
@@ -191,6 +191,9 @@ struct _PangoFcFontMapClass
   void (*_pango_reserved4) (void);
 };
 
+PangoFontDescription *font_description_from_pattern (FcPattern *pattern,
+                                                     gboolean   include_size,
+                                                     gboolean   shallow);
 
 G_END_DECLS
 
diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c
index e21032f3f..51f71cb13 100644
--- a/pango/pangofc-fontmap.c
+++ b/pango/pangofc-fontmap.c
@@ -2773,6 +2773,14 @@ pango_fc_convert_width_to_pango (int fc_stretch)
  */
 PangoFontDescription *
 pango_fc_font_description_from_pattern (FcPattern *pattern, gboolean include_size)
+{
+  return font_description_from_pattern (pattern, include_size, FALSE);
+}
+
+PangoFontDescription *
+font_description_from_pattern (FcPattern *pattern,
+                               gboolean   include_size,
+                               gboolean   shallow)
 {
   PangoFontDescription *desc;
   PangoStyle style;
@@ -2782,8 +2790,7 @@ pango_fc_font_description_from_pattern (FcPattern *pattern, gboolean include_siz
   PangoGravity gravity;
   PangoVariant variant;
   gboolean all_caps;
-
-  FcChar8 *s;
+  const char *s;
   int i;
   double d;
   FcResult res;
@@ -2793,7 +2800,10 @@ pango_fc_font_description_from_pattern (FcPattern *pattern, gboolean include_siz
   res = FcPatternGetString (pattern, FC_FAMILY, 0, (FcChar8 **) &s);
   g_assert (res == FcResultMatch);
 
-  pango_font_description_set_family (desc, (gchar *)s);
+  if (shallow)
+    pango_font_description_set_family_static (desc, s);
+  else
+    pango_font_description_set_family (desc, s);
 
   if (FcPatternGetInteger (pattern, FC_SLANT, 0, &i) == FcResultMatch)
     style = pango_fc_convert_slant_to_pango (i);
@@ -2821,8 +2831,6 @@ pango_fc_font_description_from_pattern (FcPattern *pattern, gboolean include_siz
 
   for (int i = 0; i < 32; i++)
     {
-      const char *s;
-
       if (FcPatternGetString (pattern, FC_FONT_FEATURES, i, (FcChar8 **)&s) == FcResultMatch)
         {
           if (strcmp (s, "smcp=1") == 0)
@@ -2907,7 +2915,12 @@ pango_fc_font_description_from_pattern (FcPattern *pattern, gboolean include_siz
   if (include_size && FcPatternGetString (pattern, FC_FONT_VARIATIONS, 0, (FcChar8 **)&s) == FcResultMatch)
     {
       if (s && *s)
-        pango_font_description_set_variations (desc, (char *)s);
+        {
+          if (shallow)
+            pango_font_description_set_variations_static (desc, s);
+          else
+            pango_font_description_set_variations (desc, s);
+        }
     }
 
   return desc;


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