[gtk+] iconhelper: Improve size determination for surfaces



commit afd0e284375baa0da4a86c31fd1994a3612e1a24
Author: Benjamin Otte <otte redhat com>
Date:   Sat Nov 28 07:06:37 2015 +0100

    iconhelper: Improve size determination for surfaces
    
    Instead of just working for image surface, this should now work for all
    bounded surfaces.
    
    Test included.

 gtk/gtkiconhelper.c                               |   28 ++++++-----
 testsuite/reftests/Makefile.am                    |    3 +
 testsuite/reftests/image-recording-surface.c      |   52 +++++++++++++++++++++
 testsuite/reftests/image-recording-surface.ref.ui |   16 ++++++
 testsuite/reftests/image-recording-surface.ui     |   16 ++++++
 5 files changed, 103 insertions(+), 12 deletions(-)
---
diff --git a/gtk/gtkiconhelper.c b/gtk/gtkiconhelper.c
index d4b5aa0..89488c3 100644
--- a/gtk/gtkiconhelper.c
+++ b/gtk/gtkiconhelper.c
@@ -346,22 +346,26 @@ get_surface_size (GtkIconHelper   *self,
                  int *width,
                  int *height)
 {
-  double x_scale, y_scale;
+  GdkRectangle clip;
+  cairo_t *cr;
 
-  if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE)
+  cr = cairo_create (surface);
+  if (gdk_cairo_get_clip_rectangle (cr, &clip))
     {
-      x_scale = y_scale = 1;
-
-      cairo_surface_get_device_scale (surface, &x_scale, &y_scale);
-
-      /* Assume any set scaling is icon scale */
-      *width =
-       ceil (cairo_image_surface_get_width (surface) / x_scale);
-      *height =
-       ceil (cairo_image_surface_get_height (surface) / y_scale);
+      if (clip.x != 0 || clip.y != 0)
+        {
+          g_warning ("origin of surface is %d %d, not supported", clip.x, clip.y);
+        }
+      *width = clip.width;
+      *height = clip.height;
     }
   else
-    ensure_icon_size (self, width, height);
+    {
+      g_warning ("infinite surface size not supported");
+      ensure_icon_size (self, width, height);
+    }
+
+  cairo_destroy (cr);
 }
 
 static void
diff --git a/testsuite/reftests/Makefile.am b/testsuite/reftests/Makefile.am
index 584d47e..66e3c50 100644
--- a/testsuite/reftests/Makefile.am
+++ b/testsuite/reftests/Makefile.am
@@ -279,6 +279,8 @@ testdata = \
        image-load-from-file.css \
        image-load-from-file.ref.ui \
        image-load-from-file.ui \
+       image-recording-surface.ref.ui \
+       image-recording-surface.ui \
        info-bar-message-types.css \
        info-bar-message-types.ref.ui \
        info-bar-message-types.ui \
@@ -496,6 +498,7 @@ libreftest_la_CFLAGS = $(gtk_reftest_CFLAGS)
 libreftest_la_LIBADD = $(gtk_reftest_LDADD)
 libreftest_la_SOURCES =                        \
        expand-expander.c               \
+       image-recording-surface.c       \
        label-text-shadow-changes-modify-clip.c \
        letter-spacing.c                \
        set-default-direction.c         \
diff --git a/testsuite/reftests/image-recording-surface.c b/testsuite/reftests/image-recording-surface.c
new file mode 100644
index 0000000..bae9a5a
--- /dev/null
+++ b/testsuite/reftests/image-recording-surface.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2014 Red Hat Inc.
+ *
+ * Author:
+ *      Matthias Clasen <mclasen redhat com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gtk/gtk.h>
+
+G_MODULE_EXPORT void
+image_recording_surface_set (GtkWidget *widget,
+                             gpointer   unused)
+{
+  GError *error = NULL;
+  GdkPixbuf *pixbuf;
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  cairo_rectangle_t rect;
+
+  pixbuf = gdk_pixbuf_new_from_resource ("/org/gtk/libgtk/inspector/logo.png", &error);
+  g_assert_no_error (error);
+  rect.x = 0;
+  rect.y = 0;
+  rect.width = gdk_pixbuf_get_width (pixbuf);
+  rect.height = gdk_pixbuf_get_height (pixbuf);
+  surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &rect);
+
+  cr = cairo_create (surface);
+  gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
+  cairo_paint (cr);
+  cairo_destroy (cr);
+
+  gtk_image_set_from_surface (GTK_IMAGE (widget), surface);
+
+  cairo_surface_destroy (surface);
+  g_object_unref (pixbuf);
+}
diff --git a/testsuite/reftests/image-recording-surface.ref.ui 
b/testsuite/reftests/image-recording-surface.ref.ui
new file mode 100644
index 0000000..aef6f63
--- /dev/null
+++ b/testsuite/reftests/image-recording-surface.ref.ui
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.19.0 -->
+<interface>
+  <requires lib="gtk+" version="3.16"/>
+  <object class="GtkWindow" id="window1">
+    <property name="can_focus">False</property>
+    <property name="type">popup</property>
+    <child>
+      <object class="GtkImage" id="image1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="resource">/org/gtk/libgtk/inspector/logo.png</property>
+      </object>
+    </child>
+  </object>
+</interface>
diff --git a/testsuite/reftests/image-recording-surface.ui b/testsuite/reftests/image-recording-surface.ui
new file mode 100644
index 0000000..064c28d
--- /dev/null
+++ b/testsuite/reftests/image-recording-surface.ui
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.19.0 -->
+<interface>
+  <requires lib="gtk+" version="3.16"/>
+  <object class="GtkWindow" id="window1">
+    <property name="can_focus">False</property>
+    <property name="type">popup</property>
+    <child>
+      <object class="GtkImage" id="image1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <signal name="realize" handler="reftest:image_recording_surface_set" after="yes" swapped="no"/>
+      </object>
+    </child>
+  </object>
+</interface>


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