tests/test_platypus_wrapping.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4562 218cb11155d1
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
3593
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     2
#see license.txt for license details
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     3
"""Tests for context-dependent indentation
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     4
"""
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3786
diff changeset
     5
__version__='3.3.0'
3593
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     6
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     7
setOutDir(__name__)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     8
import sys, os, random
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
     9
from operator import truth
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    10
import unittest
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    11
from reportlab.pdfbase.pdfmetrics import stringWidth
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    12
from reportlab.platypus.paraparser import ParaParser
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    13
from reportlab.platypus.flowables import Flowable
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    14
from reportlab.lib.colors import Color
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    15
from reportlab.lib.units import cm
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    16
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    17
from reportlab.lib.utils import _className
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    18
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    19
from reportlab.platypus.paragraph import Paragraph
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    20
from reportlab.platypus.frames import Frame
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    21
from reportlab.platypus.doctemplate \
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    22
     import PageTemplate, BaseDocTemplate, Indenter, FrameBreak, NextPageTemplate
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    23
from reportlab.platypus import tableofcontents
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    24
from reportlab.platypus.tableofcontents import TableOfContents
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    25
from reportlab.platypus.tables import TableStyle, Table
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    26
from reportlab.platypus.paragraph import *
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    27
from reportlab.platypus.paragraph import _getFragWords
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    28
from reportlab.platypus.flowables import Spacer
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    29
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    30
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    31
def myMainPageFrame(canvas, doc):
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    32
    "The page frame used for all PDF documents."
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    33
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    34
    canvas.saveState()
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    35
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    36
    canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    37
    canvas.setFont('Times-Roman', 12)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    38
    pageNumber = canvas.getPageNumber()
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    39
    canvas.drawString(10*cm, cm, str(pageNumber))
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    40
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    41
    canvas.restoreState()
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    42
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    43
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    44
class MyDocTemplate(BaseDocTemplate):
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    45
    _invalidInitArgs = ('pageTemplates',)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    46
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    47
    def __init__(self, filename, **kw):
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    48
        frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    49
        self.allowSplitting = 0
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    50
        BaseDocTemplate.__init__(self, filename, **kw)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    51
        template1 = PageTemplate('normal', [frame1], myMainPageFrame)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    52
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    53
        frame2 = Frame(2.5*cm, 16*cm, 15*cm, 10*cm, id='F2', showBoundary=1)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    54
        frame3 = Frame(2.5*cm, 2.5*cm, 15*cm, 10*cm, id='F3', showBoundary=1)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    55
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    56
        template2 = PageTemplate('updown', [frame2, frame3])
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    57
        self.addPageTemplates([template1, template2])
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    58
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    59
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    60
class WrappingTestCase(unittest.TestCase):
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    61
    "Test wrapping of long urls"
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    62
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    63
    def test0(self):
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    64
        "This makes one long multi-page paragraph."
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    65
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    66
        # Build story.
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    67
        story = []
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    68
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    69
        styleSheet = getSampleStyleSheet()
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    70
        h1 = styleSheet['Heading1']
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    71
        h1.spaceBefore = 18
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    72
        bt = styleSheet['BodyText']
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    73
        bt.spaceBefore = 6
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    74
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    75
        story.append(Paragraph('Test of paragraph wrapping',h1))
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    76
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    77
        story.append(Spacer(18,18))
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    78
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    79
        txt = "Normally we wrap paragraphs by looking for spaces between the words.  However, with long technical command references and URLs, sometimes this gives ugly results.  We attempt to split really long words on certain tokens:  slashes, dots etc."
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    80
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    81
        story.append(Paragraph(txt,bt))
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    82
        
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    83
        story.append(Paragraph('This is an attempt to break long URLs sanely.  Here is a file name: <font face="Courier">C:\\Windows\\System32\\Drivers\\etc\\hosts</font>.  ', bt))
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    84
        
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    85
3786
5008a8b9567b fix usage of instanceStringWidthT1
robin
parents: 3617
diff changeset
    86
        story.append(Paragraph('This paragraph has a URL (basically, a word) too long to fit on one line, so it just overflows. http://some-really-long-site.somewhere-verbose.com/webthingies/framework/xc4987236hgsdlkafh/foo?format=dingbats&amp;content=rubbish. Ideally, we would wrap it in the middle.', bt))
3593
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    87
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    88
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    89
        
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    90
        
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    91
        doc = MyDocTemplate(outputfile('test_platypus_wrapping.pdf'))
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    92
        doc.multiBuild(story)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    93
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    94
#noruntests
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    95
def makeSuite():
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    96
    return makeSuiteForClasses(WrappingTestCase)
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    97
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    98
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
    99
#noruntests
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
   100
if __name__ == "__main__":
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
   101
    unittest.TextTestRunner().run(makeSuite())
355b9b5505ec added new test script for hyphenating long commands and URLs. New behaviour is not done yet
andy
parents:
diff changeset
   102
    printLocation()