src/reportlab/platypus/paragraph.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4316 14c18006410a
child 4342 b4f097de0b7f
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4316
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
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
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4231
diff changeset
     4
__version__='3.3.0'
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 3031
diff changeset
     5
__doc__='''The standard paragraph implementation'''
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
     6
from string import whitespace
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
     7
from operator import truth
3547
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
     8
from unicodedata import category
2883
212d93981b3c paragraph.py: use proper ascent/descent info
rgbecker
parents: 2882
diff changeset
     9
from reportlab.pdfbase.pdfmetrics import stringWidth, getFont, getAscentDescent
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
    10
from reportlab.platypus.paraparser import ParaParser, _PCT
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 268
diff changeset
    11
from reportlab.platypus.flowables import Flowable
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    12
from reportlab.lib.colors import Color
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    13
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
3042
2ae512e69843 Implemented variable padding for platypus paragraphs.
jonas
parents: 3032
diff changeset
    14
from reportlab.lib.geomutils import normalizeTRBL
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
    15
from reportlab.lib.textsplit import wordSplit, ALL_CANNOT_START
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
    16
from copy import deepcopy
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    17
from reportlab.lib.abag import ABag
4136
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
    18
from reportlab.rl_config import platypus_link_underline, decimalSymbol, _FUZZ, paraFontSizeHeightOffset
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
    19
from reportlab.lib.utils import _className, isBytes, unicodeT, bytesT, strTypes
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3761
diff changeset
    20
from reportlab.lib.rl_accel import sameFrag
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
    21
import re
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
    22
from types import MethodType
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    23
3799
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
    24
#on UTF8/py33 branch, split and strip must be unicode-safe!
3022
4bbfeb0709ea reportlab: add Dirk Holtwick inspired split to paragraph.py
rgbecker
parents: 3003
diff changeset
    25
#thanks to Dirk Holtwick for helpful discussions/insight
4bbfeb0709ea reportlab: add Dirk Holtwick inspired split to paragraph.py
rgbecker
parents: 3003
diff changeset
    26
#on this one
3493
cff04ae591d7 paragraph.py: fix up some special cases for whitespace
rgbecker
parents: 3492
diff changeset
    27
_wsc = ''.join((
3761
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    28
    u'\u0009',  # HORIZONTAL TABULATION
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    29
    u'\u000A',  # LINE FEED
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    30
    u'\u000B',  # VERTICAL TABULATION
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    31
    u'\u000C',  # FORM FEED
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    32
    u'\u000D',  # CARRIAGE RETURN
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    33
    u'\u001C',  # FILE SEPARATOR
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    34
    u'\u001D',  # GROUP SEPARATOR
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    35
    u'\u001E',  # RECORD SEPARATOR
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    36
    u'\u001F',  # UNIT SEPARATOR
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    37
    u'\u0020',  # SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    38
    u'\u0085',  # NEXT LINE
3022
4bbfeb0709ea reportlab: add Dirk Holtwick inspired split to paragraph.py
rgbecker
parents: 3003
diff changeset
    39
    #u'\u00A0', # NO-BREAK SPACE
3761
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    40
    u'\u1680',  # OGHAM SPACE MARK
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    41
    u'\u2000',  # EN QUAD
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    42
    u'\u2001',  # EM QUAD
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    43
    u'\u2002',  # EN SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    44
    u'\u2003',  # EM SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    45
    u'\u2004',  # THREE-PER-EM SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    46
    u'\u2005',  # FOUR-PER-EM SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    47
    u'\u2006',  # SIX-PER-EM SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    48
    u'\u2007',  # FIGURE SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    49
    u'\u2008',  # PUNCTUATION SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    50
    u'\u2009',  # THIN SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    51
    u'\u200A',  # HAIR SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    52
    u'\u200B',  # ZERO WIDTH SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    53
    u'\u2028',  # LINE SEPARATOR
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    54
    u'\u2029',  # PARAGRAPH SEPARATOR
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    55
    u'\u202F',  # NARROW NO-BREAK SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    56
    u'\u205F',  # MEDIUM MATHEMATICAL SPACE
7c6245b62827 paragraph.py: python-3.3 allows u prefix
robin
parents: 3759
diff changeset
    57
    u'\u3000',  # IDEOGRAPHIC SPACE
3493
cff04ae591d7 paragraph.py: fix up some special cases for whitespace
rgbecker
parents: 3492
diff changeset
    58
    ))
cff04ae591d7 paragraph.py: fix up some special cases for whitespace
rgbecker
parents: 3492
diff changeset
    59
_wsc_re_split=re.compile('[%s]+'% re.escape(_wsc)).split
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
    60
_wsc_end_search=re.compile('[%s]+$'% re.escape(_wsc)).search
3022
4bbfeb0709ea reportlab: add Dirk Holtwick inspired split to paragraph.py
rgbecker
parents: 3003
diff changeset
    61
2898
71cc316046ff paragraph.py: fix longstanding split bug
rgbecker
parents: 2894
diff changeset
    62
def split(text, delim=None):
3835
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3799
diff changeset
    63
    if isBytes(text): text = text.decode('utf8')
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3799
diff changeset
    64
    if delim is not None and isBytes(delim): delim = delim.decode('utf8')
3882
a74c1b4f7940 paragraph.py: fix 3.3 only code
robin
parents: 3835
diff changeset
    65
    return [uword for uword in (_wsc_re_split(text) if delim is None and u'\xa0' in text else text.split(delim))]
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    66
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    67
def strip(text):
3835
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3799
diff changeset
    68
    if isBytes(text): text = text.decode('utf8')
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
    69
    return text.strip(_wsc)
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
    70
518
5be3fcb26c78 Semantic Name changes
rgbecker
parents: 516
diff changeset
    71
class ParaLines(ABag):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    72
    """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    73
    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
    74
        kind=0  Simple
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    75
        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
    76
        lines   [(extraSpace1,words1),....,(extraspaceN,wordsN)]
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    77
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    78
        kind==1 Complex
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    79
        lines   [FragLine1,...,FragLineN]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    80
    """
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    81
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    82
class FragLine(ABag):
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    83
    """
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    84
    class FragLine contains a styled line (ie a line with more than one style)::
521
e09fc89a6bb3 Cosmetic changes
rgbecker
parents: 519
diff changeset
    85
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    86
        extraSpace  unused space for justification only
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    87
        wordCount   1+spaces in line for justification purposes
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    88
        words       [ParaFrags] style text lumps to be concatenated together
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    89
        fontSize    maximum fontSize seen on the line; not used at present,
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
    90
                    but could be used for line spacing.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    91
    """
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
    92
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
    93
def _lineClean(L):
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
    94
    return ' '.join(list(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
    95
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
    96
def cleanBlockQuotedText(text,joiner=' '):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
    97
    """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
    98
    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
    99
    (hopefully) the paragraph the user intended originally."""
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3656
diff changeset
   100
    L=list(filter(truth,list(map(_lineClean, split(text, '\n')))))
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   101
    return joiner.join(L)
539
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
   102
14dd200244e2 Layout fixes; the text objects needed moves before & after and the bullet width
rgbecker
parents: 534
diff changeset
   103
def setXPos(tx,dx):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   104
    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
   105
        tx.setXPos(dx)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   106
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   107
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
   108
    setXPos(tx,offset)
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   109
    tx._textOut(' '.join(words),1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   110
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   111
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   112
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   113
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
   114
    m = offset + 0.5 * extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   115
    setXPos(tx,m)
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   116
    tx._textOut(' '.join(words),1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   117
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   118
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   119
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   120
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
   121
    m = offset + extraspace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   122
    setXPos(tx,m)
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   123
    tx._textOut(' '.join(words),1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   124
    setXPos(tx,-m)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   125
    return m
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   126
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   127
def _nbspCount(w):
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   128
    if isBytes(w):
3835
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3799
diff changeset
   129
        return w.count(b'\xc2\xa0')
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   130
    else:
3888
3e3e5ba4bfcc paragraph.py: fix another 3 only failure
robin
parents: 3882
diff changeset
   131
        return w.count(u'\xa0')
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   132
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   133
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
   134
    setXPos(tx,offset)
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   135
    text  = ' '.join(words)
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   136
    if last or extraspace<=1e-8:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   137
        #last one, left align
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   138
        tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   139
    else:
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   140
        nSpaces = len(words)+sum([_nbspCount(w) for w in words])-1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   141
        if nSpaces:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   142
            tx.setWordSpace(extraspace / float(nSpaces))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   143
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   144
            tx.setWordSpace(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   145
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   146
            tx._textOut(text,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   147
    setXPos(tx,-offset)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   148
    return offset
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   149
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   150
def imgVRange(h,va,fontSize):
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   151
    '''return bottom,top offsets relative to baseline(0)'''
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   152
    if va=='baseline':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   153
        iyo = 0
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   154
    elif va in ('text-top','top'):
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   155
        iyo = fontSize-h
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   156
    elif va=='middle':
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   157
        iyo = fontSize - (1.2*fontSize+h)*0.5
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   158
    elif va in ('text-bottom','bottom'):
2863
63a40c330032 paragraph.py: restore & fix the attempt at ascent/descent auto leading
rgbecker
parents: 2862
diff changeset
   159
        iyo = fontSize - 1.2*fontSize
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   160
    elif va=='super':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   161
        iyo = 0.5*fontSize
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   162
    elif va=='sub':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   163
        iyo = -0.5*fontSize
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   164
    elif hasattr(va,'normalizedValue'):
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   165
        iyo = va.normalizedValue(fontSize)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   166
    else:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   167
        iyo = va
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   168
    return iyo,iyo+h
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   169
3434
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   170
def imgNormV(v,nv):
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   171
    if hasattr(v,'normalizedValue'):
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   172
        return v.normalizedValue(nv)
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   173
    else:
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   174
        return v
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   175
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   176
def _getDotsInfo(style):
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   177
    dots = style.endDots
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   178
    if isinstance(dots,str):
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   179
        text = dots
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   180
        fontName = style.fontName
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   181
        fontSize = style.fontSize
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   182
        textColor = style.textColor
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   183
        backColor = style.backColor
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   184
        dy = 0
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   185
    else:
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   186
        text = getattr(dots,'text','.')
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   187
        fontName = getattr(dots,'fontName',style.fontName)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   188
        fontSize = getattr(dots,'fontSize',style.fontSize)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   189
        textColor = getattr(dots,'textColor',style.textColor)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   190
        backColor = getattr(dots,'backColor',style.backColor)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   191
        dy = getattr(dots,'dy',0)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   192
    return text,fontName,fontSize,textColor,backColor,dy
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   193
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   194
_56=5./6
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   195
_16=1./6
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   196
def _putFragLine(cur_x, tx, line, last, pKind):
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   197
    xs = tx.XtraState
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   198
    cur_y = xs.cur_y
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   199
    x0 = tx._x0
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   200
    autoLeading = xs.autoLeading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   201
    leading = xs.leading
2910
5522f685b679 paragraph.py: fix leftIndent problem
rgbecker
parents: 2901
diff changeset
   202
    cur_x += xs.leftIndent
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   203
    dal = autoLeading in ('min','max')
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   204
    if dal:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   205
        if autoLeading=='max':
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   206
            ascent = max(_56*leading,line.ascent)
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   207
            descent = max(_16*leading,-line.descent)
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   208
        else:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   209
            ascent = line.ascent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   210
            descent = -line.descent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   211
        leading = ascent+descent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   212
    if tx._leading!=leading:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   213
        tx.setLeading(leading)
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   214
    if dal:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   215
        olb = tx._olb
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   216
        if olb is not None:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   217
            xcy = olb-ascent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   218
            if tx._oleading!=leading:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   219
                cur_y += leading - tx._oleading
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   220
            if abs(xcy-cur_y)>1e-8:
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   221
                cur_y = xcy
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   222
                tx.setTextOrigin(x0,cur_y)
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   223
                xs.cur_y = cur_y
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   224
        tx._olb = cur_y - descent
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   225
        tx._oleading = leading
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   226
    ws = getattr(tx,'_wordSpace',0)
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   227
    nSpaces = 0
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   228
    words = line.words
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   229
    for i, f in enumerate(words):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   230
        if hasattr(f,'cbDefn'):
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   231
            cbDefn = f.cbDefn
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   232
            kind = cbDefn.kind
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   233
            if kind=='img':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   234
                #draw image cbDefn,cur_y,cur_x
2871
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   235
                txfs = tx._fontsize
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   236
                if txfs is None:
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   237
                    txfs = xs.style.fontSize
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   238
                w = imgNormV(cbDefn.width,xs.paraWidth)
3434
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   239
                h = imgNormV(cbDefn.height,txfs)
2871
afd057e790cb paragraph.py: attempt to fix problem when inline img is first in line
rgbecker
parents: 2865
diff changeset
   240
                iy0,iy1 = imgVRange(h,cbDefn.valign,txfs)
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   241
                cur_x_s = cur_x + nSpaces*ws
2894
78cd38a39719 paragraph.py: add auto mask for inline images
rgbecker
parents: 2892
diff changeset
   242
                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
   243
                cur_x += w
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   244
                cur_x_s += w
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   245
                setXPos(tx,cur_x_s-tx._x0)
2744
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   246
            else:
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   247
                name = cbDefn.name
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   248
                if kind=='anchor':
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   249
                    tx._canvas.bookmarkHorizontal(name,cur_x,cur_y+leading)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   250
                else:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   251
                    func = getattr(tx._canvas,name,None)
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   252
                    if not func:
2892
131b417f9845 paragraph: improved <a> syntax checks and better test
rgbecker
parents: 2888
diff changeset
   253
                        raise AttributeError("Missing %s callback attribute '%s'" % (kind,name))
3048
3ff2383916b9 Renamed setencrypt to setEncrypt to follow naming convention.
jonas
parents: 3042
diff changeset
   254
                    tx._canvas._curr_tx_info=dict(tx=tx,cur_x=cur_x,cur_y=cur_y,leading=leading,xs=tx.XtraState)
3ff2383916b9 Renamed setencrypt to setEncrypt to follow naming convention.
jonas
parents: 3042
diff changeset
   255
                    try:
4231
7c45177d8a77 onDraw tag doesn't require a label
robin
parents: 4220
diff changeset
   256
                        func(tx._canvas,kind,getattr(cbDefn,'label',None))
3048
3ff2383916b9 Renamed setencrypt to setEncrypt to follow naming convention.
jonas
parents: 3042
diff changeset
   257
                    finally:
3ff2383916b9 Renamed setencrypt to setEncrypt to follow naming convention.
jonas
parents: 3042
diff changeset
   258
                        del tx._canvas._curr_tx_info
2750
05229265aff3 reportlab: fix special case <a> tag problem, add AnchorFlowable class
rgbecker
parents: 2745
diff changeset
   259
            if f is words[-1]:
05229265aff3 reportlab: fix special case <a> tag problem, add AnchorFlowable class
rgbecker
parents: 2745
diff changeset
   260
                if not tx._fontname:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   261
                    tx.setFont(xs.style.fontName,xs.style.fontSize)
3322
e27df3a204a1 pargraph.py: all eol cbDefns should do textOut('',1)
rgbecker
parents: 3192
diff changeset
   262
                tx._textOut('',1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   263
        else:
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   264
            cur_x_s = cur_x + nSpaces*ws
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   265
            end_x = cur_x_s
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   266
            if i > 0:
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   267
                end_x = cur_x_s - _trailingSpaceLength(words[i-1].text, tx)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   268
            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
   269
                tx._setFont(f.fontName, f.fontSize)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   270
            if xs.textColor!=f.textColor:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   271
                xs.textColor = f.textColor
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   272
                tx.setFillColor(f.textColor)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   273
            if xs.rise!=f.rise:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   274
                xs.rise=f.rise
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   275
                tx.setRise(f.rise)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   276
            text = f.text
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   277
            tx._textOut(text,f is words[-1])    # cheap textOut
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   278
            if not xs.underline and f.underline:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   279
                xs.underline = 1
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   280
                xs.underline_x = cur_x_s
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   281
                xs.underlineColor = f.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   282
            elif xs.underline:
2456
afa6ef178273 paragraph.py: added proper undeerline color
rgbecker
parents: 2445
diff changeset
   283
                if not f.underline:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   284
                    xs.underline = 0
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   285
                    xs.underlines.append( (xs.underline_x, end_x, xs.underlineColor) )
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   286
                    xs.underlineColor = None
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   287
                elif xs.textColor!=xs.underlineColor:
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   288
                    xs.underlines.append( (xs.underline_x, end_x, xs.underlineColor) )
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   289
                    xs.underlineColor = xs.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   290
                    xs.underline_x = cur_x_s
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   291
            if not xs.strike and f.strike:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   292
                xs.strike = 1
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   293
                xs.strike_x = cur_x_s
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   294
                xs.strikeColor = f.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   295
            elif xs.strike:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   296
                if not f.strike:
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   297
                    xs.strike = 0
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   298
                    xs.strikes.append( (xs.strike_x, end_x, xs.strikeColor) )
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   299
                    xs.strikeColor = None
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   300
                elif xs.textColor!=xs.strikeColor:
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   301
                    xs.strikes.append( (xs.strike_x, end_x, xs.strikeColor) )
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   302
                    xs.strikeColor = xs.textColor
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   303
                    xs.strike_x = cur_x_s
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   304
            if f.link and not xs.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   305
                if not xs.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   306
                    xs.link = f.link
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   307
                    xs.link_x = cur_x_s
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   308
                    xs.linkColor = xs.textColor
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   309
            elif xs.link:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   310
                if not f.link:
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   311
                    xs.links.append( (xs.link_x, end_x, xs.link, xs.linkColor) )
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   312
                    xs.link = None
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   313
                    xs.linkColor = None
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   314
                elif f.link!=xs.link or xs.textColor!=xs.linkColor:
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   315
                    xs.links.append( (xs.link_x, end_x, xs.link, xs.linkColor) )
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   316
                    xs.link = f.link
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   317
                    xs.link_x = cur_x_s
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   318
                    xs.linkColor = xs.textColor
3476
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   319
            bg = getattr(f,'backColor',None)
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   320
            if bg and not xs.backColor:
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   321
                xs.backColor = bg
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   322
                xs.backColor_x = cur_x_s
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   323
            elif xs.backColor:
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   324
                if not bg:
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   325
                    xs.backColors.append( (xs.backColor_x, end_x, xs.backColor) )
3476
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   326
                    xs.backColor = None
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   327
                elif f.backColor!=xs.backColor or xs.textColor!=xs.backColor:
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   328
                    xs.backColors.append( (xs.backColor_x, end_x, xs.backColor) )
3476
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   329
                    xs.backColor = bg
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   330
                    xs.backColor_x = cur_x_s
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   331
            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
   332
            cur_x += txtlen
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   333
            nSpaces += text.count(' ')+_nbspCount(text)
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   334
    cur_x_s = cur_x+(nSpaces-1)*ws
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   335
    if last and pKind!='right' and xs.style.endDots:
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   336
        _do_dots_frag(cur_x,cur_x_s,line.maxWidth,xs,tx)
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   337
    if xs.underline:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   338
        xs.underlines.append( (xs.underline_x, cur_x_s, xs.underlineColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   339
    if xs.strike:
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   340
        xs.strikes.append( (xs.strike_x, cur_x_s, xs.strikeColor) )
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   341
    if xs.link:
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   342
        xs.links.append( (xs.link_x, cur_x_s, xs.link,xs.linkColor) )
3476
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   343
    if xs.backColor:
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   344
        xs.backColors.append( (xs.backColor_x, cur_x_s, xs.backColor) )
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   345
    if tx._x0!=x0:
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   346
        setXPos(tx,x0-tx._x0)
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   347
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   348
def _do_dots_frag(cur_x, cur_x_s, maxWidth, xs, tx):
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   349
    text,fontName,fontSize,textColor,backColor,dy = _getDotsInfo(xs.style)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   350
    txtlen = tx._canvas.stringWidth(text, fontName, fontSize)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   351
    if cur_x_s+txtlen<=maxWidth:
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   352
        if tx._fontname!=fontName or tx._fontsize!=fontSize:
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   353
            tx.setFont(fontName,fontSize)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   354
        maxWidth += getattr(tx,'_dotsOffsetX',tx._x0)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   355
        tx.setTextOrigin(0,xs.cur_y+dy)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   356
        setXPos(tx,cur_x_s-cur_x)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   357
        n = int((maxWidth-cur_x_s)/txtlen)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   358
        setXPos(tx,maxWidth - txtlen*n)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   359
        if xs.textColor!=textColor:
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   360
            tx.setFillColor(textColor)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   361
        if backColor: xs.backColors.append((cur_x,maxWidth,backColor))
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   362
        tx._textOut(n*text,1)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   363
        if dy: tx.setTextOrigin(tx._x0,xs.cur_y-dy)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   364
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   365
def _leftDrawParaLineX( tx, offset, line, last=0):
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   366
    extraSpace = line.extraSpace
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   367
    simple = extraSpace>-1e-8 or getattr(line,'preformatted',False)
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   368
    if not simple:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   369
        nSpaces = line.wordCount+sum([_nbspCount(w.text) for w in line.words if not hasattr(w,'cbDefn')])-1
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   370
        simple = not nSpaces
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   371
    if simple:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   372
        setXPos(tx,offset)
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   373
        _putFragLine(offset, tx, line, last, 'left')
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   374
    else:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   375
        tx.setWordSpace(extraSpace / float(nSpaces))
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   376
        _putFragLine(offset, tx, line, last, 'left')
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   377
        tx.setWordSpace(0)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   378
    setXPos(tx,-offset)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   379
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   380
def _centerDrawParaLineX( tx, offset, line, last=0):
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   381
    tx._dotsOffsetX = offset + tx._x0
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   382
    try:
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   383
        extraSpace = line.extraSpace
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   384
        simple = extraSpace>-1e-8 or getattr(line,'preformatted',False)
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   385
        if not simple:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   386
            nSpaces = line.wordCount+sum([_nbspCount(w.text) for w in line.words if not hasattr(w,'cbDefn')])-1
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   387
            simple = not nSpaces
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   388
        if simple:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   389
            m = offset+0.5*line.extraSpace
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   390
            setXPos(tx,m)
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   391
            _putFragLine(m, tx, line, last,'center')
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   392
        else:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   393
            m = offset
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   394
            tx.setWordSpace(extraSpace / float(nSpaces))
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   395
            _putFragLine(m, tx, line, last, 'center')
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   396
            tx.setWordSpace(0)
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   397
        setXPos(tx,-m)
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   398
    finally:
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   399
        del tx._dotsOffsetX
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   400
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   401
def _rightDrawParaLineX( tx, offset, line, last=0):
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   402
    extraSpace = line.extraSpace
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   403
    simple = extraSpace>-1e-8 or getattr(line,'preformatted',False)
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   404
    if not simple:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   405
        nSpaces = line.wordCount+sum([_nbspCount(w.text) for w in line.words if not hasattr(w,'cbDefn')])-1
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   406
        simple = not nSpaces
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   407
    if simple:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   408
        m = offset+line.extraSpace
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   409
        setXPos(tx,m)
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   410
        _putFragLine(m,tx, line, last, 'right')
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   411
    else:
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   412
        m = offset
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   413
        tx.setWordSpace(extraSpace / float(nSpaces))
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   414
        _putFragLine(m, tx, line, last, 'right')
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   415
        tx.setWordSpace(0)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   416
    setXPos(tx,-m)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   417
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   418
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
   419
    setXPos(tx,offset)
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   420
    extraSpace = line.extraSpace
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   421
    simple = last or abs(extraSpace)<=1e-8 or line.lineBreak
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   422
    if not simple:
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   423
        nSpaces = line.wordCount+sum([_nbspCount(w.text) for w in line.words if not hasattr(w,'cbDefn')])-1
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   424
        simple = not nSpaces
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   425
    if not simple:
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   426
        tx.setWordSpace(extraSpace / float(nSpaces))
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   427
        _putFragLine(offset, tx, line, last, 'justify')
2840
69e2c8b05684 reportlab: fixes to paragraph <br/> tag
rgbecker
parents: 2836
diff changeset
   428
        tx.setWordSpace(0)
3373
127aed2a021d paragraph.py: fixup for nbsp
rgbecker
parents: 3326
diff changeset
   429
    else:
3575
6821eb805106 reportlab: added support for endDots in paragraphs styles
rgbecker
parents: 3559
diff changeset
   430
        _putFragLine(offset, tx, line, last, 'justify') #no space modification
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   431
    setXPos(tx,-offset)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   432
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   433
def _trailingSpaceLength(text, tx):
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   434
    ws = _wsc_end_search(text)
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   435
    return tx._canvas.stringWidth(ws.group(), tx._fontname, tx._fontsize) if ws else 0
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   436
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   437
class _HSWord(list):
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   438
    pass
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   439
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   440
class _SplitList(list):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   441
    pass
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   442
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   443
class _SplitListLast(_SplitList):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   444
    pass
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   445
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   446
class _HSSplitList(_HSWord):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   447
    pass
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   448
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   449
def _processed_frags(frags):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   450
    try:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   451
        return isinstance(frags[0][0],(float,int))
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   452
    except:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   453
        return False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   454
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   455
_FK_TEXT = 0
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   456
_FK_IMG = 1
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   457
_FK_APPEND = 2
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   458
_FK_BREAK = 3
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   459
3434
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   460
def _getFragWords(frags,maxWidth=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   461
    ''' 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
   462
        [[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
   463
        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
   464
        each sublist represents a word
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   465
    '''
4316
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   466
    def _rescaleFrag(f):
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   467
        w = f[0]
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   468
        if isinstance(w,_PCT):
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   469
            if w._normalizer!=maxWidth:
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   470
                w._normalizer = maxWidth
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   471
                w = w.normalizedValue(maxWidth)
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   472
                f[0] = w
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   473
    R = []
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   474
    R_append = R.append
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   475
    if _processed_frags(frags):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   476
        i = 0
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   477
        n = len(frags)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   478
        while i<n:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   479
            f = frags[i]
4316
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   480
            _rescaleFrag(f)
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   481
            if isinstance(f,(_SplitList,_HSSplitList,_SplitListLast)):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   482
                #we need to re-join these to make a single word
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   483
                W = [0]
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   484
                while True:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   485
                    W[0] += f[0]
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   486
                    W.extend(f[1:])
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   487
                    if isinstance(f,(_SplitListLast,_HSSplitList)):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   488
                        break
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   489
                    i += 1
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   490
                    if i==n: break  #exceptional case when a split paragraph split in the middle of such a sequence
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   491
                    f = frags[i]
4316
14c18006410a fix bug in _getFragWords
robin <robin@reportlab.com>
parents: 4315
diff changeset
   492
                    _rescaleFrag(f)
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   493
                if isinstance(f,_HSSplitList):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   494
                    f = _HSWord(W)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   495
                else:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   496
                    f = W
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   497
            R_append(f)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   498
            i += 1
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   499
    else:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   500
        W = []
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   501
        hangingSpace = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   502
        n = 0
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   503
        hangingStrip = True
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   504
        for f in frags:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   505
            text = f.text
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   506
            if text!='':
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   507
                f._fkind = _FK_TEXT
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   508
                if hangingStrip:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   509
                    text = text.lstrip()
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   510
                    if not text: continue
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   511
                    hangingStrip = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   512
                S = split(text)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   513
                if text[0] in whitespace:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   514
                    if W:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   515
                        W.insert(0,n)   #end preceding word
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   516
                        R_append(W)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   517
                        whs = hangingSpace
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   518
                        W = []
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   519
                        hangingSpace = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   520
                        n = 0
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   521
                    else:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   522
                        whs = R and isinstance(R[-1],_HSWord)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   523
                    if not whs:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   524
                        S.insert(0,'')
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   525
                    elif not S:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   526
                        continue
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   527
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   528
                for w in S[:-1]:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   529
                    W.append((f,w))
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   530
                    n += stringWidth(w, f.fontName, f.fontSize)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   531
                    W.insert(0,n)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   532
                    R_append(_HSWord(W))
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   533
                    W = []
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   534
                    n = 0
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   535
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   536
                hangingSpace = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   537
                w = S[-1]
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   538
                W.append((f,w))
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   539
                n += stringWidth(w, f.fontName, f.fontSize)
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   540
                if text and text[-1] in whitespace:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   541
                    W.insert(0,n)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   542
                    R_append(_HSWord(W))
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   543
                    W = []
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   544
                    n = 0
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   545
            elif hasattr(f,'cbDefn'):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   546
                cb = f.cbDefn
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   547
                w = getattr(cb,'width',0)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   548
                if w:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   549
                    if hasattr(w,'normalizedValue'):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   550
                        w._normalizer = maxWidth
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   551
                        w = w.normalizedValue(maxWidth)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   552
                    if W:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   553
                        W.insert(0,n)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   554
                        R_append(_HSWord(W) if hangingSpace else W)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   555
                        W = []
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   556
                        hangingSpace = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   557
                        n = 0
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   558
                    f._fkind = _FK_IMG
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   559
                    R_append([w,(f,'')])
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   560
                    hangingStrip = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   561
                else:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   562
                    f._fkind = _FK_APPEND
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   563
                    if not W and R and isinstance(R[-1],_HSWord):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   564
                        R[-1].append((f,''))
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   565
                    else:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   566
                        W.append((f,''))
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   567
            elif hasattr(f, 'lineBreak'):
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   568
                #pass the frag through.  The line breaker will scan for it.
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   569
                if W:
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   570
                    W.insert(0,n)
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   571
                    R_append(W)
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   572
                    W = []
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
   573
                    n = 0
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   574
                    hangingSpace = False
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   575
                f._fkind = _FK_BREAK
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   576
                R_append([0,(f,'')])
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   577
                hangingStrip = True
508
37dc6fc3fa65 More <onDraw> hacks
rgbecker
parents: 506
diff changeset
   578
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   579
        if W:
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   580
            W.insert(0,n)
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   581
            R_append(W)
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   582
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   583
    return R
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
   584
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   585
def _fragWordIter(w):
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   586
    for f, s in w[1:]:
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   587
        if hasattr(f,'cbDefn'):
4272
a3e48855dd48 fix AttributeError reported by Kay Schluehr bitbucket issue #81, version-->3.3.8
robin
parents: 4252
diff changeset
   588
            yield f, getattr(f,'width',0), s
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   589
        elif s:
3929
b8175c55af71 paragraph.py: fix bug in _fragWordIter (transplant from 3772 with changes)
robin
parents: 3921
diff changeset
   590
            if isBytes(s):
b8175c55af71 paragraph.py: fix bug in _fragWordIter (transplant from 3772 with changes)
robin
parents: 3921
diff changeset
   591
                s = s.decode('utf8')    #only encoding allowed
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   592
            for c in s:
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   593
                yield f, stringWidth(c,f.fontName, f.fontSize), c
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   594
        else:
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   595
            yield f, 0, s
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   596
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   597
def _splitFragWord(w,maxWidth,maxWidths,lineno):
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   598
    '''given a frag word, w, as returned by getFragWords
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   599
    split it into frag words that fit in lines of length
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   600
    maxWidth
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   601
    maxWidths[lineno+1]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   602
    .....
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   603
    maxWidths[lineno+n]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   604
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   605
    return the new word list which is either 
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   606
    _SplitList....._SPlitList or
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   607
    _splitList....._HSSplitList if the word is hanging space.
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   608
    '''
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   609
    R = []
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   610
    maxlineno = len(maxWidths)-1
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   611
    W = []
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   612
    lineWidth = 0
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
   613
    fragText = u''
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   614
    wordWidth = 0
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   615
    f = w[1][0]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   616
    for g,cw,c in _fragWordIter(w):
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   617
        newLineWidth = lineWidth+cw
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   618
        tooLong = newLineWidth>maxWidth
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   619
        if g is not f or tooLong:
3739
970f6cb12b90 fix bug in _splitFragWord and use splitLongWords
robin
parents: 3737
diff changeset
   620
            f = f.clone()
970f6cb12b90 fix bug in _splitFragWord and use splitLongWords
robin
parents: 3737
diff changeset
   621
            if hasattr(f,'text'):
970f6cb12b90 fix bug in _splitFragWord and use splitLongWords
robin
parents: 3737
diff changeset
   622
                f.text = fragText
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   623
            W.append((f,fragText))
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   624
            if tooLong:
3739
970f6cb12b90 fix bug in _splitFragWord and use splitLongWords
robin
parents: 3737
diff changeset
   625
                W = _SplitList([wordWidth]+W)
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   626
                R.append(W)
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   627
                lineno += 1
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   628
                maxWidth = maxWidths[min(maxlineno,lineno)]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   629
                W = []
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   630
                newLineWidth = cw
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   631
                wordWidth = 0
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
   632
            fragText = u''
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   633
            f = g
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   634
        wordWidth += cw
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   635
        fragText += c
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   636
        lineWidth = newLineWidth
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   637
    W.append((f,fragText))
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
   638
    W = _HSSplitList([wordWidth]+W) if isinstance(w,_HSWord) else _SplitListLast([wordWidth]+W)
4277
838129322a55 really merge para-measure-fix; version-->3.3.11
robin
parents: 4272
diff changeset
   639
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   640
    R.append(W)
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   641
    return R
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   642
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
   643
class _SplitText(unicodeT):
3739
970f6cb12b90 fix bug in _splitFragWord and use splitLongWords
robin
parents: 3737
diff changeset
   644
    pass
970f6cb12b90 fix bug in _splitFragWord and use splitLongWords
robin
parents: 3737
diff changeset
   645
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
   646
def _splitWord(w,maxWidth,maxWidths,lineno,fontName,fontSize,encoding='utf8'):
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   647
    '''
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   648
    split w into words that fit in lines of length
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   649
    maxWidth
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   650
    maxWidths[lineno+1]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   651
    .....
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   652
    maxWidths[lineno+n]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   653
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   654
    then push those new words onto words
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   655
    '''
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   656
    #TODO fix this to use binary search for the split points
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   657
    R = []
4280
7f3365bf11f2 fix issue reported by Yitzchak Scott-Thoennes <sthoenna@gmail.com>, version-->3.3.14
robin
parents: 4277
diff changeset
   658
    aR = R.append
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   659
    maxlineno = len(maxWidths)-1
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   660
    lineWidth = 0
3930
950841932d03 paragraph.py: fix encoding issue in _splitWord (transplant from 3774 with changes)
robin
parents: 3929
diff changeset
   661
    wordText = u''
950841932d03 paragraph.py: fix encoding issue in _splitWord (transplant from 3774 with changes)
robin
parents: 3929
diff changeset
   662
    if isBytes(w):
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
   663
        w = w.decode(encoding)
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   664
    for c in w:
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   665
        cw = stringWidth(c,fontName,fontSize,encoding)
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   666
        newLineWidth = lineWidth+cw
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   667
        if newLineWidth>maxWidth:
4280
7f3365bf11f2 fix issue reported by Yitzchak Scott-Thoennes <sthoenna@gmail.com>, version-->3.3.14
robin
parents: 4277
diff changeset
   668
            aR(_SplitText(wordText))
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   669
            lineno += 1
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   670
            maxWidth = maxWidths[min(maxlineno,lineno)]
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   671
            newLineWidth = cw
4046
aed0e2af4546 paragraph: fix _splitWord for unicode/bytes bug reported by Anders Hammarquist & add a test
robin
parents: 4035
diff changeset
   672
            wordText = u''
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   673
        wordText += c
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   674
        lineWidth = newLineWidth
4280
7f3365bf11f2 fix issue reported by Yitzchak Scott-Thoennes <sthoenna@gmail.com>, version-->3.3.14
robin
parents: 4277
diff changeset
   675
    aR(_SplitText(wordText))
7f3365bf11f2 fix issue reported by Yitzchak Scott-Thoennes <sthoenna@gmail.com>, version-->3.3.14
robin
parents: 4277
diff changeset
   676
    if not R[0]: del R[0]   #delete in special case
3737
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   677
    return R
610ffa01744e first try at word splitting
robin
parents: 3731
diff changeset
   678
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   679
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
   680
    f = blPara.clone()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   681
    for a in ('lines', 'kind', 'text'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   682
        if hasattr(f,a): delattr(f,a)
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   683
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   684
    f.words = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   685
    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
   686
        for w in l[1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   687
            f.words.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   688
    return [f]
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   689
532
fa0a46295460 Added a text background color attribute
andy_robinson
parents: 529
diff changeset
   690
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
   691
    f = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   692
    lines = blPara.lines[start:stop]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   693
    for l in lines:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   694
        for w in l.words:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   695
            f.append(w)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   696
        if l is not lines[-1]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   697
            i = len(f)-1
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   698
            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
   699
            if i>=0:
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   700
                g = f[i]
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   701
                if not g.text: g.text = ' '
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   702
                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
   703
    return f
212
b04e22acb4fa Splitting changes
rgbecker
parents: 208
diff changeset
   704
4094
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   705
def _drawBullet(canvas, offset, cur_y, bulletText, style, rtl):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   706
    '''draw a bullet text could be a simple string or a frag list'''
4136
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   707
    bulletAnchor = style.bulletAnchor
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   708
    if rtl or style.bulletAnchor!='start':
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   709
        numeric = bulletAnchor=='numeric'
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   710
        if isinstance(bulletText,strTypes):
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   711
            t =  bulletText
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   712
            q = numeric and decimalSymbol in t
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   713
            if q: t = t[:t.index(decimalSymbol)]
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   714
            bulletWidth = stringWidth(t, style.bulletFontName, style.bulletFontSize)
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   715
            if q: bulletWidth += 0.5 * stringWidth(decimalSymbol, style.bulletFontName, style.bulletFontSize)
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   716
        else:
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   717
            #it's a list of fragments
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   718
            bulletWidth = 0
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   719
            for f in bulletText:
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   720
                t = f.text
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   721
                q = numeric and decimalSymbol in t
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   722
                if q:
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   723
                    t = t[:t.index(decimalSymbol)]
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   724
                    bulletWidth += 0.5 * stringWidth(decimalSymbol, f.fontName, f.fontSize)
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   725
                bulletWidth += stringWidth(t, f.fontName, f.fontSize)
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   726
                if q:
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   727
                    break
4094
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   728
    else:
4136
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   729
        bulletWidth = 0
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   730
    if bulletAnchor=='middle': bulletWidth *= 0.5
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   731
    cur_y += getattr(style,"bulletOffsetY",0)
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   732
    if not rtl:
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   733
        tx2 = canvas.beginText(style.bulletIndent-bulletWidth,cur_y)
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   734
    else:
4094
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   735
        width = rtl[0]
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   736
        bulletStart = width+style.rightIndent-(style.bulletIndent+bulletWidth)
4136
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   737
        tx2 = canvas.beginText(bulletStart, cur_y)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   738
    tx2.setFont(style.bulletFontName, style.bulletFontSize)
4136
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   739
    tx2.setFillColor(getattr(style,'bulletColor',style.textColor))
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   740
    if isinstance(bulletText,strTypes):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   741
        tx2.textOut(bulletText)
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
        for f in bulletText:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   744
            tx2.setFont(f.fontName, f.fontSize)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   745
            tx2.setFillColor(f.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   746
            tx2.textOut(f.text)
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   747
2366
7dd247980b7d minor twitching on Indenter
rgbecker
parents: 2341
diff changeset
   748
    canvas.drawText(tx2)
4094
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   749
    if not rtl:
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   750
        #AR making definition lists a bit less ugly
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   751
        #bulletEnd = tx2.getX()
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   752
        bulletEnd = tx2.getX() + style.bulletFontSize * 0.6
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   753
        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
   754
    return offset
250
a1bcf9c6c21e <bullet> xml tag added
rgbecker
parents: 221
diff changeset
   755
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   756
def _handleBulletWidth(bulletText,style,maxWidths):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   757
    '''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
   758
    '''
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   759
    if bulletText:
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   760
        if isinstance(bulletText,strTypes):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   761
            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
   762
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   763
            #it's a list of fragments
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   764
            bulletWidth = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   765
            for f in bulletText:
4136
16f067cf3dae added rl_settings.decimalSymbol & support for simple bullet anchoring, version-->3.1.35
robin
parents: 4135
diff changeset
   766
                bulletWidth += stringWidth(f.text, f.fontName, f.fontSize)
4094
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   767
        bulletLen = style.bulletIndent + bulletWidth + 0.6 * style.bulletFontSize
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   768
        if style.wordWrap=='RTL':
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   769
            indent = style.rightIndent+style.firstLineIndent
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   770
        else:
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   771
            indent = style.leftIndent+style.firstLineIndent
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   772
        if bulletLen > indent:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   773
            #..then it overruns, and we have less space available on line 1
4094
186f3923093a transfrred rtl-support changes to default, version --> 3.1.13
robin
parents: 4067
diff changeset
   774
            maxWidths[0] -= (bulletLen - indent)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
   775
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   776
def splitLines0(frags,widths):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   777
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   778
    given a list of ParaFrags we return a list of ParaLines
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   779
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   780
    each ParaLine has
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   781
    1)  ExtraSpace
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   782
    2)  blankCount
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   783
    3)  [textDefns....]
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
   784
    each text definition is a (ParaFrag, start, limit) triplet
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   785
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   786
    #initialise the algorithm
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   787
    lines   = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   788
    lineNum = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   789
    maxW    = widths[lineNum]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   790
    i       = -1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   791
    l       = len(frags)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   792
    lim     = start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   793
    while 1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   794
        #find a non whitespace character
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   795
        while i<l:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   796
            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
   797
            if start==lim:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   798
                i += 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   799
                if i==l: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   800
                start = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   801
                f = frags[i]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   802
                text = f.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   803
                lim = len(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   804
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   805
                break   # we found one
551
21b8a2cb8c8b Hack to fix Dinu's problem
rgbecker
parents: 549
diff changeset
   806
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   807
        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
   808
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   809
        #start of a line
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   810
        g       = (None,None,None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   811
        line    = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   812
        cLen    = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   813
        nSpaces = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   814
        while cLen<maxW:
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
   815
            j = text.find(' ',start)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   816
            if j<0: j==lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   817
            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
   818
            cLen += w
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   819
            if cLen>maxW and line!=[]:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   820
                cLen = cLen-w
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   821
                #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
   822
                while g.text[lim]==' ':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   823
                    lim = lim - 1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   824
                    nSpaces = nSpaces-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   825
                break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   826
            if j<0: j = lim
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   827
            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
   828
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   829
                g = (f,start,j)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   830
                line.append(g)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
   831
            if j==lim:
2593
3adaab508968 reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents: 2584
diff changeset
   832
                i += 1
549
f9e66c978f3a Start on new simpler line splitter
rgbecker
parents: 547
diff changeset
   833
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   834
def _old_do_line(tx, x1, y1, x2, y2):
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   835
    tx._canvas.line(x1, y1, x2, y2)
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   836
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   837
def _do_line(tx, x1, y1, x2, y2):
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   838
    olw = tx._canvas._lineWidth
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   839
    nlw = tx._underlineProportion*tx._fontsize
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   840
    if nlw!=olw:
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   841
        tx._canvas.setLineWidth(nlw)
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   842
        tx._canvas.line(x1, y1, x2, y2)
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   843
        tx._canvas.setLineWidth(olw)
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   844
    else:
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   845
        tx._canvas.line(x1, y1, x2, y2)
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   846
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   847
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
   848
    y = tx.XtraState.cur_y - i*tx.XtraState.style.leading + lm*tx.XtraState.f.fontSize
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   849
    textlen = tx._canvas.stringWidth(' '.join(tx.XtraState.lines[i][1]), tx._fontname, tx._fontsize)
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   850
    tx._do_line(t_off, y, t_off+textlen, y)
1915
5a94ea499657 Added in Ury Marshak's underlining stuff hacked for <2.2
rgbecker
parents: 1752
diff changeset
   851
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   852
_scheme_re = re.compile('^[a-zA-Z][-+a-zA-Z0-9]+$')
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   853
def _doLink(tx,link,rect):
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   854
    parts = link.split(':',1)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   855
    scheme = len(parts)==2 and parts[0].lower() or ''
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   856
    if _scheme_re.match(scheme) and scheme!='document':
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   857
        kind=scheme.lower()=='pdf' and 'GoToR' or 'URI'
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   858
        if kind=='GoToR': link = parts[1]
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   859
        tx._canvas.linkURL(link, rect, relative=1, kind=kind)
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   860
    else:
2744
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   861
        if link[0]=='#':
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   862
            link = link[1:]
9472eedb9702 reportlab/platypus: add two way <a> tag
rgbecker
parents: 2735
diff changeset
   863
            scheme=''
2594
746800f5caf9 reportlab: fix up links in paragraphs
rgbecker
parents: 2593
diff changeset
   864
        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
   865
2708
13619490e003 paragraph.py: fix underlining position errors
rgbecker
parents: 2681
diff changeset
   866
def _do_link_line(i, t_off, ws, tx):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   867
    xs = tx.XtraState
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   868
    leading = xs.style.leading
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   869
    y = xs.cur_y - i*leading - xs.f.fontSize/8.0 # 8.0 factor copied from para.py
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3721
diff changeset
   870
    text = ' '.join(xs.lines[i][1])
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   871
    textlen = tx._canvas.stringWidth(text, tx._fontname, tx._fontsize)
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   872
    _doLink(tx, xs.link, (t_off, y, t_off+textlen, y+leading))
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   873
2899
bdcad3b5bf68 attempt to fix offset problems with images
rgbecker
parents: 2898
diff changeset
   874
def _do_post_text(tx):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   875
    xs = tx.XtraState
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   876
    leading = xs.style.leading
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   877
    autoLeading = xs.autoLeading
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   878
    f = xs.f
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   879
    if autoLeading=='max':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   880
        leading = max(leading,1.2*f.fontSize)
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   881
    elif autoLeading=='min':
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   882
        leading = 1.2*f.fontSize
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   883
    ff = 0.125*f.fontSize
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   884
    y0 = xs.cur_y
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   885
    y = y0 - ff
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   886
    csc = None
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   887
    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
   888
        if c!=csc:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   889
            tx._canvas.setStrokeColor(c)
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   890
            csc = c
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   891
        tx._do_line(x1, y, x2, y)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   892
    xs.underlines = []
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   893
    xs.underline=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   894
    xs.underlineColor=None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   895
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   896
    ys = y0 + 2*ff
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   897
    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
   898
        if c!=csc:
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   899
            tx._canvas.setStrokeColor(c)
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   900
            csc = c
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   901
        tx._do_line(x1, ys, x2, ys)
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   902
    xs.strikes = []
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   903
    xs.strike=0
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   904
    xs.strikeColor=None
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
   905
4156
e71d19a8699c modify backColor layout behaviour, fix single span backColor, bump to 3.1.43
robin
parents: 4136
diff changeset
   906
    yl = y0 + f.fontSize
e71d19a8699c modify backColor layout behaviour, fix single span backColor, bump to 3.1.43
robin
parents: 4136
diff changeset
   907
    ydesc = yl - leading
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   908
    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
   909
        if platypus_link_underline:
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   910
            if c!=csc:
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   911
                tx._canvas.setStrokeColor(c)
9223e843c42a reprotlab: add platypus_link_underline and fix some paragraph issues
rgbecker
parents: 2886
diff changeset
   912
                csc = c
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
   913
            tx._do_line(x1, y, x2, y)
4156
e71d19a8699c modify backColor layout behaviour, fix single span backColor, bump to 3.1.43
robin
parents: 4136
diff changeset
   914
        _doLink(tx, link, (x1, ydesc, x2, yl))
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   915
    xs.links = []
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   916
    xs.link=None
2886
7c66d24b6845 paragraph.py: fix some bugs in paragraph and bring xpreformatted nearer
rgbecker
parents: 2884
diff changeset
   917
    xs.linkColor=None
3476
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   918
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   919
    for x1,x2,c in xs.backColors:
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   920
        tx._canvas.setFillColor(c)
4156
e71d19a8699c modify backColor layout behaviour, fix single span backColor, bump to 3.1.43
robin
parents: 4136
diff changeset
   921
        tx._canvas.rect(x1,ydesc,x2-x1,leading,stroke=0,fill=1)
3476
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   922
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   923
    xs.backColors=[]
0d4b3cfbbd04 paragraph.py: add in support for backColor in <font> tag
rgbecker
parents: 3440
diff changeset
   924
    xs.backColor=None
2836
66fb84201abe platypus: add support for autoLeading
rgbecker
parents: 2791
diff changeset
   925
    xs.cur_y -= leading
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
   926
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   927
def textTransformFrags(frags,style):
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   928
    tt = style.textTransform
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   929
    if tt:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   930
        tt=tt.lower()
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   931
        if tt=='lowercase':
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   932
            tt = unicodeT.lower
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   933
        elif tt=='uppercase':
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   934
            tt = unicodeT.upper
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   935
        elif  tt=='capitalize':
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   936
            tt = unicodeT.title
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   937
        elif tt=='none':
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   938
            return
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   939
        else:
3559
fb200750d086 paragraph.py: remove unwanted code
rgbecker
parents: 3555
diff changeset
   940
            raise ValueError('ParaStyle.textTransform value %r is invalid' % style.textTransform)
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   941
        n = len(frags)
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   942
        if n==1:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   943
            #single fragment the easy case
3799
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   944
            frags[0].text = tt(frags[0].text)
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   945
        elif tt is unicodeT.title:
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   946
            pb = True
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   947
            for f in frags:
3799
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   948
                u = f.text
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   949
                if not u: continue
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   950
                if u.startswith(u' ') or pb:
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   951
                    u = tt(u)
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   952
                else:
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   953
                    i = u.find(u' ')
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   954
                    if i>=0:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   955
                        u = u[:i]+tt(u[i:])
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   956
                pb = u.endswith(u' ')
3799
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   957
                f.text = u
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   958
        else:
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   959
            for f in frags:
3799
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   960
                u = f.text
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   961
                if not u: continue
9e42094b0298 paragraph.py: move towards no decoding/encoding
robin
parents: 3781
diff changeset
   962
                f.text = tt(u)
2936
97860052e0fc paragraph.py: make textTransformFrags externall callable
rgbecker
parents: 2935
diff changeset
   963
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   964
class cjkU(unicodeT):
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   965
    '''simple class to hold the frag corresponding to a str'''
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   966
    def __new__(cls,value,frag,encoding):
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   967
        self = unicodeT.__new__(cls,value)
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   968
        self._frag = frag
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   969
        if hasattr(frag,'cbDefn'):
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   970
            w = getattr(frag.cbDefn,'width',0)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   971
            self._width = w
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   972
        else:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   973
            self._width = stringWidth(value,frag.fontName,frag.fontSize)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   974
        return self
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   975
    frag = property(lambda self: self._frag)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   976
    width = property(lambda self: self._width)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   977
3624
c85cbc87b630 paragraph.py: fix CJK + endDots
rgbecker
parents: 3617
diff changeset
   978
def makeCJKParaLine(U,maxWidth,widthUsed,extraSpace,lineBreak,calcBounds):
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   979
    words = []
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   980
    CW = []
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   981
    f0 = FragLine()
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   982
    maxSize = maxAscent = minDescent = 0
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   983
    for u in U:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   984
        f = u.frag
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   985
        fontSize = f.fontSize
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   986
        if calcBounds:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   987
            cbDefn = getattr(f,'cbDefn',None)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   988
            if getattr(cbDefn,'width',0):
3434
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
   989
                descent, ascent = imgVRange(imgNormV(cbDefn.height,fontSize),cbDefn.valign,fontSize)
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   990
            else:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   991
                ascent, descent = getAscentDescent(f.fontName,fontSize)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   992
        else:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   993
            ascent, descent = getAscentDescent(f.fontName,fontSize)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   994
        maxSize = max(maxSize,fontSize)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   995
        maxAscent = max(maxAscent,ascent)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   996
        minDescent = min(minDescent,descent)
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3761
diff changeset
   997
        if not sameFrag(f0,f):
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
   998
            f0=f0.clone()
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
   999
            f0.text = u''.join(CW)
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1000
            words.append(f0)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1001
            CW = []
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1002
            f0 = f
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1003
        CW.append(u)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1004
    if CW:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1005
        f0=f0.clone()
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
  1006
        f0.text = u''.join(CW)
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1007
        words.append(f0)
3624
c85cbc87b630 paragraph.py: fix CJK + endDots
rgbecker
parents: 3617
diff changeset
  1008
    return FragLine(kind=1,extraSpace=extraSpace,wordCount=1,words=words[1:],fontSize=maxSize,ascent=maxAscent,descent=minDescent,maxWidth=maxWidth,currentWidth=widthUsed,lineBreak=lineBreak)
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1009
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1010
def cjkFragSplit(frags, maxWidths, calcBounds, encoding='utf8'):
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1011
    '''This attempts to be wordSplit for frags using the dumb algorithm'''
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1012
    U = []  #get a list of single glyphs with their widths etc etc
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1013
    for f in frags:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1014
        text = f.text
3921
4fab94274861 paragraph.py: fix some things that used to be unicode etc etc
robin
parents: 3888
diff changeset
  1015
        if isBytes(text):
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1016
            text = text.decode(encoding)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1017
        if text:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1018
            U.extend([cjkU(t,f,encoding) for t in text])
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1019
        else:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1020
            U.append(cjkU(text,f,encoding))
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1021
    lines = []
3156
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1022
    i = widthUsed = lineStartPos = 0
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1023
    maxWidth = maxWidths[0]
3156
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1024
    nU = len(U)
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1025
    while i<nU:
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1026
        u = U[i]
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1027
        i += 1
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1028
        w = u.width
3438
3cac4523a459 paragraph.py: fix cjk breaking for <img width='x%'
rgbecker
parents: 3434
diff changeset
  1029
        if hasattr(w,'normalizedValue'):
3434
3c14212cc997 platypus: preliminary working version of % height/width for <img> tag
rgbecker
parents: 3373
diff changeset
  1030
            w._normalizer = maxWidth
4315
7c65c6e52b13 fix paragraph splitting bug (reporters Olivia Zhang & Echo Bell); version-->3.3.31
robin <robin@reportlab.com>
parents: 4313
diff changeset
  1031
            w = w.normalizedValue(maxWidth)
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1032
        widthUsed += w
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1033
        lineBreak = hasattr(u.frag,'lineBreak')
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1034
        endLine = (widthUsed>maxWidth + _FUZZ and widthUsed>0) or lineBreak
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1035
        if endLine:
3156
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1036
            extraSpace = maxWidth - widthUsed
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1037
            if not lineBreak:
3547
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1038
                if ord(u)<0x3000:
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1039
                    # we appear to be inside a non-Asian script section.
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1040
                    # (this is a very crude test but quick to compute).
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1041
                    # This is likely to be quite rare so the speed of the
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1042
                    # code below is hopefully not a big issue.  The main
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1043
                    # situation requiring this is that a document title
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1044
                    # with an english product name in it got cut.
3559
fb200750d086 paragraph.py: remove unwanted code
rgbecker
parents: 3555
diff changeset
  1045
fb200750d086 paragraph.py: remove unwanted code
rgbecker
parents: 3555
diff changeset
  1046
fb200750d086 paragraph.py: remove unwanted code
rgbecker
parents: 3555
diff changeset
  1047
                    # we count back and look for
3547
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1048
                    #  - a space-like character
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1049
                    #  - reversion to Kanji (which would be a good split point)
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1050
                    #  - in the worst case, roughly half way back along the line
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1051
                    limitCheck = (lineStartPos+i)>>1        #(arbitrary taste issue)
4156
e71d19a8699c modify backColor layout behaviour, fix single span backColor, bump to 3.1.43
robin
parents: 4136
diff changeset
  1052
                    for j in xrange(i-1,limitCheck,-1):
3547
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1053
                        uj = U[j]
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1054
                        if uj and category(uj)=='Zs' or ord(uj)>=0x3000:
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1055
                            k = j+1
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1056
                            if k<i:
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1057
                                j = k+1
4156
e71d19a8699c modify backColor layout behaviour, fix single span backColor, bump to 3.1.43
robin
parents: 4136
diff changeset
  1058
                                extraSpace += sum(U[ii].width for ii in xrange(j,i))
3547
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1059
                                w = U[k].width
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1060
                                u = U[k]
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1061
                                i = j
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1062
                                break
6d9cf43ebf9d western character modificaton for CJK splitting
rgbecker
parents: 3540
diff changeset
  1063
3492
9f7288085d44 reportlab: fix support for cjk splitting
rgbecker
parents: 3489
diff changeset
  1064
                #we are pushing this character back, but
9f7288085d44 reportlab: fix support for cjk splitting
rgbecker
parents: 3489
diff changeset
  1065
                #the most important of the Japanese typography rules
9f7288085d44 reportlab: fix support for cjk splitting
rgbecker
parents: 3489
diff changeset
  1066
                #if this character cannot start a line, wrap it up to this line so it hangs
3156
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1067
                #in the right margin. We won't do two or more though - that's unlikely and
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1068
                #would result in growing ugliness.
3489
5a3b2a822a33 paragraph.py: improve cjk frag splitting
rgbecker
parents: 3476
diff changeset
  1069
                #and increase the extra space
5a3b2a822a33 paragraph.py: improve cjk frag splitting
rgbecker
parents: 3476
diff changeset
  1070
                #bug fix contributed by Alexander Vasilenko <alexs.vasilenko@gmail.com>
3492
9f7288085d44 reportlab: fix support for cjk splitting
rgbecker
parents: 3489
diff changeset
  1071
                if u not in ALL_CANNOT_START and i>lineStartPos+1:
9f7288085d44 reportlab: fix support for cjk splitting
rgbecker
parents: 3489
diff changeset
  1072
                    #otherwise we need to push the character back
9f7288085d44 reportlab: fix support for cjk splitting
rgbecker
parents: 3489
diff changeset
  1073
                    #the i>lineStart+1 condition ensures progress
3489
5a3b2a822a33 paragraph.py: improve cjk frag splitting
rgbecker
parents: 3476
diff changeset
  1074
                    i -= 1
5a3b2a822a33 paragraph.py: improve cjk frag splitting
rgbecker
parents: 3476
diff changeset
  1075
                    extraSpace += w
3624
c85cbc87b630 paragraph.py: fix CJK + endDots
rgbecker
parents: 3617
diff changeset
  1076
            lines.append(makeCJKParaLine(U[lineStartPos:i],maxWidth,widthUsed,extraSpace,lineBreak,calcBounds))
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1077
            try:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1078
                maxWidth = maxWidths[len(lines)]
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1079
            except IndexError:
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1080
                maxWidth = maxWidths[-1]  # use the last one
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1081
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1082
            lineStartPos = i
3489
5a3b2a822a33 paragraph.py: improve cjk frag splitting
rgbecker
parents: 3476
diff changeset
  1083
            widthUsed = 0
3156
b696c4ce59a8 paragraph.py: attempt to fix up the br tag for CJK
rgbecker
parents: 3154
diff changeset
  1084
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1085
    #any characters left?
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1086
    if widthUsed > 0:
3624
c85cbc87b630 paragraph.py: fix CJK + endDots
rgbecker
parents: 3617
diff changeset
  1087
        lines.append(makeCJKParaLine(U[lineStartPos:],maxWidth,widthUsed,maxWidth-widthUsed,False,calcBounds))
2940
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1088
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1089
    return ParaLines(kind=1,lines=lines)
a0e54b5117ce reportlab: add multi-frag cjk
rgbecker
parents: 2938
diff changeset
  1090
131
72cb089e6fda Removed from layout.py
rgbecker
parents:
diff changeset
  1091
class Paragraph(Flowable):
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1092
    """ 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
  1093
        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
  1094
        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
  1095
        bulletText is an optional bullet defintion.
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1096
        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
  1097
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1098
        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
  1099
        into a paragraph with a given style.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
  1100
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1101
        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
  1102
        <b> ... </b> - bold
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1103
        <i> ... </i> - italics
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1104
        <u> ... </u> - underline
2644
e762ad1c8909 reportlab: add support for strike through
rgbecker
parents: 2598
diff changeset
  1105
        <strike> ... </strike> - strike through
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1106
        <super> ... </super> - superscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1107
        <sub> ... </sub> - subscript
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1108
        <font name=fontfamily/fontname color=colorname size=float>
3552
20ecbcc53c15 paraparser.py add support for <span style=stylename>
rgbecker
parents: 3547
diff changeset
  1109
        <span name=fontfamily/fontname color=colorname backcolor=colorname size=float style=stylename>
3165
cbda9e7d0ee3 reportlab: new index support
rgbecker
parents: 3159
diff changeset
  1110
        <onDraw name=callable label="a label"/>
cbda9e7d0ee3 reportlab: new index support
rgbecker
parents: 3159
diff changeset
  1111
        <index [name="callablecanvasattribute"] label="a label"/>
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
  1112
        <link>link text</link>
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1113
        attributes of links
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1114
        size/fontSize=num
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1115
        name/face/fontName=name
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1116
        fg/textColor/color=color
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1117
        backcolor/backColor/bgcolor=color
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1118
        dest/destination/target/href/link=target
2745
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
  1119
        <a>anchor text</a>
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1120
        attributes of anchors
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1121
        fontSize=num
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1122
        fontName=name
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1123
        fg/textColor/color=color
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1124
        backcolor/backColor/bgcolor=color
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1125
        href=href
2745
0b44535fa1a5 paraparser/paragraph.py: fix comments
rgbecker
parents: 2744
diff changeset
  1126
        <a name="anchorpoint"/>
2670
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
  1127
        <unichar name="unicode character name"/>
3fdd642a7b76 minor cosmetic changes
rgbecker
parents: 2664
diff changeset
  1128
        <unichar value="unicode code point"/>
2857
487dc2450eec reprotlab: inline images horizontal positioning OK
rgbecker
parents: 2841
diff changeset
  1129
        <img src="path" width="1in" height="1in" valign="bottom"/>
3440
739ddbe7feab paaraparser/paragraph.py: add info re percentage in <img> for idea contributed by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents: 3438
diff changeset
  1130
                width="w%" --> fontSize*w/100   idea from Roberto Alsina
739ddbe7feab paaraparser/paragraph.py: add info re percentage in <img> for idea contributed by Roberto Alsina <ralsina@netmanagers.com.ar>
rgbecker
parents: 3438
diff changeset
  1131
                height="h%" --> linewidth*h/100 <ralsina@netmanagers.com.ar>
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
  1132
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1133
        The whole may be surrounded by <para> </para> tags
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
  1134
2791
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
  1135
        The <b> and <i> tags will work for the built-in fonts (Helvetica
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
  1136
        /Times / Courier).  For other fonts you need to register a family
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
  1137
        of 4 fonts using reportlab.pdfbase.pdfmetrics.registerFont; then
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
  1138
        use the addMapping function to tell the library that these 4 fonts
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
  1139
        form a family e.g.
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1140
        from reportlab.lib.fonts import addMapping
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1141
        addMapping('Vera', 0, 0, 'Vera')    #normal
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1142
        addMapping('Vera', 0, 1, 'Vera-Italic')    #italic
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1143
        addMapping('Vera', 1, 0, 'Vera-Bold')    #bold
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3027
diff changeset
  1144
        addMapping('Vera', 1, 1, 'Vera-BoldItalic')    #italic and bold
2791
806b0a5177c1 added docs on mapping
andy
parents: 2758
diff changeset
  1145
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1146
        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
  1147
    """
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1148
    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
  1149
        self.caseSensitive = caseSensitive
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2561
diff changeset
  1150
        self.encoding = encoding
3555
8a805f3ead47 paragraph.py allow bulletText in the style
rgbecker
parents: 3552
diff changeset
  1151
        self._setup(text, style, bulletText or getattr(style,'bulletText',None), frags, cleanBlockQuotedText)
438
fa5d480207bc Preparing for cleanup
rgbecker
parents: 434
diff changeset
  1152
3656
b9af7f5459d0 get rid of global paraparser
Andy Robinson <andy@reportlab.com>
parents: 3624
diff changeset
  1153
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1154
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1155
        n = self.__class__.__name__
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1156
        L = [n+"("]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3656
diff changeset
  1157
        keys = list(self.__dict__.keys())
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1158
        for k in keys:
3088
6beaabf3fd4e paragraph.py: improve repr
rgbecker
parents: 3048
diff changeset
  1159
            L.append('%s: %s' % (repr(k).replace("\n", " ").replace("  "," "),repr(getattr(self, k)).replace("\n", " ").replace("  "," ")))
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1160
        L.append(") #"+n)
2881
41b10eb435c1 paragraph.py: fix autoLeading
rgbecker
parents: 2877
diff changeset
  1161
        return '\n'.join(L)
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 540
diff changeset
  1162
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1163
    def _setup(self, text, style, bulletText, frags, cleaner):
4135
226667ad6b1f Viktoras Agejevas' proportional underlining patch (slightly modified); version -->3.1.34
robin
parents: 4098
diff changeset
  1164
3656
b9af7f5459d0 get rid of global paraparser
Andy Robinson <andy@reportlab.com>
parents: 3624
diff changeset
  1165
        #This used to be a global parser to save overhead.
b9af7f5459d0 get rid of global paraparser
Andy Robinson <andy@reportlab.com>
parents: 3624
diff changeset
  1166
        #In the interests of thread safety it is being instantiated per paragraph.
b9af7f5459d0 get rid of global paraparser
Andy Robinson <andy@reportlab.com>
parents: 3624
diff changeset
  1167
        #On the next release, we'll replace with a cElementTree parser
b9af7f5459d0 get rid of global paraparser
Andy Robinson <andy@reportlab.com>
parents: 3624
diff changeset
  1168
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1169
        if frags is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1398
diff changeset
  1170
            text = cleaner(text)
4035
49c5ac6a58e8 paragraph.py: create parser only if wanted
robin
parents: 3930
diff changeset
  1171
            _parser = ParaParser()
1940
baa0abc136c4 Henning von Bargen's caseSensitive flag
rgbecker
parents: 1915
diff changeset
  1172
            _parser.caseSensitive = self.caseSensitive
1677
1450177dd19e Exterminated all tab charact