"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.23/tests/test_charts_textlabels.py" (31 May 2019, 9464 Bytes) of package /linux/privat/reportlab-3.5.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_charts_textlabels.py": 3.5.21_vs_3.5.23.

    1 #Copyright ReportLab Europe Ltd. 2000-2017
    2 #see license.txt for license details
    3 """
    4 Tests for the text Label class.
    5 """
    6 from reportlab.lib.testutils import setOutDir,setOutDir,makeSuiteForClasses, outputfile, printLocation
    7 setOutDir(__name__)
    8 
    9 import os, sys, copy
   10 from os.path import join, basename, splitext
   11 import unittest
   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
   47         BaseDocTemplate.__init__(self, filename, **kw)
   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'):
   89             boxAnchors = boxAnchors.split(' ')
   90 
   91             # Create drawing.
   92             d = Drawing(w, h)
   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))
   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()
  139         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  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()
  156         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  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         try:
  169             from reportlab.pdfbase.pdfmetrics import registerFont
  170             from reportlab.pdfbase.ttfonts import TTFont
  171             fontName = 'Vera'
  172             registerFont(TTFont(fontName, "Vera.ttf"))
  173         except:
  174             fontName = 'Helvetica'
  175         story.append(Paragraph('%s 18pt, multi-line' % fontName, h3))
  176         story.append(Spacer(0, 0.5*cm))
  177 
  178         w, h = drawWidth, drawHeight = 400, 100
  179         protoLabel = self._makeProtoLabel()
  180         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  181         protoLabel.textAnchor = 'start'
  182         protoLabel.fontName = fontName
  183         protoLabel.fontSize = 18
  184         drawings = self._makeDrawings(protoLabel, text=u'Hello\nW\xf6rld!')
  185         for d in drawings:
  186             story.append(d)
  187             story.append(Spacer(0, 1*cm))
  188 
  189         story.append(PageBreak())
  190 
  191         story.append(Paragraph('Testing text (and box) anchors', h2))
  192         story.append(Paragraph("""This should display labels as before,
  193 but now with a fixes size and showing some effect of setting the
  194 textAnchor attribute.""", bt))
  195         story.append(Spacer(0, 0.5*cm))
  196 
  197         # Round 2a
  198         story.append(Paragraph("Helvetica 10pt, textAnchor='start'", h3))
  199         story.append(Spacer(0, 0.5*cm))
  200 
  201         w, h = drawWidth, drawHeight = 400, 100
  202         protoLabel = self._makeProtoLabel()
  203         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  204         protoLabel.width = 4*cm
  205         protoLabel.height = 1.5*cm
  206         protoLabel.textAnchor = 'start'
  207         protoLabel.fontName = 'Helvetica'
  208         protoLabel.fontSize = 10
  209         drawings = self._makeDrawings(protoLabel)
  210         for d in drawings:
  211             story.append(d)
  212             story.append(Spacer(0, 1*cm))
  213 
  214         story.append(PageBreak())
  215 
  216         # Round 2b
  217         story.append(Paragraph("Helvetica 10pt, textAnchor='middle'", h3))
  218         story.append(Spacer(0, 0.5*cm))
  219 
  220         w, h = drawWidth, drawHeight = 400, 100
  221         protoLabel = self._makeProtoLabel()
  222         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  223         protoLabel.width = 4*cm
  224         protoLabel.height = 1.5*cm
  225         protoLabel.textAnchor = 'middle'
  226         protoLabel.fontName = 'Helvetica'
  227         protoLabel.fontSize = 10
  228         drawings = self._makeDrawings(protoLabel)
  229         for d in drawings:
  230             story.append(d)
  231             story.append(Spacer(0, 1*cm))
  232 
  233         story.append(PageBreak())
  234 
  235         # Round 2c
  236         story.append(Paragraph("Helvetica 10pt, textAnchor='end'", h3))
  237         story.append(Spacer(0, 0.5*cm))
  238 
  239         w, h = drawWidth, drawHeight = 400, 100
  240         protoLabel = self._makeProtoLabel()
  241         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  242         protoLabel.width = 4*cm
  243         protoLabel.height = 1.5*cm
  244         protoLabel.textAnchor = 'end'
  245         protoLabel.fontName = 'Helvetica'
  246         protoLabel.fontSize = 10
  247         drawings = self._makeDrawings(protoLabel)
  248         for d in drawings:
  249             story.append(d)
  250             story.append(Spacer(0, 1*cm))
  251 
  252         story.append(PageBreak())
  253 
  254         # Round 2d
  255         story.append(Paragraph("Helvetica 10pt, multi-line, textAnchor='start'", h3))
  256         story.append(Spacer(0, 0.5*cm))
  257 
  258         w, h = drawWidth, drawHeight = 400, 100
  259         protoLabel = self._makeProtoLabel()
  260         protoLabel.setOrigin(drawWidth*0.5, drawHeight*0.5)
  261         protoLabel.width = 4*cm
  262         protoLabel.height = 1.5*cm
  263         protoLabel.textAnchor = 'start'
  264         protoLabel.fontName = 'Helvetica'
  265         protoLabel.fontSize = 10
  266         drawings = self._makeDrawings(protoLabel, text='Hello\nWorld!')
  267         for d in drawings:
  268             story.append(d)
  269             story.append(Spacer(0, 1*cm))
  270 
  271         story.append(PageBreak())
  272 
  273         path = outputfile('test_charts_textlabels.pdf')
  274         doc = MyDocTemplate(path)
  275         doc.multiBuild(story)
  276 
  277 
  278 def makeSuite():
  279     return makeSuiteForClasses(LabelTestCase)
  280 
  281 
  282 #noruntests
  283 if __name__ == "__main__":
  284     unittest.TextTestRunner().run(makeSuite())
  285     printLocation()