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




commit c6b3222f992ef080dcb79c777cc2b8b9fafb8cc1
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 | 74 ++++++++++++++++++++++++++++++++++++++++++
 pango2/pangodwrite-fontmap.h   | 12 ++++++-
 2 files changed, 85 insertions(+), 1 deletion(-)
---
diff --git a/pango2/pangodwrite-fontmap.cpp b/pango2/pangodwrite-fontmap.cpp
index 6fa51c240..585472425 100644
--- a/pango2/pangodwrite-fontmap.cpp
+++ b/pango2/pangodwrite-fontmap.cpp
@@ -408,6 +408,80 @@ pango2_direct_write_font_map_new (void)
   return (Pango2DirectWriteFontMap *) g_object_new (PANGO2_TYPE_DIRECT_WRITE_FONT_MAP, NULL);
 }
 
+/**
+ * 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);
+
+  font = static_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]