[cairo-commit] pycairo/examples/gtk cairo-knockout.py, NONE, 1.1 lsystem.py, NONE, 1.1 text.py, NONE, 1.1

Steve Chaplin commit at pdx.freedesktop.org
Tue Feb 15 18:17:55 PST 2005


Committed by: stevech1097

Update of /cvs/cairo/pycairo/examples/gtk
In directory gabe:/tmp/cvs-serv17439/examples/gtk

Added Files:
	cairo-knockout.py lsystem.py text.py 
Log Message:
SC 16/02/2005

--- NEW FILE: cairo-knockout.py ---
#!/usr/bin/env python

from __future__ import division
import math
import sys

import gtk
import cairo
import cairo.gtk


def oval_path(ctx, xc, yc, xr, yr):
    matrix = cairo.Matrix ()

    ctx.translate (xc, yc)
    ctx.scale (1.0, yr / xr)
    ctx.move_to (xr, 0.0)
    ctx.arc (  0, 0,
               xr,
               0, 2 * math.pi)
    ctx.close_path ()

    ctx.set_matrix (matrix)
    
def fill_checks(ctx, x, y, width, height):
    ctx.save()

    CHECK_SIZE = 32
    check = ctx.target_surface.create_similar(cairo.FORMAT_RGB24,
                                              2*CHECK_SIZE, 2*CHECK_SIZE)
    check.set_repeat(1)

    ctx.save()
    ctx.set_target_surface(check)
    ctx.set_operator(cairo.OPERATOR_SRC)

    ctx.set_rgb_color(0.4, 0.4, 0.4)
    
    ctx.rectangle(0, 0, 2*CHECK_SIZE, 2*CHECK_SIZE)
    ctx.fill()

    ctx.set_rgb_color(0.7, 0.7, 0.7)
    
    ctx.rectangle(x, y, CHECK_SIZE, CHECK_SIZE)
    ctx.fill()
    ctx.rectangle(x+CHECK_SIZE, y+CHECK_SIZE, CHECK_SIZE, CHECK_SIZE)
    ctx.fill()

    ctx.restore()

    check_pattern = cairo.Pattern (surface=check)
    ctx.set_pattern(check_pattern)
    ctx.rectangle(0, 0, width, height)
    ctx.fill()

    ctx.restore()
    

def draw_3circles(ctx, xc, yc, radius):
    subradius = radius * (2 / 3. - 0.1)

    ctx.set_rgb_color(1, 0, 0)
    oval_path(ctx,
              xc + radius / 3. * math.cos(math.pi * 0.5),
              yc - radius / 3. * math.sin(math.pi * 0.5),
              subradius, subradius)
    ctx.fill()

    ctx.set_rgb_color(0, 1, 0)
    oval_path(ctx,
              xc + radius / 3. * math.cos(math.pi * (0.5 + 2/.3)),
              yc - radius / 3. * math.sin(math.pi * (0.5 + 2/.3)),
              subradius, subradius)
    ctx.fill()

    ctx.set_rgb_color(0, 0, 1)
    oval_path(ctx,
              xc + radius / 3. * math.cos(math.pi * (0.5 + 4/.3)),
              yc - radius / 3. * math.sin(math.pi * (0.5 + 4/.3)),
              subradius, subradius)
    ctx.fill()

def expose(drawingarea, event):
    # a bug is highlighted when the window is obscured - the next expose event
    # does not redraw the window properly.
    # If you draw to a gdk.Pixmap first the problem does not appear
    drawable = drawingarea.window
    width = drawingarea.allocation.width
    height = drawingarea.allocation.height

    radius = 0.5 * min(width, height) - 10
    xc = width / 2.
    yc = height / 2.

    ctx = cairo.Context()
    cairo.gtk.set_target_drawable(ctx, drawable)
    surface = ctx.target_surface

    overlay = surface.create_similar(cairo.FORMAT_ARGB32, width,height)
    punch   = surface.create_similar(cairo.FORMAT_A8,     width,height)
    circles = surface.create_similar(cairo.FORMAT_ARGB32, width,height)

    fill_checks(ctx, 0, 0, width, height)

    ctx.save()
    ctx.set_target_surface(overlay)

    # Draw a black circle on the overlay
    ctx.set_rgb_color(0, 0, 0)
    oval_path(ctx, xc, yc, radius, radius)
    ctx.fill()

    ctx.save()
    ctx.set_target_surface(punch)

    # Draw 3 circles to the punch surface, then cut
    # that out of the main circle in the overlay
    draw_3circles(ctx, xc, yc, radius)
    
    ctx.restore()

    ctx.set_operator(cairo.OPERATOR_OUT_REVERSE)
    ctx.show_surface(punch, width, height)

    ctx.save()

    ctx.set_target_surface(circles)
    ctx.set_alpha(0.5)
    ctx.set_operator(cairo.OPERATOR_OVER)
    draw_3circles(ctx, xc, yc, radius)
    
    ctx.restore()

    ctx.set_operator(cairo.OPERATOR_ADD)
    ctx.show_surface(circles, width, height)

    ctx.restore()

    ctx.show_surface(overlay, width, height)
    
def main():
    win = gtk.Window()
    win.connect('destroy', gtk.main_quit)
    win.set_title('Knockout Groups')
    win.set_default_size(400, 400)

    drawingarea = gtk.DrawingArea()
    drawingarea.connect('expose_event', expose)

    win.add(drawingarea)
    win.show_all()

    gtk.main()

if __name__ == '__main__':
    main()

--- NEW FILE: lsystem.py ---
#!/usr/bin/env python

import gtk
import cairo
import cairo.gtk

# Copyright 2003 Jesse Andrews (jdandr2 at uky.edu) under GPL


class lindenmayer:
    def __init__( self ):
        self.str = ''
        self.prod = {'[':'[','f':'f',']':']','+':'+','-':'-'}
        self.SIZE = 10
        self.THETA = 90

    def addProd( self, let, pro ):
        self.prod[let]=pro

    def iterate( self, qty=1 ):
        for i in xrange(qty):
            self.str = ''.join([ self.prod[l] for l in self.str])
        print 'Done iterating'

    def expose( self, drawingarea, event ):
        drawable = drawingarea.window
        width = drawingarea.allocation.width
        height = drawingarea.allocation.height

        drawable.clear()

        ctx = cairo.Context()
        cairo.gtk.set_target_drawable(ctx, drawable)
        ctx.set_rgb_color(0, 0, 0)

        ctx.set_line_width(self.SIZE / 4)
        ctx.set_tolerance(0.1)
        ctx.set_line_join(cairo.LINE_JOIN_BEVEL)

        ctx.new_path()
        ctx.move_to(100,100)

        for c in self.str:
            if c == 'f': line(ctx, self.SIZE )
            if c == '+': rotate( ctx, +self.THETA )
            if c == '-': rotate( ctx, -self.THETA )
            if c == '[': ctx.save()
            if c == ']': ctx.restore()

        ctx.stroke()

def line(ctx, len):
    ctx.rel_line_to( 0, len )

def rotate(ctx, deg):
    ctx.rotate( 2*3.141592653589793*deg/360.0  )

def main():
    win = gtk.Window()
    win.connect('destroy', lambda x: gtk.main_quit())
    win.set_title('Cairo Lindenmayer System')
    win.set_default_size(600, 600)

    drawingarea = gtk.DrawingArea()
    cls = lindenmayer()

    ################# SETUP LSYSTEM HERE ################

    ### Generic stuff ###

    cls.str = 'f'   # the starting string

    cls.SIZE = 5    # length of a line

    ##############################################
    ##############################################
    #### Uncomment the one you want to use... ####
    #### only one at a time right now!        ####
    ##############################################
    ##############################################

    ###### Kock Square Curve #######
    cls.addProd('f','f-f+f+f-f')
    cls.THETA = 90


    ###### Kock Snowflake ######

#    cls.addProd('f','f-f++f-f')
#    cls.THETA = 60


    ######## Peano Curve ########
#    cls.addProd('x', 'xfyfx+f+yfxfy-f-xfyfx')
#    cls.addProd('y', 'yfxfy-f-xfyfx+f+yfxfy')
#    cls.addProd('f', 'f')
#    cls.THETA = 90
#    cls.str = 'y'


    ###### the plant ######
    ## doesn't seem to work ... .save & .restore messed up ##

#    cls.addProd( 'f','f[+f]f[-f]f' )
#    cls.THETA = 25

    ####### the tree #########
    ## doesn't seem to work ... .save & .restore messed up ##

#    cls.addProd( 'f', 'ff+[+f-f-f]-[-f+f+f]' )
#    cls.THETA = 22


    ### times to iterate string rewriting ###
    #this grows QUICKLY, so start only inc by 1 each run!
    cls.iterate(4)

    ################ DONE SETUP ###############

    drawingarea.connect('expose_event', cls.expose)

    win.add(drawingarea)
    win.show_all()

    gtk.main()

if __name__ == '__main__':
    main()


--- NEW FILE: text.py ---
#!/usr/bin/env python

import gtk
import cairo
import cairo.gtk

def expose_event(widget, event):
    ctx = cairo.Context()
    cairo.gtk.set_target_drawable(ctx, widget.window)
    ctx.set_line_width(6)
    ctx.set_tolerance(.1)

    ctx.select_font('sans-serif')
    ctx.scale_font(48)
    (x, y, width, height, dx, dy) = ctx.text_extents('Hello World')

    ctx.translate (100, 100)

    ctx.new_path()
    ctx.move_to(x-10,y-10)
    ctx.rel_line_to(width + 20, 0)
    ctx.rel_line_to(0, height + 20)
    ctx.rel_line_to(-(width + 20), 0)
    ctx.close_path()
    ctx.set_rgb_color(0,0,1)
    ctx.stroke()

    ctx.move_to(0, 0)
    ctx.set_rgb_color(0,0,0)
    ctx.show_text('Hello World')

win = gtk.Window()
win.set_title('Cairo Demo')

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

win.show_all()

win.connect('destroy', lambda x: gtk.main_quit())
drawingarea.connect('expose_event', expose_event)

gtk.main()




More information about the cairo-commit mailing list