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

Carl Worth cworth at east.isi.edu
Fri Nov 14 10:44:53 PST 2003


On Nov 14, Jesse Andrews wrote:
 > The order that the path is being drawn (in logo psuedo code):
 > 
 > new path
 > 
 >   forward
 >   save state
 >   rotate
 >   forward
 >   restore state
 >   forward
 > 
 > stroke

Ah, I see the miscommunication. The path is part of the state that is
saved/restored. So, assuming that forward only modifies the path, (and
doesn't draw anything), then the above sequence is equivalent to:

new path

  forward
  forward

stroke

Which is not what you were wanting, (but does jibe with the fact that
I only ever see a straight line with the plant example).

I've attached another python program demonstrating your sequence above.

 > It seems like the restore does NOT reload the position, but does reload 
 > the rotation.

The current point *is* restored along with the rest of the path.

-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)
    cr.save ()
    rotate (cr, math.pi / 4)
    forward (cr, 10)
    cr.restore ()
    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