tests/test_platypus_cjk_wrap.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4630 604952d66599
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
3545
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     2
#see license.txt for license details
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     3
"""Tests for the reportlab.platypus.paragraphs module.
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     4
"""
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3721
diff changeset
     5
__version__='3.3.0'
3545
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     6
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     7
setOutDir(__name__)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     8
import sys, os, unittest
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
     9
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    10
from reportlab.platypus.paragraph import Paragraph
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    11
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    12
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate, PageBreak, NextPageTemplate
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    13
from reportlab.platypus.frames import Frame, ShowBoundaryValue
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    14
from reportlab.lib.colors import Color
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    15
from reportlab.lib.units import cm
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    16
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    17
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    18
from reportlab.pdfbase.cidfonts import CIDFont, findCMapFile, UnicodeCIDFont
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    19
from reportlab.pdfbase import pdfmetrics
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    20
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    21
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    22
#says "Japanese is difficult, isn't it?"
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    23
shortText = '\u65e5\u672c\u8a9e\u306f\u96e3\u3057\u3044\u3067\u3059\u306d\uff01'
3545
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    24
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    25
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    26
gatwickText = '\xe3\x82\xac\xe3\x83\x88\xe3\x82\xa6\xe3\x82\xa3\xe3\x83\x83\xe3\x82\xaf\xe7\xa9\xba\xe6\xb8\xaf\xe3\x81\xa8\xe9\x80\xa3\xe7\xb5\xa1\xe9\x80\x9a\xe8\xb7\xaf\xe3\x81\xa7\xe7\x9b\xb4\xe7\xb5\x90\xe3\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x82\x8b\xe5\x94\xaf\xe4\xb8\x80\xe3\x81\xae\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x81\xa7\xe3\x81\x82\xe3\x82\x8b\xe5\xbd\x93\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x81\xaf\xe3\x80\x81VERYLONGENGLISHGOESHERE\xe8\xa1\x97\xe3\x81\xae\xe4\xb8\xad\xe5\xbf\x83\xe9\x83\xa8\xe3\x81\x8b\xe3\x82\x8930\xe5\x88\x86\xe3\x81\xae\xe5\xa0\xb4\xe6\x89\x80\xe3\x81\xab\xe3\x81\x94\xe3\x81\x96\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe5\x85\xa8\xe5\xae\xa2\xe5\xae\xa4\xe3\x81\xab\xe9\xab\x98\xe9\x80\x9f\xe3\x82\xa4\xe3\x83\xb3\xe3\x82\xbf\xe3\x83\xbc\xe3\x83\x8d\xe3\x83\x83\xe3\x83\x88\xe7\x92\xb0\xe5\xa2\x83\xe3\x82\x92\xe5\xae\x8c\xe5\x82\x99\xe3\x81\x97\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe3\x83\x95\xe3\x82\xa1\xe3\x83\x9f\xe3\x83\xaa\xe3\x83\xbc\xe3\x83\xab\xe3\x83\xbc\xe3\x83\xa0\xe3\x81\xaf5\xe5\x90\x8d\xe6\xa7\x98\xe3\x81\xbe\xe3\x81\xa7\xe3\x81\x8a\xe6\xb3\x8a\xe3\x82\x8a\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe3\x81\xbe\xe3\x81\x9f\xe3\x80\x81\xe3\x82\xa8\xe3\x82\xb0\xe3\x82\xbc\xe3\x82\xaf\xe3\x83\x86\xe3\x82\xa3\xe3\x83\x96\xe3\x83\xab\xe3\x83\xbc\xe3\x83\xa0\xe3\x81\xae\xe3\x81\x8a\xe5\xae\xa2\xe6\xa7\x98\xe3\x81\xaf\xe3\x80\x81\xe3\x82\xa8\xe3\x82\xb0\xe3\x82\xbc\xe3\x82\xaf\xe3\x83\x86\xe3\x82\xa3\xe3\x83\x96\xe3\x83\xa9\xe3\x82\xa6\xe3\x83\xb3\xe3\x82\xb8\xe3\x82\x92\xe3\x81\x94\xe5\x88\xa9\xe7\x94\xa8\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82\xe4\xba\x8b\xe5\x89\x8d\xe3\x81\xab\xe3\x81\x94\xe4\xba\x88\xe7\xb4\x84\xe3\x81\x84\xe3\x81\x9f\xe3\x81\xa0\xe3\x81\x91\xe3\x82\x8b\xe3\x82\xbf\xe3\x82\xa4\xe3\x83\xa0\xe3\x83\x88\xe3\x82\xa5\xe3\x83\x95\xe3\x83\xa9\xe3\x82\xa4\xe3\x83\xbb\xe3\x83\x91\xe3\x83\x83\xe3\x82\xb1\xe3\x83\xbc\xe3\x82\xb8\xe3\x81\xab\xe3\x81\xaf\xe3\x80\x81\xe7\xa9\xba\xe6\xb8\xaf\xe3\x81\xae\xe9\xa7\x90\xe8\xbb\x8a\xe6\x96\x99\xe9\x87\x91\xe3\x81\x8c\xe5\x90\xab\xe3\x81\xbe\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x8a\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x99\xe3\x80\x82'
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    27
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    28
def myMainPageFrame(canvas, doc):
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    29
    "The page frame used for all PDF documents."
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    30
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    31
    canvas.saveState()
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    32
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    33
    canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    34
    canvas.setFont('Times-Roman', 12)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    35
    pageNumber = canvas.getPageNumber()
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    36
    canvas.drawString(10*cm, cm, str(pageNumber))
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    37
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    38
    canvas.restoreState()
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    39
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    40
class MyDocTemplate(BaseDocTemplate):
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    41
    _invalidInitArgs = ('pageTemplates',)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    42
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    43
    def __init__(self, filename, **kw):
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    44
        frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    45
        frame2 = Frame(2.5*cm, 2.5*cm, 310, 25*cm, id='F2')
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    46
        self.allowSplitting = 0
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    47
        BaseDocTemplate.__init__(self, filename, **kw)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    48
        template = PageTemplate('normal', [frame1], myMainPageFrame)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    49
        template1 = PageTemplate('special', [frame2], myMainPageFrame)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    50
        self.addPageTemplates([template,template1])
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    51
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    52
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    53
class CJKWrap(unittest.TestCase):
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    54
    "Exercises cjkFragSplit"
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    55
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    56
    def test1(self):
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    57
        "This makes several special paragraphs."
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    58
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    59
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    60
        pdfmetrics.registerFont(UnicodeCIDFont('HeiseiMin-W3'))
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    61
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    62
        # Build story.
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    63
        story = []
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    64
        styleSheet = getSampleStyleSheet()
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    65
        bt = styleSheet['BodyText']
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    66
        cjk = ParagraphStyle(
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    67
                            name="cjkwrap", 
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    68
                            fontName="HeiseiMin-W3",
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    69
                            parent=styleSheet['BodyText'], 
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    70
                            wordWrap="CJK")
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    71
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    72
        story.append(Paragraph('''This is a test of CJK word wrapping.  
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    73
            When using the CJK algorithm, we split between characters.  
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    74
            Unfortunately this sucks when English words are involved - for example the title
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    75
            of a technical product manual.  This paragraph uses CJK wrapping, so if you can 
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    76
            see English words getting broken in two, something went wrong! At the time of writing, it looks OK.
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    77
            ''',style=cjk))
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    78
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    79
        story.append(Paragraph(shortText * 3 + "This should get broken between some words. " + shortText * 2,style=cjk))
3545
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    80
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    81
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    82
        story.append(Paragraph(shortText * 3 + "This_should_be_unbroken_as_we_backtrack_to_previous_kanji. " + shortText * 2,style=cjk))
3545
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    83
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
    84
        story.append(Paragraph(shortText + "This_should_be_broken_in_mid_word_as_we_do_not_backtrack_home_than_half_a_line. " + shortText * 2,style=cjk))
3545
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    85
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    86
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    87
        story.append(Paragraph("Regression test: we really don't want line 2 to start with an exclamation mark." + shortText * 5,style=cjk))
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    88
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    89
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    90
        doc = MyDocTemplate(outputfile('test_platypus_cjk_wrap.pdf'))
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    91
        doc.build(story)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    92
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    93
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    94
#noruntests
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    95
def makeSuite():
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    96
    return makeSuiteForClasses(CJKWrap,)
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    97
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    98
#noruntests
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
    99
if __name__ == "__main__":
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
   100
    unittest.TextTestRunner().run(makeSuite())
538ad211299b english-in-japanese splitting
andy
parents:
diff changeset
   101
    printLocation()