author | robin |
Sat, 23 Feb 2013 19:12:41 +0000 | |
branch | py33 |
changeset 3727 | 67e69a84138b |
parent 3725 | ca840494f9dd |
child 3731 | b233dd0577ff |
permissions | -rw-r--r-- |
3725 | 1 |
from reportlab.lib.utils import isUnicodeType, isSeqType |
2 |
from math import log |
|
3 |
_log_10 = lambda x,log=log,_log_e_10=log(10.0): log(x)/_log_e_10 |
|
4 |
_fp_fmts = "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f" |
|
5 |
def fp_str(*a): |
|
6 |
'''convert separate arguments (or single sequence arg) into space separated numeric strings''' |
|
7 |
if len(a)==1 and isSeqType(a[0]): a = a[0] |
|
8 |
s = [] |
|
9 |
A = s.append |
|
10 |
for i in a: |
|
11 |
sa =abs(i) |
|
12 |
if sa<=1e-7: A('0') |
|
13 |
else: |
|
14 |
l = sa<=1 and 6 or min(max(0,(6-int(_log_10(sa)))),6) |
|
15 |
n = _fp_fmts[l]%i |
|
16 |
if l: |
|
17 |
j = len(n) |
|
18 |
while j: |
|
19 |
j -= 1 |
|
20 |
if n[j]!='0': |
|
21 |
if n[j]!='.': j += 1 |
|
22 |
break |
|
23 |
n = n[:j] |
|
24 |
A((n[0]!='0' or len(n)==1) and n or n[1:]) |
|
25 |
return ' '.join(s) |
|
26 |
||
27 |
#hack test for comma users |
|
28 |
if ',' in fp_str(0.25): |
|
29 |
_FP_STR = fp_str |
|
30 |
def fp_str(*a): |
|
31 |
return _FP_STR(*a).replace(',','.') |
|
32 |
||
3727
67e69a84138b
changes to bring into line with new Python 3.3.0 universe
robin
parents:
3725
diff
changeset
|
33 |
def unicode2T1(utext,fonts): |
3725 | 34 |
'''return a list of (font,string) pairs representing the unicode text''' |
35 |
R = [] |
|
36 |
font, fonts = fonts[0], fonts[1:] |
|
37 |
enc = font.encName |
|
38 |
if 'UCS-2' in enc: |
|
39 |
enc = 'UTF16' |
|
40 |
while utext: |
|
41 |
try: |
|
42 |
if isUnicodeType(utext): |
|
43 |
s = utext.encode(enc) |
|
44 |
else: |
|
45 |
s = utext |
|
46 |
R.append((font,s)) |
|
47 |
break |
|
48 |
except UnicodeEncodeError as e: |
|
49 |
i0, il = e.args[2:4] |
|
50 |
if i0: |
|
51 |
R.append((font,utext[:i0].encode(enc))) |
|
52 |
if fonts: |
|
3727
67e69a84138b
changes to bring into line with new Python 3.3.0 universe
robin
parents:
3725
diff
changeset
|
53 |
R.extend(unicode2T1(utext[i0:il],fonts)) |
3725 | 54 |
else: |
3727
67e69a84138b
changes to bring into line with new Python 3.3.0 universe
robin
parents:
3725
diff
changeset
|
55 |
R.append((font._notdefFont,font._notdefChar*(il-i0))) |
3725 | 56 |
utext = utext[il:] |
57 |
return R |
|
58 |
||
59 |
def _instanceStringWidthU(self, text, size, encoding='utf8'): |
|
60 |
"""This is the "purist" approach to width""" |
|
61 |
if not isUnicodeType(text): text = text.decode(encoding) |
|
62 |
return sum([sum(map(f.widths.__getitem__,list(map(ord,t)))) for f, t in unicode2T1(text,[self]+self.substitutionFonts)])*0.001*size |
|
63 |
||
64 |
if __name__=='__main__': |
|
65 |
import sys, os |
|
66 |
for modname in 'reportlab.lib.rl_accel','reportlab.lib._rl_accel': |
|
67 |
for cmd in ( |
|
68 |
#"unicode2T1('abcde fghi . jkl ; mno',fonts)", |
|
69 |
#"unicode2T1(u'abcde fghi . jkl ; mno',fonts)", |
|
70 |
"_instanceStringWidthU(font,'abcde fghi . jkl ; mno',10)", |
|
71 |
"_instanceStringWidthU(font,u'abcde fghi . jkl ; mno',10)", |
|
72 |
): |
|
73 |
print '%s %s' % (modname,cmd) |
|
74 |
s=';'.join(( |
|
75 |
"from reportlab.pdfbase.pdfmetrics import getFont", |
|
76 |
"from %s import unicode2T1,_instanceStringWidthU" % modname, |
|
77 |
"fonts=[getFont('Helvetica')]+getFont('Helvetica').substitutionFonts""", |
|
78 |
"font=fonts[0]", |
|
79 |
)) |
|
80 |
os.system('%s -m timeit -s"%s" "%s"' % (sys.executable,s,cmd)) |