reportlab/platypus/paragraph.py
author rgbecker
Wed, 10 Nov 2004 18:35:10 +0000
changeset 2408 1c5e79611b59
parent 2400 f47d5c6d8fcd
child 2445 5e52f2209426
permissions -rw-r--r--
flowables: add SlowPageBreak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2321
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 488
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2321
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/paragraph.py
2321
3454f5b41760 Unicode and UTF8 support changes
andy
parents: 2211
diff changeset
     4
__version__=''' $Id$ '''
938
36f66f88a4a6 Added find to imports
rgbecker
parents: 683
diff changeset
     5
from string import split, strip, join, whitespace, find
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
     6
from operator import truth
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
     7
from types import StringType, ListType
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
     8
from reportlab.pdfbase.pdfmetrics import stringWidth
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
     9
from reportlab.platypus.paraparser import ParaParser
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 268
diff changeset
    10
from reportlab.platypus.flowables import Flowable
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    11
from reportlab.lib.colors import Color
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    12
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
449
1ba0d6fb0f9e XPreformatted fixed empty lines and leading space
rgbecker
parents: 438
diff changeset
    13
from reportlab.lib.utils import _className
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
    14
from copy import deepcopy
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    15
from reportlab.lib.abag import ABag
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    16
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    17
class ParaLines(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    18
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    19
    class ParaLines contains the broken into lines representation of Paragraphs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    20
        kind=0  Simple
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    21
        fontName, fontSize, textColor apply to whole Paragraph
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    22
        lines   [(extraSpace1,words1),....,(extraspaceN,wordsN)]
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    23
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    24
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    25
        kind==1 Complex
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    26
        lines   [FragLine1,...,FragLineN]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    27
    """
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    28
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    29
class FragLine(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    30
    """class FragLine contains a styled line (ie a line with more than one style)
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    31
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    32
    extraSpace  unused space for justification only
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    33
    wordCount   1+spaces in line for justification purposes
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    34
    words       [ParaFrags] style text lumps to be concatenated together
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    35
    fontSize    maximum fontSize seen on the line; not used at present,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    36
                but could be used for line spacing.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    37
    """
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    38
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    39
#our one and only parser
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    40
# XXXXX if the parser has any internal state using only one is probably a BAD idea!
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    41
_parser=ParaParser()
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    42
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    43
def _lineClean(L):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    44
    return join(filter(truth,split(strip(L))))
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    45
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
    46
def cleanBlockQuotedText(text,joiner=' '):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    47
    """This is an internal utility which takes triple-
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    48
    quoted text form within the document and returns
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    49
    (hopefully) the paragraph the user intended originally."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    50
    L=filter(truth,map(_lineClean, split(text, '\n')))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    51
    return join(L, joiner)
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    52
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    53
def setXPos(tx,dx):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    54
    if dx>1e-6 or dx<-1e-6:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    55
        tx.setXPos(dx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    56
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    57
def _leftDrawParaLine( tx, offset, extraspace, words, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    58
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    59
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    60
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    61
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    62
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    63
def _centerDrawParaLine( tx, offset, extraspace, words, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    64
    m = offset + 0.5 * extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    65
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    66
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    67
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    68
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    69
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    70
def _rightDrawParaLine( tx, offset, extraspace, words, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    71
    m = offset + extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    72
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    73
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    74
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    75
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    76
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    77
def _justifyDrawParaLine( tx, offset, extraspace, words, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    78
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    79
    text  = join(words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    80
    if last:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    81
        #last one, left align
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    82
        tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    83
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    84
        nSpaces = len(words)-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    85
        if nSpaces:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    86
            tx.setWordSpace(extraspace / float(nSpaces))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    87
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    88
            tx.setWordSpace(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    89
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    90
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    91
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    92
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    93
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    94
def _putFragLine(tx,words):
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    95
    cur_x = 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    96
    for f in words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    97
        if hasattr(f,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    98
            func = getattr(tx._canvas,f.cbDefn.name,None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    99
            if not func:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   100
                raise AttributeError, "Missing %s callback attribute '%s'" % (f.cbDefn.kind,f.cbDefn.name)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   101
            func(tx._canvas,f.cbDefn.kind,f.cbDefn.label)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   102
            if f is words[-1]: tx._textOut('',1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   103
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   104
            if (tx._fontname,tx._fontsize)!=(f.fontName,f.fontSize):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   105
                tx._setFont(f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   106
            if tx.XtraState.textColor!=f.textColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   107
                tx.XtraState.textColor = f.textColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   108
                tx.setFillColor(f.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   109
            if tx.XtraState.rise!=f.rise:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   110
                tx.XtraState.rise=f.rise
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   111
                tx.setRise(f.rise)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   112
            tx._textOut(f.text,f is words[-1])  # cheap textOut
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   113
            txtlen = tx._canvas.stringWidth(f.text, tx._fontname, tx._fontsize)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   114
            if not tx.XtraState.underline and f.underline:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   115
                tx.XtraState.underline = 1
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   116
                tx.XtraState.underline_x = cur_x
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   117
            elif tx.XtraState.underline and not f.underline:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   118
                tx.XtraState.underline = 0
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   119
                spacelen = tx._canvas.stringWidth(' ', tx._fontname, tx._fontsize)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   120
                tx.XtraState.underlines.append( (tx.XtraState.underline_x, cur_x-spacelen) )
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   121
            cur_x = cur_x + txtlen
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   122
    if tx.XtraState.underline:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   123
        tx.XtraState.underlines.append( (tx.XtraState.underline_x, cur_x) )
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   124
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   125
def _leftDrawParaLineX( tx, offset, line, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   126
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   127
    _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   128
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   129
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   130
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   131
def _centerDrawParaLineX( tx, offset, line, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   132
    m = offset+0.5*line.extraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   133
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   134
    _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   135
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   136
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   137
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   138
def _rightDrawParaLineX( tx, offset, line, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   139
    m = offset+line.extraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   140
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   141
    _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   142
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   143
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   144
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   145
def _justifyDrawParaLineX( tx, offset, line, last=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   146
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   147
    if last:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   148
        #last one, left align
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   149
        _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   150
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   151
        nSpaces = line.wordCount - 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   152
        if nSpaces:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   153
            tx.setWordSpace(line.extraSpace / float(nSpaces))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   154
            _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   155
            tx.setWordSpace(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   156
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   157
            _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   158
    setXPos(tx,-offset)
2183
324abf21b7e5 Fix missing return
rgbecker
parents: 2182
diff changeset
   159
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   160
1373
df3383c5f8bc Added accelerated _sameFrag
rgbecker
parents: 1160
diff changeset
   161
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   162
    from _rl_accel import _sameFrag
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   163
except ImportError:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   164
    try:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   165
        from reportlab.lib._rl_accel import _sameFrag
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   166
    except ImportError:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   167
        def _sameFrag(f,g):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   168
            'returns 1 if two ParaFrags map out the same'
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   169
            if hasattr(f,'cbDefn') or hasattr(g,'cbDefn'): return 0
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   170
            for a in ('fontName', 'fontSize', 'textColor', 'rise', 'underline'):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   171
                if getattr(f,a)!=getattr(g,a): return 0
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   172
            return 1
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   173
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   174
def _getFragWords(frags):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   175
    ''' given a Parafrag list return a list of fragwords
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   176
        [[size, (f00,w00), ..., (f0n,w0n)],....,[size, (fm0,wm0), ..., (f0n,wmn)]]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   177
        each pair f,w represents a style and some string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   178
        each sublist represents a word
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   179
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   180
    R = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   181
    W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   182
    n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   183
    for f in frags:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   184
        text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   185
        #del f.text # we can't do this until we sort out splitting
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   186
                    # of paragraphs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   187
        if text!='':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   188
            S = split(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   189
            if S==[]: S = ['']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   190
            if W!=[] and text[0] in whitespace:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   191
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   192
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   193
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   194
                n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   195
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   196
            for w in S[:-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   197
                W.append((f,w))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   198
                n = n + stringWidth(w, f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   199
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   200
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   201
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   202
                n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   203
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   204
            w = S[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   205
            W.append((f,w))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   206
            n = n + stringWidth(w, f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   207
            if text[-1] in whitespace:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   208
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   209
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   210
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   211
                n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   212
        elif hasattr(f,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   213
            W.append((f,''))
508
37dc6fc3fa65 More <onDraw> hacks
rgbecker
parents: 506
diff changeset
   214
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   215
    if W!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   216
        W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   217
        R.append(W)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   218
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   219
    return R
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   220
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   221
def _split_blParaSimple(blPara,start,stop):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   222
    f = blPara.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   223
    for a in ('lines', 'kind', 'text'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   224
        if hasattr(f,a): delattr(f,a)
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   225
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   226
    f.words = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   227
    for l in blPara.lines[start:stop]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   228
        for w in l[1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   229
            f.words.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   230
    return [f]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   231
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   232
def _split_blParaHard(blPara,start,stop):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   233
    f = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   234
    lines = blPara.lines[start:stop]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   235
    for l in lines:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   236
        for w in l.words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   237
            f.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   238
        if l is not lines[-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   239
            i = len(f)-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   240
            while hasattr(f[i],'cbDefn'): i = i-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   241
            g = f[i]
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   242
            if g.text and g.text[-1]!=' ': g.text = g.text+' '
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   243
    return f
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   244
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   245
def _drawBullet(canvas, offset, cur_y, bulletText, style):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   246
    '''draw a bullet text could be a simple string or a frag list'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   247
    tx2 = canvas.beginText(style.bulletIndent, cur_y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   248
    tx2.setFont(style.bulletFontName, style.bulletFontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   249
    tx2.setFillColor(hasattr(style,'bulletColor') and style.bulletColor or style.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   250
    if type(bulletText) is StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   251
        tx2.textOut(bulletText)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   252
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   253
        for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   254
            tx2.setFont(f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   255
            tx2.setFillColor(f.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   256
            tx2.textOut(f.text)
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   257
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   258
    canvas.drawText(tx2)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   259
    #AR making definition lists a bit less ugly
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   260
    #bulletEnd = tx2.getX()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   261
    bulletEnd = tx2.getX() + style.bulletFontSize * 0.6
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   262
    offset = max(offset,bulletEnd - style.leftIndent)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   263
    return offset
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   264
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   265
def _handleBulletWidth(bulletText,style,maxWidths):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   266
    '''work out bullet width and adjust maxWidths[0] if neccessary
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   267
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   268
    if bulletText <> None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   269
        if type(bulletText) is StringType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   270
            bulletWidth = stringWidth( bulletText, style.bulletFontName, style.bulletFontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   271
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   272
            #it's a list of fragments
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   273
            bulletWidth = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   274
            for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   275
                bulletWidth = bulletWidth + stringWidth(f.text, f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   276
        bulletRight = style.bulletIndent + bulletWidth + 0.6 * style.bulletFontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   277
        indent = style.leftIndent+style.firstLineIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   278
        if bulletRight > indent:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   279
            #..then it overruns, and we have less space available on line 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   280
            maxWidths[0] = maxWidths[0] - (bulletRight - indent)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   281
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   282
def splitLines0(frags,widths):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   283
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   284
    given a list of ParaFrags we return a list of ParaLines
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   285
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   286
    each ParaLine has
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   287
    1)  ExtraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   288
    2)  blankCount
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   289
    3)  [textDefns....]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   290
        each text definition is a (ParaFrag, start, limit) triplet
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   291
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   292
    #initialise the algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   293
    lines   = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   294
    lineNum = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   295
    maxW    = widths[lineNum]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   296
    i       = -1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   297
    l       = len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   298
    lim     = start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   299
    while 1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   300
        #find a non whitespace character
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   301
        while i<l:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   302
            while start<lim and text[start]==' ': start=start+1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   303
            if start==lim:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   304
                i = i + 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   305
                if i==l: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   306
                start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   307
                f = frags[i]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   308
                text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   309
                lim = len(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   310
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   311
                break   # we found one
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
   312
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   313
        if start==lim: break    #if we didn't find one we are done
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
   314
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   315
        #start of a line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   316
        g       = (None,None,None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   317
        line    = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   318
        cLen    = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   319
        nSpaces = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   320
        while cLen<maxW:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   321
            j = find(text,' ',start)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   322
            if j<0: j==lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   323
            w = stringWidth(text[start:j],f.fontName,f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   324
            cLen = cLen + w
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   325
            if cLen>maxW and line!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   326
                cLen = cLen-w
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   327
                #this is the end of the line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   328
                while g.text[lim]==' ':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   329
                    lim = lim - 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   330
                    nSpaces = nSpaces-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   331
                break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   332
            if j<0: j = lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   333
            if g[0] is f: g[2] = j  #extend
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   334
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   335
                g = (f,start,j)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   336
                line.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   337
            if j==lim:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   338
                i=i+1
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   339
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   340
def _do_under_lines(i, t_off, tx):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   341
    y = tx.XtraState.cur_y - i*tx.XtraState.style.leading - tx.XtraState.f.fontSize/8.0 # 8.0 factor copied from para.py
2211
7d7e9b66a4b6 Fix to <u> from Ulrich Screiner
rgbecker
parents: 2200
diff changeset
   342
    text = join(tx.XtraState.lines[i][1])
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   343
    textlen = tx._canvas.stringWidth(text, tx._fontname, tx._fontsize)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   344
    tx._canvas.line(t_off, y, t_off+textlen, y)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   345
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   346
def _do_under(i, t_off, tx):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   347
    y = tx.XtraState.cur_y - i*tx.XtraState.style.leading - tx.XtraState.f.fontSize/8.0 # 8.0 factor copied from para.py
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   348
    for x1,x2 in tx.XtraState.underlines:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   349
        tx._canvas.line(t_off+x1, y, t_off+x2, y)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   350
    tx.XtraState.underlines = []
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   351
    tx.XtraState.underline=0
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   352
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   353
class Paragraph(Flowable):
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   354
    """ Paragraph(text, style, bulletText=None, caseSensitive=1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   355
        text a string of stuff to go into the paragraph.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   356
        style is a style definition as in reportlab.lib.styles.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   357
        bulletText is an optional bullet defintion.
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   358
        caseSensitive set this to 0 if you want the markup tags and their attributes to be case-insensitive.
316
e15cf6a28eb8 Documentation fixes
rgbecker
parents: 302
diff changeset
   359
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   360
        This class is a flowable that can format a block of text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   361
        into a paragraph with a given style.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   362
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   363
        The paragraph Text can contain XML-like markup including the tags:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   364
        <b> ... </b> - bold
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   365
        <i> ... </i> - italics
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   366
        <u> ... </u> - underline
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   367
        <super> ... </super> - superscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   368
        <sub> ... </sub> - subscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   369
        <font name=fontfamily/fontname color=colorname size=float>
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   370
        <onDraw name=callable label="a label">
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   371
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   372
        The whole may be surrounded by <para> </para> tags
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   373
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   374
        It will also be able to handle any MathML specified Greek characters.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   375
    """
2341
78400fba14ad Removed unicode changes
rgbecker
parents: 2332
diff changeset
   376
    def __init__(self, text, style, bulletText = None, frags=None, caseSensitive=1):
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2183
diff changeset
   377
        self.caseSensitive = caseSensitive
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   378
        self._setup(text, style, bulletText, frags, cleanBlockQuotedText)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   379
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 540
diff changeset
   380
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   381
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   382
        import string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   383
        n = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   384
        L = [n+"("]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   385
        keys = self.__dict__.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   386
        for k in keys:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   387
            v = getattr(self, k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   388
            rk = repr(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   389
            rv = repr(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   390
            rk = "  "+string.replace(rk, "\n", "\n  ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   391
            rv = "    "+string.replace(rv, "\n", "\n    ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   392
            L.append(rk)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   393
            L.append(rv)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   394
        L.append(") #"+n)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   395
        return string.join(L, "\n")
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 540
diff changeset
   396
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   397
    def _setup(self, text, style, bulletText, frags, cleaner):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   398
        if frags is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   399
            text = cleaner(text)
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   400
            _parser.caseSensitive = self.caseSensitive
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   401
            style, frags, bulletTextFrags = _parser.parse(text,style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   402
            if frags is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   403
                raise "xml parser error (%s) in paragraph beginning\n'%s'"\
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   404
                    % (_parser.errors[0],text[:min(30,len(text))])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   405
            if bulletTextFrags: bulletText = bulletTextFrags
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 268
diff changeset
   406
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   407
        #AR hack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   408
        self.text = text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   409
        self.frags = frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   410
        self.style = style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   411
        self.bulletText = bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   412
        self.debug = 0  #turn this on to see a pretty one with all the margins etc.
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   413
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   414
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   415
        # work out widths array for breaking
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   416
        self.width = availWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   417
        leftIndent = self.style.leftIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   418
        first_line_width = availWidth - (leftIndent+self.style.firstLineIndent) - self.style.rightIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   419
        later_widths = availWidth - leftIndent - self.style.rightIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   420
        self.blPara = self.breakLines([first_line_width, later_widths])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   421
        self.height = len(self.blPara.lines) * self.style.leading
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   422
        return (self.width, self.height)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   423
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   424
    def minWidth(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   425
        'Attempt to determine a minimum sensible width'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   426
        frags = self.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   427
        nFrags= len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   428
        if nFrags==1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   429
            f = frags[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   430
            fS = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   431
            fN = f.fontName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   432
            words = hasattr(f,'text') and split(f.text, ' ') or f.words
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   433
            func = lambda w, fS=fS, fN=fN: stringWidth(w,fN,fS)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   434
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   435
            words = _getFragWords(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   436
            func  = lambda x: x[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   437
        return max(map(func,words))
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 938
diff changeset
   438
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   439
    def _get_split_blParaFunc(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   440
        return self.blPara.kind==0 and _split_blParaSimple or _split_blParaHard
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   441
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   442
    def split(self,availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   443
        if len(self.frags)<=0: return []
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   444
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   445
        #the split information is all inside self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   446
        if not hasattr(self,'blPara'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   447
            self.wrap(availWidth,availHeight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   448
        blPara = self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   449
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   450
        leading = style.leading
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   451
        lines = blPara.lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   452
        n = len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   453
        s = int(availHeight/leading)
1737
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   454
        if s<=1:
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   455
            del self.blPara
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   456
            return []
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   457
        if n<=s: return [self]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   458
        func = self._get_split_blParaFunc()
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   459
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   460
        P1=self.__class__(None,style,bulletText=self.bulletText,frags=func(blPara,0,s))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   461
        #this is a major hack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   462
        P1.blPara = ParaLines(kind=1,lines=blPara.lines[0:s],aH=availHeight,aW=availWidth)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   463
        P1._JustifyLast = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   464
        if style.firstLineIndent != 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   465
            style = deepcopy(style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   466
            style.firstLineIndent = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   467
        P2=self.__class__(None,style,bulletText=None,frags=func(blPara,s,n))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   468
        return [P1,P2]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   469
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   470
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   471
        #call another method for historical reasons.  Besides, I
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   472
        #suspect I will be playing with alternate drawing routines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   473
        #so not doing it here makes it easier to switch.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   474
        self.drawPara(self.debug)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   475
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   476
    def breakLines(self, width):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   477
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   478
        Returns a broken line structure. There are two cases
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   479
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   480
        A) For the simple case of a single formatting input fragment the output is
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   481
            A fragment specifier with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   482
                kind = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   483
                fontName, fontSize, leading, textColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   484
                lines=  A list of lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   485
                        Each line has two items.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   486
                        1) unused width in points
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   487
                        2) word list
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   488
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   489
        B) When there is more than one input formatting fragment the out put is
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   490
            A fragment specifier with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   491
                kind = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   492
                lines=  A list of fragments each having fields
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   493
                            extraspace (needed for justified)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   494
                            fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   495
                            words=word list
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   496
                                each word is itself a fragment with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   497
                                various settings
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   498
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   499
        This structure can be used to easily draw paragraphs with the various alignments.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   500
        You can supply either a single width or a list of widths; the latter will have its
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   501
        last item repeated until necessary. A 2-element list is useful when there is a
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   502
        different first line indent; a longer list could be created to facilitate custom wraps
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   503
        around irregular objects."""
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   504
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   505
        if type(width) <> ListType: maxWidths = [width]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   506
        else: maxWidths = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   507
        lines = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   508
        lineno = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   509
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   510
        fFontSize = float(style.fontSize)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   511
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   512
        #for bullets, work out width and ensure we wrap the right amount onto line one
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   513
        _handleBulletWidth(self.bulletText,style,maxWidths)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   514
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   515
        maxWidth = maxWidths[0]
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
   516
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   517
        self.height = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   518
        frags = self.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   519
        nFrags= len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   520
        if nFrags==1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   521
            f = frags[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   522
            fontSize = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   523
            fontName = f.fontName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   524
            words = hasattr(f,'text') and split(f.text, ' ') or f.words
2341
78400fba14ad Removed unicode changes
rgbecker
parents: 2332
diff changeset
   525
            spaceWidth = stringWidth(' ', fontName, fontSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   526
            cLine = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   527
            currentWidth = - spaceWidth   # hack to get around extra space for word 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   528
            for word in words:
2341
78400fba14ad Removed unicode changes
rgbecker
parents: 2332
diff changeset
   529
                wordWidth = stringWidth(word, fontName, fontSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   530
                newWidth = currentWidth + spaceWidth + wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   531
                if newWidth<=maxWidth or len(cLine)==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   532
                    # fit one more on this line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   533
                    cLine.append(word)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   534
                    currentWidth = newWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   535
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   536
                    if currentWidth>self.width: self.width = currentWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   537
                    #end of line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   538
                    lines.append((maxWidth - currentWidth, cLine))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   539
                    cLine = [word]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   540
                    currentWidth = wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   541
                    lineno = lineno + 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   542
                    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   543
                        maxWidth = maxWidths[lineno]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   544
                    except IndexError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   545
                        maxWidth = maxWidths[-1]  # use the last one
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   546
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   547
            #deal with any leftovers on the final line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   548
            if cLine!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   549
                if currentWidth>self.width: self.width = currentWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   550
                lines.append((maxWidth - currentWidth, cLine))
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   551
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   552
            return f.clone(kind=0, lines=lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   553
        elif nFrags<=0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   554
            return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   555
                            textColor=style.textColor, lines=[])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   556
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   557
            if hasattr(self,'blPara'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   558
                #NB this is an utter hack that awaits the proper information
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   559
                #preserving splitting algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   560
                return self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   561
            n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   562
            nSp = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   563
            for w in _getFragWords(frags):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   564
                spaceWidth = stringWidth(' ',w[-1][0].fontName, w[-1][0].fontSize)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   565
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   566
                if n==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   567
                    currentWidth = -spaceWidth   # hack to get around extra space for word 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   568
                    words = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   569
                    maxSize = 0
496
bb47cf5c2739 Layout error fixes
rgbecker
parents: 494
diff changeset
   570
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   571
                wordWidth = w[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   572
                f = w[1][0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   573
                if wordWidth>0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   574
                    newWidth = currentWidth + spaceWidth + wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   575
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   576
                    newWidth = currentWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   577
                if newWidth<=maxWidth or n==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   578
                    # fit one more on this line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   579
                    n = n + 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   580
                    maxSize = max(maxSize,f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   581
                    nText = w[1][1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   582
                    if words==[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   583
                        g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   584
                        words = [g]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   585
                        g.text = nText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   586
                    elif not _sameFrag(g,f):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   587
                        if currentWidth>0 and ((nText!='' and nText[0]!=' ') or hasattr(f,'cbDefn')):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   588
                            if hasattr(g,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   589
                                i = len(words)-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   590
                                while hasattr(words[i],'cbDefn'): i = i-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   591
                                words[i].text = words[i].text + ' '
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   592
                            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   593
                                g.text = g.text + ' '
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   594
                            nSp = nSp + 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   595
                        g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   596
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   597
                        g.text = nText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   598
                    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   599
                        if nText!='' and nText[0]!=' ':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   600
                            g.text = g.text + ' ' + nText
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   601
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   602
                    for i in w[2:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   603
                        g = i[0].clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   604
                        g.text=i[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   605
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   606
                        maxSize = max(maxSize,g.fontSize)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   607
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   608
                    currentWidth = newWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   609
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   610
                    if currentWidth>self.width: self.width = currentWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   611
                    #end of line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   612
                    lines.append(FragLine(extraSpace=(maxWidth - currentWidth),wordCount=n,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   613
                                        words=words, fontSize=maxSize))
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   614
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   615
                    #start new line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   616
                    lineno = lineno + 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   617
                    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   618
                        maxWidth = maxWidths[lineno]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   619
                    except IndexError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   620
                        maxWidth = maxWidths[-1]  # use the last one
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   621
                    currentWidth = wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   622
                    n = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   623
                    maxSize = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   624
                    g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   625
                    words = [g]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   626
                    g.text = w[1][1]
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   627
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   628
                    for i in w[2:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   629
                        g = i[0].clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   630
                        g.text=i[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   631
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   632
                        maxSize = max(maxSize,g.fontSize)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   633
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   634
            #deal with any leftovers on the final line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   635
            if words<>[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   636
                if currentWidth>self.width: self.width = currentWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   637
                lines.append(ParaLines(extraSpace=(maxWidth - currentWidth),wordCount=n,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   638
                                    words=words, fontSize=maxSize))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   639
            return ParaLines(kind=1, lines=lines)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   640
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   641
        return lines
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   642
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   643
    def beginText(self, x, y):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   644
        return self.canv.beginText(x, y)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   645
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   646
    def drawPara(self,debug=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   647
        """Draws a paragraph according to the given style.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   648
        Returns the final y position at the bottom. Not safe for
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   649
        paragraphs without spaces e.g. Japanese; wrapping
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   650
        algorithm will go infinite."""
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   651
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   652
        #stash the key facts locally for speed
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   653
        canvas = self.canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   654
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   655
        blPara = self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   656
        lines = blPara.lines
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   657
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   658
        #work out the origin for line 1
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   659
        leftIndent = style.leftIndent
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   660
        cur_x = leftIndent
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   661
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   662
        #if has a background, draw it
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   663
        if style.backColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   664
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   665
            canvas.setFillColor(style.backColor)
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   666
            canvas.rect(leftIndent,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   667
                        0,
2367
ff834913c5e2 fix backcolour bug
rgbecker
parents: 2366
diff changeset
   668
                        self.width - (leftIndent+style.rightIndent),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   669
                        self.height,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   670
                        fill=1,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   671
                        stroke=0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   672
            canvas.restoreState()
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   673
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   674
        if debug:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   675
            # This boxes and shades stuff to show how the paragraph
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   676
            # uses its space.  Useful for self-documentation so
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   677
            # the debug code stays!
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   678
            # box the lot
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   679
            canvas.rect(0, 0, self.width, self.height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   680
            #left and right margins
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   681
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   682
            canvas.setFillColor(Color(0.9,0.9,0.9))
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   683
            canvas.rect(0, 0, leftIndent, self.height)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   684
            canvas.rect(self.width - style.rightIndent, 0, style.rightIndent, self.height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   685
            # shade above and below
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   686
            canvas.setFillColor(Color(1.0,1.0,0.0))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   687
            canvas.restoreState()
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   688
            #self.drawLine(x + leftIndent, y, x + leftIndent, cur_y)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   689
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   690
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   691
        nLines = len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   692
        bulletText = self.bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   693
        if nLines > 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   694
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   695
            #canvas.addLiteral('%% %s.drawPara' % _className(self))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   696
            alignment = style.alignment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   697
            offset = style.firstLineIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   698
            lim = nLines-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   699
            noJustifyLast = not (hasattr(self,'_JustifyLast') and self._JustifyLast)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   700
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   701
            if blPara.kind==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   702
                if alignment == TA_LEFT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   703
                    dpl = _leftDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   704
                elif alignment == TA_CENTER:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   705
                    dpl = _centerDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   706
                elif self.style.alignment == TA_RIGHT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   707
                    dpl = _rightDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   708
                elif self.style.alignment == TA_JUSTIFY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   709
                    dpl = _justifyDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   710
                f = blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   711
                cur_y = self.height - f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   712
                if bulletText <> None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   713
                    offset = _drawBullet(canvas,offset,cur_y,bulletText,style)
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   714
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   715
                #set up the font etc.
2400
f47d5c6d8fcd paragraph.py: use canvas.setFillColor
rgbecker
parents: 2367
diff changeset
   716
                canvas.setFillColor(f.textColor)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   717
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   718
                tx = self.beginText(cur_x, cur_y)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   719
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   720
                #now the font for the rest of the paragraph
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   721
                tx.setFont(f.fontName, f.fontSize, style.leading)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   722
                t_off = dpl( tx, offset, lines[0][0], lines[0][1], noJustifyLast and nLines==1)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   723
                if f.underline:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   724
                    tx.XtraState=ABag()
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   725
                    tx.XtraState.cur_y = cur_y
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   726
                    tx.XtraState.f = f
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   727
                    tx.XtraState.style = style
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   728
                    tx.XtraState.lines = lines
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   729
                    _do_under_lines(0, t_off+leftIndent, tx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   730
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   731
                    #now the middle of the paragraph, aligned with the left margin which is our origin.
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   732
                    for i in range(1, nLines):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   733
                        t_off = dpl( tx, 0, lines[i][0], lines[i][1], noJustifyLast and i==lim)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   734
                        if f.underline:
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   735
                            _do_under_lines(i, t_off+leftIndent, tx)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   736
                else:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   737
                    for i in range(1, nLines):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   738
                        dpl( tx, 0, lines[i][0], lines[i][1], noJustifyLast and i==lim)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   739
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   740
                f = lines[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   741
                cur_y = self.height - f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   742
                # default?
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   743
                dpl = _leftDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   744
                if bulletText <> None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   745
                    offset = _drawBullet(canvas,offset,cur_y,bulletText,style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   746
                if alignment == TA_LEFT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   747
                    dpl = _leftDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   748
                elif alignment == TA_CENTER:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   749
                    dpl = _centerDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   750
                elif self.style.alignment == TA_RIGHT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   751
                    dpl = _rightDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   752
                elif self.style.alignment == TA_JUSTIFY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   753
                    dpl = _justifyDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   754
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   755
                    raise ValueError, "bad align %s" % repr(alignment)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   756
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   757
                #set up the font etc.
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   758
                tx = self.beginText(cur_x, cur_y)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   759
                tx.XtraState=ABag()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   760
                tx.XtraState.textColor=None
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   761
                tx.XtraState.rise=0
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   762
                tx.XtraState.underline=0
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   763
                tx.XtraState.underlines=[]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   764
                tx.setLeading(style.leading)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   765
                tx.XtraState.cur_y = cur_y
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   766
                tx.XtraState.f = f
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   767
                tx.XtraState.style = style
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   768
                #f = lines[0].words[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   769
                #tx._setFont(f.fontName, f.fontSize)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   770
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   771
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   772
                tx._fontname,tx._fontsize = None, None
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   773
                t_off = dpl( tx, offset, lines[0], noJustifyLast and nLines==1)
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   774
                _do_under(0, t_off+leftIndent, tx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   775
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   776
                #now the middle of the paragraph, aligned with the left margin which is our origin.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   777
                for i in range(1, nLines):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   778
                    f = lines[i]
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   779
                    t_off = dpl( tx, 0, f, noJustifyLast and i==lim)
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   780
                    _do_under(i, t_off+leftIndent, tx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   781
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   782
            canvas.drawText(tx)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   783
            canvas.restoreState()
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   784
2172
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   785
    def getPlainText(self,identify=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   786
        """Convenience function for templates which want access
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   787
        to the raw text, without XML tags. """
2172
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   788
        frags = getattr(self,'frags',None)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   789
        if frags:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   790
            plains = []
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   791
            for frag in frags:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   792
                plains.append(frag.text)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   793
            return join(plains, '')
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   794
        elif identify:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   795
            text = getattr(self,'text',None)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   796
            if text is None: text = repr(self)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   797
            return text
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   798
        else:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
   799
            return ''
302
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   800
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   801
    def getActualLineWidths0(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   802
        """Convenience function; tells you how wide each line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   803
        actually is.  For justified styles, this will be
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   804
        the same as the wrap width; for others it might be
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   805
        useful for seeing if paragraphs will fit in spaces."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   806
        assert hasattr(self, 'width'), "Cannot call this method before wrap()"
1752
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
   807
        if self.blPara.kind:
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
   808
            func = lambda frag, w=self.width: w - frag.extraSpace
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
   809
        else:
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
   810
            func = lambda frag, w=self.width: w - frag[0]
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
   811
        return map(func,self.blPara.lines)
497
63f575fc1170 Added experimental method getActualLineWidths0
andy_robinson
parents: 496
diff changeset
   812
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   813
if __name__=='__main__':    #NORUNTESTS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   814
    def dumpParagraphLines(P):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   815
        print 'dumpParagraphLines(<Paragraph @ %d>)' % id(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   816
        lines = P.blPara.lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   817
        n =len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   818
        for l in range(n):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   819
            line = lines[l]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   820
            if hasattr(line,'words'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   821
                words = line.words
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   822
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   823
                words = line[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   824
            nwords = len(words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   825
            print 'line%d: %d(%s)\n  ' % (l,nwords,str(getattr(line,'wordCount','Unknown'))),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   826
            for w in range(nwords):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   827
                print "%d:'%s'"%(w,getattr(words[w],'text',words[w])),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   828
            print
302
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   829
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   830
    def dumpParagraphFrags(P):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   831
        print 'dumpParagraphFrags(<Paragraph @ %d>) minWidth() = %.2f' % (id(P), P.minWidth())
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   832
        frags = P.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   833
        n =len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   834
        for l in range(n):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   835
            print "frag%d: '%s'" % (l, frags[l].text)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   836
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   837
        l = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   838
        cum = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   839
        for W in _getFragWords(frags):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   840
            cum = cum + W[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   841
            print "fragword%d: cum=%3d size=%d" % (l, cum, W[0]),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   842
            for w in W[1:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   843
                print "'%s'" % w[1],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   844
            print
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   845
            l = l + 1
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 383
diff changeset
   846
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 938
diff changeset
   847
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   848
    from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   849
    import sys
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   850
    TESTS = sys.argv[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   851
    if TESTS==[]: TESTS=['4']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   852
    def flagged(i,TESTS=TESTS):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   853
        return 'all' in TESTS or '*' in TESTS or str(i) in TESTS
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 938
diff changeset
   854
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   855
    styleSheet = getSampleStyleSheet()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   856
    B = styleSheet['BodyText']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   857
    style = ParagraphStyle("discussiontext", parent=B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   858
    style.fontName= 'Helvetica'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   859
    if flagged(1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   860
        text='''The <font name=courier color=green>CMYK</font> or subtractive method follows the way a printer
302
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   861
mixes three pigments (cyan, magenta, and yellow) to form colors.
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   862
Because mixing chemicals is more difficult than combining light there
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   863
is a fourth parameter for darkness.  For example a chemical
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   864
combination of the <font name=courier color=green>CMY</font> pigments generally never makes a perfect
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   865
black -- instead producing a muddy color -- so, to get black printers
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   866
don't use the <font name=courier color=green>CMY</font> pigments but use a direct black ink.  Because
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   867
<font name=courier color=green>CMYK</font> maps more directly to the way printer hardware works it may
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 383
diff changeset
   868
be the case that &amp;| &amp; | colors specified in <font name=courier color=green>CMYK</font> will provide better fidelity
302
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   869
and better control when printed.
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
   870
'''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   871
        P=Paragraph(text,style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   872
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   873
        aW, aH = 456.0, 42.8
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   874
        w,h = P.wrap(aW, aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   875
        dumpParagraphLines(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   876
        S = P.split(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   877
        for s in S:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   878
            s.wrap(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   879
            dumpParagraphLines(s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   880
            aH = 500
488
a270e8b45200 Added J Alet's bug case
rgbecker
parents: 471
diff changeset
   881
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   882
    if flagged(2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   883
        P=Paragraph("""Price<super><font color="red">*</font></super>""", styleSheet['Normal'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   884
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   885
        w,h = P.wrap(24, 200)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   886
        dumpParagraphLines(P)
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   887
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   888
    if flagged(3):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   889
        text = """Dieses Kapitel bietet eine schnelle <b><font color=red>Programme :: starten</font></b>
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   890
<onDraw name=myIndex label="Programme :: starten">
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   891
<b><font color=red>Eingabeaufforderung :: (&gt;&gt;&gt;)</font></b>
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   892
<onDraw name=myIndex label="Eingabeaufforderung :: (&gt;&gt;&gt;)">
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   893
<b><font color=red>&gt;&gt;&gt; (Eingabeaufforderung)</font></b>
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   894
<onDraw name=myIndex label="&gt;&gt;&gt; (Eingabeaufforderung)">
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 553
diff changeset
   895
Einf\374hrung in Python <b><font color=red>Python :: Einf\374hrung</font></b>
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 553
diff changeset
   896
<onDraw name=myIndex label="Python :: Einf\374hrung">.
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   897
Das Ziel ist, die grundlegenden Eigenschaften von Python darzustellen, ohne
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   898
sich zu sehr in speziellen Regeln oder Details zu verstricken. Dazu behandelt
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 553
diff changeset
   899
dieses Kapitel kurz die wesentlichen Konzepte wie Variablen, Ausdr\374cke,
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   900
Kontrollfluss, Funktionen sowie Ein- und Ausgabe. Es erhebt nicht den Anspruch,
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
   901
umfassend zu sein."""
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   902
        P=Paragraph(text, styleSheet['Code'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   903
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   904
        w,h = P.wrap(6*72, 9.7*72)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   905
        dumpParagraphLines(P)
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
   906
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   907
    if flagged(4):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   908
        text='''Die eingebaute Funktion <font name=Courier>range(i, j [, stride])</font><onDraw name=myIndex label="eingebaute Funktionen::range()"><onDraw name=myIndex label="range() (Funktion)"><onDraw name=myIndex label="Funktionen::range()"> erzeugt eine Liste von Ganzzahlen und f\374llt sie mit Werten <font name=Courier>k</font>, f\374r die gilt: <font name=Courier>i &lt;= k &lt; j</font>. Man kann auch eine optionale Schrittweite angeben. Die eingebaute Funktion <font name=Courier>xrange()</font><onDraw name=myIndex label="eingebaute Funktionen::xrange()"><onDraw name=myIndex label="xrange() (Funktion)"><onDraw name=myIndex label="Funktionen::xrange()"> erf\374llt einen \344hnlichen Zweck, gibt aber eine unver\344nderliche Sequenz vom Typ <font name=Courier>XRangeType</font><onDraw name=myIndex label="XRangeType"> zur\374ck. Anstatt alle Werte in der Liste abzuspeichern, berechnet diese Liste ihre Werte, wann immer sie angefordert werden. Das ist sehr viel speicherschonender, wenn mit sehr langen Listen von Ganzzahlen gearbeitet wird. <font name=Courier>XRangeType</font> kennt eine einzige Methode, <font name=Courier>s.tolist()</font><onDraw name=myIndex label="XRangeType::tolist() (Methode)"><onDraw name=myIndex label="s.tolist() (Methode)"><onDraw name=myIndex label="Methoden::s.tolist()">, die seine Werte in eine Liste umwandelt.'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   909
        aW = 420
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   910
        aH = 64.4
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   911
        P=Paragraph(text, B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   912
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   913
        w,h = P.wrap(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   914
        print 'After initial wrap',w,h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   915
        dumpParagraphLines(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   916
        S = P.split(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   917
        dumpParagraphFrags(S[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   918
        w0,h0 = S[0].wrap(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   919
        print 'After split wrap',w0,h0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   920
        dumpParagraphLines(S[0])
683
fbd2b18742a5 Added test 5
rgbecker
parents: 681
diff changeset
   921
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   922
    if flagged(5):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   923
        text = '<para> %s <![CDATA[</font></b>& %s < >]]></para>' % (chr(163),chr(163))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   924
        P=Paragraph(text, styleSheet['Code'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   925
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   926
        w,h = P.wrap(6*72, 9.7*72)
1737
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   927
        dumpParagraphLines(P)
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   928
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   929
    if flagged(6):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   930
        for text in ['''Here comes <FONT FACE="Helvetica" SIZE="14pt">Helvetica 14</FONT> with <STRONG>strong</STRONG> <EM>emphasis</EM>.''',
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   931
                     '''Here comes <font face="Helvetica" size="14pt">Helvetica 14</font> with <Strong>strong</Strong> <em>emphasis</em>.''',
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   932
                     '''Here comes <font face="Courier" size="3cm">Courier 3cm</font> and normal again.''',
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   933
                     ]:
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   934
            P=Paragraph(text, styleSheet['Normal'], caseSensitive=0)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   935
            dumpParagraphFrags(P)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   936
            w,h = P.wrap(6*72, 9.7*72)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   937
            dumpParagraphLines(P)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2183
diff changeset
   938
1945
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
   939
    if flagged(7):
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
   940
        text = """<para align="CENTER" fontSize="24" leading="30"><b>Generated by:</b>\tDilbert</para>"""
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
   941
        P=Paragraph(text, styleSheet['Code'])
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
   942
        dumpParagraphFrags(P)
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
   943
        w,h = P.wrap(6*72, 9.7*72)
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
   944
        dumpParagraphLines(P)