[gtk/wip/fontchooser-harfbuzz: 50/52] Port the font chooser to just use harfbuzz



commit a0d45a934e10b2efa86f42c3485cdbe45331ca10
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jul 24 08:02:07 2019 -0400

    Port the font chooser to just use harfbuzz

 gtk/gtkfontchooserwidget.c | 166 +++++++++++++++++++++------------------------
 gtk/language-names.c       |   4 ++
 2 files changed, 82 insertions(+), 88 deletions(-)
---
diff --git a/gtk/gtkfontchooserwidget.c b/gtk/gtkfontchooserwidget.c
index 8c93be4e0c..fddd95fe9d 100644
--- a/gtk/gtkfontchooserwidget.c
+++ b/gtk/gtkfontchooserwidget.c
@@ -57,18 +57,10 @@
 #include "gtkeventcontrollerscroll.h"
 #include "gtkroot.h"
 
-#if defined(HAVE_HARFBUZZ) && defined(HAVE_PANGOFT)
-#include <pango/pangofc-font.h>
-#include <hb.h>
 #include <hb-ot.h>
-#include <hb-ft.h>
-#include <ft2build.h>
-#include FT_FREETYPE_H
-#include FT_MULTIPLE_MASTERS_H
+
 #include "language-names.h"
 #include "script-names.h"
-#endif
-
 #include "open-type-layout.h"
 
 /**
@@ -954,10 +946,7 @@ gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
 
   /* Load data and set initial style-dependent parameters */
   gtk_font_chooser_widget_load_fonts (fontchooser, TRUE);
-
-#if defined(HAVE_HARFBUZZ) && defined(HAVE_PANGOFT)
   gtk_font_chooser_widget_populate_features (fontchooser);
-#endif
 
   gtk_font_chooser_widget_set_cell_size (fontchooser);
   gtk_font_chooser_widget_take_font_desc (fontchooser, NULL);
@@ -1508,12 +1497,8 @@ gtk_font_chooser_widget_ensure_selection (GtkFontChooserWidget *fontchooser)
     }
 }
 
-#if defined(HAVE_HARFBUZZ) && defined(HAVE_PANGOFT)
-
 /* OpenType variations */
 
-#define FixedToFloat(f) (((float)(f))/65536.0)
-
 static void
 add_font_variations (GtkFontChooserWidget *fontchooser,
                      GString              *s)
@@ -1568,49 +1553,56 @@ adjustment_changed (GtkAdjustment *adjustment,
 }
 
 static gboolean
-should_show_axis (FT_Var_Axis *ax)
+should_show_axis (hb_ot_var_axis_info_t *ax)
 {
-  /* FIXME use FT_Get_Var_Axis_Flags */
-  if (ax->tag == FT_MAKE_TAG ('o', 'p', 's', 'z'))
+  if (ax->flags & HB_OT_VAR_AXIS_FLAG_HIDDEN)
     return FALSE;
 
   return TRUE;
 }
 
 static gboolean
-is_named_instance (FT_Face face)
+is_named_instance (hb_font_t *font)
 {
-  return (face->face_index >> 16) > 0;
+  /* FIXME */
+  return FALSE;
 }
 
 static struct {
   guint32 tag;
   const char *name;
 } axis_names[] = {
-  { FT_MAKE_TAG ('w', 'd', 't', 'h'), N_("Width") },
-  { FT_MAKE_TAG ('w', 'g', 'h', 't'), N_("Weight") },
-  { FT_MAKE_TAG ('i', 't', 'a', 'l'), N_("Italic") },
-  { FT_MAKE_TAG ('s', 'l', 'n', 't'), N_("Slant") },
-  { FT_MAKE_TAG ('o', 'p', 's', 'z'), N_("Optical Size") },
+  { HB_OT_TAG_VAR_AXIS_WIDTH,        N_("Width") },
+  { HB_OT_TAG_VAR_AXIS_WEIGHT,       N_("Weight") },
+  { HB_OT_TAG_VAR_AXIS_ITALIC,       N_("Italic") },
+  { HB_OT_TAG_VAR_AXIS_SLANT,        N_("Slant") },
+  { HB_OT_TAG_VAR_AXIS_OPTICAL_SIZE, N_("Optical Size") },
 };
 
 static gboolean
-add_axis (GtkFontChooserWidget *fontchooser,
-          FT_Face               face,
-          FT_Var_Axis          *ax,
-          FT_Fixed              value,
-          int                   row)
+add_axis (GtkFontChooserWidget  *fontchooser,
+          hb_font_t             *hb_font,
+          hb_ot_var_axis_info_t *ax,
+          int                    value,
+          int                    row)
 {
   GtkFontChooserWidgetPrivate *priv = gtk_font_chooser_widget_get_instance_private (fontchooser);
+  hb_face_t *hb_face;
   Axis *axis;
   const char *name;
+  char buffer[20];
+  unsigned int buffer_len = 20;
   int i;
 
+  hb_face = hb_font_get_face (hb_font);
+
   axis = g_new (Axis, 1);
   axis->tag = ax->tag;
   axis->fontchooser = GTK_WIDGET (fontchooser);
 
-  name = ax->name;
+  hb_ot_name_get_utf8 (hb_face, ax->name_id, HB_LANGUAGE_INVALID, &buffer_len, buffer);
+  name = buffer;
+
   for (i = 0; i < G_N_ELEMENTS (axis_names); i++)
     {
       if (axis_names[i].tag == ax->tag)
@@ -1619,16 +1611,18 @@ add_axis (GtkFontChooserWidget *fontchooser,
           break;
         }
     }
+
   axis->label = gtk_label_new (name);
+
   gtk_widget_set_halign (axis->label, GTK_ALIGN_START);
   gtk_widget_set_valign (axis->label, GTK_ALIGN_BASELINE);
   gtk_grid_attach (GTK_GRID (priv->axis_grid), axis->label, 0, row, 1, 1);
-  axis->adjustment = gtk_adjustment_new ((double)FixedToFloat(value),
-                                         (double)FixedToFloat(ax->minimum),
-                                         (double)FixedToFloat(ax->maximum),
+  axis->adjustment = gtk_adjustment_new ((double)value,
+                                         (double)ax->min_value,
+                                         (double)ax->max_value,
                                          1.0, 10.0, 0.0);
   axis->scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, axis->adjustment);
-  gtk_scale_add_mark (GTK_SCALE (axis->scale), (double)FixedToFloat(ax->def), GTK_POS_TOP, NULL);
+  gtk_scale_add_mark (GTK_SCALE (axis->scale), (double)ax->default_value, GTK_POS_TOP, NULL);
   gtk_widget_set_valign (axis->scale, GTK_ALIGN_BASELINE);
   gtk_widget_set_hexpand (axis->scale, TRUE);
   gtk_widget_set_size_request (axis->scale, 100, -1);
@@ -1643,7 +1637,7 @@ add_axis (GtkFontChooserWidget *fontchooser,
 
   adjustment_changed (axis->adjustment, axis);
   g_signal_connect (axis->adjustment, "value-changed", G_CALLBACK (adjustment_changed), axis);
-  if (is_named_instance (face) || !should_show_axis (ax))
+  if (is_named_instance (hb_font) || !should_show_axis (ax))
     {
       gtk_widget_hide (axis->label);
       gtk_widget_hide (axis->scale);
@@ -1655,15 +1649,31 @@ add_axis (GtkFontChooserWidget *fontchooser,
   return TRUE;
 }
 
+/* FIXME: This doesn't work if the font has an avar table */
+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);
+  else
+    return axis->default_value + r * (axis->max_value - axis->default_value);
+}
+
 static gboolean
 gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchooser)
 {
   GtkFontChooserWidgetPrivate *priv = gtk_font_chooser_widget_get_instance_private (fontchooser);
   PangoFont *pango_font;
-  FT_Face ft_face;
-  FT_MM_Var *ft_mm_var;
-  FT_Error ret;
+  hb_font_t *hb_font;
+  hb_face_t *hb_face;
+  const int *coords;
+  unsigned int n_coords;
+  unsigned int n_axes;
+  hb_ot_var_axis_info_t *axes;
   gboolean has_axis = FALSE;
+  int i;
 
   if (priv->updating_variations)
     return FALSE;
@@ -1676,39 +1686,30 @@ gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchoose
 
   pango_font = pango_context_load_font (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
                                         priv->font_desc);
-  ft_face = pango_fc_font_lock_face (PANGO_FC_FONT (pango_font));
+  hb_font = pango_font_get_hb_font (pango_font);
+  hb_face = hb_font_get_face (hb_font);
 
-  ret = FT_Get_MM_Var (ft_face, &ft_mm_var);
-  if (ret == 0)
-    {
-      int i;
-      FT_Fixed *coords;
-
-      coords = g_new (FT_Fixed, ft_mm_var->num_axis);
-      for (i = 0; i < ft_mm_var->num_axis; i++)
-        coords[i] = ft_mm_var->axis[i].def;
+  if (!hb_ot_var_has_data (hb_face))
+    return FALSE;
 
-      if (ft_face->face_index > 0)
-        {
-          int instance_id = ft_face->face_index >> 16;
-          if (instance_id && instance_id <= ft_mm_var->num_namedstyles)
-            {
-              FT_Var_Named_Style *instance = &ft_mm_var->namedstyle[instance_id - 1];
-              memcpy (coords, instance->coords, ft_mm_var->num_axis * sizeof (*coords));
-            }
-        }
+  coords = hb_font_get_var_coords_normalized (hb_font, &n_coords);
 
-      for (i = 0; i < ft_mm_var->num_axis; i++)
-        {
-          if (add_axis (fontchooser, ft_face, &ft_mm_var->axis[i], coords[i], i + 4))
-            has_axis = TRUE;
-        }
+  n_axes = hb_ot_var_get_axis_count (hb_face);
+  axes = g_new0 (hb_ot_var_axis_info_t, n_axes);
+  hb_ot_var_get_axis_infos (hb_face, 0, &n_axes, axes);
 
-      g_free (coords);
-      free (ft_mm_var);
+  for (i = 0; i < n_axes; i++)
+    {
+      float value;
+      if (coords && i < n_coords)
+        value = denorm_coord (&axes[i], coords[i]);
+      else
+        value = axes[i].default_value;
+      if (add_axis (fontchooser, hb_font, &axes[i], value, i + 4))
+        has_axis = TRUE;
     }
 
-  pango_fc_font_unlock_face (PANGO_FC_FONT (pango_font));
+  g_free (axes);
   g_object_unref (pango_font);
 
   return has_axis;
@@ -1835,16 +1836,19 @@ feat_pressed (GtkGesture *gesture,
 
 static char *
 find_affected_text (hb_tag_t   feature_tag,
-                    hb_face_t *hb_face,
+                    hb_font_t *hb_font,
                     hb_tag_t   script_tag,
                     hb_tag_t   lang_tag,
                     int        max_chars)
 {
+  hb_face_t *hb_face;
   unsigned int script_index = 0;
   unsigned int lang_index = 0;
   unsigned int feature_index = 0;
   GString *chars;
 
+  hb_face = hb_font_get_face (hb_font);
+
   chars = g_string_new ("");
 
   hb_ot_layout_table_find_script (hb_face, HB_OT_TAG_GSUB, script_tag, &script_index);
@@ -1872,7 +1876,6 @@ find_affected_text (hb_tag_t   feature_tag,
           hb_set_t* glyphs_input  = NULL;
           hb_set_t* glyphs_after  = NULL;
           hb_set_t* glyphs_output = NULL;
-          hb_font_t *hb_font = NULL;
           hb_codepoint_t gid;
 
           glyphs_input  = hb_set_create ();
@@ -1886,9 +1889,6 @@ find_affected_text (hb_tag_t   feature_tag,
                                               glyphs_after,
                                               glyphs_output);
 
-          hb_font = hb_font_create (hb_face);
-          hb_ft_font_set_funcs (hb_font);
-
           gid = -1;
           while (hb_set_next (glyphs_input, &gid)) {
             hb_codepoint_t ch;
@@ -1908,7 +1908,6 @@ find_affected_text (hb_tag_t   feature_tag,
             }
           }
           hb_set_destroy (glyphs_input);
-          hb_font_destroy (hb_font);
         }
     }
 
@@ -1917,7 +1916,7 @@ find_affected_text (hb_tag_t   feature_tag,
 
 static void
 update_feature_example (FeatureItem          *item,
-                        hb_face_t            *hb_face,
+                        hb_font_t            *hb_font,
                         hb_tag_t              script_tag,
                         hb_tag_t              lang_tag,
                         PangoFontDescription *font_desc)
@@ -1971,9 +1970,9 @@ update_feature_example (FeatureItem          *item,
       else if (strcmp (item->name, "frac") == 0)
         input = g_strdup ("1/2 2/3 7/8");
       else if (strcmp (item->name, "nalt") == 0)
-        input = find_affected_text (item->tag, hb_face, script_tag, lang_tag, 3);
+        input = find_affected_text (item->tag, hb_font, script_tag, lang_tag, 3);
       else
-        input = find_affected_text (item->tag, hb_face, script_tag, lang_tag, 10);
+        input = find_affected_text (item->tag, hb_font, script_tag, lang_tag, 10);
 
       if (input[0] != '\0')
         {
@@ -2179,7 +2178,6 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
 {
   GtkFontChooserWidgetPrivate *priv = gtk_font_chooser_widget_get_instance_private (fontchooser);
   PangoFont *pango_font;
-  FT_Face ft_face;
   hb_font_t *hb_font;
   hb_tag_t script_tag;
   hb_tag_t lang_tag;
@@ -2201,8 +2199,7 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
 
   pango_font = pango_context_load_font (gtk_widget_get_pango_context (GTK_WIDGET (fontchooser)),
                                         priv->font_desc);
-  ft_face = pango_fc_font_lock_face (PANGO_FC_FONT (pango_font)),
-  hb_font = hb_ft_font_create (ft_face, NULL);
+  hb_font = pango_font_get_hb_font (pango_font);
 
   if (hb_font)
     {
@@ -2248,7 +2245,7 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
               gtk_widget_show (item->top);
               gtk_widget_show (gtk_widget_get_parent (item->top));
 
-              update_feature_example (item, hb_face, script_tag, lang_tag, priv->font_desc);
+              update_feature_example (item, hb_font, script_tag, lang_tag, priv->font_desc);
 
               if (GTK_IS_RADIO_BUTTON (item->feat))
                 {
@@ -2261,11 +2258,8 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
                 }
             }
         }
-
-      hb_font_destroy (hb_font);
     }
 
-  pango_fc_font_unlock_face (PANGO_FC_FONT (pango_font));
   g_object_unref (pango_font);
 
   return has_feature;
@@ -2318,8 +2312,6 @@ update_font_features (GtkFontChooserWidget *fontchooser)
   gtk_font_chooser_widget_update_preview_attributes (fontchooser);
 }
 
-#endif
-
 static void
 gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget       *fontchooser,
                                          const PangoFontDescription *font_desc,
@@ -2365,12 +2357,11 @@ gtk_font_chooser_widget_merge_font_desc (GtkFontChooserWidget       *fontchooser
 
       gtk_font_chooser_widget_update_marks (fontchooser);
 
-#if defined(HAVE_HARFBUZZ) && defined(HAVE_PANGOFT)
       if (gtk_font_chooser_widget_update_font_features (fontchooser))
         has_tweak = TRUE;
       if (gtk_font_chooser_widget_update_font_variations (fontchooser))
         has_tweak = TRUE;
-#endif
+
       g_simple_action_set_enabled (G_SIMPLE_ACTION (priv->tweak_action), has_tweak);
     }
 
@@ -2587,4 +2578,3 @@ gtk_font_chooser_widget_get_tweak_action (GtkWidget *widget)
 
   return priv->tweak_action;
 }
-
diff --git a/gtk/language-names.c b/gtk/language-names.c
index 6fa6eaa2f2..dd9ec7e5fb 100644
--- a/gtk/language-names.c
+++ b/gtk/language-names.c
@@ -19,6 +19,10 @@
 
 #include "language-names.h"
 
+#ifndef ISO_CODES_PREFIX
+#define ISO_CODES_PREFIX "/usr"
+#endif
+
 #define ISO_CODES_DATADIR ISO_CODES_PREFIX "/share/xml/iso-codes"
 #define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
 


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