[gtksourceview] pixbuf: handle creating pixbufs for HiDPI resolutions



commit b58ab9446e1c2c14bfe74ae6eea0d59e600385b1
Author: Christian Hergert <christian hergert me>
Date:   Mon Aug 31 15:22:35 2015 -0700

    pixbuf: handle creating pixbufs for HiDPI resolutions
    
    Under certain HiDPI situations, 1x icons were loaded and rendered with
    the scale factor applied. As expected, this resulted in blurry icons.
    
    Of note, were blurry icons in the GtkSourceGutterRendererPixbuf.
    
    This is not the most efficient, but is not terribly different from what
    is happening today. We create a new cairo_surface_t for the pixbuf
    manually, with the scale applied. gdk_cairo_set_source_pixbuf() also
    does this, but without the scale.
    
    We could optimize this in the future by making our pixbuf helper return
    cairo_surface_t* rather than GdkPixbuf*.

 gtksourceview/gtksourcegutterrendererpixbuf.c |   24 +++++++++++++++++++++++-
 gtksourceview/gtksourcepixbufhelper.c         |   11 +++++++----
 2 files changed, 30 insertions(+), 5 deletions(-)
---
diff --git a/gtksourceview/gtksourcegutterrendererpixbuf.c b/gtksourceview/gtksourcegutterrendererpixbuf.c
index 47e5636..f737c48 100644
--- a/gtksourceview/gtksourcegutterrendererpixbuf.c
+++ b/gtksourceview/gtksourcegutterrendererpixbuf.c
@@ -87,9 +87,11 @@ gutter_renderer_pixbuf_draw (GtkSourceGutterRenderer      *renderer,
        gfloat yalign;
        GtkSourceGutterRendererAlignmentMode mode;
        GtkTextView *view;
+       gint scale;
        gint x = 0;
        gint y = 0;
        GdkPixbuf *pixbuf;
+       cairo_surface_t *surface;
 
        /* Chain up to draw background */
        if (GTK_SOURCE_GUTTER_RENDERER_CLASS (gtk_source_gutter_renderer_pixbuf_parent_class)->draw != NULL)
@@ -117,6 +119,22 @@ gutter_renderer_pixbuf_draw (GtkSourceGutterRenderer      *renderer,
        width = gdk_pixbuf_get_width (pixbuf);
        height = gdk_pixbuf_get_height (pixbuf);
 
+       /*
+        * We might have gotten a pixbuf back from the helper that will allow
+        * us to render for HiDPI. If we detect this, we pretend that we got a
+        * different size back and then gdk_cairo_surface_create_from_pixbuf()
+        * will take care of the rest.
+        */
+       scale = gtk_widget_get_scale_factor (GTK_WIDGET (view));
+       if ((scale > 1) &&
+           ((width > cell_area->width) || (height > cell_area->height)) &&
+           (width <= (cell_area->width * scale)) &&
+           (height <= (cell_area->height * scale)))
+       {
+               width = width / scale;
+               height = height / scale;
+       }
+
        gtk_source_gutter_renderer_get_alignment (renderer,
                                                  &xalign,
                                                  &yalign);
@@ -155,8 +173,12 @@ gutter_renderer_pixbuf_draw (GtkSourceGutterRenderer      *renderer,
                        g_assert_not_reached ();
        }
 
-       gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
+       surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, NULL);
+       cairo_set_source_surface (cr, surface, x, y);
+
        cairo_paint (cr);
+
+       cairo_surface_destroy (surface);
 }
 
 static void
diff --git a/gtksourceview/gtksourcepixbufhelper.c b/gtksourceview/gtksourcepixbufhelper.c
index 6a10b7f..99dd6b2 100644
--- a/gtksourceview/gtksourcepixbufhelper.c
+++ b/gtksourceview/gtksourcepixbufhelper.c
@@ -279,16 +279,19 @@ from_name (GtkSourcePixbufHelper *helper,
        GtkIconTheme *icon_theme;
        GtkIconInfo *info;
        GtkIconLookupFlags flags;
+       gint scale;
 
        screen = gtk_widget_get_screen (widget);
        icon_theme = gtk_icon_theme_get_for_screen (screen);
 
        flags = GTK_ICON_LOOKUP_USE_BUILTIN;
+        scale = gtk_widget_get_scale_factor (widget);
 
-       info = gtk_icon_theme_lookup_icon (icon_theme,
-                                          helper->icon_name,
-                                          size,
-                                          flags);
+       info = gtk_icon_theme_lookup_icon_for_scale (icon_theme,
+                                                    helper->icon_name,
+                                                    size,
+                                                    scale,
+                                                    flags);
 
        if (info)
        {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]