src/reportlab/lib/rl_accel.py
author robin
Fri, 07 Feb 2014 16:51:11 +0000
branchpy33
changeset 4026 6c8ac18e0c9c
parent 4002 4bf4b598196e
child 4071 8a945e72d376
permissions -rw-r--r--
rl_accel.py: fix global setting & calcChecksum
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     1
#this is the interface module that imports all from the C extension _rl_accel
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     2
_c_funcs = {}
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     3
_py_funcs = {}
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     4
### NOTE!  FP_STR SHOULD PROBABLY ALWAYS DO A PYTHON STR() CONVERSION ON ARGS
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     5
### IN CASE THEY ARE "LAZY OBJECTS".  ACCELLERATOR DOESN'T DO THIS (YET)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     6
__all__ = list(filter(None,'''
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     7
        fp_str
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     8
        unicode2T1
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
     9
        instanceStringWidthT1
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    10
        instanceStringWidthTTF
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    11
        asciiBase85Encode
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    12
        asciiBase85Decode
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    13
        escapePDF
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    14
        sameFrag
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    15
        calcChecksum
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    16
        add32
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    17
        hex32
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    18
        '''.split()))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    19
import __main__
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    20
testing = getattr(__main__,'_rl_testing',False)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    21
del __main__
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
    22
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    23
for fn in __all__:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    24
    try:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    25
        exec('from reportlab.lib._rl_accel import %s as f' % fn)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    26
        _c_funcs[fn] = f
3862
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
    27
        if testing: _py_funcs[fn] = None
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    28
    except ImportError:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    29
        _py_funcs[fn] = None
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    30
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    31
if _py_funcs:
4002
4bf4b598196e _rl_accel.c & rl_accel.py: fix aascii85 encode decode to do the 'right' thing
robin
parents: 3879
diff changeset
    32
    from reportlab.lib.utils import isBytes, isUnicode, isSeq, isPy3, rawBytes, asNative
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    33
    from math import log
3862
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
    34
    from struct import unpack
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    35
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    36
if 'fp_str' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    37
    _log_10 = lambda x,log=log,_log_e_10=log(10.0): log(x)/_log_e_10
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    38
    _fp_fmts = "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f"
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
    39
    def fp_str(*a):
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    40
        '''convert separate arguments (or single sequence arg) into space separated numeric strings'''
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    41
        if len(a)==1 and isSeq(a[0]): a = a[0]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    42
        s = []
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    43
        A = s.append
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    44
        for i in a:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    45
            sa =abs(i)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    46
            if sa<=1e-7: A('0')
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    47
            else:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    48
                l = sa<=1 and 6 or min(max(0,(6-int(_log_10(sa)))),6)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    49
                n = _fp_fmts[l]%i
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    50
                if l:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    51
                    j = len(n)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    52
                    while j:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    53
                        j -= 1
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    54
                        if n[j]!='0':
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    55
                            if n[j]!='.': j += 1
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    56
                            break
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    57
                    n = n[:j]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    58
                A((n[0]!='0' or len(n)==1) and n or n[1:])
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    59
        return ' '.join(s)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    60
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    61
    #hack test for comma users
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    62
    if ',' in fp_str(0.25):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    63
        _FP_STR = _fp_str
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    64
        def _fp_str(*a):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    65
            return _FP_STR(*a).replace(',','.')
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    66
    _py_funcs['fp_str'] = fp_str
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    67
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    68
if 'unicode2T1' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    69
    def unicode2T1(utext,fonts):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    70
        '''return a list of (font,string) pairs representing the unicode text'''
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    71
        R = []
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    72
        font, fonts = fonts[0], fonts[1:]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    73
        enc = font.encName
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    74
        if 'UCS-2' in enc:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    75
            enc = 'UTF16'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    76
        while utext:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    77
            try:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    78
                if isUnicode(utext):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    79
                    s = utext.encode(enc)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    80
                else:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    81
                    s = utext
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    82
                R.append((font,s))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    83
                break
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    84
            except UnicodeEncodeError as e:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    85
                i0, il = e.args[2:4]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    86
                if i0:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    87
                    R.append((font,utext[:i0].encode(enc)))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    88
                if fonts:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    89
                    R.extend(unicode2T1(utext[i0:il],fonts))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    90
                else:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    91
                    R.append((font._notdefFont,font._notdefChar*(il-i0)))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    92
                utext = utext[il:]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    93
        return R
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    94
    _py_funcs['unicode2T1'] = unicode2T1
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    95
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
    96
if 'instanceStringWidthT1' in _py_funcs:
3862
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
    97
    if isPy3:
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
    98
        def instanceStringWidthT1(self, text, size, encoding='utf8'):
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
    99
            """This is the "purist" approach to width"""
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   100
            if not isUnicode(text): text = text.decode(encoding)
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   101
            return sum([sum(map(f.widths.__getitem__,t)) for f, t in unicode2T1(text,[self]+self.substitutionFonts)])*0.001*size
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   102
    else:
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   103
        def instanceStringWidthT1(self, text, size, encoding='utf8'):
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   104
            """This is the "purist" approach to width"""
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   105
            if not isUnicode(text): text = text.decode(encoding)
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   106
            return sum([sum(map(f.widths.__getitem__,list(map(ord,t)))) for f, t in unicode2T1(text,[self]+self.substitutionFonts)])*0.001*size
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   107
    _py_funcs['instanceStringWidthT1'] = instanceStringWidthT1
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   108
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   109
if 'instanceStringWidthTTF' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   110
    def instanceStringWidthTTF(self, text, size, encoding='utf-8'):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   111
        "Calculate text width"
3873
73d4262b7edd rl_accel.py: fix python version of instanceStringWidthTTF
robin
parents: 3872
diff changeset
   112
        if not isUnicode(text):
73d4262b7edd rl_accel.py: fix python version of instanceStringWidthTTF
robin
parents: 3872
diff changeset
   113
            text = text.decode(encoding or 'utf-8')
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   114
        g = self.face.charWidths.get
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   115
        dw = self.face.defaultWidth
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   116
        return 0.001*size*sum([g(ord(u),dw) for u in text])
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   117
    _py_funcs['instanceStringWidthTTF'] = instanceStringWidthTTF
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   118
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   119
if 'hex32' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   120
    def hex32(i):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   121
        return '0X%8.8X' % (int(i)&0xFFFFFFFF)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   122
    _py_funcs['hex32'] = hex32
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   123
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   124
if 'add32' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   125
    def add32(x, y):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   126
        "Calculate (x + y) modulo 2**32"
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   127
        return (x+y) & 0xFFFFFFFF
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   128
    _py_funcs['add32'] = add32
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   129
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   130
if 'calcChecksum' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   131
    def calcChecksum(data):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   132
        """Calculates TTF-style checksums"""
3862
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   133
        data = rawBytes(data)
4026
6c8ac18e0c9c rl_accel.py: fix global setting & calcChecksum
robin
parents: 4002
diff changeset
   134
        if len(data)&3: data = data + (4-(len(data)&3))*b"\0"
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   135
        return sum(unpack(">%dl" % (len(data)>>2), data)) & 0xFFFFFFFF
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   136
    _py_funcs['calcChecksum'] = calcChecksum
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   137
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   138
if 'escapePDF' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   139
    _ESCAPEDICT={}
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   140
    for c in range(256):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   141
        if c<32 or c>=127:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   142
            _ESCAPEDICT[c]= '\\%03o' % c
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   143
        elif c in (ord('\\'),ord('('),ord(')')):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   144
            _ESCAPEDICT[c] = '\\'+chr(c)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   145
        else:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   146
            _ESCAPEDICT[c] = chr(c)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   147
    del c
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   148
    #Michael Hudson donated this
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   149
    def escapePDF(s):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   150
        r = []
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   151
        for c in s:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   152
            if not type(c) is int:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   153
                c = ord(c)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   154
            r.append(_ESCAPEDICT[c])
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   155
        return ''.join(r)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   156
    _py_funcs['escapePDF'] = escapePDF
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   157
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   158
if 'asciiBase85Encode' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   159
    def asciiBase85Encode(input):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   160
        """Encodes input using ASCII-Base85 coding.
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   161
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   162
        This is a compact encoding used for binary data within
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   163
        a PDF file.  Four bytes of binary data become five bytes of
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   164
        ASCII.  This is the default method used for encoding images."""
3879
991bbd763dbc rl_accel.py: improve test (suggested by) Lele Gaifax
robin
parents: 3873
diff changeset
   165
        doOrd =  not isPy3 or isUnicode(input)
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   166
        # special rules apply if not a multiple of four bytes.
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   167
        whole_word_count, remainder_size = divmod(len(input), 4)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   168
        cut = 4 * whole_word_count
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   169
        body, lastbit = input[0:cut], input[cut:]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   170
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   171
        out = [].append
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   172
        for i in range(whole_word_count):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   173
            offset = i*4
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   174
            b1 = body[offset]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   175
            b2 = body[offset+1]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   176
            b3 = body[offset+2]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   177
            b4 = body[offset+3]
3835
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   178
            if doOrd:
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   179
                b1 = ord(b1)
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   180
                b2 = ord(b2)
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   181
                b3 = ord(b3)
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   182
                b4 = ord(b4)
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   183
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   184
            if b1<128:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   185
                num = (((((b1<<8)|b2)<<8)|b3)<<8)|b4
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   186
            else:
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   187
                num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   188
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   189
            if num == 0:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   190
                #special case
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   191
                out('z')
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   192
            else:
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   193
                #solve for five base-85 numbers
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   194
                temp, c5 = divmod(num, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   195
                temp, c4 = divmod(temp, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   196
                temp, c3 = divmod(temp, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   197
                c1, c2 = divmod(temp, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   198
                assert ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5 == num, 'dodgy code!'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   199
                out(chr(c1+33))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   200
                out(chr(c2+33))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   201
                out(chr(c3+33))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   202
                out(chr(c4+33))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   203
                out(chr(c5+33))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   204
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   205
        # now we do the final bit at the end.  I repeated this separately as
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   206
        # the loop above is the time-critical part of a script, whereas this
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   207
        # happens only once at the end.
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   208
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   209
        #encode however many bytes we have as usual
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   210
        if remainder_size > 0:
3862
272e650eecf5 rl_accel.py: improvements and more tests working
robin
parents: 3835
diff changeset
   211
            lastbit += (4-len(lastbit))*('\0' if doOrd else b'\000')
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   212
            b1 = lastbit[0]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   213
            b2 = lastbit[1]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   214
            b3 = lastbit[2]
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   215
            b4 = lastbit[3]
3835
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   216
            if doOrd:
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   217
                b1 = ord(b1)
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   218
                b2 = ord(b2)
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   219
                b3 = ord(b3)
8e35c471e0ee various: attempt to be more consistent about isStr
robin
parents: 3779
diff changeset
   220
                b4 = ord(b4)
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   221
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   222
            num = 16777216 * b1 + 65536 * b2 + 256 * b3 + b4
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   223
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   224
            #solve for c1..c5
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   225
            temp, c5 = divmod(num, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   226
            temp, c4 = divmod(temp, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   227
            temp, c3 = divmod(temp, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   228
            c1, c2 = divmod(temp, 85)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   229
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   230
            #print 'encoding: %d %d %d %d -> %d -> %d %d %d %d %d' % (
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   231
            #    b1,b2,b3,b4,num,c1,c2,c3,c4,c5)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   232
            lastword = chr(c1+33) + chr(c2+33) + chr(c3+33) + chr(c4+33) + chr(c5+33)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   233
            #write out most of the bytes.
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   234
            out(lastword[0:remainder_size + 1])
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   235
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   236
        #terminator code for ascii 85
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   237
        out('~>')
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   238
        return ''.join(out.__self__)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   239
    _py_funcs['asciiBase85Encode'] = asciiBase85Encode
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   240
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   241
if 'asciiBase85Decode' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   242
    def asciiBase85Decode(input):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   243
        """Decodes input using ASCII-Base85 coding.
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   244
4002
4bf4b598196e _rl_accel.c & rl_accel.py: fix aascii85 encode decode to do the 'right' thing
robin
parents: 3879
diff changeset
   245
        This is not normally used - Acrobat Reader decodes for you
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   246
        - but a round trip is essential for testing."""
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   247
        #strip all whitespace
4002
4bf4b598196e _rl_accel.c & rl_accel.py: fix aascii85 encode decode to do the 'right' thing
robin
parents: 3879
diff changeset
   248
        stripped = ''.join(asNative(input).split())
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   249
        #check end
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   250
        assert stripped[-2:] == '~>', 'Invalid terminator for Ascii Base 85 Stream'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   251
        stripped = stripped[:-2]  #chop off terminator
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   252
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   253
        #may have 'z' in it which complicates matters - expand them
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   254
        stripped = stripped.replace('z','!!!!!')
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   255
        # special rules apply if not a multiple of five bytes.
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   256
        whole_word_count, remainder_size = divmod(len(stripped), 5)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   257
        #print '%d words, %d leftover' % (whole_word_count, remainder_size)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   258
        #assert remainder_size != 1, 'invalid Ascii 85 stream!'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   259
        cut = 5 * whole_word_count
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   260
        body, lastbit = stripped[0:cut], stripped[cut:]
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   261
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   262
        out = [].append
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   263
        for i in range(whole_word_count):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   264
            offset = i*5
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   265
            c1 = ord(body[offset]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   266
            c2 = ord(body[offset+1]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   267
            c3 = ord(body[offset+2]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   268
            c4 = ord(body[offset+3]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   269
            c5 = ord(body[offset+4]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   270
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   271
            num = ((85**4) * c1) + ((85**3) * c2) + ((85**2) * c3) + (85*c4) + c5
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   272
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   273
            temp, b4 = divmod(num,256)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   274
            temp, b3 = divmod(temp,256)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   275
            b1, b2 = divmod(temp, 256)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   276
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   277
            assert  num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   278
            out(chr(b1))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   279
            out(chr(b2))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   280
            out(chr(b3))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   281
            out(chr(b4))
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   282
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   283
        #decode however many bytes we have as usual
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   284
        if remainder_size > 0:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   285
            while len(lastbit) < 5:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   286
                lastbit = lastbit + '!'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   287
            c1 = ord(lastbit[0]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   288
            c2 = ord(lastbit[1]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   289
            c3 = ord(lastbit[2]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   290
            c4 = ord(lastbit[3]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   291
            c5 = ord(lastbit[4]) - 33
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   292
            num = (((85*c1+c2)*85+c3)*85+c4)*85 + (c5
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   293
                     +(0,0,0xFFFFFF,0xFFFF,0xFF)[remainder_size])
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   294
            temp, b4 = divmod(num,256)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   295
            temp, b3 = divmod(temp,256)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   296
            b1, b2 = divmod(temp, 256)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   297
            assert  num == 16777216 * b1 + 65536 * b2 + 256 * b3 + b4, 'dodgy code!'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   298
            #print 'decoding: %d %d %d %d %d -> %d -> %d %d %d %d' % (
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   299
            #    c1,c2,c3,c4,c5,num,b1,b2,b3,b4)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   300
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   301
            #the last character needs 1 adding; the encoding loses
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   302
            #data by rounding the number to x bytes, and when
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   303
            #divided repeatedly we get one less
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   304
            if remainder_size == 2:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   305
                lastword = chr(b1)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   306
            elif remainder_size == 3:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   307
                lastword = chr(b1) + chr(b2)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   308
            elif remainder_size == 4:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   309
                lastword = chr(b1) + chr(b2) + chr(b3)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   310
            else:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   311
                lastword = ''
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   312
            out(lastword)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   313
4002
4bf4b598196e _rl_accel.c & rl_accel.py: fix aascii85 encode decode to do the 'right' thing
robin
parents: 3879
diff changeset
   314
        r = ''.join(out.__self__)
4bf4b598196e _rl_accel.c & rl_accel.py: fix aascii85 encode decode to do the 'right' thing
robin
parents: 3879
diff changeset
   315
        return r.encode('latin1') if isUnicode(input) else r
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   316
    _py_funcs['asciiBase85Decode'] = asciiBase85Decode
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   317
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   318
if 'sameFrag' in _py_funcs:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   319
    def sameFrag(f,g):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   320
        'returns 1 if two ParaFrags map out the same'
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   321
        if (hasattr(f,'cbDefn') or hasattr(g,'cbDefn')
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   322
                or hasattr(f,'lineBreak') or hasattr(g,'lineBreak')): return 0
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   323
        for a in ('fontName', 'fontSize', 'textColor', 'rise', 'underline', 'strike', 'link', "backColor"):
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   324
            if getattr(f,a,None)!=getattr(g,a,None): return 0
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   325
        return 1
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   326
    _py_funcs['sameFrag'] = sameFrag
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   327
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   328
G=globals()
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   329
for fn in __all__:
4026
6c8ac18e0c9c rl_accel.py: fix global setting & calcChecksum
robin
parents: 4002
diff changeset
   330
    f = _c_funcs[fn] if fn in _c_funcs else _py_funcs[fn]
3779
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   331
    if not f:
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   332
        raise RuntimeError('function %s is not properly defined' % fn)
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   333
    G[fn] = f
47bb4a41365d all _rl_accel exports come through rl_accel.py
robin
parents: 3731
diff changeset
   334
del fn, f, G
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   335
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   336
if __name__=='__main__':
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   337
    import sys, os
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   338
    for modname in 'reportlab.lib.rl_accel','reportlab.lib._rl_accel':
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   339
        for cmd  in (
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   340
            #"unicode2T1('abcde fghi . jkl ; mno',fonts)",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   341
            #"unicode2T1(u'abcde fghi . jkl ; mno',fonts)",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   342
            "_instanceStringWidthU(font,'abcde fghi . jkl ; mno',10)",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   343
            "_instanceStringWidthU(font,u'abcde fghi . jkl ; mno',10)",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   344
            ):
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3727
diff changeset
   345
            print('%s %s' % (modname,cmd))
3725
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   346
            s=';'.join((
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   347
                "from reportlab.pdfbase.pdfmetrics import getFont",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   348
                "from %s import unicode2T1,_instanceStringWidthU" % modname,
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   349
                "fonts=[getFont('Helvetica')]+getFont('Helvetica').substitutionFonts""",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   350
                "font=fonts[0]",
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   351
                ))
ca840494f9dd added rl_accel
robin
parents:
diff changeset
   352
            os.system('%s -m timeit -s"%s" "%s"' % (sys.executable,s,cmd))