~ubuntu-branches/ubuntu/vivid/python-reportlab/vivid-proposed

« back to all changes in this revision

Viewing changes to src/reportlab/lib/textsplit.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-01-12 02:16:21 UTC
  • mfrom: (1.2.9) (4.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20140112021621-f4mpp5qaj1dkq2yb
Tags: 2.8~20140112-1
* New upstream snapshot.
* Build python3 packages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
"""
12
12
 
13
 
__version__=''' $Id: textsplit.py 3959 2012-09-27 14:39:39Z robin $ '''
 
13
__version__=''' $Id$ '''
14
14
 
15
 
from types import StringType, UnicodeType
16
15
from unicodedata import category
17
16
from reportlab.pdfbase.pdfmetrics import stringWidth
18
17
from reportlab.rl_config import _FUZZ
 
18
from reportlab.lib.utils import isUnicode
19
19
 
20
20
CANNOT_START_LINE = [
21
21
    #strongly prohibited e.g. end brackets, stop, exclamation...
72
72
    >>> wordSplit('HelloWorld', 31, 'Courier', 10)
73
73
    [[1.0, 'Hello'], [1.0, 'World']]
74
74
    """
75
 
    if type(word) is not UnicodeType:
 
75
    if not isUnicode(word):
76
76
        uword = word.decode(encoding)
77
77
    else:
78
78
        uword = word
80
80
    charWidths = getCharWidths(uword, fontName, fontSize)
81
81
    lines = dumbSplit(uword, charWidths, maxWidths)
82
82
 
83
 
    if type(word) is not UnicodeType:
 
83
    if not isUnicode(word):
84
84
        lines2 = []
85
85
        #convert back
86
86
        for (extraSpace, text) in lines:
115
115
    (u'\u65e5\u672c\u8a9e', u'\u306f\u96e3\u3057\u3044\u3067\u3059\u306d\uff01')
116
116
    """
117
117
    if not isinstance(maxWidths,(list,tuple)): maxWidths = [maxWidths]
118
 
    assert type(word) is UnicodeType
 
118
    assert isUnicode(word)
119
119
    lines = []
120
120
    i = widthUsed = lineStartPos = 0
121
121
    maxWidth = maxWidths[0]
141
141
                #  - reversion to Kanji (which would be a good split point)
142
142
                #  - in the worst case, roughly half way back along the line
143
143
                limitCheck = (lineStartPos+i)>>1        #(arbitrary taste issue)
144
 
                for j in xrange(i-1,limitCheck,-1):
 
144
                for j in range(i-1,limitCheck,-1):
145
145
                    cj = word[j]
146
146
                    if category(cj)=='Zs' or ord(cj)>=0x3000:
147
147
                        k = j+1
224
224
#
225
225
#  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061
226
226
import re
227
 
rx=re.compile(u"([\u2e80-\uffff])", re.UNICODE)
 
227
rx=re.compile("([\u2e80-\uffff])", re.UNICODE)
228
228
def cjkwrap(text, width, encoding="utf8"):
229
229
     return reduce(lambda line, word, width=width: '%s%s%s' %
230
230
                (line,
232
232
                       + len(word.split('\n',1)[0] ) >= width) or
233
233
                      line[-1:] == '\0' and 2],
234
234
                 word),
235
 
                rx.sub(r'\1\0 ', unicode(text,encoding)).split(' ')
 
235
                rx.sub(r'\1\0 ', str(text,encoding)).split(' ')
236
236
            ).replace('\0', '').encode(encoding)
237
237
 
238
238
if __name__=='__main__':
239
239
    import doctest
240
 
    import textsplit
 
240
    from reportlab.lib import textsplit
241
241
    doctest.testmod(textsplit)