reportlab/platypus/paragraph.py
author rgbecker
Mon, 29 Jan 2007 13:41:44 +0000
changeset 2745 0b44535fa1a5
parent 2744 9472eedb9702
child 2750 05229265aff3
permissions -rw-r--r--
paraparser/paragraph.py: fix comments
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$ '''
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
     5
from string import 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
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
     8
from reportlab.pdfbase.pdfmetrics import stringWidth, getFont
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
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    14
from reportlab.lib.textsplit import wordSplit
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
    15
from copy import deepcopy
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    16
from reportlab.lib.abag import ABag
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
    17
import re
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    18
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    19
#on UTF8 branch, split and strip must be unicode-safe!
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    20
def split(text, delim=' '):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    21
    if type(text) is str: text = text.decode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    22
    if type(delim) is str: delim = delim.decode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    23
    return [uword.encode('utf8') for uword in text.split(delim)]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    24
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    25
def strip(text):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    26
    if type(text) is str: text = text.decode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    27
    return text.strip().encode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    28
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    29
class ParaLines(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    30
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    31
    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
    32
        kind=0  Simple
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    33
        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
    34
        lines   [(extraSpace1,words1),....,(extraspaceN,wordsN)]
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    35
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    36
        kind==1 Complex
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    37
        lines   [FragLine1,...,FragLineN]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    38
    """
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    39
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    40
class FragLine(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    41
    """class FragLine contains a styled line (ie a line with more than one style)
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    42
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    43
    extraSpace  unused space for justification only
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    44
    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
    45
    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
    46
    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
    47
                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
    48
    """
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    49
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    50
#our one and only parser
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    51
# 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
    52
_parser=ParaParser()
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    53
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    54
def _lineClean(L):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    55
    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
    56
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
    57
def cleanBlockQuotedText(text,joiner=' '):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    58
    """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
    59
    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
    60
    (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
    61
    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
    62
    return join(L, joiner)
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    63
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    64
def setXPos(tx,dx):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    65
    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
    66
        tx.setXPos(dx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    67
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    68
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
    69
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    70
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    71
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    72
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    73
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    74
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
    75
    m = offset + 0.5 * extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    76
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    77
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    78
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    79
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    80
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    81
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
    82
    m = offset + extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    83
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    84
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    85
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    86
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    87
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    88
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
    89
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    90
    text  = join(words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    91
    if last:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    92
        #last one, left align
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    93
        tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    94
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    95
        nSpaces = len(words)-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    96
        if nSpaces:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    97
            tx.setWordSpace(extraspace / float(nSpaces))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    98
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    99
            tx.setWordSpace(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   100
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   101
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   102
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   103
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   104
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   105
def _putFragLine(tx,words):
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   106
    cur_x = 0
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   107
    xtraState = tx.XtraState
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   108
    ws = getattr(tx,'_wordSpace',0)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   109
    nSpaces = 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   110
    for f in words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   111
        if hasattr(f,'cbDefn'):
2744
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   112
            name = f.cbDefn.name
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   113
            kind = f.cbDefn.kind
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   114
            if kind=='anchor':
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   115
                tx._canvas.bookmarkHorizontal(name,cur_x,tx._y+tx._leading)
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   116
            else:
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   117
                func = getattr(tx._canvas,name,None)
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   118
                if not func:
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   119
                    raise AttributeError, "Missing %s callback attribute '%s'" % (kind,name)
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   120
                func(tx._canvas,kind,f.cbDefn.label)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   121
            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
   122
        else:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   123
            cur_x_s = cur_x + nSpaces*ws
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   124
            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
   125
                tx._setFont(f.fontName, f.fontSize)
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   126
            if xtraState.textColor!=f.textColor:
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   127
                xtraState.textColor = f.textColor
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   128
                tx.setFillColor(f.textColor)
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   129
            if xtraState.rise!=f.rise:
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   130
                xtraState.rise=f.rise
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   131
                tx.setRise(f.rise)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   132
            text = f.text
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   133
            tx._textOut(text,f is words[-1])    # cheap textOut
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   134
            if not xtraState.underline and f.underline:
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   135
                xtraState.underline = 1
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   136
                xtraState.underline_x = cur_x_s
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   137
                xtraState.underlineColor = f.textColor
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   138
            elif xtraState.underline:
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   139
                if not f.underline:
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   140
                    xtraState.underline = 0
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   141
                    xtraState.underlines.append( (xtraState.underline_x, cur_x_s, xtraState.underlineColor) )
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   142
                    xtraState.underlineColor = None
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   143
                elif xtraState.textColor!=xtraState.underlineColor:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   144
                    xtraState.underlines.append( (xtraState.underline_x, cur_x_s, xtraState.underlineColor) )
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   145
                    xtraState.underlineColor = xtraState.textColor
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   146
                    xtraState.underline_x = cur_x_s
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   147
            if not xtraState.strike and f.strike:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   148
                xtraState.strike = 1
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   149
                xtraState.strike_x = cur_x_s
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   150
                xtraState.strikeColor = f.textColor
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   151
            elif xtraState.strike:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   152
                if not f.strike:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   153
                    xtraState.strike = 0
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   154
                    xtraState.strikes.append( (xtraState.strike_x, cur_x_s, xtraState.strikeColor) )
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   155
                    xtraState.strikeColor = None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   156
                elif xtraState.textColor!=xtraState.strikeColor:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   157
                    xtraState.strikes.append( (xtraState.strike_x, cur_x_s, xtraState.strikeColor) )
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   158
                    xtraState.strikeColor = xtraState.textColor
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   159
                    xtraState.strike_x = cur_x_s
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   160
            if not xtraState.link and f.link:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   161
                xtraState.link = f.link
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   162
                xtraState.link_x = cur_x_s
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   163
            elif xtraState.link and f.link is not xtraState.link:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   164
                    xtraState.links.append( (xtraState.link_x, cur_x_s, xtraState.link) )
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   165
                    xtraState.link = None
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   166
            txtlen = tx._canvas.stringWidth(text, tx._fontname, tx._fontsize)
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   167
            cur_x += txtlen
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   168
            nSpaces += text.count(' ')
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   169
    cur_x_s = cur_x+(nSpaces-1)*ws
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   170
    if xtraState.underline:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   171
        xtraState.underlines.append( (xtraState.underline_x, cur_x_s, xtraState.underlineColor) )
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   172
    if xtraState.strike:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   173
        xtraState.strikes.append( (xtraState.strike_x, cur_x_s, xtraState.strikeColor) )
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   174
    if xtraState.link:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   175
        xtraState.links.append( (xtraState.link_x, cur_x_s, xtraState.link) )
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   176
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   177
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
   178
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   179
    _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   180
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   181
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   182
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   183
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
   184
    m = offset+0.5*line.extraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   185
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   186
    _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   187
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   188
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   189
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   190
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
   191
    m = offset+line.extraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   192
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   193
    _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   194
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   195
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   196
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   197
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
   198
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   199
    if last:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   200
        #last one, left align
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   201
        _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   202
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   203
        nSpaces = line.wordCount - 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   204
        if nSpaces:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   205
            tx.setWordSpace(line.extraSpace / float(nSpaces))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   206
            _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   207
            tx.setWordSpace(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   208
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   209
            _putFragLine(tx, line.words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   210
    setXPos(tx,-offset)
2183
324abf21b7e5 Fix missing return
rgbecker
parents: 2182
diff changeset
   211
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   212
1373
df3383c5f8bc Added accelerated _sameFrag
rgbecker
parents: 1160
diff changeset
   213
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   214
    from _rl_accel import _sameFrag
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   215
except ImportError:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   216
    try:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   217
        from reportlab.lib._rl_accel import _sameFrag
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   218
    except ImportError:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   219
        #if you modify this you need to modify _rl_accel RGB
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   220
        def _sameFrag(f,g):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   221
            'returns 1 if two ParaFrags map out the same'
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   222
            if (hasattr(f,'cbDefn') or hasattr(g,'cbDefn')
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   223
                    or hasattr(f,'lineBreak') or hasattr(g,'lineBreak')): return 0
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   224
            for a in ('fontName', 'fontSize', 'textColor', 'rise', 'underline', 'strike', 'link'):
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   225
                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
   226
            return 1
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   227
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   228
def _getFragWords(frags):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   229
    ''' 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
   230
        [[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
   231
        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
   232
        each sublist represents a word
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   233
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   234
    R = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   235
    W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   236
    n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   237
    for f in frags:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   238
        text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   239
        #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
   240
                    # of paragraphs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   241
        if text!='':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   242
            S = split(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   243
            if S==[]: S = ['']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   244
            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
   245
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   246
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   247
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   248
                n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   249
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   250
            for w in S[:-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   251
                W.append((f,w))
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   252
                n += stringWidth(w, f.fontName, f.fontSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   253
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   254
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   255
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   256
                n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   257
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   258
            w = S[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   259
            W.append((f,w))
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   260
            n += stringWidth(w, f.fontName, f.fontSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   261
            if text[-1] in whitespace:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   262
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   263
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   264
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   265
                n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   266
        elif hasattr(f,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   267
            W.append((f,''))
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   268
        elif hasattr(f, 'lineBreak'):
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   269
            #pass the frag through.  The line breaker will scan for it.
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   270
            if W!=[]:
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   271
                W.insert(0,n)
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   272
                R.append(W)
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   273
                W = []
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   274
                n = 0
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   275
            R.append([0,(f,'')])
508
37dc6fc3fa65 More <onDraw> hacks
rgbecker
parents: 506
diff changeset
   276
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   277
    if W!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   278
        W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   279
        R.append(W)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   280
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   281
    return R
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   282
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   283
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
   284
    f = blPara.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   285
    for a in ('lines', 'kind', 'text'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   286
        if hasattr(f,a): delattr(f,a)
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   287
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   288
    f.words = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   289
    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
   290
        for w in l[1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   291
            f.words.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   292
    return [f]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   293
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   294
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
   295
    f = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   296
    lines = blPara.lines[start:stop]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   297
    for l in lines:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   298
        for w in l.words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   299
            f.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   300
        if l is not lines[-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   301
            i = len(f)-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   302
            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
   303
            g = f[i]
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   304
            if g.text and g.text[-1]!=' ': g.text += ' '
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   305
    return f
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   306
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   307
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
   308
    '''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
   309
    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
   310
    tx2.setFont(style.bulletFontName, style.bulletFontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   311
    tx2.setFillColor(hasattr(style,'bulletColor') and style.bulletColor or style.textColor)
2646
d177c247184a platypus: eliminate StringType usages and cp1252 usage
rgbecker
parents: 2644
diff changeset
   312
    if isinstance(bulletText,basestring):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   313
        tx2.textOut(bulletText)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   314
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   315
        for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   316
            tx2.setFont(f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   317
            tx2.setFillColor(f.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   318
            tx2.textOut(f.text)
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   319
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   320
    canvas.drawText(tx2)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   321
    #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
   322
    #bulletEnd = tx2.getX()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   323
    bulletEnd = tx2.getX() + style.bulletFontSize * 0.6
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   324
    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
   325
    return offset
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   326
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   327
def _handleBulletWidth(bulletText,style,maxWidths):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   328
    '''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
   329
    '''
2646
d177c247184a platypus: eliminate StringType usages and cp1252 usage
rgbecker
parents: 2644
diff changeset
   330
    if bulletText!=None:
d177c247184a platypus: eliminate StringType usages and cp1252 usage
rgbecker
parents: 2644
diff changeset
   331
        if isinstance(bulletText,basestring):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   332
            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
   333
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   334
            #it's a list of fragments
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   335
            bulletWidth = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   336
            for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   337
                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
   338
        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
   339
        indent = style.leftIndent+style.firstLineIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   340
        if bulletRight > indent:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   341
            #..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
   342
            maxWidths[0] = maxWidths[0] - (bulletRight - indent)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   343
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   344
def splitLines0(frags,widths):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   345
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   346
    given a list of ParaFrags we return a list of ParaLines
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   347
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   348
    each ParaLine has
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   349
    1)  ExtraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   350
    2)  blankCount
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   351
    3)  [textDefns....]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   352
        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
   353
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   354
    #initialise the algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   355
    lines   = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   356
    lineNum = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   357
    maxW    = widths[lineNum]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   358
    i       = -1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   359
    l       = len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   360
    lim     = start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   361
    while 1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   362
        #find a non whitespace character
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   363
        while i<l:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   364
            while start<lim and text[start]==' ': start += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   365
            if start==lim:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   366
                i += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   367
                if i==l: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   368
                start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   369
                f = frags[i]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   370
                text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   371
                lim = len(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   372
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   373
                break   # we found one
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
   374
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   375
        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
   376
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   377
        #start of a line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   378
        g       = (None,None,None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   379
        line    = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   380
        cLen    = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   381
        nSpaces = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   382
        while cLen<maxW:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   383
            j = find(text,' ',start)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   384
            if j<0: j==lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   385
            w = stringWidth(text[start:j],f.fontName,f.fontSize)
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   386
            cLen += w
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   387
            if cLen>maxW and line!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   388
                cLen = cLen-w
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   389
                #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
   390
                while g.text[lim]==' ':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   391
                    lim = lim - 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   392
                    nSpaces = nSpaces-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   393
                break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   394
            if j<0: j = lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   395
            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
   396
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   397
                g = (f,start,j)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   398
                line.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   399
            if j==lim:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   400
                i += 1
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   401
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   402
def _do_under_line(i, t_off, ws, tx, lm=-0.125):
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   403
    y = tx.XtraState.cur_y - i*tx.XtraState.style.leading + lm*tx.XtraState.f.fontSize
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   404
    textlen = tx._canvas.stringWidth(join(tx.XtraState.lines[i][1]), tx._fontname, tx._fontsize)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   405
    tx._canvas.line(t_off, y, t_off+textlen+ws, y)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   406
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   407
_scheme_re = re.compile('^[a-zA-Z][-+a-zA-Z0-9]+$')
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   408
def _doLink(tx,link,rect):
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   409
    if type(link) is unicode:
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   410
        link = unicode.encode('utf8')
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   411
    parts = link.split(':',1)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   412
    scheme = len(parts)==2 and parts[0].lower() or ''
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   413
    if _scheme_re.match(scheme) and scheme!='document':
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   414
        kind=scheme.lower()=='pdf' and 'GoToR' or 'URI'
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   415
        if kind=='GoToR': link = parts[1]
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   416
        tx._canvas.linkURL(link, rect, relative=1, kind=kind)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   417
    else:
2744
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   418
        if link[0]=='#':
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   419
            link = link[1:]
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   420
            scheme=''
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   421
        tx._canvas.linkRect("", scheme!='document' and link or parts[1], rect, relative=1)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   422
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   423
def _do_link_line(i, t_off, ws, tx):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   424
    xs = tx.XtraState
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   425
    leading = xs.style.leading
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   426
    y = xs.cur_y - i*leading - xs.f.fontSize/8.0 # 8.0 factor copied from para.py
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   427
    text = join(xs.lines[i][1])
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   428
    textlen = tx._canvas.stringWidth(text, tx._fontname, tx._fontsize)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   429
    _doLink(tx, xs.link, (t_off, y, t_off+textlen+ws, y+leading))
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   430
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   431
def _do_post_text(i, t_off, tx):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   432
    xs = tx.XtraState
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   433
    leading = xs.style.leading
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   434
    ff = 0.125*xs.f.fontSize
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   435
    y0 = xs.cur_y - i*leading
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   436
    y = y0 - ff
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   437
    ulc = None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   438
    for x1,x2,c in xs.underlines:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   439
        if c!=ulc:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   440
            tx._canvas.setStrokeColor(c)
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   441
            ulc = c
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   442
        tx._canvas.line(t_off+x1, y, t_off+x2, y)
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   443
    xs.underlines = []
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   444
    xs.underline=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   445
    xs.underlineColor=None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   446
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   447
    ys = y0 + 2*ff
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   448
    ulc = None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   449
    for x1,x2,c in xs.strikes:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   450
        if c!=ulc:
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   451
            tx._canvas.setStrokeColor(c)
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   452
            ulc = c
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   453
        tx._canvas.line(t_off+x1, ys, t_off+x2, ys)
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   454
    xs.strikes = []
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   455
    xs.strike=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   456
    xs.strikeColor=None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   457
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   458
    yl = y + leading
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   459
    for x1,x2,link in xs.links:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   460
        tx._canvas.line(t_off+x1, y, t_off+x2, y)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   461
        _doLink(tx, link, (t_off+x1, y, t_off+x2, yl))
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   462
    xs.links = []
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   463
    xs.link=None
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   464
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   465
class Paragraph(Flowable):
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   466
    """ 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
   467
        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
   468
        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
   469
        bulletText is an optional bullet defintion.
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   470
        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
   471
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   472
        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
   473
        into a paragraph with a given style.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   474
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   475
        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
   476
        <b> ... </b> - bold
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   477
        <i> ... </i> - italics
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   478
        <u> ... </u> - underline
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   479
        <strike> ... </strike> - strike through
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   480
        <super> ... </super> - superscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   481
        <sub> ... </sub> - subscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   482
        <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
   483
        <onDraw name=callable label="a label">
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   484
        <link>link text</link>
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   485
            attributes of links 
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   486
                size/fontSize=num
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   487
                name/face/fontName=name
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   488
                fg/textColor/color=color
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   489
                backcolor/backColor/bgcolor=color
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   490
                dest/destination/target/href/link=target
2745
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   491
        <a>anchor text</a>
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   492
            attributes of anchors 
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   493
                fontSize=num
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   494
                fontName=name
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   495
                fg/textColor/color=color
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   496
                backcolor/backColor/bgcolor=color
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   497
                href=href
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   498
        <a name="anchorpoint"/>
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   499
        <unichar name="unicode character name"/>
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   500
        <unichar value="unicode code point"/>
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   501
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   502
        The whole may be surrounded by <para> </para> tags
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   503
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   504
        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
   505
    """
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   506
    def __init__(self, text, style, bulletText = None, frags=None, caseSensitive=1, encoding='utf8'):
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2183
diff changeset
   507
        self.caseSensitive = caseSensitive
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   508
        self.encoding = encoding
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   509
        self._setup(text, style, bulletText, frags, cleanBlockQuotedText)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   510
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   511
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   512
        import string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   513
        n = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   514
        L = [n+"("]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   515
        keys = self.__dict__.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   516
        for k in keys:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   517
            v = getattr(self, k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   518
            rk = repr(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   519
            rv = repr(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   520
            rk = "  "+string.replace(rk, "\n", "\n  ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   521
            rv = "    "+string.replace(rv, "\n", "\n    ")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   522
            L.append(rk)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   523
            L.append(rv)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   524
        L.append(") #"+n)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   525
        return string.join(L, "\n")
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 540
diff changeset
   526
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   527
    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
   528
        if frags is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   529
            text = cleaner(text)
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   530
            _parser.caseSensitive = self.caseSensitive
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   531
            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
   532
            if frags is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   533
                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
   534
                    % (_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
   535
            if bulletTextFrags: bulletText = bulletTextFrags
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 268
diff changeset
   536
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   537
        #AR hack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   538
        self.text = text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   539
        self.frags = frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   540
        self.style = style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   541
        self.bulletText = bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   542
        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
   543
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   544
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   545
        # work out widths array for breaking
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   546
        self.width = availWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   547
        leftIndent = self.style.leftIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   548
        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
   549
        later_widths = availWidth - leftIndent - self.style.rightIndent
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   550
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   551
        if self.style.wordWrap == 'CJK':
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   552
            #use Asian text wrap algorithm to break characters
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   553
            self.blPara = self.breakLinesCJK([first_line_width, later_widths])
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   554
        else:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   555
            self.blPara = self.breakLines([first_line_width, later_widths])
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   556
        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
   557
        return (self.width, self.height)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   558
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   559
    def minWidth(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   560
        '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
   561
        frags = self.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   562
        nFrags= len(frags)
2467
5630a2b21a90 tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents: 2456
diff changeset
   563
        if not nFrags: return 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   564
        if nFrags==1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   565
            f = frags[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   566
            fS = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   567
            fN = f.fontName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   568
            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
   569
            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
   570
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   571
            words = _getFragWords(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   572
            func  = lambda x: x[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   573
        return max(map(func,words))
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 938
diff changeset
   574
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   575
    def _get_split_blParaFunc(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   576
        return self.blPara.kind==0 and _split_blParaSimple or _split_blParaHard
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   577
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   578
    def split(self,availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   579
        if len(self.frags)<=0: return []
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   580
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   581
        #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
   582
        if not hasattr(self,'blPara'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   583
            self.wrap(availWidth,availHeight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   584
        blPara = self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   585
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   586
        leading = style.leading
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   587
        lines = blPara.lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   588
        n = len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   589
        s = int(availHeight/leading)
1737
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   590
        if s<=1:
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   591
            del self.blPara
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
   592
            return []
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   593
        if n<=s: return [self]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   594
        func = self._get_split_blParaFunc()
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   595
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   596
        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
   597
        #this is a major hack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   598
        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
   599
        P1._JustifyLast = 1
2529
dced304f8584 flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents: 2467
diff changeset
   600
        P1._splitpara = 1
2561
a2409b6627e6 reportlab: added FlowablesAndImage
rgbecker
parents: 2557
diff changeset
   601
        P1.height = s*leading
a2409b6627e6 reportlab: added FlowablesAndImage
rgbecker
parents: 2557
diff changeset
   602
        P1.width = availWidth
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   603
        if style.firstLineIndent != 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   604
            style = deepcopy(style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   605
            style.firstLineIndent = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   606
        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
   607
        return [P1,P2]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   608
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   609
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   610
        #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
   611
        #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
   612
        #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
   613
        self.drawPara(self.debug)
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
    def breakLines(self, width):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   616
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   617
        Returns a broken line structure. There are two cases
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   618
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   619
        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
   620
            A fragment specifier with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   621
                kind = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   622
                fontName, fontSize, leading, textColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   623
                lines=  A list of lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   624
                        Each line has two items.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   625
                        1) unused width in points
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   626
                        2) word list
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   627
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   628
        B) When there is more than one input formatting fragment the output is
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   629
            A fragment specifier with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   630
                kind = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   631
                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
   632
                            extraspace (needed for justified)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   633
                            fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   634
                            words=word list
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   635
                                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
   636
                                various settings
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   637
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   638
        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
   639
        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
   640
        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
   641
        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
   642
        around irregular objects."""
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   643
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   644
        if type(width) <> ListType: maxWidths = [width]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   645
        else: maxWidths = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   646
        lines = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   647
        lineno = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   648
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   649
        fFontSize = float(style.fontSize)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   650
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   651
        #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
   652
        _handleBulletWidth(self.bulletText,style,maxWidths)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   653
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   654
        maxWidth = maxWidths[0]
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
   655
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   656
        self.height = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   657
        frags = self.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   658
        nFrags= len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   659
        if nFrags==1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   660
            f = frags[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   661
            fontSize = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   662
            fontName = f.fontName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   663
            words = hasattr(f,'text') and split(f.text, ' ') or f.words
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   664
            spaceWidth = stringWidth(' ', fontName, fontSize, self.encoding)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   665
            cLine = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   666
            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
   667
            for word in words:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   668
                #this underscores my feeling that Unicode throughout would be easier!
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   669
                wordWidth = stringWidth(word, fontName, fontSize, self.encoding)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   670
                newWidth = currentWidth + spaceWidth + wordWidth
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   671
                if newWidth <= maxWidth or not len(cLine):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   672
                    # fit one more on this line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   673
                    cLine.append(word)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   674
                    currentWidth = newWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   675
                else:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   676
                    if currentWidth > self.width: self.width = currentWidth
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   677
                    #end of line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   678
                    lines.append((maxWidth - currentWidth, cLine))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   679
                    cLine = [word]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   680
                    currentWidth = wordWidth
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   681
                    lineno += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   682
                    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   683
                        maxWidth = maxWidths[lineno]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   684
                    except IndexError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   685
                        maxWidth = maxWidths[-1]  # use the last one
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   686
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   687
            #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
   688
            if cLine!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   689
                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
   690
                lines.append((maxWidth - currentWidth, cLine))
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   691
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   692
            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
   693
        elif nFrags<=0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   694
            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
   695
                            textColor=style.textColor, lines=[])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   696
        else:
2529
dced304f8584 flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents: 2467
diff changeset
   697
            if hasattr(self,'blPara') and getattr(self,'_splitpara',0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   698
                #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
   699
                #preserving splitting algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   700
                return self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   701
            n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   702
            for w in _getFragWords(frags):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   703
                spaceWidth = stringWidth(' ',w[-1][0].fontName, w[-1][0].fontSize)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   704
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   705
                if n==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   706
                    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
   707
                    words = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   708
                    maxSize = 0
496
bb47cf5c2739 Layout error fixes
rgbecker
parents: 494
diff changeset
   709
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   710
                wordWidth = w[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   711
                f = w[1][0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   712
                if wordWidth>0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   713
                    newWidth = currentWidth + spaceWidth + wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   714
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   715
                    newWidth = currentWidth
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   716
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   717
                #text to see if this frag is a line break. If it is and we will only act on it
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   718
                #if the current width is non-negative or the previous thing was a deliberate lineBreak
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   719
                lineBreak = hasattr(f,'lineBreak')
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
   720
                endLine = (newWidth>maxWidth and n>0) or lineBreak
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   721
                if not endLine:
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   722
                    if lineBreak: continue      #throw it away
2735
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2708
diff changeset
   723
                    nText = w[1][1]
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2708
diff changeset
   724
                    if nText: n += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   725
                    maxSize = max(maxSize,f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   726
                    if words==[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   727
                        g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   728
                        words = [g]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   729
                        g.text = nText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   730
                    elif not _sameFrag(g,f):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   731
                        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
   732
                            if hasattr(g,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   733
                                i = len(words)-1
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
   734
                                while hasattr(words[i],'cbDefn'): i -= 1
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   735
                                words[i].text += ' '
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   736
                            else:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   737
                                g.text += ' '
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   738
                        g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   739
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   740
                        g.text = nText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   741
                    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   742
                        if nText!='' and nText[0]!=' ':
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   743
                            g.text += ' ' + nText
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   744
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   745
                    for i in w[2:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   746
                        g = i[0].clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   747
                        g.text=i[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   748
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   749
                        maxSize = max(maxSize,g.fontSize)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   750
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   751
                    currentWidth = newWidth
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   752
                else:  #either it won't fit, or it's a lineBreak tag
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
   753
                    if lineBreak:
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   754
                        g = f.clone()
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
   755
                        #del g.lineBreak
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   756
                        words.append(g)
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   757
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   758
                    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
   759
                    #end of line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   760
                    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
   761
                                        words=words, fontSize=maxSize))
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   762
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   763
                    #start new line
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   764
                    lineno += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   765
                    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   766
                        maxWidth = maxWidths[lineno]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   767
                    except IndexError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   768
                        maxWidth = maxWidths[-1]  # use the last one
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   769
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   770
                    if lineBreak:
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   771
                        n = 0
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   772
                        continue
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   773
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   774
                    currentWidth = wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   775
                    n = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   776
                    maxSize = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   777
                    g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   778
                    words = [g]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   779
                    g.text = w[1][1]
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   780
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   781
                    for i in w[2:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   782
                        g = i[0].clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   783
                        g.text=i[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   784
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   785
                        maxSize = max(maxSize,g.fontSize)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   786
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   787
            #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
   788
            if words<>[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   789
                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
   790
                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
   791
                                    words=words, fontSize=maxSize))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   792
            return ParaLines(kind=1, lines=lines)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   793
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   794
        return lines
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   795
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   796
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   797
    def breakLinesCJK(self, width):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   798
        """Initially, the dumbest possible wrapping algorithm.
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   799
        Cannot handle font variations."""
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   800
2598
2dfd3107217c paragraph: fix misuse of style before setting in cjk breaklines
rgbecker
parents: 2594
diff changeset
   801
        style = self.style
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   802
        #for now we only handle one fragment.  Need to generalize this quickly.
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   803
        if len(self.frags) > 1:
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   804
            raise ValueError('CJK Wordwrap can only handle one fragment per paragraph for now')
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   805
        elif len(self.frags) == 0:
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   806
            return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   807
                            textColor=style.textColor, lines=[])
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   808
        f = self.frags[0]
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   809
        if 1 and hasattr(self,'blPara') and getattr(self,'_splitpara',0):
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   810
            #NB this is an utter hack that awaits the proper information
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   811
            #preserving splitting algorithm
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   812
            return f.clone(kind=0, lines=self.blPara.lines)
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   813
        if type(width)!=ListType: maxWidths = [width]
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   814
        else: maxWidths = width
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   815
        lines = []
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   816
        lineno = 0
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   817
        fFontSize = float(style.fontSize)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   818
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   819
        #for bullets, work out width and ensure we wrap the right amount onto line one
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   820
        _handleBulletWidth(self.bulletText, style, maxWidths)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   821
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   822
        maxWidth = maxWidths[0]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   823
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   824
        self.height = 0
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   825
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   826
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   827
        f = self.frags[0]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   828
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   829
        if hasattr(f,'text'):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   830
            text = f.text
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   831
        else:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   832
            text = ''.join(getattr(f,'words',[]))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   833
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   834
        from reportlab.lib.textsplit import wordSplit
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   835
        lines = wordSplit(text, maxWidths[0], f.fontName, f.fontSize)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   836
        #the paragraph drawing routine assumes multiple frags per line, so we need an
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   837
        #extra list like this
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   838
        #  [space, [text]]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   839
        #
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   840
        wrappedLines = [(sp, [line]) for (sp, line) in lines]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   841
        return f.clone(kind=0, lines=wrappedLines)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   842
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   843
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   844
    def beginText(self, x, y):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   845
        return self.canv.beginText(x, y)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   846
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   847
    def drawPara(self,debug=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   848
        """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
   849
        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
   850
        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
   851
        algorithm will go infinite."""
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   852
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   853
        #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
   854
        canvas = self.canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   855
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   856
        blPara = self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   857
        lines = blPara.lines
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   858
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   859
        #work out the origin for line 1
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   860
        leftIndent = style.leftIndent
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   861
        cur_x = leftIndent
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   862
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   863
        #if has a background, draw it
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   864
        if style.backColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   865
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   866
            canvas.setFillColor(style.backColor)
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   867
            canvas.rect(leftIndent,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   868
                        0,
2367
ff834913c5e2 fix backcolour bug
rgbecker
parents: 2366
diff changeset
   869
                        self.width - (leftIndent+style.rightIndent),
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   870
                        self.height,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   871
                        fill=1,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   872
                        stroke=0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   873
            canvas.restoreState()
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   874
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   875
        if debug:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   876
            # 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
   877
            # 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
   878
            # the debug code stays!
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   879
            # box the lot
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   880
            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
   881
            #left and right margins
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   882
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   883
            canvas.setFillColor(Color(0.9,0.9,0.9))
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   884
            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
   885
            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
   886
            # shade above and below
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   887
            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
   888
            canvas.restoreState()
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
   889
            #self.drawLine(x + leftIndent, y, x + leftIndent, cur_y)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   890
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   891
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   892
        nLines = len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   893
        bulletText = self.bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   894
        if nLines > 0:
2557
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
   895
            _offsets = getattr(self,'_offsets',[0])
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
   896
            _offsets += (nLines-len(_offsets))*[_offsets[-1]]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   897
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   898
            #canvas.addLiteral('%% %s.drawPara' % _className(self))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   899
            alignment = style.alignment
2557
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
   900
            offset = style.firstLineIndent+_offsets[0]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   901
            lim = nLines-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   902
            noJustifyLast = not (hasattr(self,'_JustifyLast') and self._JustifyLast)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   903
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   904
            if blPara.kind==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   905
                if alignment == TA_LEFT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   906
                    dpl = _leftDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   907
                elif alignment == TA_CENTER:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   908
                    dpl = _centerDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   909
                elif self.style.alignment == TA_RIGHT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   910
                    dpl = _rightDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   911
                elif self.style.alignment == TA_JUSTIFY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   912
                    dpl = _justifyDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   913
                f = blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   914
                cur_y = self.height - f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   915
                if bulletText <> None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   916
                    offset = _drawBullet(canvas,offset,cur_y,bulletText,style)
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   917
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   918
                #set up the font etc.
2400
f47d5c6d8fcd paragraph.py: use canvas.setFillColor
rgbecker
parents: 2367
diff changeset
   919
                canvas.setFillColor(f.textColor)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   920
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   921
                tx = self.beginText(cur_x, cur_y)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   922
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   923
                #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
   924
                tx.setFont(f.fontName, f.fontSize, style.leading)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   925
                ws = lines[0][0]
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   926
                t_off = dpl( tx, offset, ws, lines[0][1], noJustifyLast and nLines==1)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   927
                if f.underline or f.link or f.strike:
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   928
                    xs = tx.XtraState = ABag()
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   929
                    xs.cur_y = cur_y
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   930
                    xs.f = f
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   931
                    xs.style = style
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   932
                    xs.lines = lines
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   933
                    xs.underlines=[]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   934
                    xs.underlineColor=None
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   935
                    xs.strikes=[]
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   936
                    xs.strikeColor=None
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   937
                    xs.links=[]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   938
                    xs.link=f.link
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   939
                    canvas.setStrokeColor(f.textColor)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   940
                    dx = t_off+leftIndent
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   941
                    if dpl!=_justifyDrawParaLine: ws = 0
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   942
                    if f.underline: _do_under_line(0, dx, ws, tx)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   943
                    if f.strike: _do_under_line(0, dx, ws, tx, lm=0.125)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   944
                    if f.link: _do_link_line(0, dx, ws, tx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   945
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   946
                    #now the middle of the paragraph, aligned with the left margin which is our origin.
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   947
                    for i in xrange(1, nLines):
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   948
                        ws = lines[i][0]
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   949
                        t_off = dpl( tx, _offsets[i], ws, lines[i][1], noJustifyLast and i==lim)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   950
                        if dpl!=_justifyDrawParaLine: ws = 0
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   951
                        if f.underline: _do_under_line(i, t_off+leftIndent, ws, tx)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   952
                        if f.strike: _do_under_line(i, t_off+leftIndent, tx, ws, lm=0.125)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   953
                        if f.link: _do_link_line(i, t_off+leftIndent, ws, tx)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   954
                else:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   955
                    for i in xrange(1, nLines):
2557
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
   956
                        dpl( tx, _offsets[i], 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
   957
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   958
                f = lines[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   959
                cur_y = self.height - f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   960
                # default?
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   961
                dpl = _leftDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   962
                if bulletText <> None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   963
                    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
   964
                if alignment == TA_LEFT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   965
                    dpl = _leftDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   966
                elif alignment == TA_CENTER:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   967
                    dpl = _centerDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   968
                elif self.style.alignment == TA_RIGHT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   969
                    dpl = _rightDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   970
                elif self.style.alignment == TA_JUSTIFY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   971
                    dpl = _justifyDrawParaLineX
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   972
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   973
                    raise ValueError, "bad align %s" % repr(alignment)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   974
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   975
                #set up the font etc.
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   976
                tx = self.beginText(cur_x, cur_y)
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   977
                xs = tx.XtraState=ABag()
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   978
                xs.textColor=None
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   979
                xs.rise=0
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   980
                xs.underline=0
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   981
                xs.underlines=[]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   982
                xs.underlineColor=None
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   983
                xs.strike=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   984
                xs.strikes=[]
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   985
                xs.strikeColor=None
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   986
                xs.links=[]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   987
                xs.link=None
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   988
                tx.setLeading(style.leading)
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   989
                xs.cur_y = cur_y
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   990
                xs.f = f
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   991
                xs.style = style
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   992
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   993
                tx._fontname,tx._fontsize = None, None
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   994
                t_off = dpl( tx, offset, lines[0], noJustifyLast and nLines==1)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   995
                _do_post_text(0, t_off+leftIndent, tx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   996
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   997
                #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
   998
                for i in range(1, nLines):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   999
                    f = lines[i]
2557
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
  1000
                    t_off = dpl( tx, _offsets[i], f, noJustifyLast and i==lim)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
  1001
                    _do_post_text(i, t_off+leftIndent, tx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1002
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1003
            canvas.drawText(tx)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1004
            canvas.restoreState()
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
  1005
2172
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1006
    def getPlainText(self,identify=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1007
        """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
  1008
        to the raw text, without XML tags. """
2172
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1009
        frags = getattr(self,'frags',None)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1010
        if frags:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1011
            plains = []
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1012
            for frag in frags:
2445
5e52f2209426 minor fix to allow getPlaintext on more paragraphs
andy
parents: 2400
diff changeset
  1013
                if hasattr(frag, 'text'):
5e52f2209426 minor fix to allow getPlaintext on more paragraphs
andy
parents: 2400
diff changeset
  1014
                    plains.append(frag.text)
2172
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1015
            return join(plains, '')
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1016
        elif identify:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1017
            text = getattr(self,'text',None)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1018
            if text is None: text = repr(self)
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1019
            return text
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1020
        else:
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 1992
diff changeset
  1021
            return ''
302
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1022
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1023
    def getActualLineWidths0(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1024
        """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
  1025
        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
  1026
        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
  1027
        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
  1028
        assert hasattr(self, 'width'), "Cannot call this method before wrap()"
1752
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
  1029
        if self.blPara.kind:
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
  1030
            func = lambda frag, w=self.width: w - frag.extraSpace
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
  1031
        else:
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
  1032
            func = lambda frag, w=self.width: w - frag[0]
0bdc6bd413ad Attempt at fixing getActualLineWidths0
rgbecker
parents: 1737
diff changeset
  1033
        return map(func,self.blPara.lines)
497
63f575fc1170 Added experimental method getActualLineWidths0
andy_robinson
parents: 496
diff changeset
  1034
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1035
if __name__=='__main__':    #NORUNTESTS
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1036
    def dumpParagraphLines(P):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1037
        print 'dumpParagraphLines(<Paragraph @ %d>)' % id(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1038
        lines = P.blPara.lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1039
        n =len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1040
        for l in range(n):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1041
            line = lines[l]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1042
            if hasattr(line,'words'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1043
                words = line.words
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1044
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1045
                words = line[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1046
            nwords = len(words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1047
            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
  1048
            for w in range(nwords):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1049
                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
  1050
            print
302
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1051
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1052
    def dumpParagraphFrags(P):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1053
        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
  1054
        frags = P.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1055
        n =len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1056
        for l in range(n):
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1057
            print "frag%d: '%s' %s" % (l, frags[l].text,' '.join(['%s=%s' % (k,getattr(frags[l],k)) for k in frags[l].__dict__ if k!=text]))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
  1058
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1059
        l = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1060
        cum = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1061
        for W in _getFragWords(frags):
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1062
            cum += W[0]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1063
            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
  1064
            for w in W[1:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1065
                print "'%s'" % w[1],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1066
            print
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1067
            l += 1
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 383
diff changeset
  1068
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 938
diff changeset
  1069
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1070
    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
  1071
    import sys
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1072
    TESTS = sys.argv[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1073
    if TESTS==[]: TESTS=['4']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1074
    def flagged(i,TESTS=TESTS):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1075
        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
  1076
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1077
    styleSheet = getSampleStyleSheet()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1078
    B = styleSheet['BodyText']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1079
    style = ParagraphStyle("discussiontext", parent=B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1080
    style.fontName= 'Helvetica'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1081
    if flagged(1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1082
        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
  1083
mixes three pigments (cyan, magenta, and yellow) to form colors.
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1084
Because mixing chemicals is more difficult than combining light there
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1085
is a fourth parameter for darkness.  For example a chemical
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1086
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
  1087
black -- instead producing a muddy color -- so, to get black printers
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1088
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
  1089
<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
  1090
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
  1091
and better control when printed.
960659f9cb5c Fixes to splitting code
rgbecker
parents: 284
diff changeset
  1092
'''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1093
        P=Paragraph(text,style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1094
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1095
        aW, aH = 456.0, 42.8
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1096
        w,h = P.wrap(aW, aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1097
        dumpParagraphLines(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1098
        S = P.split(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1099
        for s in S:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1100
            s.wrap(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1101
            dumpParagraphLines(s)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1102
            aH = 500
488
a270e8b45200 Added J Alet's bug case
rgbecker
parents: 471
diff changeset
  1103
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1104
    if flagged(2):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1105
        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
  1106
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1107
        w,h = P.wrap(24, 200)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1108
        dumpParagraphLines(P)
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1109
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1110
    if flagged(3):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1111
        text = """Dieses Kapitel bietet eine schnelle <b><font color=red>Programme :: starten</font></b>
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1112
<onDraw name=myIndex label="Programme :: starten">
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1113
<b><font color=red>Eingabeaufforderung :: (&gt;&gt;&gt;)</font></b>
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1114
<onDraw name=myIndex label="Eingabeaufforderung :: (&gt;&gt;&gt;)">
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1115
<b><font color=red>&gt;&gt;&gt; (Eingabeaufforderung)</font></b>
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1116
<onDraw name=myIndex label="&gt;&gt;&gt; (Eingabeaufforderung)">
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
  1117
Einf&#xfc;hrung in Python <b><font color=red>Python :: Einf&#xfc;hrung</font></b>
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
  1118
<onDraw name=myIndex label="Python :: Einf&#xfc;hrung">.
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1119
Das Ziel ist, die grundlegenden Eigenschaften von Python darzustellen, ohne
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1120
sich zu sehr in speziellen Regeln oder Details zu verstricken. Dazu behandelt
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
  1121
dieses Kapitel kurz die wesentlichen Konzepte wie Variablen, Ausdr&#xfc;cke,
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1122
Kontrollfluss, Funktionen sowie Ein- und Ausgabe. Es erhebt nicht den Anspruch,
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
  1123
umfassend zu sein."""
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1124
        P=Paragraph(text, styleSheet['Code'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1125
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1126
        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
  1127
        dumpParagraphLines(P)
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
  1128
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1129
    if flagged(4):
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
  1130
        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&#xfc;llt sie mit Werten <font name=Courier>k</font>, f&#xfc;r 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&#xfc;llt einen &#xe4;hnlichen Zweck, gibt aber eine unver&#xe4;nderliche Sequenz vom Typ <font name=Courier>XRangeType</font><onDraw name=myIndex label="XRangeType"> zur&#xfc;ck. 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.'''
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1131
        aW = 420
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1132
        aH = 64.4
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1133
        P=Paragraph(text, B)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1134
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1135
        w,h = P.wrap(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1136
        print 'After initial wrap',w,h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1137
        dumpParagraphLines(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1138
        S = P.split(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1139
        dumpParagraphFrags(S[0])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1140
        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
  1141
        print 'After split wrap',w0,h0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1142
        dumpParagraphLines(S[0])
683
fbd2b18742a5 Added test 5
rgbecker
parents: 681
diff changeset
  1143
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1144
    if flagged(5):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1145
        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
  1146
        P=Paragraph(text, styleSheet['Code'])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1147
        dumpParagraphFrags(P)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1148
        w,h = P.wrap(6*72, 9.7*72)
1737
a0ff4a2ac29e Fixed splitting bug
rgbecker
parents: 1683
diff changeset
  1149
        dumpParagraphLines(P)
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1150
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1151
    if flagged(6):
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1152
        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
  1153
                     '''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
  1154
                     '''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
  1155
                     ]:
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1156
            P=Paragraph(text, styleSheet['Normal'], caseSensitive=0)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1157
            dumpParagraphFrags(P)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1158
            w,h = P.wrap(6*72, 9.7*72)
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1159
            dumpParagraphLines(P)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2183
diff changeset
  1160
1945
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
  1161
    if flagged(7):
2584
0fed2bd8ef90 reportlab: fixed <greek> added <unichar [name=..|code=../> to paragraph
rgbecker
parents: 2575
diff changeset
  1162
        text = """<para align="CENTER" fontSize="24" leading="30"><b>Generated by:</b>Dilbert</para>"""
1945
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
  1163
        P=Paragraph(text, styleSheet['Code'])
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
  1164
        dumpParagraphFrags(P)
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
  1165
        w,h = P.wrap(6*72, 9.7*72)
1360994d86d7 added testcase
rgbecker
parents: 1940
diff changeset
  1166
        dumpParagraphLines(P)
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1167
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1168
    if flagged(8):
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1169
        text ="""- bullet 0<br/>- bullet 1<br/>- bullet 2<br/>- bullet 3<br/>- bullet 4<br/>- bullet 5"""
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1170
        P=Paragraph(text, styleSheet['Normal'])
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1171
        dumpParagraphFrags(P)
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1172
        w,h = P.wrap(6*72, 9.7*72)
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1173
        dumpParagraphLines(P)
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1174
        S = P.split(6*72,h/2.0)
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1175
        print len(S)
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1176
        dumpParagraphLines(S[0])
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1177
        dumpParagraphLines(S[1])