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