Menu

[r3358]: / trunk / htdocs / examples / glyph_to_path.py  Maximize  Restore  History

Download this file

85 lines (64 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import matplotlib
from matplotlib.ft2font import FT2Font
import matplotlib.agg as agg
MOVETO, LINETO, CURVE3, CURVE4, ENDPOLY = range(5)
def glyph_to_agg_path(glyph):
path = agg.path_storage()
for tup in glyph.path:
print tup
code = tup[0]
if code == MOVETO:
x,y = tup[1:]
path.move_to(x,y)
elif code == LINETO:
x,y = tup[1:]
path.line_to(x,y)
elif code == CURVE3:
xctl, yctl, xto, yto= tup[1:]
path.curve3(xctl, yctl, xto, yto)
elif code == CURVE4:
xctl1, yctl1, xctl2, yctl2, xto, yto= tup[1:]
path.curve4(xctl1, yct1, xctl2, yctl2, xto, yto)
elif code == ENDPOLY:
path.end_poly()
return path
width, height = 300,300
fname = os.path.join(matplotlib.get_data_path(), 'Vera.ttf')
font = FT2Font(fname)
glyph = font.load_char(ord('y'))
path = glyph_to_agg_path(glyph)
curve = agg.conv_curve_path(path)
scaling = agg.trans_affine_scaling(20,20)
translation = agg.trans_affine_translation(4,4)
rotation = agg.trans_affine_rotation(3.1415926)
mtrans = translation*scaling # cannot use this as a temporary
tpath = agg.conv_transform_path(path, mtrans)
curve = agg.conv_curve_trans(tpath)
stride = width*4
buffer = agg.buffer(width, height, stride)
rbuf = agg.rendering_buffer()
rbuf.attachb(buffer)
red = agg.rgba8(255,0,0,255)
blue = agg.rgba8(0,0,255,255)
white = agg.rgba8(255,255,255,255)
pf = agg.pixel_format_rgba(rbuf)
rbase = agg.renderer_base_rgba(pf)
rbase.clear_rgba8(white)
renderer = agg.renderer_scanline_aa_solid_rgba(rbase);
rasterizer = agg.rasterizer_scanline_aa()
scanline = agg.scanline_p8()
# first fill
rasterizer.add_path(curve)
renderer.color_rgba8(blue)
agg.render_scanlines_rgba(rasterizer, scanline, renderer);
# then stroke
stroke = agg.conv_stroke_curvetrans(curve)
stroke.width(2.0)
renderer.color_rgba8( red )
rasterizer.add_path(stroke)
agg.render_scanlines_rgba(rasterizer, scanline, renderer);
s = buffer.to_string()
import Image
im = Image.fromstring( "RGBA", (width, height), s)
im.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.