[Sugar-devel] [PATCH sugar-toolkit] icon: use GDK for pixbuf-to-cairo conversion

Simon Schampijer simon at schampijer.de
Wed Mar 7 09:32:59 EST 2012


Hi Daniel,

thanks for the patch!

On 03/06/2012 10:08 PM, Daniel Drake wrote:
> We currently use some strange code inside hippo to paint a pixbuf onto
> a cairo surface. This can be more effectively done within GDK.

Agreed.

> This solves a Sugar crash-on-startup on Fedora 17, where this hippo
> code is crashing, and uses the same underlying mechanism as
> sugar-toolkit-gtk3.

Yes, your patch does fix the breakage in F17. The breakage has been 
introduced between the alpha release day and today, I wonder what 
package exactly caused it. Out of curiosity, Do you know that?

> Also use cairo.Context.set_source_rgb() for setting the background
> colour, which avoids the requirement to encapsulate the cairo context
> at an early stage (which would make the rest of this patch a little
> more complicated).

Can't we do the same than in toolkit-gtk3? Or do I misinterpret your 
words. The code from below works for me.

diff --git a/src/sugar/graphics/icon.py b/src/sugar/graphics/icon.py
index 3f540d7..1736c27 100644
--- a/src/sugar/graphics/icon.py
+++ b/src/sugar/graphics/icon.py
@@ -280,10 +280,9 @@ class _IconBuffer(object):
              surface = cairo.ImageSurface(cairo.FORMAT_RGB24, int(width),
                                           int(height))
              context = cairo.Context(surface)
-            context.set_source_rgb(self.background_color.red,
-                                   self.background_color.blue,
-                                   self.background_color.green)
-            context.paint()
+            gdkcontext = gtk.gdk.CairoContext(context)
+            gdkcontext.set_source_color(self.background_color)
+            gdkcontext.paint()

          context.scale(float(width) / (icon_width + padding * 2),
                        float(height) / (icon_height + padding * 2))

Regards,
    Simon

> Signed-off-by: Daniel Drake<dsd at laptop.org>
> ---
>   src/sugar/graphics/icon.py |   24 ++++++++++++------------
>   1 files changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/src/sugar/graphics/icon.py b/src/sugar/graphics/icon.py
> index 62a79f6..3f540d7 100644
> --- a/src/sugar/graphics/icon.py
> +++ b/src/sugar/graphics/icon.py
> @@ -182,9 +182,9 @@ class _IconBuffer(object):
>
>               if not sensitive:
>                   pixbuf = self._get_insensitive_pixbuf(pixbuf, widget)
> -            surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
> -            context.set_source_surface(surface, 0, 0)
> -            context.paint()
> +            gdkcontext = gtk.gdk.CairoContext(context)
> +            gdkcontext.set_source_pixbuf(pixbuf, 0, 0)
> +            gdkcontext.paint()
>
>       def _get_size(self, icon_width, icon_height, padding):
>           if self.width is not None and self.height is not None:
> @@ -280,8 +280,9 @@ class _IconBuffer(object):
>               surface = cairo.ImageSurface(cairo.FORMAT_RGB24, int(width),
>                                            int(height))
>               context = cairo.Context(surface)
> -            context = gtk.gdk.CairoContext(context)
> -            context.set_source_color(self.background_color)
> +            context.set_source_rgb(self.background_color.red,
> +                                   self.background_color.blue,
> +                                   self.background_color.green)
>               context.paint()
>
>           context.scale(float(width) / (icon_width + padding * 2),
> @@ -295,16 +296,15 @@ class _IconBuffer(object):
>               else:
>                   pixbuf = handle.get_pixbuf()
>                   pixbuf = self._get_insensitive_pixbuf(pixbuf, widget)
> -
> -                pixbuf_surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
> -                context.set_source_surface(pixbuf_surface, 0, 0)
> -                context.paint()
> +                gdkcontext = gtk.gdk.CairoContext(context)
> +                gdkcontext.set_source_pixbuf(pixbuf, 0, 0)
> +                gdkcontext.paint()
>           else:
>               if not sensitive:
>                   pixbuf = self._get_insensitive_pixbuf(pixbuf, widget)
> -            pixbuf_surface = hippo.cairo_surface_from_gdk_pixbuf(pixbuf)
> -            context.set_source_surface(pixbuf_surface, 0, 0)
> -            context.paint()
> +            gdkcontext = gtk.gdk.CairoContext(context)
> +            gdkcontext.set_source_pixbuf(pixbuf, 0, 0)
> +            gdkcontext.paint()
>
>           if self.badge_name:
>               context.restore()



More information about the Sugar-devel mailing list