tests/test_platypus_preformatted.py
author robin
Thu, 27 Sep 2012 14:39:39 +0000
changeset 3617 ae5744e97c42
parent 3598 be6a6b08fe31
child 3794 398ea04239b5
permissions -rw-r--r--
reportlab: copyright date changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3598
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     2
#see license.txt for license details
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     3
"""Tests for context-dependent indentation
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     4
"""
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     5
__version__='''$Id: test_platypus_indents.py 3660 2010-02-08 18:17:33Z damian $'''
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     6
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     7
setOutDir(__name__)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     8
import sys, os, random
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
     9
from string import split, strip, join, whitespace
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    10
from operator import truth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    11
from types import StringType, ListType
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    12
import unittest
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    13
from reportlab.pdfbase.pdfmetrics import stringWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    14
from reportlab.platypus.paraparser import ParaParser
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    15
from reportlab.platypus.flowables import Flowable
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    16
from reportlab.lib.colors import Color
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    17
from reportlab.lib.units import cm
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    18
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    19
from reportlab.lib.utils import _className
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    20
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    21
from reportlab.platypus.paragraph import Paragraph
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    22
from reportlab.platypus.frames import Frame
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    23
from reportlab.platypus.doctemplate \
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    24
     import PageTemplate, BaseDocTemplate, Indenter, FrameBreak, NextPageTemplate
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    25
from reportlab.platypus import tableofcontents
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    26
from reportlab.platypus.tableofcontents import TableOfContents
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    27
from reportlab.platypus.tables import TableStyle, Table
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    28
from reportlab.platypus.paragraph import *
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    29
from reportlab.platypus.paragraph import _getFragWords
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    30
from reportlab.platypus.flowables import Spacer, Preformatted
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    31
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    32
def myMainPageFrame(canvas, doc):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    33
    "The page frame used for all PDF documents."
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    34
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    35
    canvas.saveState()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    36
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    37
    canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    38
    canvas.setFont('Times-Roman', 12)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    39
    pageNumber = canvas.getPageNumber()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    40
    canvas.drawString(10*cm, cm, str(pageNumber))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    41
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    42
    canvas.restoreState()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    43
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    44
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    45
class MyDocTemplate(BaseDocTemplate):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    46
    _invalidInitArgs = ('pageTemplates',)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    47
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    48
    def __init__(self, filename, **kw):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    49
        frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    50
        self.allowSplitting = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    51
        BaseDocTemplate.__init__(self, filename, **kw)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    52
        template1 = PageTemplate('normal', [frame1], myMainPageFrame)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    53
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    54
        frame2 = Frame(2.5*cm, 16*cm, 15*cm, 10*cm, id='F2', showBoundary=1)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    55
        frame3 = Frame(2.5*cm, 2.5*cm, 15*cm, 10*cm, id='F3', showBoundary=1)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    56
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    57
        template2 = PageTemplate('updown', [frame2, frame3])
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    58
        self.addPageTemplates([template1, template2])
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    59
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    60
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    61
class WrappingTestCase(unittest.TestCase):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    62
    "Test wrapping of long urls"
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    63
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    64
    def test0(self):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    65
        "This makes one long multi-page paragraph."
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    66
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    67
        # Build story.
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    68
        story = []
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    69
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    70
        styleSheet = getSampleStyleSheet()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    71
        h1 = styleSheet['Heading1']
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    72
        h1.spaceBefore = 18
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    73
        bt = styleSheet['BodyText']
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    74
        bt.spaceBefore = 6
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    75
        normalStyle = styleSheet['Code']
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    76
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    77
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    78
        story.append(Paragraph('Test of preformatted text wrapping',h1))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    79
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    80
        story.append(Spacer(18,18))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    81
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    82
        txt = """Our Preformatted class can be used for printing simple blocks
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    83
of code. It respects whitespace and newlines, and will not normally attempt 
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    84
to wrap your code. However, if your individual lines are too long, this can 
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    85
overflow the width of the column and even run off the page. Three optional 
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    86
attributes - maximumLineLength, splitCharacters and newLineCharacter - 
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    87
can be used to do simple wrapping. maximumLineLength will force the text to
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    88
wrap. Note that this simply counts characters - it takes no account of
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    89
actual width on the page. The examples below wrap lines above a certain length
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    90
and add a '>' to the start of the following line.
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    91
"""
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    92
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    93
        story.append(Paragraph(txt,bt))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    94
        
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    95
        story.append(Paragraph("",bt))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    96
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    97
	code = """
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3598
diff changeset
    98
#Copyright ReportLab Europe Ltd. 2000-2012
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    99
#see license.txt for license details
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   100
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/xpreformatted.py
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   101
__version__=''' $Id: xpreformatted.py 3866 2011-06-27 13:08:20Z rgbecker $ '''
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   102
__doc__='''A 'rich preformatted text' widget allowing internal markup'''
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   103
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   104
import string
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   105
from types import StringType, ListType
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   106
from reportlab.lib import PyFontify
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   107
from paragraph import Paragraph, cleanBlockQuotedText, _handleBulletWidth, ParaLines, _getFragWords, stringWidth, _sameFrag, getAscentDescent, imgVRange, imgNormV
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   108
from flowables import _dedenter
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   109
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   110
class XPreformatted(Paragraph):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   111
    def __init__(self, text, style, bulletText = None, frags=None, caseSensitive=1, dedent=0):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   112
        self.caseSensitive = caseSensitive
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   113
        cleaner = lambda text, dedent=dedent: string.join(_dedenter(text or '',dedent),'\\n')
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   114
        self._setup(text, style, bulletText, frags, cleaner)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   115
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   116
    def breakLines(self, width):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   117
        if type(width) != ListType: maxWidths = [width]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   118
        else: maxWidths = width
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   119
        lines = []
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   120
        lineno = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   121
        maxWidth = maxWidths[lineno]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   122
        style = self.style
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   123
        fFontSize = float(style.fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   124
        requiredWidth = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   125
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   126
        #for bullets, work out width and ensure we wrap the right amount onto line one
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   127
        _handleBulletWidth(self.bulletText,style,maxWidths)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   128
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   129
        self.height = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   130
        autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   131
        calcBounds = autoLeading not in ('','off')
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   132
        frags = self.frags
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   133
        nFrags= len(frags)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   134
        if nFrags==1:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   135
            f = frags[0]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   136
            if hasattr(f,'text'):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   137
                fontSize = f.fontSize
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   138
                fontName = f.fontName
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   139
                ascent, descent = getAscentDescent(fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   140
                kind = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   141
                L=string.split(f.text, '\\n')
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   142
                for l in L:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   143
                    currentWidth = stringWidth(l,fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   144
                    requiredWidth = max(currentWidth,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   145
                    extraSpace = maxWidth-currentWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   146
                    lines.append((extraSpace,string.split(l,' '),currentWidth))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   147
                    lineno = lineno+1
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   148
                    maxWidth = lineno<len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   149
                blPara = f.clone(kind=kind, lines=lines,ascent=ascent,descent=descent,fontSize=fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   150
            else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   151
                kind = f.kind
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   152
                lines = f.lines
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   153
                for L in lines:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   154
                    if kind==0:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   155
                        currentWidth = L[2]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   156
                    else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   157
                        currentWidth = L.currentWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   158
                    requiredWidth = max(currentWidth,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   159
                blPara = f.clone(kind=kind, lines=lines)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   160
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   161
            self.width = max(self.width,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   162
            return blPara
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   163
        elif nFrags<=0:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   164
            return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   165
                            textColor=style.textColor, ascent=style.fontSize,descent=-0.2*style.fontSize,
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   166
                            lines=[])
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   167
        else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   168
            for L in _getFragLines(frags):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   169
                currentWidth, n, w = _getFragWord(L,maxWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   170
                f = w[0][0]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   171
                maxSize = f.fontSize
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   172
                maxAscent, minDescent = getAscentDescent(f.fontName,maxSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   173
                words = [f.clone()]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   174
                words[-1].text = w[0][1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   175
                for i in w[1:]:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   176
                    f = i[0].clone()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   177
                    f.text=i[1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   178
                    words.append(f)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   179
                    fontSize = f.fontSize
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   180
                    fontName = f.fontName
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   181
                    if calcBounds:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   182
                        cbDefn = getattr(f,'cbDefn',None)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   183
                        if getattr(cbDefn,'width',0):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   184
                            descent,ascent = imgVRange(imgNormV(cbDefn.height,fontSize),cbDefn.valign,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   185
                        else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   186
                            ascent, descent = getAscentDescent(fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   187
                    else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   188
                        ascent, descent = getAscentDescent(fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   189
                    maxSize = max(maxSize,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   190
                    maxAscent = max(maxAscent,ascent)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   191
                    minDescent = min(minDescent,descent)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   192
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   193
                lineno += 1
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   194
                maxWidth = lineno<len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   195
                requiredWidth = max(currentWidth,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   196
                extraSpace = maxWidth - currentWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   197
                lines.append(ParaLines(extraSpace=extraSpace,wordCount=n, words=words, fontSize=maxSize, ascent=maxAscent,descent=minDescent,currentWidth=currentWidth))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   198
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   199
            self.width = max(self.width,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   200
            return ParaLines(kind=1, lines=lines)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   201
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   202
        return lines
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   203
"""
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   204
        
3598
be6a6b08fe31 Updated test for preformatted text wrapping
guillaume
parents: 3597
diff changeset
   205
        story.append(Preformatted(code,normalStyle,dedent=0, maxLineLength=60, newLineChars='> '))
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   206
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   207
        doc = MyDocTemplate(outputfile('test_platypus_preformatted.pdf'))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   208
        doc.multiBuild(story)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   209
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   210
#noruntests
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   211
def makeSuite():
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   212
    return makeSuiteForClasses(WrappingTestCase)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   213
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   214
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   215
#noruntests
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   216
if __name__ == "__main__":
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   217
    unittest.TextTestRunner().run(makeSuite())
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   218
    printLocation()