[pango/pango1-dwrite] PangoWin32: Use HarfBuzz platform APIs if available



commit 0135e4a30adec9904587dd181969bd5de926f9a4
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Tue Aug 16 17:10:48 2022 +0800

    PangoWin32: Use HarfBuzz platform APIs if available
    
    Use the DirectWrite and/or GDI APIs in HarfBuzz to create the hb_font_t's that
    we need, if they are available in HarfBuzz.  Use the former raw-data method
    if neither are available, or if using DirectWrite failed and GDI support is
    not available.

 pango/pangowin32-dwrite-fontmap.cpp | 12 ++++++++++++
 pango/pangowin32-private.h          |  2 ++
 pango/pangowin32.c                  | 37 ++++++++++++++++++++++++++++++++-----
 3 files changed, 46 insertions(+), 5 deletions(-)
---
diff --git a/pango/pangowin32-dwrite-fontmap.cpp b/pango/pangowin32-dwrite-fontmap.cpp
index 4975f12a2..04be11ee4 100644
--- a/pango/pangowin32-dwrite-fontmap.cpp
+++ b/pango/pangowin32-dwrite-fontmap.cpp
@@ -25,6 +25,10 @@
 #include <initguid.h>
 #include <dwrite_1.h>
 
+#ifdef HAVE_HARFBUZZ_DIRECT_WRITE
+#include <hb-directwrite.h>
+#endif
+
 #ifdef STRICT
 #undef STRICT
 #endif
@@ -558,6 +562,14 @@ pango_win32_dwrite_font_check_is_hinted (PangoWin32Font *font)
   return result;
 }
 
+#ifdef HAVE_HARFBUZZ_DIRECT_WRITE
+hb_face_t *
+pango_win32_dwrite_font_face_create_hb_font (gpointer face)
+{
+  return hb_directwrite_face_create ((IDWriteFontFace *)face);
+}
+#endif
+
 void
 pango_win32_dwrite_font_release (gpointer dwrite_font)
 {
diff --git a/pango/pangowin32-private.h b/pango/pangowin32-private.h
index 1502ecd92..89bee4ec3 100644
--- a/pango/pangowin32-private.h
+++ b/pango/pangowin32-private.h
@@ -321,6 +321,8 @@ gpointer               pango_win32_logfontw_get_dwrite_font   (LOGFONTW
 PangoFontDescription *
 pango_win32_font_description_from_logfontw_dwrite             (const LOGFONTW        *logfontw);
 
+hb_face_t      *pango_win32_dwrite_font_face_create_hb_font   (gpointer               face);
+
 G_END_DECLS
 
 #endif /* __PANGOWIN32_PRIVATE_H__ */
diff --git a/pango/pangowin32.c b/pango/pangowin32.c
index 5a26ee98b..06b8424b3 100644
--- a/pango/pangowin32.c
+++ b/pango/pangowin32.c
@@ -29,6 +29,10 @@
 #include <glib.h>
 #include <hb.h>
 
+#if defined (HAVE_HARFBUZZ_GDI)
+#include <hb-gdi.h>
+#endif
+
 #include "pango-impl-utils.h"
 #include "pangowin32.h"
 #include "pangowin32-private.h"
@@ -1285,20 +1289,43 @@ hfont_reference_table (hb_face_t *face, hb_tag_t tag, void *user_data)
 static hb_font_t *
 pango_win32_font_create_hb_font (PangoFont *font)
 {
-  PangoWin32Font *win32font = (PangoWin32Font *)font;
-  HFONT hfont;
+  PangoWin32Font *win32font = PANGO_WIN32_FONT (font);
+  HFONT hfont = NULL;
   hb_face_t *face = NULL;
   hb_font_t *hb_font = NULL;
+  static const hb_user_data_key_t key;
+  hb_destroy_func_t destroy_func = NULL;
+  void *destroy_obj = NULL;
+  gpointer dwrite_font_face = NULL;
 
   g_return_val_if_fail (font != NULL, NULL);
 
-  hfont = _pango_win32_font_get_hfont (font);
+#ifdef HAVE_HARFBUZZ_DIRECT_WRITE
+  dwrite_font_face = pango_win32_font_get_dwrite_font_face (win32font);
 
-  /* We are *not* allowed to destroy the HFONT here ! */
-  face = hb_face_create_for_tables (hfont_reference_table, (void *)hfont, NULL);
+  if (dwrite_font_face != NULL)
+    {
+      face = pango_win32_dwrite_font_face_create_hb_font (dwrite_font_face);
+      destroy_func = pango_win32_dwrite_font_face_release;
+      destroy_obj = dwrite_font_face;
+    }
+#endif
+  if (face == NULL)
+    {
+      hfont = _pango_win32_font_get_hfont (font);
+
+#ifdef HAVE_HARFBUZZ_GDI
+      face = hb_gdi_face_create (hfont);
+#else
+      face = hb_face_create_for_tables (hfont_reference_table, (void *)hfont, NULL);
+#endif
+    }
 
   hb_font = hb_font_create (face);
   hb_font_set_scale (hb_font, win32font->size, win32font->size);
+  if (destroy_func != NULL && destroy_obj != NULL)
+    hb_font_set_user_data (hb_font, &key, destroy_obj, destroy_func, TRUE);
+
   hb_face_destroy (face);
 
   return hb_font;


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