reportlab/platypus/paragraph.py
author rgbecker
Wed, 18 Jun 2008 13:03:25 +0000
changeset 2938 5115bf2e8eeb
parent 2936 97860052e0fc
child 2940 a0e54b5117ce
permissions -rw-r--r--
paragraph.py: fix encoding bug
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2921
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2008
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$ '''
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
     5
from string import join, whitespace
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
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
     8
from reportlab.pdfbase.pdfmetrics import stringWidth, getFont, getAscentDescent
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
2887
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
    17
from reportlab.rl_config import platypus_link_underline 
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
    18
import re
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    19
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    20
#on UTF8 branch, split and strip must be unicode-safe!
2898
71cc316046ff paragraph.py: fix longstanding split bug
rgbecker
parents: 2894
diff changeset
    21
def split(text, delim=None):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    22
    if type(text) is str: text = text.decode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    23
    if type(delim) is str: delim = delim.decode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    24
    return [uword.encode('utf8') for uword in text.split(delim)]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    25
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    26
def strip(text):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    27
    if type(text) is str: text = text.decode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    28
    return text.strip().encode('utf8')
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    29
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    30
class ParaLines(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    31
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    32
    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
    33
        kind=0  Simple
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    34
        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
    35
        lines   [(extraSpace1,words1),....,(extraspaceN,wordsN)]
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    36
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    37
        kind==1 Complex
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    38
        lines   [FragLine1,...,FragLineN]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    39
    """
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    40
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    41
class FragLine(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    42
    """class FragLine contains a styled line (ie a line with more than one style)
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    43
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    44
    extraSpace  unused space for justification only
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    45
    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
    46
    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
    47
    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
    48
                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
    49
    """
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    50
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    51
#our one and only parser
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    52
# 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
    53
_parser=ParaParser()
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    54
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    55
def _lineClean(L):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    56
    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
    57
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
    58
def cleanBlockQuotedText(text,joiner=' '):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    59
    """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
    60
    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
    61
    (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
    62
    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
    63
    return join(L, joiner)
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    64
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    65
def setXPos(tx,dx):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    66
    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
    67
        tx.setXPos(dx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    68
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    69
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
    70
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    71
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    72
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    73
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    74
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    75
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
    76
    m = offset + 0.5 * extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    77
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    78
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    79
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    80
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    81
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    82
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
    83
    m = offset + extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    84
    setXPos(tx,m)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    85
    tx._textOut(join(words),1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    86
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
    87
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    88
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
    89
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
    90
    setXPos(tx,offset)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    91
    text  = join(words)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    92
    if last:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    93
        #last one, left align
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    94
        tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    95
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    96
        nSpaces = len(words)-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    97
        if nSpaces:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    98
            tx.setWordSpace(extraspace / float(nSpaces))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    99
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   100
            tx.setWordSpace(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   101
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   102
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   103
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   104
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   105
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   106
def imgVRange(h,va,fontSize):
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   107
    '''return bottom,top offsets relative to baseline(0)'''
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   108
    if va=='baseline':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   109
        iyo = 0
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   110
    elif va in ('text-top','top'):
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   111
        iyo = fontSize-h
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   112
    elif va=='middle':
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   113
        iyo = fontSize - (1.2*fontSize+h)*0.5
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   114
    elif va in ('text-bottom','bottom'):
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   115
        iyo = fontSize - 1.2*fontSize
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   116
    elif va=='super':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   117
        iyo = 0.5*fontSize
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   118
    elif va=='sub':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   119
        iyo = -0.5*fontSize
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   120
    elif hasattr(va,'normalizedValue'):
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   121
        iyo = va.normalizedValue(fontSize)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   122
    else:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   123
        iyo = va
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   124
    return iyo,iyo+h
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   125
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   126
_56=5./6
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   127
_16=1./6
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   128
def _putFragLine(cur_x, tx, line):
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   129
    xs = tx.XtraState
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   130
    cur_y = xs.cur_y
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   131
    x0 = tx._x0
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   132
    autoLeading = xs.autoLeading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   133
    leading = xs.leading
2910
5522f685b679 paragraph.py: fix leftIndent problem
rgbecker
parents: 2901
diff changeset
   134
    cur_x += xs.leftIndent
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   135
    dal = autoLeading in ('min','max')
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   136
    if dal:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   137
        if autoLeading=='max':
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   138
            ascent = max(_56*leading,line.ascent)
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   139
            descent = max(_16*leading,-line.descent)
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   140
        else:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   141
            ascent = line.ascent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   142
            descent = -line.descent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   143
        leading = ascent+descent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   144
    if tx._leading!=leading:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   145
        tx.setLeading(leading)
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   146
    if dal:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   147
        olb = tx._olb
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   148
        if olb is not None:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   149
            xcy = olb-ascent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   150
            if tx._oleading!=leading:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   151
                cur_y += leading - tx._oleading
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   152
            if abs(xcy-cur_y)>1e-8:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   153
                cur_y = xcy
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   154
                tx.setTextOrigin(x0,cur_y)
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   155
                xs.cur_y = cur_y
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   156
        tx._olb = cur_y - descent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   157
        tx._oleading = leading
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   158
    ws = getattr(tx,'_wordSpace',0)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   159
    nSpaces = 0
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   160
    words = line.words
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   161
    for f in words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   162
        if hasattr(f,'cbDefn'):
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   163
            cbDefn = f.cbDefn
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   164
            kind = cbDefn.kind
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   165
            if kind=='img':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   166
                #draw image cbDefn,cur_y,cur_x
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   167
                w = cbDefn.width
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   168
                h = cbDefn.height
2871
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   169
                txfs = tx._fontsize
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   170
                if txfs is None:
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   171
                    txfs = xs.style.fontSize
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   172
                iy0,iy1 = imgVRange(h,cbDefn.valign,txfs)
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   173
                cur_x_s = cur_x + nSpaces*ws
2894
78cd38a39719 paragraph.py: add auto mask for inline images
rgbecker
parents: 2892
diff changeset
   174
                tx._canvas.drawImage(cbDefn.image,cur_x_s,cur_y+iy0,w,h,mask='auto')
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   175
                cur_x += w
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   176
                cur_x_s += w
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   177
                setXPos(tx,cur_x_s-tx._x0)
2744
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   178
            else:
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   179
                name = cbDefn.name
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   180
                if kind=='anchor':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   181
                    tx._canvas.bookmarkHorizontal(name,cur_x,cur_y+leading)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   182
                else:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   183
                    func = getattr(tx._canvas,name,None)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   184
                    if not func:
2892
131b417f9845 paragraph: improved <a> syntax checks and better test
rgbecker
parents: 2888
diff changeset
   185
                        raise AttributeError("Missing %s callback attribute '%s'" % (kind,name))
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   186
                    func(tx._canvas,kind,cbDefn.label)
2750
05229265aff3 reportlab: fix special case <a> tag problem, add AnchorFlowable class
rgbecker
parents: 2745
diff changeset
   187
            if f is words[-1]:
05229265aff3 reportlab: fix special case <a> tag problem, add AnchorFlowable class
rgbecker
parents: 2745
diff changeset
   188
                if not tx._fontname:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   189
                    tx.setFont(xs.style.fontName,xs.style.fontSize)
2750
05229265aff3 reportlab: fix special case <a> tag problem, add AnchorFlowable class
rgbecker
parents: 2745
diff changeset
   190
                    tx._textOut('',1)
2912
703e319fffdc paragraph.py: fix inline img at eol bug
rgbecker
parents: 2910
diff changeset
   191
                elif kind=='img':
703e319fffdc paragraph.py: fix inline img at eol bug
rgbecker
parents: 2910
diff changeset
   192
                    tx._textOut('',1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   193
        else:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   194
            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
   195
            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
   196
                tx._setFont(f.fontName, f.fontSize)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   197
            if xs.textColor!=f.textColor:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   198
                xs.textColor = f.textColor
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   199
                tx.setFillColor(f.textColor)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   200
            if xs.rise!=f.rise:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   201
                xs.rise=f.rise
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   202
                tx.setRise(f.rise)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   203
            text = f.text
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   204
            tx._textOut(text,f is words[-1])    # cheap textOut
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   205
            if not xs.underline and f.underline:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   206
                xs.underline = 1
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   207
                xs.underline_x = cur_x_s
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   208
                xs.underlineColor = f.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   209
            elif xs.underline:
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   210
                if not f.underline:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   211
                    xs.underline = 0
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   212
                    xs.underlines.append( (xs.underline_x, cur_x_s, xs.underlineColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   213
                    xs.underlineColor = None
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   214
                elif xs.textColor!=xs.underlineColor:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   215
                    xs.underlines.append( (xs.underline_x, cur_x_s, xs.underlineColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   216
                    xs.underlineColor = xs.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   217
                    xs.underline_x = cur_x_s
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   218
            if not xs.strike and f.strike:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   219
                xs.strike = 1
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   220
                xs.strike_x = cur_x_s
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   221
                xs.strikeColor = f.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   222
            elif xs.strike:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   223
                if not f.strike:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   224
                    xs.strike = 0
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   225
                    xs.strikes.append( (xs.strike_x, cur_x_s, xs.strikeColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   226
                    xs.strikeColor = None
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   227
                elif xs.textColor!=xs.strikeColor:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   228
                    xs.strikes.append( (xs.strike_x, cur_x_s, xs.strikeColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   229
                    xs.strikeColor = xs.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   230
                    xs.strike_x = cur_x_s
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   231
            if f.link and not xs.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   232
                if not xs.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   233
                    xs.link = f.link
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   234
                    xs.link_x = cur_x_s
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   235
                    xs.linkColor = xs.textColor
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   236
            elif xs.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   237
                if not f.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   238
                    xs.links.append( (xs.link_x, cur_x_s, xs.link, xs.linkColor) )
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   239
                    xs.link = None
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   240
                    xs.linkColor = None
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   241
                elif f.link!=xs.link or xs.textColor!=xs.linkColor:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   242
                    xs.links.append( (xs.link_x, cur_x_s, xs.link, xs.linkColor) )
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   243
                    xs.link = f.link
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   244
                    xs.link_x = cur_x_s
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   245
                    xs.linkColor = xs.textColor
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   246
            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
   247
            cur_x += txtlen
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   248
            nSpaces += text.count(' ')
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   249
    cur_x_s = cur_x+(nSpaces-1)*ws
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   250
    if xs.underline:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   251
        xs.underlines.append( (xs.underline_x, cur_x_s, xs.underlineColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   252
    if xs.strike:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   253
        xs.strikes.append( (xs.strike_x, cur_x_s, xs.strikeColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   254
    if xs.link:
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   255
        xs.links.append( (xs.link_x, cur_x_s, xs.link,xs.linkColor) )
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   256
    if tx._x0!=x0:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   257
        setXPos(tx,x0-tx._x0)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   258
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   259
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
   260
    setXPos(tx,offset)
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   261
    _putFragLine(offset, tx, line)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   262
    setXPos(tx,-offset)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   263
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   264
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
   265
    m = offset+0.5*line.extraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   266
    setXPos(tx,m)
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   267
    _putFragLine(m,tx, line)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   268
    setXPos(tx,-m)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   269
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   270
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
   271
    m = offset+line.extraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   272
    setXPos(tx,m)
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   273
    _putFragLine(m,tx, line)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   274
    setXPos(tx,-m)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   275
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   276
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
   277
    setXPos(tx,offset)
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   278
    extraSpace = line.extraSpace
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   279
    nSpaces = line.wordCount - 1
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   280
    if last or not nSpaces or abs(extraSpace)<=1e-8 or line.lineBreak:
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   281
        _putFragLine(offset, tx, line)  #no space modification
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   282
    else:
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   283
        tx.setWordSpace(extraSpace / float(nSpaces))
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   284
        _putFragLine(offset, tx, line)
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   285
        tx.setWordSpace(0)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   286
    setXPos(tx,-offset)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   287
1373
df3383c5f8bc Added accelerated _sameFrag
rgbecker
parents: 1160
diff changeset
   288
try:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   289
    from _rl_accel import _sameFrag
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   290
except ImportError:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   291
    try:
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   292
        from reportlab.lib._rl_accel import _sameFrag
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   293
    except ImportError:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   294
        #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
   295
        def _sameFrag(f,g):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   296
            'returns 1 if two ParaFrags map out the same'
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   297
            if (hasattr(f,'cbDefn') or hasattr(g,'cbDefn')
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   298
                    or hasattr(f,'lineBreak') or hasattr(g,'lineBreak')): return 0
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   299
            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
   300
                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
   301
            return 1
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   302
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   303
def _getFragWords(frags):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   304
    ''' 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
   305
        [[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
   306
        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
   307
        each sublist represents a word
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   308
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   309
    R = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   310
    W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   311
    n = 0
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   312
    hangingStrip = False
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   313
    for f in frags:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   314
        text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   315
        #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
   316
                    # of paragraphs
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   317
        if text!='':
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   318
            if hangingStrip:
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   319
                hangingStrip = False
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   320
                text = text.lstrip()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   321
            S = split(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   322
            if S==[]: S = ['']
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   323
            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
   324
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   325
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   326
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   327
                n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   328
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   329
            for w in S[:-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   330
                W.append((f,w))
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   331
                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
   332
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   333
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   334
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   335
                n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   336
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   337
            w = S[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   338
            W.append((f,w))
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   339
            n += stringWidth(w, f.fontName, f.fontSize)
2841
c18f03647e07 fixed _getFragWords
hassan
parents: 2840
diff changeset
   340
            if text and text[-1] in whitespace:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   341
                W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   342
                R.append(W)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   343
                W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   344
                n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   345
        elif hasattr(f,'cbDefn'):
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   346
            w = getattr(f.cbDefn,'width',0)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   347
            if w:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   348
                if W!=[]:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   349
                    W.insert(0,n)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   350
                    R.append(W)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   351
                    W = []
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   352
                    n = 0
2865
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   353
                R.append([w,(f,'')])
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   354
            else:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   355
                W.append((f,''))
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   356
        elif hasattr(f, 'lineBreak'):
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
   357
            #pass the frag through.  The line breaker will scan for it.
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   358
            if W!=[]:
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   359
                W.insert(0,n)
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   360
                R.append(W)
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   361
                W = []
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   362
                n = 0
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   363
            R.append([0,(f,'')])
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   364
            hangingStrip = True
508
37dc6fc3fa65 More <onDraw> hacks
rgbecker
parents: 506
diff changeset
   365
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   366
    if W!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   367
        W.insert(0,n)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   368
        R.append(W)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   369
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   370
    return R
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   371
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   372
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
   373
    f = blPara.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   374
    for a in ('lines', 'kind', 'text'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   375
        if hasattr(f,a): delattr(f,a)
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   376
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   377
    f.words = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   378
    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
   379
        for w in l[1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   380
            f.words.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   381
    return [f]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   382
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   383
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
   384
    f = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   385
    lines = blPara.lines[start:stop]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   386
    for l in lines:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   387
        for w in l.words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   388
            f.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   389
        if l is not lines[-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   390
            i = len(f)-1
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   391
            while i>=0 and hasattr(f[i],'cbDefn') and not getattr(f[i].cbDefn,'width',0): i -= 1
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   392
            if i>=0:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   393
                g = f[i]
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   394
                if not g.text: g.text = ' '
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   395
                elif g.text[-1]!=' ': g.text += ' '
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   396
    return f
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   397
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   398
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
   399
    '''draw a bullet text could be a simple string or a frag list'''
2860
3f14d66194c2 platypus: added bulletOffsetY inspired by haraldarminmassa@gmail.com
rgbecker
parents: 2857
diff changeset
   400
    tx2 = canvas.beginText(style.bulletIndent, cur_y+getattr(style,"bulletOffsetY",0))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   401
    tx2.setFont(style.bulletFontName, style.bulletFontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   402
    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
   403
    if isinstance(bulletText,basestring):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   404
        tx2.textOut(bulletText)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   405
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   406
        for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   407
            tx2.setFont(f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   408
            tx2.setFillColor(f.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   409
            tx2.textOut(f.text)
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   410
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   411
    canvas.drawText(tx2)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   412
    #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
   413
    #bulletEnd = tx2.getX()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   414
    bulletEnd = tx2.getX() + style.bulletFontSize * 0.6
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   415
    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
   416
    return offset
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   417
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   418
def _handleBulletWidth(bulletText,style,maxWidths):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   419
    '''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
   420
    '''
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   421
    if bulletText:
2646
d177c247184a platypus: eliminate StringType usages and cp1252 usage
rgbecker
parents: 2644
diff changeset
   422
        if isinstance(bulletText,basestring):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   423
            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
   424
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   425
            #it's a list of fragments
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   426
            bulletWidth = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   427
            for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   428
                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
   429
        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
   430
        indent = style.leftIndent+style.firstLineIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   431
        if bulletRight > indent:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   432
            #..then it overruns, and we have less space available on line 1
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   433
            maxWidths[0] -= (bulletRight - indent)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   434
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   435
def splitLines0(frags,widths):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   436
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   437
    given a list of ParaFrags we return a list of ParaLines
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   438
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   439
    each ParaLine has
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   440
    1)  ExtraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   441
    2)  blankCount
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   442
    3)  [textDefns....]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   443
        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
   444
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   445
    #initialise the algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   446
    lines   = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   447
    lineNum = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   448
    maxW    = widths[lineNum]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   449
    i       = -1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   450
    l       = len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   451
    lim     = start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   452
    while 1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   453
        #find a non whitespace character
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   454
        while i<l:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   455
            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
   456
            if start==lim:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   457
                i += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   458
                if i==l: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   459
                start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   460
                f = frags[i]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   461
                text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   462
                lim = len(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   463
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   464
                break   # we found one
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
   465
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   466
        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
   467
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   468
        #start of a line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   469
        g       = (None,None,None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   470
        line    = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   471
        cLen    = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   472
        nSpaces = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   473
        while cLen<maxW:
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   474
            j = text.find(' ',start)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   475
            if j<0: j==lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   476
            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
   477
            cLen += w
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   478
            if cLen>maxW and line!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   479
                cLen = cLen-w
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   480
                #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
   481
                while g.text[lim]==' ':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   482
                    lim = lim - 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   483
                    nSpaces = nSpaces-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   484
                break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   485
            if j<0: j = lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   486
            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
   487
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   488
                g = (f,start,j)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   489
                line.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   490
            if j==lim:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   491
                i += 1
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   492
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   493
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
   494
    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
   495
    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
   496
    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
   497
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   498
_scheme_re = re.compile('^[a-zA-Z][-+a-zA-Z0-9]+$')
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   499
def _doLink(tx,link,rect):
2938
5115bf2e8eeb paragraph.py: fix encoding bug
rgbecker
parents: 2936
diff changeset
   500
    if isinstance(link,unicode):
5115bf2e8eeb paragraph.py: fix encoding bug
rgbecker
parents: 2936
diff changeset
   501
        link = link.encode('utf8')
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   502
    parts = link.split(':',1)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   503
    scheme = len(parts)==2 and parts[0].lower() or ''
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   504
    if _scheme_re.match(scheme) and scheme!='document':
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   505
        kind=scheme.lower()=='pdf' and 'GoToR' or 'URI'
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   506
        if kind=='GoToR': link = parts[1]
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   507
        tx._canvas.linkURL(link, rect, relative=1, kind=kind)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   508
    else:
2744
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   509
        if link[0]=='#':
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   510
            link = link[1:]
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   511
            scheme=''
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   512
        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
   513
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   514
def _do_link_line(i, t_off, ws, tx):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   515
    xs = tx.XtraState
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   516
    leading = xs.style.leading
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   517
    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
   518
    text = join(xs.lines[i][1])
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   519
    textlen = tx._canvas.stringWidth(text, tx._fontname, tx._fontsize)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   520
    _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
   521
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   522
def _do_post_text(tx):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   523
    xs = tx.XtraState
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   524
    leading = xs.style.leading
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   525
    autoLeading = xs.autoLeading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   526
    f = xs.f
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   527
    if autoLeading=='max':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   528
        leading = max(leading,1.2*f.fontSize)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   529
    elif autoLeading=='min':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   530
        leading = 1.2*f.fontSize
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   531
    ff = 0.125*f.fontSize
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   532
    y0 = xs.cur_y
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   533
    y = y0 - ff
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   534
    csc = None
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   535
    for x1,x2,c in xs.underlines:
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   536
        if c!=csc:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   537
            tx._canvas.setStrokeColor(c)
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   538
            csc = c
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   539
        tx._canvas.line(x1, y, x2, y)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   540
    xs.underlines = []
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   541
    xs.underline=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   542
    xs.underlineColor=None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   543
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   544
    ys = y0 + 2*ff
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   545
    for x1,x2,c in xs.strikes:
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   546
        if c!=csc:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   547
            tx._canvas.setStrokeColor(c)
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   548
            csc = c
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   549
        tx._canvas.line(x1, ys, x2, ys)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   550
    xs.strikes = []
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   551
    xs.strike=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   552
    xs.strikeColor=None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   553
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   554
    yl = y + leading
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   555
    for x1,x2,link,c in xs.links:
2887
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   556
        if platypus_link_underline:
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   557
            if c!=csc:
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   558
                tx._canvas.setStrokeColor(c)
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   559
                csc = c
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   560
            tx._canvas.line(x1, y, x2, y)
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   561
        _doLink(tx, link, (x1, y, x2, yl))
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   562
    xs.links = []
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   563
    xs.link=None
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   564
    xs.linkColor=None
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   565
    xs.cur_y -= leading
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   566
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   567
def textTransformFrags(frags,style):
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   568
    tt = style.textTransform
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   569
    if tt:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   570
        tt=tt.lower()
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   571
        if tt=='lowercase':
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   572
            tt = unicode.lower
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   573
        elif tt=='uppercase':
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   574
            tt = unicode.upper
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   575
        elif  tt=='capitalize':
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   576
            tt = unicode.title
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   577
        elif tt=='none':
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   578
            return
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   579
        else:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   580
            raise ValueError('ParaStyle.textTransform value %r is invalid' % style.textTransform) 
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   581
        n = len(frags)
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   582
        if n==1:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   583
            #single fragment the easy case
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   584
            frags[0].text = tt(frags[0].text.decode('utf8')).encode('utf8')
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   585
        elif tt is unicode.title:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   586
            pb = True
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   587
            for f in frags:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   588
                t = f.text
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   589
                if not t: continue
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   590
                u = t.decode('utf8')
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   591
                if u.startswith(u' ') or pb:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   592
                    u = tt(u)
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   593
                else:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   594
                    i = u.find(u' ')
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   595
                    if i>=0:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   596
                        u = u[:i]+tt(u[i:])
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   597
                pb = u.endswith(u' ')
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   598
                f.text = u.encode('utf8')
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   599
        else:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   600
            for f in frags:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   601
                t = f.text
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   602
                if not t: continue
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   603
                f.text = tt(t.decode('utf8')).encode('utf8')
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   604
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   605
class Paragraph(Flowable):
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   606
    """ 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
   607
        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
   608
        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
   609
        bulletText is an optional bullet defintion.
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   610
        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
   611
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   612
        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
   613
        into a paragraph with a given style.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   614
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   615
        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
   616
        <b> ... </b> - bold
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   617
        <i> ... </i> - italics
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   618
        <u> ... </u> - underline
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   619
        <strike> ... </strike> - strike through
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   620
        <super> ... </super> - superscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   621
        <sub> ... </sub> - subscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   622
        <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
   623
        <onDraw name=callable label="a label">
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   624
        <link>link text</link>
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   625
            attributes of links
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   626
                size/fontSize=num
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   627
                name/face/fontName=name
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   628
                fg/textColor/color=color
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   629
                backcolor/backColor/bgcolor=color
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   630
                dest/destination/target/href/link=target
2745
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   631
        <a>anchor text</a>
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   632
            attributes of anchors
2745
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   633
                fontSize=num
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   634
                fontName=name
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   635
                fg/textColor/color=color
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   636
                backcolor/backColor/bgcolor=color
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   637
                href=href
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
   638
        <a name="anchorpoint"/>
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   639
        <unichar name="unicode character name"/>
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   640
        <unichar value="unicode code point"/>
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   641
        <img src="path" width="1in" height="1in" valign="bottom"/>
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   642
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   643
        The whole may be surrounded by <para> </para> tags
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
   644
2791
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   645
        The <b> and <i> tags will work for the built-in fonts (Helvetica
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   646
        /Times / Courier).  For other fonts you need to register a family
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   647
        of 4 fonts using reportlab.pdfbase.pdfmetrics.registerFont; then
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   648
        use the addMapping function to tell the library that these 4 fonts
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   649
        form a family e.g.
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   650
            from reportlab.lib.fonts import addMapping
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   651
            addMapping('Vera', 0, 0, 'Vera')    #normal
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   652
            addMapping('Vera', 0, 1, 'Vera-Italic')    #italic
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   653
            addMapping('Vera', 1, 0, 'Vera-Bold')    #bold
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   654
            addMapping('Vera', 1, 1, 'Vera-BoldItalic')    #italic and bold
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
   655
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   656
        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
   657
    """
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   658
    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
   659
        self.caseSensitive = caseSensitive
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   660
        self.encoding = encoding
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   661
        self._setup(text, style, bulletText, frags, cleanBlockQuotedText)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   662
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   663
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   664
        n = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   665
        L = [n+"("]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   666
        keys = self.__dict__.keys()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   667
        for k in keys:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   668
            v = getattr(self, k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   669
            rk = repr(k)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   670
            rv = repr(v)
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   671
            rk = "  "+rk.replace("\n", "\n  ")
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   672
            rv = "    "+rk.replace("\n", "\n    ")
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   673
            L.append(rk)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   674
            L.append(rv)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   675
        L.append(") #"+n)
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   676
        return '\n'.join(L)
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 540
diff changeset
   677
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   678
    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
   679
        if frags is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   680
            text = cleaner(text)
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
   681
            _parser.caseSensitive = self.caseSensitive
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   682
            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
   683
            if frags is None:
2892
131b417f9845 paragraph: improved <a> syntax checks and better test
rgbecker
parents: 2888
diff changeset
   684
                raise ValueError("xml parser error (%s) in paragraph beginning\n'%s'"\
131b417f9845 paragraph: improved <a> syntax checks and better test
rgbecker
parents: 2888
diff changeset
   685
                    % (_parser.errors[0],text[:min(30,len(text))]))
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   686
            textTransformFrags(frags,style)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   687
            if bulletTextFrags: bulletText = bulletTextFrags
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 268
diff changeset
   688
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   689
        #AR hack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   690
        self.text = text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   691
        self.frags = frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   692
        self.style = style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   693
        self.bulletText = bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   694
        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
   695
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   696
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   697
        # work out widths array for breaking
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   698
        self.width = availWidth
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   699
        style = self.style
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   700
        leftIndent = style.leftIndent
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   701
        first_line_width = availWidth - (leftIndent+style.firstLineIndent) - style.rightIndent
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   702
        later_widths = availWidth - leftIndent - style.rightIndent
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   703
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   704
        if style.wordWrap == 'CJK':
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   705
            #use Asian text wrap algorithm to break characters
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   706
            blPara = self.breakLinesCJK([first_line_width, later_widths])
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   707
        else:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   708
            blPara = self.breakLines([first_line_width, later_widths])
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   709
        self.blPara = blPara
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   710
        autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   711
        leading = style.leading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   712
        if blPara.kind==1 and autoLeading not in ('','off'):
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   713
            height = 0
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   714
            if autoLeading=='max':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   715
                for l in blPara.lines:
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   716
                    height += max(l.ascent-l.descent,leading)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   717
            elif autoLeading=='min':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   718
                for l in blPara.lines:
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   719
                    height += l.ascent - l.descent
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   720
            else:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   721
                raise ValueError('invalid autoLeading value %r' % autoLeading)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   722
        else:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   723
            if autoLeading=='max':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   724
                leading = max(leading,1.2*style.fontSize)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   725
            elif autoLeading=='min':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   726
                leading = 1.2*style.fontSize
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   727
            height = len(blPara.lines) * leading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   728
        self.height = height
2900
bbad3077c8f1 paragraph.py: fix simple paragraph vertical position
rgbecker
parents: 2899
diff changeset
   729
        return self.width, height
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   730
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   731
    def minWidth(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   732
        '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
   733
        frags = self.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   734
        nFrags= len(frags)
2467
5630a2b21a90 tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents: 2456
diff changeset
   735
        if not nFrags: return 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   736
        if nFrags==1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   737
            f = frags[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   738
            fS = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   739
            fN = f.fontName
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   740
            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
   741
            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
   742
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   743
            words = _getFragWords(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   744
            func  = lambda x: x[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   745
        return max(map(func,words))
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 938
diff changeset
   746
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   747
    def _get_split_blParaFunc(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   748
        return self.blPara.kind==0 and _split_blParaSimple or _split_blParaHard
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   749
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   750
    def split(self,availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   751
        if len(self.frags)<=0: return []
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   752
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   753
        #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
   754
        if not hasattr(self,'blPara'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   755
            self.wrap(availWidth,availHeight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   756
        blPara = self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   757
        style = self.style
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   758
        autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   759
        leading = style.leading
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   760
        lines = blPara.lines
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   761
        if blPara.kind==1 and autoLeading not in ('','off'):
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   762
            s = height = 0
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   763
            if autoLeading=='max':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   764
                for i,l in enumerate(blPara.lines):
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   765
                    h = max(l.ascent-l.descent,leading)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   766
                    n = height+h
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   767
                    if n>availHeight+1e-8:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   768
                        break
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   769
                    height = n
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   770
                    s = i+1
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   771
            elif autoLeading=='min':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   772
                for i,l in enumerate(blPara.lines):
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   773
                    n = height+l.ascent-l.descent
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   774
                    if n>availHeight+1e-8:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   775
                        break
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   776
                    height = n
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   777
                    s = i+1
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   778
            else:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   779
                raise ValueError('invalid autoLeading value %r' % autoLeading)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   780
        else:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   781
            l = leading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   782
            if autoLeading=='max':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   783
                l = max(leading,1.2*style.fontSize)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   784
            elif autoLeading=='min':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   785
                l = 1.2*style.fontSize
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   786
            s = int(availHeight/l)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   787
            height = s*l
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   788
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   789
        n = len(lines)
2921
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   790
        allowWidows = getattr(self,'allowWidows',getattr(self,'allowWidows',1))
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   791
        allowOrphans = getattr(self,'allowOrphans',getattr(self,'allowOrphans',0))
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   792
        if not allowOrphans:
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   793
            if s<=1:    #orphan?
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   794
                del self.blPara
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   795
                return []
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   796
        if n<=s: return [self]
2921
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   797
        if not allowWidows:
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   798
            if n==s+1: #widow?
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   799
                if (allowOrphans and n==3) or n>3:
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   800
                    s -= 1  #give the widow some company
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   801
                else:
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   802
                    del self.blPara #no room for adjustment; force the whole para onwards
d0bcee3e3785 reportlab: add in some widows/orphans control
rgbecker
parents: 2912
diff changeset
   803
                    return []
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   804
        func = self._get_split_blParaFunc()
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   805
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   806
        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
   807
        #this is a major hack
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   808
        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
   809
        P1._JustifyLast = 1
2529
dced304f8584 flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents: 2467
diff changeset
   810
        P1._splitpara = 1
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   811
        P1.height = height
2561
a2409b6627e6 reportlab: added FlowablesAndImage
rgbecker
parents: 2557
diff changeset
   812
        P1.width = availWidth
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   813
        if style.firstLineIndent != 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   814
            style = deepcopy(style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   815
            style.firstLineIndent = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   816
        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
   817
        return [P1,P2]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   818
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   819
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   820
        #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
   821
        #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
   822
        #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
   823
        self.drawPara(self.debug)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   824
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   825
    def breakLines(self, width):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   826
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   827
        Returns a broken line structure. There are two cases
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   828
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   829
        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
   830
            A fragment specifier with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   831
                kind = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   832
                fontName, fontSize, leading, textColor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   833
                lines=  A list of lines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   834
                        Each line has two items.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   835
                        1) unused width in points
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   836
                        2) word list
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   837
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   838
        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
   839
            A fragment specifier with
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   840
                kind = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   841
                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
   842
                            extraspace (needed for justified)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   843
                            fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   844
                            words=word list
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   845
                                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
   846
                                various settings
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   847
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   848
        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
   849
        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
   850
        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
   851
        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
   852
        around irregular objects."""
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   853
2932
bcdff7b8f469 platypus: initial try at textTransform style attribute
rgbecker
parents: 2923
diff changeset
   854
        if not isinstance(width,(tuple,list)): maxWidths = [width]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   855
        else: maxWidths = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   856
        lines = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   857
        lineno = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   858
        style = self.style
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   859
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   860
        #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
   861
        _handleBulletWidth(self.bulletText,style,maxWidths)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   862
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   863
        maxWidth = maxWidths[0]
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
   864
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   865
        self.height = 0
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   866
        autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   867
        calcBounds = autoLeading not in ('','off')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   868
        frags = self.frags
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   869
        nFrags= len(frags)
2750
05229265aff3 reportlab: fix special case <a> tag problem, add AnchorFlowable class
rgbecker
parents: 2745
diff changeset
   870
        if nFrags==1 and not hasattr(frags[0],'cbDefn'):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   871
            f = frags[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   872
            fontSize = f.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   873
            fontName = f.fontName
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   874
            ascent, descent = getAscentDescent(fontName,fontSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   875
            words = hasattr(f,'text') and split(f.text, ' ') or f.words
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   876
            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
   877
            cLine = []
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   878
            currentWidth = -spaceWidth   # hack to get around extra space for word 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   879
            for word in words:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   880
                #this underscores my feeling that Unicode throughout would be easier!
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   881
                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
   882
                newWidth = currentWidth + spaceWidth + wordWidth
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
   883
                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
   884
                    # fit one more on this line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   885
                    cLine.append(word)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   886
                    currentWidth = newWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   887
                else:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   888
                    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
   889
                    #end of line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   890
                    lines.append((maxWidth - currentWidth, cLine))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   891
                    cLine = [word]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   892
                    currentWidth = wordWidth
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   893
                    lineno += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   894
                    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   895
                        maxWidth = maxWidths[lineno]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   896
                    except IndexError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   897
                        maxWidth = maxWidths[-1]  # use the last one
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   898
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   899
            #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
   900
            if cLine!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   901
                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
   902
                lines.append((maxWidth - currentWidth, cLine))
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   903
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   904
            return f.clone(kind=0, lines=lines,ascent=ascent,descent=descent,fontSize=fontSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   905
        elif nFrags<=0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   906
            return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   907
                            textColor=style.textColor, ascent=style.fontSize,descent=-0.2*style.fontSize,
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   908
                            lines=[])
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   909
        else:
2529
dced304f8584 flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents: 2467
diff changeset
   910
            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
   911
                #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
   912
                #preserving splitting algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   913
                return self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   914
            n = 0
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
   915
            words = []
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   916
            for w in _getFragWords(frags):
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   917
                f=w[-1][0]
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   918
                fontName = f.fontName
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   919
                fontSize = f.fontSize
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   920
                spaceWidth = stringWidth(' ',fontName, fontSize)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   921
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   922
                if not words:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   923
                    currentWidth = -spaceWidth   # hack to get around extra space for word 1
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   924
                    maxSize = fontSize
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   925
                    maxAscent, minDescent = getAscentDescent(fontName,fontSize)
496
bb47cf5c2739 Layout error fixes
rgbecker
parents: 494
diff changeset
   926
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   927
                wordWidth = w[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   928
                f = w[1][0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   929
                if wordWidth>0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   930
                    newWidth = currentWidth + spaceWidth + wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   931
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   932
                    newWidth = currentWidth
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   933
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
   934
                #test to see if this frag is a line break. If it is we will only act on it
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   935
                #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
   936
                lineBreak = hasattr(f,'lineBreak')
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
   937
                endLine = (newWidth>maxWidth and n>0) or lineBreak
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   938
                if not endLine:
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
   939
                    if lineBreak: continue      #throw it away
2735
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2708
diff changeset
   940
                    nText = w[1][1]
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2708
diff changeset
   941
                    if nText: n += 1
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   942
                    fontSize = f.fontSize
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   943
                    if calcBounds:
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   944
                        cbDefn = getattr(f,'cbDefn',None)
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   945
                        if getattr(cbDefn,'width',0):
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   946
                            descent,ascent = imgVRange(cbDefn.height,cbDefn.valign,fontSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   947
                        else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   948
                            ascent, descent = getAscentDescent(f.fontName,fontSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   949
                    else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   950
                        ascent, descent = getAscentDescent(f.fontName,fontSize)
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   951
                    maxSize = max(maxSize,fontSize)
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   952
                    maxAscent = max(maxAscent,ascent)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   953
                    minDescent = min(minDescent,descent)
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   954
                    if not words:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   955
                        g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   956
                        words = [g]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   957
                        g.text = nText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   958
                    elif not _sameFrag(g,f):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   959
                        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
   960
                            if hasattr(g,'cbDefn'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   961
                                i = len(words)-1
2865
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   962
                                while i>=0:
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   963
                                    wi = words[i]
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   964
                                    cbDefn = getattr(wi,'cbDefn',None)
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   965
                                    if cbDefn:
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   966
                                        if not getattr(cbDefn,'width',0):
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   967
                                            i -= 1
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   968
                                            continue
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   969
                                    if not wi.text.endswith(' '):
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   970
                                        wi.text += ' '
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   971
                                    break
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   972
                            else:
2865
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   973
                                if not g.text.endswith(' '):
dcf03cc19361 paragraph.py: attempt to fix up inline images across paragraph split
rgbecker
parents: 2863
diff changeset
   974
                                    g.text += ' '
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   975
                        g = f.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   976
                        words.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   977
                        g.text = nText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   978
                    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   979
                        if nText!='' and nText[0]!=' ':
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   980
                            g.text += ' ' + nText
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   981
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   982
                    for i in w[2:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   983
                        g = i[0].clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   984
                        g.text=i[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   985
                        words.append(g)
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   986
                        fontSize = g.fontSize
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   987
                        if calcBounds:
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   988
                            cbDefn = getattr(g,'cbDefn',None)
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   989
                            if getattr(cbDefn,'width',0):
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   990
                                descent,ascent = imgVRange(cbDefn.height,cbDefn.valign,fontSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   991
                            else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   992
                                ascent, descent = getAscentDescent(g.fontName,fontSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   993
                        else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
   994
                            ascent, descent = getAscentDescent(g.fontName,fontSize)
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   995
                        maxSize = max(maxSize,fontSize)
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
   996
                        maxAscent = max(maxAscent,ascent)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   997
                        minDescent = min(minDescent,descent)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   998
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   999
                    currentWidth = newWidth
2663
927cc273c5a5 <br/> work in progress
andy
parents: 2646
diff changeset
  1000
                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
  1001
                    if lineBreak:
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1002
                        g = f.clone()
2681
eb733ebd1ee7 paragraph.py: fix splitting cases for br tag
rgbecker
parents: 2670
diff changeset
  1003
                        #del g.lineBreak
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1004
                        words.append(g)
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1005
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1006
                    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
  1007
                    #end of line
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
  1008
                    lines.append(FragLine(extraSpace=maxWidth-currentWidth, wordCount=n,
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
  1009
                                        lineBreak=lineBreak, words=words, fontSize=maxSize, ascent=maxAscent, descent=minDescent))
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1010
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1011
                    #start new line
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1012
                    lineno += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1013
                    try:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1014
                        maxWidth = maxWidths[lineno]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1015
                    except IndexError:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1016
                        maxWidth = maxWidths[-1]  # use the last one
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1017
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1018
                    if lineBreak:
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1019
                        n = 0
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1020
                        words = []
2664
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1021
                        continue
c9faa3a99e93 reportlab/platypus: <br/> tags now working
rgbecker
parents: 2663
diff changeset
  1022
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1023
                    currentWidth = wordWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1024
                    n = 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1025
                    g = f.clone()
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
  1026
                    maxSize = g.fontSize
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1027
                    if calcBounds:
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1028
                        cbDefn = getattr(g,'cbDefn',None)
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1029
                        if getattr(cbDefn,'width',0):
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
  1030
                            minDescent,maxAscent = imgVRange(cbDefn.height,cbDefn.valign,maxSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1031
                        else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
  1032
                            maxAscent, minDescent = getAscentDescent(g.fontName,maxSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1033
                    else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
  1034
                        maxAscent, minDescent = getAscentDescent(g.fontName,maxSize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1035
                    words = [g]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1036
                    g.text = w[1][1]
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
  1037
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1038
                    for i in w[2:]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1039
                        g = i[0].clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1040
                        g.text=i[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1041
                        words.append(g)
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
  1042
                        fontSize = g.fontSize
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1043
                        if calcBounds:
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1044
                            cbDefn = getattr(g,'cbDefn',None)
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1045
                            if getattr(cbDefn,'width',0):
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
  1046
                                descent,ascent = imgVRange(cbDefn.height,cbDefn.valign,fontSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1047
                            else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
  1048
                                ascent, descent = getAscentDescent(g.fontName,fontSize)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1049
                        else:
2888
edf4cc50dbc1 paragraph.py: better initialization for maxSize/maxAscent etc
rgbecker
parents: 2887
diff changeset
  1050
                            ascent, descent = getAscentDescent(g.fontName,fontSize)
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
  1051
                        maxSize = max(maxSize,fontSize)
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
  1052
                        maxAscent = max(maxAscent,ascent)
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1053
                        minDescent = min(minDescent,descent)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
  1054
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1055
            #deal with any leftovers on the final line
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1056
            if words!=[]:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1057
                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
  1058
                lines.append(ParaLines(extraSpace=(maxWidth - currentWidth),wordCount=n,
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
  1059
                                    words=words, fontSize=maxSize,ascent=maxAscent,descent=minDescent))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1060
            return ParaLines(kind=1, lines=lines)
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
  1061
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1062
        return lines
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1063
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1064
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1065
    def breakLinesCJK(self, width):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1066
        """Initially, the dumbest possible wrapping algorithm.
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1067
        Cannot handle font variations."""
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1068
2598
2dfd3107217c paragraph: fix misuse of style before setting in cjk breaklines
rgbecker
parents: 2594
diff changeset
  1069
        style = self.style
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1070
        #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
  1071
        if len(self.frags) > 1:
2923
b5c8043382fc some improvements to tests and error tracebacks
andy
parents: 2921
diff changeset
  1072
            raise ValueError('CJK Wordwrap can only handle one fragment per paragraph for now.  Tried to handle:\ntext:  %s\nfrags: %s' % (self.text, self.frags))
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1073
        elif len(self.frags) == 0:
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1074
            return ParaLines(kind=0, fontSize=style.fontSize, fontName=style.fontName,
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1075
                            textColor=style.textColor, lines=[],ascent=style.fontSize,descent=-0.2*style.fontSize)
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1076
        f = self.frags[0]
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1077
        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
  1078
            #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
  1079
            #preserving splitting algorithm
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
  1080
            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
  1081
        if type(width)!=ListType: maxWidths = [width]
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1082
        else: maxWidths = width
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1083
        lines = []
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1084
        lineno = 0
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1085
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1086
        #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
  1087
        _handleBulletWidth(self.bulletText, style, maxWidths)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1088
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1089
        maxWidth = maxWidths[0]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1090
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1091
        self.height = 0
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1092
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1093
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1094
        f = self.frags[0]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1095
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1096
        if hasattr(f,'text'):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1097
            text = f.text
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1098
        else:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1099
            text = ''.join(getattr(f,'words',[]))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1100
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1101
        from reportlab.lib.textsplit import wordSplit
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1102
        lines = wordSplit(text, maxWidths[0], f.fontName, f.fontSize)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1103
        #the paragraph drawing routine assumes multiple frags per line, so we need an
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1104
        #extra list like this
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1105
        #  [space, [text]]
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1106
        #
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1107
        wrappedLines = [(sp, [line]) for (sp, line) in lines]
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
  1108
        return f.clone(kind=0, lines=wrappedLines, ascent=f.fontSize, descent=-0.2*f.fontSize)
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1109
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
  1110
    def beginText(self, x, y):
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
  1111
        return self.canv.beginText(x, y)
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
  1112
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1113
    def drawPara(self,debug=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1114
        """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
  1115
        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
  1116
        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
  1117
        algorithm will go infinite."""
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1118
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1119
        #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
  1120
        canvas = self.canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1121
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1122
        blPara = self.blPara
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1123
        lines = blPara.lines
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
  1124
        leading = style.leading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
  1125
        autoLeading = getattr(self,'autoLeading',getattr(style,'autoLeading',''))
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
  1126
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1127
        #work out the origin for line 1
2182
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
  1128
        leftIndent = style.leftIndent
a8b905569feb Underline alignment fix from Marc Stober
rgbecker
parents: 2172
diff changeset
  1129
        cur_x = leftIndent
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1130
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1131
        if debug:
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1132
            bw = 0.5
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1133
            bc = Color(1,1,0)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1134
            bg = Color(0.9,0.9,0.9)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1135
        else:
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1136
            bw = getattr(style,'borderWidth',None)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1137
            bc = getattr(style,'borderColor',None)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1138
            bg = style.backColor
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
  1139
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1140
        #if has a background or border, draw it
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1141
        if bg or (bc and bw):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1142
            canvas.saveState()
2758
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1143
            op = canvas.rect
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1144
            kwds = dict(fill=0,stroke=0)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1145
            if bc and bw:
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1146
                canvas.setStrokeColor(bc)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1147
                canvas.setLineWidth(bw)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1148
                kwds['stroke'] = 1
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1149
                br = getattr(style,'borderRadius',0)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1150
                if br and not debug:
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1151
                    op = canvas.roundRect
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1152
                    kwds['radius'] = br
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1153
            if bg:
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1154
                canvas.setFillColor(bg)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1155
                kwds['fill'] = 1
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1156
            bp = getattr(style,'borderPadding',0)
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1157
            op(leftIndent-bp,
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1158
                        -bp,
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1159
                        self.width - (leftIndent+style.rightIndent)+2*bp,
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1160
                        self.height+2*bp,
c404102cb7e3 paragraph: add border styling and fix trailing <br/> bug
rgbecker
parents: 2750
diff changeset
  1161
                        **kwds)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1162
            canvas.restoreState()
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1163
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1164
        nLines = len(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1165
        bulletText = self.bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1166
        if nLines > 0:
2557
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
  1167
            _offsets = getattr(self,'_offsets',[0])
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
  1168
            _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
  1169
            canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1170
            #canvas.addLiteral('%% %s.drawPara' % _className(self))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1171
            alignment = style.alignment
2557
d84c18fb377f added _offsets handling to paragraph for flow around on left side
rgbecker
parents: 2529
diff changeset
  1172
            offset = style.firstLineIndent+_offsets[0]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1173
            lim = nLines-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1174
            noJustifyLast = not (hasattr(self,'_JustifyLast') and self._JustifyLast)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1175
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1176
            if blPara.kind==0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1177
                if alignment == TA_LEFT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1178
                    dpl = _leftDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1179
                elif alignment == TA_CENTER:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1180
                    dpl = _centerDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1181
                elif self.style.alignment == TA_RIGHT:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1182
                    dpl = _rightDrawParaLine
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1183
                elif self.style.alignment == TA_JUSTIFY:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398