inspector: Use the right font for the fps overlay
Initialize the font only when we have a widget that provides it. That way we don't pick a terrible default font, but the widget's preferred one.
This commit is contained in:
committed by
Matthias Clasen
parent
84343d30fb
commit
64ecfd656e
@@ -31,21 +31,24 @@
|
||||
/* duration when fade is finished in us */
|
||||
#define GDK_FPS_OVERLAY_FADE_DURATION (500 * 1000)
|
||||
|
||||
typedef struct _GtkFpsInfo {
|
||||
typedef struct _GtkFpsInfo GtkFpsInfo;
|
||||
|
||||
struct _GtkFpsInfo
|
||||
{
|
||||
PangoFont *font;
|
||||
PangoGlyphString *glyphs;
|
||||
PangoGlyphString *digits;
|
||||
int width, height, baseline;
|
||||
|
||||
gint64 last_frame;
|
||||
GskRenderNode *last_node;
|
||||
} GtkFpsInfo;
|
||||
};
|
||||
|
||||
struct _GtkFpsOverlay
|
||||
{
|
||||
GtkInspectorOverlay parent_instance;
|
||||
|
||||
GHashTable *infos; /* GtkWidget => GtkFpsInfo */
|
||||
|
||||
PangoFont *font;
|
||||
PangoGlyphString *glyphs;
|
||||
PangoGlyphString *digits;
|
||||
int width, height, baseline;
|
||||
};
|
||||
|
||||
struct _GtkFpsOverlayClass
|
||||
@@ -60,11 +63,55 @@ gtk_fps_info_free (gpointer data)
|
||||
{
|
||||
GtkFpsInfo *info = data;
|
||||
|
||||
g_clear_pointer (&info->glyphs, pango_glyph_string_free);
|
||||
g_clear_pointer (&info->digits, pango_glyph_string_free);
|
||||
g_clear_object (&info->font);
|
||||
|
||||
gsk_render_node_unref (info->last_node);
|
||||
|
||||
g_free (info);
|
||||
}
|
||||
|
||||
static GtkFpsInfo *
|
||||
gtk_fps_info_new (GtkWidget *widget)
|
||||
{
|
||||
PangoLayout *layout;
|
||||
PangoLayoutIter *iter;
|
||||
PangoLayoutRun *run;
|
||||
PangoAttrList *attrs;
|
||||
GtkFpsInfo *info;
|
||||
|
||||
info = g_new0 (GtkFpsInfo, 1);
|
||||
|
||||
layout = gtk_widget_create_pango_layout (widget, "0000.00 fps");
|
||||
attrs = pango_attr_list_new ();
|
||||
pango_attr_list_insert (attrs, pango_attr_font_features_new ("tnum=1"));
|
||||
pango_layout_set_attributes (layout, attrs);
|
||||
pango_attr_list_unref (attrs);
|
||||
pango_layout_get_pixel_size (layout, &info->width, &info->height);
|
||||
|
||||
iter = pango_layout_get_iter (layout);
|
||||
info->baseline = pango_layout_iter_get_baseline (iter) / (double) PANGO_SCALE;
|
||||
run = pango_layout_iter_get_run_readonly (iter);
|
||||
|
||||
info->glyphs = pango_glyph_string_copy (run->glyphs);
|
||||
info->font = g_object_ref (run->item->analysis.font);
|
||||
|
||||
pango_layout_iter_free (iter);
|
||||
|
||||
pango_layout_set_text (layout, "0123456789", -1);
|
||||
|
||||
iter = pango_layout_get_iter (layout);
|
||||
run = pango_layout_iter_get_run_readonly (iter);
|
||||
|
||||
info->digits = pango_glyph_string_copy (run->glyphs);
|
||||
|
||||
pango_layout_iter_free (iter);
|
||||
g_object_unref (layout);
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
static double
|
||||
gtk_fps_overlay_get_fps (GtkWidget *widget)
|
||||
{
|
||||
@@ -105,7 +152,7 @@ gtk_fps_overlay_snapshot (GtkInspectorOverlay *overlay,
|
||||
info = g_hash_table_lookup (self->infos, widget);
|
||||
if (info == NULL)
|
||||
{
|
||||
info = g_new0 (GtkFpsInfo, 1);
|
||||
info = gtk_fps_info_new (widget);
|
||||
g_hash_table_insert (self->infos, widget, info);
|
||||
}
|
||||
if (info->last_node != node)
|
||||
@@ -149,13 +196,13 @@ gtk_fps_overlay_snapshot (GtkInspectorOverlay *overlay,
|
||||
|
||||
gtk_snapshot_save (snapshot);
|
||||
if (has_bounds)
|
||||
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (bounds.origin.x + bounds.size.width - self->width, bounds.origin.y));
|
||||
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (bounds.origin.x + bounds.size.width - info->width, bounds.origin.y));
|
||||
|
||||
if (overlay_opacity < 1.0)
|
||||
gtk_snapshot_push_opacity (snapshot, overlay_opacity);
|
||||
gtk_snapshot_append_color (snapshot,
|
||||
&(GdkRGBA) { 0, 0, 0, 0.5 },
|
||||
&GRAPHENE_RECT_INIT (-1, -1, self->width + 2, self->height + 2));
|
||||
&GRAPHENE_RECT_INIT (-1, -1, info->width + 2, info->height + 2));
|
||||
|
||||
fps = gtk_fps_overlay_get_fps (widget);
|
||||
if (fps != 0.0)
|
||||
@@ -167,15 +214,15 @@ gtk_fps_overlay_snapshot (GtkInspectorOverlay *overlay,
|
||||
for (int i = 0; i < 7; i++)
|
||||
{
|
||||
if (g_ascii_isdigit (fps_string[i]))
|
||||
self->glyphs->glyphs[i].glyph = self->digits->glyphs[fps_string[i] - '0'].glyph;
|
||||
info->glyphs->glyphs[i].glyph = info->digits->glyphs[fps_string[i] - '0'].glyph;
|
||||
else if (fps_string[i] == ' ')
|
||||
self->glyphs->glyphs[i].glyph = PANGO_GLYPH_EMPTY;
|
||||
info->glyphs->glyphs[i].glyph = PANGO_GLYPH_EMPTY;
|
||||
}
|
||||
|
||||
fps_node = gsk_text_node_new (self->font,
|
||||
self->glyphs,
|
||||
fps_node = gsk_text_node_new (info->font,
|
||||
info->glyphs,
|
||||
&(GdkRGBA) { 1, 1, 1, 1 },
|
||||
&GRAPHENE_POINT_INIT (0, self->baseline));
|
||||
&GRAPHENE_POINT_INIT (0, info->baseline));
|
||||
gtk_snapshot_append_node (snapshot, fps_node);
|
||||
gsk_render_node_unref (fps_node);
|
||||
}
|
||||
@@ -206,10 +253,6 @@ gtk_fps_overlay_dispose (GObject *object)
|
||||
|
||||
g_hash_table_unref (self->infos);
|
||||
|
||||
g_clear_pointer (&self->glyphs, pango_glyph_string_free);
|
||||
g_clear_pointer (&self->digits, pango_glyph_string_free);
|
||||
g_clear_object (&self->font);
|
||||
|
||||
G_OBJECT_CLASS (gtk_fps_overlay_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
@@ -228,44 +271,7 @@ gtk_fps_overlay_class_init (GtkFpsOverlayClass *klass)
|
||||
static void
|
||||
gtk_fps_overlay_init (GtkFpsOverlay *self)
|
||||
{
|
||||
PangoContext *context;
|
||||
PangoLayout *layout;
|
||||
PangoLayoutIter *iter;
|
||||
PangoLayoutRun *run;
|
||||
PangoAttrList *attrs;
|
||||
|
||||
self->infos = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, gtk_fps_info_free);
|
||||
|
||||
context = pango_font_map_create_context (pango_cairo_font_map_get_default ());
|
||||
pango_context_set_language (context, gtk_get_default_language ());
|
||||
|
||||
layout = pango_layout_new (context);
|
||||
pango_layout_set_text (layout, "0000.00 fps", -1);
|
||||
attrs = pango_attr_list_new ();
|
||||
pango_attr_list_insert (attrs, pango_attr_font_features_new ("tnum=1"));
|
||||
pango_layout_set_attributes (layout, attrs);
|
||||
pango_attr_list_unref (attrs);
|
||||
pango_layout_get_pixel_size (layout, &self->width, &self->height);
|
||||
|
||||
iter = pango_layout_get_iter (layout);
|
||||
self->baseline = pango_layout_iter_get_baseline (iter) / (double) PANGO_SCALE;
|
||||
run = pango_layout_iter_get_run_readonly (iter);
|
||||
|
||||
self->glyphs = pango_glyph_string_copy (run->glyphs);
|
||||
self->font = g_object_ref (run->item->analysis.font);
|
||||
|
||||
pango_layout_iter_free (iter);
|
||||
|
||||
pango_layout_set_text (layout, "0123456789", -1);
|
||||
|
||||
iter = pango_layout_get_iter (layout);
|
||||
run = pango_layout_iter_get_run_readonly (iter);
|
||||
|
||||
self->digits = pango_glyph_string_copy (run->glyphs);
|
||||
|
||||
pango_layout_iter_free (iter);
|
||||
g_object_unref (layout);
|
||||
g_object_unref (context);
|
||||
}
|
||||
|
||||
GtkInspectorOverlay *
|
||||
|
||||
Reference in New Issue
Block a user