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

Carl Worth cworth at east.isi.edu
Fri Nov 14 10:55:56 PST 2003


On Nov 14, Bill Spitzak wrote:
 > In fact, although I know you are saving and restoring it now, I was thinking 
 > that even the current path should not be modified by save/restore. 

A design goal of cairo has been to not deviate from the PostScript
model without extremely convincing evidence for doing so. I haven't
seen a compelling reason not to remain compatible with the PostScript
model saving/restoring the current path.

 > This would allow you to do transforms (in particular resets to the identity) 
 > to draw portions of the path.

This is still possible, though the user will have to save/restore the
matrix manually. My last email had an example, (turtle.py), that
showed that path modifications between save/restore are not visible
after the restore.

Here is a "fix" to that example that shows how to get the same
sequence of forward/rotate calls to yield the desired result. Instead
of save/restore, this code manually saves the current matrix then
restores it afterwards. (Note that if you actually wanted to make a
more complete logo-like system, you might want a stack of matrix
objects).

-Carl

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

import gtk
import cairo
import cairo.gtk
import math

def forward (cr, distance):
    cr.rel_line_to (0, distance)

def rotate (cr, angle):
    cr.rotate (angle)

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

    cr.move_to (20, 20)
    
    forward (cr, 10)
    m = cr.matrix
    rotate (cr, math.pi / 4)
    forward (cr, 10)
    cr.set_matrix (m)
    forward (cr, 10)

    cr.set_rgb_color (0, 0, 1)
    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