[pango/simple-fontmap: 19/22] pango-list: Show variable faces




commit d5b6b8658c9c20269abf45ea0a51a7f65bfdec4b
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jan 1 22:38:30 2022 -0500

    pango-list: Show variable faces
    
    Add an '@' to the face name for variable faces.
    And various other improvements.

 utils/meson.build  |   3 +-
 utils/pango-list.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 125 insertions(+), 19 deletions(-)
---
diff --git a/utils/meson.build b/utils/meson.build
index 6d06dbec..c76d35be 100644
--- a/utils/meson.build
+++ b/utils/meson.build
@@ -67,7 +67,8 @@ if cairo_dep.found()
   pango_list_deps = [
     pango_deps,
     libpango_dep,
-    libpangocairo_dep
+    libpangocairo_dep,
+    libpangoft2_dep
   ]
 
   pango_list = executable('pango-list', pango_list_sources,
diff --git a/utils/pango-list.c b/utils/pango-list.c
index 82a5647c..ed33e5ba 100644
--- a/utils/pango-list.c
+++ b/utils/pango-list.c
@@ -22,6 +22,9 @@
 
 #include "config.h"
 #include <pango/pangocairo.h>
+#include <pango/pangofc-hbfontmap.h>
+#include <pango/pangofc-font.h>
+#include <pango/pango-hbface-private.h>
 #include <hb-ot.h>
 #include <glib/gstdio.h>
 #include <stdlib.h>
@@ -31,12 +34,37 @@
 static float
 denorm_coord (hb_ot_var_axis_info_t *axis, int coord)
 {
-  float r = coord / 16384.0;
-
-  if (coord < 0)
-    return axis->default_value + r * (axis->default_value - axis->min_value);
+  if (coord == 0)
+    return axis->default_value;
+  else if (coord < 0)
+    return coord * (axis->default_value - axis->min_value) / 16384.f + axis->default_value;
   else
-    return axis->default_value + r * (axis->max_value - axis->default_value);
+    return coord * (axis->max_value - axis->default_value) / 16384.f + axis->default_value;
+}
+
+static char *
+get_name_from_hb_face (hb_face_t       *face,
+                       hb_ot_name_id_t  name_id,
+                       hb_ot_name_id_t  fallback_id)
+{
+  char buf[256];
+  unsigned int len;
+  hb_language_t language;
+
+  language = hb_language_from_string ("en", -1);
+
+  len = sizeof (buf);
+  if (hb_ot_name_get_utf8 (face, name_id, language, &len, buf) > 0)
+    return g_strdup (buf);
+
+  if (fallback_id != HB_OT_NAME_ID_INVALID)
+    {
+      len = sizeof (buf);
+      if (hb_ot_name_get_utf8 (face, fallback_id, language, &len, buf) > 0)
+        return g_strdup (buf);
+    }
+
+  return g_strdup ("Unnamed");
 }
 
 int
@@ -85,7 +113,6 @@ main (int    argc,
       exit (0);
     }
 
-  /* Use PangoCairo to get default fontmap so it works on every platform. */
   fontmap = pango_cairo_font_map_get_default ();
   ctx = pango_font_map_create_context (fontmap);
 
@@ -125,25 +152,46 @@ main (int    argc,
          const char *face_name = pango_font_face_get_face_name (faces[j]);
          gboolean is_synth = pango_font_face_is_synthesized (faces[j]);
          const char *synth_str = is_synth ? "*" : "";
-          width = MAX (width, strlen (synth_str) + strlen (face_name));
+          gboolean is_variable = pango_font_face_is_variable (faces[j]);
+         const char *variable_str = is_variable ? "@" : "";
+          width = MAX (width, strlen (synth_str) + strlen (variable_str) + strlen (face_name));
         }
 
       for (j = 0; j < n_faces; j++)
-       {
-         const char *face_name = pango_font_face_get_face_name (faces[j]);
-         gboolean is_synth = pango_font_face_is_synthesized (faces[j]);
-         const char *synth_str = is_synth ? "*" : "";
-         PangoFontDescription *desc = pango_font_face_describe (faces[j]);
-         char *desc_str = pango_font_description_to_string (desc);
+        {
+          const char *face_name = pango_font_face_get_face_name (faces[j]);
+          gboolean is_synth = pango_font_face_is_synthesized (faces[j]);
+          const char *synth_str = is_synth ? "*" : "";
+          gboolean is_variable = pango_font_face_is_variable (faces[j]);
+          const char *variable_str = is_variable ? "@" : "";
+          PangoFontDescription *desc;
+          char *desc_str;
+
+          desc = pango_font_face_describe (faces[j]);
+
+          if (pango_font_face_is_synthesized (faces[j]) && PANGO_IS_HB_FACE (faces[j]))
+            {
+              hb_face_t *hbface = pango_hb_face_get_hb_face (PANGO_HB_FACE (faces[j]));
+              char *family = get_name_from_hb_face (hbface,
+                                                    HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY,
+                                                    HB_OT_NAME_ID_FONT_FAMILY);
+
+              pango_font_description_set_family (desc, family);
+
+              g_free (family);
+            }
+
+          desc_str = pango_font_description_to_string (desc);
+
+          g_print ("  %s%s%s: %*s%s\n", synth_str, variable_str, face_name,
+                   width - (int)strlen (face_name) - (int)strlen (synth_str) - (int)strlen (variable_str), 
"", desc_str);
 
-         g_print ("  %s%s: %*s%s\n", synth_str, face_name,
-                   width - (int)strlen (face_name) - (int)strlen (synth_str), "", desc_str);
+          pango_font_description_set_absolute_size (desc, 10 * PANGO_SCALE);
 
           if (opt_metrics)
             {
               PangoFontMetrics *metrics;
 
-              pango_font_description_set_absolute_size (desc, 10 * PANGO_SCALE);
               metrics = pango_context_get_metrics (ctx, desc, pango_language_from_string ("en-us"));
               g_print ("    (a %d d %d h %d cw %d dw %d u %d %d s %d %d)\n",
                        pango_font_metrics_get_ascent (metrics),
@@ -165,12 +213,38 @@ main (int    argc,
               hb_font_t *hb_font;
               const int *coords;
               unsigned int length;
+              int instance_id = -1;
 
-              pango_font_description_set_absolute_size (desc, 10 * PANGO_SCALE);
+              /* set variations here, to make the fontmap prefer the variable family
+               * over a non-variable one.
+               */
+              pango_font_description_set_variations (desc, "@");
 
               font = pango_context_load_font (ctx, desc);
               hb_font = pango_font_get_hb_font (font);
+              if (PANGO_IS_HB_FONT (font))
+                {
+                  PangoHbFace *hbface = (PangoHbFace *)faces[j];
+                  instance_id = hbface->instance_id;
+                }
+              else if (PANGO_IS_FC_FONT (font))
+                {
+                  int index;
+                  FcPattern *pattern = pango_fc_font_get_pattern (PANGO_FC_FONT (font));
+                  FcPatternGetInteger (pattern, FC_INDEX, 0, (int *)&index);
+                  instance_id = (index >> 16) - 1;
+                }
+
+              if (pango_font_face_is_variable (faces[j]))
+                {
+                  if (instance_id >= 0)
+                    g_print ("    Instance %d\n", instance_id);
+                  else if (instance_id == -1)
+                    g_print ("    Default Instance\n");
+                }
+
               coords = hb_font_get_var_coords_normalized (hb_font, &length);
+
               if (coords)
                 {
                   hb_face_t *hb_face = hb_font_get_face (hb_font);
@@ -192,13 +266,44 @@ main (int    argc,
 
                       g_print ("    %s: %g (%g - %g, %g)\n",
                                name,
-                               denorm_coord (&axes[i], coords[i]),
+                               denorm_coord (&axes[i], coords[axes[i].axis_index]),
                                axes[i].min_value,
                                axes[i].max_value,
                                axes[i].default_value);
                     }
                   g_free (axes);
                 }
+
+              g_object_unref (font);
+            }
+
+          if (opt_verbose)
+            {
+              if (PANGO_IS_HB_FACE (faces[j]))
+                {
+                  PangoHbFace *hbface = (PangoHbFace *)faces[j];
+
+                  if (hbface->file)
+                    g_print ("    %d %s\n", hbface->index, hbface->file);
+                }
+              else
+                {
+                  PangoFont *font;
+
+                  font = pango_context_load_font (ctx, desc);
+                  if (PANGO_IS_FC_FONT (font))
+                    {
+                      FcPattern *pattern;
+                      const char *file;
+                      int index;
+
+                      pattern = pango_fc_font_get_pattern (PANGO_FC_FONT (font));
+                      FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **)&file);
+                      FcPatternGetInteger (pattern, FC_INDEX, 0, &index);
+                      g_print ("    %d %s\n", index, file);
+                    }
+                  g_object_unref (font);
+                }
             }
 
          g_free (desc_str);


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