author | robin <robin@reportlab.com> |
Tue, 07 Mar 2017 10:00:34 +0000 | |
changeset 4330 | 617ffa6bbdc8 |
parent 4303 | 1dbe6269e831 |
child 4370 | 823a8c33ce43 |
permissions | -rw-r--r-- |
575 | 1 |
#!/bin/env python |
4330 | 2 |
#Copyright ReportLab Europe Ltd. 2000-2017 |
817 | 3 |
#see license.txt for license details |
2332 | 4 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/testshapes.py |
575 | 5 |
|
577 | 6 |
# testshapes.py - draws shapes onto a PDF canvas. |
575 | 7 |
|
3032 | 8 |
|
9 |
__version__ = ''' $Id $ ''' |
|
10 |
__doc__='''Execute this script to see some test drawings. |
|
575 | 11 |
|
12 |
This contains a number of routines to generate test drawings |
|
577 | 13 |
for reportlab/graphics. For now many of them are contrived, |
14 |
but we will expand them to try and trip up any parser. |
|
15 |
Feel free to add more. |
|
3032 | 16 |
''' |
575 | 17 |
|
4303
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
18 |
import os, sys, base64 |
575 | 19 |
|
20 |
from reportlab.lib import colors |
|
21 |
from reportlab.lib.units import cm |
|
3987 | 22 |
from reportlab.lib.utils import asNative |
575 | 23 |
from reportlab.pdfgen.canvas import Canvas |
24 |
from reportlab.pdfbase.pdfmetrics import stringWidth |
|
25 |
from reportlab.platypus import Flowable |
|
26 |
from reportlab.graphics.shapes import * |
|
27 |
from reportlab.graphics.renderPDF import _PDFRenderer |
|
2207 | 28 |
import unittest |
575 | 29 |
|
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
30 |
_FONTS = ['Times-Roman','Vera','Times-BoldItalic',] |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
31 |
|
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
32 |
def _setup(): |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
33 |
from reportlab.pdfbase import pdfmetrics, ttfonts |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
34 |
pdfmetrics.registerFont(ttfonts.TTFont("Vera", "Vera.ttf")) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
35 |
pdfmetrics.registerFont(ttfonts.TTFont("VeraBd", "VeraBd.ttf")) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
36 |
pdfmetrics.registerFont(ttfonts.TTFont("VeraIt", "VeraIt.ttf")) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
37 |
pdfmetrics.registerFont(ttfonts.TTFont("VeraBI", "VeraBI.ttf")) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
38 |
F = ['Times-Roman','Courier','Helvetica','Vera', 'VeraBd', 'VeraIt', 'VeraBI'] |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
39 |
if sys.platform=='win32': |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
40 |
for name, ttf in [ |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
41 |
('Adventurer Light SF','Advlit.ttf'),('ArialMS','ARIAL.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
42 |
('Arial Unicode MS', 'ARIALUNI.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
43 |
('Book Antiqua','BKANT.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
44 |
('Century Gothic','GOTHIC.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
45 |
('Comic Sans MS', 'COMIC.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
46 |
('Elementary Heavy SF Bold','Vwagh.ttf'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
47 |
('Firenze SF','flot.ttf'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
48 |
('Garamond','GARA.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
49 |
('Jagger','Rols.ttf'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
50 |
('Monotype Corsiva','MTCORSVA.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
51 |
('Seabird SF','seag.ttf'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
52 |
('Tahoma','TAHOMA.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
53 |
('VerdanaMS','VERDANA.TTF'), |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
54 |
]: |
4302 | 55 |
for D in (r'c:\WINNT',r'c:\Windows'): |
3368
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
56 |
fn = os.path.join(D,'Fonts',ttf) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
57 |
if os.path.isfile(fn): |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
58 |
try: |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
59 |
f = ttfonts.TTFont(name, fn) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
60 |
pdfmetrics.registerFont(f) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
61 |
F.append(name) |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
62 |
except: |
afa025c34493
reportlab: new base font mechanism more fully applied
rgbecker
parents:
3115
diff
changeset
|
63 |
pass |
3502 | 64 |
return F |
65 |
||
3637
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
66 |
def resetFonts(): |
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
67 |
for f in _setup(): |
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
68 |
if f not in _FONTS: |
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
69 |
_FONTS.append(f) |
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
70 |
from reportlab.rl_config import register_reset |
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
71 |
register_reset(resetFonts) |
d9cc2ec146df
fixes to testshapes & pdfform resetting contributed by Stephan Richter <srichter@cosmos.phy.tufts.edu>
rgbecker
parents:
3617
diff
changeset
|
72 |
resetFonts() |
575 | 73 |
|
74 |
######################################################### |
|
75 |
# |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
76 |
# Collections of shape drawings. |
575 | 77 |
# |
78 |
######################################################### |
|
577 | 79 |
def getFailedDrawing(funcName): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
80 |
"""Generate a drawing in case something goes really wrong. |
577 | 81 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
82 |
This will create a drawing to be displayed whenever some |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
83 |
other drawing could not be executed, because the generating |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
84 |
function does something terribly wrong! The box contains |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
85 |
an attention triangle, plus some error message. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
86 |
""" |
1683 | 87 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
88 |
D = Drawing(400, 200) |
577 | 89 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
90 |
points = [200,170, 140,80, 260,80] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
91 |
D.add(Polygon(points, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
92 |
strokeWidth=0.5*cm, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
93 |
strokeColor=colors.red, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
94 |
fillColor=colors.yellow)) |
577 | 95 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
96 |
s = String(200, 40, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
97 |
"Error in generating function '%s'!" % funcName, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
98 |
textAnchor='middle') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
99 |
D.add(s) |
577 | 100 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
101 |
return D |
577 | 102 |
|
103 |
||
104 |
# These are the real drawings to be eye-balled. |
|
105 |
||
587 | 106 |
def getDrawing01(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
107 |
"""Hello World, on a rectangular background. |
575 | 108 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
109 |
The rectangle's fillColor is yellow. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
110 |
The string's fillColor is red. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
111 |
""" |
1683 | 112 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
113 |
D = Drawing(400, 200) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
114 |
D.add(Rect(50, 50, 300, 100, fillColor=colors.yellow)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
115 |
D.add(String(180,100, 'Hello World', fillColor=colors.red)) |
3987 | 116 |
D.add(String(180,86, b'Special characters \xc2\xa2\xc2\xa9\xc2\xae\xc2\xa3\xce\xb1\xce\xb2', fillColor=colors.red)) |
575 | 117 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
118 |
return D |
575 | 119 |
|
120 |
||
587 | 121 |
def getDrawing02(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
122 |
"""Various Line shapes. |
575 | 123 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
124 |
The lines are blue and their strokeWidth is 5 mm. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
125 |
One line has a strokeDashArray set to [5, 10, 15]. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
126 |
""" |
1683 | 127 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
128 |
D = Drawing(400, 200) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
129 |
D.add(Line(50,50, 300,100, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
130 |
strokeColor=colors.blue, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
131 |
strokeWidth=0.5*cm, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
132 |
)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
133 |
D.add(Line(50,100, 300,50, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
134 |
strokeColor=colors.blue, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
135 |
strokeWidth=0.5*cm, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
136 |
strokeDashArray=[5, 10, 15], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
137 |
)) |
575 | 138 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
139 |
#x = 1/0 # Comment this to see the actual drawing! |
576 | 140 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
141 |
return D |
576 | 142 |
|
143 |
||
587 | 144 |
def getDrawing03(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
145 |
"""Text strings in various sizes and different fonts. |
578
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
146 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
147 |
Font size increases from 12 to 36 and from bottom left |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
148 |
to upper right corner. The first ones should be in |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
149 |
Times-Roman. Finally, a solitary Courier string at |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
150 |
the top right corner. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
151 |
""" |
1683 | 152 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
153 |
D = Drawing(400, 200) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
154 |
for size in range(12, 36, 4): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
155 |
D.add(String(10+size*2, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
156 |
10+size*2, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
157 |
'Hello World', |
2112 | 158 |
fontName=_FONTS[0], |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
159 |
fontSize=size)) |
578
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
160 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
161 |
D.add(String(150, 150, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
162 |
'Hello World', |
2112 | 163 |
fontName=_FONTS[1], |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
164 |
fontSize=36)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
165 |
return D |
578
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
166 |
|
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
167 |
|
587 | 168 |
def getDrawing04(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
169 |
"""Text strings in various colours. |
575 | 170 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
171 |
Colours are blue, yellow and red from bottom left |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
172 |
to upper right. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
173 |
""" |
1683 | 174 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
175 |
D = Drawing(400, 200) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
176 |
i = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
177 |
for color in (colors.blue, colors.yellow, colors.red): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
178 |
D.add(String(50+i*30, 50+i*30, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
179 |
'Hello World', fillColor=color)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
180 |
i = i + 1 |
576 | 181 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
182 |
return D |
576 | 183 |
|
184 |
||
587 | 185 |
def getDrawing05(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
186 |
"""Text strings with various anchors (alignments). |
576 | 187 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
188 |
Text alignment conforms to the anchors in the left column. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
189 |
""" |
1683 | 190 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
191 |
D = Drawing(400, 200) |
576 | 192 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
193 |
lineX = 250 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
194 |
D.add(Line(lineX,10, lineX,190, strokeColor=colors.gray)) |
576 | 195 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
196 |
y = 130 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
197 |
for anchor in ('start', 'middle', 'end'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
198 |
D.add(String(lineX, y, 'Hello World', textAnchor=anchor)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
199 |
D.add(String(50, y, anchor + ':')) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
200 |
y = y - 30 |
576 | 201 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
202 |
return D |
576 | 203 |
|
204 |
||
587 | 205 |
def getDrawing06(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
206 |
"""This demonstrates all the basic shapes at once. |
575 | 207 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
208 |
There are no groups or references. |
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
209 |
Each solid shape should have a green fill. |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
210 |
""" |
575 | 211 |
|
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
212 |
green = colors.green |
1683 | 213 |
|
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
214 |
D = Drawing(400, 200) #, fillColor=green) |
1683 | 215 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
216 |
D.add(Line(10,10, 390,190)) |
575 | 217 |
|
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
218 |
D.add(Circle(100,100,20, fillColor=green)) |
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
219 |
D.add(Circle(200,100,40, fillColor=green)) |
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
220 |
D.add(Circle(300,100,30, fillColor=green)) |
575 | 221 |
|
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
222 |
D.add(Wedge(330,100,40, -10,40, fillColor=green)) |
575 | 223 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
224 |
D.add(PolyLine([120,10, 130,20, 140,10, 150,20, 160,10, |
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
225 |
170,20, 180,10, 190,20, 200,10], fillColor=green)) |
575 | 226 |
|
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
227 |
D.add(Polygon([300,20, 350,20, 390,80, 300,75, 330,40], fillColor=green)) |
575 | 228 |
|
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
229 |
D.add(Ellipse(50,150, 40, 20, fillColor=green)) |
575 | 230 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
231 |
D.add(Rect(120,150, 60,30, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
232 |
strokeWidth=10, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
233 |
strokeColor=colors.yellow, |
3115
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
234 |
fillColor=green)) #square corners |
049d6a82b49a
renderSVG.py: add a unit (px) to fontSizes following suggestion by PJACock
rgbecker
parents:
3066
diff
changeset
|
235 |
D.add(Rect(220, 150, 60, 30, 10, 10, fillColor=green)) #round corners |
575 | 236 |
|
3372 | 237 |
D.add(String(10,50, 'Basic Shapes', fillColor=colors.black, fontName='Helvetica')) |
575 | 238 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
239 |
return D |
575 | 240 |
|
587 | 241 |
def getDrawing07(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
242 |
"""This tests the ability to translate and rotate groups. The first set of axes should be |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
243 |
near the bottom left of the drawing. The second should be rotated counterclockwise |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
244 |
by 15 degrees. The third should be rotated by 30 degrees.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
245 |
D = Drawing(400, 200) |
575 | 246 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
247 |
Axis = Group( |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
248 |
Line(0,0,100,0), #x axis |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
249 |
Line(0,0,0,50), # y axis |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
250 |
Line(0,10,10,10), #ticks on y axis |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
251 |
Line(0,20,10,20), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
252 |
Line(0,30,10,30), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
253 |
Line(0,40,10,40), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
254 |
Line(10,0,10,10), #ticks on x axis |
1683 | 255 |
Line(20,0,20,10), |
256 |
Line(30,0,30,10), |
|
257 |
Line(40,0,40,10), |
|
258 |
Line(50,0,50,10), |
|
259 |
Line(60,0,60,10), |
|
260 |
Line(70,0,70,10), |
|
261 |
Line(80,0,80,10), |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
262 |
Line(90,0,90,10), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
263 |
String(20, 35, 'Axes', fill=colors.black) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
264 |
) |
578
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
265 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
266 |
firstAxisGroup = Group(Axis) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
267 |
firstAxisGroup.translate(10,10) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
268 |
D.add(firstAxisGroup) |
1683 | 269 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
270 |
secondAxisGroup = Group(Axis) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
271 |
secondAxisGroup.translate(150,10) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
272 |
secondAxisGroup.rotate(15) |
1683 | 273 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
274 |
D.add(secondAxisGroup) |
578
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
275 |
|
f797bf806ee5
Added group tests, fixed font bug, resynched with Dinu
andy_robinson
parents:
577
diff
changeset
|
276 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
277 |
thirdAxisGroup = Group(Axis, transform=mmult(translate(300,10), rotate(30))) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
278 |
D.add(thirdAxisGroup) |
1683 | 279 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
280 |
return D |
575 | 281 |
|
282 |
||
587 | 283 |
def getDrawing08(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
284 |
"""This tests the ability to scale coordinates. The bottom left set of axes should be |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
285 |
near the bottom left of the drawing. The bottom right should be stretched vertically |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
286 |
by a factor of 2. The top left one should be stretched horizontally by a factor of 2. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
287 |
The top right should have the vertical axiss leaning over to the right by 30 degrees.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
288 |
D = Drawing(400, 200) |
575 | 289 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
290 |
Axis = Group( |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
291 |
Line(0,0,100,0), #x axis |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
292 |
Line(0,0,0,50), # y axis |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
293 |
Line(0,10,10,10), #ticks on y axis |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
294 |
Line(0,20,10,20), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
295 |
Line(0,30,10,30), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
296 |
Line(0,40,10,40), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
297 |
Line(10,0,10,10), #ticks on x axis |
1683 | 298 |
Line(20,0,20,10), |
299 |
Line(30,0,30,10), |
|
300 |
Line(40,0,40,10), |
|
301 |
Line(50,0,50,10), |
|
302 |
Line(60,0,60,10), |
|
303 |
Line(70,0,70,10), |
|
304 |
Line(80,0,80,10), |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
305 |
Line(90,0,90,10), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
306 |
String(20, 35, 'Axes', fill=colors.black) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
307 |
) |
575 | 308 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
309 |
firstAxisGroup = Group(Axis) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
310 |
firstAxisGroup.translate(10,10) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
311 |
D.add(firstAxisGroup) |
1683 | 312 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
313 |
secondAxisGroup = Group(Axis) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
314 |
secondAxisGroup.translate(150,10) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
315 |
secondAxisGroup.scale(1,2) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
316 |
D.add(secondAxisGroup) |
1683 | 317 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
318 |
thirdAxisGroup = Group(Axis) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
319 |
thirdAxisGroup.translate(10,125) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
320 |
thirdAxisGroup.scale(2,1) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
321 |
D.add(thirdAxisGroup) |
575 | 322 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
323 |
fourthAxisGroup = Group(Axis) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
324 |
fourthAxisGroup.translate(250,125) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
325 |
fourthAxisGroup.skew(30,0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
326 |
D.add(fourthAxisGroup) |
575 | 327 |
|
1683 | 328 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
329 |
return D |
575 | 330 |
|
587 | 331 |
def getDrawing09(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
332 |
"""This tests rotated strings |
580
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
333 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
334 |
Some renderers will have a separate mechanism for font drawing. This test |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
335 |
just makes sure strings get transformed the same way as regular graphics.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
336 |
D = Drawing(400, 200) |
580
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
337 |
|
2112 | 338 |
fontName = _FONTS[0] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
339 |
fontSize = 12 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
340 |
text = "I should be totally horizontal and enclosed in a box" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
341 |
textWidth = stringWidth(text, fontName, fontSize) |
580
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
342 |
|
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
343 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
344 |
g1 = Group( |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
345 |
String(20, 20, text, fontName=fontName, fontSize = fontSize), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
346 |
Rect(18, 18, textWidth + 4, fontSize + 4, fillColor=None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
347 |
) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
348 |
D.add(g1) |
580
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
349 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
350 |
text = "I should slope up by 15 degrees, so my right end is higher than my left" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
351 |
textWidth = stringWidth(text, fontName, fontSize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
352 |
g2 = Group( |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
353 |
String(20, 20, text, fontName=fontName, fontSize = fontSize), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
354 |
Rect(18, 18, textWidth + 4, fontSize + 4, fillColor=None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
355 |
) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
356 |
g2.translate(0, 50) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
357 |
g2.rotate(15) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
358 |
D.add(g2) |
580
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
359 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
360 |
return D |
580
62e61180ae41
Added postscript renderer and tests, fixed renderer bugs
andy_robinson
parents:
578
diff
changeset
|
361 |
|
587 | 362 |
def getDrawing10(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
363 |
"""This tests nested groups with multiple levels of coordinate transformation. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
364 |
Each box should be staggered up and to the right, moving by 25 points each time.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
365 |
D = Drawing(400, 200) |
587 | 366 |
|
2112 | 367 |
fontName = _FONTS[0] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
368 |
fontSize = 12 |
587 | 369 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
370 |
g1 = Group( |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
371 |
Rect(0, 0, 100, 20, fillColor=colors.yellow), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
372 |
String(5, 5, 'Text in the box', fontName=fontName, fontSize = fontSize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
373 |
) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
374 |
D.add(g1) |
587 | 375 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
376 |
g2 = Group(g1, transform = translate(25,25)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
377 |
D.add(g2) |
1683 | 378 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
379 |
g3 = Group(g2, transform = translate(25,25)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
380 |
D.add(g3) |
590 | 381 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
382 |
g4 = Group(g3, transform = translate(25,25)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
383 |
D.add(g4) |
590 | 384 |
|
1683 | 385 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
386 |
return D |
585
e0144950b3e2
Fixes to CTM to support bitmap renderer; extra string rotation
andy_robinson
parents:
580
diff
changeset
|
387 |
|
3975
4a3599863c11
eliminate from . imports in favour of absolutes to allow running modules
robin
parents:
3721
diff
changeset
|
388 |
from reportlab.graphics.widgets.signsandsymbols import SmileyFace |
817 | 389 |
def getDrawing11(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
390 |
'''test of anchoring''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
391 |
def makeSmiley(x, y, size, color): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
392 |
"Make a smiley data item representation." |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
393 |
d = size |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
394 |
s = SmileyFace() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
395 |
s.fillColor = color |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
396 |
s.x = x-d |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
397 |
s.y = y-d |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
398 |
s.size = d*2 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
399 |
return s |
817 | 400 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
401 |
D = Drawing(400, 200) #, fillColor=colors.purple) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
402 |
g = Group(transform=(1,0,0,1,0,0)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
403 |
g.add(makeSmiley(100,100,10,colors.red)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
404 |
g.add(Line(90,100,110,100,strokeColor=colors.green)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
405 |
g.add(Line(100,90,100,110,strokeColor=colors.green)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
406 |
D.add(g) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
407 |
g = Group(transform=(2,0,0,2,100,-100)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
408 |
g.add(makeSmiley(100,100,10,colors.blue)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
409 |
g.add(Line(90,100,110,100,strokeColor=colors.green)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
410 |
g.add(Line(100,90,100,110,strokeColor=colors.green)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
411 |
D.add(g) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
412 |
g = Group(transform=(2,0,0,2,0,0)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
413 |
return D |
817 | 414 |
|
577 | 415 |
|
1445 | 416 |
def getDrawing12(): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
417 |
"""Text strings in a non-standard font. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
418 |
All that is required is to place the .afm and .pfb files |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
419 |
on the font patch given in rl_config.py, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
420 |
for example in reportlab/lib/fonts/. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
421 |
""" |
2977
beca8d75f400
reportlab: remove LetErrorRobot-Chrome, rina & luxi. Add DarkGardenMK and fix tests
rgbecker
parents:
2964
diff
changeset
|
422 |
faceName = "DarkGardenMK" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
423 |
D = Drawing(400, 200) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
424 |
for size in range(12, 36, 4): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
425 |
D.add(String(10+size*2, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
426 |
10+size*2, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
427 |
'Hello World', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
428 |
fontName=faceName, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
429 |
fontSize=size)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
430 |
return D |
577 | 431 |
|
2143 | 432 |
def getDrawing13(): |
433 |
'Test Various TTF Fonts' |
|
434 |
||
435 |
def drawit(F,w=400,h=200,fontSize=12,slack=2,gap=5): |
|
436 |
D = Drawing(w,h) |
|
437 |
th = 2*gap + fontSize*1.2 |
|
438 |
gh = gap + .2*fontSize |
|
439 |
y = h |
|
440 |
maxx = 0 |
|
441 |
for fontName in F: |
|
442 |
y -= th |
|
3987 | 443 |
text = fontName+asNative(b': I should be totally horizontal and enclosed in a box and end in alphabetagamma \xc2\xa2\xc2\xa9\xc2\xae\xc2\xa3\xca\xa5\xd0\x96\xd6\x83\xd7\x90\xd9\x82\xe0\xa6\x95\xce\xb1\xce\xb2\xce\xb3') |
2143 | 444 |
textWidth = stringWidth(text, fontName, fontSize) |
445 |
maxx = max(maxx,textWidth+20) |
|
446 |
D.add( |
|
447 |
Group(Rect(8, y-gh, textWidth + 4, th, strokeColor=colors.red, strokeWidth=.5, fillColor=colors.lightgrey), |
|
448 |
String(10, y, text, fontName=fontName, fontSize = fontSize))) |
|
449 |
y -= 5 |
|
450 |
return maxx, h-y+gap, D |
|
3502 | 451 |
maxx, maxy, D = drawit(_FONTS) |
452 |
if maxx>400 or maxy>200: _,_,D = drawit(_FONTS,maxx,maxy) |
|
2143 | 453 |
return D |
454 |
||
4303
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
455 |
def smallArrow(): |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
456 |
'''create a small PIL image''' |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
457 |
from reportlab.graphics.renderPM import _getImage |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
458 |
from reportlab.lib.utils import getBytesIO |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
459 |
b = base64.decodestring(b'''R0lGODdhCgAHAIMAAP/////29v/d3f+ysv9/f/9VVf9MTP8iIv8ICP8AAAAAAAAAAAAAAAAAAAAA |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
460 |
AAAAACwAAAAACgAHAAAIMwABCBxIsKABAQASFli4MAECAgEAJJhIceKBAQkyasx4YECBjx8TICAQ |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
461 |
AIDJkwYEAFgZEAA7''') |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
462 |
return _getImage().open(getBytesIO(b)) |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
463 |
|
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
464 |
def getDrawing14(): |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
465 |
'''test shapes.Image''' |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
466 |
from reportlab.graphics.shapes import Image |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
467 |
D = Drawing(400, 200) |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
468 |
im0 = smallArrow() |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
469 |
D.add(Image(x=0,y=0,width=None,height=None,path=im0)) |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
470 |
im1 = smallArrow() |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
471 |
D.add(Image(x=400-20,y=200-14,width=20,height=14,path=im1)) |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
472 |
return D |
1dbe6269e831
improved support for images in renderPM/renderSVG bug report from Claude Paroz; version --> 3.3.26
robin
parents:
4302
diff
changeset
|
473 |
|
2184 | 474 |
def getAllFunctionDrawingNames(doTTF=1): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
475 |
"Get a list of drawing function names from somewhere." |
1445 | 476 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
477 |
funcNames = [] |
1445 | 478 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
479 |
# Here we get the names from the global name space. |
3721 | 480 |
symbols = list(globals().keys()) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
481 |
symbols.sort() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
482 |
for funcName in symbols: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
483 |
if funcName[0:10] == 'getDrawing': |
2184 | 484 |
if doTTF or funcName!='getDrawing13': |
485 |
funcNames.append(funcName) |
|
1445 | 486 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
487 |
return funcNames |
577 | 488 |
|
960 | 489 |
def _evalFuncDrawing(name, D, l=None, g=None): |
2575 | 490 |
try: |
491 |
d = eval(name + '()', g or globals(), l or locals()) |
|
492 |
except: |
|
493 |
d = getFailedDrawing(name) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
494 |
D.append((d, eval(name + '.__doc__'), name[3:])) |
939 | 495 |
|
2184 | 496 |
def getAllTestDrawings(doTTF=1): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
497 |
D = [] |
2184 | 498 |
for f in getAllFunctionDrawingNames(doTTF=doTTF): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
499 |
_evalFuncDrawing(f,D) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
500 |
return D |
939 | 501 |
|
577 | 502 |
def writePDF(drawings): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
503 |
"Create and save a PDF file containing some drawings." |
1683 | 504 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
505 |
pdfPath = os.path.splitext(sys.argv[0])[0] + '.pdf' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
506 |
c = Canvas(pdfPath) |
2112 | 507 |
c.setFont(_FONTS[0], 32) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
508 |
c.drawString(80, 750, 'ReportLab Graphics-Shapes Test') |
575 | 509 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
510 |
# Print drawings in a loop, with their doc strings. |
2112 | 511 |
c.setFont(_FONTS[0], 12) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
512 |
y = 740 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
513 |
i = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
514 |
for (drawing, docstring, funcname) in drawings: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
515 |
if y < 300: # Allows 5-6 lines of text. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
516 |
c.showPage() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
517 |
y = 740 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
518 |
# Draw a title. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
519 |
y = y - 30 |
2112 | 520 |
c.setFont(_FONTS[2],12) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
521 |
c.drawString(80, y, '%s (#%d)' % (funcname, i)) |
2112 | 522 |
c.setFont(_FONTS[0],12) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
523 |
y = y - 14 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
524 |
textObj = c.beginText(80, y) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
525 |
textObj.textLines(docstring) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
526 |
c.drawText(textObj) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
527 |
y = textObj.getY() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
528 |
y = y - drawing.height |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
529 |
drawing.drawOn(c, 80, y) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
530 |
i = i + 1 |
575 | 531 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
532 |
c.save() |
3721 | 533 |
print('wrote %s ' % pdfPath) |
1683 | 534 |
|
577 | 535 |
|
536 |
class ShapesTestCase(unittest.TestCase): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
537 |
"Test generating all kinds of shapes." |
577 | 538 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
539 |
def setUp(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
540 |
"Prepare some things before the tests start." |
577 | 541 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
542 |
self.funcNames = getAllFunctionDrawingNames() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
543 |
self.drawings = [] |
575 | 544 |
|
545 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
546 |
def tearDown(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
547 |
"Do what has to be done after the tests are over." |
577 | 548 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
549 |
writePDF(self.drawings) |
1683 | 550 |
|
577 | 551 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
552 |
# This should always succeed. If each drawing would be |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
553 |
# wrapped in a dedicated test method like this one, it |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
554 |
# would be possible to have a count for wrong tests |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
555 |
# as well... Something like this is left for later... |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
556 |
def testAllDrawings(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
557 |
"Make a list of drawings." |
577 | 558 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
559 |
for f in self.funcNames: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
560 |
if f[0:10] == 'getDrawing': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
561 |
# Make an instance and get its doc string. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
562 |
# If that fails, use a default error drawing. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
563 |
_evalFuncDrawing(f,self.drawings) |
577 | 564 |
|
565 |
||
566 |
def makeSuite(): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
567 |
"Make a test suite for unit testing." |
1683 | 568 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
569 |
suite = unittest.TestSuite() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
570 |
suite.addTest(ShapesTestCase('testAllDrawings')) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1445
diff
changeset
|
571 |
return suite |
577 | 572 |
|
573 |
||
574 |
if __name__ == "__main__": |
|
2112 | 575 |
unittest.TextTestRunner().run(makeSuite()) |