[Cairo] py-cairo example & Context save/restore quesiton

Carl Worth cworth at east.isi.edu
Fri Nov 14 08:34:18 PST 2003

On Nov 14, Jesse Andrews wrote:
 > I hacked a quick Lindenmayer system for cairo using the python-cairo 
 > port by jamesh.

Cool. l-systems are fun. Good to see some done with cairo. I've just
committed this as python-cairo/examples/lsystem. If you'd like to
maintain this in CVS, just send me an ssh key and a preferred

 > python-cairo worked pretty good except for the fact that saving and 
 > restoring state in a context seem to take "forver" (in the sense that 
 > doing that hundreds of times in a update took seconds)

Hmm... I've never noticed any problems with save/restore performance,
but I've probably never done it hundreds of time before
either. Looking at the code, most of what it copies is simple data
structures and atomic values.

It does copy the current path, which can obviously be quite large, so
you might want avoid having a large path current path at the time of
save if that's possible.

Other than that it save the current pen, which is tiny, and it
re-creates a copy of the current font. I don't know how heavy the font
object is, but since we're dealing with a stack here, it wouldn't be
hard at all to do something more efficient there if necessary.

 > and the fact that the state doesn't seem to be stored.
 > Is the state (rotate/translation/position) stored just as in the 
 > postscript model or is this a different idea?

Yes, using cairo.save saves the current matrix, colors, path, etc. and
cairo.restore restores them. I've tested this in python-cairo and it
does work, (see attached).

Oh, one possible point of confusion is that the matrix transforms
points as they are added to the path, (eg. at the time of move_to,
line_to, curve_to). Further changes to the matrix do not affect points
that are already on the path. Could that explain the problem you are

Otherwise, a minimal test case demonstrating the problem would be
quite helpful.

 > ( Note: python-cairo & cairo are currently out of sync, so compiling 
 > took some commenting out source of python-cairo )

I've just removed from python-cairo several things that were recently
removed from cairo. Let me know if I missed anything.



-------------- next part --------------
#!/usr/bin/env python

import gtk
import cairo
import cairo.gtk

def expose_event (widget, event):
    cr = cairo.Context ()
    cairo.gtk.set_target_drawable (cr, widget.window)

    cr.rectangle (10, 10, 50, 50)

    # First set things up for a red stroke.
    cr.set_rgb_color (0, 0, 1)
    cr.set_line_width (8)

    # Do blue fill inside save/restore
    cr.save ()
    cr.rotate (0.16)
    cr.set_rgb_color (1, 0, 0)
    cr.fill ()
    cr.restore ()

    # Now back to color/line_width from before save
    cr.stroke ()

win = gtk.Window()
win.set_title('Cairo demo of save/restore')

drawingarea = gtk.DrawingArea ()
drawingarea.set_size_request (400,100)
win.add (drawingarea)

win.show_all ()

win.connect ('destroy', gtk.mainquit)
drawingarea.connect ('expose_event', expose_event)

gtk.main ()

More information about the cairo mailing list