canvas gsave/restore reorganization
[PyX/mjg.git] / test / functional / test_path.py
blobec03dc53ceb8883b210d9cdb80fd50d37b4f2d9c
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 bbox=cmd.bbox()
9 return rect("%f t pt" % bbox.llx, "%f t pt" % bbox.lly,
10 "%f t pt" % (bbox.urx-bbox.llx), "%f t pt" % (bbox.ury-bbox.lly))
13 def dotest(c, x, y, test):
14 c2 = c.insert(canvas.canvas(trafo.translate(x, y)))
15 eval("%s(c2)" % test)
16 c.stroke(bboxrect(c2))
19 class cross(path):
20 def __init__(self, x, y):
21 self.path=[moveto(x,y),
22 rmoveto(-0.1, -0.1),
23 rlineto(0.2, 0.2),
24 rmoveto(-0.1, -0.1),
25 rmoveto(-0.1, +0.1),
26 rlineto(0.2, -0.2)]
29 def drawpathwbbox(c, p):
30 c.stroke(p, color.rgb.red)
31 np=normpath(p)
32 c.stroke(np, color.rgb.green, canvas.linestyle.dashed)
33 c.stroke(bboxrect(p))
36 def testarcs(c):
37 def testarc(c, x, y, phi1, phi2):
38 p=path(arc(x,y, 0.5, phi1, phi2))
39 np=normpath(p)
40 c.stroke(p, color.rgb.red)
41 c.stroke(np, color.rgb.green, canvas.linestyle.dashed)
43 def testarcn(c, x, y, phi1, phi2):
44 p=path(arcn(x,y, 0.5, phi1, phi2))
45 np=normpath(p)
46 c.stroke(p, color.rgb.red)
47 c.stroke(np, color.rgb.green, canvas.linestyle.dashed)
49 def testarct(c, r, x0, y0, dx1, dy1, dx2, dy2):
50 p=path(moveto(x0,y0), arct(x0+dx1,y0+dy1, x0+dx2, y0+dy2, r), rlineto(dx2-dx1, dy2-dy1), closepath())
51 np=normpath(p)
52 c.stroke(p, color.rgb.red, canvas.linewidth.Thick)
53 c.stroke(np, color.rgb.green, canvas.linewidth.THin, canvas.filled(color.rgb.green))
55 testarc(c, 1, 2, 0, 90)
56 testarc(c, 2, 2, -90, 90)
57 testarc(c, 3, 2, 270, 90)
58 testarc(c, 4, 2, 90, -90)
59 testarc(c, 5, 2, 90, 270)
60 testarc(c, 4, 3, 45, -90)
61 testarc(c, 2, 3, 45, -90-2*360)
62 testarc(c, 1, 3, 45, +90+2*360)
64 testarcn(c, 1, 5, 0, 90)
65 testarcn(c, 2, 5, -90, 90)
66 testarcn(c, 3, 5, 270, 90)
67 testarcn(c, 4, 5, 90, -90)
68 testarcn(c, 5, 5, 90, 270)
69 testarcn(c, 4, 6, 45, -90)
70 testarcn(c, 2, 6, 45, -90-360)
71 testarcn(c, 1, 6, 45, -90+360)
73 testarct(c, 0.5, 1, 8, 0, 1, 1, 1)
74 testarct(c, 0.5, 3, 8, 1, 1, 1, 2)
75 testarct(c, 0.5, 5, 8, 1, 0, 2, 1)
76 testarct(c, 0.5, 7, 8, 1, 0, 2, 0)
77 testarct(c, 0.0, 9, 8, 0, 1, 1, 1)
79 # testarct(c, 0.5, 11, 8, 0, 1, 0, 0) # not allowed
82 def testmidpointsplit(c):
83 p=path(moveto(1,1), rlineto(2,2), arc(5,2,1,30,300), closepath())
84 bpsplit=p.bpath().MidPointSplit()
85 c.stroke(p, color.rgb.red)
86 c.stroke(bpsplit, color.rgb.green, canvas.linestyle.dashed)
89 def testintersectbezier(c):
90 p=normpath(moveto(0,0), curveto(2,6,4,5,2,9))
91 q=normpath(moveto(2,0), curveto(2,6,4,12,1,6))
93 c.stroke(q, canvas.linewidth.THIN)
94 c.stroke(p, canvas.linewidth.THIN)
96 isect = p.intersect(q, epsilon=1e-4)
98 for i in isect:
99 x, y = p.at(i[0])
100 c.stroke(cross(x, y), canvas.linewidth.THIN)
103 def testnormpathtrafo(c):
104 p=path(moveto(0,5),
105 curveto(2,1,4,0,2,4),
106 rcurveto(-3,2,1,2,3,6),
107 rlineto(2,3), closepath())
110 c.stroke(p.transformed(trafo.translate(3,1)), color.rgb.red)
111 c.insert(canvas.canvas(trafo.translate(3,1))).draw(p,
112 color.rgb.green,
113 canvas.linestyle.dashed)
115 c.stroke(p)
116 c.stroke(p.reversed())
118 c.stroke(cross(*(p.at(0))))
119 c.stroke(cross(*(p.reversed().at(0))))
120 c.stroke(p.tangent(0, "30 pt"), canvas.earrow.normal)
121 c.stroke(p.reversed().tangent(0, "30 pt"), canvas.earrow.normal)
123 # p1, p2, p3 = p.split(1.0, 2.1)
124 p1, p2 = p.split(1.0, 2.1)
125 c.stroke(p1, color.rgb.red, canvas.linestyle.dashed)
126 c.stroke(p2, color.rgb.green, canvas.linestyle.dashed)
127 # c.stroke(p3, color.rgb.blue, canvas.linestyle.dashed)
129 circ1 = circle(0, 10, 1)
130 circ2 = circle(1.7, 10, 1)
132 c.stroke(circ1)
133 c.stroke(circ2)
135 isectcirc1, isectcirc2 = circ1.intersect(circ2)
136 segment1 = circ1.split(*isectcirc1)[0]
137 segment2 = circ2.split(*isectcirc2)[1]
139 segment = segment1 << segment2
140 segment.append(closepath())
142 c.stroke(segment, canvas.linewidth.THick, canvas.filled(color.rgb.green))
145 def testtangent(c):
146 p=path(moveto(0,5),
147 curveto(2,1,4,0,2,4),
148 rcurveto(-3,2,1,2,3,6),
149 rlineto(2,3))+circle(5,5,1)
150 c.stroke(p)
151 for i in range(int(p.range())*2):
152 c.stroke(p.tangent(i/2.0, "20 t pt"), color.rgb.blue, canvas.earrow.normal)
155 def testarcbbox(c):
156 for phi in range(0,360,30):
157 drawpathwbbox(c,path(arc(phi*0.1, phi*0.1, 1, 0, phi)))
159 for phi in range(0,360,30):
160 drawpathwbbox(c,path(arc(phi*0.1, 5+phi*0.1, 1, phi, 360)))
162 for phi in range(0,360,30):
163 drawpathwbbox(c,path(arc(phi*0.1, 10+phi*0.1, 1, phi, phi+30)))
165 for phi in range(0,360,30):
166 drawpathwbbox(c,path(arc(phi*0.1, 15+phi*0.1, 1, phi, phi+120)))
168 for phi in range(0,360,30):
169 drawpathwbbox(c,path(arc(phi*0.1, 20+phi*0.1, 1, phi, phi+210)))
171 for phi in range(0,360,30):
172 drawpathwbbox(c,path(arc(phi*0.1, 25+phi*0.1, 1, phi, phi+300)))
174 for phi in range(0,360,30):
175 drawpathwbbox(c,path(arc(phi*0.1, 30+phi*0.1, 1, phi, phi+390)))
178 for phi in range(0,360,30):
179 drawpathwbbox(c,path(moveto(20+phi*0.1, phi*0.09),
180 arc(20+phi*0.1, phi*0.1, 1, 0, phi)))
182 for phi in range(0,360,30):
183 drawpathwbbox(c,path(moveto(20+phi*0.1, 5+phi*0.11),
184 arc(20+phi*0.1, 5+phi*0.1, 1, 0, phi)))
186 for phi in range(0,360,30):
187 drawpathwbbox(c,path(moveto(20+phi*0.1, 10+phi*0.09),
188 arcn(20+phi*0.1, 10+phi*0.1, 1, 0, phi)))
190 for phi in range(0,360,30):
191 drawpathwbbox(c,path(moveto(20+phi*0.1, 15+phi*0.11),
192 arcn(20+phi*0.1, 15+phi*0.1, 1, 0, phi)))
194 for phi in range(0,360,30):
195 drawpathwbbox(c,path(moveto(50+phi*0.1, phi*0.09),
196 arc(50+phi*0.1, phi*0.1, 1, 0, phi),
197 rlineto(1,1)))
199 for phi in range(0,360,30):
200 drawpathwbbox(c,path(moveto(50+phi*0.1, 5+phi*0.11),
201 arc(50+phi*0.1, 5+phi*0.1, 1, 0, phi),
202 rlineto(1,1)))
204 for phi in range(0,360,30):
205 drawpathwbbox(c,path(moveto(50+phi*0.1, 10+phi*0.09),
206 arcn(50+phi*0.1, 10+phi*0.1, 1, 0, phi),
207 rlineto(1,1)))
209 for phi in range(0,360,30):
210 drawpathwbbox(c,path(moveto(50+phi*0.1, 15+phi*0.11),
211 arcn(50+phi*0.1, 15+phi*0.1, 1, 0, phi),
212 rlineto(1,1)))
215 def testcurvetobbox(c):
216 drawpathwbbox(c,path(moveto(10,60), curveto(12,66,14,65,12,69)))
219 def testtrafobbox(c):
220 sc=c.insert(canvas.canvas(trafo.translate(0,40).rotated(10)))
222 p=path(moveto(10,10), curveto(12,16,14,15,12,19)); drawpathwbbox(sc,p)
223 p=path(moveto(5,17), curveto(6,18, 5,16, 7,15)); drawpathwbbox(sc,p)
226 def testclipbbox(c):
227 clip=canvas.clip(rect(11,11,10,5))
229 p1=path(moveto(10,10), curveto(12,16,14,15,12,19));
230 p2=path(moveto(12,12), curveto(6,18, 5,16, 7,15));
232 # just a simple test for clipping
233 sc=c.insert(canvas.canvas(clip))
234 drawpathwbbox(sc,p1)
235 drawpathwbbox(sc,p2)
237 # more complicated operations
239 # 1. transformation followed by clipping:
240 # in this case, the clipping path will be evaluated in the
241 # context of the already transformed canvas, so that the
242 # actually displayed portion of the path should be the same
244 sc=c.insert(canvas.canvas(trafo.translate(5,0), clip))
245 drawpathwbbox(sc,p1)
246 drawpathwbbox(sc,p2)
248 # 2. clipping followed by transformation
249 # in this case, the clipping path will not be transformed, so
250 # that the display portionof the path should change
252 sc=c.insert(canvas.canvas(clip, trafo.translate(1,1)))
253 drawpathwbbox(sc,p1)
254 drawpathwbbox(sc,p2)
257 c=canvas.canvas()
258 dotest(c, 0, 0, "testarcs")
259 # dotest(c, 12, 3, "testmidpointsplit")
260 dotest(c, 2, 12, "testintersectbezier")
261 dotest(c, 10,11, "testnormpathtrafo")
262 dotest(c, 12, -4, "testtangent")
263 c.writetofile("test_path", paperformat="a4", rotated=0, fittosize=1)
265 c=canvas.canvas()
266 testarcbbox(c)
267 testcurvetobbox(c)
268 testtrafobbox(c)
269 testclipbbox(c)
270 c.writetofile("test_bbox", paperformat="a4", rotated=1, fittosize=1)
271 #c.writetofile("test_bbox")