[pango: 1/2] pangowin32: Clear cmap on finalize




commit 587834ca76aa80c2bf950396aa2e1edd0869f589
Author: Seungha Yang <seungha centricular com>
Date:   Wed Apr 7 18:38:20 2021 +0900

    pangowin32: Clear cmap on finalize
    
    Implement copy method for format_4_cmap and format_12_cmap, and
    free don't leak the struct.

 pango/pangowin32-fontmap.c |  4 ++--
 pango/pangowin32-private.h |  4 ++++
 pango/pangowin32.c         | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 2 deletions(-)
---
diff --git a/pango/pangowin32-fontmap.c b/pango/pangowin32-fontmap.c
index e5e81bba..5cccdce7 100644
--- a/pango/pangowin32-fontmap.c
+++ b/pango/pangowin32-fontmap.c
@@ -678,7 +678,7 @@ create_standard_family (PangoWin32FontMap *win32fontmap,
 
               new_face->has_cmap = old_face->has_cmap;
               new_face->cmap_format = old_face->cmap_format;
-              new_face->cmap = old_face->cmap;
+              new_face->cmap = _pango_win32_copy_cmap (old_face->cmap, old_face->cmap_format);
 
               new_face->cached_fonts = NULL;
 
@@ -1759,7 +1759,7 @@ pango_win32_face_finalize (GObject *object)
 
   g_free (win32face->face_name);
 
-  //g_free (win32face->cmap); // Err, cmap does not have lifecycle management currently :(
+  g_free (win32face->cmap);
 
   g_slist_free (win32face->cached_fonts);
 //  g_slist_free_full (win32face->cached_fonts, g_object_unref); // This doesn't work.
diff --git a/pango/pangowin32-private.h b/pango/pangowin32-private.h
index 1c9c1c09..3884c776 100644
--- a/pango/pangowin32-private.h
+++ b/pango/pangowin32-private.h
@@ -275,6 +275,10 @@ HFONT              _pango_win32_font_get_hfont         (PangoFont          *font);
 _PANGO_EXTERN
 HDC             _pango_win32_get_display_dc                 (void);
 
+_PANGO_EXTERN
+gpointer        _pango_win32_copy_cmap (gpointer cmap,
+                                        guint16 cmap_format);
+
 extern gboolean _pango_win32_debug;
 
 #endif /* __PANGOWIN32_PRIVATE_H__ */
diff --git a/pango/pangowin32.c b/pango/pangowin32.c
index 12b4a36e..f8fedcb3 100644
--- a/pango/pangowin32.c
+++ b/pango/pangowin32.c
@@ -1272,3 +1272,62 @@ pango_win32_font_create_hb_font (PangoFont *font)
 
   return hb_font;
 }
+
+gpointer
+_pango_win32_copy_cmap (gpointer cmap, guint16 cmap_format)
+{
+  if (!cmap)
+    return NULL;
+
+  if (cmap_format == 12)
+    {
+      struct format_12_cmap *new_table;
+      struct format_12_cmap *old_table;
+      guint32 *tbl, *tbl_end;
+
+      old_table = (struct format_12_cmap *) cmap;
+
+      new_table = g_malloc (old_table->length);
+      memcpy (old_table, new_table, sizeof (struct format_12_cmap));
+
+      tbl_end = (guint32 *) ((char *) new_table + new_table->length);
+      tbl = new_table->groups;
+
+      while (tbl < tbl_end)
+        {
+          *tbl = GUINT32_FROM_BE (*tbl);
+          tbl++;
+        }
+
+      return new_table;
+    }
+  else if (cmap_format == 4)
+    {
+      struct format_4_cmap *new_table;
+      struct format_4_cmap *old_table;
+      guint16 *tbl, *tbl_end;
+
+      old_table = (struct format_4_cmap *) cmap;
+
+      new_table = g_malloc (old_table->length);
+      memcpy (old_table, new_table, sizeof (struct format_4_cmap));
+
+      tbl_end = (guint16 *)((char *) new_table + new_table->length);
+      tbl = &new_table->reserved;
+
+      while (tbl < tbl_end)
+        {
+          *tbl = GUINT16_FROM_BE (*tbl);
+          tbl++;
+        }
+
+      return new_table;
+    }
+  else
+    {
+      /* got non-null cmap but unknown format, it shouldn't happen */
+      g_assert_not_reached ();
+    }
+
+  return NULL;
+}


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