[pango/simple-fontmap: 6/10] pango-view: Add --fontmap and --font-file options




commit 5510faeb1b7264c38112f4558ba99d4163176f5d
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Aug 31 07:47:17 2021 -0400

    pango-view: Add --fontmap and --font-file options
    
    The --fontmap option lets us test the new fontmap
    implementations.
    
    The --font-file option can be used multiple times,
    to create a simple fontmap that has exactly the given
    font files, and nothing else.
    
    If the options aren't used, behave as before
    and use a default fontmap.

 utils/meson.build         |  3 +-
 utils/pango-list.c        | 79 +++++++++++++++++++++++++++++++++++++++------
 utils/viewer-pangocairo.c | 81 +++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 151 insertions(+), 12 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..2c5de3e0 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-fontmap2.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,12 @@
 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;
 }
 
 int
@@ -47,11 +50,13 @@ main (int    argc,
   gboolean opt_metrics = FALSE;
   gboolean opt_variations = FALSE;
   gboolean opt_version = FALSE;
+  const char *opt_fontmap = NULL;
   GOptionEntry entries[] = {
     { "verbose", 0, 0, G_OPTION_ARG_NONE, &opt_verbose, "Print verbose information", NULL },
     { "metrics", 0, 0, G_OPTION_ARG_NONE, &opt_metrics, "Print font metrics", NULL },
     { "variations", 0, 0, G_OPTION_ARG_NONE, &opt_variations, "Print font variations", NULL },
     { "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Show version" },
+    { "fontmap", 0, 0, G_OPTION_ARG_STRING, &opt_fontmap, "Select fontmap, fc2 or default", "NAME" },
     { NULL, }
   };
   GOptionContext *context;
@@ -86,7 +91,10 @@ main (int    argc,
     }
 
   /* Use PangoCairo to get default fontmap so it works on every platform. */
-  fontmap = pango_cairo_font_map_get_default ();
+  if (g_strcmp0 (opt_fontmap, "fc2") == 0)
+    fontmap = PANGO_FONT_MAP (pango_fc_font_map2_new ());
+  else
+    fontmap = pango_cairo_font_map_get_default ();
   ctx = pango_font_map_create_context (fontmap);
 
   if (opt_verbose)
@@ -139,11 +147,12 @@ main (int    argc,
          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 +174,33 @@ 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 (instance_id != -1)
+                g_print ("    Instance %d\n", instance_id);
+
               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 +222,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);
diff --git a/utils/viewer-pangocairo.c b/utils/viewer-pangocairo.c
index 8fe9db59..e03164c4 100644
--- a/utils/viewer-pangocairo.c
+++ b/utils/viewer-pangocairo.c
@@ -25,8 +25,11 @@
 #include "viewer-cairo.h"
 
 #include <pango/pangocairo.h>
+#include <pango/pangofc-fontmap2.h>
 
 static int opt_annotate = 0;
+static const char **opt_font_file = NULL;
+static const char *opt_fontmap = NULL;
 
 typedef struct
 {
@@ -48,8 +51,80 @@ pangocairo_view_create (const PangoViewer *klass G_GNUC_UNUSED)
 
   instance->backend = cairo_viewer_iface_create (&instance->iface);
 
-  instance->fontmap = pango_cairo_font_map_new ();
-  pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (instance->fontmap), opt_dpi);
+  if (g_strcmp0 (opt_fontmap, "fc2") == 0)
+    {
+      instance->fontmap = PANGO_FONT_MAP (pango_fc_font_map2_new ());
+      pango_hb_font_map_set_resolution (PANGO_HB_FONT_MAP (instance->fontmap), opt_dpi);
+    }
+  else if (g_strcmp0 (opt_fontmap, "hb") == 0)
+    {
+      instance->fontmap = PANGO_FONT_MAP (pango_hb_font_map_new ());
+      pango_hb_font_map_set_resolution (PANGO_HB_FONT_MAP (instance->fontmap), opt_dpi);
+    }
+  else
+    {
+      instance->fontmap = pango_cairo_font_map_new ();
+      pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (instance->fontmap), opt_dpi);
+    }
+
+  if (opt_font_file != NULL)
+    {
+      PangoFontFamily *fam;
+
+      for (int i = 0; opt_font_file[i]; i++)
+        {
+          if (PANGO_IS_HB_FONT_MAP (instance->fontmap))
+            pango_hb_font_map_add_file (PANGO_HB_FONT_MAP (instance->fontmap), opt_font_file[i]);
+        }
+
+      if (g_strcmp0 (opt_fontmap, "hb") == 0)
+        {
+          fam = pango_font_map_get_family (instance->fontmap, "Cantarell");
+          if (fam)
+            {
+              PangoFontFace *face = pango_font_family_get_face (fam, "Regular");
+              PangoHbFace *slanted;
+              PangoMatrix matrix;
+              PangoFontDescription *desc;
+
+              matrix = (PangoMatrix) { 1., -0.2, 0., 1., 0., 0. };
+              desc = pango_font_description_new ();
+              pango_font_description_set_style (desc, PANGO_STYLE_ITALIC);
+              slanted = pango_hb_face_new_variant (PANGO_HB_FACE (face), &matrix, FALSE, desc);
+              pango_hb_font_map_add_face (PANGO_HB_FONT_MAP (instance->fontmap), slanted);
+              g_object_unref (slanted);
+              pango_font_description_free (desc);
+
+              matrix = (PangoMatrix) { 0.5, 0., 0., 1., 0., 0. };
+              desc = pango_font_description_new ();
+              pango_font_description_set_stretch (desc, PANGO_STRETCH_CONDENSED);
+              slanted = pango_hb_face_new_variant (PANGO_HB_FACE (face), &matrix, FALSE, desc);
+              pango_hb_font_map_add_face (PANGO_HB_FONT_MAP (instance->fontmap), slanted);
+              g_object_unref (slanted);
+              pango_font_description_free (desc);
+            }
+
+          fam = pango_font_map_get_family (instance->fontmap, "Liberation Mono");
+          if (fam)
+            {
+              PangoFontFace *face = pango_font_family_get_face (fam, "Regular");
+              PangoHbFace *alias;
+              PangoFontDescription *desc;
+
+              desc = pango_font_description_new ();
+              pango_font_description_set_family (desc, "monospace");
+              alias = pango_hb_face_new_variant (PANGO_HB_FACE (face), NULL, FALSE, desc);
+              pango_hb_font_map_add_face (PANGO_HB_FONT_MAP (instance->fontmap), alias);
+              g_object_unref (alias);
+              pango_font_description_free (desc);
+            }
+        }
+    }
+  else
+    {
+      instance->fontmap = pango_cairo_font_map_new ();
+      pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (instance->fontmap), opt_dpi);
+    }
 
   instance->font_options = cairo_font_options_create ();
   if (opt_hinting != HINT_DEFAULT)
@@ -794,6 +869,8 @@ pangocairo_view_get_option_group (const PangoViewer *klass G_GNUC_UNUSED)
   GOptionEntry entries[] =
   {
     {"annotate", 0, 0, G_OPTION_ARG_CALLBACK, parse_annotate_arg, annotate_arg_help, "FLAGS"},
+    {"font-file", 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &opt_font_file, "Font file to use", "FILE"},
+    {"fontmap", 0, 0, G_OPTION_ARG_STRING, &opt_fontmap, "Fontmap to use, default, hb or fc2 ", "NAME"},
     {NULL}
   };
   GOptionGroup *group;


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