reportlab/test/test_platypus_paragraphs.py
author rgbecker
Thu, 18 Nov 2004 14:23:20 +0000
changeset 2410 f505ed647678
parent 2332 2a7ab4405e18
child 2505 0859d48c2558
permissions -rw-r--r--
reportlab: add fix for <para/> and new test
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2268
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
817
8c3a399effda License text changes
rgbecker
parents: 763
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2268
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/test/test_platypus_paragraphs.py
646
82c8a12841b2 Added doc strings.
dinu_gherman
parents: 639
diff changeset
     4
"""Tests for the reportlab.platypus.paragraphs module.
82c8a12841b2 Added doc strings.
dinu_gherman
parents: 639
diff changeset
     5
"""
82c8a12841b2 Added doc strings.
dinu_gherman
parents: 639
diff changeset
     6
2267
e02203404549 Remove unwanted import
rgbecker
parents: 2250
diff changeset
     7
import sys, os
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
     8
from string import split, strip, join, whitespace
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
     9
from operator import truth
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    10
from types import StringType, ListType
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    11
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    12
from reportlab.test import unittest
2268
a51a4b6873c8 Move to having all test output in a temp folder
rgbecker
parents: 2267
diff changeset
    13
from reportlab.test.utils import makeSuiteForClasses, outputfile
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
    14
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    15
from reportlab.pdfbase.pdfmetrics import stringWidth
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    16
from reportlab.platypus.paraparser import ParaParser
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    17
from reportlab.platypus.flowables import Flowable
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    18
from reportlab.lib.colors import Color
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    19
from reportlab.lib.units import cm
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    20
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    21
from reportlab.lib.utils import _className
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    22
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    23
from reportlab.platypus.paragraph import Paragraph
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    24
from reportlab.platypus.frames import Frame
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    25
from reportlab.platypus.doctemplate \
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    26
     import PageTemplate, BaseDocTemplate
753
155af19db555 Fixed imports for non-trailing digit modules.
dinu_gherman
parents: 686
diff changeset
    27
from reportlab.platypus import tableofcontents
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
    28
from reportlab.platypus.tableofcontents import TableOfContents
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    29
from reportlab.platypus.tables import TableStyle, Table
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    30
from reportlab.platypus.paragraph import *
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    31
from reportlab.platypus.paragraph import _getFragWords
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    32
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
    33
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    34
def myMainPageFrame(canvas, doc):
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    35
    "The page frame used for all PDF documents."
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    36
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    37
    canvas.saveState()
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    38
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    39
    canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    40
    canvas.setFont('Times-Roman', 12)
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    41
    pageNumber = canvas.getPageNumber()
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    42
    canvas.drawString(10*cm, cm, str(pageNumber))
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    43
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    44
    canvas.restoreState()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    45
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    46
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    47
class MyDocTemplate(BaseDocTemplate):
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    48
    _invalidInitArgs = ('pageTemplates',)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    49
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    50
    def __init__(self, filename, **kw):
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    51
        frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    52
        self.allowSplitting = 0
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    53
        apply(BaseDocTemplate.__init__, (self, filename), kw)
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    54
        template = PageTemplate('normal', [frame1], myMainPageFrame)
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    55
        self.addPageTemplates(template)
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    56
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    57
2410
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    58
class ParagraphCorners(unittest.TestCase):
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    59
    "some corner cases which should parse"
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    60
    def check(text,bt = getSampleStyleSheet()['BodyText']):
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    61
        try:
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    62
            P = Paragraph(text,st)
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    63
        except:
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    64
            raise AssertionError("'%s' should parse"%text)
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    65
            
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    66
    def test0(self):
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    67
        self.check('<para />')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    68
        self.check('<para/>')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    69
        self.check('\t\t\t\n\n\n<para />')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    70
        self.check('\t\t\t\n\n\n<para/>')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    71
        self.check('<para\t\t\t\t/>')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    72
        self.check('<para></para>')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    73
        self.check('<para>      </para>')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    74
        self.check('\t\t\n\t\t\t   <para>      </para>')
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    75
        
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    76
f505ed647678 reportlab: add fix for <para/> and new test
rgbecker
parents: 2332
diff changeset
    77
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    78
class ParagraphSplitTestCase(unittest.TestCase):
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    79
    "Test multi-page splitting of paragraphs (eyeball-test)."
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
    80
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
    81
    def test0(self):
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    82
        "This makes one long multi-page paragraph."
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    83
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    84
        # Build story.
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    85
        story = []
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    86
        styleSheet = getSampleStyleSheet()
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
    87
        bt = styleSheet['BodyText']
1767
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    88
        text = '''If you imagine that the box of X's tothe left is
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    89
an image, what I want to be able to do is flow a
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    90
series of paragraphs around the image
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    91
so that once the bottom of the image is reached, then text will flow back to the
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    92
left margin. I know that it would be possible to something like this
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    93
using tables, but I can't see how to have a generic solution.
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1767
diff changeset
    94
There are two examples of this in the demonstration section of the reportlab
1767
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    95
site.
1771
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1767
diff changeset
    96
If you look at the "minimal" euro python conference brochure, at the end of the
105572a4222f Whitespace and tab character cleanup
andy_robinson
parents: 1767
diff changeset
    97
timetable section (page 8), there are adverts for "AdSu" and "O'Reilly". I can
1767
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    98
see how the AdSu one might be done generically, but the O'Reilly, unsure...
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
    99
I guess I'm hoping that I've missed something, and that
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   100
it's actually easy to do using platypus.
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   101
'''
0417dea6ed28 Added test of ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   102
        from reportlab.platypus.flowables import ParagraphAndImage, Image
2250
ffa5bc439092 Now works in compact distro
rgbecker
parents: 1771
diff changeset
   103
        from reportlab.lib.utils import _RL_DIR
ffa5bc439092 Now works in compact distro
rgbecker
parents: 1771
diff changeset
   104
        gif = os.path.join(_RL_DIR,'test','pythonpowered.gif')
ffa5bc439092 Now works in compact distro
rgbecker
parents: 1771
diff changeset
   105
        story.append(ParagraphAndImage(Paragraph(text,bt),Image(gif)))
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
   106
        phrase = 'This should be a paragraph spanning at least three pages. '
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
   107
        description = phrase * 250
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
   108
        story.append(Paragraph(description, bt))
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
   109
2268
a51a4b6873c8 Move to having all test output in a temp folder
rgbecker
parents: 2267
diff changeset
   110
        doc = MyDocTemplate(outputfile('test_platypus_paragraphs.pdf'))
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 817
diff changeset
   111
        doc.multiBuild(story)
686
17c6f15386e0 Added a multi-page paragraph test.
dinu_gherman
parents: 646
diff changeset
   112
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   113
class FragmentTestCase(unittest.TestCase):
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   114
    "Test fragmentation of paragraphs."
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1667
diff changeset
   115
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   116
    def test0(self):
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   117
        "Test empty paragraph."
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   118
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   119
        styleSheet = getSampleStyleSheet()
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   120
        B = styleSheet['BodyText']
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   121
        text = ''
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   122
        P = Paragraph(text, B)
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   123
        frags = map(lambda f:f.text, P.frags)
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   124
        assert frags == []
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   125
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   126
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   127
    def test1(self):
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   128
        "Test simple paragraph."
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   129
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   130
        styleSheet = getSampleStyleSheet()
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   131
        B = styleSheet['BodyText']
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   132
        text = "X<font name=Courier>Y</font>Z"
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   133
        P = Paragraph(text, B)
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   134
        frags = map(lambda f:f.text, P.frags)
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   135
        assert frags == ['X', 'Y', 'Z']
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   136
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   137
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   138
#noruntests
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   139
def makeSuite():
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   140
    return makeSuiteForClasses(FragmentTestCase, ParagraphSplitTestCase)
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   141
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   142
763
2755ba5be930 Added utils\runtests.py inhibitor comment
rgbecker
parents: 753
diff changeset
   143
#noruntests
639
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   144
if __name__ == "__main__":
ca024e564629 Initial checkin.
dinu_gherman
parents:
diff changeset
   145
    unittest.TextTestRunner().run(makeSuite())
1667
dfe2f821f4e2 Slightly refactored test suite to do the same thing with less code.
dinu_gherman
parents: 1440
diff changeset
   146