4330
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2017
|
2963
|
2 |
#see license.txt for license details
|
|
3 |
"""
|
|
4 |
Tests for the text Label class.
|
|
5 |
"""
|
2984
|
6 |
from reportlab.lib.testutils import setOutDir,setOutDir,makeSuiteForClasses, outputfile, printLocation
|
|
7 |
setOutDir(__name__)
|
2963
|
8 |
|
|
9 |
import os, sys, copy
|
|
10 |
from os.path import join, basename, splitext
|
2966
|
11 |
import unittest
|
2963
|
12 |
from reportlab.lib import colors
|
|
13 |
from reportlab.lib.units import cm
|
|
14 |
from reportlab.lib.pagesizes import A4
|
|
15 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
16 |
from reportlab.pdfgen.canvas import Canvas
|
|
17 |
from reportlab.graphics.shapes import *
|
|
18 |
from reportlab.graphics.charts.textlabels import Label
|
|
19 |
from reportlab.platypus.flowables import Spacer, PageBreak
|
|
20 |
from reportlab.platypus.paragraph import Paragraph
|
|
21 |
from reportlab.platypus.xpreformatted import XPreformatted
|
|
22 |
from reportlab.platypus.frames import Frame
|
|
23 |
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
|
|
24 |
|
|
25 |
|
|
26 |
def myMainPageFrame(canvas, doc):
|
|
27 |
"The page frame used for all PDF documents."
|
|
28 |
|
|
29 |
canvas.saveState()
|
|
30 |
|
|
31 |
#canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
|
|
32 |
canvas.setFont('Times-Roman', 12)
|
|
33 |
pageNumber = canvas.getPageNumber()
|
|
34 |
canvas.drawString(10*cm, cm, str(pageNumber))
|
|
35 |
|
|
36 |
canvas.restoreState()
|
|
37 |
|
|
38 |
|
|
39 |
class MyDocTemplate(BaseDocTemplate):
|
|
40 |
"The document template used for all PDF documents."
|
|
41 |
|
|
42 |
_invalidInitArgs = ('pageTemplates',)
|
|
43 |
|
|
44 |
def __init__(self, filename, **kw):
|
|
45 |
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
|
|
46 |
self.allowSplitting = 0
|
3326
|
47 |
BaseDocTemplate.__init__(self, filename, **kw)
|
2963
|
48 |
template = PageTemplate('normal', [frame1], myMainPageFrame)
|
|
49 |
self.addPageTemplates(template)
|
|
50 |
|
|
51 |
|
|
52 |
class LabelTestCase(unittest.TestCase):
|
|
53 |
"Test Label class."
|
|
54 |
|
|
55 |
def _test0(self):
|
|
56 |
"Perform original test function."
|
|
57 |
|
|
58 |
pdfPath = outputfile('test_charts_textlabels.pdf')
|
|
59 |
c = Canvas(pdfPath)
|
|
60 |
|
|
61 |
label = Label()
|
|
62 |
demoLabel = label.demo()
|
|
63 |
demoLabel.drawOn(c, 0, 0)
|
|
64 |
|
|
65 |
c.save()
|
|
66 |
|
|
67 |
|
|
68 |
def _makeProtoLabel(self):
|
|
69 |
"Return a label prototype for further modification."
|
|
70 |
|
|
71 |
protoLabel = Label()
|
|
72 |
protoLabel.dx = 0
|
|
73 |
protoLabel.dy = 0
|
|
74 |
protoLabel.boxStrokeWidth = 0.1
|
|
75 |
protoLabel.boxStrokeColor = colors.black
|
|
76 |
protoLabel.boxFillColor = colors.yellow
|
|
77 |
# protoLabel.text = 'Hello World!' # Does not work as expected.
|
|
78 |
|
|
79 |
return protoLabel
|
|
80 |
|
|
81 |
|
|
82 |
def _makeDrawings(self, protoLabel, text=None):
|
|
83 |
# Set drawing dimensions.
|
|
84 |
w, h = drawWidth, drawHeight = 400, 100
|
|
85 |
|
|
86 |
drawings = []
|
|
87 |
|
|
88 |
for boxAnchors in ('sw se nw ne', 'w e n s', 'c'):
|
3794
|
89 |
boxAnchors = boxAnchors.split(' ')
|
2963
|
90 |
|
|
91 |
# Create drawing.
|
|
92 |
d = Drawing(w, h)
|
3326
|
93 |
d.add(Line(0, h*0.5, w, h*0.5, strokeColor=colors.gray, strokeWidth=0.5))
|
|
94 |
d.add(Line(w*0.5 ,0, w*0.5, h, strokeColor=colors.gray, strokeWidth=0.5))
|
2963
|
95 |
|
|
96 |
labels = []
|
|
97 |
for boxAnchor in boxAnchors:
|
|
98 |
# Modify label, put it on a drawing.
|
|
99 |
label = copy.deepcopy(protoLabel)
|
|
100 |
label.boxAnchor = boxAnchor
|
|
101 |
args = {'ba':boxAnchor, 'text':text or 'Hello World!'}
|
|
102 |
label.setText('(%(ba)s) %(text)s (%(ba)s)' % args)
|
|
103 |
labels.append(label)
|
|
104 |
|
|
105 |
for label in labels:
|
|
106 |
d.add(label)
|
|
107 |
|
|
108 |
drawings.append(d)
|
|
109 |
|
|
110 |
return drawings
|
|
111 |
|
|
112 |
|
|
113 |
def test1(self):
|
|
114 |
"Test all different box anchors."
|
|
115 |
|
|
116 |
# Build story.
|
|
117 |
story = []
|
|
118 |
styleSheet = getSampleStyleSheet()
|
|
119 |
bt = styleSheet['BodyText']
|
|
120 |
h1 = styleSheet['Heading1']
|
|
121 |
h2 = styleSheet['Heading2']
|
|
122 |
h3 = styleSheet['Heading3']
|
|
123 |
|
|
124 |
story.append(Paragraph('Tests for class <i>Label</i>', h1))
|
|
125 |
story.append(Paragraph('Testing box anchors', h2))
|
|
126 |
story.append(Paragraph("""This should display "Hello World" labels
|
|
127 |
written as black text on a yellow box relative to the origin of the crosshair
|
|
128 |
axes. The labels indicate their relative position being one of the nine
|
|
129 |
canonical points of a box: sw, se, nw, ne, w, e, n, s or c (standing for
|
|
130 |
<i>southwest</i>, <i>southeast</i>... and <i>center</i>).""", bt))
|
|
131 |
story.append(Spacer(0, 0.5*cm))
|
|
132 |
|
|
133 |
# Round 1a
|
|
134 |
story.append(Paragraph('Helvetica 10pt', h3))
|
|
135 |
story.append(Spacer(0, 0.5*cm))
|
|
136 |
|
|
137 |
w, h = drawWidth, drawHeight = 400, 100
|
|
138 |
protoLabel = self._makeProtoLabel()
|
3326
|
139 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
140 |
protoLabel.textAnchor = 'start'
|
|
141 |
protoLabel.fontName = 'Helvetica'
|
|
142 |
protoLabel.fontSize = 10
|
|
143 |
drawings = self._makeDrawings(protoLabel)
|
|
144 |
for d in drawings:
|
|
145 |
story.append(d)
|
|
146 |
story.append(Spacer(0, 1*cm))
|
|
147 |
|
|
148 |
story.append(PageBreak())
|
|
149 |
|
|
150 |
# Round 1b
|
|
151 |
story.append(Paragraph('Helvetica 18pt', h3))
|
|
152 |
story.append(Spacer(0, 0.5*cm))
|
|
153 |
|
|
154 |
w, h = drawWidth, drawHeight = 400, 100
|
|
155 |
protoLabel = self._makeProtoLabel()
|
3326
|
156 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
157 |
protoLabel.textAnchor = 'start'
|
|
158 |
protoLabel.fontName = 'Helvetica'
|
|
159 |
protoLabel.fontSize = 18
|
|
160 |
drawings = self._makeDrawings(protoLabel)
|
|
161 |
for d in drawings:
|
|
162 |
story.append(d)
|
|
163 |
story.append(Spacer(0, 1*cm))
|
|
164 |
|
|
165 |
story.append(PageBreak())
|
|
166 |
|
|
167 |
# Round 1c
|
|
168 |
story.append(Paragraph('Helvetica 18pt, multi-line', h3))
|
|
169 |
story.append(Spacer(0, 0.5*cm))
|
|
170 |
|
|
171 |
w, h = drawWidth, drawHeight = 400, 100
|
|
172 |
protoLabel = self._makeProtoLabel()
|
3326
|
173 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
174 |
protoLabel.textAnchor = 'start'
|
|
175 |
protoLabel.fontName = 'Helvetica'
|
|
176 |
protoLabel.fontSize = 18
|
|
177 |
drawings = self._makeDrawings(protoLabel, text='Hello\nWorld!')
|
|
178 |
for d in drawings:
|
|
179 |
story.append(d)
|
|
180 |
story.append(Spacer(0, 1*cm))
|
|
181 |
|
|
182 |
story.append(PageBreak())
|
|
183 |
|
|
184 |
story.append(Paragraph('Testing text (and box) anchors', h2))
|
|
185 |
story.append(Paragraph("""This should display labels as before,
|
|
186 |
but now with a fixes size and showing some effect of setting the
|
|
187 |
textAnchor attribute.""", bt))
|
|
188 |
story.append(Spacer(0, 0.5*cm))
|
|
189 |
|
|
190 |
# Round 2a
|
|
191 |
story.append(Paragraph("Helvetica 10pt, textAnchor='start'", h3))
|
|
192 |
story.append(Spacer(0, 0.5*cm))
|
|
193 |
|
|
194 |
w, h = drawWidth, drawHeight = 400, 100
|
|
195 |
protoLabel = self._makeProtoLabel()
|
3326
|
196 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
197 |
protoLabel.width = 4*cm
|
|
198 |
protoLabel.height = 1.5*cm
|
|
199 |
protoLabel.textAnchor = 'start'
|
|
200 |
protoLabel.fontName = 'Helvetica'
|
|
201 |
protoLabel.fontSize = 10
|
|
202 |
drawings = self._makeDrawings(protoLabel)
|
|
203 |
for d in drawings:
|
|
204 |
story.append(d)
|
|
205 |
story.append(Spacer(0, 1*cm))
|
|
206 |
|
|
207 |
story.append(PageBreak())
|
|
208 |
|
|
209 |
# Round 2b
|
|
210 |
story.append(Paragraph("Helvetica 10pt, textAnchor='middle'", h3))
|
|
211 |
story.append(Spacer(0, 0.5*cm))
|
|
212 |
|
|
213 |
w, h = drawWidth, drawHeight = 400, 100
|
|
214 |
protoLabel = self._makeProtoLabel()
|
3326
|
215 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
216 |
protoLabel.width = 4*cm
|
|
217 |
protoLabel.height = 1.5*cm
|
|
218 |
protoLabel.textAnchor = 'middle'
|
|
219 |
protoLabel.fontName = 'Helvetica'
|
|
220 |
protoLabel.fontSize = 10
|
|
221 |
drawings = self._makeDrawings(protoLabel)
|
|
222 |
for d in drawings:
|
|
223 |
story.append(d)
|
|
224 |
story.append(Spacer(0, 1*cm))
|
|
225 |
|
|
226 |
story.append(PageBreak())
|
|
227 |
|
|
228 |
# Round 2c
|
|
229 |
story.append(Paragraph("Helvetica 10pt, textAnchor='end'", h3))
|
|
230 |
story.append(Spacer(0, 0.5*cm))
|
|
231 |
|
|
232 |
w, h = drawWidth, drawHeight = 400, 100
|
|
233 |
protoLabel = self._makeProtoLabel()
|
3326
|
234 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
235 |
protoLabel.width = 4*cm
|
|
236 |
protoLabel.height = 1.5*cm
|
|
237 |
protoLabel.textAnchor = 'end'
|
|
238 |
protoLabel.fontName = 'Helvetica'
|
|
239 |
protoLabel.fontSize = 10
|
|
240 |
drawings = self._makeDrawings(protoLabel)
|
|
241 |
for d in drawings:
|
|
242 |
story.append(d)
|
|
243 |
story.append(Spacer(0, 1*cm))
|
|
244 |
|
|
245 |
story.append(PageBreak())
|
|
246 |
|
|
247 |
# Round 2d
|
|
248 |
story.append(Paragraph("Helvetica 10pt, multi-line, textAnchor='start'", h3))
|
|
249 |
story.append(Spacer(0, 0.5*cm))
|
|
250 |
|
|
251 |
w, h = drawWidth, drawHeight = 400, 100
|
|
252 |
protoLabel = self._makeProtoLabel()
|
3326
|
253 |
protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
|
2963
|
254 |
protoLabel.width = 4*cm
|
|
255 |
protoLabel.height = 1.5*cm
|
|
256 |
protoLabel.textAnchor = 'start'
|
|
257 |
protoLabel.fontName = 'Helvetica'
|
|
258 |
protoLabel.fontSize = 10
|
|
259 |
drawings = self._makeDrawings(protoLabel, text='Hello\nWorld!')
|
|
260 |
for d in drawings:
|
|
261 |
story.append(d)
|
|
262 |
story.append(Spacer(0, 1*cm))
|
|
263 |
|
|
264 |
story.append(PageBreak())
|
|
265 |
|
|
266 |
path = outputfile('test_charts_textlabels.pdf')
|
|
267 |
doc = MyDocTemplate(path)
|
|
268 |
doc.multiBuild(story)
|
|
269 |
|
|
270 |
|
|
271 |
def makeSuite():
|
|
272 |
return makeSuiteForClasses(LabelTestCase)
|
|
273 |
|
|
274 |
|
|
275 |
#noruntests
|
|
276 |
if __name__ == "__main__":
|
|
277 |
unittest.TextTestRunner().run(makeSuite())
|
|
278 |
printLocation()
|