[gtk+/wip/matthiasc/font-variations: 259/260] Fixes to the font variations demo



commit d97e7ab707d9ea80c1dec855083742235e8b2bf6
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Dec 18 18:59:03 2017 -0500

    Fixes to the font variations demo
    
    Make things work with the current pango,
    cairo and freetype branches.

 demos/gtk-demo/font_features.c |   27 +++++++++++++++++++-
 gtk/gtkfontchooserwidget.c     |   51 +++++++++++++++++++++++++++++-----------
 2 files changed, 62 insertions(+), 16 deletions(-)
---
diff --git a/demos/gtk-demo/font_features.c b/demos/gtk-demo/font_features.c
index e3768e4..041d622 100644
--- a/demos/gtk-demo/font_features.c
+++ b/demos/gtk-demo/font_features.c
@@ -1326,6 +1326,20 @@ out:
   g_free (text);
 }
 
+static gboolean
+matches_instance (FT_Var_Named_Style *instance,
+                  FT_Fixed           *coords,
+                  FT_UInt             num_coords)
+{
+  FT_UInt i;
+
+  for (i = 0; i < num_coords; i++)
+    if (coords[i] != instance->coords[i])
+      return FALSE;
+
+  return TRUE;
+}
+
 static void
 update_font_variations (void)
 {
@@ -1357,6 +1371,9 @@ update_font_variations (void)
       unsigned int i;
       FT_Fixed *coords;
 
+      coords = g_new (FT_Fixed, ft_mm_var->num_axis);
+      ret = FT_Get_Var_Design_Coordinates (ft_face, ft_mm_var->num_axis, coords);
+
       if (ft_mm_var->num_namedstyles > 0)
         {
            GtkWidget *label;
@@ -1377,12 +1394,18 @@ update_font_variations (void)
 
            for (i = 0; i < ft_mm_var->num_namedstyles; i++)
              add_instance (ft_face, ft_mm_var, &ft_mm_var->namedstyle[i], combo, i);
+           for (i = 0; i < ft_mm_var->num_namedstyles; i++)
+             {
+               if (matches_instance (&ft_mm_var->namedstyle[i], coords, ft_mm_var->num_axis))
+                 {
+                   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), i + 1);
+                   break;
+                 }
+             }
 
            instance_combo = combo;
         }
 
-      coords = g_new (FT_Fixed, ft_mm_var->num_axis);
-      ret = FT_Get_Var_Design_Coordinates (ft_face, ft_mm_var->num_axis, coords);
       if (ret == 0)
         {
           for (i = 0; i < ft_mm_var->num_axis; i++)
diff --git a/gtk/gtkfontchooserwidget.c b/gtk/gtkfontchooserwidget.c
index 86e108f..ad6773d 100644
--- a/gtk/gtkfontchooserwidget.c
+++ b/gtk/gtkfontchooserwidget.c
@@ -543,6 +543,8 @@ cursor_changed_cb (GtkTreeView *treeview,
                       FONT_DESC_COLUMN, &desc,
                       -1);
 
+  // reset variations explicitly
+  pango_font_description_set_variations (priv->font_desc, NULL);
   gtk_font_chooser_widget_merge_font_desc (fontchooser,
                                            gtk_delayed_font_description_get (desc),
                                            &iter);
@@ -1045,7 +1047,7 @@ gtk_font_chooser_widget_cell_data_func (GtkTreeViewColumn *column,
   text = g_strconcat (preview_title, "\n", fontchooser->priv->preview_text, NULL);
   first_line_len = strlen (preview_title) + 1;
 
-  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, 
+  attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser,
                                                           gtk_delayed_font_description_get (desc),
                                                           first_line_len);
 
@@ -1348,10 +1350,12 @@ adjustment_changed (GtkAdjustment *adjustment,
   s = g_string_new ("");
   add_font_variations (fontchooser, s);
 
-  font_desc = pango_font_description_new ();
-  pango_font_description_set_variations (font_desc, s->str);
-  pango_font_description_set_variations (font_desc, s->str);
-  gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
+  if (s->len > 0)
+    {
+      font_desc = pango_font_description_new ();
+      pango_font_description_set_variations (font_desc, s->str);
+      gtk_font_chooser_widget_take_font_desc (fontchooser, font_desc);
+    }
 
   g_string_free (s, TRUE);
 
@@ -1368,6 +1372,12 @@ should_show_axis (FT_Var_Axis *ax)
   return TRUE;
 }
 
+static gboolean
+is_named_instance (FT_Face face)
+{
+  return (face->face_index >> 16) > 0;
+}
+
 struct {
   guint32 tag;
   const char *name;
@@ -1381,6 +1391,7 @@ struct {
 
 static void
 add_axis (GtkFontChooserWidget *fontchooser,
+          FT_Face               face,
           FT_Var_Axis          *ax,
           FT_Fixed              value,
           int                   i)
@@ -1426,6 +1437,12 @@ 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))
+    {
+      gtk_widget_hide (axis->label);
+      gtk_widget_hide (axis->scale);
+      gtk_widget_hide (axis->spin);
+    }
 }
 
 static void
@@ -1446,6 +1463,7 @@ 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));
+
   ret = FT_Get_MM_Var (ft_face, &ft_mm_var);
   if (ret == 0)
     {
@@ -1454,19 +1472,24 @@ gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchoose
       FT_Fixed *coords;
 
       coords = g_new (FT_Fixed, ft_mm_var->num_axis);
-      ret = FT_Get_Var_Design_Coordinates (ft_face, ft_mm_var->num_axis, coords);
-      if (ret == 0)
+      for (i = 0; i < ft_mm_var->num_axis; i++)
+        coords[i] = ft_mm_var->axis[i].def;
+
+      if (ft_face->face_index > 0)
         {
-          pos = 0;
-          for (i = 0; i < ft_mm_var->num_axis; i++)
+          int instance_id = ft_face->face_index >> 16;
+          if (instance_id && instance_id <= ft_mm_var->num_namedstyles)
             {
-              if (should_show_axis (&ft_mm_var->axis[i]))
-                {
-                  add_axis (fontchooser, &ft_mm_var->axis[i], coords[i], pos);
-                  pos++;
-                }
+              FT_Var_Named_Style *instance = &ft_mm_var->namedstyle[instance_id - 1];
+              memcpy (coords, instance->coords, ft_mm_var->num_axis * sizeof (*coords));
             }
         }
+
+      if (ret == 0)
+        {
+          for (i = 0; i < ft_mm_var->num_axis; i++)
+            add_axis (fontchooser, ft_face, &ft_mm_var->axis[i], coords[i], i);
+        }
       g_free (coords);
       free (ft_mm_var);
     }


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