[pango/pango1-dwrite: 12/24] pangowin32: Initialize DirectWrite




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

    pangowin32: Initialize DirectWrite
    
    We set up the boilerplate that is necessary for using DirectWrite in our code.
    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 | 86 +++++++++++++++++++++++++++++++++++++
 pango/pangowin32-fontmap.c          |  1 +
 pango/pangowin32-private.h          | 11 +++++
 pango/pangowin32.c                  | 15 +++++++
 6 files changed, 115 insertions(+), 1 deletion(-)
---
diff --git a/pango/meson.build b/pango/meson.build
index d6b2d81cd..7d18cb003 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 e95abc7a7..fbdad850d 100644
--- a/pango/pango-font-private.h
+++ b/pango/pango-font-private.h
@@ -61,7 +61,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..bb22efea2
--- /dev/null
+++ b/pango/pangowin32-dwrite-fontmap.cpp
@@ -0,0 +1,86 @@
+/* Pango
+ * pangowin32-dwrite-fontmap.cpp: Win32 font handling with DirectWrite
+ *
+ * Copyright (C) 2022 Luca Bacci
+ * Copyright (C) 2022 Chun-wei Fan
+ *
+ * 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"
+
+#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;
+};
+
+PangoWin32DWriteItems *
+pango_win32_init_direct_write (void)
+{
+  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 (SUCCEEDED (hr) && dwrite_items->dwrite_factory != NULL)
+    {
+      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);
+          dwrite_items->dwrite_factory->Release ();
+          failed = TRUE;
+        }
+    }
+  else
+    {
+      g_error ("DWriteCreateFactory failed with error code %x", (unsigned)hr);
+      failed = TRUE;
+    }
+
+
+  if (failed)
+    g_free (dwrite_items);
+
+  return failed ? NULL : dwrite_items;
+}
+
+void
+pango_win32_dwrite_items_destroy (PangoWin32DWriteItems *dwrite_items)
+{
+  dwrite_items->gdi_interop->Release ();
+  dwrite_items->dwrite_factory->Release ();
+
+  g_free (dwrite_items);
+}
diff --git a/pango/pangowin32-fontmap.c b/pango/pangowin32-fontmap.c
index 9b9801224..be6daa788 100644
--- a/pango/pangowin32-fontmap.c
+++ b/pango/pangowin32-fontmap.c
@@ -809,6 +809,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);
diff --git a/pango/pangowin32-private.h b/pango/pangowin32-private.h
index 9102662a4..ffd247c1b 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;
@@ -281,4 +284,12 @@ gpointer        _pango_win32_copy_cmap (gpointer cmap,
 
 extern gboolean _pango_win32_debug;
 
+PangoWin32DWriteItems *pango_win32_init_direct_write          (void);
+
+PangoWin32DWriteItems *pango_win32_get_direct_write_items     (void);
+
+void                   pango_win32_dwrite_items_destroy       (PangoWin32DWriteItems *items);
+
+G_END_DECLS
+
 #endif /* __PANGOWIN32_PRIVATE_H__ */
diff --git a/pango/pangowin32.c b/pango/pangowin32.c
index eed92dde7..55a079c9f 100644
--- a/pango/pangowin32.c
+++ b/pango/pangowin32.c
@@ -126,11 +126,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)
     {
@@ -151,9 +153,22 @@ _pango_win32_get_display_dc (void)
 #endif
     }
 
+  items = g_private_get (&dwrite_items);
+  if (items == NULL)
+    {
+      items = pango_win32_init_direct_write ();
+      g_private_set (&dwrite_items, items);
+    }
+
   return hdc;
 }
 
+PangoWin32DWriteItems *
+pango_win32_get_direct_write_items (void)
+{
+  return g_private_get (&dwrite_items);
+}
+
 /**
  * pango_win32_get_dc:
  *


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