[gtk+] GtkWidget: Add custom font map support



commit e7fe194b2da4a3614b81ea3275146bca41d5f938
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jul 6 00:33:47 2015 -0400

    GtkWidget: Add custom font map support
    
    This allows to use a custom font map for parts of the widget
    hierarchy - this can be used for application-specific fonts.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=751812

 docs/reference/gtk/gtk3-sections.txt |    2 +
 gtk/gtkwidget.c                      |   81 ++++++++++++++++++++++++++++++++--
 gtk/gtkwidget.h                      |   12 ++++-
 3 files changed, 88 insertions(+), 7 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 7b29c13..64d6f3c 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -5474,6 +5474,8 @@ gtk_widget_create_pango_context
 gtk_widget_get_pango_context
 gtk_widget_set_font_options
 gtk_widget_get_font_options
+gtk_widget_set_font_map
+gtk_widget_get_font_map
 gtk_widget_create_pango_layout
 gtk_widget_render_icon
 gtk_widget_render_icon_pixbuf
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 92e0dc5..f1b322a 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -601,6 +601,7 @@ struct _GtkWidgetPrivate
   GList *event_controllers;
 
   cairo_font_options_t *font_options;
+  PangoFontMap *font_map;
 };
 
 struct _GtkWidgetClassPrivate
@@ -10308,6 +10309,17 @@ gtk_widget_get_pango_context (GtkWidget *widget)
   return context;
 }
 
+static PangoFontMap *
+gtk_widget_get_effective_font_map (GtkWidget *widget)
+{
+  if (widget->priv->font_map)
+    return widget->priv->font_map;
+  else if (widget->priv->parent)
+    return gtk_widget_get_effective_font_map (widget->priv->parent);
+  else
+    return pango_cairo_font_map_get_default ();
+}
+
 static void
 update_pango_context (GtkWidget    *widget,
                       PangoContext *context)
@@ -10323,12 +10335,13 @@ update_pango_context (GtkWidget    *widget,
                          NULL);
 
   pango_context_set_font_description (context, font_desc);
+
+  pango_font_description_free (font_desc);
+
   pango_context_set_base_dir (context,
                              gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
                              PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
 
-  pango_font_description_free (font_desc);
-
   pango_cairo_context_set_resolution (context,
                                       _gtk_css_number_value_get (
                                           _gtk_style_context_peek_property (style_context,
@@ -10350,6 +10363,8 @@ update_pango_context (GtkWidget    *widget,
       pango_cairo_context_set_font_options (context,
                                             gdk_screen_get_font_options (screen));
     }
+
+  pango_context_set_font_map (context, gtk_widget_get_effective_font_map (widget));
 }
 
 static void
@@ -10410,13 +10425,71 @@ gtk_widget_get_font_options (GtkWidget *widget)
   return widget->priv->font_options;
 }
 
+static void
+gtk_widget_set_font_map_recurse (GtkWidget *widget, gpointer data)
+{
+  if (widget->priv->font_map)
+    return;
+
+  gtk_widget_update_pango_context (widget);
+
+  if (GTK_IS_CONTAINER (widget))
+    gtk_container_forall (GTK_CONTAINER (widget),
+                          gtk_widget_set_font_map_recurse,
+                          data);
+}
+
+/**
+ * gtk_widget_set_font_map:
+ * @widget: a #GtkWidget
+ * @font_map: (allow-nonw): a #PangoFontMap, or %NULL to unset any previously
+ *     set font map
+ *
+ * Sets the font map to use for Pango rendering. When not set, the widget
+ * will inherit the font map from its parent.
+ *
+ * Since: 3.18
+ */
+void
+gtk_widget_set_font_map (GtkWidget    *widget,
+                         PangoFontMap *font_map)
+{
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  if (g_set_object (&widget->priv->font_map, font_map))
+    {
+      gtk_widget_update_pango_context (widget);
+      if (GTK_IS_CONTAINER (widget))
+        gtk_container_forall (GTK_CONTAINER (widget),
+                              gtk_widget_set_font_map_recurse,
+                              NULL);
+    }
+}
+
+/**
+ * gtk_widget_get_font_map:
+ * @widget: a #GtkWidget
+ *
+ * Gets the font map that has been set with gtk_widget_set_font_map().
+ *
+ * Returns: (transfer none): A #PangoFontMap, or %NULL
+ *
+ * Since: 3.18
+ */
+PangoFontMap *
+gtk_widget_get_font_map (GtkWidget *widget)
+{
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+  return widget->priv->font_map;
+}
+
 /**
  * gtk_widget_create_pango_context:
  * @widget: a #GtkWidget
  *
  * Creates a new #PangoContext with the appropriate font map,
- * font description, and base direction for drawing text for
- * this widget. See also gtk_widget_get_pango_context().
+ * font options, font description, and base direction for drawing
+ * text for this widget. See also gtk_widget_get_pango_context().
  *
  * Returns: (transfer full): the new #PangoContext
  **/
diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h
index 86f8e89..9f6e86b 100644
--- a/gtk/gtkwidget.h
+++ b/gtk/gtkwidget.h
@@ -1481,11 +1481,17 @@ void    gtk_widget_class_bind_template_child_full       (GtkWidgetClass        *
                                                         gssize                 struct_offset);
 
 GDK_AVAILABLE_IN_3_16
-GActionGroup           *gtk_widget_get_action_group                     (GtkWidget    *widget,
-                                                                         const gchar  *prefix);
+GActionGroup           *gtk_widget_get_action_group     (GtkWidget             *widget,
+                                                         const gchar           *prefix);
 
 GDK_AVAILABLE_IN_3_16
-const gchar **          gtk_widget_list_action_prefixes                 (GtkWidget    *widget);
+const gchar **          gtk_widget_list_action_prefixes (GtkWidget             *widget);
+
+GDK_AVAILABLE_IN_3_18
+void                    gtk_widget_set_font_map         (GtkWidget             *widget,
+                                                         PangoFontMap          *fontmap);
+GDK_AVAILABLE_IN_3_18
+PangoFontMap *          gtk_widget_get_font_map         (GtkWidget             *widget);
 
 G_END_DECLS
 


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