Merge branch 'scale' into 'master'

Honor widget scale  factor

See merge request Librem5/squeekboard!56
This commit is contained in:
David Boddie
2019-07-15 22:27:34 +00:00
4 changed files with 41 additions and 57 deletions

View File

@ -121,6 +121,8 @@ eek_gtk_keyboard_real_draw (GtkWidget *self,
eek_renderer_set_allocation_size (priv->renderer, eek_renderer_set_allocation_size (priv->renderer,
allocation.width, allocation.width,
allocation.height); allocation.height);
eek_renderer_set_scale_factor (priv->renderer,
gtk_widget_get_scale_factor (self));
} }
eek_renderer_render_keyboard (priv->renderer, cr); eek_renderer_render_keyboard (priv->renderer, cr);

View File

@ -30,66 +30,29 @@
G_DEFINE_TYPE (EekGtkRenderer, eek_gtk_renderer, EEK_TYPE_RENDERER); G_DEFINE_TYPE (EekGtkRenderer, eek_gtk_renderer, EEK_TYPE_RENDERER);
static cairo_surface_t *
pixbuf_to_cairo_surface (GdkPixbuf *pixbuf)
{
cairo_surface_t *dummy_surface;
cairo_pattern_t *pattern;
cairo_surface_t *surface;
cairo_t *cr;
dummy_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
cr = cairo_create (dummy_surface);
gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
pattern = cairo_get_source (cr);
cairo_pattern_get_surface (pattern, &surface);
cairo_surface_reference (surface);
cairo_destroy (cr);
cairo_surface_destroy (dummy_surface);
return surface;
}
static cairo_surface_t * static cairo_surface_t *
eek_gtk_renderer_real_get_icon_surface (EekRenderer *self, eek_gtk_renderer_real_get_icon_surface (EekRenderer *self,
const gchar *icon_name, const gchar *icon_name,
gint size) gint size,
gint scale)
{ {
GdkPixbuf *pixbuf;
GError *error = NULL; GError *error = NULL;
cairo_surface_t *surface; cairo_surface_t *surface;
gchar *path = g_strconcat("/sm/puri/squeekboard/icons/", icon_name, ".svg", NULL); surface = gtk_icon_theme_load_surface (gtk_icon_theme_get_default (),
icon_name,
pixbuf = gdk_pixbuf_new_from_resource_at_scale (path, size, size, size,
TRUE, &error); scale,
NULL,
if (pixbuf != NULL) 0,
goto found; &error);
else { if (surface == NULL) {
/* g_warning ("can't get icon pixbuf for %s: %s", path, error->message);*/ g_warning ("can't get icon surface for %s: %s",
g_error_free (error);
error = NULL;
}
g_free(path);
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
icon_name,
size,
0,
&error);
if (pixbuf == NULL) {
g_warning ("can't get icon pixbuf for %s: %s",
icon_name, icon_name,
error->message); error->message);
g_error_free (error); g_error_free (error);
return NULL; return NULL;
} }
found:
surface = pixbuf_to_cairo_surface (pixbuf);
g_object_unref (pixbuf);
return surface; return surface;
} }
@ -104,6 +67,9 @@ eek_gtk_renderer_class_init (EekGtkRendererClass *klass)
static void static void
eek_gtk_renderer_init (EekGtkRenderer *self) eek_gtk_renderer_init (EekGtkRenderer *self)
{ {
GtkIconTheme *theme = gtk_icon_theme_get_default ();
gtk_icon_theme_add_resource_path (theme, "/sm/puri/squeekboard/icons");
} }
EekRenderer * EekRenderer *

View File

@ -48,6 +48,7 @@ typedef struct _EekRendererPrivate
gdouble allocation_width; gdouble allocation_width;
gdouble allocation_height; gdouble allocation_height;
gdouble scale; gdouble scale;
gint scale_factor; /* the outputs scale factor */
PangoFontDescription *ascii_font; PangoFontDescription *ascii_font;
PangoFontDescription *font; PangoFontDescription *font;
@ -499,21 +500,21 @@ render_key (EekRenderer *self,
#define SCALE 0.4 #define SCALE 0.4
if (eek_symbol_get_icon_name (symbol)) { if (eek_symbol_get_icon_name (symbol)) {
gint scale = priv->scale_factor;
cairo_surface_t *icon_surface = cairo_surface_t *icon_surface =
eek_renderer_get_icon_surface (self, eek_renderer_get_icon_surface (self,
eek_symbol_get_icon_name (symbol), eek_symbol_get_icon_name (symbol),
MIN(bounds.width, bounds.height)); SCALE * MIN(bounds.width, bounds.height),
scale);
if (icon_surface) { if (icon_surface) {
gint width = cairo_image_surface_get_width (icon_surface); gint width = cairo_image_surface_get_width (icon_surface);
gint height = cairo_image_surface_get_height (icon_surface); gint height = cairo_image_surface_get_height (icon_surface);
cairo_save (cr); cairo_save (cr);
cairo_translate (cr, cairo_translate (cr,
(bounds.width - width * SCALE) / 2, (bounds.width - width / scale) / 2,
(bounds.height - height * SCALE) / 2); (bounds.height - height / scale) / 2);
cairo_rectangle (cr, 0, 0, width, height); cairo_rectangle (cr, 0, 0, width, height);
cairo_scale (cr, SCALE, SCALE);
cairo_clip (cr); cairo_clip (cr);
/* Draw the shape of the icon using the foreground color */ /* Draw the shape of the icon using the foreground color */
cairo_set_source_rgba (cr, foreground.red, cairo_set_source_rgba (cr, foreground.red,
@ -844,6 +845,7 @@ eek_renderer_init (EekRenderer *self)
priv->allocation_width = 0.0; priv->allocation_width = 0.0;
priv->allocation_height = 0.0; priv->allocation_height = 0.0;
priv->scale = 1.0; priv->scale = 1.0;
priv->scale_factor = 1;
priv->font = NULL; priv->font = NULL;
priv->outline_surface_cache = priv->outline_surface_cache =
g_hash_table_new_full (g_direct_hash, g_hash_table_new_full (g_direct_hash,
@ -1028,6 +1030,15 @@ eek_renderer_get_scale (EekRenderer *renderer)
return priv->scale; return priv->scale;
} }
void
eek_renderer_set_scale_factor (EekRenderer *renderer, gint scale)
{
g_return_if_fail (EEK_IS_RENDERER(renderer));
EekRendererPrivate *priv = eek_renderer_get_instance_private (renderer);
priv->scale_factor = scale;
}
PangoLayout * PangoLayout *
eek_renderer_create_pango_layout (EekRenderer *renderer) eek_renderer_create_pango_layout (EekRenderer *renderer)
{ {
@ -1071,7 +1082,8 @@ eek_renderer_render_key_outline (EekRenderer *renderer,
cairo_surface_t * cairo_surface_t *
eek_renderer_get_icon_surface (EekRenderer *renderer, eek_renderer_get_icon_surface (EekRenderer *renderer,
const gchar *icon_name, const gchar *icon_name,
gint size) gint size,
gint scale)
{ {
EekRendererClass *klass; EekRendererClass *klass;
@ -1079,7 +1091,7 @@ eek_renderer_get_icon_surface (EekRenderer *renderer,
klass = EEK_RENDERER_GET_CLASS(renderer); klass = EEK_RENDERER_GET_CLASS(renderer);
if (klass->get_icon_surface) if (klass->get_icon_surface)
return klass->get_icon_surface (renderer, icon_name, size); return klass->get_icon_surface (renderer, icon_name, size, scale);
return NULL; return NULL;
} }

View File

@ -58,7 +58,8 @@ struct _EekRendererClass
cairo_surface_t *(* get_icon_surface) (EekRenderer *self, cairo_surface_t *(* get_icon_surface) (EekRenderer *self,
const gchar *icon_name, const gchar *icon_name,
gint size); gint size,
gint scale);
/*< private >*/ /*< private >*/
/* padding */ /* padding */
@ -81,6 +82,8 @@ void eek_renderer_get_key_bounds (EekRenderer *renderer,
gboolean rotate); gboolean rotate);
gdouble eek_renderer_get_scale (EekRenderer *renderer); gdouble eek_renderer_get_scale (EekRenderer *renderer);
void eek_renderer_set_scale_factor (EekRenderer *renderer,
gint scale);
PangoLayout *eek_renderer_create_pango_layout PangoLayout *eek_renderer_create_pango_layout
(EekRenderer *renderer); (EekRenderer *renderer);
@ -103,7 +106,8 @@ void eek_renderer_render_key (EekRenderer *renderer,
cairo_surface_t *eek_renderer_get_icon_surface (EekRenderer *renderer, cairo_surface_t *eek_renderer_get_icon_surface (EekRenderer *renderer,
const gchar *icon_name, const gchar *icon_name,
gint size); gint size,
gint scale);
void eek_renderer_render_keyboard (EekRenderer *renderer, void eek_renderer_render_keyboard (EekRenderer *renderer,
cairo_t *cr); cairo_t *cr);