[gtk/pango2] fontexplorer: Allow opening font files



commit 82ff952ac0a307d1f5dbbeac36ad5c44b313b81c
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jul 9 17:33:23 2022 -0400

    fontexplorer: Allow opening font files
    
    In this case, we set up a custom font map to
    let our widgets load the font, and we hide the
    font button.

 demos/font-explorer/fontcolors.c      | 24 +++++++++++++++++++--
 demos/font-explorer/fontcolors.h      |  4 +++-
 demos/font-explorer/fontexplorerapp.c | 19 +++++++++++++++++
 demos/font-explorer/fontexplorerwin.c | 39 +++++++++++++++++++++++++++++++++++
 demos/font-explorer/fontexplorerwin.h |  2 ++
 demos/font-explorer/fontfeatures.c    | 22 ++++++++++++++++++--
 demos/font-explorer/fontfeatures.h    |  4 +++-
 demos/font-explorer/fontvariations.c  | 21 +++++++++++++++++--
 demos/font-explorer/fontvariations.h  |  2 ++
 demos/font-explorer/fontview.c        | 33 ++++++++++++++++++++++++-----
 demos/font-explorer/fontview.h        |  6 ++++--
 demos/font-explorer/glyphmodel.c      |  9 +++-----
 demos/font-explorer/glyphmodel.h      |  2 +-
 13 files changed, 165 insertions(+), 22 deletions(-)
---
diff --git a/demos/font-explorer/fontcolors.c b/demos/font-explorer/fontcolors.c
index 26ec404524..ffb99de209 100644
--- a/demos/font-explorer/fontcolors.c
+++ b/demos/font-explorer/fontcolors.c
@@ -22,6 +22,8 @@ struct _FontColors
   gboolean has_colors;
   char *palette;
   GtkCheckButton *default_check;
+
+  Pango2FontMap *map;
 };
 
 struct _FontColorsClass
@@ -35,9 +37,16 @@ static Pango2Font *
 get_font (FontColors *self)
 {
   Pango2Context *context;
+  Pango2Font *font;
+
+  context = pango2_context_new ();
+  if (self->map)
+    pango2_context_set_font_map (context, self->map);
 
-  context = gtk_widget_get_pango_context (GTK_WIDGET (self));
-  return pango2_context_load_font (context, self->font_desc);
+  font = pango2_context_load_font (context, self->font_desc);
+  g_object_unref (context);
+
+  return font;
 }
 
 static void
@@ -201,6 +210,8 @@ font_colors_finalize (GObject *object)
   g_clear_pointer (&self->font_desc, pango2_font_description_free);
   g_free (self->palette);
 
+  g_clear_object (&self->map);
+
   G_OBJECT_CLASS (font_colors_parent_class)->finalize (object);
 }
 
@@ -285,3 +296,12 @@ font_colors_get_reset_action (FontColors *self)
 {
   return G_ACTION (self->reset_action);
 }
+
+void
+font_colors_set_font_map (FontColors    *self,
+                          Pango2FontMap *map)
+{
+  g_set_object (&self->map, map);
+  update_colors (self);
+}
+
diff --git a/demos/font-explorer/fontcolors.h b/demos/font-explorer/fontcolors.h
index d3abcf6ddc..8417239638 100644
--- a/demos/font-explorer/fontcolors.h
+++ b/demos/font-explorer/fontcolors.h
@@ -13,4 +13,6 @@ typedef struct _FontColorsClass    FontColorsClass;
 
 GType        font_colors_get_type          (void);
 FontColors * font_colors_new               (void);
-GAction *    font_colors_get_reset_action  (FontColors *self);
+void         font_colors_set_font_map      (FontColors    *self,
+                                            Pango2FontMap *map);
+GAction *    font_colors_get_reset_action  (FontColors    *self);
diff --git a/demos/font-explorer/fontexplorerapp.c b/demos/font-explorer/fontexplorerapp.c
index d98276ed17..74b88d89a6 100644
--- a/demos/font-explorer/fontexplorerapp.c
+++ b/demos/font-explorer/fontexplorerapp.c
@@ -149,11 +149,29 @@ font_explorer_app_activate (GApplication *app)
   gtk_window_present (GTK_WINDOW (win));
 }
 
+static void
+font_explorer_app_open (GApplication  *app,
+                        GFile        **files,
+                        int            n_files,
+                        const char    *hint)
+{
+  FontExplorerWindow *win;
+  int i;
+
+  for (i = 0; i < n_files; i++)
+    {
+      win = font_explorer_window_new (FONT_EXPLORER_APP (app));
+      font_explorer_window_load (win, files[i]);
+      gtk_window_present (GTK_WINDOW (win));
+    }
+}
+
 static void
 font_explorer_app_class_init (FontExplorerAppClass *class)
 {
   G_APPLICATION_CLASS (class)->startup = font_explorer_app_startup;
   G_APPLICATION_CLASS (class)->activate = font_explorer_app_activate;
+  G_APPLICATION_CLASS (class)->open = font_explorer_app_open;
 }
 
 FontExplorerApp *
@@ -161,5 +179,6 @@ font_explorer_app_new (void)
 {
   return g_object_new (FONT_EXPLORER_APP_TYPE,
                        "application-id", "org.gtk.FontExplorer",
+                       "flags", G_APPLICATION_HANDLES_OPEN,
                        NULL);
 }
diff --git a/demos/font-explorer/fontexplorerwin.c b/demos/font-explorer/fontexplorerwin.c
index 0356134c7f..a0d8eecebb 100644
--- a/demos/font-explorer/fontexplorerwin.c
+++ b/demos/font-explorer/fontexplorerwin.c
@@ -129,3 +129,42 @@ font_explorer_window_new (FontExplorerApp *app)
 {
   return g_object_new (FONT_EXPLORER_WINDOW_TYPE, "application", app, NULL);
 }
+
+void
+font_explorer_window_load (FontExplorerWindow *win,
+                           GFile              *file)
+{
+  const char *path;
+  Pango2FontMap *map;
+  Pango2HbFace *face;
+  Pango2FontDescription *desc;
+  char *title;
+
+  path = g_file_peek_path (file);
+
+  face = pango2_hb_face_new_from_file (path, 0, -2, NULL, NULL);
+  desc = pango2_font_face_describe (PANGO2_FONT_FACE (face));
+
+  map = pango2_font_map_new ();
+  pango2_font_map_add_face (map, PANGO2_FONT_FACE (face));
+  pango2_font_map_set_fallback (map, pango2_font_map_get_default ());
+
+  font_features_set_font_map (win->features, map);
+  font_variations_set_font_map (win->variations, map);
+  font_colors_set_font_map (win->colors, map);
+  font_view_set_font_map (win->view, map);
+
+  g_object_unref (map);
+
+  gtk_font_chooser_set_font_desc (GTK_FONT_CHOOSER (win->fontbutton), desc);
+
+  gtk_widget_hide (GTK_WIDGET (win->fontbutton));
+
+  title = g_strdup_printf ("%s — %s",
+                           pango2_font_description_get_family (desc),
+                           path);
+  gtk_window_set_title (GTK_WINDOW (win), title);
+  g_free (title);
+
+  pango2_font_description_free (desc);
+}
diff --git a/demos/font-explorer/fontexplorerwin.h b/demos/font-explorer/fontexplorerwin.h
index 77716775b6..130b216710 100644
--- a/demos/font-explorer/fontexplorerwin.h
+++ b/demos/font-explorer/fontexplorerwin.h
@@ -14,3 +14,5 @@ typedef struct _FontExplorerWindowClass    FontExplorerWindowClass;
 
 GType                   font_explorer_window_get_type     (void);
 FontExplorerWindow *    font_explorer_window_new          (FontExplorerApp    *app);
+void                    font_explorer_window_load         (FontExplorerWindow *win,
+                                                           GFile              *file);
diff --git a/demos/font-explorer/fontfeatures.c b/demos/font-explorer/fontfeatures.c
index 0412d974be..6920c5c5b2 100644
--- a/demos/font-explorer/fontfeatures.c
+++ b/demos/font-explorer/fontfeatures.c
@@ -32,6 +32,8 @@ struct _FontFeatures
   GSimpleAction *reset_action;
   Pango2Language *lang;
   GList *feature_items;
+
+  Pango2FontMap *map;
 };
 
 struct _FontFeaturesClass
@@ -45,9 +47,16 @@ static Pango2Font *
 get_font (FontFeatures *self)
 {
   Pango2Context *context;
+  Pango2Font *font;
+
+  context = pango2_context_new ();
+  if (self->map)
+    pango2_context_set_font_map (context, self->map);
+
+  font = pango2_context_load_font (context, self->font_desc);
+  g_object_unref (context);
 
-  context = gtk_widget_get_pango_context (GTK_WIDGET (self));
-  return pango2_context_load_font (context, self->font_desc);
+  return font;
 }
 
 static gboolean
@@ -629,6 +638,7 @@ font_features_finalize (GObject *object)
   FontFeatures *self = FONT_FEATURES (object);
 
   g_clear_pointer (&self->font_desc, pango2_font_description_free);
+  g_clear_object (&self->map);
 
   G_OBJECT_CLASS (font_features_parent_class)->finalize (object);
 }
@@ -725,3 +735,11 @@ font_features_get_reset_action (FontFeatures *self)
 {
   return G_ACTION (self->reset_action);
 }
+
+void
+font_features_set_font_map (FontFeatures  *self,
+                            Pango2FontMap *map)
+{
+  g_set_object (&self->map, map);
+  update_features (self);
+}
diff --git a/demos/font-explorer/fontfeatures.h b/demos/font-explorer/fontfeatures.h
index af6e92171a..c0877ed6fc 100644
--- a/demos/font-explorer/fontfeatures.h
+++ b/demos/font-explorer/fontfeatures.h
@@ -13,4 +13,6 @@ typedef struct _FontFeaturesClass    FontFeaturesClass;
 
 GType          font_features_get_type          (void);
 FontFeatures * font_features_new               (void);
-GAction *      font_features_get_reset_action  (FontFeatures *self);
+void           font_features_set_font_map      (FontFeatures  *self,
+                                                Pango2FontMap *map);
+GAction *      font_features_get_reset_action  (FontFeatures  *self);
diff --git a/demos/font-explorer/fontvariations.c b/demos/font-explorer/fontvariations.c
index e00fc3301f..5926df29be 100644
--- a/demos/font-explorer/fontvariations.c
+++ b/demos/font-explorer/fontvariations.c
@@ -24,6 +24,8 @@ struct _FontVariations
   GtkWidget *instance_combo;
   GHashTable *axes;
   GHashTable *instances;
+
+  Pango2FontMap *map;
 };
 
 struct _FontVariationsClass
@@ -37,9 +39,15 @@ static Pango2Font *
 get_font (FontVariations *self)
 {
   Pango2Context *context;
+  Pango2Font *font;
+
+  context = pango2_context_new ();
+  if (self->map)
+    pango2_context_set_font_map (context, self->map);
+  font = pango2_context_load_font (context, self->font_desc);
+  g_object_unref (context);
 
-  context = gtk_widget_get_pango_context (GTK_WIDGET (self));
-  return pango2_context_load_font (context, self->font_desc);
+  return font;
 }
 
 typedef struct {
@@ -401,6 +409,7 @@ font_variations_finalize (GObject *object)
   FontVariations *self = FONT_VARIATIONS (object);
 
   g_clear_pointer (&self->font_desc, pango2_font_description_free);
+  g_clear_object (&self->map);
 
   G_OBJECT_CLASS (font_variations_parent_class)->finalize (object);
 }
@@ -486,3 +495,11 @@ font_variations_get_reset_action (FontVariations *self)
 {
   return G_ACTION (self->reset_action);
 }
+
+void
+font_variations_set_font_map (FontVariations *self,
+                              Pango2FontMap  *map)
+{
+  g_set_object (&self->map, map);
+  update_variations (self);
+}
diff --git a/demos/font-explorer/fontvariations.h b/demos/font-explorer/fontvariations.h
index afd12dbaf9..91e932694f 100644
--- a/demos/font-explorer/fontvariations.h
+++ b/demos/font-explorer/fontvariations.h
@@ -13,4 +13,6 @@ typedef struct _FontVariationsClass    FontVariationsClass;
 
 GType            font_variations_get_type          (void);
 FontVariations * font_variations_new               (void);
+void             font_variations_set_font_map      (FontVariations *self,
+                                                    Pango2FontMap  *map);
 GAction *        font_variations_get_reset_action  (FontVariations *self);
diff --git a/demos/font-explorer/fontview.c b/demos/font-explorer/fontview.c
index 2c6d6bd196..0ce33340e3 100644
--- a/demos/font-explorer/fontview.c
+++ b/demos/font-explorer/fontview.c
@@ -47,6 +47,8 @@ struct _FontView
   GtkCssProvider *bg_provider;
   char *sample_text;
   gboolean do_waterfall;
+
+  Pango2FontMap *map;
 };
 
 struct _FontViewClass
@@ -96,6 +98,7 @@ font_view_finalize (GObject *object)
   g_free (self->variations);
   g_free (self->features);
   g_free (self->palette);
+  g_clear_object (&self->map);
 
   G_OBJECT_CLASS (font_view_parent_class)->finalize (object);
 }
@@ -261,32 +264,40 @@ info_changed (GtkToggleButton *button,
 }
 
 static Pango2Font *
-get_font (FontView *self)
+get_font (FontView *self,
+          int       size)
 {
   Pango2Context *context;
   Pango2FontDescription *desc;
   Pango2Font *font;
 
+  context = pango2_context_new ();
+  if (self->map)
+    pango2_context_set_font_map (context, self->map);
+
   desc = pango2_font_description_copy_static (self->font_desc);
   pango2_font_description_set_variations (desc, self->variations);
-  context = gtk_widget_get_pango_context (GTK_WIDGET (self));
+  pango2_font_description_set_size (desc, size);
   font = pango2_context_load_font (context, desc);
   pango2_font_description_free (desc);
 
+  g_object_unref (context);
+
   return font;
 }
 
 static void
 update_glyph_model (FontView *self)
 {
-  Pango2Font *font = get_font (self);
+  Pango2Font *font = get_font (self, 60 * PANGO2_SCALE);
   GlyphModel *gm;
   GtkSelectionModel *model;
 
-  gm = glyph_model_new (pango2_font_get_face (font));
+  gm = glyph_model_new (font);
   model = GTK_SELECTION_MODEL (gtk_no_selection_new (G_LIST_MODEL (gm)));
   gtk_grid_view_set_model (self->glyphs, model);
   g_object_unref (model);
+  g_object_unref (font);
 }
 
 static void
@@ -399,7 +410,8 @@ static void
 update_info (FontView *self)
 {
   GtkWidget *child;
-  Pango2Font *pango_font = get_font (self);
+  int size = pango2_font_description_get_size (self->font_desc);
+  Pango2Font *pango_font = get_font (self, MAX (size, 10 * PANGO2_SCALE));
   hb_font_t *font1 = pango2_font_get_hb_font (pango_font);
   hb_face_t *face = hb_font_get_face (font1);
   hb_font_t *font = hb_font_create_sub_font (font1);
@@ -530,6 +542,7 @@ font_view_set_property (GObject      *object,
     case PROP_VARIATIONS:
       g_free (self->variations);
       self->variations = g_strdup (g_value_get_string (value));
+      update_glyph_model (self);
       update_info (self);
       break;
 
@@ -700,3 +713,13 @@ font_view_new (void)
 {
   return g_object_new (FONT_VIEW_TYPE, NULL);
 }
+
+void
+font_view_set_font_map (FontView      *self,
+                        Pango2FontMap *map)
+{
+  g_set_object (&self->map, map);
+  gtk_widget_set_font_map (GTK_WIDGET (self->content), self->map);
+  update_view (self);
+}
+
diff --git a/demos/font-explorer/fontview.h b/demos/font-explorer/fontview.h
index fb35b524cb..31112fe15c 100644
--- a/demos/font-explorer/fontview.h
+++ b/demos/font-explorer/fontview.h
@@ -11,5 +11,7 @@ typedef struct _FontView         FontView;
 typedef struct _FontViewClass    FontViewClass;
 
 
-GType           font_view_get_type              (void);
-FontView *      font_view_new                   (void);
+GType           font_view_get_type     (void);
+FontView *      font_view_new          (void);
+void            font_view_set_font_map (FontView      *self,
+                                        Pango2FontMap *map);
diff --git a/demos/font-explorer/glyphmodel.c b/demos/font-explorer/glyphmodel.c
index cfb53b412b..eb925795a7 100644
--- a/demos/font-explorer/glyphmodel.c
+++ b/demos/font-explorer/glyphmodel.c
@@ -80,18 +80,15 @@ glyph_model_class_init (GlyphModelClass *class)
 }
 
 GlyphModel *
-glyph_model_new (Pango2FontFace *face)
+glyph_model_new (Pango2Font *font)
 {
   GlyphModel *self;
-  Pango2FontDescription *desc;
   hb_face_t *hb_face;
 
   self = g_object_new (GLYPH_MODEL_TYPE, NULL);
 
-  desc = pango2_font_description_new ();
-  pango2_font_description_set_size (desc, 60 * PANGO2_SCALE);
-  self->font = PANGO2_FONT (pango2_hb_font_new_for_description (PANGO2_HB_FACE (face), desc, 96., NULL));
-  hb_face = pango2_hb_face_get_hb_face (PANGO2_HB_FACE (face));
+  self->font = g_object_ref (font);
+  hb_face = pango2_hb_face_get_hb_face (PANGO2_HB_FACE (pango2_font_get_face (font)));
   self->num_glyphs = hb_face_get_glyph_count (hb_face);
   self->glyphs = g_new0 (GlyphItem *, self->num_glyphs);
 
diff --git a/demos/font-explorer/glyphmodel.h b/demos/font-explorer/glyphmodel.h
index c6cd63b465..de8c5fb0fb 100644
--- a/demos/font-explorer/glyphmodel.h
+++ b/demos/font-explorer/glyphmodel.h
@@ -11,4 +11,4 @@ typedef struct _GlyphModelClass    GlyphModelClass;
 
 
 GType        glyph_model_get_type (void);
-GlyphModel * glyph_model_new      (Pango2FontFace *face);
+GlyphModel * glyph_model_new      (Pango2Font *font);


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