author | Andy Robinson <andy@reportlab.com> |
Sun, 24 Mar 2013 07:42:19 +0000 | |
changeset 3667 | ab79820991f9 |
parent 3617 | ae5744e97c42 |
child 3680 | cbfd10f961b0 |
permissions | -rw-r--r-- |
2963 | 1 |
#!/bin/env python |
3617 | 2 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
2963 | 3 |
#see license.txt for license details |
2967
ea62529bd1df
reportlab-2.2: first stage changes in on the trunk
rgbecker
parents:
2966
diff
changeset
|
4 |
__doc__='testscript for reportlab.pdfgen' |
2963 | 5 |
__version__=''' $Id$ ''' |
6 |
#tests and documents new low-level canvas |
|
2987 | 7 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
2984 | 8 |
setOutDir(__name__) |
2963 | 9 |
import os, string |
2966 | 10 |
import unittest |
2963 | 11 |
from reportlab.pdfgen import canvas # gmcm 2000/10/13, pdfgen now a package |
12 |
from reportlab.lib.units import inch, cm |
|
13 |
from reportlab.lib import colors |
|
14 |
from reportlab.lib.utils import haveImages, fileName2Utf8 |
|
15 |
||
16 |
################################################################# |
|
17 |
# |
|
18 |
# first some drawing utilities |
|
19 |
# |
|
20 |
# |
|
21 |
################################################################ |
|
22 |
||
23 |
BASEFONT = ('Times-Roman', 10) |
|
24 |
def framePageForm(c): |
|
25 |
c.beginForm("frame") |
|
26 |
c.saveState() |
|
27 |
# forms can't do non-constant operations |
|
28 |
#canvas.setFont('Times-BoldItalic',20) |
|
29 |
#canvas.drawString(inch, 10.5 * inch, title) |
|
30 |
||
31 |
#c.setFont('Times-Roman',10) |
|
32 |
#c.drawCentredString(4.135 * inch, 0.75 * inch, |
|
33 |
# 'Page %d' % c.getPageNumber()) |
|
34 |
||
35 |
#draw a border |
|
36 |
c.setFillColor(colors.ReportLabBlue) |
|
37 |
c.rect(0.3*inch, inch, 0.5*inch, 10*inch, fill=1) |
|
38 |
from reportlab.lib import corp |
|
39 |
c.translate(0.8*inch, 9.6*inch) |
|
40 |
c.rotate(90) |
|
41 |
logo = corp.ReportLabLogo(width=1.3*inch, height=0.5*inch, powered_by=1) |
|
42 |
c.setFillColorRGB(1,1,1) |
|
43 |
c.setStrokeColorRGB(1,1,1) |
|
44 |
logo.draw(c) |
|
45 |
#c.setStrokeColorRGB(1,0,0) |
|
46 |
#c.setLineWidth(5) |
|
47 |
#c.line(0.8 * inch, inch, 0.8 * inch, 10.75 * inch) |
|
48 |
#reset carefully afterwards |
|
49 |
#canvas.setLineWidth(1) |
|
50 |
#canvas.setStrokeColorRGB(0,0,0)\ |
|
51 |
c.restoreState() |
|
52 |
c.endForm() |
|
53 |
||
54 |
def framePage(canvas, title): |
|
55 |
global closeit |
|
56 |
titlelist.append(title) |
|
57 |
#canvas._inPage0() # do we need this at all? would be good to eliminate it |
|
58 |
canvas.saveState() |
|
59 |
canvas.setFont('Times-BoldItalic',20) |
|
60 |
||
61 |
canvas.drawString(inch, 10.5 * inch, title) |
|
62 |
canvas.bookmarkHorizontalAbsolute(title, 10.8*inch) |
|
63 |
#newsection(title) |
|
64 |
canvas.addOutlineEntry(title+" section", title, level=0, closed=closeit) |
|
65 |
closeit = not closeit # close every other one |
|
66 |
canvas.setFont('Times-Roman',10) |
|
67 |
canvas.drawCentredString(4.135 * inch, 0.75 * inch, |
|
68 |
'Page %d' % canvas.getPageNumber()) |
|
69 |
canvas.restoreState() |
|
70 |
canvas.doForm("frame") |
|
71 |
||
72 |
||
73 |
def makesubsection(canvas, title, horizontal): |
|
74 |
canvas.bookmarkHorizontalAbsolute(title, horizontal) |
|
75 |
#newsubsection(title) |
|
76 |
canvas.addOutlineEntry(title+" subsection", title, level=1) |
|
77 |
||
78 |
||
79 |
# outline helpers |
|
80 |
#outlinenametree = [] |
|
81 |
#def newsection(name): |
|
82 |
# outlinenametree.append(name) |
|
83 |
||
84 |
||
85 |
#def newsubsection(name): |
|
86 |
# from types import TupleType |
|
87 |
# thissection = outlinenametree[-1] |
|
88 |
# if type(thissection) is not TupleType: |
|
89 |
# subsectionlist = [] |
|
90 |
# thissection = outlinenametree[-1] = (thissection, subsectionlist) |
|
91 |
# else: |
|
92 |
# (sectionname, subsectionlist) = thissection |
|
93 |
# subsectionlist.append(name) |
|
94 |
||
95 |
||
96 |
class DocBlock: |
|
97 |
"""A DocBlock has a chunk of commentary and a chunk of code. |
|
98 |
It prints the code and commentary, then executes the code, |
|
99 |
which is presumed to draw in a region reserved for it. |
|
100 |
""" |
|
101 |
def __init__(self): |
|
102 |
self.comment1 = "A doc block" |
|
103 |
self.code = "canvas.setTextOrigin(cm, cm)\ncanvas.textOut('Hello World')" |
|
104 |
self.comment2 = "That was a doc block" |
|
105 |
self.drawHeight = 0 |
|
106 |
||
107 |
def _getHeight(self): |
|
108 |
"splits into lines" |
|
109 |
self.comment1lines = string.split(self.comment1, '\n') |
|
110 |
self.codelines = string.split(self.code, '\n') |
|
111 |
self.comment2lines = string.split(self.comment2, '\n') |
|
112 |
textheight = (len(self.comment1lines) + |
|
113 |
len(self.code) + |
|
114 |
len(self.comment2lines) + |
|
115 |
18) |
|
116 |
return max(textheight, self.drawHeight) |
|
117 |
||
118 |
def draw(self, canvas, x, y): |
|
119 |
#specifies top left corner |
|
120 |
canvas.saveState() |
|
121 |
height = self._getHeight() |
|
122 |
canvas.rect(x, y-height, 6*inch, height) |
|
123 |
#first draw the text |
|
124 |
canvas.setTextOrigin(x + 3 * inch, y - 12) |
|
125 |
canvas.setFont('Times-Roman',10) |
|
126 |
canvas.textLines(self.comment1) |
|
127 |
drawCode(canvas, self.code) |
|
128 |
canvas.textLines(self.comment2) |
|
129 |
||
130 |
#now a box for the drawing, slightly within rect |
|
131 |
canvas.rect(x + 9, y - height + 9, 198, height - 18) |
|
132 |
#boundary: |
|
133 |
self.namespace = {'canvas':canvas,'cm': cm,'inch':inch} |
|
134 |
canvas.translate(x+9, y - height + 9) |
|
135 |
codeObj = compile(self.code, '<sample>','exec') |
|
136 |
exec codeObj in self.namespace |
|
137 |
||
138 |
canvas.restoreState() |
|
139 |
||
140 |
||
141 |
def drawAxes(canvas, label): |
|
142 |
"""draws a couple of little rulers showing the coords - |
|
143 |
uses points as units so you get an imperial ruler |
|
144 |
one inch on each side""" |
|
145 |
#y axis |
|
146 |
canvas.line(0,0,0,72) |
|
147 |
for y in range(9): |
|
148 |
tenths = (y+1) * 7.2 |
|
149 |
canvas.line(-6,tenths,0,tenths) |
|
150 |
canvas.line(-6, 66, 0, 72) #arrow... |
|
151 |
canvas.line(6, 66, 0, 72) #arrow... |
|
152 |
||
153 |
canvas.line(0,0,72,0) |
|
154 |
for x in range(9): |
|
155 |
tenths = (x+1) * 7.2 |
|
156 |
canvas.line(tenths,-6,tenths, 0) |
|
157 |
canvas.line(66, -6, 72, 0) #arrow... |
|
158 |
canvas.line(66, +6, 72, 0) #arrow... |
|
159 |
||
160 |
canvas.drawString(18, 30, label) |
|
161 |
||
162 |
||
163 |
def drawCrossHairs(canvas, x, y): |
|
164 |
"""just a marker for checking text metrics - blue for fun""" |
|
165 |
||
166 |
canvas.saveState() |
|
167 |
canvas.setStrokeColorRGB(0,1,0) |
|
168 |
canvas.line(x-6,y,x+6,y) |
|
169 |
canvas.line(x,y-6,x,y+6) |
|
170 |
canvas.restoreState() |
|
171 |
||
172 |
||
173 |
def drawCode(canvas, code): |
|
174 |
"""Draws a block of text at current point, indented and in Courier""" |
|
175 |
canvas.addLiteral('36 0 Td') |
|
176 |
canvas.setFillColor(colors.blue) |
|
177 |
canvas.setFont('Courier',10) |
|
178 |
||
179 |
t = canvas.beginText() |
|
180 |
t.textLines(code) |
|
181 |
c.drawText(t) |
|
182 |
||
183 |
canvas.setFillColor(colors.black) |
|
184 |
canvas.addLiteral('-36 0 Td') |
|
185 |
canvas.setFont('Times-Roman',10) |
|
186 |
||
187 |
||
188 |
def makeDocument(filename, pageCallBack=None): |
|
189 |
#the extra arg is a hack added later, so other |
|
190 |
#tests can get hold of the canvas just before it is |
|
191 |
#saved |
|
192 |
global titlelist, closeit |
|
193 |
titlelist = [] |
|
194 |
closeit = 0 |
|
195 |
||
196 |
c = canvas.Canvas(filename) |
|
197 |
c.setPageCompression(0) |
|
198 |
c.setPageCallBack(pageCallBack) |
|
199 |
framePageForm(c) # define the frame form |
|
200 |
c.showOutline() |
|
201 |
||
202 |
framePage(c, 'PDFgen graphics API test script') |
|
203 |
makesubsection(c, "PDFgen", 10*inch) |
|
204 |
||
205 |
#quickie encoding test: when canvas encoding not set, |
|
206 |
#the following should do (tm), (r) and (c) |
|
207 |
msg_uni = u'copyright\u00A9 trademark\u2122 registered\u00AE ReportLab in unicode!' |
|
208 |
msg_utf8 = msg_uni.replace('unicode','utf8').encode('utf8') |
|
209 |
c.drawString(100, 100, msg_uni) |
|
210 |
c.drawString(100, 80, msg_utf8) |
|
211 |
||
212 |
||
213 |
||
214 |
||
215 |
t = c.beginText(inch, 10*inch) |
|
216 |
t.setFont('Times-Roman', 10) |
|
217 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
218 |
t.textLines(""" |
|
219 |
The ReportLab library permits you to create PDF documents directly from |
|
220 |
your Python code. The "pdfgen" subpackage is the lowest level exposed |
|
221 |
to the user and lets you directly position test and graphics on the |
|
222 |
page, with access to almost the full range of PDF features. |
|
223 |
The API is intended to closely mirror the PDF / Postscript imaging |
|
224 |
model. There is an almost one to one correspondence between commands |
|
225 |
and PDF operators. However, where PDF provides several ways to do a job, |
|
226 |
we have generally only picked one. |
|
227 |
The test script attempts to use all of the methods exposed by the Canvas |
|
228 |
class, defined in reportlab/pdfgen/canvas.py |
|
229 |
First, let's look at text output. There are some basic commands |
|
230 |
to draw strings: |
|
231 |
- canvas.setFont(fontname, fontsize [, leading]) |
|
232 |
- canvas.drawString(x, y, text) |
|
233 |
- canvas.drawRightString(x, y, text) |
|
234 |
- canvas.drawCentredString(x, y, text) |
|
235 |
||
236 |
The coordinates are in points starting at the bottom left corner of the |
|
237 |
page. When setting a font, the leading (i.e. inter-line spacing) |
|
238 |
defaults to 1.2 * fontsize if the fontsize is not provided. |
|
239 |
||
240 |
For more sophisticated operations, you can create a Text Object, defined |
|
241 |
in reportlab/pdfgen/testobject.py. Text objects produce tighter PDF, run |
|
242 |
faster and have many methods for precise control of spacing and position. |
|
243 |
Basic usage goes as follows: |
|
244 |
- tx = canvas.beginText(x, y) |
|
245 |
- tx.textOut('Hello') # this moves the cursor to the right |
|
246 |
- tx.textLine('Hello again') # prints a line and moves down |
|
247 |
- y = tx.getY() # getX, getY and getCursor track position |
|
248 |
- canvas.drawText(tx) # all gets drawn at the end |
|
249 |
||
250 |
The green crosshairs below test whether the text cursor is working |
|
251 |
properly. They should appear at the bottom left of each relevant |
|
252 |
substring. |
|
253 |
""") |
|
254 |
||
255 |
t.setFillColorRGB(1,0,0) |
|
256 |
t.setTextOrigin(inch, 4*inch) |
|
257 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
258 |
t.textOut('textOut moves across:') |
|
259 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
260 |
t.textOut('textOut moves across:') |
|
261 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
262 |
t.textOut('textOut moves across:') |
|
263 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
264 |
t.textLine('') |
|
265 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
266 |
t.textLine('textLine moves down') |
|
267 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
268 |
t.textLine('textLine moves down') |
|
269 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
270 |
t.textLine('textLine moves down') |
|
271 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
272 |
||
273 |
t.setTextOrigin(4*inch,3.25*inch) |
|
274 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
275 |
t.textLines('This is a multi-line\nstring with embedded newlines\ndrawn with textLines().\n') |
|
276 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
277 |
t.textLines(['This is a list of strings', |
|
278 |
'drawn with textLines().']) |
|
279 |
c.drawText(t) |
|
280 |
||
281 |
t = c.beginText(2*inch,2*inch) |
|
282 |
t.setFont('Times-Roman',10) |
|
283 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
284 |
t.textOut('Small text.') |
|
285 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
286 |
t.setFont('Courier',14) |
|
287 |
t.textOut('Bigger fixed width text.') |
|
288 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
289 |
t.setFont('Times-Roman',10) |
|
290 |
t.textOut('Small text again.') |
|
291 |
drawCrossHairs(c, t.getX(),t.getY()) |
|
292 |
c.drawText(t) |
|
293 |
||
294 |
#try out the decimal tabs high on the right. |
|
295 |
c.setStrokeColor(colors.silver) |
|
296 |
c.line(7*inch, 6*inch, 7*inch, 4.5*inch) |
|
297 |
||
298 |
c.setFillColor(colors.black) |
|
299 |
c.setFont('Times-Roman',10) |
|
300 |
c.drawString(6*inch, 6.2*inch, "Testing decimal alignment") |
|
301 |
c.drawString(6*inch, 6.05*inch, "- aim for silver line") |
|
302 |
c.line(7*inch, 6*inch, 7*inch, 4.5*inch) |
|
303 |
||
304 |
c.drawAlignedString(7*inch, 5.8*inch, "1,234,567.89") |
|
305 |
c.drawAlignedString(7*inch, 5.6*inch, "3,456.789") |
|
306 |
c.drawAlignedString(7*inch, 5.4*inch, "123") |
|
307 |
c.setFillColor(colors.red) |
|
308 |
c.drawAlignedString(7*inch, 5.2*inch, "(7,192,302.30)") |
|
309 |
||
310 |
#mark the cursor where it stopped |
|
311 |
c.showPage() |
|
312 |
||
313 |
||
314 |
############################################################## |
|
315 |
# |
|
316 |
# page 2 - line styles |
|
317 |
# |
|
318 |
############################################################### |
|
319 |
||
320 |
#page 2 - lines and styles |
|
321 |
framePage(c, 'Line Drawing Styles') |
|
322 |
||
323 |
||
324 |
||
325 |
# three line ends, lines drawn the hard way |
|
326 |
#firt make some vertical end markers |
|
327 |
c.setDash(4,4) |
|
328 |
c.setLineWidth(0) |
|
329 |
c.line(inch,9.2*inch,inch, 7.8*inch) |
|
330 |
c.line(3*inch,9.2*inch,3*inch, 7.8*inch) |
|
331 |
c.setDash() #clears it |
|
332 |
||
333 |
c.setLineWidth(5) |
|
334 |
c.setLineCap(0) |
|
335 |
p = c.beginPath() |
|
336 |
p.moveTo(inch, 9*inch) |
|
337 |
p.lineTo(3*inch, 9*inch) |
|
338 |
c.drawPath(p) |
|
339 |
c.drawString(4*inch, 9*inch, 'the default - butt caps project half a width') |
|
340 |
makesubsection(c, "caps and joins", 8.5*inch) |
|
341 |
||
342 |
c.setLineCap(1) |
|
343 |
p = c.beginPath() |
|
344 |
p.moveTo(inch, 8.5*inch) |
|
345 |
p.lineTo(3*inch, 8.5*inch) |
|
346 |
c.drawPath(p) |
|
347 |
c.drawString(4*inch, 8.5*inch, 'round caps') |
|
348 |
||
349 |
c.setLineCap(2) |
|
350 |
p = c.beginPath() |
|
351 |
p.moveTo(inch, 8*inch) |
|
352 |
p.lineTo(3*inch, 8*inch) |
|
353 |
c.drawPath(p) |
|
354 |
c.drawString(4*inch, 8*inch, 'square caps') |
|
355 |
||
356 |
c.setLineCap(0) |
|
357 |
||
358 |
# three line joins |
|
359 |
c.setLineJoin(0) |
|
360 |
p = c.beginPath() |
|
361 |
p.moveTo(inch, 7*inch) |
|
362 |
p.lineTo(2*inch, 7*inch) |
|
363 |
p.lineTo(inch, 6.7*inch) |
|
364 |
c.drawPath(p) |
|
365 |
c.drawString(4*inch, 6.8*inch, 'Default - mitered join') |
|
366 |
||
367 |
c.setLineJoin(1) |
|
368 |
p = c.beginPath() |
|
369 |
p.moveTo(inch, 6.5*inch) |
|
370 |
p.lineTo(2*inch, 6.5*inch) |
|
371 |
p.lineTo(inch, 6.2*inch) |
|
372 |
c.drawPath(p) |
|
373 |
c.drawString(4*inch, 6.3*inch, 'round join') |
|
374 |
||
375 |
c.setLineJoin(2) |
|
376 |
p = c.beginPath() |
|
377 |
p.moveTo(inch, 6*inch) |
|
378 |
p.lineTo(2*inch, 6*inch) |
|
379 |
p.lineTo(inch, 5.7*inch) |
|
380 |
c.drawPath(p) |
|
381 |
c.drawString(4*inch, 5.8*inch, 'bevel join') |
|
382 |
||
383 |
c.setDash(6,6) |
|
384 |
p = c.beginPath() |
|
385 |
p.moveTo(inch, 5*inch) |
|
386 |
p.lineTo(3*inch, 5*inch) |
|
387 |
c.drawPath(p) |
|
388 |
c.drawString(4*inch, 5*inch, 'dash 6 points on, 6 off- setDash(6,6) setLineCap(0)') |
|
389 |
makesubsection(c, "dash patterns", 5*inch) |
|
390 |
||
391 |
c.setLineCap(1) |
|
392 |
p = c.beginPath() |
|
393 |
p.moveTo(inch, 4.5*inch) |
|
394 |
p.lineTo(3*inch, 4.5*inch) |
|
395 |
c.drawPath(p) |
|
396 |
c.drawString(4*inch, 4.5*inch, 'dash 6 points on, 6 off- setDash(6,6) setLineCap(1)') |
|
397 |
||
398 |
c.setLineCap(0) |
|
399 |
c.setDash([1,2,3,4,5,6],0) |
|
400 |
p = c.beginPath() |
|
401 |
p.moveTo(inch, 4.0*inch) |
|
402 |
p.lineTo(3*inch, 4.0*inch) |
|
403 |
c.drawPath(p) |
|
404 |
c.drawString(4*inch, 4*inch, 'dash growing - setDash([1,2,3,4,5,6],0) setLineCap(0)') |
|
405 |
||
406 |
c.setLineCap(1) |
|
407 |
c.setLineJoin(1) |
|
408 |
c.setDash(32,12) |
|
409 |
p = c.beginPath() |
|
410 |
p.moveTo(inch, 3.0*inch) |
|
411 |
p.lineTo(2.5*inch, 3.0*inch) |
|
412 |
p.lineTo(inch, 2*inch) |
|
413 |
c.drawPath(p) |
|
414 |
c.drawString(4*inch, 3*inch, 'dash pattern, join and cap style interacting - ') |
|
415 |
c.drawString(4*inch, 3*inch - 12, 'round join & miter results in sausages') |
|
416 |
c.textAnnotation('Annotation',Rect=(4*inch, 3*inch-72, inch,inch-12)) |
|
417 |
||
418 |
c.showPage() |
|
419 |
||
420 |
||
421 |
############################################################## |
|
422 |
# |
|
423 |
# higher level shapes |
|
424 |
# |
|
425 |
############################################################### |
|
426 |
framePage(c, 'Shape Drawing Routines') |
|
427 |
||
428 |
t = c.beginText(inch, 10*inch) |
|
429 |
t.textLines(""" |
|
430 |
Rather than making your own paths, you have access to a range of shape routines. |
|
431 |
These are built in pdfgen out of lines and bezier curves, but use the most compact |
|
432 |
set of operators possible. We can add any new ones that are of general use at no |
|
433 |
cost to performance.""") |
|
434 |
t.textLine() |
|
435 |
||
436 |
#line demo |
|
3574 | 437 |
makesubsection(c, "lines", 9*inch) |
438 |
c.line(inch, 9*inch, 3*inch, 9*inch) |
|
439 |
t.setTextOrigin(4*inch, 9*inch) |
|
2963 | 440 |
t.textLine('canvas.line(x1, y1, x2, y2)') |
441 |
||
442 |
#bezier demo - show control points |
|
3574 | 443 |
makesubsection(c, "bezier curves", 8.5*inch) |
2963 | 444 |
(x1, y1, x2, y2, x3, y3, x4, y4) = ( |
3574 | 445 |
inch, 7.8*inch, |
446 |
1.2*inch, 8.8 * inch, |
|
447 |
3*inch, 8.8 * inch, |
|
448 |
3.5*inch, 8.05 * inch |
|
2963 | 449 |
) |
450 |
c.bezier(x1, y1, x2, y2, x3, y3, x4, y4) |
|
451 |
c.setDash(3,3) |
|
452 |
c.line(x1,y1,x2,y2) |
|
453 |
c.line(x3,y3,x4,y4) |
|
454 |
c.setDash() |
|
3574 | 455 |
t.setTextOrigin(4*inch, 8.3 * inch) |
2963 | 456 |
t.textLine('canvas.bezier(x1, y1, x2, y2, x3, y3, x4, y4)') |
457 |
||
458 |
#rectangle |
|
3574 | 459 |
makesubsection(c, "rectangles", 8*inch) |
460 |
c.rect(inch, 7 * inch, 2 * inch, 0.75 * inch) |
|
461 |
t.setTextOrigin(4*inch, 7.375 * inch) |
|
2963 | 462 |
t.textLine('canvas.rect(x, y, width, height) - x,y is lower left') |
463 |
||
3574 | 464 |
c.roundRect(inch,6.25*inch,2*inch,0.6*inch,0.1*inch) |
465 |
t.setTextOrigin(4*inch, 6.55*inch) |
|
466 |
t.textLine('canvas.roundRect(x,y,width,height,radius)') |
|
467 |
||
468 |
makesubsection(c, "arcs", 8*inch) |
|
469 |
c.arc(inch,5*inch,3*inch,6*inch,0,90) |
|
470 |
t.setTextOrigin(4*inch, 5.5*inch) |
|
471 |
t.textLine('canvas.arc(x1, y1, x2, y2, startDeg, extentDeg)') |
|
472 |
t.textLine('Note that this is an elliptical arc, not just circular!') |
|
473 |
||
474 |
||
2963 | 475 |
#wedge |
476 |
makesubsection(c, "wedges", 5*inch) |
|
3574 | 477 |
c.wedge(inch, 4.5*inch, 3*inch, 3.5*inch, 0, 315) |
478 |
t.setTextOrigin(4*inch, 4*inch) |
|
2963 | 479 |
t.textLine('canvas.wedge(x1, y1, x2, y2, startDeg, extentDeg)') |
480 |
t.textLine('Note that this is an elliptical arc, not just circular!') |
|
481 |
||
482 |
#wedge the other way |
|
3574 | 483 |
c.wedge(inch, 3.75*inch, 3*inch, 2.75*inch, 0, -45) |
484 |
t.setTextOrigin(4*inch, 3*inch) |
|
2963 | 485 |
t.textLine('Use a negative extent to go clockwise') |
486 |
||
487 |
#circle |
|
488 |
makesubsection(c, "circles", 3.5*inch) |
|
489 |
c.circle(1.5*inch, 2*inch, 0.5 * inch) |
|
490 |
c.circle(3*inch, 2*inch, 0.5 * inch) |
|
491 |
t.setTextOrigin(4*inch, 2 * inch) |
|
492 |
t.textLine('canvas.circle(x, y, radius)') |
|
493 |
c.drawText(t) |
|
494 |
||
495 |
c.showPage() |
|
496 |
||
497 |
############################################################## |
|
498 |
# |
|
499 |
# Page 4 - fonts |
|
500 |
# |
|
501 |
############################################################### |
|
502 |
framePage(c, "Font Control") |
|
503 |
||
504 |
c.drawString(inch, 10*inch, 'Listing available fonts...') |
|
505 |
||
506 |
y = 9.5*inch |
|
507 |
for fontname in c.getAvailableFonts(): |
|
508 |
c.setFont(fontname,24) |
|
509 |
c.drawString(inch, y, 'This should be %s' % fontname) |
|
510 |
y = y - 28 |
|
511 |
makesubsection(c, "fonts and colors", 4*inch) |
|
512 |
||
513 |
c.setFont('Times-Roman', 12) |
|
514 |
t = c.beginText(inch, 4*inch) |
|
515 |
t.textLines("""Now we'll look at the color functions and how they interact |
|
516 |
with the text. In theory, a word is just a shape; so setFillColorRGB() |
|
517 |
determines most of what you see. If you specify other text rendering |
|
518 |
modes, an outline color could be defined by setStrokeColorRGB() too""") |
|
519 |
c.drawText(t) |
|
520 |
||
521 |
t = c.beginText(inch, 2.75 * inch) |
|
522 |
t.setFont('Times-Bold',36) |
|
523 |
t.setFillColor(colors.green) #green |
|
524 |
t.textLine('Green fill, no stroke') |
|
525 |
||
526 |
#t.setStrokeColorRGB(1,0,0) #ou can do this in a text object, or the canvas. |
|
527 |
t.setStrokeColor(colors.red) #ou can do this in a text object, or the canvas. |
|
528 |
t.setTextRenderMode(2) # fill and stroke |
|
529 |
t.textLine('Green fill, red stroke - yuk!') |
|
530 |
||
531 |
t.setTextRenderMode(0) # back to default - fill only |
|
532 |
t.setFillColorRGB(0,0,0) #back to default |
|
533 |
t.setStrokeColorRGB(0,0,0) #ditto |
|
534 |
c.drawText(t) |
|
535 |
c.showPage() |
|
536 |
||
537 |
######################################################################### |
|
538 |
# |
|
539 |
# Page 5 - coord transforms |
|
540 |
# |
|
541 |
######################################################################### |
|
542 |
framePage(c, "Coordinate Transforms") |
|
543 |
c.setFont('Times-Roman', 12) |
|
544 |
t = c.beginText(inch, 10 * inch) |
|
545 |
t.textLines("""This shows coordinate transformations. We draw a set of axes, |
|
546 |
moving down the page and transforming space before each one. |
|
547 |
You can use saveState() and restoreState() to unroll transformations. |
|
548 |
Note that functions which track the text cursor give the cursor position |
|
549 |
in the current coordinate system; so if you set up a 6 inch high frame |
|
550 |
2 inches down the page to draw text in, and move the origin to its top |
|
551 |
left, you should stop writing text after six inches and not eight.""") |
|
552 |
c.drawText(t) |
|
553 |
||
554 |
drawAxes(c, "0. at origin") |
|
555 |
c.addLiteral('%about to translate space') |
|
556 |
c.translate(2*inch, 7 * inch) |
|
557 |
drawAxes(c, '1. translate near top of page') |
|
558 |
||
559 |
c.saveState() |
|
560 |
c.translate(1*inch, -2 * inch) |
|
561 |
drawAxes(c, '2. down 2 inches, across 1') |
|
562 |
c.restoreState() |
|
563 |
||
564 |
c.saveState() |
|
565 |
c.translate(0, -3 * inch) |
|
566 |
c.scale(2, -1) |
|
567 |
drawAxes(c, '3. down 3 from top, scale (2, -1)') |
|
568 |
c.restoreState() |
|
569 |
||
570 |
c.saveState() |
|
571 |
c.translate(0, -5 * inch) |
|
572 |
c.rotate(-30) |
|
573 |
drawAxes(c, "4. down 5, rotate 30' anticlockwise") |
|
574 |
c.restoreState() |
|
575 |
||
576 |
c.saveState() |
|
577 |
c.translate(3 * inch, -5 * inch) |
|
578 |
c.skew(0,30) |
|
579 |
drawAxes(c, "5. down 5, 3 across, skew beta 30") |
|
580 |
c.restoreState() |
|
581 |
||
582 |
c.showPage() |
|
583 |
||
584 |
######################################################################### |
|
585 |
# |
|
586 |
# Page 6 - clipping |
|
587 |
# |
|
588 |
######################################################################### |
|
589 |
framePage(c, "Clipping") |
|
590 |
c.setFont('Times-Roman', 12) |
|
591 |
t = c.beginText(inch, 10 * inch) |
|
592 |
t.textLines("""This shows clipping at work. We draw a chequerboard of rectangles |
|
593 |
into a path object, and clip it. This then forms a mask which limits the region of |
|
594 |
the page on which one can draw. This paragraph was drawn after setting the clipping |
|
595 |
path, and so you should only see part of the text.""") |
|
596 |
c.drawText(t) |
|
597 |
||
598 |
c.saveState() |
|
599 |
#c.setFillColorRGB(0,0,1) |
|
600 |
p = c.beginPath() |
|
601 |
#make a chesboard effect, 1 cm squares |
|
602 |
for i in range(14): |
|
603 |
x0 = (3 + i) * cm |
|
604 |
for j in range(7): |
|
605 |
y0 = (16 + j) * cm |
|
606 |
p.rect(x0, y0, 0.85*cm, 0.85*cm) |
|
607 |
c.addLiteral('%Begin clip path') |
|
608 |
c.clipPath(p) |
|
609 |
c.addLiteral('%End clip path') |
|
610 |
t = c.beginText(3 * cm, 22.5 * cm) |
|
611 |
t.textLines("""This shows clipping at work. We draw a chequerboard of rectangles |
|
612 |
into a path object, and clip it. This then forms a mask which limits the region of |
|
613 |
the page on which one can draw. This paragraph was drawn after setting the clipping |
|
614 |
path, and so you should only see part of the text. |
|
615 |
This shows clipping at work. We draw a chequerboard of rectangles |
|
616 |
into a path object, and clip it. This then forms a mask which limits the region of |
|
617 |
the page on which one can draw. This paragraph was drawn after setting the clipping |
|
618 |
path, and so you should only see part of the text. |
|
619 |
This shows clipping at work. We draw a chequerboard of rectangles |
|
620 |
into a path object, and clip it. This then forms a mask which limits the region of |
|
621 |
the page on which one can draw. This paragraph was drawn after setting the clipping |
|
622 |
path, and so you should only see part of the text.""") |
|
623 |
c.drawText(t) |
|
624 |
||
625 |
c.restoreState() |
|
626 |
||
627 |
t = c.beginText(inch, 5 * inch) |
|
628 |
t.textLines("""You can also use text as an outline for clipping with the text render mode. |
|
629 |
The API is not particularly clean on this and one has to follow the right sequence; |
|
630 |
this can be optimized shortly.""") |
|
631 |
c.drawText(t) |
|
632 |
||
633 |
#first the outline |
|
634 |
c.saveState() |
|
635 |
t = c.beginText(inch, 3.0 * inch) |
|
636 |
t.setFont('Helvetica-BoldOblique',108) |
|
637 |
t.setTextRenderMode(5) #stroke and add to path |
|
638 |
t.textLine('Python!') |
|
639 |
t.setTextRenderMode(0) |
|
640 |
c.drawText(t) #this will make a clipping mask |
|
641 |
||
642 |
#now some small stuff which wil be drawn into the current clip mask |
|
643 |
t = c.beginText(inch, 4 * inch) |
|
644 |
t.setFont('Times-Roman',6) |
|
645 |
t.textLines((('spam ' * 40) + '\n') * 15) |
|
646 |
c.drawText(t) |
|
647 |
||
648 |
#now reset canvas to get rid of the clipping mask |
|
649 |
c.restoreState() |
|
650 |
||
651 |
c.showPage() |
|
652 |
||
653 |
||
654 |
######################################################################### |
|
655 |
# |
|
656 |
# Page 7 - images |
|
657 |
# |
|
658 |
######################################################################### |
|
659 |
framePage(c, "Images") |
|
660 |
c.setFont('Times-Roman', 12) |
|
661 |
t = c.beginText(inch, 10 * inch) |
|
662 |
if not haveImages: |
|
663 |
c.drawString(inch, 11*inch, |
|
664 |
"Python or Java Imaging Library not found! Below you see rectangles instead of images.") |
|
665 |
||
666 |
t.textLines("""PDFgen uses the Python Imaging Library (or, under Jython, java.awt.image and javax.imageio) |
|
667 |
to process a very wide variety of image formats. |
|
668 |
This page shows image capabilities. If I've done things right, the bitmap should have |
|
669 |
its bottom left corner aligned with the crosshairs. |
|
670 |
There are two methods for drawing images. The recommended use is to call drawImage. |
|
671 |
This produces the smallest PDFs and the fastest generation times as each image's binary data is |
|
672 |
only embedded once in the file. Also you can use advanced features like transparency masks. |
|
673 |
You can also use drawInlineImage, which puts images in the page stream directly. |
|
674 |
This is slightly faster for Acrobat to render or for very small images, but wastes |
|
675 |
space if you use images more than once.""") |
|
676 |
||
677 |
c.drawText(t) |
|
678 |
||
679 |
if haveImages: |
|
2987 | 680 |
from reportlab.lib.testutils import testsFolder |
2966 | 681 |
gif = os.path.join(testsFolder,'pythonpowered.gif') |
2963 | 682 |
c.drawInlineImage(gif,2*inch, 7*inch) |
683 |
else: |
|
684 |
c.rect(2*inch, 7*inch, 110, 44) |
|
685 |
||
686 |
c.line(1.5*inch, 7*inch, 4*inch, 7*inch) |
|
687 |
c.line(2*inch, 6.5*inch, 2*inch, 8*inch) |
|
688 |
c.drawString(4.5 * inch, 7.25*inch, 'inline image drawn at natural size') |
|
689 |
||
690 |
if haveImages: |
|
691 |
c.drawInlineImage(gif,2*inch, 5*inch, inch, inch) |
|
692 |
else: |
|
693 |
c.rect(2*inch, 5*inch, inch, inch) |
|
694 |
||
695 |
c.line(1.5*inch, 5*inch, 4*inch, 5*inch) |
|
696 |
c.line(2*inch, 4.5*inch, 2*inch, 6*inch) |
|
697 |
c.drawString(4.5 * inch, 5.25*inch, 'inline image distorted to fit box') |
|
698 |
||
699 |
c.drawString(1.5 * inch, 4*inch, 'Image XObjects can be defined once in the file and drawn many times.') |
|
700 |
c.drawString(1.5 * inch, 3.75*inch, 'This results in faster generation and much smaller files.') |
|
701 |
||
702 |
for i in range(5): |
|
703 |
if haveImages: |
|
704 |
(w, h) = c.drawImage(gif, (1.5 + i)*inch, 3*inch) |
|
705 |
else: |
|
706 |
(w, h) = (144, 10) |
|
707 |
c.rect((1.5 + i)*inch, 3*inch, 110, 44) |
|
708 |
||
709 |
myMask = [254,255,222,223,0,1] |
|
710 |
c.drawString(1.5 * inch, 2.5*inch, "The optional 'mask' parameter lets you define transparent colors. We used a color picker") |
|
711 |
c.drawString(1.5 * inch, 2.3*inch, "to determine that the yellow in the image above is RGB=(225,223,0). We then define a mask") |
|
712 |
c.drawString(1.5 * inch, 2.1*inch, "spanning these RGB values: %s. The background vanishes!!" % myMask) |
|
713 |
c.drawString(2.5*inch, 1.2*inch, 'This would normally be obscured') |
|
714 |
if haveImages: |
|
715 |
c.drawImage(gif, 1*inch, 1.2*inch, w, h, mask=myMask) |
|
716 |
c.drawImage(gif, 3*inch, 1.2*inch, w, h, mask='auto') |
|
717 |
else: |
|
718 |
c.rect(1*inch, 1.2*inch, w, h) |
|
719 |
c.rect(3*inch, 1.2*inch, w, h) |
|
720 |
||
721 |
c.showPage() |
|
722 |
c.drawString(1*inch, 10.25*inch, "For rgba type images we can use the alpha channel if we set mask='auto'.") |
|
723 |
c.drawString(1*inch, 10.25*inch-14.4, "The first image is solid red with variable alpha.") |
|
724 |
c.drawString(1*inch, 10.25*inch-2*14.4, "The second image is white alpha=0% to purple=100%") |
|
725 |
||
726 |
||
727 |
for i in xrange(8): |
|
728 |
c.drawString(1*inch,8*inch+i*14.4,"mask=None Line %d"%i) |
|
729 |
c.drawString(3*inch,8*inch+i*14.4,"mask='auto' Line %d"%i) |
|
730 |
c.drawString(1*inch,6*inch+i*14.4,"mask=None Line %d"%i) |
|
731 |
c.drawString(3*inch,6*inch+i*14.4,"mask='auto' Line %d"%i) |
|
732 |
w = 100 |
|
733 |
h = 75 |
|
734 |
c.rect(1*inch, 8+14.4*inch, w, h) |
|
735 |
c.rect(3*inch, 8+14.4*inch, w, h) |
|
736 |
c.rect(1*inch, 6+14.4*inch, w, h) |
|
737 |
c.rect(3*inch, 6+14.4*inch, w, h) |
|
738 |
if haveImages: |
|
2987 | 739 |
from reportlab.lib.testutils import testsFolder |
2966 | 740 |
png = os.path.join(testsFolder,'solid_red_alpha.png') |
2963 | 741 |
c.drawImage(png, 1*inch, 8*inch+14.4, w, h, mask=None) |
742 |
c.drawImage(png, 3*inch, 8*inch+14.4, w, h, mask='auto') |
|
2966 | 743 |
png = os.path.join(testsFolder,'alpha_test.png') |
2963 | 744 |
c.drawImage(png, 1*inch, 6*inch+14.4, w, h, mask=None) |
745 |
c.drawImage(png, 3*inch, 6*inch+14.4, w, h, mask='auto') |
|
746 |
c.showPage() |
|
747 |
||
748 |
if haveImages: |
|
749 |
import shutil |
|
750 |
c.drawString(1*inch, 10.25*inch, 'This jpeg is actually a gif') |
|
751 |
jpg = outputfile('_i_am_actually_a_gif.jpg') |
|
752 |
shutil.copyfile(gif,jpg) |
|
753 |
c.drawImage(jpg, 1*inch, 9.25*inch, w, h, mask='auto') |
|
754 |
tjpg = os.path.join(os.path.dirname(os.path.dirname(gif)),'docs','images','lj8100.jpg') |
|
755 |
if os.path.isfile(tjpg): |
|
756 |
c.drawString(4*inch, 10.25*inch, 'This gif is actually a jpeg') |
|
757 |
tgif = outputfile(os.path.basename('_i_am_actually_a_jpeg.gif')) |
|
758 |
shutil.copyfile(tjpg,tgif) |
|
759 |
c.drawImage(tgif, 4*inch, 9.25*inch, w, h, mask='auto') |
|
760 |
||
3092 | 761 |
c.drawString(inch, 9.0*inch, 'Image positioning tests with preserveAspectRatio') |
762 |
||
2963 | 763 |
#preserveAspectRatio test |
3092 | 764 |
c.drawString(inch, 8.8*inch, 'Both of these should appear within the boxes, vertically centered') |
2963 | 765 |
|
3092 | 766 |
|
767 |
x, y, w, h = inch, 6.75* inch, 2*inch, 2*inch |
|
768 |
c.rect(x, y, w, h) |
|
769 |
(w2, h2) = c.drawImage(gif, #anchor southwest, drawImage |
|
770 |
x, y, width=w, height=h, |
|
771 |
preserveAspectRatio=True, |
|
772 |
anchor='c' |
|
773 |
) |
|
774 |
||
775 |
#now test drawInlineImage across the page |
|
776 |
x = 5 * inch |
|
777 |
c.rect(x, y, w, h) |
|
778 |
(w2, h2) = c.drawInlineImage(gif, #anchor southwest, drawInlineImage |
|
779 |
x, y, width=w, height=h, |
|
780 |
preserveAspectRatio=True, |
|
781 |
anchor='c' |
|
782 |
) |
|
2963 | 783 |
|
3092 | 784 |
c.drawString(inch, 5.75*inch, |
785 |
'anchored by respective corners - use both a wide and a tall one as tests') |
|
786 |
x = 0.25 * inch |
|
787 |
for anchor in ['nw','n','ne','w','c','e','sw','s','se']: |
|
788 |
x += 0.75*inch |
|
789 |
c.rect(x, 5*inch, 0.6*inch, 0.6*inch) |
|
790 |
c.drawImage( |
|
791 |
gif, x, 5*inch, |
|
792 |
width=0.6*inch, height=0.6*inch, |
|
793 |
preserveAspectRatio=True, |
|
794 |
anchor=anchor |
|
795 |
) |
|
796 |
c.drawString(x, 4.9*inch, anchor) |
|
797 |
||
798 |
x = 0.25 * inch |
|
799 |
tall_red = os.path.join(testsFolder,'tall_red.png') |
|
800 |
for anchor in ['nw','n','ne','w','c','e','sw','s','se']: |
|
801 |
x += 0.75*inch |
|
802 |
c.rect(x, 4*inch, 0.6*inch, 0.6*inch) |
|
803 |
c.drawImage( |
|
804 |
tall_red, x, 4*inch, |
|
805 |
width=0.6*inch, height=0.6*inch, |
|
806 |
preserveAspectRatio=True, |
|
807 |
anchor=anchor |
|
808 |
) |
|
809 |
c.drawString(x, 3.9*inch, anchor) |
|
810 |
||
811 |
||
2963 | 812 |
|
813 |
c.showPage() |
|
814 |
||
815 |
||
816 |
######################################################################### |
|
817 |
# |
|
818 |
# Page 8 - Forms and simple links |
|
819 |
# |
|
820 |
######################################################################### |
|
821 |
framePage(c, "Forms and Links") |
|
822 |
c.setFont('Times-Roman', 12) |
|
823 |
t = c.beginText(inch, 10 * inch) |
|
824 |
t.textLines("""Forms are sequences of text or graphics operations |
|
825 |
which are stored only once in a PDF file and used as many times |
|
826 |
as desired. The blue logo bar to the left is an example of a form |
|
827 |
in this document. See the function framePageForm in this demo script |
|
828 |
for an example of how to use canvas.beginForm(name, ...) ... canvas.endForm(). |
|
829 |
||
830 |
Documents can also contain cross references where (for example) a rectangle |
|
831 |
on a page may be bound to a position on another page. If the user clicks |
|
832 |
on the rectangle the PDF viewer moves to the bound position on the other |
|
833 |
page. There are many other types of annotations and links supported by PDF. |
|
834 |
||
835 |
For example, there is a bookmark to each page in this document and below |
|
836 |
is a browsable index that jumps to those pages. In addition we show two |
|
837 |
URL hyperlinks; for these, you specify a rectangle but must draw the contents |
|
838 |
or any surrounding rectangle yourself. |
|
839 |
""") |
|
840 |
c.drawText(t) |
|
841 |
||
842 |
nentries = len(titlelist) |
|
843 |
xmargin = 3*inch |
|
844 |
xmax = 7*inch |
|
845 |
ystart = 6.54*inch |
|
846 |
ydelta = 0.4*inch |
|
847 |
for i in range(nentries): |
|
848 |
yposition = ystart - i*ydelta |
|
849 |
title = titlelist[i] |
|
850 |
c.drawString(xmargin, yposition, title) |
|
851 |
c.linkAbsolute(title, title, (xmargin-ydelta/4, yposition-ydelta/4, xmax, yposition+ydelta/2)) |
|
852 |
||
853 |
# test URLs |
|
854 |
r1 = (inch, 3*inch, 5*inch, 3.25*inch) # this is x1,y1,x2,y2 |
|
855 |
c.linkURL('http://www.reportlab.com/', r1, thickness=1, color=colors.green) |
|
856 |
c.drawString(inch+3, 3*inch+6, 'Hyperlink to www.reportlab.com, with green border') |
|
857 |
||
858 |
r1 = (inch, 2.5*inch, 5*inch, 2.75*inch) # this is x1,y1,x2,y2 |
|
859 |
c.linkURL('mailto:reportlab-users@egroups.com', r1) #, border=0) |
|
860 |
c.drawString(inch+3, 2.5*inch+6, 'mailto: hyperlink, without border') |
|
861 |
||
862 |
r1 = (inch, 2*inch, 5*inch, 2.25*inch) # this is x1,y1,x2,y2 |
|
863 |
c.linkURL('http://www.reportlab.com/', r1, |
|
864 |
thickness=2, |
|
865 |
dashArray=[2,4], |
|
866 |
color=colors.magenta) |
|
867 |
c.drawString(inch+3, 2*inch+6, 'Hyperlink with custom border style') |
|
868 |
||
869 |
xpdf = fileName2Utf8(outputfile('test_hello.pdf').replace('\\','/')) |
|
870 |
link = 'Hard link to %s, with red border' % xpdf |
|
871 |
r1 = (inch, 1.5*inch, inch+2*3+c.stringWidth(link,c._fontname, c._fontsize), 1.75*inch) # this is x1,y1,x2,y2 |
|
872 |
c.linkURL(xpdf, r1, thickness=1, color=colors.red, kind='GoToR') |
|
873 |
c.drawString(inch+3, 1.5*inch+6, link ) |
|
3571
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
874 |
c.showPage() |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
875 |
|
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
876 |
############# colour gradients |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
877 |
title = 'Gradients code contributed by Peter Johnson <johnson.peter@gmail.com>' |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
878 |
c.drawString(1*inch,10.8*inch,title) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
879 |
c.addOutlineEntry(title+" section", title, level=0, closed=True) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
880 |
c.bookmarkHorizontalAbsolute(title, 10.8*inch) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
881 |
from reportlab.lib.colors import red, green, blue |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
882 |
|
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
883 |
c.saveState() |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
884 |
p = c.beginPath() |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
885 |
p.moveTo(1*inch,2*inch) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
886 |
p.lineTo(1.5*inch,2.5*inch) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
887 |
p.curveTo(2*inch,3*inch,3.0*inch,3*inch,4*inch,2.9*inch) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
888 |
p.lineTo(5.5*inch,2.1*inch) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
889 |
p.close() |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
890 |
c.clipPath(p) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
891 |
|
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
892 |
# Draw a linear gradient from (0, 2*inch) to (5*inch, 3*inch), from orange to white. |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
893 |
# The gradient will extend past the endpoints (so you probably want a clip path in place) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
894 |
c.linearGradient(1*inch, 2*inch, 6*inch, 3*inch, (red, blue)) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
895 |
c.restoreState() |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
896 |
|
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
897 |
# Draw a radial gradient with a radius of 3 inches. |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
898 |
# The color starts orange and stays orange until 20% of the radius, |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
899 |
# then fades to white at 80%, and ends up green at 3 inches from the center. |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
900 |
# Since extend is false, the gradient stops drawing at the edge of the circle. |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
901 |
c.radialGradient(4*inch, 6*inch, 3*inch, (red, green, blue), (0.2, 0.8, 1.0), extend=False) |
5fbf027dd6c3
test_pdfgen_general.py: add examples of gradients
rgbecker
parents:
3469
diff
changeset
|
902 |
c.showPage() |
2963 | 903 |
|
904 |
### now do stuff for the outline |
|
905 |
#for x in outlinenametree: print x |
|
906 |
#stop |
|
3326 | 907 |
#c.setOutlineNames0(*outlinenametree) |
2963 | 908 |
return c |
909 |
||
910 |
||
911 |
def run(filename): |
|
912 |
c = makeDocument(filename) |
|
913 |
c.setAuthor(u'R\xfcp\xe9rt B\xe8\xe4r') |
|
914 |
c.setTitle('R\xc3\xbcp\xc3\xa9rt B\xc3\xa8\xc3\xa4r\'s Book') |
|
3456 | 915 |
c.setCreator(u'Some Creator') |
916 |
c.setSubject(u'Some Subject') |
|
2963 | 917 |
c.save() |
918 |
c = makeDocument(filename) |
|
919 |
import os |
|
920 |
f = os.path.splitext(filename) |
|
921 |
f = open('%sm%s' % (f[0],f[1]),'wb') |
|
922 |
f.write(c.getpdfdata()) |
|
923 |
f.close() |
|
924 |
||
925 |
def pageShapes(c): |
|
926 |
"""Demonstrates the basic lines and shapes""" |
|
927 |
||
928 |
c.showPage() |
|
929 |
framePage(c, "Basic line and shape routines""") |
|
930 |
c.setTextOrigin(inch, 10 * inch) |
|
931 |
c.setFont('Times-Roman', 12) |
|
932 |
c.textLines("""pdfgen provides some basic routines for drawing straight and curved lines, |
|
933 |
and also for solid shapes.""") |
|
934 |
||
935 |
y = 9 * inch |
|
936 |
d = DocBlock() |
|
937 |
d.comment1 = 'Lesson one' |
|
938 |
d.code = "canvas.textOut('hello, world')" |
|
939 |
print d.code |
|
940 |
||
941 |
d.comment2 = 'Lesson two' |
|
942 |
||
943 |
d.draw(c, inch, 9 * inch) |
|
944 |
||
945 |
||
946 |
class PdfgenTestCase(unittest.TestCase): |
|
947 |
"Make documents with lots of Pdfgen features" |
|
948 |
||
949 |
def test0(self): |
|
950 |
"Make a PDFgen document with most graphics features" |
|
951 |
run(outputfile('test_pdfgen_general.pdf')) |
|
952 |
||
953 |
def test1(self): |
|
954 |
c=canvas.Canvas(outputfile('test_pdfgen_obscure.pdf')) |
|
955 |
c.setViewerPreference('PrintScaling','None') |
|
956 |
c.setViewerPreference('HideToolbar','true') |
|
957 |
c.setViewerPreference('HideMenubar','true') |
|
958 |
c.addPageLabel(0, prefix="Front") |
|
959 |
c.addPageLabel(1, style='ROMAN_LOWER', start=2) |
|
960 |
c.addPageLabel(8, style='ARABIC') |
|
961 |
# (These are fixes for missing pages) |
|
962 |
c.addPageLabel(11, style='ARABIC',start=6) |
|
963 |
c.addPageLabel(17, style='ARABIC', start=14) |
|
964 |
c.addPageLabel(21, style='ARABIC', start=22) |
|
965 |
c.addPageLabel(99, style='LETTERS_UPPER') |
|
966 |
c.addPageLabel(102, prefix="Back",start=1) |
|
967 |
||
968 |
# Make some (mostly) empty pages |
|
969 |
for i in xrange(113): |
|
970 |
c.drawString(100, 100, 'Tis is page '+str(i)) |
|
971 |
c.showPage() |
|
972 |
||
973 |
# Output the PDF |
|
974 |
c.save() |
|
975 |
||
3108 | 976 |
def test2(self): |
3160
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
977 |
c=canvas.Canvas('test_pdfgen_autocropmarks.pdf',cropMarks=True) |
3108 | 978 |
c.saveState() |
979 |
c.setStrokeColor((1,0,0)) |
|
980 |
c.rect(0,0,c._pagesize[0],c._pagesize[1],stroke=1) |
|
981 |
c.restoreState() |
|
982 |
c.drawString(72,c._pagesize[1]-72,'Auto Crop Marks') |
|
983 |
c.showPage() |
|
3160
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
984 |
c.saveState() |
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
985 |
c.setStrokeColor((1,0,0)) |
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
986 |
c.rect(0,0,c._pagesize[0],c._pagesize[1],stroke=1) |
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
987 |
c.restoreState() |
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
988 |
c.drawString(72,c._pagesize[1]-72,'Auto Crop Marks Another Page') |
3c34cc4aece0
tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents:
3108
diff
changeset
|
989 |
c.showPage() |
3108 | 990 |
c.save() |
991 |
||
3199
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
992 |
def test3(self): |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
993 |
'''some special properties''' |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
994 |
palette = [ |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
995 |
colors.CMYKColorSep(0.6,0.34,0,0.1,spotName='625C',density=1), |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
996 |
colors.CMYKColorSep(0.13,0.51,0.87,0.48,spotName='464c',density=1), |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
997 |
] |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
998 |
canv = canvas.Canvas( 'test_pdfgen_general_spots.pdf', |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
999 |
pagesize=(346,102), |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1000 |
) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1001 |
|
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1002 |
canv.setLineWidth(1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1003 |
canv.setStrokeColor(colors.CMYKColor(0,0,0,1)) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1004 |
x=10 |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1005 |
y=10 |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1006 |
for c in palette: |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1007 |
c.density = 1.0 |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1008 |
canv.setFillColor(c) |
3201
2d81dd43d7eb
test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents:
3199
diff
changeset
|
1009 |
canv.setFont('Helvetica',20) |
3199
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1010 |
canv.drawString(x,80,'This is %s' % c.spotName) |
3201
2d81dd43d7eb
test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents:
3199
diff
changeset
|
1011 |
canv.setFont('Helvetica',6) |
3199
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1012 |
canv.rect(x,y,50,50,fill=1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1013 |
canv.setFillColor(c.clone(density=0.5)) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1014 |
canv.rect(x+55,y,20,20,fill=1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1015 |
canv.setFillColor(colors.CMYKColor(0,0,1,0)) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1016 |
canv.rect(x+80,y,30,30,fill=1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1017 |
canv.rect(x+120,y,30,30,fill=1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1018 |
alpha = c is palette[0] and 1 or 0.5 |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1019 |
op = c is palette[0] and True or False |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1020 |
canv.setFillAlpha(alpha) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1021 |
canv.setFillColor(colors.CMYKColor(1,0,0,0)) |
3201
2d81dd43d7eb
test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents:
3199
diff
changeset
|
1022 |
canv.drawString(x+80+1,y+3,'OP=%d' % int(False)) |
2d81dd43d7eb
test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents:
3199
diff
changeset
|
1023 |
canv.drawString(x+80+1,y+23,'Alpha=%.1f' % alpha) |
3199
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1024 |
canv.rect(x+90,y+10,10,10,fill=1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1025 |
canv.setFillOverprint(op) |
3201
2d81dd43d7eb
test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents:
3199
diff
changeset
|
1026 |
canv.drawString(x+120+1,y+3,'OP=%d' % int(op)) |
2d81dd43d7eb
test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents:
3199
diff
changeset
|
1027 |
canv.drawString(x+120+1,y+23,'Alpha=%.1f' % alpha) |
3199
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1028 |
canv.rect(x+130,y+10,10,10,fill=1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1029 |
canv.setFillAlpha(1) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1030 |
canv.setFillOverprint(False) |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1031 |
x += canv._pagesize[0]*0.5 |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1032 |
canv.showPage() |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1033 |
canv.save() |
d490c9f4590d
reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents:
3160
diff
changeset
|
1034 |
|
3447
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1035 |
def test4(self): |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1036 |
sc = colors.CMYKColorSep |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1037 |
rgb = ['red','green','blue', 'black'] |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1038 |
cmykb = [(0,0,0,1)] |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1039 |
cmyk = [(1,0,0,0),(0,1,0,0),(0,0,1,0)]+cmykb |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1040 |
seps = [sc(1,1,0,0,spotName='sep0'),sc(0,1,1,0,spotName='sep1')] |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1041 |
sepb = [sc(0,0,0,1,spotName='sepb')] |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1042 |
#these should all work |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1043 |
trySomeColors(rgb+cmyk+seps) |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1044 |
trySomeColors(rgb,'rgb') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1045 |
trySomeColors(cmyk,'cmyk') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1046 |
trySomeColors(seps+cmyk,'sep_cmyk') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1047 |
trySomeColors(seps+sepb,'sep') #we need a fake black for now |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1048 |
trySomeColors(seps+['black']+cmykb,'sep_black') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1049 |
self.assertRaises(ValueError,trySomeColors,rgb+cmyk+seps,'rgb') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1050 |
self.assertRaises(ValueError,trySomeColors,rgb+cmyk,'rgb') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1051 |
self.assertRaises(ValueError,trySomeColors,rgb+seps,'rgb') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1052 |
trySomeColors(rgb+sepb,'rgb') #should work because blacks are convertible |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1053 |
trySomeColors(rgb+cmykb,'rgb') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1054 |
self.assertRaises(ValueError,trySomeColors,cmyk+rgb+seps,'cmyk') |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1055 |
trySomeColors(cmyk+['black']+seps,'cmyk') #OK because black & seps are convertible |
3464
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1056 |
|
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1057 |
def test5(self): |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1058 |
from reportlab.lib.pagesizes import A4,LETTER |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1059 |
canv = canvas.Canvas(outputfile('test_pdfgen_general_page_sizes.pdf'), |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1060 |
pagesize=A4, |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1061 |
) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1062 |
canv.setFont('Helvetica',10) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1063 |
S = A4 |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1064 |
canv.drawString(0,S[1]-10,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1065 |
canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1066 |
canv.drawRightString(S[0],2,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1067 |
canv.showPage() |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1068 |
S = LETTER |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1069 |
canv.setPageSize(S) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1070 |
canv.drawString(0,S[1]-10,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1071 |
canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1072 |
canv.drawRightString(S[0],2,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1073 |
canv.showPage() |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1074 |
S = A4 |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1075 |
canv.setPageSize(S) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1076 |
canv.setPageRotation(180) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1077 |
canv.drawString(0,S[1]-10,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1078 |
canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1079 |
canv.drawRightString(S[0],2,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1])) |
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1080 |
canv.showPage() |
3469
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1081 |
S = A4[1],A4[0] |
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1082 |
canv.setPageSize(S) |
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1083 |
canv.setPageRotation(0) |
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1084 |
canv.drawString(0,S[1]-30,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1])) |
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1085 |
canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1])) |
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1086 |
canv.drawRightString(S[0],32,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1])) |
be341d58c699
test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents:
3464
diff
changeset
|
1087 |
canv.showPage() |
3464
635569457733
test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents:
3456
diff
changeset
|
1088 |
canv.save() |
3447
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1089 |
|
3667
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1090 |
def testMultipleSavesOk(self): |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1091 |
c=canvas.Canvas(outputfile('test_pdfgen_savetwice.pdf')) |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1092 |
c.drawString(100, 700, 'Hello. This was saved twice') |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1093 |
c.showPage() |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1094 |
|
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1095 |
# Output the PDF |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1096 |
stuff = c.getpdfdata() |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1097 |
#multiple calls to save / getpdfdata used to cause errors |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1098 |
stuff = c.getpdfdata() |
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1099 |
|
ab79820991f9
added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents:
3617
diff
changeset
|
1100 |
|
3447
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1101 |
def trySomeColors(C,enforceColorSpace=None): |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1102 |
from StringIO import StringIO |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1103 |
out=StringIO() |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1104 |
canv = canvas.Canvas(out,enforceColorSpace=enforceColorSpace) |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1105 |
canv.setFont('Helvetica',10) |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1106 |
x = 0 |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1107 |
y = 0 |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1108 |
w,h = canv._pagesize |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1109 |
for c in C: |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1110 |
if y+10>h: |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1111 |
y = 0 |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1112 |
x += 10 |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1113 |
canv.setFillColor(c) |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1114 |
canv.rect(x,y,10,10,fill=1,stroke=0) |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1115 |
y += 10 |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1116 |
canv.showPage() |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1117 |
canv.save() |
70d7f0450df0
test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents:
3326
diff
changeset
|
1118 |
|
2963 | 1119 |
def makeSuite(): |
1120 |
return makeSuiteForClasses(PdfgenTestCase) |
|
1121 |
||
1122 |
#noruntests |
|
1123 |
if __name__ == "__main__": |
|
1124 |
unittest.TextTestRunner().run(makeSuite()) |
|
1125 |
printLocation() |