[pango/pango2-windows: 2/5] pangodwrite-fontmap: Add APIs to query font description from Windows fonts




commit 9663cd69864054474ee549f2abdf6f8b66d98fb7
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Tue Jul 5 16:35:12 2022 +0800

    pangodwrite-fontmap: Add APIs to query font description from Windows fonts
    
    Add 2 APIs for querying the Pango2FontDescription that corresponds to the
    specified Windows LOGFONTW (GDI font) or IDWriteFont, largely using the
    code that we already have in the DirectWrite support.
    
    Notice that with the LOGFONTW path, we use DirectWrite's GDI interop to
    acquire a IDWriteFont first and then go down the same (existing) route
    as when we use a IDWriteFont, since it may be necessary to use LOGFONTW's
    since Windows APIs such as SystemParametersInfoW() return LOGFONTW's
    instead of IDWriteFont's, but DirectWrite has more fine-grained support
    for font properties than GDI.

 pango2/pangodwrite-fontmap.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++
 pango2/pangodwrite-fontmap.h   | 12 +++++-
 2 files changed, 96 insertions(+), 1 deletion(-)
---
diff --git a/pango2/pangodwrite-fontmap.cpp b/pango2/pangodwrite-fontmap.cpp
index 6fa51c240..1cb6e4b60 100644
--- a/pango2/pangodwrite-fontmap.cpp
+++ b/pango2/pangodwrite-fontmap.cpp
@@ -408,6 +408,91 @@ pango2_direct_write_font_map_new (void)
   return (Pango2DirectWriteFontMap *) g_object_new (PANGO2_TYPE_DIRECT_WRITE_FONT_MAP, NULL);
 }
 
+/*
+ * Check whether the gpointer passed in for dwrite_font is indeed an IDWriteFont*
+ * Required for C compatiblity in pangodwrite-fontmap.h
+ */
+template <class T>
+gboolean is_dwrite_font (T *t) {return FALSE;}
+
+template <>
+gboolean is_dwrite_font (IDWriteFont *t) {return TRUE;}
+
+/**
+ * pango2_direct_write_font_map_get_font_description_from_dwrite_font:
+ * @font_map: the `Pango2FontMap` to use
+ * @dwrite_font: the `IDWriteFont` to query the font description
+ *
+ * Returns a Pango2FontDescription that corresonds to the IDWriteFont.
+ *
+ * Return value: the `Pango2FontDescription` corresonding to @dwrite_font,
+ * the returned value should be freed with [method Pango2 FontDescription free]
+ */
+Pango2FontDescription *
+pango2_direct_write_font_map_get_font_description_from_dwrite_font (Pango2FontMap *font_map,
+                                                                    gpointer       dwrite_font)
+{
+  HRESULT hr;
+  IDWriteFont *font = NULL;
+  IDWriteFontFamily *family = NULL;
+  Pango2FontDescription *desc = NULL;
+
+  g_return_val_if_fail (PANGO2_IS_DIRECT_WRITE_FONT_MAP (font_map), NULL);
+  g_return_val_if_fail (is_dwrite_font (dwrite_font), NULL);
+
+  font = reinterpret_cast<IDWriteFont*>(dwrite_font);
+  hr = font->GetFontFamily (&family);
+
+  if (SUCCEEDED (hr) && family != NULL)
+    {
+      char *family_name = util_dwrite_get_font_family_name (family);
+
+      if (family_name != NULL)
+        desc = util_get_pango2_font_description (font, family_name);
+
+      family->Release ();
+    }
+  else
+    g_error ("IDWriteFont::GetFontFamily failed with error code %x\n", (unsigned)hr);
+
+  return desc;
+}
+
+/**
+ * pango2_direct_write_font_map_get_font_description_from_logfontw:
+ * @font_map: the `Pango2FontMap` to use
+ * @lfw: the `LOGFONTW` to query the font description
+ *
+ * Returns a Pango2FontDescription that corresonds to the LOGFONTW.
+ *
+ * Return value: the `Pango2FontDescription` corresonding to @lfw,
+ * the returned value should be freed with [method Pango2 FontDescription free]
+ */
+Pango2FontDescription *
+pango2_direct_write_font_map_get_font_description_from_logfontw (Pango2FontMap *font_map,
+                                                                 LOGFONTW      *lfw)
+{
+  HRESULT hr;
+  IDWriteFont *dwrite_font = NULL;
+  Pango2DirectWriteFontMap *dwrite_fontmap = NULL;
+  Pango2FontDescription *desc = NULL;
+
+  g_return_val_if_fail (PANGO2_IS_DIRECT_WRITE_FONT_MAP (font_map), NULL);
+
+  dwrite_fontmap = PANGO2_DIRECT_WRITE_FONT_MAP (font_map);
+  hr = dwrite_fontmap->gdi_interop->CreateFontFromLOGFONT (lfw, &dwrite_font);
+
+  if (FAILED (hr) || dwrite_font == NULL)
+    {
+      g_error ("IDWriteFactory::GdiInterop::CreateFontFromLOGFONT failed with error code %x\n", 
(unsigned)hr);
+      return NULL;
+    }
+
+  desc = pango2_direct_write_font_map_get_font_description_from_dwrite_font (font_map, dwrite_font);
+  dwrite_font->Release ();
+
+  return desc;
+}
 /* }}} */
 
 /* vim:set foldmethod=marker expandtab: */
diff --git a/pango2/pangodwrite-fontmap.h b/pango2/pangodwrite-fontmap.h
index c873759da..f52ddf5ec 100644
--- a/pango2/pangodwrite-fontmap.h
+++ b/pango2/pangodwrite-fontmap.h
@@ -21,6 +21,9 @@
 
 #pragma once
 
+#define WIN32_LEAN_AND_MEAN 1
+
+#include <windows.h>
 #include <pango2/pango.h>
 
 G_BEGIN_DECLS
@@ -34,7 +37,14 @@ PANGO2_DECLARE_INTERNAL_TYPE (Pango2DirectWriteFontMap,
                               Pango2FontMap)
 
 PANGO2_AVAILABLE_IN_ALL
-Pango2DirectWriteFontMap * pango2_direct_write_font_map_new (void);
+Pango2DirectWriteFontMap *pango2_direct_write_font_map_new                                   (void);
+
+PANGO2_AVAILABLE_IN_ALL
+Pango2FontDescription    *pango2_direct_write_font_map_get_font_description_from_dwrite_font (Pango2FontMap 
*font_map,
+                                                                                              gpointer       
dwrite_font);
 
+PANGO2_AVAILABLE_IN_ALL
+Pango2FontDescription    *pango2_direct_write_font_map_get_font_description_from_logfontw    (Pango2FontMap 
*font_map,
+                                                                                              LOGFONTW      
*lfw);
 
 G_END_DECLS


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