[cairo-commit] pycairo/examples warpedtext.py, NONE,
1.1 context-subclass.py, NONE, 1.1
Steve Chaplin
commit at pdx.freedesktop.org
Mon Nov 15 02:05:30 PST 2004
Committed by: stevech1097
Update of /cvs/cairo/pycairo/examples
In directory gabe:/tmp/cvs-serv9185/examples
Added Files:
warpedtext.py context-subclass.py
Log Message:
SC 15/11/2004
--- NEW FILE: warpedtext.py ---
#!/usr/bin/env python
import cairo
import math
class PathWarp(object):
def __init__(self, function, ctx):
self.function = function
self.ctx = ctx
def lineto(self, x, y):
x, y = self.function(x, y)
self.ctx.line_to(x, y)
def moveto(self, x, y):
if self.first:
self.ctx.new_path()
self.first = False
x, y = self.function(x, y)
self.ctx.move_to(x, y)
def curveto(self, x1, y1, x2, y2, x3, y3):
x1, y1 = self.function(x1, y1)
x2, y2 = self.function(x2, y2)
x3, y3 = self.function(x3, y3)
self.ctx.curve_to(x1, y1, x2, y2, x3, y3)
def closepath(self):
self.ctx.close_path()
def warpPath(self):
self.first = True
self.ctx.current_path(self.moveto, self.lineto, self.curveto, self.closepath)
WIDTH, HEIGHT = 512, 512
file = open("warpedtext.png", "wb")
ctx = cairo.Context()
ctx.set_target_png(file, cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx.identity_matrix()
solidpattern = ctx.pattern
# background
pat = cairo.Pattern(x0=0.0, y0=0.0, x1=0, y1=HEIGHT)
pat.add_color_stop (1, 0, 0, 0, 1)
pat.add_color_stop (0, 1, 1, 1, 1)
ctx.rectangle (0,0,WIDTH,HEIGHT)
ctx.set_pattern (pat)
ctx.fill ()
# foreground
ctx.set_pattern (solidpattern)
ctx.set_rgb_color(1,1,1)
# spiral text
ctx.scale_font(80)
ctx.move_to(0, 0)
ctx.text_path("pycairo - " + "spam " * 5)
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
warp = PathWarp(spiral, ctx)
warp.warpPath()
ctx.fill()
ctx.new_path()
# curly text
ctx.move_to(0, 0)
ctx.set_rgb_color(0.3, 0.3, 0.3)
text = "I am curly :)"
ctx.text_path(text)
textwidth, textheight = ctx.text_extents(text)[2:4]
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
warp = PathWarp(curl, ctx)
warp.warpPath()
ctx.fill()
ctx.show_page()
--- NEW FILE: context-subclass.py ---
#!/usr/bin/env python
"""test/example of subclassing cairo.Context
"""
import cairo
WIDTH, HEIGHT = 400, 400
class MyContext(cairo.Context):
def triangle(self, x, y, size):
self.new_path()
self.move_to(x, y)
self.rel_line_to(size, 2*size)
self.rel_line_to(-2*size, 0)
self.close_path()
def bowtie(self, x, y, size):
self.new_path()
self.move_to(x, y)
self.rel_line_to(2*size, 2*size)
self.rel_line_to(-2*size, 0)
self.rel_line_to(2*size, -2*size)
self.close_path()
def draw(self):
self.rectangle (0, 0, WIDTH, HEIGHT)
self.set_rgb_color (1, 1, 1)
self.fill()
self.triangle (WIDTH/2, 20, 50)
self.set_rgb_color (1, 0, 0)
self.set_line_width(6)
self.fill()
self.bowtie (WIDTH/2, 150, 50)
self.set_rgb_color (0, 1, 0)
self.stroke()
try:
fileObject = file('context-subclass.png', 'wb')
except IOError, exc:
raise SystemExit("%s: %s" % (exc.filename, exc.strerror))
ctx = MyContext()
ctx.set_target_png (fileObject, cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx.draw()
ctx.show_page()
More information about the cairo-commit
mailing list