[pango/pango2-windows: 5/5] PangoCairo: Add support creating GDI-based cairo_font_face_t's




commit 88cd3ff9e38fbaf1eb13da6411a2ad5fe73265d2
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Mon Jul 4 18:27:33 2022 +0800

    PangoCairo: Add support creating GDI-based cairo_font_face_t's
    
    If HarfBuzz is built with GDI/Uniscribe support, allow one to create LOGFONTW-
    based cairo_font_face_t's by retrieving a LOGFONTW from the hb_font_t, so that
    applications that still rely on GDI can continue to work with Pango2 even if
    they do not have the custom renderer that is needed for DirectWrite fonts.
    
    Support for enabling this code path, as with creating the hb_face_t with
    GDI-based LOGFONTW's, is not yet added into the code.

 meson.build                  |  7 ++--
 pango2/meson.build           |  5 +++
 pango2/pangocairo-font.c     | 19 ++++++++-
 pango2/pangocairo-gdi-font.c | 95 ++++++++++++++++++++++++++++++++++++++++++++
 pango2/pangocairo-private.h  |  5 +++
 5 files changed, 127 insertions(+), 4 deletions(-)
---
diff --git a/meson.build b/meson.build
index 34f1b5815..53d907cf8 100644
--- a/meson.build
+++ b/meson.build
@@ -325,9 +325,10 @@ if host_system == 'windows'
     cc.find_library('dwrite'),
   ]
 
-  if cc.has_header_symbol('hb-gdi.h',
-                          'hb_gdi_face_create',
-                          dependencies: harfbuzz_dep)
+  harfbuzz_have_gdi = cc.has_header_symbol('hb-gdi.h',
+                                           'hb_gdi_face_create',
+                                           dependencies: harfbuzz_dep)
+  if harfbuzz_have_gdi
     pango_conf.set('HAVE_GDI', 1)
   endif
 
diff --git a/pango2/meson.build b/pango2/meson.build
index e0f0626db..ee86b278c 100644
--- a/pango2/meson.build
+++ b/pango2/meson.build
@@ -177,6 +177,11 @@ if host_system == 'windows'
     pango_sources += [
       'pangocairo-dwrite-font.cpp',
     ]
+    if harfbuzz_have_gdi
+      pango_sources += [
+        'pangocairo-gdi-font.c',
+      ]
+    endif
   endif
 
   pango_features_conf.set('PANGO2_HAS_DIRECT_WRITE_FONTMAP', 1)
diff --git a/pango2/pangocairo-font.c b/pango2/pangocairo-font.c
index 3349389ce..21a89ac1e 100644
--- a/pango2/pangocairo-font.c
+++ b/pango2/pangocairo-font.c
@@ -34,6 +34,7 @@
 #include "pango-userfont-private.h"
 #include "pango-userface-private.h"
 #include "pango-font-private.h"
+#include "pangodwrite-fontmap.h"
 
 static Pango2CairoFontPrivate * _pango2_font_get_cairo_font_private (Pango2Font *font);
 static cairo_scaled_font_t * _pango2_font_get_scaled_font (Pango2Font *font);
@@ -84,7 +85,23 @@ create_cairo_font_face (Pango2Font *font)
 #endif
 
 #ifdef HAVE_DIRECT_WRITE
-  cairo_face = create_cairo_dwrite_font_face (font);
+#ifdef HAVE_GDI
+  gboolean use_gdi = FALSE;
+  Pango2FontFamily *family = pango2_font_face_get_family (pango2_font_get_face (font));
+  if (family)
+    {
+      Pango2FontMap *fontmap = pango2_font_family_get_font_map (family);
+
+      if (PANGO2_IS_DIRECT_WRITE_FONT_MAP (fontmap))
+        use_gdi = pango2_direct_write_font_map_get_use_gdi (fontmap);
+    }
+
+  if (use_gdi)
+    cairo_face = create_cairo_gdi_font_face (font);
+  else
+#endif
+    cairo_face = create_cairo_dwrite_font_face (font);
+
   if (cairo_face)
     return cairo_face;
 #endif
diff --git a/pango2/pangocairo-gdi-font.c b/pango2/pangocairo-gdi-font.c
new file mode 100644
index 000000000..bb92ce80f
--- /dev/null
+++ b/pango2/pangocairo-gdi-font.c
@@ -0,0 +1,95 @@
+/* Pango
+ * pangocairo-dwrite-font.cpp: PangoCairo fonts using DirectWrite
+ *
+ * Copyright (C) 2022 the GTK team
+ *
+ * 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_DIRECT_WRITE
+
+#include "pangocairo-private.h"
+#include "pango-font-private.h"
+#include "pango-font.h"
+
+#include <windows.h>
+#include <hb-uniscribe.h>
+#include <cairo-win32.h>
+
+cairo_font_face_t *
+create_cairo_gdi_font_face (Pango2Font *font)
+{
+  hb_font_t *hb_font;
+  hb_font_t *hb_font_new = hb_font_get_empty ();
+  hb_face_t *hb_face;
+  hb_face_t *hb_face_new = hb_face_get_empty ();
+  hb_blob_t *blob = hb_blob_get_empty ();
+  unsigned int idx;
+  cairo_font_face_t *result = NULL;
+
+  hb_font = pango2_font_get_hb_font (font);
+
+  hb_face = hb_font_get_face (hb_font);
+  idx = hb_face_get_index (hb_face);
+  blob = pango2_font_get_hb_blob (font);
+
+  hb_face_new = hb_face_create (blob, idx);
+  if (hb_face_new != hb_face_get_empty ())
+    {
+      HFONT hfont = NULL;
+      hb_font_t *hb_font_new = hb_font_create (hb_face_new);
+
+      if (hb_font_new != hb_font_get_empty ())
+        hfont = hb_uniscribe_font_get_hfont (hb_font_new);
+
+      if (hfont)
+        result = cairo_win32_font_face_create_for_hfont (hfont);
+    }
+
+  if (result != NULL)
+    {
+      static cairo_user_data_key_t key;
+      static cairo_user_data_key_t key2;
+      static cairo_user_data_key_t key3;
+
+      cairo_font_face_set_user_data (result, &key,
+                                     hb_font_new,
+                                     (cairo_destroy_func_t) hb_font_destroy);
+      cairo_font_face_set_user_data (result, &key2,
+                                     hb_face_new,
+                                     (cairo_destroy_func_t) hb_face_destroy);
+      cairo_font_face_set_user_data (result, &key3,
+                                     blob,
+                                     (cairo_destroy_func_t) hb_blob_destroy);
+    }
+  else
+    {
+      if (hb_font_new != hb_font_get_empty ())
+        hb_font_destroy (hb_font_new);
+      if (hb_face_new != hb_face_get_empty ())
+        hb_face_destroy (hb_face_new);
+
+      hb_blob_destroy (blob);
+    }
+
+  return result;
+}
+
+#endif /* HAVE_DIRECT_WRITE */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/pango2/pangocairo-private.h b/pango2/pangocairo-private.h
index be9fa8cf9..0467d75d2 100644
--- a/pango2/pangocairo-private.h
+++ b/pango2/pangocairo-private.h
@@ -84,4 +84,9 @@ cairo_font_face_t *
 create_cairo_dwrite_font_face (Pango2Font *font);
 #endif
 
+#ifdef HAVE_GDI
+cairo_font_face_t *
+create_cairo_gdi_font_face (Pango2Font *font);
+#endif
+
 G_END_DECLS


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