author | robin |
Tue, 19 Nov 2013 13:50:34 +0000 | |
branch | py33 |
changeset 3794 | 398ea04239b5 |
parent 3723 | 99aa837b6703 |
child 3800 | e8547b00eb59 |
permissions | -rw-r--r-- |
2963 | 1 |
testannotations=""" |
2 |
def annotations(canvas): |
|
3 |
from reportlab.lib.units import inch |
|
4 |
canvas.drawString(inch, 2.5*inch, |
|
5 |
"setAuthor, setTitle, setSubject have no visible effect") |
|
6 |
canvas.drawString(inch, inch, "But if you are viewing this document dynamically") |
|
7 |
canvas.drawString(inch, 0.5*inch, "please look at File/Document Info") |
|
8 |
canvas.setAuthor("the ReportLab Team") |
|
9 |
canvas.setTitle("ReportLab PDF Generation User Guide") |
|
10 |
canvas.setSubject("How to Generate PDF files using the ReportLab modules") |
|
11 |
""" |
|
12 |
||
13 |
# magic function making module |
|
14 |
||
15 |
test1 = """ |
|
16 |
def f(a,b): |
|
17 |
print "it worked", a, b |
|
18 |
return a+b |
|
19 |
""" |
|
20 |
||
21 |
test2 = """ |
|
22 |
def g(n): |
|
23 |
if n==0: return 1 |
|
24 |
else: return n*g(n-1) |
|
25 |
""" |
|
26 |
||
27 |
testhello = """ |
|
28 |
def hello(c): |
|
29 |
from reportlab.lib.units import inch |
|
30 |
# move the origin up and to the left |
|
31 |
c.translate(inch,inch) |
|
32 |
# define a large font |
|
33 |
c.setFont("Helvetica", 14) |
|
34 |
# choose some colors |
|
35 |
c.setStrokeColorRGB(0.2,0.5,0.3) |
|
36 |
c.setFillColorRGB(1,0,1) |
|
37 |
# draw some lines |
|
38 |
c.line(0,0,0,1.7*inch) |
|
39 |
c.line(0,0,1*inch,0) |
|
40 |
# draw a rectangle |
|
41 |
c.rect(0.2*inch,0.2*inch,1*inch,1.5*inch, fill=1) |
|
42 |
# make text go straight up |
|
43 |
c.rotate(90) |
|
44 |
# change color |
|
45 |
c.setFillColorRGB(0,0,0.77) |
|
46 |
# say hello (note after rotate the y coord needs to be negative!) |
|
47 |
c.drawString(0.3*inch, -inch, "Hello World") |
|
48 |
""" |
|
49 |
||
50 |
testcoords = """ |
|
51 |
def coords(canvas): |
|
52 |
from reportlab.lib.units import inch |
|
53 |
from reportlab.lib.colors import pink, black, red, blue, green |
|
54 |
c = canvas |
|
55 |
c.setStrokeColor(pink) |
|
56 |
c.grid([inch, 2*inch, 3*inch, 4*inch], [0.5*inch, inch, 1.5*inch, 2*inch, 2.5*inch]) |
|
57 |
c.setStrokeColor(black) |
|
58 |
c.setFont("Times-Roman", 20) |
|
59 |
c.drawString(0,0, "(0,0) the Origin") |
|
60 |
c.drawString(2.5*inch, inch, "(2.5,1) in inches") |
|
61 |
c.drawString(4*inch, 2.5*inch, "(4, 2.5)") |
|
62 |
c.setFillColor(red) |
|
63 |
c.rect(0,2*inch,0.2*inch,0.3*inch, fill=1) |
|
64 |
c.setFillColor(green) |
|
65 |
c.circle(4.5*inch, 0.4*inch, 0.2*inch, fill=1) |
|
66 |
""" |
|
67 |
||
68 |
testtranslate = """ |
|
69 |
def translate(canvas): |
|
70 |
from reportlab.lib.units import cm |
|
71 |
canvas.translate(2.3*cm, 0.3*cm) |
|
72 |
coords(canvas) |
|
73 |
""" |
|
74 |
||
75 |
testscale = """ |
|
76 |
def scale(canvas): |
|
77 |
canvas.scale(0.75, 0.5) |
|
78 |
coords(canvas) |
|
79 |
""" |
|
80 |
||
81 |
testscaletranslate = """ |
|
82 |
def scaletranslate(canvas): |
|
83 |
from reportlab.lib.units import inch |
|
84 |
canvas.setFont("Courier-BoldOblique", 12) |
|
85 |
# save the state |
|
86 |
canvas.saveState() |
|
87 |
# scale then translate |
|
88 |
canvas.scale(0.3, 0.5) |
|
89 |
canvas.translate(2.4*inch, 1.5*inch) |
|
90 |
canvas.drawString(0, 2.7*inch, "Scale then translate") |
|
91 |
coords(canvas) |
|
92 |
# forget the scale and translate... |
|
93 |
canvas.restoreState() |
|
94 |
# translate then scale |
|
95 |
canvas.translate(2.4*inch, 1.5*inch) |
|
96 |
canvas.scale(0.3, 0.5) |
|
97 |
canvas.drawString(0, 2.7*inch, "Translate then scale") |
|
98 |
coords(canvas) |
|
99 |
""" |
|
100 |
||
101 |
testmirror = """ |
|
102 |
def mirror(canvas): |
|
103 |
from reportlab.lib.units import inch |
|
104 |
canvas.translate(5.5*inch, 0) |
|
105 |
canvas.scale(-1.0, 1.0) |
|
106 |
coords(canvas) |
|
107 |
""" |
|
108 |
||
109 |
testcolors = """ |
|
110 |
def colors(canvas): |
|
111 |
from reportlab.lib import colors |
|
112 |
from reportlab.lib.units import inch |
|
113 |
black = colors.black |
|
114 |
y = x = 0; dy=inch*3/4.0; dx=inch*5.5/5; w=h=dy/2; rdx=(dx-w)/2 |
|
115 |
rdy=h/5.0; texty=h+2*rdy |
|
116 |
canvas.setFont("Helvetica",10) |
|
117 |
for [namedcolor, name] in ( |
|
118 |
[colors.lavenderblush, "lavenderblush"], |
|
119 |
[colors.lawngreen, "lawngreen"], |
|
120 |
[colors.lemonchiffon, "lemonchiffon"], |
|
121 |
[colors.lightblue, "lightblue"], |
|
122 |
[colors.lightcoral, "lightcoral"]): |
|
123 |
canvas.setFillColor(namedcolor) |
|
124 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1) |
|
125 |
canvas.setFillColor(black) |
|
126 |
canvas.drawCentredString(x+dx/2, y+texty, name) |
|
127 |
x = x+dx |
|
128 |
y = y + dy; x = 0 |
|
129 |
for rgb in [(1,0,0), (0,1,0), (0,0,1), (0.5,0.3,0.1), (0.4,0.5,0.3)]: |
|
130 |
r,g,b = rgb |
|
131 |
canvas.setFillColorRGB(r,g,b) |
|
132 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1) |
|
133 |
canvas.setFillColor(black) |
|
134 |
canvas.drawCentredString(x+dx/2, y+texty, "r%s g%s b%s"%rgb) |
|
135 |
x = x+dx |
|
136 |
y = y + dy; x = 0 |
|
137 |
for cmyk in [(1,0,0,0), (0,1,0,0), (0,0,1,0), (0,0,0,1), (0,0,0,0)]: |
|
138 |
c,m,y1,k = cmyk |
|
139 |
canvas.setFillColorCMYK(c,m,y1,k) |
|
140 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1) |
|
141 |
canvas.setFillColor(black) |
|
142 |
canvas.drawCentredString(x+dx/2, y+texty, "c%s m%s y%s k%s"%cmyk) |
|
143 |
x = x+dx |
|
144 |
y = y + dy; x = 0 |
|
145 |
for gray in (0.0, 0.25, 0.50, 0.75, 1.0): |
|
146 |
canvas.setFillGray(gray) |
|
147 |
canvas.rect(x+rdx, y+rdy, w, h, fill=1) |
|
148 |
canvas.setFillColor(black) |
|
149 |
canvas.drawCentredString(x+dx/2, y+texty, "gray: %s"%gray) |
|
150 |
x = x+dx |
|
151 |
""" |
|
152 |
||
153 |
testspumoni = """ |
|
154 |
def spumoni(canvas): |
|
155 |
from reportlab.lib.units import inch |
|
156 |
from reportlab.lib.colors import pink, green, brown, white |
|
157 |
x = 0; dx = 0.4*inch |
|
158 |
for i in range(4): |
|
159 |
for color in (pink, green, brown): |
|
160 |
canvas.setFillColor(color) |
|
161 |
canvas.rect(x,0,dx,3*inch,stroke=0,fill=1) |
|
162 |
x = x+dx |
|
163 |
canvas.setFillColor(white) |
|
164 |
canvas.setStrokeColor(white) |
|
165 |
canvas.setFont("Helvetica-Bold", 85) |
|
166 |
canvas.drawCentredString(2.75*inch, 1.3*inch, "SPUMONI") |
|
167 |
""" |
|
168 |
||
169 |
testspumoni2 = """ |
|
170 |
def spumoni2(canvas): |
|
171 |
from reportlab.lib.units import inch |
|
172 |
from reportlab.lib.colors import pink, green, brown, white, black |
|
173 |
# draw the previous drawing |
|
174 |
spumoni(canvas) |
|
175 |
# now put an ice cream cone on top of it: |
|
176 |
# first draw a triangle (ice cream cone) |
|
177 |
p = canvas.beginPath() |
|
178 |
xcenter = 2.75*inch |
|
179 |
radius = 0.45*inch |
|
180 |
p.moveTo(xcenter-radius, 1.5*inch) |
|
181 |
p.lineTo(xcenter+radius, 1.5*inch) |
|
182 |
p.lineTo(xcenter, 0) |
|
183 |
canvas.setFillColor(brown) |
|
184 |
canvas.setStrokeColor(black) |
|
185 |
canvas.drawPath(p, fill=1) |
|
186 |
# draw some circles (scoops) |
|
187 |
y = 1.5*inch |
|
188 |
for color in (pink, green, brown): |
|
189 |
canvas.setFillColor(color) |
|
190 |
canvas.circle(xcenter, y, radius, fill=1) |
|
191 |
y = y+radius |
|
192 |
""" |
|
193 |
||
194 |
testbezier = """ |
|
195 |
def bezier(canvas): |
|
196 |
from reportlab.lib.colors import yellow, green, red, black |
|
197 |
from reportlab.lib.units import inch |
|
198 |
i = inch |
|
199 |
d = i/4 |
|
200 |
# define the bezier curve control points |
|
201 |
x1,y1, x2,y2, x3,y3, x4,y4 = d,1.5*i, 1.5*i,d, 3*i,d, 5.5*i-d,3*i-d |
|
202 |
# draw a figure enclosing the control points |
|
203 |
canvas.setFillColor(yellow) |
|
204 |
p = canvas.beginPath() |
|
205 |
p.moveTo(x1,y1) |
|
206 |
for (x,y) in [(x2,y2), (x3,y3), (x4,y4)]: |
|
207 |
p.lineTo(x,y) |
|
208 |
canvas.drawPath(p, fill=1, stroke=0) |
|
209 |
# draw the tangent lines |
|
210 |
canvas.setLineWidth(inch*0.1) |
|
211 |
canvas.setStrokeColor(green) |
|
212 |
canvas.line(x1,y1,x2,y2) |
|
213 |
canvas.setStrokeColor(red) |
|
214 |
canvas.line(x3,y3,x4,y4) |
|
215 |
# finally draw the curve |
|
216 |
canvas.setStrokeColor(black) |
|
217 |
canvas.bezier(x1,y1, x2,y2, x3,y3, x4,y4) |
|
218 |
""" |
|
219 |
||
220 |
testbezier2 = """ |
|
221 |
def bezier2(canvas): |
|
222 |
from reportlab.lib.colors import yellow, green, red, black |
|
223 |
from reportlab.lib.units import inch |
|
224 |
# make a sequence of control points |
|
225 |
xd,yd = 5.5*inch/2, 3*inch/2 |
|
226 |
xc,yc = xd,yd |
|
227 |
dxdy = [(0,0.33), (0.33,0.33), (0.75,1), (0.875,0.875), |
|
228 |
(0.875,0.875), (1,0.75), (0.33,0.33), (0.33,0)] |
|
229 |
pointlist = [] |
|
230 |
for xoffset in (1,-1): |
|
231 |
yoffset = xoffset |
|
232 |
for (dx,dy) in dxdy: |
|
233 |
px = xc + xd*xoffset*dx |
|
234 |
py = yc + yd*yoffset*dy |
|
235 |
pointlist.append((px,py)) |
|
236 |
yoffset = -xoffset |
|
237 |
for (dy,dx) in dxdy: |
|
238 |
px = xc + xd*xoffset*dx |
|
239 |
py = yc + yd*yoffset*dy |
|
240 |
pointlist.append((px,py)) |
|
241 |
# draw tangent lines and curves |
|
242 |
canvas.setLineWidth(inch*0.1) |
|
243 |
while pointlist: |
|
244 |
[(x1,y1),(x2,y2),(x3,y3),(x4,y4)] = pointlist[:4] |
|
245 |
del pointlist[:4] |
|
246 |
canvas.setLineWidth(inch*0.1) |
|
247 |
canvas.setStrokeColor(green) |
|
248 |
canvas.line(x1,y1,x2,y2) |
|
249 |
canvas.setStrokeColor(red) |
|
250 |
canvas.line(x3,y3,x4,y4) |
|
251 |
# finally draw the curve |
|
252 |
canvas.setStrokeColor(black) |
|
253 |
canvas.bezier(x1,y1, x2,y2, x3,y3, x4,y4) |
|
254 |
""" |
|
255 |
||
256 |
testpencil = """ |
|
257 |
def pencil(canvas, text="No.2"): |
|
258 |
from reportlab.lib.colors import yellow, red, black,white |
|
259 |
from reportlab.lib.units import inch |
|
260 |
u = inch/10.0 |
|
261 |
canvas.setStrokeColor(black) |
|
262 |
canvas.setLineWidth(4) |
|
263 |
# draw erasor |
|
264 |
canvas.setFillColor(red) |
|
265 |
canvas.circle(30*u, 5*u, 5*u, stroke=1, fill=1) |
|
266 |
# draw all else but the tip (mainly rectangles with different fills) |
|
267 |
canvas.setFillColor(yellow) |
|
268 |
canvas.rect(10*u,0,20*u,10*u, stroke=1, fill=1) |
|
269 |
canvas.setFillColor(black) |
|
270 |
canvas.rect(23*u,0,8*u,10*u,fill=1) |
|
271 |
canvas.roundRect(14*u, 3.5*u, 8*u, 3*u, 1.5*u, stroke=1, fill=1) |
|
272 |
canvas.setFillColor(white) |
|
273 |
canvas.rect(25*u,u,1.2*u,8*u, fill=1,stroke=0) |
|
274 |
canvas.rect(27.5*u,u,1.2*u,8*u, fill=1, stroke=0) |
|
275 |
canvas.setFont("Times-Roman", 3*u) |
|
276 |
canvas.drawCentredString(18*u, 4*u, text) |
|
277 |
# now draw the tip |
|
278 |
penciltip(canvas,debug=0) |
|
279 |
# draw broken lines across the body. |
|
280 |
canvas.setDash([10,5,16,10],0) |
|
281 |
canvas.line(11*u,2.5*u,22*u,2.5*u) |
|
282 |
canvas.line(22*u,7.5*u,12*u,7.5*u) |
|
283 |
""" |
|
284 |
||
285 |
testpenciltip = """ |
|
286 |
def penciltip(canvas, debug=1): |
|
287 |
from reportlab.lib.colors import tan, black, green |
|
288 |
from reportlab.lib.units import inch |
|
289 |
u = inch/10.0 |
|
290 |
canvas.setLineWidth(4) |
|
291 |
if debug: |
|
292 |
canvas.scale(2.8,2.8) # make it big |
|
293 |
canvas.setLineWidth(1) # small lines |
|
294 |
canvas.setStrokeColor(black) |
|
295 |
canvas.setFillColor(tan) |
|
296 |
p = canvas.beginPath() |
|
297 |
p.moveTo(10*u,0) |
|
298 |
p.lineTo(0,5*u) |
|
299 |
p.lineTo(10*u,10*u) |
|
300 |
p.curveTo(11.5*u,10*u, 11.5*u,7.5*u, 10*u,7.5*u) |
|
301 |
p.curveTo(12*u,7.5*u, 11*u,2.5*u, 9.7*u,2.5*u) |
|
302 |
p.curveTo(10.5*u,2.5*u, 11*u,0, 10*u,0) |
|
303 |
canvas.drawPath(p, stroke=1, fill=1) |
|
304 |
canvas.setFillColor(black) |
|
305 |
p = canvas.beginPath() |
|
306 |
p.moveTo(0,5*u) |
|
307 |
p.lineTo(4*u,3*u) |
|
308 |
p.lineTo(5*u,4.5*u) |
|
309 |
p.lineTo(3*u,6.5*u) |
|
310 |
canvas.drawPath(p, stroke=1, fill=1) |
|
311 |
if debug: |
|
312 |
canvas.setStrokeColor(green) # put in a frame of reference |
|
313 |
canvas.grid([0,5*u,10*u,15*u], [0,5*u,10*u]) |
|
314 |
""" |
|
315 |
||
316 |
testnoteannotation = """ |
|
317 |
from reportlab.platypus.flowables import Flowable |
|
318 |
class NoteAnnotation(Flowable): |
|
319 |
'''put a pencil in the margin.''' |
|
320 |
def wrap(self, *args): |
|
321 |
return (1,10) # I take up very little space! (?) |
|
322 |
def draw(self): |
|
323 |
canvas = self.canv |
|
324 |
canvas.translate(-10,-10) |
|
325 |
canvas.rotate(180) |
|
326 |
canvas.scale(0.2,0.2) |
|
327 |
pencil(canvas, text="NOTE") |
|
328 |
""" |
|
329 |
||
330 |
testhandannotation = """ |
|
331 |
from reportlab.platypus.flowables import Flowable |
|
332 |
from reportlab.lib.colors import tan, green |
|
333 |
class HandAnnotation(Flowable): |
|
334 |
'''A hand flowable.''' |
|
335 |
def __init__(self, xoffset=0, size=None, fillcolor=tan, strokecolor=green): |
|
336 |
from reportlab.lib.units import inch |
|
337 |
if size is None: size=4*inch |
|
338 |
self.fillcolor, self.strokecolor = fillcolor, strokecolor |
|
339 |
self.xoffset = xoffset |
|
340 |
self.size = size |
|
341 |
# normal size is 4 inches |
|
342 |
self.scale = size/(4.0*inch) |
|
343 |
def wrap(self, *args): |
|
344 |
return (self.xoffset, self.size) |
|
345 |
def draw(self): |
|
346 |
canvas = self.canv |
|
347 |
canvas.setLineWidth(6) |
|
348 |
canvas.setFillColor(self.fillcolor) |
|
349 |
canvas.setStrokeColor(self.strokecolor) |
|
350 |
canvas.translate(self.xoffset+self.size,0) |
|
351 |
canvas.rotate(90) |
|
352 |
canvas.scale(self.scale, self.scale) |
|
353 |
hand(canvas, debug=0, fill=1) |
|
354 |
""" |
|
355 |
||
356 |
lyrics = '''\ |
|
357 |
well she hit Net Solutions |
|
358 |
and she registered her own .com site now |
|
359 |
and filled it up with yahoo profile pics |
|
360 |
she snarfed in one night now |
|
361 |
and she made 50 million when Hugh Hefner |
|
362 |
bought up the rights now |
|
363 |
and she'll have fun fun fun |
|
364 |
til her Daddy takes the keyboard away''' |
|
365 |
||
3794 | 366 |
lyrics = lyrics.split("\n") |
2963 | 367 |
testtextsize = """ |
368 |
def textsize(canvas): |
|
369 |
from reportlab.lib.units import inch |
|
370 |
from reportlab.lib.colors import magenta, red |
|
371 |
canvas.setFont("Times-Roman", 20) |
|
372 |
canvas.setFillColor(red) |
|
373 |
canvas.drawCentredString(2.75*inch, 2.5*inch, "Font size examples") |
|
374 |
canvas.setFillColor(magenta) |
|
375 |
size = 7 |
|
376 |
y = 2.3*inch |
|
377 |
x = 1.3*inch |
|
378 |
for line in lyrics: |
|
379 |
canvas.setFont("Helvetica", size) |
|
380 |
canvas.drawRightString(x,y,"%s points: " % size) |
|
381 |
canvas.drawString(x,y, line) |
|
382 |
y = y-size*1.2 |
|
383 |
size = size+1.5 |
|
384 |
""" |
|
385 |
||
386 |
teststar = """ |
|
387 |
def star(canvas, title="Title Here", aka="Comment here.", |
|
388 |
xcenter=None, ycenter=None, nvertices=5): |
|
389 |
from math import pi |
|
390 |
from reportlab.lib.units import inch |
|
391 |
radius=inch/3.0 |
|
392 |
if xcenter is None: xcenter=2.75*inch |
|
393 |
if ycenter is None: ycenter=1.5*inch |
|
394 |
canvas.drawCentredString(xcenter, ycenter+1.3*radius, title) |
|
395 |
canvas.drawCentredString(xcenter, ycenter-1.4*radius, aka) |
|
396 |
p = canvas.beginPath() |
|
397 |
p.moveTo(xcenter,ycenter+radius) |
|
398 |
from math import pi, cos, sin |
|
399 |
angle = (2*pi)*2/5.0 |
|
400 |
startangle = pi/2.0 |
|
401 |
for vertex in range(nvertices-1): |
|
402 |
nextangle = angle*(vertex+1)+startangle |
|
403 |
x = xcenter + radius*cos(nextangle) |
|
404 |
y = ycenter + radius*sin(nextangle) |
|
405 |
p.lineTo(x,y) |
|
406 |
if nvertices==5: |
|
407 |
p.close() |
|
408 |
canvas.drawPath(p) |
|
409 |
""" |
|
410 |
||
411 |
testjoins = """ |
|
412 |
def joins(canvas): |
|
413 |
from reportlab.lib.units import inch |
|
414 |
# make lines big |
|
415 |
canvas.setLineWidth(5) |
|
416 |
star(canvas, "Default: mitered join", "0: pointed", xcenter = 1*inch) |
|
417 |
canvas.setLineJoin(1) |
|
418 |
star(canvas, "Round join", "1: rounded") |
|
419 |
canvas.setLineJoin(2) |
|
420 |
star(canvas, "Bevelled join", "2: square", xcenter=4.5*inch) |
|
421 |
""" |
|
422 |
||
423 |
testcaps = """ |
|
424 |
def caps(canvas): |
|
425 |
from reportlab.lib.units import inch |
|
426 |
# make lines big |
|
427 |
canvas.setLineWidth(5) |
|
428 |
star(canvas, "Default", "no projection",xcenter = 1*inch, |
|
429 |
nvertices=4) |
|
430 |
canvas.setLineCap(1) |
|
431 |
star(canvas, "Round cap", "1: ends in half circle", nvertices=4) |
|
432 |
canvas.setLineCap(2) |
|
433 |
star(canvas, "Square cap", "2: projects out half a width", xcenter=4.5*inch, |
|
434 |
nvertices=4) |
|
435 |
""" |
|
436 |
||
437 |
testdashes = """ |
|
438 |
def dashes(canvas): |
|
439 |
from reportlab.lib.units import inch |
|
440 |
# make lines big |
|
441 |
canvas.setDash(6,3) |
|
442 |
star(canvas, "Simple dashes", "6 points on, 3 off", xcenter = 1*inch) |
|
443 |
canvas.setDash(1,2) |
|
444 |
star(canvas, "Dots", "One on, two off") |
|
445 |
canvas.setDash([1,1,3,3,1,4,4,1], 0) |
|
446 |
star(canvas, "Complex Pattern", "[1,1,3,3,1,4,4,1]", xcenter=4.5*inch) |
|
447 |
""" |
|
448 |
||
449 |
testcursormoves1 = """ |
|
450 |
def cursormoves1(canvas): |
|
451 |
from reportlab.lib.units import inch |
|
452 |
textobject = canvas.beginText() |
|
453 |
textobject.setTextOrigin(inch, 2.5*inch) |
|
454 |
textobject.setFont("Helvetica-Oblique", 14) |
|
455 |
for line in lyrics: |
|
456 |
textobject.textLine(line) |
|
457 |
textobject.setFillGray(0.4) |
|
458 |
textobject.textLines(''' |
|
459 |
With many apologies to the Beach Boys |
|
460 |
and anyone else who finds this objectionable |
|
461 |
''') |
|
462 |
canvas.drawText(textobject) |
|
463 |
""" |
|
464 |
||
465 |
testcursormoves2 = """ |
|
466 |
def cursormoves2(canvas): |
|
467 |
from reportlab.lib.units import inch |
|
468 |
textobject = canvas.beginText() |
|
469 |
textobject.setTextOrigin(2, 2.5*inch) |
|
470 |
textobject.setFont("Helvetica-Oblique", 14) |
|
471 |
for line in lyrics: |
|
472 |
textobject.textOut(line) |
|
473 |
textobject.moveCursor(14,14) # POSITIVE Y moves down!!! |
|
474 |
textobject.setFillColorRGB(0.4,0,1) |
|
475 |
textobject.textLines(''' |
|
476 |
With many apologies to the Beach Boys |
|
477 |
and anyone else who finds this objectionable |
|
478 |
''') |
|
479 |
canvas.drawText(textobject) |
|
480 |
""" |
|
481 |
||
482 |
testcharspace = """ |
|
483 |
def charspace(canvas): |
|
484 |
from reportlab.lib.units import inch |
|
485 |
textobject = canvas.beginText() |
|
486 |
textobject.setTextOrigin(3, 2.5*inch) |
|
487 |
textobject.setFont("Helvetica-Oblique", 10) |
|
488 |
charspace = 0 |
|
489 |
for line in lyrics: |
|
490 |
textobject.setCharSpace(charspace) |
|
491 |
textobject.textLine("%s: %s" %(charspace,line)) |
|
492 |
charspace = charspace+0.5 |
|
493 |
textobject.setFillGray(0.4) |
|
494 |
textobject.textLines(''' |
|
495 |
With many apologies to the Beach Boys |
|
496 |
and anyone else who finds this objectionable |
|
497 |
''') |
|
498 |
canvas.drawText(textobject) |
|
499 |
""" |
|
500 |
||
501 |
testwordspace = """ |
|
502 |
def wordspace(canvas): |
|
503 |
from reportlab.lib.units import inch |
|
504 |
textobject = canvas.beginText() |
|
505 |
textobject.setTextOrigin(3, 2.5*inch) |
|
506 |
textobject.setFont("Helvetica-Oblique", 12) |
|
507 |
wordspace = 0 |
|
508 |
for line in lyrics: |
|
509 |
textobject.setWordSpace(wordspace) |
|
510 |
textobject.textLine("%s: %s" %(wordspace,line)) |
|
511 |
wordspace = wordspace+2.5 |
|
512 |
textobject.setFillColorCMYK(0.4,0,0.4,0.2) |
|
513 |
textobject.textLines(''' |
|
514 |
With many apologies to the Beach Boys |
|
515 |
and anyone else who finds this objectionable |
|
516 |
''') |
|
517 |
canvas.drawText(textobject) |
|
518 |
""" |
|
519 |
testhorizontalscale = """ |
|
520 |
def horizontalscale(canvas): |
|
521 |
from reportlab.lib.units import inch |
|
522 |
textobject = canvas.beginText() |
|
523 |
textobject.setTextOrigin(3, 2.5*inch) |
|
524 |
textobject.setFont("Helvetica-Oblique", 12) |
|
525 |
horizontalscale = 80 # 100 is default |
|
526 |
for line in lyrics: |
|
527 |
textobject.setHorizScale(horizontalscale) |
|
528 |
textobject.textLine("%s: %s" %(horizontalscale,line)) |
|
529 |
horizontalscale = horizontalscale+10 |
|
530 |
textobject.setFillColorCMYK(0.0,0.4,0.4,0.2) |
|
531 |
textobject.textLines(''' |
|
532 |
With many apologies to the Beach Boys |
|
533 |
and anyone else who finds this objectionable |
|
534 |
''') |
|
535 |
canvas.drawText(textobject) |
|
536 |
""" |
|
537 |
testleading = """ |
|
538 |
def leading(canvas): |
|
539 |
from reportlab.lib.units import inch |
|
540 |
textobject = canvas.beginText() |
|
541 |
textobject.setTextOrigin(3, 2.5*inch) |
|
542 |
textobject.setFont("Helvetica-Oblique", 14) |
|
543 |
leading = 8 |
|
544 |
for line in lyrics: |
|
545 |
textobject.setLeading(leading) |
|
546 |
textobject.textLine("%s: %s" %(leading,line)) |
|
547 |
leading = leading+2.5 |
|
548 |
textobject.setFillColorCMYK(0.8,0,0,0.3) |
|
549 |
textobject.textLines(''' |
|
550 |
With many apologies to the Beach Boys |
|
551 |
and anyone else who finds this objectionable |
|
552 |
''') |
|
553 |
canvas.drawText(textobject) |
|
554 |
""" |
|
555 |
||
556 |
testhand = """ |
|
557 |
def hand(canvas, debug=1, fill=0): |
|
558 |
(startx, starty) = (0,0) |
|
559 |
curves = [ |
|
560 |
( 0, 2), ( 0, 4), ( 0, 8), # back of hand |
|
561 |
( 5, 8), ( 7,10), ( 7,14), |
|
562 |
(10,14), (10,13), ( 7.5, 8), # thumb |
|
563 |
(13, 8), (14, 8), (17, 8), |
|
564 |
(19, 8), (19, 6), (17, 6), |
|
565 |
(15, 6), (13, 6), (11, 6), # index, pointing |
|
566 |
(12, 6), (13, 6), (14, 6), |
|
567 |
(16, 6), (16, 4), (14, 4), |
|
568 |
(13, 4), (12, 4), (11, 4), # middle |
|
569 |
(11.5, 4), (12, 4), (13, 4), |
|
570 |
(15, 4), (15, 2), (13, 2), |
|
571 |
(12.5, 2), (11.5, 2), (11, 2), # ring |
|
572 |
(11.5, 2), (12, 2), (12.5, 2), |
|
573 |
(14, 2), (14, 0), (12.5, 0), |
|
574 |
(10, 0), (8, 0), (6, 0), # pinky, then close |
|
575 |
] |
|
576 |
from reportlab.lib.units import inch |
|
577 |
if debug: canvas.setLineWidth(6) |
|
578 |
u = inch*0.2 |
|
579 |
p = canvas.beginPath() |
|
580 |
p.moveTo(startx, starty) |
|
581 |
ccopy = list(curves) |
|
582 |
while ccopy: |
|
583 |
[(x1,y1), (x2,y2), (x3,y3)] = ccopy[:3] |
|
584 |
del ccopy[:3] |
|
585 |
p.curveTo(x1*u,y1*u,x2*u,y2*u,x3*u,y3*u) |
|
586 |
p.close() |
|
587 |
canvas.drawPath(p, fill=fill) |
|
588 |
if debug: |
|
589 |
from reportlab.lib.colors import red, green |
|
590 |
(lastx, lasty) = (startx, starty) |
|
591 |
ccopy = list(curves) |
|
592 |
while ccopy: |
|
593 |
[(x1,y1), (x2,y2), (x3,y3)] = ccopy[:3] |
|
594 |
del ccopy[:3] |
|
595 |
canvas.setStrokeColor(red) |
|
596 |
canvas.line(lastx*u,lasty*u, x1*u,y1*u) |
|
597 |
canvas.setStrokeColor(green) |
|
598 |
canvas.line(x2*u,y2*u, x3*u,y3*u) |
|
599 |
(lastx,lasty) = (x3,y3) |
|
600 |
""" |
|
601 |
||
602 |
testhand2 = """ |
|
603 |
def hand2(canvas): |
|
604 |
canvas.translate(20,10) |
|
605 |
canvas.setLineWidth(3) |
|
606 |
canvas.setFillColorRGB(0.1, 0.3, 0.9) |
|
607 |
canvas.setStrokeGray(0.5) |
|
608 |
hand(canvas, debug=0, fill=1) |
|
609 |
""" |
|
610 |
||
611 |
testfonts = """ |
|
612 |
def fonts(canvas): |
|
613 |
from reportlab.lib.units import inch |
|
614 |
text = "Now is the time for all good men to..." |
|
615 |
x = 1.8*inch |
|
616 |
y = 2.7*inch |
|
617 |
for font in canvas.getAvailableFonts(): |
|
618 |
canvas.setFont(font, 10) |
|
619 |
canvas.drawString(x,y,text) |
|
620 |
canvas.setFont("Helvetica", 10) |
|
621 |
canvas.drawRightString(x-10,y, font+":") |
|
622 |
y = y-13 |
|
623 |
""" |
|
624 |
||
625 |
testarcs = """ |
|
626 |
def arcs(canvas): |
|
627 |
from reportlab.lib.units import inch |
|
628 |
canvas.setLineWidth(4) |
|
629 |
canvas.setStrokeColorRGB(0.8, 1, 0.6) |
|
630 |
# draw rectangles enclosing the arcs |
|
631 |
canvas.rect(inch, inch, 1.5*inch, inch) |
|
632 |
canvas.rect(3*inch, inch, inch, 1.5*inch) |
|
633 |
canvas.setStrokeColorRGB(0, 0.2, 0.4) |
|
634 |
canvas.setFillColorRGB(1, 0.6, 0.8) |
|
635 |
p = canvas.beginPath() |
|
636 |
p.moveTo(0.2*inch, 0.2*inch) |
|
637 |
p.arcTo(inch, inch, 2.5*inch,2*inch, startAng=-30, extent=135) |
|
638 |
p.arc(3*inch, inch, 4*inch, 2.5*inch, startAng=-45, extent=270) |
|
639 |
canvas.drawPath(p, fill=1, stroke=1) |
|
640 |
""" |
|
641 |
testvariousshapes = """ |
|
642 |
def variousshapes(canvas): |
|
643 |
from reportlab.lib.units import inch |
|
644 |
inch = int(inch) |
|
645 |
canvas.setStrokeGray(0.5) |
|
646 |
canvas.grid(range(0,11*inch/2,inch/2), range(0,7*inch/2,inch/2)) |
|
647 |
canvas.setLineWidth(4) |
|
648 |
canvas.setStrokeColorRGB(0, 0.2, 0.7) |
|
649 |
canvas.setFillColorRGB(1, 0.6, 0.8) |
|
650 |
p = canvas.beginPath() |
|
651 |
p.rect(0.5*inch, 0.5*inch, 0.5*inch, 2*inch) |
|
652 |
p.circle(2.75*inch, 1.5*inch, 0.3*inch) |
|
653 |
p.ellipse(3.5*inch, 0.5*inch, 1.2*inch, 2*inch) |
|
654 |
canvas.drawPath(p, fill=1, stroke=1) |
|
655 |
""" |
|
656 |
||
657 |
testclosingfigures = """ |
|
658 |
def closingfigures(canvas): |
|
659 |
from reportlab.lib.units import inch |
|
660 |
h = inch/3.0; k = inch/2.0 |
|
661 |
canvas.setStrokeColorRGB(0.2,0.3,0.5) |
|
662 |
canvas.setFillColorRGB(0.8,0.6,0.2) |
|
663 |
canvas.setLineWidth(4) |
|
664 |
p = canvas.beginPath() |
|
665 |
for i in (1,2,3,4): |
|
666 |
for j in (1,2): |
|
667 |
xc,yc = inch*i, inch*j |
|
668 |
p.moveTo(xc,yc) |
|
669 |
p.arcTo(xc-h, yc-k, xc+h, yc+k, startAng=0, extent=60*i) |
|
670 |
# close only the first one, not the second one |
|
671 |
if j==1: |
|
672 |
p.close() |
|
673 |
canvas.drawPath(p, fill=1, stroke=1) |
|
674 |
""" |
|
675 |
||
676 |
testforms = """ |
|
677 |
def forms(canvas): |
|
678 |
#first create a form... |
|
679 |
canvas.beginForm("SpumoniForm") |
|
680 |
#re-use some drawing functions from earlier |
|
681 |
spumoni(canvas) |
|
682 |
canvas.endForm() |
|
683 |
||
684 |
#then draw it |
|
685 |
canvas.doForm("SpumoniForm") |
|
686 |
""" |
|
687 |
||
688 |
def doctemplateillustration(canvas): |
|
689 |
from reportlab.lib.units import inch |
|
690 |
canvas.setFont("Helvetica", 10) |
|
691 |
canvas.drawString(inch/4.0, 2.75*inch, "DocTemplate") |
|
692 |
W = 4/3.0*inch |
|
693 |
H = 2*inch |
|
694 |
Wd = x = inch/4.0 |
|
695 |
Hd =y = inch/2.0 |
|
696 |
for name in ("two column", "chapter page", "title page"): |
|
697 |
canvas.setFillColorRGB(0.5,1.0,1.0) |
|
698 |
canvas.rect(x,y,W,H, fill=1) |
|
699 |
canvas.setFillColorRGB(0,0,0) |
|
700 |
canvas.drawString(x+inch/8, y+H-Wd, "PageTemplate") |
|
701 |
canvas.drawCentredString(x+W/2.0, y-Wd, name) |
|
702 |
x = x+W+Wd |
|
703 |
canvas.saveState() |
|
704 |
d = inch/16 |
|
705 |
dW = (W-3*d)/2.0 |
|
706 |
hD = H -2*d-Wd |
|
707 |
canvas.translate(Wd+d, Hd+d) |
|
708 |
for name in ("left Frame", "right Frame"): |
|
709 |
canvas.setFillColorRGB(1.0,0.5,1.0) |
|
710 |
canvas.rect(0,0, dW,hD, fill=1) |
|
711 |
canvas.setFillGray(0.7) |
|
712 |
dd= d/2.0 |
|
713 |
ddH = (hD-6*dd)/5.0 |
|
714 |
ddW = dW-2*dd |
|
715 |
yy = dd |
|
716 |
xx = dd |
|
717 |
for i in range(5): |
|
718 |
canvas.rect(xx,yy,ddW,ddH, fill=1, stroke=0) |
|
719 |
yy = yy+ddH+dd |
|
720 |
canvas.setFillColorRGB(0,0,0) |
|
721 |
canvas.saveState() |
|
722 |
canvas.rotate(90) |
|
723 |
canvas.drawString(d,-dW/2, name) |
|
724 |
canvas.restoreState() |
|
725 |
canvas.translate(dW+d,0) |
|
726 |
canvas.restoreState() |
|
727 |
canvas.setFillColorRGB(1.0, 0.5, 1.0) |
|
728 |
mx = Wd+W+Wd+d |
|
729 |
my = Hd+d |
|
730 |
mW = W-2*d |
|
731 |
mH = H-d-Hd |
|
732 |
canvas.rect(mx, my, mW, mH, fill=1) |
|
733 |
canvas.rect(Wd+2*(W+Wd)+d, Hd+3*d, W-2*d, H/2.0, fill=1) |
|
734 |
canvas.setFillGray(0.7) |
|
735 |
canvas.rect(Wd+2*(W+Wd)+d+dd, Hd+5*d, W-2*d-2*dd, H/2.0-2*d-dd, fill=1) |
|
736 |
xx = mx+dd |
|
737 |
yy = my+mH/5.0 |
|
738 |
ddH = (mH-6*dd-mH/5.0)/3.0 |
|
739 |
ddW = mW - 2*dd |
|
740 |
for i in range(3): |
|
741 |
canvas.setFillGray(0.7) |
|
742 |
canvas.rect(xx,yy,ddW,ddH, fill=1, stroke=1) |
|
743 |
canvas.setFillGray(0) |
|
744 |
canvas.drawString(xx+dd/2.0,yy+dd/2.0, "flowable %s" %(157-i)) |
|
745 |
yy = yy+ddH+dd |
|
746 |
canvas.drawCentredString(3*Wd+2*W+W/2, Hd+H/2.0, "First Flowable") |
|
747 |
canvas.setFont("Times-BoldItalic", 8) |
|
748 |
canvas.setFillGray(0) |
|
749 |
canvas.drawCentredString(mx+mW/2.0, my+mH+3*dd, "Chapter 6: Lubricants") |
|
750 |
canvas.setFont("Times-BoldItalic", 10) |
|
751 |
canvas.drawCentredString(3*Wd+2*W+W/2, Hd+H-H/4, "College Life") |
|
752 |
||
753 |
class PlatIllust: |
|
754 |
#wrap the above for PP# |
|
755 |
def __init__(self, x, y, scale=1): |
|
756 |
self.x = x |
|
757 |
self.y = y |
|
758 |
self.scale = scale |
|
759 |
def drawOn(self, canvas): |
|
760 |
canvas.saveState() |
|
761 |
canvas.translate(self.x, self.y) |
|
762 |
canvas.scale(self.scale, self.scale) |
|
763 |
doctemplateillustration(canvas) |
|
764 |
canvas.restoreState() |
|
765 |
||
766 |
class PingoIllust: |
|
767 |
#wrap the above for PP# |
|
768 |
def __init__(self, x, y, scale=1): |
|
769 |
## print 'Pingo illustration %f, %f, %f' % (x,y,scale) |
|
770 |
self.x = x |
|
771 |
self.y = y |
|
772 |
self.scale = scale |
|
773 |
def drawOn(self, canvas): |
|
774 |
canvas.rect(self.x, self.y, 100,100, stroke=1, fill=1) |
|
775 |
## from pingo import testdrawings |
|
776 |
## from pingo import pingopdf |
|
777 |
## drawing = testdrawings.getDrawing3() |
|
778 |
## canvas.saveState() |
|
779 |
## canvas.scale(self.scale, self.scale) |
|
780 |
## pingopdf.draw(drawing, canvas, self.x, self.y) |
|
781 |
## canvas.restoreState() |
|
782 |
||
783 |
# D = dir() |
|
784 |
g = globals() |
|
785 |
Dprime = {} |
|
786 |
from types import StringType |
|
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
787 |
for a,b in g.items()): |
2963 | 788 |
if a[:4]=="test" and type(b) is StringType: |
789 |
#print 'for', a |
|
790 |
#print b |
|
3794 | 791 |
b = b.strip() |
2963 | 792 |
exec(b+'\n') |
793 |
||
794 |
platypussetup = """ |
|
795 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer |
|
796 |
from reportlab.lib.styles import getSampleStyleSheet |
|
797 |
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE |
|
798 |
from reportlab.lib.units import inch |
|
799 |
PAGE_HEIGHT=DEFAULT_PAGE_SIZE[1]; PAGE_WIDTH=DEFAULT_PAGE_SIZE[0] |
|
800 |
styles = getSampleStyleSheet() |
|
801 |
""" |
|
802 |
platypusfirstpage = """ |
|
803 |
Title = "Hello world" |
|
804 |
pageinfo = "platypus example" |
|
805 |
def myFirstPage(canvas, doc): |
|
806 |
canvas.saveState() |
|
807 |
canvas.setFont('Times-Bold',16) |
|
808 |
canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-108, Title) |
|
809 |
canvas.setFont('Times-Roman',9) |
|
810 |
canvas.drawString(inch, 0.75 * inch, "First Page / %s" % pageinfo) |
|
811 |
canvas.restoreState() |
|
812 |
""" |
|
813 |
platypusnextpage = """ |
|
814 |
def myLaterPages(canvas, doc): |
|
815 |
canvas.saveState() |
|
816 |
canvas.setFont('Times-Roman',9) |
|
817 |
canvas.drawString(inch, 0.75 * inch, "Page %d %s" % (doc.page, pageinfo)) |
|
818 |
canvas.restoreState() |
|
819 |
""" |
|
820 |
platypusgo = """ |
|
821 |
def go(): |
|
822 |
doc = SimpleDocTemplate("phello.pdf") |
|
823 |
Story = [Spacer(1,2*inch)] |
|
824 |
style = styles["Normal"] |
|
825 |
for i in range(100): |
|
826 |
bogustext = ("This is Paragraph number %s. " % i) *20 |
|
827 |
p = Paragraph(bogustext, style) |
|
828 |
Story.append(p) |
|
829 |
Story.append(Spacer(1,0.2*inch)) |
|
830 |
doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages) |
|
831 |
""" |
|
832 |
||
833 |
if __name__=="__main__": |
|
834 |
# then do the platypus hello world |
|
835 |
for b in platypussetup, platypusfirstpage, platypusnextpage, platypusgo: |
|
3794 | 836 |
b = b.strip() |
2963 | 837 |
exec(b+'\n') |
838 |
go() |