- start of rewrite of pdfwriter
[PyX/mjg.git] / test / functional / test_path.py
blobb96901f6ee3dbd867e4624d67685073a25cd9c04
1 #!/usr/bin/env python
2 import sys; sys.path[:0] = ["../.."]
4 from pyx import *
5 from pyx.path import *
7 def bboxrect(cmd):
8 return cmd.bbox().rect()
11 def dotest(c, x, y, test):
12 c2 = c.insert(canvas.canvas([trafo.translate(x, y)]))
13 eval("%s(c2)" % test)
14 c.stroke(bboxrect(c2))
17 class cross(path):
18 def __init__(self, x, y):
19 self.path=[moveto(x,y),
20 rmoveto(-0.1, -0.1),
21 rlineto(0.2, 0.2),
22 rmoveto(-0.1, -0.1),
23 rmoveto(-0.1, +0.1),
24 rlineto(0.2, -0.2)]
27 def drawpathwbbox(c, p):
28 c.stroke(p, [color.rgb.red])
29 np = p.normpath()
30 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
31 c.stroke(bboxrect(p))
34 def testarcs(c):
35 def testarc(c, x, y, phi1, phi2):
36 p=path(arc(x,y, 0.5, phi1, phi2))
37 np = p.normpath()
38 c.stroke(p, [color.rgb.red])
39 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
41 def testarcn(c, x, y, phi1, phi2):
42 p=path(arcn(x,y, 0.5, phi1, phi2))
43 np = p.normpath()
44 c.stroke(p, [color.rgb.red])
45 c.stroke(np, [color.rgb.green, style.linestyle.dashed])
47 def testarct(c, r, x0, y0, dx1, dy1, dx2, dy2):
48 p=path(moveto(x0,y0), arct(x0+dx1,y0+dy1, x0+dx2, y0+dy2, r), rlineto(dx2-dx1, dy2-dy1), closepath())
49 np = p.normpath()
50 c.stroke(p, [color.rgb.red, style.linewidth.Thick])
51 c.stroke(np, [color.rgb.green, style.linewidth.THin, deco.filled([color.rgb.green])])
53 testarc(c, 1, 2, 0, 90)
54 testarc(c, 2, 2, -90, 90)
55 testarc(c, 3, 2, 270, 90)
56 testarc(c, 4, 2, 90, -90)
57 testarc(c, 5, 2, 90, 270)
58 testarc(c, 4, 3, 45, -90)
59 testarc(c, 2, 3, 45, -90-2*360)
60 testarc(c, 1, 3, 45, +90+2*360)
62 testarcn(c, 1, 5, 0, 90)
63 testarcn(c, 2, 5, -90, 90)
64 testarcn(c, 3, 5, 270, 90)
65 testarcn(c, 4, 5, 90, -90)
66 testarcn(c, 5, 5, 90, 270)
67 testarcn(c, 4, 6, 45, -90)
68 testarcn(c, 2, 6, 45, -90-360)
69 testarcn(c, 1, 6, 45, -90+360)
71 testarct(c, 0.5, 1, 8, 0, 1, 1, 1)
72 testarct(c, 0.5, 3, 8, 1, 1, 1, 2)
73 testarct(c, 0.5, 5, 8, 1, 0, 2, 1)
74 testarct(c, 0.5, 7, 8, 1, 0, 2, 0)
75 testarct(c, 0.0, 9, 8, 0, 1, 1, 1)
77 # testarct(c, 0.5, 11, 8, 0, 1, 0, 0) # not allowed
80 def testintersectbezier(c):
81 p=path(moveto(0,0), curveto(2,6,4,5,2,9)).normpath(epsilon=1e-4)
82 q=path(moveto(2,0), curveto(2,6,4,12,1,6)).normpath(epsilon=1e-4)
84 c.stroke(q, [style.linewidth.THIN])
85 c.stroke(p, [style.linewidth.THIN])
87 isect = p.intersect(q)
89 for i in isect[0]:
90 x, y = p.at(i)
91 c.stroke(cross(x, y), [style.linewidth.THIN])
93 def testintersectcircle(c):
94 k=circle(0, 0, 2)
95 l=line(0, 0, 3, 0)
96 c.stroke(k, [style.linewidth.THIN])
97 c.stroke(l, [style.linewidth.THIN])
99 isect = k.intersect(l)
100 assert len(isect[0])==1, "double count of intersections"
102 for i in isect[0]:
103 x, y = k.at(i)
104 c.stroke(cross(x, y), [style.linewidth.THIN])
106 def testintersectline(c):
107 l1=line(0, 0, 1, 1)
108 l2=line(0, 1, 1, 0)
109 c.stroke(l1, [style.linewidth.THIN])
110 c.stroke(l2, [style.linewidth.THIN])
112 isect = l1.intersect(l2)
114 for i in isect[0]:
115 x, y = l1.at(i)
116 c.stroke(circle(x, y, 0.1), [style.linewidth.THIN])
119 def testnormpathtrafo(c):
120 p = path(moveto(0, 5),
121 curveto(2, 1, 4, 0, 2, 4),
122 rcurveto(-3, 2, 1, 2, 3, 6),
123 rlineto(0, 3),
124 closepath())
126 c.stroke(p)
127 c.stroke(p.normpath(), [color.rgb.green, style.linestyle.dashed])
128 c.stroke(p, [trafo.translate(3, 1), color.rgb.red])
129 c.insert(canvas.canvas([trafo.translate(3,1)])).stroke(p,
130 [color.rgb.green,
131 style.linestyle.dashed])
133 c.stroke(p.reversed(), [color.rgb.blue, style.linestyle.dotted, style.linewidth.THick])
135 c.stroke(cross(*(p.at(0))))
136 c.stroke(cross(*(p.reversed().at(0))))
138 p1, p2 = p.split([1.0, 2.1])
139 c.stroke(p1, [color.rgb.red, style.linestyle.dashed])
140 c.stroke(p2, [color.rgb.green, style.linestyle.dashed])
142 circ1 = circle(0, 10, 1)
143 circ2 = circle(1.7, 10, 1)
145 c.stroke(circ1)
146 c.stroke(circ2)
148 isectcirc1, isectcirc2 = circ1.intersect(circ2)
149 segment1 = circ1.split(isectcirc1)[0]
150 segment2 = circ2.split(isectcirc2)[1]
152 segment = segment1 << segment2
153 segment[-1].close()
155 c.stroke(segment, [style.linewidth.THick, deco.filled([color.rgb.green])])
157 def testtangent(c):
158 p=path(moveto(0,5),
159 curveto(2,1,4,0,2,4),
160 rcurveto(-3,2,1,2,3,6),
161 rlineto(2,3)) + circle(5,5,1)
162 c.stroke(p, [style.linewidth.THick])
163 arclen = p.arclen()
164 points = 20
165 for i in range(points):
166 c.stroke(p.tangent(arclen*i/points, length=20*unit.t_pt), [color.rgb.blue, deco.earrow.normal])
167 c.stroke(line(0, 0, 1, 0).transformed(p.trafo(arclen*i/points)), [color.rgb.green, deco.earrow.normal])
168 c.stroke(line(0, 0, 0, 1).transformed(p.trafo(arclen*i/points)), [color.rgb.red, deco.earrow.normal])
170 # test the curvature
171 cc = canvas.canvas()
172 cc.insert(p)
173 cc = canvas.canvas([canvas.clip(cc.bbox().path())])
174 for i in range(points):
175 radius = p.curveradius(arclen*i/points)
176 if radius is not None:
177 radius = unit.tocm(radius)
178 pos = p.trafo(arclen*i/points).apply(0,radius*radius/abs(radius))
179 cc.stroke(circle(0, 0,unit.t_cm * abs(radius)), [color.grey(0.5), trafo.translate(*pos)])
180 c.insert(cc)
183 def testarcbbox(c):
184 for phi in range(0,360,30):
185 drawpathwbbox(c,path(arc(phi*0.1, phi*0.1, 1, 0, phi)))
187 for phi in range(0,360,30):
188 drawpathwbbox(c,path(arc(phi*0.1, 5+phi*0.1, 1, phi, 360)))
190 for phi in range(0,360,30):
191 drawpathwbbox(c,path(arc(phi*0.1, 10+phi*0.1, 1, phi, phi+30)))
193 for phi in range(0,360,30):
194 drawpathwbbox(c,path(arc(phi*0.1, 15+phi*0.1, 1, phi, phi+120)))
196 for phi in range(0,360,30):
197 drawpathwbbox(c,path(arc(phi*0.1, 20+phi*0.1, 1, phi, phi+210)))
199 for phi in range(0,360,30):
200 drawpathwbbox(c,path(arc(phi*0.1, 25+phi*0.1, 1, phi, phi+300)))
202 for phi in range(0,360,30):
203 drawpathwbbox(c,path(arc(phi*0.1, 30+phi*0.1, 1, phi, phi+390)))
206 for phi in range(0,360,30):
207 drawpathwbbox(c,path(moveto(20+phi*0.1, phi*0.09),
208 arc(20+phi*0.1, phi*0.1, 1, 0, phi)))
210 for phi in range(0,360,30):
211 drawpathwbbox(c,path(moveto(20+phi*0.1, 5+phi*0.11),
212 arc(20+phi*0.1, 5+phi*0.1, 1, 0, phi)))
214 for phi in range(0,360,30):
215 drawpathwbbox(c,path(moveto(20+phi*0.1, 10+phi*0.09),
216 arcn(20+phi*0.1, 10+phi*0.1, 1, 0, phi)))
218 for phi in range(0,360,30):
219 drawpathwbbox(c,path(moveto(20+phi*0.1, 15+phi*0.11),
220 arcn(20+phi*0.1, 15+phi*0.1, 1, 0, phi)))
222 for phi in range(0,360,30):
223 drawpathwbbox(c,path(moveto(50+phi*0.1, phi*0.09),
224 arc(50+phi*0.1, phi*0.1, 1, 0, phi),
225 rlineto(1,1)))
227 for phi in range(0,360,30):
228 drawpathwbbox(c,path(moveto(50+phi*0.1, 5+phi*0.11),
229 arc(50+phi*0.1, 5+phi*0.1, 1, 0, phi),
230 rlineto(1,1)))
232 for phi in range(0,360,30):
233 drawpathwbbox(c,path(moveto(50+phi*0.1, 10+phi*0.09),
234 arcn(50+phi*0.1, 10+phi*0.1, 1, 0, phi),
235 rlineto(1,1)))
237 for phi in range(0,360,30):
238 drawpathwbbox(c,path(moveto(50+phi*0.1, 15+phi*0.11),
239 arcn(50+phi*0.1, 15+phi*0.1, 1, 0, phi),
240 rlineto(1,1)))
243 def testcurvetobbox(c):
244 drawpathwbbox(c,path(moveto(10,60), curveto(12,66,14,65,12,69)))
247 def testtrafobbox(c):
248 sc=c.insert(canvas.canvas([trafo.translate(0,40).rotated(10)]))
250 p=path(moveto(10,10), curveto(12,16,14,15,12,19)); drawpathwbbox(sc,p)
251 p=path(moveto(5,17), curveto(6,18, 5,16, 7,15)); drawpathwbbox(sc,p)
254 def testclipbbox(c):
255 clip=canvas.clip(rect(11,11,10,5))
257 p1=path(moveto(10,10), curveto(12,16,14,15,12,19));
258 p2=path(moveto(12,12), curveto(6,18, 5,16, 7,15));
260 # just a simple test for clipping
261 sc=c.insert(canvas.canvas([clip]))
262 drawpathwbbox(sc,p1)
263 drawpathwbbox(sc,p2)
265 # more complicated operations
267 # 1. transformation followed by clipping:
268 # in this case, the clipping path will be evaluated in the
269 # context of the already transformed canvas, so that the
270 # actually displayed portion of the path should be the same
272 sc=c.insert(canvas.canvas([trafo.translate(5,0), clip]))
273 drawpathwbbox(sc,p1)
274 drawpathwbbox(sc,p2)
276 # 2. clipping followed by transformation
277 # in this case, the clipping path will not be transformed, so
278 # that the display portionof the path should change
280 sc=c.insert(canvas.canvas([clip, trafo.translate(1,1)]))
281 drawpathwbbox(sc,p1)
282 drawpathwbbox(sc,p2)
284 def testarclentoparam(c):
285 curve=path(moveto(0,0), lineto(0,5), curveto(5,0,0,10,5,5), closepath(),
286 moveto(5,0), lineto(10,5))
287 ll = curve.arclen()
288 # l=[-0.8*ll, -0.6*ll, -0.4*ll, -0.2*ll, 0, 0.1*ll, 0.3*ll, 0.5*ll, 0.7*ll, 0.9*ll]
289 l=[0, 0.1*ll, 0.2*ll, 0.3*ll, 0.4*ll, 0.5*ll, 0.6*ll, 0.7*ll, 0.8*ll, 0.9*ll]
290 cols=[color.gray.black, color.gray(0.3), color.gray(0.7), color.rgb.red,
291 color.rgb.green, color.rgb.blue, color.cmyk(1,0,0,0),
292 color.cmyk(0,1,0,0), color.cmyk(0,0,1,0), color.gray.black]
293 t=curve.arclentoparam(l)
294 c.stroke(curve)
295 for i in range(len(t)):
296 c.draw(path(circle(curve.at(t[i])[0], curve.at(t[i])[1], 0.1)), [deco.filled([cols[i]]), deco.stroked()])
299 c = canvas.canvas()
300 dotest(c, 0, 0, "testarcs")
301 dotest(c, 2, 12, "testintersectbezier")
302 dotest(c, 10,11, "testnormpathtrafo")
303 dotest(c, 12, -4, "testtangent")
304 dotest(c, 5, -4, "testintersectcircle")
305 dotest(c, 9, -4, "testintersectline")
306 dotest(c, 21, 12, "testarclentoparam")
307 c.writeEPSfile("test_path", paperformat=document.paperformat.A4, rotated=0, fittosize=1)
308 c.writePDFfile("test_path")
310 c = canvas.canvas()
311 testarcbbox(c)
312 testcurvetobbox(c)
313 testtrafobbox(c)
314 testclipbbox(c)
315 c.writeEPSfile("test_bbox", paperformat=document.paperformat.A4, rotated=1, fittosize=1)
316 c.writePDFfile("test_bbox")
317 #c.writeEPSfile("test_bbox")