185 lines
7.4 KiB
Python
185 lines
7.4 KiB
Python
#Copyright ReportLab Europe Ltd. 2000-2004
|
|
#see license.txt for license details
|
|
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/test/test_platypus_paragraphs.py
|
|
"""Tests for the reportlab.platypus.paragraphs module.
|
|
"""
|
|
|
|
import sys, os
|
|
from string import split, strip, join, whitespace
|
|
from operator import truth
|
|
from types import StringType, ListType
|
|
|
|
from reportlab.test import unittest
|
|
from reportlab.test.utils import makeSuiteForClasses, outputfile, printLocation
|
|
|
|
from reportlab.pdfbase.pdfmetrics import stringWidth
|
|
from reportlab.platypus.paraparser import ParaParser
|
|
from reportlab.platypus.flowables import Flowable
|
|
from reportlab.lib.colors import Color
|
|
from reportlab.lib.units import cm
|
|
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
|
|
from reportlab.lib.utils import _className
|
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
from reportlab.platypus.paragraph import Paragraph
|
|
from reportlab.platypus.frames import Frame
|
|
from reportlab.platypus.doctemplate \
|
|
import PageTemplate, BaseDocTemplate
|
|
from reportlab.platypus import tableofcontents
|
|
from reportlab.platypus.tableofcontents import TableOfContents
|
|
from reportlab.platypus.tables import TableStyle, Table
|
|
from reportlab.platypus.paragraph import *
|
|
from reportlab.platypus.paragraph import _getFragWords
|
|
|
|
|
|
def myMainPageFrame(canvas, doc):
|
|
"The page frame used for all PDF documents."
|
|
|
|
canvas.saveState()
|
|
|
|
canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
|
|
canvas.setFont('Times-Roman', 12)
|
|
pageNumber = canvas.getPageNumber()
|
|
canvas.drawString(10*cm, cm, str(pageNumber))
|
|
|
|
canvas.restoreState()
|
|
|
|
|
|
class MyDocTemplate(BaseDocTemplate):
|
|
_invalidInitArgs = ('pageTemplates',)
|
|
|
|
def __init__(self, filename, **kw):
|
|
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
|
|
self.allowSplitting = 0
|
|
apply(BaseDocTemplate.__init__, (self, filename), kw)
|
|
template = PageTemplate('normal', [frame1], myMainPageFrame)
|
|
self.addPageTemplates(template)
|
|
|
|
|
|
class ParagraphCorners(unittest.TestCase):
|
|
"some corner cases which should parse"
|
|
def check(text,bt = getSampleStyleSheet()['BodyText']):
|
|
try:
|
|
P = Paragraph(text,st)
|
|
except:
|
|
raise AssertionError("'%s' should parse"%text)
|
|
|
|
def test0(self):
|
|
self.check('<para />')
|
|
self.check('<para/>')
|
|
self.check('\t\t\t\n\n\n<para />')
|
|
self.check('\t\t\t\n\n\n<para/>')
|
|
self.check('<para\t\t\t\t/>')
|
|
self.check('<para></para>')
|
|
self.check('<para> </para>')
|
|
self.check('\t\t\n\t\t\t <para> </para>')
|
|
|
|
|
|
|
|
class ParagraphSplitTestCase(unittest.TestCase):
|
|
"Test multi-page splitting of paragraphs (eyeball-test)."
|
|
|
|
def test0(self):
|
|
"This makes one long multi-page paragraph."
|
|
|
|
# Build story.
|
|
story = []
|
|
styleSheet = getSampleStyleSheet()
|
|
bt = styleSheet['BodyText']
|
|
text = '''If you imagine that the box of X's tothe left is
|
|
an image, what I want to be able to do is flow a
|
|
series of paragraphs around the image
|
|
so that once the bottom of the image is reached, then text will flow back to the
|
|
left margin. I know that it would be possible to something like this
|
|
using tables, but I can't see how to have a generic solution.
|
|
There are two examples of this in the demonstration section of the reportlab
|
|
site.
|
|
If you look at the "minimal" euro python conference brochure, at the end of the
|
|
timetable section (page 8), there are adverts for "AdSu" and "O'Reilly". I can
|
|
see how the AdSu one might be done generically, but the O'Reilly, unsure...
|
|
I guess I'm hoping that I've missed something, and that
|
|
it's actually easy to do using platypus.
|
|
'''
|
|
from reportlab.platypus.flowables import ParagraphAndImage, Image
|
|
from reportlab.lib.utils import _RL_DIR
|
|
gif = os.path.join(_RL_DIR,'test','pythonpowered.gif')
|
|
story.append(ParagraphAndImage(Paragraph(text,bt),Image(gif)))
|
|
phrase = 'This should be a paragraph spanning at least three pages. '
|
|
description = ''.join([('%d: '%i)+phrase for i in xrange(250)])
|
|
story.append(ParagraphAndImage(Paragraph(description, bt),Image(gif),side='left'))
|
|
|
|
doc = MyDocTemplate(outputfile('test_platypus_paragraphandimage.pdf'))
|
|
doc.multiBuild(story)
|
|
|
|
def test1(self):
|
|
"This makes one long multi-page paragraph."
|
|
|
|
# Build story.
|
|
story = []
|
|
styleSheet = getSampleStyleSheet()
|
|
h3 = styleSheet['Heading3']
|
|
bt = styleSheet['BodyText']
|
|
text = '''If you imagine that the box of X's tothe left is
|
|
an image, what I want to be able to do is flow a
|
|
series of paragraphs around the image
|
|
so that once the bottom of the image is reached, then text will flow back to the
|
|
left margin. I know that it would be possible to something like this
|
|
using tables, but I can't see how to have a generic solution.
|
|
There are two examples of this in the demonstration section of the reportlab
|
|
site.
|
|
If you look at the "minimal" euro python conference brochure, at the end of the
|
|
timetable section (page 8), there are adverts for "AdSu" and "O'Reilly". I can
|
|
see how the AdSu one might be done generically, but the O'Reilly, unsure...
|
|
I guess I'm hoping that I've missed something, and that
|
|
it's actually easy to do using platypus.We can do greek letters <greek>mDngG</greek>. This should be a
|
|
u with a dieresis on top <unichar code=0xfc/>="<unichar code=0xfc/>" and this &#xfc;="ü" and this \\xc3\\xbc="\xc3\xbc". On the other hand this
|
|
should be a pound sign &pound;="£" and this an alpha &alpha;="α". You can have links in the page <link href=http://www.reportlab.com color=blue>ReportLab</link>.
|
|
Use scheme "pdf:" to indicate an external PDF link, "http:", "https:" to indicate an external link eg something to open in
|
|
your browser. If an internal link begins with something that looks like a scheme, precede with "document:".
|
|
'''
|
|
from reportlab.platypus.flowables import ImageAndFlowables, Image
|
|
from reportlab.lib.utils import _RL_DIR
|
|
gif = os.path.join(_RL_DIR,'test','pythonpowered.gif')
|
|
heading = Paragraph('This is a heading',h3)
|
|
story.append(ImageAndFlowables(Image(gif),[heading,Paragraph(text,bt)]))
|
|
phrase = 'This should be a paragraph spanning at least three pages. '
|
|
description = ''.join([('%d: '%i)+phrase for i in xrange(250)])
|
|
story.append(ImageAndFlowables(Image(gif),[heading,Paragraph(description, bt)],imageSide='left'))
|
|
|
|
doc = MyDocTemplate(outputfile('test_platypus_imageandflowables.pdf'))
|
|
doc.multiBuild(story)
|
|
|
|
class FragmentTestCase(unittest.TestCase):
|
|
"Test fragmentation of paragraphs."
|
|
|
|
def test0(self):
|
|
"Test empty paragraph."
|
|
|
|
styleSheet = getSampleStyleSheet()
|
|
B = styleSheet['BodyText']
|
|
text = ''
|
|
P = Paragraph(text, B)
|
|
frags = map(lambda f:f.text, P.frags)
|
|
assert frags == []
|
|
|
|
|
|
def test1(self):
|
|
"Test simple paragraph."
|
|
|
|
styleSheet = getSampleStyleSheet()
|
|
B = styleSheet['BodyText']
|
|
text = "X<font name=Courier>Y</font>Z"
|
|
P = Paragraph(text, B)
|
|
frags = map(lambda f:f.text, P.frags)
|
|
assert frags == ['X', 'Y', 'Z']
|
|
|
|
|
|
#noruntests
|
|
def makeSuite():
|
|
return makeSuiteForClasses(FragmentTestCase, ParagraphSplitTestCase)
|
|
|
|
|
|
#noruntests
|
|
if __name__ == "__main__":
|
|
unittest.TextTestRunner().run(makeSuite())
|
|
printLocation()
|