[pango/pango2-windows: 1/7] pangodwrite-fontmap.cpp: Initilize DirectWrite in object init




commit bca656f696a786d5673999248358436ee4d560ea
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Wed Jun 15 11:23:53 2022 +0800

    pangodwrite-fontmap.cpp: Initilize DirectWrite in object init
    
    We don't really need to reinitialize and destroy the DirectWrite
    factory whenever we (re-)populate the PangoFontMap.  Just initialize
    DirectWrite when we create the PangoDirectWriteFontMap object, and
    release/destroy it upon object finalization.

 pango/pangodwrite-fontmap.cpp | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)
---
diff --git a/pango/pangodwrite-fontmap.cpp b/pango/pangodwrite-fontmap.cpp
index 1ef30324..9a274904 100644
--- a/pango/pangodwrite-fontmap.cpp
+++ b/pango/pangodwrite-fontmap.cpp
@@ -48,6 +48,8 @@
 struct _PangoDirectWriteFontMap
 {
   PangoFontMap parent_instance;
+
+  IDWriteFactory *dwrite_factory;
 };
 
 struct _PangoDirectWriteFontMapClass
@@ -231,18 +233,12 @@ util_create_pango_hb_face (IDWriteFontFamily *family,
 static void
 pango_direct_write_font_map_populate (PangoFontMap *map)
 {
-  IDWriteFactory *factory = NULL;
+  PangoDirectWriteFontMap *dwrite_map = PANGO_DIRECT_WRITE_FONT_MAP (map);
   IDWriteFontCollection *collection = NULL;
   UINT32 count;
   HRESULT hr;
 
-  hr = DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED,
-                            UUID_OF_IDWriteFactory,
-                            reinterpret_cast<IUnknown**> (&factory));
-  if (FAILED (hr) || !factory)
-    g_error ("DWriteCreateFactory failed with error code %x", (unsigned)hr);
-
-  hr = factory->GetSystemFontCollection (&collection, FALSE);
+  hr = dwrite_map->dwrite_factory->GetSystemFontCollection (&collection, FALSE);
   if (FAILED (hr) || collection == NULL)
     g_error ("IDWriteFactory::GetSystemFontCollection failed with error code %x\n", (unsigned)hr);
 
@@ -297,9 +293,6 @@ pango_direct_write_font_map_populate (PangoFontMap *map)
   collection->Release ();
   collection = NULL;
 
-  factory->Release ();
-  factory = NULL;
-
   /* Add generic aliases */
   struct {
     const char *alias_name;
@@ -333,19 +326,33 @@ pango_direct_write_font_map_populate (PangoFontMap *map)
 }
 
 /* }}} */
-/* {{{ PangoDirctWriteFontMap implementation */
+/* {{{ PangoDirectWriteFontMap implementation */
 
 G_DEFINE_FINAL_TYPE (PangoDirectWriteFontMap, pango_direct_write_font_map, PANGO_TYPE_FONT_MAP)
 
 static void
 pango_direct_write_font_map_init (PangoDirectWriteFontMap *self)
 {
+  HRESULT hr;
+
+  hr = DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED,
+                            UUID_OF_IDWriteFactory,
+                            reinterpret_cast<IUnknown**> (&self->dwrite_factory));
+
+  if (FAILED (hr) || !self->dwrite_factory)
+    g_error ("DWriteCreateFactory failed with error code %x", (unsigned)hr);
+
   pango_font_map_repopulate (PANGO_FONT_MAP (self), TRUE);
 }
 
 static void
 pango_direct_write_font_map_finalize (GObject *object)
 {
+  PangoDirectWriteFontMap *dwrite_map = PANGO_DIRECT_WRITE_FONT_MAP (object);
+
+  dwrite_map->dwrite_factory->Release ();
+  dwrite_map->dwrite_factory = NULL;
+
   G_OBJECT_CLASS (pango_direct_write_font_map_parent_class)->finalize (object);
 }
 


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