tests/test_platypus_preformatted.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
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
"""
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3854
diff changeset
     5
__version__='3.3.0'
3597
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 operator import truth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    10
import unittest
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    11
from reportlab.pdfbase.pdfmetrics import stringWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    12
from reportlab.platypus.paraparser import ParaParser
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    13
from reportlab.platypus.flowables import Flowable
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    14
from reportlab.lib.colors import Color
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    15
from reportlab.lib.units import cm
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    16
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
    17
from reportlab.lib.utils import _className
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    18
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    19
from reportlab.platypus.paragraph import Paragraph
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    20
from reportlab.platypus.frames import Frame
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    21
from reportlab.platypus.doctemplate \
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    22
     import PageTemplate, BaseDocTemplate, Indenter, FrameBreak, NextPageTemplate
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    23
from reportlab.platypus import tableofcontents
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    24
from reportlab.platypus.tableofcontents import TableOfContents
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    25
from reportlab.platypus.tables import TableStyle, Table
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    26
from reportlab.platypus.paragraph import *
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    27
from reportlab.platypus.paragraph import _getFragWords
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    28
from reportlab.platypus.flowables import Spacer, Preformatted
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    29
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    30
def myMainPageFrame(canvas, doc):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    31
    "The page frame used for all PDF documents."
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    32
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    33
    canvas.saveState()
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.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    36
    canvas.setFont('Times-Roman', 12)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    37
    pageNumber = canvas.getPageNumber()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    38
    canvas.drawString(10*cm, cm, str(pageNumber))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    39
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    40
    canvas.restoreState()
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
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    43
class MyDocTemplate(BaseDocTemplate):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    44
    _invalidInitArgs = ('pageTemplates',)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    45
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    46
    def __init__(self, filename, **kw):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    47
        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
    48
        self.allowSplitting = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    49
        BaseDocTemplate.__init__(self, filename, **kw)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    50
        template1 = PageTemplate('normal', [frame1], myMainPageFrame)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    51
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    52
        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
    53
        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
    54
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    55
        template2 = PageTemplate('updown', [frame2, frame3])
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    56
        self.addPageTemplates([template1, template2])
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    57
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    58
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    59
class WrappingTestCase(unittest.TestCase):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    60
    "Test wrapping of long urls"
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    61
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    62
    def test0(self):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    63
        "This makes one long multi-page paragraph."
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    64
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    65
        # Build story.
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    66
        story = []
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    67
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    68
        styleSheet = getSampleStyleSheet()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    69
        h1 = styleSheet['Heading1']
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    70
        h1.spaceBefore = 18
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    71
        bt = styleSheet['BodyText']
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    72
        bt.spaceBefore = 6
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    73
        normalStyle = styleSheet['Code']
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    74
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    75
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    76
        story.append(Paragraph('Test of preformatted text wrapping',h1))
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(Spacer(18,18))
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
        txt = """Our Preformatted class can be used for printing simple blocks
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    81
of code. It respects whitespace and newlines, and will not normally attempt 
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    82
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
    83
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
    84
attributes - maximumLineLength, splitCharacters and newLineCharacter - 
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    85
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
    86
wrap. Note that this simply counts characters - it takes no account of
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    87
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
    88
and add a '>' to the start of the following line.
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    89
"""
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    90
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    91
        story.append(Paragraph(txt,bt))
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("",bt))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    94
3854
88461978a888 fix tests
robin
parents: 3800
diff changeset
    95
        code = """
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
    96
#Copyright ReportLab Europe Ltd. 2000-2017
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    97
#see license.txt for license details
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
    98
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/xpreformatted.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3854
diff changeset
    99
__version__='3.3.0'
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   100
__doc__='''A 'rich preformatted text' widget allowing internal markup'''
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   101
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   102
from reportlab.lib import PyFontify
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   103
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
   104
from flowables import _dedenter
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   105
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   106
class XPreformatted(Paragraph):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   107
    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
   108
        self.caseSensitive = caseSensitive
3794
398ea04239b5 string module usage minimization
robin
parents: 3617
diff changeset
   109
        cleaner = lambda text, dedent=dedent: ,'\\n'.join(_dedenter(text or '',dedent))
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   110
        self._setup(text, style, bulletText, frags, cleaner)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   111
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   112
    def breakLines(self, width):
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
   113
        if isinstance(width,list): maxWidths = [width]
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   114
        else: maxWidths = width
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   115
        lines = []
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   116
        lineno = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   117
        maxWidth = maxWidths[lineno]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   118
        style = self.style
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   119
        fFontSize = float(style.fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   120
        requiredWidth = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   121
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   122
        #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
   123
        _handleBulletWidth(self.bulletText,style,maxWidths)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   124
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   125
        self.height = 0
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   126
        autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   127
        calcBounds = autoLeading not in ('','off')
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   128
        frags = self.frags
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   129
        nFrags= len(frags)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   130
        if nFrags==1:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   131
            f = frags[0]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   132
            if hasattr(f,'text'):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   133
                fontSize = f.fontSize
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   134
                fontName = f.fontName
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   135
                ascent, descent = getAscentDescent(fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   136
                kind = 0
3794
398ea04239b5 string module usage minimization
robin
parents: 3617
diff changeset
   137
                L=f.text.split('\\n')
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   138
                for l in L:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   139
                    currentWidth = stringWidth(l,fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   140
                    requiredWidth = max(currentWidth,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   141
                    extraSpace = maxWidth-currentWidth
3794
398ea04239b5 string module usage minimization
robin
parents: 3617
diff changeset
   142
                    lines.append((extraSpace,l.split(' '),currentWidth))
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   143
                    lineno = lineno+1
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   144
                    maxWidth = lineno&lt;len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   145
                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
   146
            else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   147
                kind = f.kind
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   148
                lines = f.lines
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   149
                for L in lines:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   150
                    if kind==0:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   151
                        currentWidth = L[2]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   152
                    else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   153
                        currentWidth = L.currentWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   154
                    requiredWidth = max(currentWidth,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   155
                blPara = f.clone(kind=kind, lines=lines)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   156
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   157
            self.width = max(self.width,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   158
            return blPara
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   159
        elif nFrags&lt;=0:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   160
            return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   161
                            textColor=style.textColor, ascent=style.fontSize,descent=-0.2*style.fontSize,
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   162
                            lines=[])
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   163
        else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   164
            for L in _getFragLines(frags):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   165
                currentWidth, n, w = _getFragWord(L,maxWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   166
                f = w[0][0]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   167
                maxSize = f.fontSize
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   168
                maxAscent, minDescent = getAscentDescent(f.fontName,maxSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   169
                words = [f.clone()]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   170
                words[-1].text = w[0][1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   171
                for i in w[1:]:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   172
                    f = i[0].clone()
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   173
                    f.text=i[1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   174
                    words.append(f)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   175
                    fontSize = f.fontSize
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   176
                    fontName = f.fontName
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   177
                    if calcBounds:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   178
                        cbDefn = getattr(f,'cbDefn',None)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   179
                        if getattr(cbDefn,'width',0):
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   180
                            descent,ascent = imgVRange(imgNormV(cbDefn.height,fontSize),cbDefn.valign,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   181
                        else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   182
                            ascent, descent = getAscentDescent(fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   183
                    else:
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   184
                        ascent, descent = getAscentDescent(fontName,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   185
                    maxSize = max(maxSize,fontSize)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   186
                    maxAscent = max(maxAscent,ascent)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   187
                    minDescent = min(minDescent,descent)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   188
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   189
                lineno += 1
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   190
                maxWidth = lineno&lt;len(maxWidths) and maxWidths[lineno] or maxWidths[-1]
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   191
                requiredWidth = max(currentWidth,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   192
                extraSpace = maxWidth - currentWidth
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   193
                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
   194
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   195
            self.width = max(self.width,requiredWidth)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   196
            return ParaLines(kind=1, lines=lines)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   197
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   198
        return lines
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   199
"""
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   200
        
3598
be6a6b08fe31 Updated test for preformatted text wrapping
guillaume
parents: 3597
diff changeset
   201
        story.append(Preformatted(code,normalStyle,dedent=0, maxLineLength=60, newLineChars='> '))
3597
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   202
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   203
        doc = MyDocTemplate(outputfile('test_platypus_preformatted.pdf'))
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   204
        doc.multiBuild(story)
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   205
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   206
#noruntests
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   207
def makeSuite():
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   208
    return makeSuiteForClasses(WrappingTestCase)
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
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   211
#noruntests
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   212
if __name__ == "__main__":
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   213
    unittest.TextTestRunner().run(makeSuite())
b1473bb847f6 Test for preformatted text wrapping in Paltypus
guillaume
parents:
diff changeset
   214
    printLocation()