568
|
1 |
#!/bin/env python
|
3617
|
2 |
#Copyright ReportLab Europe Ltd. 2000-2012
|
817
|
3 |
#see license.txt for license details
|
2332
|
4 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/testdrawings.py
|
568
|
5 |
__version__=''' $Id $ '''
|
3032
|
6 |
__doc__="""Defines some standard drawings to use as test cases
|
|
7 |
|
|
8 |
This contains a number of routines to generate test drawings
|
568
|
9 |
for reportlab/graphics. For now they are contrived, but we will expand them
|
|
10 |
to try and trip up any parser. Feel free to add more.
|
|
11 |
|
|
12 |
"""
|
|
13 |
|
|
14 |
from reportlab.graphics.shapes import *
|
|
15 |
from reportlab.lib import colors
|
|
16 |
|
|
17 |
def getDrawing1():
|
|
18 |
"""Hello World, on a rectangular background"""
|
1683
|
19 |
|
568
|
20 |
D = Drawing(400, 200)
|
1683
|
21 |
D.add(Rect(50, 50, 300, 100, fillColor=colors.yellow)) #round corners
|
568
|
22 |
D.add(String(180,100, 'Hello World', fillColor=colors.red))
|
|
23 |
|
1683
|
24 |
|
568
|
25 |
return D
|
|
26 |
|
|
27 |
|
|
28 |
def getDrawing2():
|
|
29 |
"""This demonstrates the basic shapes. There are
|
|
30 |
no groups or references. Each solid shape should have
|
|
31 |
a purple fill."""
|
|
32 |
D = Drawing(400, 200) #, fillColor=colors.purple)
|
1683
|
33 |
|
568
|
34 |
D.add(Line(10,10,390,190))
|
|
35 |
D.add(Circle(100,100,20, fillColor=colors.purple))
|
|
36 |
D.add(Circle(200,100,20, fillColor=colors.purple))
|
|
37 |
D.add(Circle(300,100,20, fillColor=colors.purple))
|
|
38 |
|
|
39 |
D.add(Wedge(330,100,40, -10,40, fillColor=colors.purple))
|
|
40 |
|
|
41 |
D.add(PolyLine([120,10,130,20,140,10,150,20,160,10,
|
1683
|
42 |
170,20,180,10,190,20,200,10]))
|
568
|
43 |
|
|
44 |
D.add(Polygon([300,20,350,20,390,80,300,75, 330, 40]))
|
|
45 |
|
|
46 |
D.add(Ellipse(50, 150, 40, 20))
|
|
47 |
|
|
48 |
D.add(Rect(120, 150, 60, 30,
|
|
49 |
strokeWidth=10,
|
|
50 |
strokeColor=colors.red,
|
|
51 |
fillColor=colors.yellow)) #square corners
|
1683
|
52 |
|
|
53 |
D.add(Rect(220, 150, 60, 30, 10, 10)) #round corners
|
568
|
54 |
|
|
55 |
D.add(String(10,50, 'Basic Shapes', fillColor=colors.black))
|
|
56 |
|
|
57 |
return D
|
|
58 |
|
|
59 |
|
|
60 |
##def getDrawing2():
|
|
61 |
## """This drawing uses groups. Each group has two circles and a comment.
|
|
62 |
## The line style is set at group level and should be red for the left,
|
|
63 |
## bvlue for the right."""
|
|
64 |
## D = Drawing(400, 200)
|
|
65 |
##
|
|
66 |
## Group1 = Group()
|
|
67 |
##
|
|
68 |
## Group1.add(String(50, 50, 'Group 1', fillColor=colors.black))
|
|
69 |
## Group1.add(Circle(75,100,25))
|
|
70 |
## Group1.add(Circle(125,100,25))
|
|
71 |
## D.add(Group1)
|
|
72 |
##
|
|
73 |
## Group2 = Group(
|
|
74 |
## String(250, 50, 'Group 2', fillColor=colors.black),
|
|
75 |
## Circle(275,100,25),
|
|
76 |
## Circle(325,100,25)#,
|
817
|
77 |
|
|
78 |
|
|
79 |
##def getDrawing2():
|
|
80 |
## """This drawing uses groups. Each group has two circles and a comment.
|
|
81 |
## The line style is set at group level and should be red for the left,
|
|
82 |
## bvlue for the right."""
|
|
83 |
## D = Drawing(400, 200)
|
|
84 |
##
|
|
85 |
## Group1 = Group()
|
|
86 |
##
|
|
87 |
## Group1.add(String(50, 50, 'Group 1', fillColor=colors.black))
|
|
88 |
## Group1.add(Circle(75,100,25))
|
|
89 |
## Group1.add(Circle(125,100,25))
|
|
90 |
## D.add(Group1)
|
|
91 |
##
|
|
92 |
## Group2 = Group(
|
|
93 |
## String(250, 50, 'Group 2', fillColor=colors.black),
|
|
94 |
## Circle(275,100,25),
|
|
95 |
## Circle(325,100,25)#,
|
568
|
96 |
##
|
|
97 |
## #group attributes
|
|
98 |
## #strokeColor=colors.blue
|
1683
|
99 |
## )
|
568
|
100 |
## D.add(Group2)
|
|
101 |
|
|
102 |
## return D
|
|
103 |
##
|
|
104 |
##
|
|
105 |
##def getDrawing3():
|
|
106 |
## """This uses a named reference object. The house is a 'subroutine'
|
|
107 |
## the basic brick colored walls are defined, but the roof and window
|
|
108 |
## color are undefined and may be set by the container."""
|
1683
|
109 |
##
|
568
|
110 |
## D = Drawing(400, 200, fill=colors.bisque)
|
|
111 |
##
|
1683
|
112 |
##
|
568
|
113 |
## House = Group(
|
|
114 |
## Rect(2,20,36,30, fill=colors.bisque), #walls
|
|
115 |
## Polygon([0,20,40,20,20,5]), #roof
|
|
116 |
## Rect(8, 38, 8, 12), #door
|
|
117 |
## Rect(25, 38, 8, 7), #window
|
|
118 |
## Rect(8, 25, 8, 7), #window
|
|
119 |
## Rect(25, 25, 8, 7) #window
|
1683
|
120 |
##
|
|
121 |
## )
|
568
|
122 |
## D.addDef('MyHouse', House)
|
|
123 |
##
|
|
124 |
## # one row all the same color
|
|
125 |
## D.add(String(20, 40, 'British Street...',fill=colors.black))
|
|
126 |
## for i in range(6):
|
|
127 |
## x = i * 50
|
|
128 |
## D.add(NamedReference('MyHouse',
|
|
129 |
## House,
|
|
130 |
## transform=translate(x, 40),
|
|
131 |
## fill = colors.brown
|
|
132 |
## )
|
|
133 |
## )
|
|
134 |
##
|
|
135 |
## # now do a row all different
|
|
136 |
## D.add(String(20, 120, 'Mediterranean Street...',fill=colors.black))
|
|
137 |
## x = 0
|
|
138 |
## for color in (colors.blue, colors.yellow, colors.orange,
|
|
139 |
## colors.red, colors.green, colors.chartreuse):
|
|
140 |
## D.add(NamedReference('MyHouse',
|
|
141 |
## House,
|
|
142 |
## transform=translate(x,120),
|
|
143 |
## fill = color,
|
|
144 |
## )
|
|
145 |
## )
|
|
146 |
## x = x + 50
|
|
147 |
## #..by popular demand, the mayor gets a big one at the end
|
|
148 |
## D.add(NamedReference('MyHouse',
|
|
149 |
## House,
|
|
150 |
## transform=mmult(translate(x,110), scale(1.2,1.2)),
|
|
151 |
## fill = color,
|
|
152 |
## )
|
|
153 |
## )
|
1683
|
154 |
##
|
|
155 |
##
|
568
|
156 |
## return D
|
|
157 |
##
|
|
158 |
##def getDrawing4():
|
|
159 |
## """This tests that attributes are 'unset' correctly when
|
|
160 |
## one steps back out of a drawing node. All the circles are part of a
|
|
161 |
## group setting the line color to blue; the second circle explicitly
|
|
162 |
## sets it to red. Ideally, the third circle should go back to blue."""
|
|
163 |
## D = Drawing(400, 200)
|
|
164 |
##
|
|
165 |
##
|
|
166 |
## G = Group(
|
|
167 |
## Circle(100,100,20),
|
|
168 |
## Circle(200,100,20, stroke=colors.blue),
|
|
169 |
## Circle(300,100,20),
|
|
170 |
## stroke=colors.red,
|
|
171 |
## stroke_width=3,
|
|
172 |
## fill=colors.aqua
|
|
173 |
## )
|
1683
|
174 |
## D.add(G)
|
568
|
175 |
##
|
1683
|
176 |
##
|
568
|
177 |
## D.add(String(10,50, 'Stack Unwinding - should be red, blue, red'))
|
|
178 |
##
|
|
179 |
## return D
|
|
180 |
##
|
|
181 |
##
|
|
182 |
##def getDrawing5():
|
|
183 |
## """This Rotates Coordinate Axes"""
|
|
184 |
## D = Drawing(400, 200)
|
1683
|
185 |
##
|
568
|
186 |
##
|
|
187 |
##
|
|
188 |
## Axis = Group(
|
|
189 |
## Line(0,0,100,0), #x axis
|
|
190 |
## Line(0,0,0,50), # y axis
|
|
191 |
## Line(0,10,10,10), #ticks on y axis
|
|
192 |
## Line(0,20,10,20),
|
|
193 |
## Line(0,30,10,30),
|
|
194 |
## Line(0,40,10,40),
|
|
195 |
## Line(10,0,10,10), #ticks on x axis
|
1683
|
196 |
## Line(20,0,20,10),
|
|
197 |
## Line(30,0,30,10),
|
|
198 |
## Line(40,0,40,10),
|
|
199 |
## Line(50,0,50,10),
|
|
200 |
## Line(60,0,60,10),
|
|
201 |
## Line(70,0,70,10),
|
|
202 |
## Line(80,0,80,10),
|
568
|
203 |
## Line(90,0,90,10),
|
|
204 |
## String(20, 35, 'Axes', fill=colors.black)
|
|
205 |
## )
|
|
206 |
##
|
1683
|
207 |
## D.addDef('Axes', Axis)
|
|
208 |
##
|
568
|
209 |
## D.add(NamedReference('Axis', Axis,
|
|
210 |
## transform=translate(10,10)))
|
|
211 |
## D.add(NamedReference('Axis', Axis,
|
|
212 |
## transform=mmult(translate(150,10),rotate(15)))
|
|
213 |
## )
|
|
214 |
## return D
|
|
215 |
##
|
|
216 |
##def getDrawing6():
|
|
217 |
## """This Rotates Text"""
|
|
218 |
## D = Drawing(400, 300, fill=colors.black)
|
|
219 |
##
|
|
220 |
## xform = translate(200,150)
|
|
221 |
## C = (colors.black,colors.red,colors.green,colors.blue,colors.brown,colors.gray, colors.pink,
|
|
222 |
## colors.lavender,colors.lime, colors.mediumblue, colors.magenta, colors.limegreen)
|
|
223 |
##
|
1683
|
224 |
## for i in range(12):
|
568
|
225 |
## D.add(String(0, 0, ' - - Rotated Text', fill=C[i%len(C)], transform=mmult(xform, rotate(30*i))))
|
1683
|
226 |
##
|
568
|
227 |
## return D
|
|
228 |
##
|
|
229 |
##def getDrawing7():
|
|
230 |
## """This defines and tests a simple UserNode0 (the trailing zero denotes
|
|
231 |
## an experimental method which is not part of the supported API yet).
|
|
232 |
## Each of the four charts is a subclass of UserNode which generates a random
|
|
233 |
## series when rendered."""
|
|
234 |
##
|
|
235 |
## class MyUserNode(UserNode0):
|
|
236 |
## import whrandom, math
|
1683
|
237 |
##
|
568
|
238 |
##
|
|
239 |
## def provideNode(self, sender):
|
|
240 |
## """draw a simple chart that changes everytime it's drawn"""
|
|
241 |
## # print "here's a random number %s" % self.whrandom.random()
|
|
242 |
## #print "MyUserNode.provideNode being called by %s" % sender
|
|
243 |
## g = Group()
|
|
244 |
## #g._state = self._state # this is naughty
|
|
245 |
## PingoNode.__init__(g, self._state) # is this less naughty ?
|
|
246 |
## w = 80.0
|
|
247 |
## h = 50.0
|
|
248 |
## g.add(Rect(0,0, w, h, stroke=colors.black))
|
|
249 |
## N = 10.0
|
|
250 |
## x,y = (0,h)
|
|
251 |
## dx = w/N
|
|
252 |
## for ii in range(N):
|
|
253 |
## dy = (h/N) * self.whrandom.random()
|
|
254 |
## g.add(Line(x,y,x+dx, y-dy))
|
|
255 |
## x = x + dx
|
|
256 |
## y = y - dy
|
|
257 |
## return g
|
|
258 |
##
|
|
259 |
## D = Drawing(400,200, fill=colors.white) # AR - same size as others
|
1683
|
260 |
##
|
568
|
261 |
## D.add(MyUserNode())
|
|
262 |
##
|
|
263 |
## graphcolor= [colors.green, colors.red, colors.brown, colors.purple]
|
|
264 |
## for ii in range(4):
|
|
265 |
## D.add(Group( MyUserNode(stroke=graphcolor[ii], stroke_width=2),
|
|
266 |
## transform=translate(ii*90,0) ))
|
|
267 |
##
|
|
268 |
## #un = MyUserNode()
|
|
269 |
## #print un.provideNode()
|
|
270 |
## return D
|
|
271 |
##
|
|
272 |
##def getDrawing8():
|
|
273 |
## """Test Path operations--lineto, curveTo, etc."""
|
|
274 |
## D = Drawing(400, 200, fill=None, stroke=colors.purple, stroke_width=2)
|
|
275 |
##
|
|
276 |
## xform = translate(200,100)
|
|
277 |
## C = (colors.black,colors.red,colors.green,colors.blue,colors.brown,colors.gray, colors.pink,
|
|
278 |
## colors.lavender,colors.lime, colors.mediumblue, colors.magenta, colors.limegreen)
|
|
279 |
## p = Path(50,50)
|
|
280 |
## p.lineTo(100,100)
|
|
281 |
## p.moveBy(-25,25)
|
|
282 |
## p.curveTo(150,125, 125,125, 200,50)
|
|
283 |
## p.curveTo(175, 75, 175, 98, 62, 87)
|
|
284 |
##
|
|
285 |
##
|
|
286 |
## D.add(p)
|
|
287 |
## D.add(String(10,30, 'Tests of path elements-lines and bezier curves-and text formating'))
|
|
288 |
## D.add(Line(220,150, 220,200, stroke=colors.red))
|
|
289 |
## D.add(String(220,180, "Text should be centered", text_anchor="middle") )
|
|
290 |
##
|
1683
|
291 |
##
|
568
|
292 |
## return D
|
1683
|
293 |
|
568
|
294 |
|
|
295 |
if __name__=='__main__':
|
3721
|
296 |
print(__doc__)
|