[cairo] How to draw a picture inside another picture

Daniel Amelang daniel.amelang at gmail.com
Sun Apr 8 01:37:11 PDT 2007


On 4/8/07, Andrea Spadaccini <a.spadaccini at catania.linux.it> wrote:
> Ciao Daniel,
>
> > I'm not a GTK guru, but since this has gone answered to 2 days, so here goes.
> >
> > Have you already retrieved a cairo context for your widget? If not,
> > use gdk_cairo_create to do so. That's what you'll use to perform the
> > over operation with the small image, I would think.
>
> Here's my expose method, taken from a widget that inherits from a
> gtk.DrawingArea:
>
>     def expose(self, widget, event, surface):
>         c = widget.window.cairo_create()
>
>         self.image = cairo.ImageSurface.create_from_png("icon.png")
>         cx = cairo.Context(surface)
>         cx.set_source_surface(self.image, 10, 10)
>
>         c.set_source_surface(surface, 0,0)
>         c.paint()
>         self.drawArea(widget)
>         self.queue_draw()
>
>
> > Or are you saying that you just don't know how do perform an over
> > operation? cairo_set_source_surface and cairo_paint should be all you
> > need.
>
> I simply don't understand how can I connect the image self.image and the cairo
> context c, that comes from the gtk.DrawingArea-like widget.

Ah, I think you're not understanding how cairo works. For example, you
never paint the icon onto the background surface. You just call
set_source_surface with the icon surface, and then you walk away. You
need to paint it on.

But there's more that's wrong here. Try this (warning, I'm not a
pycairo guru either):

c = widget.window.cairo_create()
c.set_source_surface(surface, 0, 0)
c.paint()
icon = cairo.ImageSurface.create_from_png("icon.png")
c.set_source_surface(icon, 10, 10)
c.paint()
# icon.destroy() # I don't know pycairo+pygtk well, so I don't know if
you need this
# c.destroy() # I don't know pycairo+pygtk well, so I don't know if
you need this
self.drawArea(widget)
self.queue_draw()
# this must be an animation if you're queuing a draw right after you
finish drawing, right?

You'll probably benefit from this tutorial, as it explains cairo's
drawing model quite well. And it uses pycairo in the examples:

http://www.tortall.net/mu/wiki/CairoTutorial

Dan


More information about the cairo mailing list