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




commit f3b088666b4f7d4c8df433bc29a6da63354b034a
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 | 88 +++++++++++++++++++++++++++++++++++++
 pango/pangowin32-fontmap.c          |  1 +
 pango/pangowin32-private.h          | 11 +++++
 pango/pangowin32.c                  | 15 +++++++
 6 files changed, 117 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 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..b100bce21
--- /dev/null
+++ b/pango/pangowin32-dwrite-fontmap.cpp
@@ -0,0 +1,88 @@
+/* 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"
+
+#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]