Commit dfe04d0c authored by Matthias Clasen's avatar Matthias Clasen
Browse files

font chooser: Make the language property writable

This is meant as an input to the font chooser.
We don't want the user to select a language, but
rather have fonts presented as they would work for
the current language. Therefore, do away with the
lang/script combo on the tweak page.
parent 71acf236
......@@ -85,7 +85,7 @@ struct _GtkFontButtonPrivate
PangoFontMap *font_map;
gint font_size;
char *font_features;
char *language;
PangoLanguage *language;
gchar *preview_text;
GtkFontFilterFunc font_filter;
gpointer font_filter_data;
......@@ -137,11 +137,13 @@ static void dialog_destroy (GtkWidget *widget,
static void gtk_font_button_label_use_font (GtkFontButton *gfs);
static void gtk_font_button_update_font_info (GtkFontButton *gfs);
static void font_button_set_font_name (GtkFontButton *font_button,
const char *fontname);
static void gtk_font_button_set_level (GtkFontButton *font_button,
GtkFontChooserLevel level);
static void gtk_font_button_set_font_name (GtkFontButton *button,
const char *fontname);
static const char *gtk_font_button_get_font_name (GtkFontButton *button);
static void gtk_font_button_set_level (GtkFontButton *font_button,
GtkFontChooserLevel level);
static void gtk_font_button_set_language (GtkFontButton *button,
const char *language);
static guint font_button_signals[LAST_SIGNAL] = { 0 };
......@@ -167,9 +169,6 @@ clear_font_data (GtkFontButton *font_button)
g_free (priv->font_features);
priv->font_features = NULL;
g_free (priv->language);
priv->language = NULL;
}
static void
......@@ -629,6 +628,7 @@ gtk_font_button_init (GtkFontButton *font_button)
font_button->priv->level = GTK_FONT_CHOOSER_LEVEL_FAMILY |
GTK_FONT_CHOOSER_LEVEL_STYLE |
GTK_FONT_CHOOSER_LEVEL_SIZE;
font_button->priv->language = pango_language_get_default ();
gtk_widget_init_template (GTK_WIDGET (font_button));
......@@ -681,6 +681,9 @@ gtk_font_button_set_property (GObject *object,
case GTK_FONT_CHOOSER_PROP_FONT_DESC:
gtk_font_button_take_font_desc (font_button, g_value_dup_boxed (value));
break;
case GTK_FONT_CHOOSER_PROP_LANGUAGE:
gtk_font_button_set_language (font_button, g_value_get_string (value));
break;
case GTK_FONT_CHOOSER_PROP_LEVEL:
gtk_font_button_set_level (font_button, g_value_get_flags (value));
break;
......@@ -733,7 +736,7 @@ gtk_font_button_get_property (GObject *object,
g_value_set_string (value, priv->font_features);
break;
case GTK_FONT_CHOOSER_PROP_LANGUAGE:
g_value_set_string (value, priv->language);
g_value_set_string (value, pango_language_to_string (priv->language));
break;
case GTK_FONT_CHOOSER_PROP_LEVEL:
g_value_set_flags (value, priv->level);
......@@ -1100,7 +1103,8 @@ gtk_font_button_clicked (GtkButton *button)
if (priv->font_map)
gtk_font_chooser_set_font_map (font_dialog, priv->font_map);
gtk_font_chooser_set_show_preview_entry (font_dialog, priv->show_preview_entry);
g_object_set (font_dialog, "level", priv->level, NULL);
gtk_font_chooser_set_level (GTK_FONT_CHOOSER (font_dialog), priv->level);
gtk_font_chooser_set_language (GTK_FONT_CHOOSER (font_dialog), priv->language);
if (priv->preview_text)
{
......@@ -1438,3 +1442,16 @@ gtk_font_button_set_level (GtkFontButton *button,
g_object_notify (G_OBJECT (button), "level");
}
static void
gtk_font_button_set_language (GtkFontButton *button,
const char *language)
{
GtkFontButtonPrivate *priv = button->priv;
priv->language = pango_language_from_string (language);
if (priv->font_dialog)
gtk_font_chooser_set_language (GTK_FONT_CHOOSER (priv->font_dialog), language);
g_object_notify (G_OBJECT (button), "language");
}
......@@ -154,7 +154,7 @@ gtk_font_chooser_default_init (GtkFontChooserInterface *iface)
P_("Language"),
P_("Language for which features have been selected"),
"",
GTK_PARAM_READABLE));
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkFontChooser::font-activated:
......@@ -532,3 +532,95 @@ gtk_font_chooser_get_font_map (GtkFontChooser *fontchooser)
return fontmap;
}
/**
* gtk_font_chooser_set_level:
* @fontchooser: a #GtkFontChooser
* @level: the desired level of granularity
*
* Sets the desired level of granularity for selecting fonts.
*/
void
gtk_font_chooser_set_level (GtkFontChooser *fontchooser,
GtkFontChooserLevel level)
{
g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
g_object_set (fontchooser, "level", level, NULL);
}
/**
* gtk_font_chooser_get_level:
* @fontchooser: a #GtkFontChooser
*
* Returns the current level of granularity for selecting fonts.
*
* Returns: the current granularity level
*/
GtkFontChooserLevel
gtk_font_chooser_get_level (GtkFontChooser *fontchooser)
{
GtkFontChooserLevel level;
g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), 0);
g_object_get (fontchooser, "level", &level, NULL);
return level;
}
/**
* gtk_font_chooser_get_font_features:
* @fontchooser: a #GtkFontChooser
*
* Gets the currently-selected font features.
*
* Returns: the currently selected font features
*/
char *
gtk_font_chooser_get_font_features (GtkFontChooser *fontchooser)
{
char *text;
g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
g_object_get (fontchooser, "font-features", &text, NULL);
return text;
}
/**
* gtk_font_chooser_get_language:
* @fontchooser: a #GtkFontChooser
*
* Gets the language that is used for font features.
*
* Returns: the currently selected language
*/
char *
gtk_font_chooser_get_language (GtkFontChooser *fontchooser)
{
char *text;
g_return_val_if_fail (GTK_IS_FONT_CHOOSER (fontchooser), NULL);
g_object_get (fontchooser, "language", &text, NULL);
return text;
}
/**
* gtk_font_chooser_set_language:
* @fontchooser: a #GtkFontChooser
* @language: a language
*
* Sets the language to use for font features.
*/
void
gtk_font_chooser_set_language (GtkFontChooser *fontchooser,
const char *language)
{
g_return_if_fail (GTK_IS_FONT_CHOOSER (fontchooser));
g_object_set (fontchooser, "language", language, NULL);
}
......@@ -145,7 +145,19 @@ void gtk_font_chooser_set_font_map (GtkFontChooser *fo
GDK_AVAILABLE_IN_3_18
PangoFontMap * gtk_font_chooser_get_font_map (GtkFontChooser *fontchooser);
GDK_AVAILABLE_IN_3_24
void gtk_font_chooser_set_level (GtkFontChooser *fontchooser,
GtkFontChooserLevel level);
GDK_AVAILABLE_IN_3_24
GtkFontChooserLevel
gtk_font_chooser_get_level (GtkFontChooser *fontchooser);
GDK_AVAILABLE_IN_3_24
char * gtk_font_chooser_get_font_features (GtkFontChooser *fontchooser);
GDK_AVAILABLE_IN_3_24
char * gtk_font_chooser_get_language (GtkFontChooser *fontchooser);
GDK_AVAILABLE_IN_3_24
void gtk_font_chooser_set_language (GtkFontChooser *fontchooser,
const char *language);
G_END_DECLS
......
......@@ -222,7 +222,7 @@ gtk_font_chooser_dialog_class_init (GtkFontChooserDialogClass *klass)
/* Bind class to template
*/
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gtk/libgtk/ui/gtkfontchooserdialog.ui");
"/org/gtk/libgtk/ui/gtkfontchooserdialog.ui");
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserDialog, fontchooser);
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserDialog, select_button);
......
......@@ -118,13 +118,12 @@ struct _GtkFontChooserWidgetPrivate
GtkWidget *axis_grid;
GtkWidget *feature_box;
GtkWidget *feature_language_combo;
PangoFontMap *font_map;
PangoFontDescription *font_desc;
char *font_features;
PangoLanguage *font_language;
PangoLanguage *language;
GtkTreeIter font_iter; /* invalid if font not available or pointer into model
(not filter_model) to the row containing font */
GtkFontFilterFunc filter_func;
......@@ -228,8 +227,11 @@ static void update_language (GtkFontChooserWidget *fontchooser);
static void gtk_font_chooser_widget_set_level (GtkFontChooserWidget *fontchooser,
GtkFontChooserLevel level);
static GtkFontChooserLevel gtk_font_chooser_widget_get_level (GtkFontChooserWidget *fontchooser);
static void gtk_font_chooser_widget_set_language (GtkFontChooserWidget *fontchooser,
const char *language);
static void selection_changed (GtkTreeSelection *selection,
GtkFontChooserWidget *fontchooser);
static void update_font_features (GtkFontChooserWidget *fontchooser);
static void gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface);
......@@ -320,6 +322,9 @@ gtk_font_chooser_widget_set_property (GObject *object,
case GTK_FONT_CHOOSER_PROP_LEVEL:
gtk_font_chooser_widget_set_level (fontchooser, g_value_get_flags (value));
break;
case GTK_FONT_CHOOSER_PROP_LANGUAGE:
gtk_font_chooser_widget_set_language (fontchooser, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
......@@ -358,7 +363,7 @@ gtk_font_chooser_widget_get_property (GObject *object,
g_value_set_string (value, fontchooser->priv->font_features);
break;
case GTK_FONT_CHOOSER_PROP_LANGUAGE:
g_value_set_string (value, pango_language_to_string (fontchooser->priv->font_language));
g_value_set_string (value, pango_language_to_string (fontchooser->priv->language));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
......@@ -617,8 +622,8 @@ gtk_font_chooser_widget_update_preview_attributes (GtkFontChooserWidget *fontcho
pango_attr_list_insert (attrs, pango_attr_font_desc_new (priv->font_desc));
if (priv->font_features)
pango_attr_list_insert (attrs, pango_attr_font_features_new (priv->font_features));
if (priv->font_language)
pango_attr_list_insert (attrs, pango_attr_language_new (priv->font_language));
if (priv->language)
pango_attr_list_insert (attrs, pango_attr_language_new (priv->language));
gtk_entry_set_attributes (GTK_ENTRY (priv->preview), attrs);
pango_attr_list_unref (attrs);
......@@ -717,7 +722,6 @@ gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, stack);
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, font_name_label);
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, feature_box);
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, feature_language_combo);
gtk_widget_class_bind_template_child_private (widget_class, GtkFontChooserWidget, axis_grid);
gtk_widget_class_bind_template_callback (widget_class, text_changed_cb);
......@@ -732,9 +736,8 @@ gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
gtk_widget_class_bind_template_callback (widget_class, size_change_cb);
gtk_widget_class_bind_template_callback (widget_class, output_cb);
gtk_widget_class_bind_template_callback (widget_class, selection_changed);
gtk_widget_class_bind_template_callback (widget_class, update_language);
gtk_widget_class_set_css_name (widget_class, "fontchooser");
gtk_widget_class_set_css_name (widget_class, I_("fontchooser"));
}
static void
......@@ -827,6 +830,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;
priv->language = pango_language_get_default ();
/* Set default preview text */
gtk_entry_set_text (GTK_ENTRY (priv->preview), priv->preview_text);
......@@ -1644,102 +1651,28 @@ gtk_font_chooser_widget_update_font_variations (GtkFontChooserWidget *fontchoose
/* OpenType features */
/* look for a lang / script combination that matches the
* language property and is supported by the hb_face. If
* none is found, return the default lang / script tags.
*/
static void
add_script (GtkListStore *store,
guint script_index,
hb_tag_t script,
guint lang_index,
hb_tag_t lang)
{
char langbuf[5];
const char *langname;
if (lang == HB_OT_TAG_DEFAULT_LANGUAGE)
langname = C_("Language", "Default");
else
{
langname = get_language_name_for_tag (lang);
if (!langname)
{
hb_tag_to_string (lang, langbuf);
langbuf[4] = 0;
langname = langbuf;
}
}
gtk_list_store_insert_with_values (store, NULL, -1,
0, langname,
1, script_index,
2, lang_index,
3, lang,
-1);
}
static int
feature_language_sort_func (GtkTreeModel *model,
GtkTreeIter *a,
GtkTreeIter *b,
gpointer user_data)
{
char *sa, *sb;
int ret;
gtk_tree_model_get (model, a, 0, &sa, -1);
gtk_tree_model_get (model, b, 0, &sb, -1);
ret = strcmp (sa, sb);
g_free (sa);
g_free (sb);
return ret;
}
typedef struct {
hb_tag_t script_tag;
hb_tag_t lang_tag;
unsigned int script_index;
unsigned int lang_index;
} TagPair;
static guint
tag_pair_hash (gconstpointer data)
{
const TagPair *pair = data;
return pair->script_tag + pair->lang_tag;
}
static gboolean
tag_pair_equal (gconstpointer a, gconstpointer b)
{
const TagPair *pair_a = a;
const TagPair *pair_b = b;
return pair_a->script_tag == pair_b->script_tag && pair_a->lang_tag == pair_b->lang_tag;
}
static void
update_language_combo (GtkFontChooserWidget *fontchooser,
hb_face_t *hb_face)
find_language_and_script (GtkFontChooserWidget *fontchooser,
hb_face_t *hb_face,
hb_tag_t *lang_tag,
hb_tag_t *script_tag)
{
GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
GtkListStore *store;
gint i, j, k;
hb_tag_t scripts[80];
unsigned int n_scripts;
unsigned int count;
hb_tag_t table[2] = { HB_OT_TAG_GSUB, HB_OT_TAG_GPOS };
GHashTable *tags;
GHashTableIter iter;
TagPair *pair;
tags = g_hash_table_new_full (tag_pair_hash, tag_pair_equal, g_free, NULL);
hb_language_t lang;
const char *langname, *p;
pair = g_new (TagPair, 1);
pair->script_tag = HB_OT_TAG_DEFAULT_SCRIPT;
pair->lang_tag = HB_OT_TAG_DEFAULT_LANGUAGE;
g_hash_table_add (tags, pair);
langname = pango_language_to_string (priv->language);
p = strchr (langname, '-');
lang = hb_language_from_string (langname, p ? p - langname : -1);
n_scripts = 0;
for (i = 0; i < 2; i++)
......@@ -1764,30 +1697,22 @@ update_language_combo (GtkFontChooserWidget *fontchooser,
for (k = 0; k < n_languages; k++)
{
pair = g_new (TagPair, 1);
pair->script_tag = scripts[j];
pair->lang_tag = languages[k];
pair->script_index = j;
pair->lang_index = k;
g_hash_table_add (tags, pair);
hb_language_t *l;
char buf[5], buf2[5];
hb_tag_to_string (languages[k], buf); buf[4] = '\0';
hb_tag_to_string (scripts[j], buf2); buf2[4] = '\0';
l = hb_ot_tag_to_language (languages[k]);
if (l == lang)
{
*script_tag = scripts[j];
*lang_tag = languages[k];
return;
}
}
}
store = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
g_hash_table_iter_init (&iter, tags);
while (g_hash_table_iter_next (&iter, (gpointer *)&pair, NULL))
add_script (store, pair->script_index, pair->script_tag, pair->lang_index, pair->lang_tag);
g_hash_table_unref (tags);
gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (store),
feature_language_sort_func, NULL, NULL);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
GTK_SORT_ASCENDING);
gtk_combo_box_set_model (GTK_COMBO_BOX (priv->feature_language_combo), GTK_TREE_MODEL (store));
gtk_combo_box_set_active (GTK_COMBO_BOX (priv->feature_language_combo), 0);
*lang_tag = HB_OT_TAG_DEFAULT_LANGUAGE;
*script_tag = HB_OT_TAG_DEFAULT_SCRIPT;
}
typedef struct {
......@@ -2196,16 +2121,14 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
PangoFont *pango_font;
FT_Face ft_face;
hb_font_t *hb_font;
hb_tag_t script_tag = FT_MAKE_TAG('l','a','t','n');
hb_tag_t lang_tag = FT_MAKE_TAG('D','E','U',' ');
hb_tag_t script_tag;
hb_tag_t lang_tag;
guint script_index = 0;
guint lang_index = 0;
int i, j;
GList *l;
gboolean has_feature = FALSE;
gtk_widget_hide (priv->feature_language_combo);
for (l = priv->feature_items; l; l = l->next)
{
FeatureItem *item = l->data;
......@@ -2231,7 +2154,7 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
hb_face = hb_font_get_face (hb_font);
update_language_combo (fontchooser, hb_face);
find_language_and_script (fontchooser, hb_face, &lang_tag, &script_tag);
n_features = 0;
for (i = 0; i < 2; i++)
......@@ -2260,7 +2183,6 @@ gtk_font_chooser_widget_update_font_features (GtkFontChooserWidget *fontchooser)
has_feature = TRUE;
gtk_widget_show (item->top);
gtk_widget_show (gtk_widget_get_parent (item->top));
gtk_widget_show (priv->feature_language_combo);
update_feature_example (item, hb_face, script_tag, lang_tag, priv->font_desc);
......@@ -2332,33 +2254,6 @@ update_font_features (GtkFontChooserWidget *fontchooser)
gtk_font_chooser_widget_update_preview_attributes (fontchooser);
}
static void
update_language (GtkFontChooserWidget *fontchooser)
{
GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
GtkTreeIter iter;
if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->feature_language_combo), &iter))
{
GtkTreeModel *model;
PangoLanguage *lang;
hb_tag_t lang_tag;
model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->feature_language_combo));
gtk_tree_model_get (model, &iter,
3, &lang_tag,
-1);
lang = pango_language_from_string (hb_language_to_string (hb_ot_tag_to_language (lang_tag)));
if (priv->font_language != lang)
{
priv->font_language = lang;
g_object_notify (G_OBJECT (fontchooser), "language");
}
}
gtk_font_chooser_widget_update_preview_attributes (fontchooser);
}
#endif
static void
......@@ -2587,6 +2482,23 @@ gtk_font_chooser_widget_get_level (GtkFontChooserWidget *fontchooser)
return priv->level;
}
static void
gtk_font_chooser_widget_set_language (GtkFontChooserWidget *fontchooser,
const char *language)
{
GtkFontChooserWidgetPrivate *priv = fontchooser->priv;
PangoLanguage *lang;
lang = pango_language_from_string (language);
if (priv->language == lang)
return;
priv->language = lang;
g_object_notify (G_OBJECT (fontchooser), "language");
gtk_font_chooser_widget_update_preview_attributes (fontchooser);
}
static void
gtk_font_chooser_widget_iface_init (GtkFontChooserIface *iface)
{
......
<?xml version="1.0" encoding="UTF-8"?>
<interface domain="gtk30">
<interface domain="gtk40">
<!-- interface-requires gtk+ 3.10 -->
<object class="GtkListStore" id="model">
<columns>
......@@ -15,8 +15,8 @@
</object>
<object class="GtkTreeModelFilter" id="filter_model">
<property name="child-model">model</property>
<signal name="row-deleted" handler="row_deleted_cb" swapped="no"/>
<signal name="row-inserted" handler="row_inserted_cb" swapped="no"/>
<signal name="row-deleted" handler="rows_changed_cb" swapped="yes"/>
<signal name="row-inserted" handler="rows_changed_cb" swapped="yes"/>
</object>
<object class="GtkAdjustment" id="slider_adjustment">
<property name="upper">100</property>
......@@ -30,18 +30,15 @@
<property name="page-increment">10</property>
<signal name="value-changed" handler="size_change_cb" swapped="no"/>
</object>
<template class="GtkFontChooserWidget" parent="GtkBox">
<template class="GtkFontChooserWidget" parent="GtkWidget">
<child>
<object class="GtkStack" id="stack">
<property name="visible">1</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">1</property>
<object class="GtkGrid" id="grid">
<property name="row-spacing">6</property>
<property name="column-spacing">6</property>
<child>
<object class="GtkSearchEntry" id="search_entry">
<property name="visible">1</property>
<property name="can-focus">1</property>
<property name="hexpand">1</property>
<property name="activates-default">1</property>
......@@ -57,22 +54,18 @@
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkStack" id="list_stack">
<property name="visible">1</property>
<child>
<object class="GtkGrid">
<property name="visible">1</property>
<object class="GtkGrid" id="font_grid">
<property name="row-spacing">6</property>
<property name="column-spacing">6</property>
<child>
<object class="GtkScrolledWindow" id="list_scrolled_window">
<property name="width-request">400</property>
<property name="height-request">300</property>
<property name="visible">1</property>
<property name="can-focus">1</property>
<property name="hexpand">1</property>
<property name="vexpand">1</property>
......@@ -80,7 +73,6 @@
<property name="shadow-type">etched-in</property>