Merge branch 'nieldsg/font-filter' into 'main'

gtk: Introduce private GtkFontFilter helper

See merge request GNOME/gtk!7262
This commit is contained in:
Matthias Clasen 2024-05-21 01:22:38 +00:00
commit 4440ce874e
5 changed files with 360 additions and 95 deletions

View File

@ -23,6 +23,7 @@
#include "deprecated/gtkfontchooserwidget.h"
#include "gtkfontchooserwidgetprivate.h"
#include "gtkfontfilterprivate.h"
#include "gtkadjustment.h"
#include "gtkbuildable.h"
@ -108,10 +109,10 @@ struct _GtkFontChooserWidget
GtkWidget *family_face_list;
GtkWidget *list_stack;
GtkSingleSelection *selection;
GtkCustomFilter *custom_filter;
GtkCustomFilter *user_filter;
GtkCustomFilter *multi_filter;
GtkFilterListModel *filter_model;
GtkCustomFilter *custom_filter;
GtkFontFilter *user_filter;
GtkCustomFilter *multi_filter;
GtkFilterListModel *filter_model;
GtkWidget *preview;
GtkWidget *preview2;
@ -130,16 +131,12 @@ struct _GtkFontChooserWidget
GtkWidget *axis_grid;
GtkWidget *feature_box;
GtkFrame *language_button;
GtkCheckButton *language_button;
GtkFrame *language_frame;
GtkWidget *language_list;
GtkStringList *languages;
GHashTable *language_table;
PangoLanguage *filter_language;
gboolean filter_by_language;
gboolean filter_by_monospace;
PangoFontMap *font_map;
PangoFontDescription *font_desc;
@ -348,77 +345,29 @@ output_cb (GtkSpinButton *spin,
return TRUE;
}
static gboolean
user_filter_cb (gpointer item,
gpointer data)
static void
update_filter_language (GtkFontChooserWidget *self)
{
GtkFontChooserWidget *self = GTK_FONT_CHOOSER_WIDGET (data);
PangoFontFamily *family;
PangoFontFace *face;
gboolean should_filter_language;
if (PANGO_IS_FONT_FAMILY (item))
should_filter_language = gtk_check_button_get_active (self->language_button);
if (!should_filter_language)
{
family = item;
face = pango_font_family_get_face (family, NULL);
_gtk_font_filter_set_language (self->user_filter, NULL);
}
else
{
face = PANGO_FONT_FACE (item);
family = pango_font_face_get_family (face);
GtkSelectionModel *model;
gpointer obj;
PangoLanguage *language = NULL;
model = gtk_list_view_get_model (GTK_LIST_VIEW (self->language_list));
obj = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (model));
if (obj)
language = pango_language_from_string (gtk_string_object_get_string (obj));
_gtk_font_filter_set_language (self->user_filter, language);
}
if (self->filter_by_monospace &&
!pango_font_family_is_monospace (family))
return FALSE;
if (self->filter_by_language &&
self->filter_language)
{
PangoFontDescription *desc;
PangoContext *context;
PangoFont *font;
gboolean ret;
PangoLanguage **langs;
desc = pango_font_face_describe (face);
pango_font_description_set_size (desc, 20);
context = gtk_widget_get_pango_context (GTK_WIDGET (self));
font = pango_context_load_font (context, desc);
ret = FALSE;
langs = pango_font_get_languages (font);
if (langs)
{
for (int i = 0; langs[i]; i++)
{
if (langs[i] == self->filter_language)
{
ret = TRUE;
break;
}
}
}
g_object_unref (font);
pango_font_description_free (desc);
return ret;
}
return TRUE;
}
static void
monospace_check_changed (GtkCheckButton *check,
GParamSpec *pspec,
GtkFontChooserWidget *self)
{
self->filter_by_monospace = gtk_check_button_get_active (check);
gtk_filter_changed (GTK_FILTER (self->user_filter),
self->filter_by_monospace ? GTK_FILTER_CHANGE_MORE_STRICT
: GTK_FILTER_CHANGE_LESS_STRICT);
}
static void
@ -426,10 +375,7 @@ language_check_changed (GtkCheckButton *check,
GParamSpec *pspec,
GtkFontChooserWidget *self)
{
self->filter_by_language = gtk_check_button_get_active (check);
gtk_filter_changed (GTK_FILTER (self->user_filter),
self->filter_by_language ? GTK_FILTER_CHANGE_MORE_STRICT
: GTK_FILTER_CHANGE_LESS_STRICT);
update_filter_language (self);
}
static void
@ -551,6 +497,7 @@ maybe_update_preview_text (GtkFontChooserWidget *self,
{
PangoContext *context;
PangoFont *font;
PangoLanguage *filter_lang;
const char *sample;
PangoLanguage **languages;
GHashTable *langs = NULL;
@ -564,9 +511,10 @@ maybe_update_preview_text (GtkFontChooserWidget *self,
if (self->preview_text_set)
return;
if (self->filter_by_language && self->filter_language)
filter_lang = _gtk_font_filter_get_language (self->user_filter);
if (filter_lang != NULL)
{
sample = pango_language_get_sample_string (self->filter_language);
sample = pango_language_get_sample_string (filter_lang);
gtk_font_chooser_widget_set_preview_text (self, sample);
return;
}
@ -840,15 +788,23 @@ gtk_font_chooser_widget_unmap (GtkWidget *widget)
static void
gtk_font_chooser_widget_root (GtkWidget *widget)
{
GtkFontChooserWidget *self = GTK_FONT_CHOOSER_WIDGET (widget);
GTK_WIDGET_CLASS (gtk_font_chooser_widget_parent_class)->root (widget);
g_signal_connect_swapped (gtk_widget_get_root (widget), "notify::focus-widget",
G_CALLBACK (update_key_capture), widget);
_gtk_font_filter_set_pango_context (self->user_filter,
gtk_widget_get_pango_context (widget));
}
static void
gtk_font_chooser_widget_unroot (GtkWidget *widget)
{
GtkFontChooserWidget *self = GTK_FONT_CHOOSER_WIDGET (widget);
_gtk_font_filter_set_pango_context (self->user_filter,
gtk_widget_get_pango_context (widget));
g_signal_handlers_disconnect_by_func (gtk_widget_get_root (widget),
update_key_capture, widget);
@ -880,6 +836,7 @@ gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
GParamSpec *pspec;
g_type_ensure (G_TYPE_THEMED_ICON);
g_type_ensure (GTK_TYPE_FONT_FILTER);
widget_class->root = gtk_font_chooser_widget_root;
widget_class->unroot = gtk_font_chooser_widget_unroot;
@ -945,7 +902,6 @@ gtk_font_chooser_widget_class_init (GtkFontChooserWidgetClass *klass)
gtk_widget_class_bind_template_callback (widget_class, output_cb);
gtk_widget_class_bind_template_callback (widget_class, selection_changed_cb);
gtk_widget_class_bind_template_callback (widget_class, resize_by_scroll_cb);
gtk_widget_class_bind_template_callback (widget_class, monospace_check_changed);
gtk_widget_class_bind_template_callback (widget_class, language_check_changed);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
@ -1212,17 +1168,7 @@ language_selection_changed (GtkSelectionModel *model,
guint n_items,
GtkFontChooserWidget *self)
{
gpointer obj;
obj = gtk_single_selection_get_selected_item (GTK_SINGLE_SELECTION (model));
if (obj)
self->filter_language = pango_language_from_string (gtk_string_object_get_string (obj));
else
self->filter_language = NULL;
if (self->filter_by_language)
gtk_filter_changed (GTK_FILTER (self->user_filter), GTK_FILTER_CHANGE_DIFFERENT);
update_filter_language (self);
}
static gboolean
@ -1293,8 +1239,6 @@ gtk_font_chooser_widget_init (GtkFontChooserWidget *self)
gtk_font_chooser_widget_take_font_desc (self, NULL);
gtk_custom_filter_set_filter_func (self->user_filter, user_filter_cb, self, NULL);
setup_language_list (self);
}

272
gtk/gtkfontfilter.c Normal file
View File

@ -0,0 +1,272 @@
/* GTK - The GIMP Toolkit
* gtkfontfilter.c:
* Copyright (C) 2024 Niels De Graef <nielsdegraef@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <glib/gprintf.h>
#include <string.h>
#include "gtkfontfilterprivate.h"
#include "gtkprivate.h"
struct _GtkFontFilter
{
GtkFilter parent_instance;
PangoContext *pango_context;
gboolean monospace;
PangoLanguage *language;
};
enum {
PROP_0,
PROP_PANGO_CONTEXT,
PROP_MONOSPACE,
PROP_LANGUAGE,
N_PROPS
};
static GParamSpec *properties[N_PROPS] = { NULL, };
G_DEFINE_TYPE (GtkFontFilter, gtk_font_filter, GTK_TYPE_FILTER)
static void
gtk_font_filter_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkFontFilter *self = GTK_FONT_FILTER (object);
switch (prop_id)
{
case PROP_PANGO_CONTEXT:
_gtk_font_filter_set_pango_context (self, g_value_get_object (value));
break;
case PROP_MONOSPACE:
_gtk_font_filter_set_monospace (self, g_value_get_boolean (value));
break;
case PROP_LANGUAGE:
_gtk_font_filter_set_language (self, g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_font_filter_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkFontFilter *self = GTK_FONT_FILTER (object);
switch (prop_id)
{
case PROP_PANGO_CONTEXT:
g_value_set_object (value, self->pango_context);
break;
case PROP_MONOSPACE:
g_value_set_boolean (value, _gtk_font_filter_get_monospace (self));
break;
case PROP_LANGUAGE:
g_value_set_boxed (value, _gtk_font_filter_get_language (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gtk_font_filter_match (GtkFilter *filter,
gpointer item)
{
GtkFontFilter *self = GTK_FONT_FILTER (filter);
PangoFontFamily *family;
PangoFontFace *face;
if (PANGO_IS_FONT_FAMILY (item))
{
family = item;
face = pango_font_family_get_face (family, NULL);
}
else
{
face = PANGO_FONT_FACE (item);
family = pango_font_face_get_family (face);
}
if (self->monospace &&
!pango_font_family_is_monospace (family))
return FALSE;
if (self->language)
{
PangoFontDescription *desc;
PangoFont *font;
PangoLanguage **langs;
gboolean ret = FALSE;
desc = pango_font_face_describe (face);
pango_font_description_set_size (desc, 20);
font = pango_context_load_font (self->pango_context, desc);
langs = pango_font_get_languages (font);
if (langs)
{
for (int i = 0; langs[i]; i++)
{
if (langs[i] == self->language)
{
ret = TRUE;
break;
}
}
}
g_object_unref (font);
pango_font_description_free (desc);
return ret;
}
return TRUE;
}
static GtkFilterMatch
gtk_font_filter_get_strictness (GtkFilter *filter)
{
GtkFontFilter *self = GTK_FONT_FILTER (filter);
if (!self->monospace && self->language == NULL)
return GTK_FILTER_MATCH_ALL;
return GTK_FILTER_MATCH_SOME;
}
static void
gtk_font_filter_class_init (GtkFontFilterClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GtkFilterClass *filter_class = GTK_FILTER_CLASS (klass);
filter_class->match = gtk_font_filter_match;
filter_class->get_strictness = gtk_font_filter_get_strictness;
gobject_class->set_property = gtk_font_filter_set_property;
gobject_class->get_property = gtk_font_filter_get_property;
properties[PROP_PANGO_CONTEXT] =
g_param_spec_object ("pango-context", NULL, NULL,
PANGO_TYPE_CONTEXT,
GTK_PARAM_READWRITE);
properties[PROP_MONOSPACE] =
g_param_spec_boolean ("monospace", NULL, NULL,
FALSE,
GTK_PARAM_READWRITE);
properties[PROP_LANGUAGE] =
g_param_spec_boxed ("language", NULL, NULL,
PANGO_TYPE_LANGUAGE,
GTK_PARAM_READWRITE);
g_object_class_install_properties (gobject_class, N_PROPS, properties);
}
static void
gtk_font_filter_init (GtkFontFilter *self)
{
}
void
_gtk_font_filter_set_pango_context (GtkFontFilter *self,
PangoContext *context)
{
g_return_if_fail (GTK_IS_FONT_FILTER (self));
g_return_if_fail (PANGO_IS_CONTEXT (context));
if (self->pango_context == context)
return;
self->pango_context = context;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PANGO_CONTEXT]);
}
gboolean
_gtk_font_filter_get_monospace (GtkFontFilter *self)
{
g_return_val_if_fail (GTK_IS_FONT_FILTER (self), FALSE);
return self->monospace;
}
void
_gtk_font_filter_set_monospace (GtkFontFilter *self,
gboolean monospace)
{
g_return_if_fail (GTK_IS_FONT_FILTER (self));
if (self->monospace == monospace)
return;
self->monospace = monospace;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MONOSPACE]);
gtk_filter_changed (GTK_FILTER (self),
monospace ? GTK_FILTER_CHANGE_MORE_STRICT
: GTK_FILTER_CHANGE_LESS_STRICT);
}
PangoLanguage *
_gtk_font_filter_get_language (GtkFontFilter *self)
{
g_return_val_if_fail (GTK_IS_FONT_FILTER (self), NULL);
return self->language;
}
void
_gtk_font_filter_set_language (GtkFontFilter *self,
PangoLanguage *lang)
{
GtkFilterChange filter_change = GTK_FILTER_CHANGE_DIFFERENT;
g_return_if_fail (GTK_IS_FONT_FILTER (self));
if (self->language == lang)
return;
if (lang == NULL || self->language == NULL)
{
filter_change = (lang != NULL) ? GTK_FILTER_CHANGE_MORE_STRICT
: GTK_FILTER_CHANGE_LESS_STRICT;
}
self->language = lang;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_LANGUAGE]);
gtk_filter_changed (GTK_FILTER (self), filter_change);
}
GtkFilter *
_gtk_font_filter_new (void)
{
return g_object_new (GTK_TYPE_FONT_FILTER, NULL);
}

View File

@ -0,0 +1,46 @@
/* GTK - The GIMP Toolkit
* gtkfontfilterprivate.h:
* Copyright (C) 2024 Niels De Graef <nielsdegraef@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gtk/gtkfilter.h>
G_BEGIN_DECLS
#define GTK_TYPE_FONT_FILTER (gtk_font_filter_get_type ())
G_DECLARE_FINAL_TYPE (GtkFontFilter, gtk_font_filter,
GTK, FONT_FILTER,
GtkFilter)
GtkFilter * _gtk_font_filter_new (void);
void _gtk_font_filter_set_pango_context (GtkFontFilter *self,
PangoContext *context);
gboolean _gtk_font_filter_get_monospace (GtkFontFilter *self);
void _gtk_font_filter_set_monospace (GtkFontFilter *self,
gboolean monospace);
PangoLanguage * _gtk_font_filter_get_language (GtkFontFilter *self);
void _gtk_font_filter_set_language (GtkFontFilter *self,
PangoLanguage *language);
G_END_DECLS

View File

@ -113,6 +113,7 @@ gtk_private_sources = files([
'gtkfilechoosercell.c',
'gtkfilesystemmodel.c',
'gtkfilethumbnail.c',
'gtkfontfilter.c',
'gtkgizmo.c',
'gtkiconcache.c',
'gtkiconcachevalidator.c',
@ -412,6 +413,7 @@ gtk_public_sources += gtk_print_sources
gtk_private_type_headers = files([
'gtkcsstypesprivate.h',
'gtkfontfilterprivate.h',
'gtktexthandleprivate.h',
'gtkplacessidebarprivate.h',
])

View File

@ -23,7 +23,9 @@
<object class="GtkCustomFilter" id="custom_filter"/>
</child>
<child>
<object class="GtkCustomFilter" id="user_filter"/>
<object class="GtkFontFilter" id="user_filter">
<property name="monospace" bind-source="monospace_button" bind-property="active"/>
</object>
</child>
</object>
</property>
@ -97,7 +99,6 @@
<child>
<object class="GtkCheckButton" id="monospace_button">
<property name="label" translatable="yes">Monospace</property>
<signal name="notify::active" handler="monospace_check_changed"/>
</object>
</child>
<child>