~openerp/openobject-server/web-dashboard

« back to all changes in this revision

Viewing changes to bin/reportlab/tools/docco/examples.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

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