summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSteve Chaplin <>2010-09-12 21:04:44 +0800
committerSteve Chaplin <>2010-09-12 21:04:44 +0800
commitf3df40256010a0425d820136c532d54dc467f67c (patch)
tree0390a3e3c50a23d8f4d1d1dba23ba93b3e0b3d27 /examples
parent59415110e1811019d49562fd06ad75d9182b9b77 (diff)
=== Pycairo 1.8.10 ===
Many changes to make the code work with Python 3.
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/cairo_snippets/snippets/__init__.py4
-rwxr-xr-xexamples/cairo_snippets/snippets_pdf.py68
-rwxr-xr-xexamples/cairo_snippets/snippets_png.py6
-rwxr-xr-xexamples/cairo_snippets/snippets_ps.py68
-rwxr-xr-xexamples/cairo_snippets/snippets_svg.py68
-rwxr-xr-xexamples/hering.py44
-rwxr-xr-xexamples/spiral.py24
-rwxr-xr-xexamples/warpedtext.py68
8 files changed, 179 insertions, 171 deletions
diff --git a/examples/cairo_snippets/snippets/__init__.py b/examples/cairo_snippets/snippets/__init__.py
index 70b4813..a3bc4fa 100755
--- a/examples/cairo_snippets/snippets/__init__.py
+++ b/examples/cairo_snippets/snippets/__init__.py
@@ -8,5 +8,5 @@ snip_list.sort()
# function used by some or all snippets
def snippet_normalize (ctx, width, height):
- ctx.scale (width, height)
- ctx.set_line_width (0.04)
+ ctx.scale (width, height)
+ ctx.set_line_width (0.04)
diff --git a/examples/cairo_snippets/snippets_pdf.py b/examples/cairo_snippets/snippets_pdf.py
index 0606804..65b9c3e 100755
--- a/examples/cairo_snippets/snippets_pdf.py
+++ b/examples/cairo_snippets/snippets_pdf.py
@@ -9,7 +9,7 @@ import sys
import cairo
if not cairo.HAS_PDF_SURFACE:
- raise SystemExit ('cairo was not compiled with PDF support')
+ raise SystemExit ('cairo was not compiled with PDF support')
from snippets import snip_list, snippet_normalize
@@ -20,37 +20,39 @@ width, height = width_in_points, height_in_points # used by snippet_normalize()
def do_snippet (snippet):
- if verbose_mode:
- print 'processing %s' % snippet,
-
- filename = 'snippets/%s.pdf' % snippet
- surface = cairo.PDFSurface (filename, width_in_points, height_in_points)
- cr = cairo.Context (surface)
-
- cr.save()
- try:
- execfile ('snippets/%s.py' % snippet, globals(), locals())
- except:
- exc_type, exc_value = sys.exc_info()[:2]
- print >> sys.stderr, exc_type, exc_value
- else:
- cr.restore()
- cr.show_page()
- surface.finish()
-
- if verbose_mode:
- print
+ if verbose_mode:
+ print('processing %s' % snippet)
+
+ filename = 'snippets/%s.pdf' % snippet
+ surface = cairo.PDFSurface (filename, width_in_points, height_in_points)
+ cr = cairo.Context (surface)
+
+ cr.save()
+ try:
+ fName = 'snippets/%s.py' % snippet
+ code = open(fName).read()
+ exec (code, globals(), locals())
+ except:
+ exc_type, exc_value = sys.exc_info()[:2]
+ print(exc_type, exc_value, file=sys.stderr)
+ else:
+ cr.restore()
+ cr.show_page()
+ surface.finish()
+
+ if verbose_mode:
+ print
if __name__ == '__main__':
- verbose_mode = True
- if len(sys.argv) > 1 and sys.argv[1] == '-s':
- verbose_mode = False
- del sys.argv[1]
-
- if len(sys.argv) > 1: # do specified snippets
- snippet_list = sys.argv[1:]
- else: # do all snippets
- snippet_list = snip_list
-
- for s in snippet_list:
- do_snippet (s)
+ verbose_mode = True
+ if len(sys.argv) > 1 and sys.argv[1] == '-s':
+ verbose_mode = False
+ del sys.argv[1]
+
+ if len(sys.argv) > 1: # do specified snippets
+ snippet_list = sys.argv[1:]
+ else: # do all snippets
+ snippet_list = snip_list
+
+ for s in snippet_list:
+ do_snippet (s)
diff --git a/examples/cairo_snippets/snippets_png.py b/examples/cairo_snippets/snippets_png.py
index 5341979..94b5fb9 100755
--- a/examples/cairo_snippets/snippets_png.py
+++ b/examples/cairo_snippets/snippets_png.py
@@ -18,14 +18,16 @@ width, height = 256, 256 # used by snippet_normalize()
def do_snippet (snippet):
if verbose_mode:
- print 'processing %s' % snippet,
+ print('processing %s' % snippet)
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context (surface)
cr.save()
try:
- execfile ('snippets/%s.py' % snippet, globals(), locals())
+ fName = 'snippets/%s.py' % snippet
+ code = open(fName).read()
+ exec (code, globals(), locals())
except:
# exc_type, exc_value = sys.exc_info()[:2]
# print >> sys.stderr, exc_type, exc_value
diff --git a/examples/cairo_snippets/snippets_ps.py b/examples/cairo_snippets/snippets_ps.py
index 364402a..0015973 100755
--- a/examples/cairo_snippets/snippets_ps.py
+++ b/examples/cairo_snippets/snippets_ps.py
@@ -9,7 +9,7 @@ import sys
import cairo
if not cairo.HAS_PS_SURFACE:
- raise SystemExit ('cairo was not compiled with PS support')
+ raise SystemExit ('cairo was not compiled with PS support')
from snippets import snip_list, snippet_normalize
@@ -20,37 +20,39 @@ width, height = width_in_points, height_in_points # used by snippet_normalize()
def do_snippet (snippet):
- if verbose_mode:
- print 'processing %s' % snippet,
-
- filename = 'snippets/%s.ps' % snippet
- surface = cairo.PSSurface (filename, width_in_points, height_in_points)
- cr = cairo.Context (surface)
-
- cr.save()
- try:
- execfile ('snippets/%s.py' % snippet, globals(), locals())
- except:
- exc_type, exc_value = sys.exc_info()[:2]
- print >> sys.stderr, exc_type, exc_value
- else:
- cr.restore()
- cr.show_page()
- surface.finish()
-
- if verbose_mode:
- print
+ if verbose_mode:
+ print('processing %s' % snippet)
+
+ filename = 'snippets/%s.ps' % snippet
+ surface = cairo.PSSurface (filename, width_in_points, height_in_points)
+ cr = cairo.Context (surface)
+
+ cr.save()
+ try:
+ fName = 'snippets/%s.py' % snippet
+ code = open(fName).read()
+ exec (code, globals(), locals())
+ except:
+ exc_type, exc_value = sys.exc_info()[:2]
+ print >> sys.stderr, exc_type, exc_value
+ else:
+ cr.restore()
+ cr.show_page()
+ surface.finish()
+
+ if verbose_mode:
+ print
if __name__ == '__main__':
- verbose_mode = True
- if len(sys.argv) > 1 and sys.argv[1] == '-s':
- verbose_mode = False
- del sys.argv[1]
-
- if len(sys.argv) > 1: # do specified snippets
- snippet_list = sys.argv[1:]
- else: # do all snippets
- snippet_list = snip_list
-
- for s in snippet_list:
- do_snippet (s)
+ verbose_mode = True
+ if len(sys.argv) > 1 and sys.argv[1] == '-s':
+ verbose_mode = False
+ del sys.argv[1]
+
+ if len(sys.argv) > 1: # do specified snippets
+ snippet_list = sys.argv[1:]
+ else: # do all snippets
+ snippet_list = snip_list
+
+ for s in snippet_list:
+ do_snippet (s)
diff --git a/examples/cairo_snippets/snippets_svg.py b/examples/cairo_snippets/snippets_svg.py
index 3620ee7..e9c9d8f 100755
--- a/examples/cairo_snippets/snippets_svg.py
+++ b/examples/cairo_snippets/snippets_svg.py
@@ -6,7 +6,7 @@ import sys
import cairo
if not cairo.HAS_SVG_SURFACE:
- raise SystemExit ('cairo was not compiled with SVG support')
+ raise SystemExit ('cairo was not compiled with SVG support')
from snippets import snip_list, snippet_normalize
@@ -17,37 +17,39 @@ width, height = width_in_points, height_in_points # used by snippet_normalize()
def do_snippet (snippet):
- if verbose_mode:
- print 'processing %s' % snippet,
-
- filename = 'snippets/%s.svg' % snippet
- surface = cairo.SVGSurface (filename, width_in_points, height_in_points)
- cr = cairo.Context (surface)
-
- cr.save()
- try:
- execfile ('snippets/%s.py' % snippet, globals(), locals())
- except:
- exc_type, exc_value = sys.exc_info()[:2]
- print >> sys.stderr, exc_type, exc_value
- else:
- cr.restore()
- cr.show_page()
- surface.finish()
-
- if verbose_mode:
- print
+ if verbose_mode:
+ print('processing %s' % snippet)
+
+ filename = 'snippets/%s.svg' % snippet
+ surface = cairo.SVGSurface (filename, width_in_points, height_in_points)
+ cr = cairo.Context (surface)
+
+ cr.save()
+ try:
+ fName = 'snippets/%s.py' % snippet
+ code = open(fName).read()
+ exec (code, globals(), locals())
+ except:
+ exc_type, exc_value = sys.exc_info()[:2]
+ print >> sys.stderr, exc_type, exc_value
+ else:
+ cr.restore()
+ cr.show_page()
+ surface.finish()
+
+ if verbose_mode:
+ print
if __name__ == '__main__':
- verbose_mode = True
- if len(sys.argv) > 1 and sys.argv[1] == '-s':
- verbose_mode = False
- del sys.argv[1]
-
- if len(sys.argv) > 1: # do specified snippets
- snippet_list = sys.argv[1:]
- else: # do all snippets
- snippet_list = snip_list
-
- for s in snippet_list:
- do_snippet (s)
+ verbose_mode = True
+ if len(sys.argv) > 1 and sys.argv[1] == '-s':
+ verbose_mode = False
+ del sys.argv[1]
+
+ if len(sys.argv) > 1: # do specified snippets
+ snippet_list = sys.argv[1:]
+ else: # do all snippets
+ snippet_list = snip_list
+
+ for s in snippet_list:
+ do_snippet (s)
diff --git a/examples/hering.py b/examples/hering.py
index e400e15..1d3cf60 100755
--- a/examples/hering.py
+++ b/examples/hering.py
@@ -10,37 +10,37 @@ WIDTH = 300
HEIGHT = 600
def draw_hering (ctx, width, height):
- LINES= 32
- MAX_THETA = .80 * math.pi * 2
- THETA_INC = 2.0 * MAX_THETA / (LINES-1)
+ LINES= 32
+ MAX_THETA = .80 * math.pi * 2
+ THETA_INC = 2.0 * MAX_THETA / (LINES-1)
- ctx.set_source_rgb (0, 0, 0)
- ctx.set_line_width (2.0)
+ ctx.set_source_rgb (0, 0, 0)
+ ctx.set_line_width (2.0)
- ctx.save()
+ ctx.save()
- ctx.translate (width / 2, height / 2)
- ctx.rotate (MAX_THETA)
+ ctx.translate (width / 2, height / 2)
+ ctx.rotate (MAX_THETA)
- for i in range (LINES):
- ctx.move_to (-2 * width, 0)
- ctx.line_to (2 * width, 0)
- ctx.stroke()
+ for i in range (LINES):
+ ctx.move_to (-2 * width, 0)
+ ctx.line_to (2 * width, 0)
+ ctx.stroke()
- ctx.rotate (- THETA_INC)
+ ctx.rotate (- THETA_INC)
- ctx.restore()
+ ctx.restore()
- ctx.set_line_width (6)
- ctx.set_source_rgb (1, 0, 0)
+ ctx.set_line_width (6)
+ ctx.set_source_rgb (1, 0, 0)
- ctx.move_to (width / 4.0, 0)
- ctx.rel_line_to (0, height)
- ctx.stroke()
+ ctx.move_to (width / 4.0, 0)
+ ctx.rel_line_to (0, height)
+ ctx.stroke()
- ctx.move_to (3 * width / 4.0, 0)
- ctx.rel_line_to (0, height)
- ctx.stroke()
+ ctx.move_to (3 * width / 4.0, 0)
+ ctx.rel_line_to (0, height)
+ ctx.stroke()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
diff --git a/examples/spiral.py b/examples/spiral.py
index 7be9af2..b0f3952 100755
--- a/examples/spiral.py
+++ b/examples/spiral.py
@@ -7,21 +7,21 @@ import cairo
WIDTH, HEIGHT = 600, 600
def draw_spiral (ctx, width, height):
- wd = .02 * width
- hd = .02 * height
+ wd = .02 * width
+ hd = .02 * height
- width -= 2
- height -= 2
+ width -= 2
+ height -= 2
- ctx.move_to (width + 1, 1-hd)
- for i in range(9):
- ctx.rel_line_to (0, height - hd * (2 * i - 1))
- ctx.rel_line_to (- (width - wd * (2 *i)), 0)
- ctx.rel_line_to (0, - (height - hd * (2*i)))
- ctx.rel_line_to (width - wd * (2 * i + 1), 0)
+ ctx.move_to (width + 1, 1-hd)
+ for i in range(9):
+ ctx.rel_line_to (0, height - hd * (2 * i - 1))
+ ctx.rel_line_to (- (width - wd * (2 *i)), 0)
+ ctx.rel_line_to (0, - (height - hd * (2*i)))
+ ctx.rel_line_to (width - wd * (2 * i + 1), 0)
- ctx.set_source_rgb (0, 0, 1)
- ctx.stroke()
+ ctx.set_source_rgb (0, 0, 1)
+ ctx.stroke()
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
diff --git a/examples/warpedtext.py b/examples/warpedtext.py
index cd83ee7..0c8257d 100755
--- a/examples/warpedtext.py
+++ b/examples/warpedtext.py
@@ -4,44 +4,44 @@ import cairo
import math
def warpPath(ctx, function):
- first = True
-
- for type, points in ctx.copy_path():
- if type == cairo.PATH_MOVE_TO:
- if first:
- ctx.new_path()
- first = False
- x, y = function(*points)
- ctx.move_to(x, y)
-
- elif type == cairo.PATH_LINE_TO:
- x, y = function(*points)
- ctx.line_to(x, y)
-
- elif type == cairo.PATH_CURVE_TO:
- x1, y1, x2, y2, x3, y3 = points
- x1, y1 = function(x1, y1)
- x2, y2 = function(x2, y2)
- x3, y3 = function(x3, y3)
- ctx.curve_to(x1, y1, x2, y2, x3, y3)
-
- elif type == cairo.PATH_CLOSE_PATH:
- ctx.close_path()
+ first = True
+
+ for type, points in ctx.copy_path():
+ if type == cairo.PATH_MOVE_TO:
+ if first:
+ ctx.new_path()
+ first = False
+ x, y = function(*points)
+ ctx.move_to(x, y)
+
+ elif type == cairo.PATH_LINE_TO:
+ x, y = function(*points)
+ ctx.line_to(x, y)
+
+ elif type == cairo.PATH_CURVE_TO:
+ x1, y1, x2, y2, x3, y3 = points
+ x1, y1 = function(x1, y1)
+ x2, y2 = function(x2, y2)
+ x3, y3 = function(x3, y3)
+ ctx.curve_to(x1, y1, x2, y2, x3, y3)
+
+ elif type == cairo.PATH_CLOSE_PATH:
+ ctx.close_path()
def spiral(x, y):
- theta0 = -math.pi * 3 / 4
- theta = x / Width * math.pi * 2 + theta0
- radius = y + 200 - x/7
- xnew = radius*math.cos(theta)
- ynew = radius*math.sin(-theta)
- return xnew + Width/2, ynew + Height/2
+ theta0 = -math.pi * 3 / 4
+ theta = x / Width * math.pi * 2 + theta0
+ radius = y + 200 - x/7
+ xnew = radius*math.cos(theta)
+ ynew = radius*math.sin(-theta)
+ return xnew + Width/2, ynew + Height/2
def curl(x, y):
- xn = x - Textwidth/2
- #yn = y - Textheight/2
- xnew = xn
- ynew = y + xn ** 3 / ((Textwidth/2)**3) * 70
- return xnew + Width/2, ynew + Height*2/5
+ xn = x - Textwidth/2
+ #yn = y - Textheight/2
+ xnew = xn
+ ynew = y + xn ** 3 / ((Textwidth/2)**3) * 70
+ return xnew + Width/2, ynew + Height*2/5
Width, Height = 512, 512