#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_paragraphs.py # tests some paragraph styles from reportlab.test import unittest from reportlab.test.utils import makeSuiteForClasses, outputfile, printLocation from reportlab.platypus import Paragraph, SimpleDocTemplate, XBox, Indenter, XPreformatted from reportlab.lib.styles import ParagraphStyle from reportlab.lib.units import inch from reportlab.lib.colors import red, black, navy, white, green from reportlab.lib.randomtext import randomText from reportlab.rl_config import defaultPageSize (PAGE_WIDTH, PAGE_HEIGHT) = defaultPageSize def myFirstPage(canvas, doc): canvas.saveState() canvas.setStrokeColor(red) canvas.setLineWidth(5) canvas.line(66,72,66,PAGE_HEIGHT-72) canvas.setFont('Times-Bold',24) canvas.drawString(108, PAGE_HEIGHT-54, "TESTING PARAGRAPH STYLES") canvas.setFont('Times-Roman',12) canvas.drawString(4 * inch, 0.75 * inch, "First Page") canvas.restoreState() def myLaterPages(canvas, doc): canvas.saveState() canvas.setStrokeColor(red) canvas.setLineWidth(5) canvas.line(66,72,66,PAGE_HEIGHT-72) canvas.setFont('Times-Roman',12) canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page) canvas.restoreState() class ParagraphTestCase(unittest.TestCase): "Test Paragraph class (eyeball-test)." def test0(self): """Test... The story should contain... Features to be visually confirmed by a human being are: 1. ... 2. ... 3. ... """ story = [] #need a style styNormal = ParagraphStyle('normal') styGreen = ParagraphStyle('green',parent=styNormal,textColor=green) # some to test stySpaced = ParagraphStyle('spaced', parent=styNormal, spaceBefore=12, spaceAfter=12) story.append( Paragraph("This is a normal paragraph. " + randomText(), styNormal)) story.append( Paragraph("This has 12 points space before and after, set in the style. " + randomText(), stySpaced)) story.append( Paragraph("This is normal. " + randomText(), styNormal)) story.append( Paragraph(""" This has 12 points space before and after, set inline with XML tag. It works too.""" + randomText() + "This got a background from the para tag""", styNormal)) story.append( Paragraph("""\n\tThis has newlines and tabs on the front but inside the para tag""", styNormal)) story.append( Paragraph(""" This has spaces on the front but inside the para tag""", styNormal)) story.append( Paragraph("""\n\tThis has newlines and tabs on the front but no para tag""", styNormal)) story.append( Paragraph(""" This has spaces on the front but no para tag""", styNormal)) story.append(Paragraph("""This has blue text here.""", styNormal)) story.append(Paragraph("""This has italic text here.""", styNormal)) story.append(Paragraph("""This has bold text here.""", styNormal)) story.append(Paragraph("""This has underlined text here.""", styNormal)) story.append(Paragraph("""This has blue and red underlined text here.""", styNormal)) story.append(Paragraph("""green underlining""", styGreen)) story.append(Paragraph("""green underlining""", styGreen)) story.append(Paragraph("""This has m2 a superscript.""", styNormal)) story.append(Paragraph("""This has m2 a subscript. Like H2O!""", styNormal)) story.append(Paragraph("""This has a font change to Helvetica.""", styNormal)) #This one fails: #story.append(Paragraph("""This has a font change to Helvetica-Oblique.""", styNormal)) story.append(Paragraph("""This has a font change to Helvetica in italics.""", styNormal)) story.append(Paragraph('''This one uses upper case tags and has set caseSensitive=0: Here comes Helvetica 14 with strong emphasis.''', styNormal, caseSensitive=0)) story.append(Paragraph('''The same as before, but has set not set caseSensitive, thus the tags are ignored: Here comes Helvetica 14 with strong emphasis.''', styNormal)) story.append(Paragraph('''This one uses fonts with size "14pt" and also uses the em and strong tags: Here comes Helvetica 14 with strong emphasis.''', styNormal, caseSensitive=0)) story.append(Paragraph('''This uses a font size of 3cm: Here comes Courier 3cm and normal again.''', styNormal, caseSensitive=0)) story.append(Paragraph('''This is just a very long silly text to see if the caseSensitive flag also works if the paragraph is very long. '''*20, styNormal, caseSensitive=0)) story.append(Indenter("1cm")) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Indenter("1cm")) story.append(XPreformatted(")Indented list.", styNormal)) story.append(XPreformatted(")Indented list.", styNormal)) story.append(Indenter("-1cm")) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Indenter("-1cm")) story.append(Paragraph("Indented list using seqChain/Format", stySpaced)) story.append(Indenter("1cm")) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Indenter("1cm")) story.append(XPreformatted(")Indented list.", styNormal)) story.append(XPreformatted(")Indented list.", styNormal)) story.append(Indenter("-1cm")) story.append(Paragraph(")Indented list. %s" % randomText(), styNormal)) story.append(Indenter("1cm")) story.append(XPreformatted(")Indented list.", styNormal)) story.append(Indenter("1cm")) story.append(XPreformatted(")Indented list. line1", styNormal)) story.append(XPreformatted(")Indented list. line2", styNormal)) story.append(Indenter("-1cm")) story.append(XPreformatted(")Indented list.", styNormal)) story.append(Indenter("-1cm")) story.append(Indenter("-1cm")) template = SimpleDocTemplate(outputfile('test_paragraphs.pdf'), showBoundary=1) template.build(story, onFirstPage=myFirstPage, onLaterPages=myLaterPages) def makeSuite(): return makeSuiteForClasses(ParagraphTestCase) #noruntests if __name__ == "__main__": unittest.TextTestRunner().run(makeSuite()) printLocation()