font chooser: Make levels flags

Turn the GtkFontChooserLevel field into flags, and
add flags for OpenType variations and features. The
motivation for this is to make font-features in the UI
opt-in, since applications need to support them by
applying the pango attribute.
This commit is contained in:
Matthias Clasen 2018-03-27 16:10:19 -04:00
parent 85a7d0a201
commit b1b05bee53
4 changed files with 52 additions and 31 deletions

View File

@ -606,6 +606,10 @@ gtk_font_button_init (GtkFontButton *font_button)
font_button->priv->font_face = NULL;
font_button->priv->font_size = -1;
font_button->priv->title = g_strdup (_("Pick a Font"));
font_button->priv->level = GTK_FONT_CHOOSER_LEVEL_FAMILY |
GTK_FONT_CHOOSER_LEVEL_STYLE |
GTK_FONT_CHOOSER_LEVEL_SIZE |
GTK_FONT_CHOOSER_LEVEL_VARIATION;
gtk_font_button_take_font_desc (font_button, NULL);
@ -659,7 +663,7 @@ gtk_font_button_set_property (GObject *object,
gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
break;
case GTK_FONT_CHOOSER_PROP_LEVEL:
gtk_font_button_set_level (font_button, g_value_get_enum (value));
gtk_font_button_set_level (font_button, g_value_get_flags (value));
break;
case GTK_FONT_CHOOSER_PROP_FONT:
gtk_font_button_set_font_name (font_button, g_value_get_string (value));
@ -706,7 +710,7 @@ gtk_font_button_get_property (GObject *object,
g_value_set_string (value, priv->language);
break;
case GTK_FONT_CHOOSER_PROP_LEVEL:
g_value_set_enum (value, priv->level);
g_value_set_flags (value, priv->level);
break;
case GTK_FONT_CHOOSER_PROP_FONT:
g_value_set_string (value, gtk_font_button_get_font_name (font_button));
@ -1284,15 +1288,15 @@ gtk_font_button_update_font_info (GtkFontButton *font_button)
else
face_name = "";
if (priv->level == GTK_FONT_CHOOSER_LEVEL_FAMILY)
family_style = g_strdup (fam_name);
else
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_STYLE) != 0)
family_style = g_strconcat (fam_name, " ", face_name, NULL);
else
family_style = g_strdup (fam_name);
gtk_label_set_text (GTK_LABEL (font_button->priv->font_label), family_style);
g_free (family_style);
if (priv->level == GTK_FONT_CHOOSER_LEVEL_FONT)
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_SIZE) != 0)
{
/* mirror Pango, which doesn't translate this either */
gchar *size = g_strdup_printf ("%2.4g%s",

View File

@ -112,12 +112,15 @@ gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
*/
g_object_interface_install_property
(iface,
g_param_spec_enum ("level",
P_("Selection level"),
P_("Whether to select family, face or font"),
GTK_TYPE_FONT_CHOOSER_LEVEL,
GTK_FONT_CHOOSER_LEVEL_FONT,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
g_param_spec_flags ("level",
P_("Selection level"),
P_("Whether to select family, face or font"),
GTK_TYPE_FONT_CHOOSER_LEVEL,
GTK_FONT_CHOOSER_LEVEL_FAMILY |
GTK_FONT_CHOOSER_LEVEL_STYLE |
GTK_FONT_CHOOSER_LEVEL_SIZE |
GTK_FONT_CHOOSER_LEVEL_VARIATION,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkFontChooser:font-features:

View File

@ -46,20 +46,24 @@ typedef gboolean (*GtkFontFilterFunc) (const PangoFontFamily *family,
/**
* GtkFontChooserLevel:
* @GTK_FONT_CHOOSER_LEVEL_FONT: Select an individual
* font, including a size. An example would be: "Arial Bold 11"
* @GTK_FONT_CHOOSER_LEVEL_FACE: Select a font face,
* without a size. An example would be: "Arial Bold"
* @GTK_FONT_CHOOSER_LEVEL_FAMILY: Select a font family, without
* specifying the face. An example would be: "Arial"
* @GTK_FONT_CHOOSER_LEVEL_FAMILY: Allow selecting a font family
* @GTK_FONT_CHOOSER_LEVEL_STYLE: Allow selecting a specific font face
* @GTK_FONT_CHOOSER_LEVEL_SIZE: Allow selecting a specific font size
* @GTK_FONT_CHOOSER_LEVEL_VARIATION: Allow changing OpenType font variation axes
* @GTK_FONT_CHOOSER_LEVEL_FEATURES: Allow selecting specific OpenType font features
*
* This enumeration specifies the granularity of font selection
* that is desired in a font chooser.
*
* This enumeration may be extended in the future; applications should
* ignore unknown values.
*/
typedef enum {
GTK_FONT_CHOOSER_LEVEL_FONT,
GTK_FONT_CHOOSER_LEVEL_FACE,
GTK_FONT_CHOOSER_LEVEL_FAMILY
GTK_FONT_CHOOSER_LEVEL_FAMILY = 0,
GTK_FONT_CHOOSER_LEVEL_STYLE = 1 << 0,
GTK_FONT_CHOOSER_LEVEL_SIZE = 1 << 1,
GTK_FONT_CHOOSER_LEVEL_VARIATION = 1 << 2,
GTK_FONT_CHOOSER_LEVEL_FEATURES = 1 << 3
} GtkFontChooserLevel;
#define GTK_TYPE_FONT_CHOOSER (gtk_font_chooser_get_type ())

View File

@ -305,7 +305,7 @@ gtk_font_chooser_widget_set_property (GObject *object,
gtk_font_chooser_widget_set_show_preview_entry (fontchooser, g_value_get_boolean (value));
break;
case GTK_FONT_CHOOSER_PROP_LEVEL:
gtk_font_chooser_widget_set_level (fontchooser, g_value_get_enum (value));
gtk_font_chooser_widget_set_level (fontchooser, g_value_get_flags (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -339,7 +339,7 @@ gtk_font_chooser_widget_get_property (GObject *object,
g_value_set_boolean (value, gtk_font_chooser_widget_get_show_preview_entry (fontchooser));
break;
case GTK_FONT_CHOOSER_PROP_LEVEL:
g_value_set_enum (value, gtk_font_chooser_widget_get_level (fontchooser));
g_value_set_flags (value, gtk_font_chooser_widget_get_level (fontchooser));
break;
case GTK_FONT_CHOOSER_PROP_FONT_FEATURES:
g_value_set_string (value, fontchooser->priv->font_features);
@ -863,6 +863,10 @@ gtk_font_chooser_widget_init (GtkFontChooserWidget *fontchooser)
priv->preview_text = g_strdup (pango_language_get_sample_string (NULL));
priv->show_preview_entry = TRUE;
priv->font_desc = pango_font_description_new ();
priv->level = GTK_FONT_CHOOSER_LEVEL_FAMILY |
GTK_FONT_CHOOSER_LEVEL_STYLE |
GTK_FONT_CHOOSER_LEVEL_SIZE |
GTK_FONT_CHOOSER_LEVEL_VARIATION;
/* Set default preview text */
gtk_entry_set_text (GTK_ENTRY (priv->preview), priv->preview_text);
@ -981,10 +985,10 @@ gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser,
face_name = pango_font_face_get_face_name (faces[j]);
if (priv->level == GTK_FONT_CHOOSER_LEVEL_FAMILY)
title = g_strdup (fam_name);
else
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_STYLE) != 0)
title = g_strconcat (fam_name, " ", face_name, NULL);
else
title = g_strdup (fam_name);
desc = gtk_delayed_font_description_new (faces[j]);
@ -998,7 +1002,7 @@ gtk_font_chooser_widget_load_fonts (GtkFontChooserWidget *fontchooser,
g_free (title);
gtk_delayed_font_description_unref (desc);
if (priv->level == GTK_FONT_CHOOSER_LEVEL_FAMILY)
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_STYLE) == 0)
break;
}
@ -1415,10 +1419,10 @@ gtk_font_chooser_widget_update_font_name (GtkFontChooserWidget *fontchooser,
g_object_unref (face);
gtk_delayed_font_description_unref (desc);
if (priv->level == GTK_FONT_CHOOSER_LEVEL_FAMILY)
title = g_strdup (fam_name);
else
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_STYLE) != 0)
title = g_strconcat (fam_name, " ", face_name, NULL);
else
title = g_strdup (fam_name);
attrs = gtk_font_chooser_widget_get_preview_attributes (fontchooser, font_desc);
gtk_label_set_attributes (GTK_LABEL (priv->font_name_label), attrs);
@ -1639,6 +1643,9 @@ gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchoose
g_hash_table_foreach (priv->axes, axis_remove, NULL);
g_hash_table_remove_all (priv->axes);
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_VARIATION) == 0)
return FALSE;
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));
@ -2042,6 +2049,9 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
gtk_widget_hide (gtk_widget_get_parent (item->feat));
}
if ((priv->level & GTK_FONT_CHOOSER_LEVEL_FEATURES) == 0)
return FALSE;
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)),
@ -2384,7 +2394,7 @@ gtk_font_chooser_widget_set_level (GtkFontChooserWidget *fontchooser,
priv->level = level;
if (level == GTK_FONT_CHOOSER_LEVEL_FONT)
if ((level & GTK_FONT_CHOOSER_LEVEL_SIZE) != 0)
{
gtk_widget_show (priv->size_label);
gtk_widget_show (priv->size_slider);