def _py_stringWidth(self, text, size, encoding='utf8'): """This is the "purist" approach to width. The practical approach is to use the stringWidth function, which may be swapped in for one written in C.""" if not isinstance(text, unicode): text = text.decode(encoding) return sum([ sum(map(f.widths.__getitem__, map(ord, t))) for f, t in unicode2T1(text, [self] + self.substitutionFonts) ]) * 0.001 * size
def _py_stringWidth(self, text, size, encoding='utf8'): """This is the "purist" approach to width. The practical approach is to use the stringWidth function, which may be swapped in for one written in C.""" if not isUnicodeType(text): text = text.decode(encoding) w = 0 for f, t in unicode2T1(text, [self] + self.substitutionFonts): if sys.version_info[0] == 3: w += sum([f.widths[c] for c in t]) else: w += sum([f.widths[ord(c)] for c in t]) return w * 0.001 * size
def _py_stringWidth(self, text, size, encoding='utf8'): """This is the "purist" approach to width. The practical approach is to use the stringWidth function, which may be swapped in for one written in C.""" if not isUnicodeType(text): text = text.decode(encoding) w = 0 for f, t in unicode2T1(text,[self]+self.substitutionFonts): if sys.version_info[0] == 3: w += sum([f.widths[c] for c in t]) else: w += sum([f.widths[ord(c)] for c in t]) return w*0.001*size
utext = 'ABCDEF \xce\x91BCDEF\xce\x91'.decode('utf8') utext = 'This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86. This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86'.decode( 'utf8') fontName = 'Times-Roman' fontSize = 12 N = 30000 def tim(msg, func, *args): t0 = time.time() for i in xrange(N): x = func(*args) t1 = time.time() return "%s N=%d t=%.3f\n%r" % (msg, N, t1 - t0, x) #print tim('_py_stringWidth', _py_stringWidth, utext, fontName, fontSize) #print tim('stringWidth2', stringWidth2, utext, fontName, fontSize) font = _py_getFont(fontName) assert unicode2T1(utext, [font] + font.substitutionFonts) == _py_unicode2T1( utext, [font] + font.substitutionFonts) #print unicode2T1(u'ABCDEF',[font]+font.substitutionFonts) #print _py_unicode2T1(u'ABCDEF',[font]+font.substitutionFonts) defns = "font font.widths font.substitutionFonts font.encName fontName utext" rcv = getrc(defns) print tim('unicode2T1', unicode2T1, utext, [font] + font.substitutionFonts) print tim('_py_unicode2T1', _py_unicode2T1, utext, [font] + font.substitutionFonts) print "rc diffs=(%s)" % checkrc(defns, rcv)
def _py_stringWidth(self, text, size, encoding='utf8'): """This is the "purist" approach to width. The practical approach is to use the stringWidth function, which may be swapped in for one written in C.""" if not isinstance(text,unicode): text = text.decode(encoding) return sum([sum(map(f.widths.__getitem__,map(ord,t))) for f, t in unicode2T1(text,[self]+self.substitutionFonts)])*0.001*size
import os, time, sys from reportlab.pdfbase.pdfmetrics import _py_getFont, _py_unicode2T1 from _rl_accel import unicode2T1 from getrc import getrc, checkrc utext = 'ABCDEF \xce\x91BCDEF\xce\x91'.decode('utf8') utext = 'This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86. This is the end of the \xce\x91\xce\xb2 world. This is the end of the \xce\x91\xce\xb2 world jap=\xe3\x83\x9b\xe3\x83\x86'.decode('utf8') fontName = 'Times-Roman' fontSize=12 N = 30000 def tim(msg,func,*args): t0 = time.time() for i in xrange(N): x = func(*args) t1 = time.time() return "%s N=%d t=%.3f\n%r" % (msg,N,t1-t0,x) #print tim('_py_stringWidth', _py_stringWidth, utext, fontName, fontSize) #print tim('stringWidth2', stringWidth2, utext, fontName, fontSize) font = _py_getFont(fontName) assert unicode2T1(utext,[font]+font.substitutionFonts)==_py_unicode2T1(utext,[font]+font.substitutionFonts) #print unicode2T1(u'ABCDEF',[font]+font.substitutionFonts) #print _py_unicode2T1(u'ABCDEF',[font]+font.substitutionFonts) defns = "font font.widths font.substitutionFonts font.encName fontName utext" rcv = getrc(defns) print tim('unicode2T1',unicode2T1,utext,[font]+font.substitutionFonts) print tim('_py_unicode2T1',_py_unicode2T1,utext,[font]+font.substitutionFonts) print "rc diffs=(%s)" % checkrc(defns,rcv)