reportlab/test/test_platypus_general.py
author rgbecker
Thu, 21 Aug 2008 16:50:11 +0000
changeset 2955 cc16265295fb
parent 2885 b8a467b98da9
child 2956 5c17941635bd
permissions -rw-r--r--
reportlab: start of doc programming
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2312
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2312
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/test/test_platypus_general.py
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2312
diff changeset
     4
__version__=''' $Id$ '''
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
     5
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
     6
#tests and documents Page Layout API
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
     7
__doc__="""This is not obvious so here's a brief explanation.  This module is both
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
     8
the test script and user guide for layout.  Each page has two frames on it:
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
     9
one for commentary, and one for demonstration objects which may be drawn in
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    10
various esoteric ways.  The two functions getCommentary() and getExamples()
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    11
return the 'story' for each.  The run() function gets the stories, then
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    12
builds a special "document model" in which the frames are added to each page
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    13
and drawn into.
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    14
"""
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
    15
1919
8b813a4f8df4 Make os & cwd agnostic
rgbecker
parents: 1732
diff changeset
    16
import string, copy, sys, os
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
    17
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
    18
from reportlab.test import unittest
2505
0859d48c2558 reportlab/test: fix so individual tests print output folder location
rgbecker
parents: 2383
diff changeset
    19
from reportlab.test.utils import makeSuiteForClasses, outputfile, printLocation
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
    20
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    21
from reportlab.pdfgen import canvas
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    22
from reportlab import platypus
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    23
from reportlab.platypus import BaseDocTemplate, PageTemplate, Flowable, FrameBreak
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    24
from reportlab.platypus import Paragraph, Preformatted
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    25
from reportlab.lib.units import inch, cm
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    26
from reportlab.lib.styles import PropertySet, getSampleStyleSheet, ParagraphStyle
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    27
from reportlab.lib import colors
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    28
from reportlab.rl_config import defaultPageSize
2813
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
    29
from reportlab.lib.utils import haveImages, _RL_DIR, rl_isfile, open_for_read, fileName2Utf8
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    30
if haveImages:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    31
    _GIF = os.path.join(_RL_DIR,'test','pythonpowered.gif')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    32
    if not rl_isfile(_GIF): _GIF = None
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    33
else:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    34
    _GIF = None
2813
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
    35
if _GIF: _GIFUTF8=fileName2Utf8(_GIF)
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
    36
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    37
_JPG = os.path.join(_RL_DIR,'docs','images','lj8100.jpg')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    38
if not rl_isfile(_JPG): _JPG = None
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    39
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    40
def getFurl(fn):
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    41
    furl = fn.replace(os.sep,'/')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    42
    if sys.platform=='win32' and furl[1]==':': furl = furl[0]+'|'+furl[2:]
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    43
    if furl[0]!='/': furl = '/'+furl
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
    44
    return 'file://'+furl
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    45
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    46
PAGE_HEIGHT = defaultPageSize[1]
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    47
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    48
#################################################################
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    49
#
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    50
#  first some drawing utilities
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    51
#
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    52
#
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    53
################################################################
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    54
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    55
BASEFONT = ('Times-Roman', 10)
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    56
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    57
def framePage(canvas,doc):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    58
    #canvas.drawImage("snkanim.gif", 36, 36)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    59
    canvas.saveState()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    60
    canvas.setStrokeColorRGB(1,0,0)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    61
    canvas.setLineWidth(5)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    62
    canvas.line(66,72,66,PAGE_HEIGHT-72)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    63
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    64
    canvas.setFont('Times-Italic',12)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    65
    canvas.drawRightString(523, PAGE_HEIGHT - 56, "Platypus User Guide and Test Script")
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    66
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    67
    canvas.setFont('Times-Roman',12)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    68
    canvas.drawString(4 * inch, 0.75 * inch,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    69
                        "Page %d" % canvas.getPageNumber())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    70
    canvas.restoreState()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    71
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    72
def getParagraphs(textBlock):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    73
    """Within the script, it is useful to whack out a page in triple
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    74
    quotes containing separate paragraphs. This breaks one into its
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    75
    constituent paragraphs, using blank lines as the delimiter."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    76
    lines = string.split(textBlock, '\n')
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    77
    paras = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    78
    currentPara = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    79
    for line in lines:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    80
        if len(string.strip(line)) == 0:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    81
            #blank, add it
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    82
            if currentPara <> []:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    83
                paras.append(string.join(currentPara, '\n'))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    84
                currentPara = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    85
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    86
            currentPara.append(line)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    87
    #...and the last one
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    88
    if currentPara <> []:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    89
        paras.append(string.join(currentPara, '\n'))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    90
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    91
    return paras
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    92
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    93
def getCommentary():
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    94
    """Returns the story for the commentary - all the paragraphs."""
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
    95
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    96
    styleSheet = getSampleStyleSheet()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    97
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    98
    story = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
    99
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   100
        PLATYPUS User Guide and Test Script
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   101
        """, styleSheet['Heading1']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   102
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   103
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   104
    spam = """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   105
    Welcome to PLATYPUS!
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   106
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   107
    Platypus stands for "Page Layout and Typography Using Scripts".  It is a high
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   108
    level page layout library which lets you programmatically create complex
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   109
    documents with a minimum of effort.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   110
1732
bdba419ede61 Fixed makeSuite non-function-call in script handler and added a token
andy_robinson
parents: 1683
diff changeset
   111
    This document is both the user guide &amp; the output of the test script.
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   112
    In other words, a script used platypus to create the document you are now
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   113
    reading, and the fact that you are reading it proves that it works.  Or
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   114
    rather, that it worked for this script anyway.  It is a first release!
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   115
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   116
    Platypus is built 'on top of' PDFgen, the Python library for creating PDF
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   117
    documents.  To learn about PDFgen, read the document testpdfgen.pdf.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   118
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   119
    """
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   120
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   121
    for text in getParagraphs(spam):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   122
        story.append(Paragraph(text, styleSheet['BodyText']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   123
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   124
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   125
        What concepts does PLATYPUS deal with?
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   126
        """, styleSheet['Heading2']))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   127
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   128
        The central concepts in PLATYPUS are Flowable Objects, Frames, Flow
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   129
        Management, Styles and Style Sheets, Paragraphs and Tables.  This is
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   130
        best explained in contrast to PDFgen, the layer underneath PLATYPUS.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   131
        PDFgen is a graphics library, and has primitive commans to draw lines
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   132
        and strings.  There is nothing in it to manage the flow of text down
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   133
        the page.  PLATYPUS works at the conceptual level fo a desktop publishing
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   134
        package; you can write programs which deal intelligently with graphic
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   135
        objects and fit them onto the page.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   136
        """, styleSheet['BodyText']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   137
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   138
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   139
        How is this document organized?
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   140
        """, styleSheet['Heading2']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   141
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   142
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   143
        Since this is a test script, we'll just note how it is organized.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   144
        the top of each page contains commentary.  The bottom half contains
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   145
        example drawings and graphic elements to whicht he commentary will
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   146
        relate.  Down below, you can see the outline of a text frame, and
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   147
        various bits and pieces within it.  We'll explain how they work
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   148
        on the next page.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   149
        """, styleSheet['BodyText']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   150
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   151
    story.append(FrameBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   152
    #######################################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   153
    #     Commentary Page 2
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   154
    #######################################################################
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   155
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   156
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   157
        Flowable Objects
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   158
        """, styleSheet['Heading2']))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   159
    spam = """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   160
        The first and most fundamental concept is that of a 'Flowable Object'.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   161
        In PDFgen, you draw stuff by calling methods of the canvas to set up
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   162
        the colors, fonts and line styles, and draw the graphics primitives.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   163
        If you set the pen color to blue, everything you draw after will be
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   164
        blue until you change it again.  And you have to handle all of the X-Y
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   165
        coordinates yourself.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   166
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   167
        A 'Flowable object' is exactly what it says.  It knows how to draw itself
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   168
        on the canvas, and the way it does so is totally independent of what
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   169
        you drew before or after.  Furthermore, it draws itself at the location
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   170
        on the page you specify.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   171
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   172
        The most fundamental Flowable Objects in most documents are likely to be
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   173
        paragraphs, tables, diagrams/charts and images - but there is no
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   174
        restriction.  You can write your own easily, and I hope that people
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   175
        will start to contribute them.  PINGO users - we provide a "PINGO flowable" object to let
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   176
        you insert platform-independent graphics into the flow of a document.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   177
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   178
        When you write a flowable object, you inherit from Flowable and
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   179
        must implement two methods.  object.wrap(availWidth, availHeight) will be called by other parts of
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   180
        the system, and tells you how much space you have.  You should return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   181
        how much space you are going to use.  For a fixed-size object, this
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   182
        is trivial, but it is critical - PLATYPUS needs to figure out if things
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   183
        will fit on the page before drawing them.  For other objects such as paragraphs,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   184
        the height is obviously determined by the available width.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   185
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   186
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   187
        The second method is object.draw().  Here, you do whatever you want.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   188
        The Flowable base class sets things up so that you have an origin of
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   189
        (0,0) for your drawing, and everything will fit nicely if you got the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   190
        height and width right.  It also saves and restores the graphics state
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   191
        around your calls, so you don;t have to reset all the properties you
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   192
        changed.
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   193
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   194
        Programs which actually draw a Flowable don't
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   195
        call draw() this directly - they call object.drawOn(canvas, x, y).
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   196
        So you can write code in your own coordinate system, and things
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   197
        can be drawn anywhere on the page (possibly even scaled or rotated).
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   198
        """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   199
    for text in getParagraphs(spam):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   200
        story.append(Paragraph(text, styleSheet['BodyText']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   201
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   202
    story.append(FrameBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   203
    #######################################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   204
    #     Commentary Page 3
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   205
    #######################################################################
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   206
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   207
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   208
        Available Flowable Objects
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   209
        """, styleSheet['Heading2']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   210
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   211
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   212
        Platypus comes with a basic set of flowable objects.  Here we list their
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   213
        class names and tell you what they do:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   214
        """, styleSheet['BodyText']))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   215
    #we can use the bullet feature to do a definition list
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   216
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   217
        <para color=green bcolor=red bg=pink>This is a contrived object to give an example of a Flowable -
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   218
        just a fixed-size box with an X through it and a centred string.</para>""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   219
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   220
            bulletText='XBox  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   221
            ))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   222
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   223
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   224
        This is the basic unit of a document.  Paragraphs can be finely
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   225
        tuned and offer a host of properties through their associated
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   226
        ParagraphStyle.""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   227
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   228
            bulletText='Paragraph  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   229
            ))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   230
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   231
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   232
        This is used for printing code and other preformatted text.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   233
        There is no wrapping, and line breaks are taken where they occur.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   234
        Many paragraph style properties do not apply.  You may supply
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   235
        an optional 'dedent' parameter to trim a number of characters
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   236
        off the front of each line.""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   237
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   238
            bulletText='Preformatted  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   239
            ))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   240
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   241
        This is a straight wrapper around an external image file.  By default
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   242
        the image will be drawn at a scale of one pixel equals one point, and
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   243
        centred in the frame.  You may supply an optional width and height.""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   244
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   245
            bulletText='Image  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   246
            ))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   247
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   248
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   249
        This is a table drawing class; it is intended to be simpler
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   250
        than a full HTML table model yet be able to draw attractive output,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   251
        and behave intelligently when the numbers of rows and columns vary.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   252
        Still need to add the cell properties (shading, alignment, font etc.)""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   253
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   254
            bulletText='Table  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   255
            ))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   256
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   257
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   258
        This is a 'null object' which merely takes up space on the page.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   259
        Use it when you want some extra padding betweene elements.""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   260
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   261
            bulletText='Spacer '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   262
            ))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   263
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   264
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   265
        A FrameBreak causes the document to call its handle_frameEnd method.""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   266
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   267
            bulletText='FrameBreak  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   268
            ))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   269
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   270
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   271
        This is in progress, but a macro is basically a chunk of Python code to
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   272
        be evaluated when it is drawn.  It could do lots of neat things.""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   273
            styleSheet['Definition'],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   274
            bulletText='Macro  '  #hack - spot the extra space after
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   275
            ))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   276
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   277
    story.append(FrameBreak())
1073
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   278
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   279
    story.append(Paragraph(
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   280
                "The next example uses a custom font",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   281
                styleSheet['Italic']))
2383
7191131cb323 test_platypus_general.py: remove another 2.1 scope problem
rgbecker
parents: 2382
diff changeset
   282
    def code(txt,story=story,styleSheet=styleSheet):
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   283
        story.append(Preformatted(txt,styleSheet['Code']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   284
    code('''import reportlab.rl_config
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   285
    reportlab.rl_config.warnOnMissingFontGlyphs = 0
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   286
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   287
    from reportlab.pdfbase import pdfmetrics
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   288
    fontDir = os.path.join(os.path.dirname(reportlab.__file__),'fonts')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   289
    face = pdfmetrics.EmbeddedType1Face(os.path.join(fontDir,'LeERC___.AFM'),
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   290
            os.path.join(fontDir,'LeERC___.PFB'))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   291
    faceName = face.name  # should be 'LettErrorRobot-Chrome'
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   292
    pdfmetrics.registerTypeFace(face)
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   293
    font = pdfmetrics.Font(faceName, faceName, 'WinAnsiEncoding')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   294
    pdfmetrics.registerFont(font)
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   295
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   296
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   297
    # put it inside a paragraph.
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   298
    story.append(Paragraph(
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   299
        """This is an ordinary paragraph, which happens to contain
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   300
        text in an embedded font:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   301
        <font name="LettErrorRobot-Chrome">LettErrorRobot-Chrome</font>.
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   302
        Now for the real challenge...""", styleSheet['Normal']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   303
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   304
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   305
    styRobot = ParagraphStyle('Robot', styleSheet['Normal'])
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   306
    styRobot.fontSize = 16
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   307
    styRobot.leading = 20
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   308
    styRobot.fontName = 'LettErrorRobot-Chrome'
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   309
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   310
    story.append(Paragraph(
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   311
                "This whole paragraph is 16-point Letterror-Robot-Chrome.",
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   312
                styRobot))''')
1073
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   313
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   314
    story.append(FrameBreak())
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   315
    if _GIF:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   316
        story.append(Paragraph("""We can use images via the file name""", styleSheet['BodyText']))
2813
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
   317
        code('''    story.append(platypus.Image('%s'))'''%_GIFUTF8)
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
   318
        code('''    story.append(platypus.Image(fileName2Utf8('%s')))''' % _GIFUTF8)
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   319
        story.append(Paragraph("""They can also be used with a file URI or from an open python file!""", styleSheet['BodyText']))
2813
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
   320
        code('''    story.append(platypus.Image('%s'))'''% getFurl(_GIFUTF8))
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
   321
        code('''    story.append(platypus.Image(open_for_read('%s','b')))''' % _GIFUTF8)
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   322
        story.append(FrameBreak())
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   323
        story.append(Paragraph("""Images can even be obtained from the internet.""", styleSheet['BodyText']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   324
        code('''    img = platypus.Image('http://www.reportlab.com/rsrc/encryption.gif')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   325
    story.append(img)''')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   326
        story.append(FrameBreak())
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   327
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   328
    if _JPG:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   329
        story.append(Paragraph("""JPEGs are a native PDF image format. They should be available even if PIL cannot be used.""", styleSheet['BodyText']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   330
        story.append(FrameBreak())
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   331
    return story
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   332
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   333
def getExamples():
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   334
    """Returns all the example flowable objects"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   335
    styleSheet = getSampleStyleSheet()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   336
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   337
    story = []
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   338
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   339
    #make a style with indents and spacing
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   340
    sty = ParagraphStyle('obvious', None)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   341
    sty.leftIndent = 18
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   342
    sty.rightIndent = 18
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   343
    sty.firstLineIndent = 18
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   344
    sty.spaceBefore = 6
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   345
    sty.spaceAfter = 6
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   346
    story.append(Paragraph("""Now for some demo stuff - we need some on this page,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   347
        even before we explain the concepts fully""", styleSheet['BodyText']))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   348
    p = Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   349
        Platypus is all about fitting objects into frames on the page.  You
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   350
        are looking at a fairly simple Platypus paragraph in Debug mode.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   351
        It has some gridlines drawn around it to show the left and right indents,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   352
        and the space before and after, all of which are attributes set in
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   353
        the style sheet.  To be specific, this paragraph has left and
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   354
        right indents of 18 points, a first line indent of 36 points,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   355
        and 6 points of space before and after itself.  A paragraph
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   356
        object fills the width of the enclosing frame, as you would expect.""", sty)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   357
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   358
    p.debug = 1   #show me the borders
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   359
    story.append(p)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   360
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   361
    story.append(Paragraph("""Same but with justification 1.5 extra leading and green text.""", styleSheet['BodyText']))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   362
    p = Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   363
        <para align=justify leading=+1.5 fg=green><font color=red>Platypus</font> is all about fitting objects into frames on the page.  You
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   364
        are looking at a fairly simple Platypus paragraph in Debug mode.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   365
        It has some gridlines drawn around it to show the left and right indents,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   366
        and the space before and after, all of which are attributes set in
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   367
        the style sheet.  To be specific, this paragraph has left and
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   368
        right indents of 18 points, a first line indent of 36 points,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   369
        and 6 points of space before and after itself.  A paragraph
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   370
        object fills the width of the enclosing frame, as you would expect.</para>""", sty)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   371
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   372
    p.debug = 1   #show me the borders
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   373
    story.append(p)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   374
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   375
    story.append(platypus.XBox(4*inch, 0.75*inch,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   376
            'This is a box with a fixed size'))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   377
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   378
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   379
        All of this is being drawn within a text frame which was defined
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   380
        on the page.  This frame is in 'debug' mode so you can see the border,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   381
        and also see the margins which it reserves.  A frame does not have
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   382
        to have margins, but they have been set to 6 points each to create
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   383
        a little space around the contents.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   384
        """, styleSheet['BodyText']))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   385
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   386
    story.append(FrameBreak())
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   387
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   388
    #######################################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   389
    #     Examples Page 2
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   390
    #######################################################################
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   391
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   392
    story.append(Paragraph("""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   393
        Here's the base class for Flowable...
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   394
        """, styleSheet['Italic']))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   395
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   396
    code = '''class Flowable:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   397
        """Abstract base class for things to be drawn.  Key concepts:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   398
    1. It knows its size
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   399
    2. It draws in its own coordinate system (this requires the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   400
        base API to provide a translate() function.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   401
        """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   402
    def __init__(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   403
        self.width = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   404
        self.height = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   405
        self.wrapped = 0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   406
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   407
    def drawOn(self, canvas, x, y):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   408
        "Tell it to draw itself on the canvas.  Do not override"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   409
        self.canv = canvas
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   410
        self.canv.saveState()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   411
        self.canv.translate(x, y)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   412
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   413
        self.draw()   #this is the bit you overload
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   414
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   415
        self.canv.restoreState()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   416
        del self.canv
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   417
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   418
    def wrap(self, availWidth, availHeight):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   419
        """This will be called by the enclosing frame before objects
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   420
        are asked their size, drawn or whatever.  It returns the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   421
        size actually used."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   422
        return (self.width, self.height)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   423
    '''
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   424
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   425
    story.append(Preformatted(code, styleSheet['Code'], dedent=4))
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   426
    story.append(FrameBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   427
    #######################################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   428
    #     Examples Page 3
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   429
    #######################################################################
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   430
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   431
    story.append(Paragraph(
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   432
                "Here are some examples of the remaining objects above.",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   433
                styleSheet['Italic']))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   434
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   435
    story.append(Paragraph("This is a bullet point", styleSheet['Bullet'], bulletText='O'))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   436
    story.append(Paragraph("Another bullet point", styleSheet['Bullet'], bulletText='O'))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   437
2236
75e7c6be6159 Add new Image flowable exemplars
rgbecker
parents: 2045
diff changeset
   438
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   439
    story.append(Paragraph("""Here is a Table, which takes all kinds of formatting options...""",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   440
                styleSheet['Italic']))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   441
    story.append(platypus.Spacer(0, 12))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   442
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   443
    g = platypus.Table(
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   444
            (('','North','South','East','West'),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   445
             ('Quarter 1',100,200,300,400),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   446
             ('Quarter 2',100,200,300,400),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   447
             ('Total',200,400,600,800)),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   448
            (72,36,36,36,36),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   449
            (24, 16,16,18)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   450
            )
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   451
    style = platypus.TableStyle([('ALIGN', (1,1), (-1,-1), 'RIGHT'),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   452
                               ('ALIGN', (0,0), (-1,0), 'CENTRE'),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   453
                               ('GRID', (0,0), (-1,-1), 0.25, colors.black),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   454
                               ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   455
                               ('LINEBELOW',(1,-1), (-1, -1), 2, (0.5, 0.5, 0.5)),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   456
                               ('TEXTCOLOR', (0,1), (0,-1), colors.black),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   457
                               ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   458
                               ])
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   459
    g.setStyle(style)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   460
    story.append(g)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   461
    story.append(FrameBreak())
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   462
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   463
    #######################################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   464
    #     Examples Page 4 - custom fonts
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   465
    #######################################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   466
    # custom font with LettError-Robot font
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   467
    import reportlab.rl_config
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   468
    reportlab.rl_config.warnOnMissingFontGlyphs = 0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   469
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   470
    from reportlab.pdfbase import pdfmetrics
1919
8b813a4f8df4 Make os & cwd agnostic
rgbecker
parents: 1732
diff changeset
   471
    fontDir = os.path.join(os.path.dirname(reportlab.__file__),'fonts')
8b813a4f8df4 Make os & cwd agnostic
rgbecker
parents: 1732
diff changeset
   472
    face = pdfmetrics.EmbeddedType1Face(os.path.join(fontDir,'LeERC___.AFM'),os.path.join(fontDir,'LeERC___.PFB'))
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   473
    faceName = face.name  # should be 'LettErrorRobot-Chrome'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   474
    pdfmetrics.registerTypeFace(face)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   475
    font = pdfmetrics.Font(faceName, faceName, 'WinAnsiEncoding')
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   476
    pdfmetrics.registerFont(font)
1073
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   477
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   478
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   479
    # put it inside a paragraph.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   480
    story.append(Paragraph(
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   481
        """This is an ordinary paragraph, which happens to contain
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   482
        text in an embedded font:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   483
        <font name="LettErrorRobot-Chrome">LettErrorRobot-Chrome</font>.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   484
        Now for the real challenge...""", styleSheet['Normal']))
1073
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   485
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   486
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   487
    styRobot = ParagraphStyle('Robot', styleSheet['Normal'])
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   488
    styRobot.fontSize = 16
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   489
    styRobot.leading = 20
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   490
    styRobot.fontName = 'LettErrorRobot-Chrome'
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   491
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   492
    story.append(Paragraph(
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   493
                "This whole paragraph is 16-point Letterror-Robot-Chrome.",
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   494
                styRobot))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   495
    story.append(FrameBreak())
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   496
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   497
    if _GIF:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   498
        story.append(Paragraph("Here is an Image flowable obtained from a string filename.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   499
        story.append(platypus.Image(_GIF))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   500
        story.append(Paragraph( "Here is an Image flowable obtained from a utf8 filename.", styleSheet['Italic']))
2813
2b9091df4470 reportlab: add fsEncodings to rl_config, add fileName2Utf8
rgbecker
parents: 2505
diff changeset
   501
        #story.append(platypus.Image(fileName2Utf8(_GIF)))
2373
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   502
        story.append(Paragraph("Here is an Image flowable obtained from a string file url.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   503
        story.append(platypus.Image(getFurl(_GIF)))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   504
        story.append(Paragraph("Here is an Image flowable obtained from an open file.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   505
        story.append(platypus.Image(open_for_read(_GIF,'b')))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   506
        story.append(FrameBreak())
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   507
        try:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   508
            img = platypus.Image('http://www.reportlab.com/rsrc/encryption.gif')
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   509
            story.append(Paragraph("Here is an Image flowable obtained from a string http url.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   510
            story.append(img)
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   511
        except:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   512
            story.append(Paragraph("The image could not be obtained from a string http url.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   513
        story.append(FrameBreak())
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   514
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   515
    if _JPG:
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   516
        img = platypus.Image(_JPG)
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   517
        story.append(Paragraph("Here is an JPEG Image flowable obtained from a filename.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   518
        story.append(img)
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   519
        story.append(Paragraph("Here is an JPEG Image flowable obtained from an open file.",styleSheet['Italic']))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   520
        img = platypus.Image(open_for_read(_JPG,'b'))
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   521
        story.append(img)
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   522
        story.append(FrameBreak())
c36300a7902c test_platypus_general.py: fix so all output seen
rgbecker
parents: 2332
diff changeset
   523
1073
56d742af01e1 Hack to let custom fonts show in paragraphs
andy_robinson
parents: 859
diff changeset
   524
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   525
    return story
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   526
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   527
class AndyTemplate(BaseDocTemplate):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   528
    _invalidInitArgs = ('pageTemplates',)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   529
    def __init__(self, filename, **kw):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   530
        frame1 = platypus.Frame(inch, 5.6*inch, 6*inch, 5.2*inch,id='F1')
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   531
        frame2 = platypus.Frame(inch, inch, 6*inch, 4.5*inch, showBoundary=1,id='F2')
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   532
        self.allowSplitting = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   533
        apply(BaseDocTemplate.__init__,(self,filename),kw)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   534
        self.addPageTemplates(PageTemplate('normal',[frame1,frame2],framePage))
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   535
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   536
    def fillFrame(self,flowables):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   537
        f = self.frame
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   538
        while len(flowables)>0 and f is self.frame:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   539
            self.handle_flowable(flowables)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   540
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   541
    def build(self, flowables1, flowables2):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   542
        assert filter(lambda x: not isinstance(x,Flowable), flowables1)==[], "flowables1 argument error"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   543
        assert filter(lambda x: not isinstance(x,Flowable), flowables2)==[], "flowables2 argument error"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   544
        self._startBuild()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   545
        while (len(flowables1) > 0 or len(flowables1) > 0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   546
            self.clean_hanging()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   547
            self.fillFrame(flowables1)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   548
            self.fillFrame(flowables2)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   549
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   550
        self._endBuild()
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   551
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   552
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   553
def showProgress(pageNo):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   554
    print 'CALLBACK SAYS: page %d' % pageNo
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   555
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   556
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   557
def run():
2268
a51a4b6873c8 Move to having all test output in a temp folder
rgbecker
parents: 2243
diff changeset
   558
    doc = AndyTemplate(outputfile('test_platypus_general.pdf'))
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   559
    #doc.setPageCallBack(showProgress)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   560
    commentary = getCommentary()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   561
    examples = getExamples()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1293
diff changeset
   562
    doc.build(commentary,examples)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   563
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   564
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   565
class PlatypusTestCase(unittest.TestCase):
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   566
    "Make documents with lots of Platypus features"
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   567
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   568
    def test0(self):
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   569
        "Make a platypus document"
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   570
        run()
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   571
2885
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   572
    def test1(self):
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   573
        #test from Wietse Jacobs
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   574
        from reportlab.lib.styles import ParagraphStyle
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   575
        from reportlab.graphics.shapes import Drawing, Rect
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   576
        from reportlab.platypus import SimpleDocTemplate
2955
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   577
        from reportlab.platypus.flowables import DocAssign, DocExec, DocPara
2885
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   578
        normal = ParagraphStyle(name='Normal', fontName='Helvetica', fontSize=8.5, leading=11)
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   579
        header = ParagraphStyle(name='Heading1', parent=normal, fontSize=14, leading=19,
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   580
                    spaceAfter=6, keepWithNext=1)
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   581
        d = Drawing(400, 200)
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   582
        d.add(Rect(50, 50, 300, 100))
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   583
2955
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   584
        story = [Paragraph("The section header", header), d,
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   585
                DocAssign('currentFrame','doc.frame.id'),
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   586
                DocAssign('currentPageTemplate','doc.pageTemplate.id'),
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   587
                DocAssign('aW','availableWidth'),
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   588
                DocAssign('aH','availableHeight'),
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   589
                DocAssign('aWH','availableWidth,availableHeight'),
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   590
                DocPara('repr(doc._nameSpace)'),
cc16265295fb reportlab: start of doc programming
rgbecker
parents: 2885
diff changeset
   591
                ]
2885
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   592
        doc = SimpleDocTemplate('test_drawing_keepwithnext.pdf')
b8a467b98da9 reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents: 2813
diff changeset
   593
        doc.build(story)
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   594
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   595
def makeSuite():
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1657
diff changeset
   596
    return makeSuiteForClasses(PlatypusTestCase)
857
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   597
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   598
#noruntests
0e2245922315 Added tests formerly under platypus/test
andy_robinson
parents:
diff changeset
   599
if __name__ == "__main__":
1657
ff3fd6ad905c Added -debug option
rgbecker
parents: 1631
diff changeset
   600
    if '-debug' in sys.argv:
ff3fd6ad905c Added -debug option
rgbecker
parents: 1631
diff changeset
   601
        run()
ff3fd6ad905c Added -debug option
rgbecker
parents: 1631
diff changeset
   602
    else:
1919
8b813a4f8df4 Make os & cwd agnostic
rgbecker
parents: 1732
diff changeset
   603
        unittest.TextTestRunner().run(makeSuite())
2505
0859d48c2558 reportlab/test: fix so individual tests print output folder location
rgbecker
parents: 2383
diff changeset
   604
        printLocation()