[pango/pango1-dwrite: 3/3] pangowin32: Initialize DirectWrite if found




commit ad09382fde94d7d5d8d6ad72a2a125490d97b5e5
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Wed Jul 6 18:07:27 2022 +0800

    pangowin32: Initialize DirectWrite if found
    
    We set up the boilerplate that is necessary for using DirectWrite in our code,
    if DirectWrite support is enabled.  Also add code to tear it down after we are
    done with it.

 pango/meson.build                   |   1 +
 pango/pango-font-private.h          |   2 +-
 pango/pangowin32-dwrite-fontmap.cpp | 107 ++++++++++++++++++++++++++++++++++++
 pango/pangowin32-fontmap.c          |   3 +
 pango/pangowin32-private.h          |  12 ++++
 5 files changed, 124 insertions(+), 1 deletion(-)
---
diff --git a/pango/meson.build b/pango/meson.build
index 1a4b41419..2c2dfa8cb 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -424,6 +424,7 @@ if host_system == 'windows'
     'pangowin32.c',
     'pangowin32-fontcache.c',
     'pangowin32-fontmap.c',
+    'pangowin32-dwrite-fontmap.cpp',
   ]
 
   pangowin32_deps = pango_deps + [
diff --git a/pango/pango-font-private.h b/pango/pango-font-private.h
index 885e38c17..d2c46f4eb 100644
--- a/pango/pango-font-private.h
+++ b/pango/pango-font-private.h
@@ -59,7 +59,7 @@ void     pango_font_get_matrix        (PangoFont   *font,
 static inline int pango_font_get_absolute_size (PangoFont *font)
 {
   GTypeClass *klass = (GTypeClass *) PANGO_FONT_GET_CLASS (font);
-  PangoFontClassPrivate *priv = g_type_class_get_private (klass, PANGO_TYPE_FONT);
+  PangoFontClassPrivate *priv = (PangoFontClassPrivate *) g_type_class_get_private (klass, PANGO_TYPE_FONT);
   return priv->get_absolute_size (font);
 }
 
diff --git a/pango/pangowin32-dwrite-fontmap.cpp b/pango/pangowin32-dwrite-fontmap.cpp
new file mode 100644
index 000000000..b2e690c25
--- /dev/null
+++ b/pango/pangowin32-dwrite-fontmap.cpp
@@ -0,0 +1,107 @@
+/* Pango
+ * pangowin32-dwrite-fontmap.cpp: Win32 font handling with DirectWrite
+ *
+ * Copyright (C) 2000 Red Hat Software
+ * Copyright (C) 2000 Tor Lillqvist
+ * Copyright (C) 2001 Alexander Larsson
+ * Copyright (C) 2007 Novell, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#ifdef HAVE_DIRECTWRITE
+
+#include <initguid.h>
+#include <dwrite.h>
+
+#ifdef STRICT
+#undef STRICT
+#endif
+#include "pangowin32-private.h"
+
+#ifdef _MSC_VER
+# define UUID_OF_IDWriteFactory __uuidof (IDWriteFactory)
+#else
+# define UUID_OF_IDWriteFactory IID_IDWriteFactory
+#endif
+
+struct _PangoWin32DWriteItems
+{
+  IDWriteFactory    *dwrite_factory;
+  IDWriteGdiInterop *gdi_interop;
+};
+
+void
+pango_win32_dwrite_font_map_init (PangoWin32FontMap *map)
+{
+  PangoWin32DWriteItems *dwrite_items = g_new0 (PangoWin32DWriteItems, 1);
+  HRESULT hr;
+  gboolean failed = FALSE;
+
+  hr = DWriteCreateFactory (DWRITE_FACTORY_TYPE_SHARED,
+                            UUID_OF_IDWriteFactory,
+                            reinterpret_cast<IUnknown**> (&dwrite_items->dwrite_factory));
+
+  if (FAILED (hr) || dwrite_items->dwrite_factory == NULL)
+    {
+      g_error ("DWriteCreateFactory failed with error code %x", (unsigned)hr);
+      failed = TRUE;
+    }
+
+  hr = dwrite_items->dwrite_factory->GetGdiInterop (&dwrite_items->gdi_interop);
+
+  if (FAILED (hr) || dwrite_items->gdi_interop == NULL)
+    {
+      g_error ("DWriteCreateFactory::GetGdiInterop failed with error code %x", (unsigned)hr);
+      failed = TRUE;
+    }
+
+  if (failed)
+    {
+      if (dwrite_items->dwrite_factory != NULL)
+        dwrite_items->dwrite_factory->Release ();
+
+      return;
+    }
+
+  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 ();
+
+  g_free (dwrite_items);
+}
+
+#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)
+{
+}
+#endif
\ No newline at end of file
diff --git a/pango/pangowin32-fontmap.c b/pango/pangowin32-fontmap.c
index 9b9801224..e6b64eb0d 100644
--- a/pango/pangowin32-fontmap.c
+++ b/pango/pangowin32-fontmap.c
@@ -706,6 +706,7 @@ _pango_win32_font_map_init (PangoWin32FontMap *win32fontmap)
   LOGFONTW logfont;
   HDC hdc = _pango_win32_get_display_dc ();
   struct EnumProcData enum_proc_data;
+  pango_win32_dwrite_font_map_init (win32fontmap);
 
   win32fontmap->families =
     g_hash_table_new_full ((GHashFunc) case_insensitive_str_hash,
@@ -809,6 +810,7 @@ _pango_win32_font_map_class_init (PangoWin32FontMapClass *class)
                                           (GEqualFunc)alias_equal,
                                           (GDestroyNotify)alias_free,
                                           NULL);
+
 #ifdef HAVE_CAIRO_WIN32
   read_windows_fallbacks (class->aliases);
   read_builtin_aliases (class->aliases);
@@ -868,6 +870,7 @@ 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);
 }
 
diff --git a/pango/pangowin32-private.h b/pango/pangowin32-private.h
index 9102662a4..db7d73a3c 100644
--- a/pango/pangowin32-private.h
+++ b/pango/pangowin32-private.h
@@ -57,6 +57,8 @@
 #include "pango-fontset.h"
 #include "pango-fontmap-private.h"
 
+G_BEGIN_DECLS
+
 #define PANGO_TYPE_WIN32_FONT_MAP             (_pango_win32_font_map_get_type ())
 #define PANGO_WIN32_FONT_MAP(object)          (G_TYPE_CHECK_INSTANCE_CAST ((object), 
PANGO_TYPE_WIN32_FONT_MAP, PangoWin32FontMap))
 #define PANGO_WIN32_IS_FONT_MAP(object)       (G_TYPE_CHECK_INSTANCE_TYPE ((object), 
PANGO_TYPE_WIN32_FONT_MAP))
@@ -73,6 +75,7 @@
 
 typedef struct _PangoWin32FontMap      PangoWin32FontMap;
 typedef struct _PangoWin32FontMapClass PangoWin32FontMapClass;
+typedef struct _PangoWin32DWriteItems  PangoWin32DWriteItems;
 typedef struct _PangoWin32Font         PangoWin32Font;
 typedef struct _PangoWin32FontClass    PangoWin32FontClass;
 typedef struct _PangoWin32Face         PangoWin32Face;
@@ -103,6 +106,9 @@ struct _PangoWin32FontMap
   GHashTable *warned_fonts;
 
   double resolution;           /* (points / pixel) * PANGO_SCALE */
+
+  /* keep track of DirectWrite boiler plate items */
+  PangoWin32DWriteItems *dwrite_items;
 };
 
 struct _PangoWin32FontMapClass
@@ -281,4 +287,10 @@ gpointer        _pango_win32_copy_cmap (gpointer cmap,
 
 extern gboolean _pango_win32_debug;
 
+void            pango_win32_dwrite_font_map_init    (PangoWin32FontMap *map);
+
+void            pango_win32_dwrite_font_map_destroy (PangoWin32FontMap *map);
+
+G_END_DECLS
+
 #endif /* __PANGOWIN32_PRIVATE_H__ */


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