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