[pango/pango1-dwrite] pangowin32: Initialize DirectWrite early on



commit a5d4af87e6636b850862c31bcdefef35494eb4a9
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Tue Jul 12 16:15:26 2022 +0800

    pangowin32: Initialize DirectWrite early on
    
    Make sure that is available, so that we can be sure that we don't want to
    repeatedly create it and its GDI interop.

 pango/pangowin32-dwrite-fontmap.cpp | 41 ++++++++-----------------------------
 pango/pangowin32-fontmap.c          |  4 +---
 pango/pangowin32-private.h          | 12 ++++-------
 pango/pangowin32.c                  | 21 +++++++++++++++++++
 4 files changed, 35 insertions(+), 43 deletions(-)
---
diff --git a/pango/pangowin32-dwrite-fontmap.cpp b/pango/pangowin32-dwrite-fontmap.cpp
index 8d50667e9..b71870782 100644
--- a/pango/pangowin32-dwrite-fontmap.cpp
+++ b/pango/pangowin32-dwrite-fontmap.cpp
@@ -47,7 +47,7 @@ struct _PangoWin32DWriteItems
 };
 
 PangoWin32DWriteItems *
-init_direct_write (void)
+pango_win32_init_direct_write (void)
 {
   PangoWin32DWriteItems *dwrite_items = g_new0 (PangoWin32DWriteItems, 1);
   HRESULT hr;
@@ -80,18 +80,8 @@ init_direct_write (void)
 }
 
 void
-pango_win32_dwrite_font_map_init (PangoWin32FontMap *map)
+pango_win32_dwrite_items_destroy (PangoWin32DWriteItems *dwrite_items)
 {
-  PangoWin32DWriteItems *dwrite_items = init_direct_write ();
-
-  map->dwrite_items = dwrite_items;
-}
-
-void
-pango_win32_dwrite_font_map_destroy (PangoWin32FontMap *map)
-{
-  PangoWin32DWriteItems *dwrite_items = map->dwrite_items;
-
   dwrite_items->gdi_interop->Release ();
   dwrite_items->dwrite_factory->Release ();
 
@@ -105,7 +95,7 @@ pango_win32_dwrite_font_map_populate (PangoWin32FontMap *map)
   UINT32 count;
   HRESULT hr;
   gboolean failed = FALSE;
-  PangoWin32DWriteItems *dwrite_items = map->dwrite_items;
+  PangoWin32DWriteItems *dwrite_items = pango_win32_get_direct_write_items ();
 
   hr = dwrite_items->dwrite_factory->GetSystemFontCollection (&collection, FALSE);
   if (FAILED (hr) || collection == NULL)
@@ -179,10 +169,9 @@ pango_win32_dwrite_font_map_populate (PangoWin32FontMap *map)
 }
 
 gpointer
-pango_win32_font_map_logfontw_get_dwrite_font (PangoWin32FontMap *map,
-                                               LOGFONTW          *logfontw)
+pango_win32_logfontw_get_dwrite_font (LOGFONTW *logfontw)
 {
-  PangoWin32DWriteItems *dwrite_items = map->dwrite_items;
+  PangoWin32DWriteItems *dwrite_items = pango_win32_get_direct_write_items ();
   IDWriteFont *font = NULL;
   HRESULT hr;
 
@@ -374,8 +363,10 @@ pango_win32_font_description_from_logfontw_dwrite (LOGFONTW *logfontw)
   IDWriteFont *font = NULL;
   HRESULT hr;
   PangoWin32DWriteItems *dwrite_items;
+  /* Make sure DirectWrite is set up */
+  _pango_win32_get_display_dc ();
 
-  dwrite_items = init_direct_write ();
+  dwrite_items = pango_win32_get_direct_write_items ();
   if (dwrite_items == NULL)
     return NULL;
 
@@ -387,10 +378,6 @@ pango_win32_font_description_from_logfontw_dwrite (LOGFONTW *logfontw)
       font->Release ();
     }
 
-  dwrite_items->gdi_interop->Release ();
-  dwrite_items->dwrite_factory->Release ();
-  g_free (dwrite_items);
-
   return desc;
 }
 
@@ -405,15 +392,6 @@ pango_win32_dwrite_font_release (gpointer dwrite_font)
 
 #else
 /* no-op's, no DirectWrite support */
-void
-pango_win32_dwrite_font_map_init (PangoWin32FontMap *map)
-{
-}
-
-void
-pango_win32_dwrite_font_map_destroy (PangoWin32FontMap *map)
-{
-}
 
 void
 pango_win32_dwrite_font_release (gpointer dwrite_font)
@@ -428,8 +406,7 @@ pango_win32_font_description_from_dwrite_font (gpointer dwrite_font)
 }
 
 gpointer
-pango_win32_font_map_logfontw_get_dwrite_font (PangoWin32FontMap *map,
-                                               LOGFONTW          *logfontw)
+pango_win32_logfontw_get_dwrite_font (LOGFONTW* logfontw)
 {
   return NULL;
 }
diff --git a/pango/pangowin32-fontmap.c b/pango/pangowin32-fontmap.c
index cfe7f366d..695b35f3e 100644
--- a/pango/pangowin32-fontmap.c
+++ b/pango/pangowin32-fontmap.c
@@ -702,7 +702,6 @@ _pango_win32_font_map_init (PangoWin32FontMap *win32fontmap)
   LOGFONTW logfont;
   HDC hdc = _pango_win32_get_display_dc ();
   struct EnumProcData enum_proc_data = {hdc, win32fontmap};
-  pango_win32_dwrite_font_map_init (win32fontmap);
 
   win32fontmap->families =
     g_hash_table_new_full ((GHashFunc) case_insensitive_str_hash,
@@ -873,7 +872,6 @@ pango_win32_font_map_finalize (GObject *object)
   g_hash_table_destroy (win32fontmap->fonts);
   g_hash_table_destroy (win32fontmap->families);
 
-  pango_win32_dwrite_font_map_destroy (win32fontmap);
   G_OBJECT_CLASS (_pango_win32_font_map_parent_class)->finalize (object);
 }
 
@@ -1703,7 +1701,7 @@ pango_win32_insert_font (PangoWin32FontMap *win32fontmap,
   *lfp2 = *lfp;
 
   if (dwrite_font == NULL)
-    dwrite_font = pango_win32_font_map_logfontw_get_dwrite_font (win32fontmap, lfp2);
+    dwrite_font = pango_win32_logfontw_get_dwrite_font (lfp2);
 
   g_hash_table_insert (win32fontmap->fonts, lfp2, lfp2);
 
diff --git a/pango/pangowin32-private.h b/pango/pangowin32-private.h
index d23f74423..5f6410e29 100644
--- a/pango/pangowin32-private.h
+++ b/pango/pangowin32-private.h
@@ -174,9 +174,6 @@ struct _PangoWin32FontMap
 
   double resolution;           /* (points / pixel) * PANGO_SCALE */
 
-  /* keep track of DirectWrite boiler plate items */
-  PangoWin32DWriteItems *dwrite_items;
-
   /* Map LOGFONTWs to IDWriteFonts corresponding to actual fonts
    * installed, if applicable.
    */
@@ -371,14 +368,13 @@ void            pango_win32_insert_font                       (PangoWin32FontMap
 _PANGO_EXTERN
 gboolean        pango_win32_font_check_is_hinted              (PangoWin32Font    *font);
 
-void            pango_win32_dwrite_font_map_init              (PangoWin32FontMap *map);
-
 void            pango_win32_dwrite_font_map_populate          (PangoWin32FontMap *map);
 
-void            pango_win32_dwrite_font_map_destroy           (PangoWin32FontMap *map);
+PangoWin32DWriteItems *pango_win32_init_direct_write          (void);
+PangoWin32DWriteItems *pango_win32_get_direct_write_items     (void);
+void            pango_win32_dwrite_items_destroy              (PangoWin32DWriteItems *items);
 
-gpointer        pango_win32_font_map_logfontw_get_dwrite_font (PangoWin32FontMap *map,
-                                                               LOGFONTW *logfontw);
+gpointer        pango_win32_logfontw_get_dwrite_font          (LOGFONTW *logfontw);
 
 PangoFontDescription *
 pango_win32_font_description_from_logfontw_dwrite             (LOGFONTW *logfontw);
diff --git a/pango/pangowin32.c b/pango/pangowin32.c
index 007a246e2..b2c3e942b 100644
--- a/pango/pangowin32.c
+++ b/pango/pangowin32.c
@@ -130,11 +130,13 @@ _pango_win32_font_init (PangoWin32Font *win32font)
 }
 
 static GPrivate display_dc_key = G_PRIVATE_INIT ((GDestroyNotify) DeleteDC);
+static GPrivate dwrite_items = G_PRIVATE_INIT ((GDestroyNotify) pango_win32_dwrite_items_destroy);
 
 HDC
 _pango_win32_get_display_dc (void)
 {
   HDC hdc = g_private_get (&display_dc_key);
+  PangoWin32DWriteItems *items;
 
   if (hdc == NULL)
     {
@@ -155,9 +157,28 @@ _pango_win32_get_display_dc (void)
 #endif
     }
 
+#if HAVE_DIRECTWRITE
+  items = g_private_get (&dwrite_items);
+  if (items == NULL)
+    {
+      items = pango_win32_init_direct_write ();
+      g_private_set (&dwrite_items, items);
+    }
+#endif
+
   return hdc;
 }
 
+PangoWin32DWriteItems *
+pango_win32_get_direct_write_items ()
+{
+#ifdef HAVE_DIRECTWRITE
+  return g_private_get (&dwrite_items);
+#else
+  return NULL;
+#endif
+}
+
 /**
  * pango_win32_get_dc:
  *


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