[pango/pango2-windows] Windows: Split out code for aliases and fallbacks



commit 2c530520e9780b9a8843cfed5d3237f93603fe90
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Fri Jul 1 11:53:10 2022 +0800

    Windows: Split out code for aliases and fallbacks
    
    We want to make this code shared between the DirectWrite code and the legacy
    GDI code, since we want to add aliases and fallbacks for all cases, at least on
    Windows (well, font family names do not differ between DirectWrite and GDI on
    a given Windows system :))

 pango2/meson.build                |   1 +
 pango2/pangodwrite-fontmap.cpp    | 128 +------------------------------
 pango2/pangowin32-utils-private.h |  29 +++++++
 pango2/pangowin32-utils.c         | 155 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 188 insertions(+), 125 deletions(-)
---
diff --git a/pango2/meson.build b/pango2/meson.build
index 0a3e8d323..e0f0626db 100644
--- a/pango2/meson.build
+++ b/pango2/meson.build
@@ -170,6 +170,7 @@ if host_system == 'windows'
 
   pango_sources += [
     'pangodwrite-fontmap.cpp',
+    'pangowin32-utils.c',
   ]
 
   if cairo_dep.found()
diff --git a/pango2/pangodwrite-fontmap.cpp b/pango2/pangodwrite-fontmap.cpp
index 235740a41..9dd5b32fa 100644
--- a/pango2/pangodwrite-fontmap.cpp
+++ b/pango2/pangodwrite-fontmap.cpp
@@ -29,13 +29,12 @@
 #include <hb-directwrite.h>
 
 #include "pangodwrite-fontmap.h"
-#include "pango-hbfamily-private.h"
 #include "pango-fontmap-private.h"
 #include "pango-hbface-private.h"
 #include "pango-hbfont-private.h"
 #include "pango-context.h"
-#include "pango-impl-utils.h"
 #include "pango-trace-private.h"
+#include "pangowin32-utils-private.h"
 
 
 /**
@@ -270,8 +269,6 @@ util_create_pango2_hb_face (IDWriteFontFamily *family,
 /* }}} */
 /* {{{ Pango2FontMap implementation */
 
-#define MAX_ALIAS_LENTH 11
-#define MAX_FAMILY_LENTH 20
 static void
 pango2_direct_write_font_map_populate (Pango2FontMap *map)
 {
@@ -280,25 +277,6 @@ pango2_direct_write_font_map_populate (Pango2FontMap *map)
   UINT32 count;
   HRESULT hr;
 
-  /* list referred from Pango 1.x, from pangowin32-fontmap.c */
-  const char sans_families[][MAX_FAMILY_LENTH] =
-    {"dejavu", "sans", "arial", "tahoma",
-        "arial unicode ms", "lucida sans unicode",
-        "browallia new" };
-  const char serif_families[][MAX_FAMILY_LENTH] =
-    {"dejavu serif", "times new roman", "georgia", "angsana new"};
-  const char mono_families[][MAX_FAMILY_LENTH] =
-    {"dejavu sans mono", "consolas", "courier new", "lucida console", "courier monothai"};
-
-  /* TODO: Add when how to populate fallback fontmap is clearer */
-  const char fallback_families[][MAX_FAMILY_LENTH] =
-    {"simsun", "simhei", "mingliu",
-     "gulimche", "ms gothic", "sylfaen",
-     "kartika", "latha", "mangal", "raavi"};
-
-  const char sans_aliases [][MAX_ALIAS_LENTH] = {"Sans-serif", "Sans"};
-  const char mono_aliases [][MAX_ALIAS_LENTH] = {"Mono", "Monospace"};
-
   hr = dwrite_map->dwrite_factory->GetSystemFontCollection (&collection, FALSE);
   if (FAILED (hr) || collection == NULL)
     g_error ("IDWriteFactory::GetSystemFontCollection failed with error code %x\n", (unsigned)hr);
@@ -354,108 +332,8 @@ pango2_direct_write_font_map_populate (Pango2FontMap *map)
   collection->Release ();
   collection = NULL;
 
-  /* populate families for Sans and Sans-serif aliases */
-  for (gsize i = 0; i < G_N_ELEMENTS (sans_families); i++)
-    {
-      Pango2FontFamily *family = pango2_font_map_get_family (map, sans_families[i]);
-
-      if (family)
-        {
-          for (gsize j = 0; j < G_N_ELEMENTS (sans_aliases); j++)
-            {
-              Pango2GenericFamily *alias_family;
-
-              alias_family = pango2_generic_family_new (sans_aliases[j]);
-              pango2_generic_family_add_family (alias_family, family);
-              pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
-            }
-        }
-    }
-
-  /* populate families for Serif aliases */
-  for (gsize i = 0; i < G_N_ELEMENTS (serif_families); i++)
-    {
-      Pango2FontFamily *family = pango2_font_map_get_family (map, serif_families[i]);
-
-      if (family)
-        {
-          Pango2GenericFamily *alias_family;
-
-          alias_family = pango2_generic_family_new ("Serif");
-          pango2_generic_family_add_family (alias_family, family);
-          pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
-        }
-    }
-
-  /* populate families for Mono and Monospace aliases */
-  for (gsize i = 0; i < G_N_ELEMENTS (mono_families); i++)
-    {
-      Pango2FontFamily *family = pango2_font_map_get_family (map, mono_families[i]);
-
-      if (family)
-        {
-          for (gsize j = 0; j < G_N_ELEMENTS (mono_aliases); j++)
-            {
-              Pango2GenericFamily *alias_family;
-
-              alias_family = pango2_generic_family_new (mono_aliases[j]);
-              pango2_generic_family_add_family (alias_family, family);
-              pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
-            }
-        }
-    }
-
-  /* TODO: populate fallback fontmap */
-#if 0
-  for (gsize i = 0; i < G_N_ELEMENTS (fallback_families); i++)
-    {
-      Pango2FontFamily *family = pango2_font_map_get_family (map->fallback, fallback_families[i]);
-
-      if (family)
-        {
-          Pango2GenericFamily *alias_family;
-
-          alias_family = pango2_generic_family_new (fallback_families[i]);
-          pango2_generic_family_add_family (alias_family, family);
-          pango2_font_map_add_family (map->fallback, PANGO2_FONT_FAMILY (alias_family));
-        }
-    }
-#endif
-
-  /* Add other generic aliases */
-  struct {
-    const char *alias_name;
-    const char *family_name;
-  } aliases[] = {
-    { "System-ui",  "Yu Gothic UI" },
-    { "System-ui",  "Segoe UI" },
-    { "System-ui",  "Meiryo" },
-    { "Emoji",      "Segoe UI Emoji" },
-    { "Emoji",      "Segoe UI Symbol" },
-    { "Emoji",      "Segoe UI" },
-    { "Cursive",    "Comic Sans MS" },
-    { "Fantasy",    "Gabriola" },
-    { "Fantasy",    "Impact" },
-  };
-
-#if 0
-  if (IsWindows11OrLater ())
-    aliases[0].family_name = "Cascadia Mono";
-#endif
-
-  for (gsize i = 0; i < G_N_ELEMENTS (aliases); i++)
-    {
-      Pango2FontFamily *family = pango2_font_map_get_family (map, aliases[i].family_name);
-
-      if (family)
-        {
-          Pango2GenericFamily *alias_family;
-
-          alias_family = pango2_generic_family_new (aliases[i].alias_name);
-          pango2_generic_family_add_family (alias_family, family);
-          pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
-        }
-    }
+  /* Add the aliases and fallbacks */
+  pango2_win32_font_map_add_aliases_and_fallbacks (map);
 }
 
 /* }}} */
diff --git a/pango2/pangowin32-utils-private.h b/pango2/pangowin32-utils-private.h
new file mode 100644
index 000000000..9acebec9f
--- /dev/null
+++ b/pango2/pangowin32-utils-private.h
@@ -0,0 +1,29 @@
+/* Pango
+ * pangowin32-utils-private.h: Miscellaneous Windows-related utils
+ *
+ * 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 "pango-fontmap-private.h"
+
+G_BEGIN_DECLS
+
+void
+pango2_win32_font_map_add_aliases_and_fallbacks (Pango2FontMap *map);
+
+G_END_DECLS
diff --git a/pango2/pangowin32-utils.c b/pango2/pangowin32-utils.c
new file mode 100644
index 000000000..52ad858fb
--- /dev/null
+++ b/pango2/pangowin32-utils.c
@@ -0,0 +1,155 @@
+/* Pango
+ * pangowin32-utils.c: Miscellaneous Windows-related utils
+ *
+ * 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 "pango-impl-utils.h"
+#include "pangowin32-utils-private.h"
+
+#define MAX_ALIAS_LENGTH 11
+#define MAX_FAMILY_LENGTH 20
+
+void
+pango2_win32_font_map_add_aliases_and_fallbacks (Pango2FontMap *map)
+{
+  /* list referred from Pango 1.x, from pangowin32-fontmap.c */
+  const char sans_families[][MAX_FAMILY_LENGTH] =
+    {"dejavu", "sans", "arial", "tahoma",
+        "arial unicode ms", "lucida sans unicode",
+        "browallia new" };
+  const char serif_families[][MAX_FAMILY_LENGTH] =
+    {"dejavu serif", "times new roman", "georgia", "angsana new"};
+  const char mono_families[][MAX_FAMILY_LENGTH] =
+    {"dejavu sans mono", "consolas", "courier new", "lucida console", "courier monothai"};
+
+  /* TODO: Add when how to populate fallback fontmap is clearer */
+  const char fallback_families[][MAX_FAMILY_LENGTH] =
+    {"simsun", "simhei", "mingliu",
+     "gulimche", "ms gothic", "sylfaen",
+     "kartika", "latha", "mangal", "raavi"};
+
+  const char sans_aliases [][MAX_ALIAS_LENGTH] = {"Sans-serif", "Sans"};
+  const char mono_aliases [][MAX_ALIAS_LENGTH] = {"Mono", "Monospace"};
+
+  /* populate families for Sans and Sans-serif aliases */
+  for (gsize i = 0; i < G_N_ELEMENTS (sans_families); i++)
+    {
+      Pango2FontFamily *family = pango2_font_map_get_family (map, sans_families[i]);
+
+      if (family)
+        {
+          for (gsize j = 0; j < G_N_ELEMENTS (sans_aliases); j++)
+            {
+              Pango2GenericFamily *alias_family;
+
+              alias_family = pango2_generic_family_new (sans_aliases[j]);
+              pango2_generic_family_add_family (alias_family, family);
+              pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
+            }
+        }
+    }
+
+  /* populate families for Serif aliases */
+  for (gsize i = 0; i < G_N_ELEMENTS (serif_families); i++)
+    {
+      Pango2FontFamily *family = pango2_font_map_get_family (map, serif_families[i]);
+
+      if (family)
+        {
+          Pango2GenericFamily *alias_family;
+
+          alias_family = pango2_generic_family_new ("Serif");
+          pango2_generic_family_add_family (alias_family, family);
+          pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
+        }
+    }
+
+  /* populate families for Mono and Monospace aliases */
+  for (gsize i = 0; i < G_N_ELEMENTS (mono_families); i++)
+    {
+      Pango2FontFamily *family = pango2_font_map_get_family (map, mono_families[i]);
+
+      if (family)
+        {
+          for (gsize j = 0; j < G_N_ELEMENTS (mono_aliases); j++)
+            {
+              Pango2GenericFamily *alias_family;
+
+              alias_family = pango2_generic_family_new (mono_aliases[j]);
+              pango2_generic_family_add_family (alias_family, family);
+              pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
+            }
+        }
+    }
+
+  /* TODO: populate fallback fontmap */
+#if 0
+  for (gsize i = 0; i < G_N_ELEMENTS (fallback_families); i++)
+    {
+      Pango2FontFamily *family = pango2_font_map_get_family (map->fallback, fallback_families[i]);
+
+      if (family)
+        {
+          Pango2GenericFamily *alias_family;
+
+          alias_family = pango2_generic_family_new (fallback_families[i]);
+          pango2_generic_family_add_family (alias_family, family);
+          pango2_font_map_add_family (map->fallback, PANGO2_FONT_FAMILY (alias_family));
+        }
+    }
+#endif
+
+  /* Add other generic aliases */
+  struct {
+    const char *alias_name;
+    const char *family_name;
+  } aliases[] = {
+    { "System-ui",  "Yu Gothic UI" },
+    { "System-ui",  "Segoe UI" },
+    { "System-ui",  "Meiryo" },
+    { "Emoji",      "Segoe UI Emoji" },
+    { "Emoji",      "Segoe UI Symbol" },
+    { "Emoji",      "Segoe UI" },
+    { "Cursive",    "Comic Sans MS" },
+    { "Fantasy",    "Gabriola" },
+    { "Fantasy",    "Impact" },
+  };
+
+#if 0
+  if (IsWindows11OrLater ())
+    aliases[0].family_name = "Cascadia Mono";
+#endif
+
+  for (gsize i = 0; i < G_N_ELEMENTS (aliases); i++)
+    {
+      Pango2FontFamily *family = pango2_font_map_get_family (map, aliases[i].family_name);
+
+      if (family)
+        {
+          Pango2GenericFamily *alias_family;
+
+          alias_family = pango2_generic_family_new (aliases[i].alias_name);
+          pango2_generic_family_add_family (alias_family, family);
+          pango2_font_map_add_family (map, PANGO2_FONT_FAMILY (alias_family));
+        }
+    }
+}
+
+#undef MAX_ALIAS_LENGTH
+#undef MAX_FAMILY_LENGTH


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