[cairo-commit] pycairo/examples .cvsignore, 1.2, 1.3 cairo-knockout.py, 1.3, 1.4 gradient.py, NONE, 1.1

Carl Worth commit at pdx.freedesktop.org
Thu Nov 4 06:45:37 PST 2004


Committed by: cworth

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

Modified Files:
	.cvsignore cairo-knockout.py 
Added Files:
	gradient.py 
Log Message:

        Fixes for set_pattern from Steve Chaplin
        <stevech1097 at yahoo.com.au>:

        * examples/cairo-knockout.py: Bring up-to-date with latest
        cairo-knockout.c. Now uses cairo_arc rather than custom arc
        approximation, and now uses new cairo.set_pattern.

        * cairo/pycairo.h: Add declaration for struct PyCairoPattern.

        * cairo/pycairo-context.c (pycairo_set_pattern): Re-enable
        pycairo_set_pattern now that it uses PyCairoPattern_Type.

        * cairo/cairomodule.c (init_cairo): Add PyCairoPattern_Type

        * cairo/pycairo-pattern.c: New file to bind to cairo_pattern_t.


Index: .cvsignore
===================================================================
RCS file: /cvs/cairo/pycairo/examples/.cvsignore,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- .cvsignore	3 Nov 2004 01:24:06 -0000	1.2
+++ .cvsignore	4 Nov 2004 14:45:35 -0000	1.3
@@ -1,4 +1,5 @@
 *.pyc
 *.pyo
+gradient.png
 hering.png
 spiral.ps

Index: cairo-knockout.py
===================================================================
RCS file: /cvs/cairo/pycairo/examples/cairo-knockout.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- cairo-knockout.py	14 Nov 2003 15:32:10 -0000	1.3
+++ cairo-knockout.py	4 Nov 2004 14:45:35 -0000	1.4
@@ -1,68 +1,61 @@
 #!/usr/bin/env python
+
+from __future__ import division
 import math
+import sys
+
 import gtk
 import cairo
 import cairo.gtk
 
-def rect_path(ctx, x, y, width, height):
-    ctx.new_path()
-    ctx.move_to(x, y)
-    ctx.rel_line_to(0, height)
-    ctx.rel_line_to(width, 0)
-    ctx.rel_line_to(0, -height)
-    ctx.rel_line_to(-width, 0)
-    ctx.close_path()
 
 def oval_path(ctx, xc, yc, xr, yr):
-    ctx.new_path()
-    ctx.move_to(xc + xr, yc)
-
-    TANGENT_MULT = 1.65591 / 3.
-    for i in range(4):
-        angle1 = ((i + 0) /2.) * math.pi
-        angle2 = ((i + 1) /2.) * math.pi
-
-        x0 = xc + xr * math.cos(angle1)
-        y0 = yc - yr * math.sin(angle1)
-        x1 = x0 - xr * math.sin(angle1) * TANGENT_MULT
-        y1 = y0 - yr * math.cos(angle1) * TANGENT_MULT
-        x3 = xc + xr * math.cos(angle2)
-        y3 = yc - yr * math.sin(angle2)
-        x2 = x3 + xr * math.sin(angle2) * TANGENT_MULT
-        y2 = y3 + yr * math.cos(angle2) * TANGENT_MULT
+    matrix = cairo.Matrix ()
+    #cairo_current_matrix (cr, matrix);
 
-        ctx.curve_to(x1,y1, x2,y2, x3,y3)
-    ctx.close_path()
+    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 ()
 
-def fill_checks(ctx, width, height):
+    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(True)
+    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)
-    rect_path(ctx, 0, 0, 2*CHECK_SIZE, 2*CHECK_SIZE)
+    
+    ctx.rectangle(0, 0, 2*CHECK_SIZE, 2*CHECK_SIZE)
     ctx.fill()
 
     ctx.set_rgb_color(0.7, 0.7, 0.7)
-    rect_path(ctx, 0, 0, CHECK_SIZE, CHECK_SIZE)
+    
+    ctx.rectangle(x, y, CHECK_SIZE, CHECK_SIZE)
     ctx.fill()
-    rect_path(ctx, CHECK_SIZE, CHECK_SIZE, CHECK_SIZE, CHECK_SIZE)
+    ctx.rectangle(x+CHECK_SIZE, y+CHECK_SIZE, CHECK_SIZE, CHECK_SIZE)
     ctx.fill()
 
     ctx.restore()
 
-    ctx.set_pattern(check)
-    rect_path(ctx, 0, 0, width, height)
+    check_pattern = cairo.Pattern (surface=check)
+    ctx.set_pattern(check_pattern)
+    ctx.rectangle(0, 0, width, height)
     ctx.fill()
 
-    ctx.save()
+    ctx.restore()
+    
 
 def draw_3circles(ctx, xc, yc, radius):
     subradius = radius * (2 / 3. - 0.1)
@@ -76,18 +69,17 @@
 
     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.)),
+              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.)),
+              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):
     drawable = drawingarea.window
@@ -103,31 +95,38 @@
     surface = ctx.target_surface
 
     overlay = surface.create_similar(cairo.FORMAT_ARGB32, width,height)
-    punch = surface.create_similar(cairo.FORMAT_A8, width,height)
+    punch   = surface.create_similar(cairo.FORMAT_A8,     width,height)
     circles = surface.create_similar(cairo.FORMAT_ARGB32, width,height)
 
-    fill_checks(ctx, 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)
@@ -136,10 +135,10 @@
     ctx.restore()
 
     ctx.show_surface(overlay, width, height)
-
+    
 def main():
     win = gtk.Window()
-    win.connect('destroy', gtk.mainquit)
+    win.connect('destroy', gtk.main_quit)
     win.set_title('Knockout Groups')
     win.set_default_size(400, 400)
 

--- NEW FILE: gradient.py ---
#!/usr/bin/env python
"""/cairo-demo/cairo_snippets/gradient.cairo translated to Python
"""

import math
import cairo

WIDTH, HEIGHT  = 256, 256

try:
    fileObject = file('gradient.png', 'wb')
except IOError, exc:
    raise SystemExit("%s: %s" % (exc.filename, exc.strerror))

ctx = cairo.Context()
ctx.set_target_png (fileObject, cairo.FORMAT_ARGB32, WIDTH, HEIGHT)

ctx.scale (WIDTH/1.0, HEIGHT/1.0)

pat = cairo.Pattern(x0=0.0, y0=0.0, x1=0.0, y1=1.0)
pat.add_color_stop (1, 0, 0, 0, 1)
pat.add_color_stop (0, 1, 1, 1, 1)

ctx.rectangle (0,0,1,1)
ctx.set_pattern (pat)
ctx.fill ()

pat = cairo.Pattern (cx0=0.45, cy0=0.4, radius0=0.1,
                     cx1=0.4,  cy1=0.4, radius1=0.5)
pat.add_color_stop (0, 1, 1, 1, 1)
pat.add_color_stop (1, 0, 0, 0, 1)

ctx.set_pattern (pat)
ctx.arc (0.5, 0.5, 0.3, 0, 2 * math.pi)
ctx.fill ()

ctx.show_page()




More information about the cairo-commit mailing list