Add gtk_widget_set_font_options and gtk_widget_get_font_options
authorArc Riley <arcriley@gmail.com>
Tue, 30 Jun 2015 17:57:49 +0000 (10:57 -0700)
committerMatthias Clasen <mclasen@redhat.com>
Wed, 1 Jul 2015 14:34:32 +0000 (07:34 -0700)
This allows a widget to override global font_options, such as hinting and
subpixel order. The widget's PangoContext is updated when this is set.

Some update code from gtk_widget_update_pango_context was moved to
update_pango_context so that gtk_widget_update_pango_context runs it.

http://bugzilla.gnome.org/show_bug.cgi?id=751677

docs/reference/gtk/gtk3-sections.txt
gtk/gtkwidget.c
gtk/gtkwidget.h

index 1967110df9a6c8f8753437550fac3d4017ae2fda..5df974531cd66dd463f1616289d918b64159720d 100644 (file)
@@ -5470,6 +5470,8 @@ gtk_widget_modify_font
 gtk_widget_modify_cursor
 gtk_widget_create_pango_context
 gtk_widget_get_pango_context
+gtk_widget_set_font_options
+gtk_widget_get_font_options
 gtk_widget_create_pango_layout
 gtk_widget_render_icon
 gtk_widget_render_icon_pixbuf
index d9230f5ac3f14ec68b132fdfb9dc2b6ac7214277..c1fd68a3f3baa051c242ec0dc8f8aca23e981d26 100644 (file)
@@ -599,6 +599,8 @@ struct _GtkWidgetPrivate
 #endif /* G_ENABLE_DEBUG */
 
   GList *event_controllers;
+
+  cairo_font_options_t *font_options;
 };
 
 struct _GtkWidgetClassPrivate
@@ -10314,10 +10316,11 @@ gtk_widget_get_pango_context (GtkWidget *widget)
 
 static void
 update_pango_context (GtkWidget    *widget,
-                     PangoContext *context)
+                      PangoContext *context)
 {
   PangoFontDescription *font_desc;
   GtkStyleContext *style_context;
+  GdkScreen *screen;
 
   style_context = gtk_widget_get_style_context (widget);
   gtk_style_context_get (style_context,
@@ -10337,6 +10340,18 @@ update_pango_context (GtkWidget    *widget,
                                           _gtk_style_context_peek_property (style_context,
                                                                             GTK_CSS_PROPERTY_DPI),
                                           100));
+
+  screen = gtk_widget_get_screen_unchecked (widget);
+  if (widget->priv->font_options)
+    {
+      pango_cairo_context_set_font_options (context,
+                                            widget->priv->font_options);
+    }
+  else if (screen)
+    {
+      pango_cairo_context_set_font_options (context,
+                                            gdk_screen_get_font_options (screen));
+    }
 }
 
 static void
@@ -10345,20 +10360,58 @@ gtk_widget_update_pango_context (GtkWidget *widget)
   PangoContext *context = gtk_widget_peek_pango_context (widget);
 
   if (context)
+    update_pango_context (widget, context);
+}
+
+/**
+ * gtk_widget_set_font_options:
+ * @widget: a #GtkWidget
+ * @options: (allow-none): a #cairo_font_options_t, or %NULL to unset any
+ *   previously set default font options.
+ *
+ * Sets the #cairo_font_options_t used for Pango rendering in this widget.
+ * When not set, the default font options for the #GdkScreen will be used.
+ *
+ * Since: 3.18
+ **/
+void
+gtk_widget_set_font_options (GtkWidget                  *widget,
+                             const cairo_font_options_t *options)
+{
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+
+  if (widget->priv->font_options != options)
     {
-      GdkScreen *screen;
+      if (widget->priv->font_options)
+        cairo_font_options_destroy (widget->priv->font_options);
 
-      update_pango_context (widget, context);
+      if (options)
+        widget->priv->font_options = cairo_font_options_copy (options);
+      else
+        widget->priv->font_options = NULL;
 
-      screen = gtk_widget_get_screen_unchecked (widget);
-      if (screen)
-       {
-         pango_cairo_context_set_font_options (context,
-                                               gdk_screen_get_font_options (screen));
-       }
+      gtk_widget_update_pango_context (widget);
     }
 }
 
+/**
+ * gtk_widget_get_font_options:
+ * @widget: a #GtkWidget
+ *
+ * Returns the #cairo_font_options_t used for Pango rendering. When not set,
+ * the defaults font options for the #GdkScreen will be used.
+ *
+ * Returns: (transfer none): the #cairo_font_options_t or %NULL if not set
+ *
+ * Since: 3.18
+ **/
+const cairo_font_options_t *
+gtk_widget_get_font_options (GtkWidget *widget)
+{
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+  return widget->priv->font_options;
+}
+
 /**
  * gtk_widget_create_pango_context:
  * @widget: a #GtkWidget
@@ -12214,6 +12267,9 @@ gtk_widget_finalize (GObject *object)
   if (priv->context)
     g_object_unref (priv->context);
 
+  if (widget->priv->font_options)
+    cairo_font_options_destroy (widget->priv->font_options);
+
   _gtk_size_request_cache_free (&priv->requests);
 
   for (l = priv->event_controllers; l; l = l->next)
index 450cd1968e25a4b410273aee5f3a9023b988e549..86f8e89d53913a324a3cc446380cb1e9e5436858 100644 (file)
@@ -1178,6 +1178,11 @@ GDK_AVAILABLE_IN_ALL
 PangoContext *gtk_widget_create_pango_context (GtkWidget   *widget);
 GDK_AVAILABLE_IN_ALL
 PangoContext *gtk_widget_get_pango_context    (GtkWidget   *widget);
+GDK_AVAILABLE_IN_3_18
+void gtk_widget_set_font_options (GtkWidget                  *widget,
+                                  const cairo_font_options_t *options);
+GDK_AVAILABLE_IN_3_18
+const cairo_font_options_t *gtk_widget_get_font_options (GtkWidget *widget);
 GDK_AVAILABLE_IN_ALL
 PangoLayout  *gtk_widget_create_pango_layout  (GtkWidget   *widget,
                                               const gchar *text);