glyphpaintable: Smart defaults

Default to showing the glyph named 'icon0'
or 'A'. For single-glyph icon fonts, this
should avoid the need to specify a glyph
explicitly.
This commit is contained in:
Matthias Clasen
2023-01-22 11:36:01 -05:00
parent 5c270a7b0e
commit 3ec1ca13e0
4 changed files with 71 additions and 27 deletions

View File

@@ -365,6 +365,19 @@ update_font (GlyphPaintable *self)
}
}
static void
update_glyph (GlyphPaintable *self)
{
hb_codepoint_t glyph;
if (hb_font_get_glyph_from_name (self->font, "icon0", -1, &glyph) ||
hb_font_get_glyph_from_name (self->font, "A", -1, &glyph))
{
self->glyph = glyph;
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_GLYPH]);
}
}
void
glyph_paintable_set_face (GlyphPaintable *self,
hb_face_t *face)
@@ -377,8 +390,10 @@ glyph_paintable_set_face (GlyphPaintable *self,
self->face = hb_face_reference (face);
update_font (self);
update_glyph (self);
gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FACE]);
}
@@ -399,6 +414,7 @@ glyph_paintable_set_glyph (GlyphPaintable *self,
self->glyph = glyph;
gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_GLYPH]);
}
@@ -442,6 +458,7 @@ glyph_paintable_set_variations (GlyphPaintable *self,
update_font (self);
gdk_paintable_invalidate_contents (GDK_PAINTABLE (self));
gdk_paintable_invalidate_size (GDK_PAINTABLE (self));
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_VARIATIONS]);
}

View File

@@ -4,6 +4,11 @@
#include <hb-ot.h>
#include <hb-gobject.h>
static void glyph_picker_set_face (GlyphPicker *self,
hb_face_t *face);
static void glyph_picker_set_glyph (GlyphPicker *self,
unsigned int glyph);
enum {
PROP_FACE = 1,
PROP_GLYPH,
@@ -105,6 +110,18 @@ update_font (GlyphPicker *self)
value_changed (self);
}
static void
update_glyph (GlyphPicker *self)
{
hb_codepoint_t glyph;
if (hb_font_get_glyph_from_name (self->font, "icon0", -1, &glyph) ||
hb_font_get_glyph_from_name (self->font, "A", -1, &glyph))
{
glyph_picker_set_glyph (self, glyph);
}
}
static void
glyph_picker_set_property (GObject *object,
unsigned int prop_id,
@@ -116,25 +133,11 @@ glyph_picker_set_property (GObject *object,
switch (prop_id)
{
case PROP_FACE:
{
hb_face_t *face = g_value_get_boxed (value);
if (self->face == face)
return;
if (self->face)
hb_face_destroy (self->face);
self->face = face;
if (self->face)
hb_face_reference (self->face);
update_bounds (self);
update_font (self);
}
glyph_picker_set_face (self, (hb_face_t *) g_value_get_boxed (value));
break;
case PROP_GLYPH:
gtk_spin_button_set_value (self->spin, g_value_get_uint (value));
glyph_picker_set_glyph (self, g_value_get_uint (value));
break;
default:
@@ -182,12 +185,12 @@ glyph_picker_class_init (GlyphPickerClass *class)
properties[PROP_FACE] =
g_param_spec_boxed ("face", "", "",
HB_GOBJECT_TYPE_FACE,
G_PARAM_READWRITE);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
properties[PROP_GLYPH] =
g_param_spec_uint ("glyph", "", "",
0, G_MAXUINT, 0,
G_PARAM_READWRITE);
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
properties[PROP_GLYPH_NAME] =
g_param_spec_string ("glyph-name", "", "",
@@ -199,3 +202,35 @@ glyph_picker_class_init (GlyphPickerClass *class)
gtk_widget_class_set_layout_manager_type (GTK_WIDGET_CLASS (class), GTK_TYPE_BIN_LAYOUT);
gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (class), "glyphpicker");
}
static void
glyph_picker_set_face (GlyphPicker *self,
hb_face_t *face)
{
if (self->face == face)
return;
if (self->face)
hb_face_destroy (self->face);
self->face = face;
if (self->face)
hb_face_reference (self->face);
update_bounds (self);
update_font (self);
update_glyph (self);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_FACE]);
}
static void
glyph_picker_set_glyph (GlyphPicker *self,
unsigned int glyph)
{
if (get_glyph (self) == glyph)
return;
gtk_spin_button_set_value (self->spin, glyph);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_GLYPH]);
}

View File

@@ -56,13 +56,6 @@ set_font_from_path (GdkPaintable *paintable,
hb_face_destroy (face);
}
static void
set_glyph (GtkWidget *widget,
hb_codepoint_t glyph)
{
g_object_set (GLYPH_PICKER (widget), "glyph", glyph, NULL);
}
static void
open_response_cb (GObject *source,
GAsyncResult *result,
@@ -244,7 +237,6 @@ do_paintable_glyph (GtkWidget *do_widget)
create_reset_action ();
set_font_from_path (paintable, "/usr/share/fonts/abattis-cantarell-vf-fonts/Cantarell-VF.otf");
set_glyph (glyph_picker, 1);
g_object_unref (builder);
g_object_unref (scope);

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GlyphPaintable" id="paintable">
<property name="glyph" bind-source="glyph_picker" bind-property="glyph"/>
<property name="glyph" bind-source="glyph_picker" bind-property="glyph" bind-flags="bidirectional | sync-create"/>
<property name="variations" bind-source="font_variations" bind-property="variations"/>
<property name="palette-index" bind-source="font_colors" bind-property="palette-index"/>
<property name="custom-colors" bind-source="font_colors" bind-property="custom-colors"/>