def testStringWidth(self): from _rl_accel import stringWidthU from reportlab.pdfbase.pdfmetrics import _py_stringWidth, getFont, registerFont, _fonts from reportlab.pdfbase.ttfonts import TTFont ttfn = 'Vera' t1fn = 'Times-Roman' registerFont(TTFont(ttfn, "Vera.ttf")) ttf = getFont(ttfn) t1f = getFont(t1fn) testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9)) enc='cp1252' senc = 'utf8' intern(senc) ts = 'ABCDEF\xce\x91\xce\xb2G' utext = 'ABCDEF\xce\x91\xce\xb2G'.decode('utf8') fontSize = 12 stringWidthU(testCp1252,t1fn,fontSize,enc) #avoid obscure startup initialization problems defns="ttfn t1fn ttf t1f testCp1252 enc senc ts utext fontSize ttf.face ttf.face.charWidths ttf.face.defaultWidth t1f.widths t1f.encName t1f.substitutionFonts _fonts" rcv = getrc(defns) #first count def tfunc(ts,fn,fontSize,enc): w1 = stringWidthU(ts,fn,fontSize,enc) w2 = _py_stringWidth(ts,fn,fontSize,enc) assert abs(w1-w2)<1e-10,"stringWidthU(%r,%r,%s,%r)-->%r != _py_stringWidth(...)-->%r" % (ts,fn,fontSize,enc,w1,w2) tfunc(testCp1252,t1fn,fontSize,enc) tfunc(ts,t1fn,fontSize,senc) tfunc(utext,t1fn,fontSize,senc) tfunc(ts,ttfn,fontSize,senc) tfunc(testCp1252,ttfn,fontSize,enc) tfunc(utext,ttfn,fontSize,senc) rcc = checkrc(defns,rcv) #second count and compare assert not rcc, "rc diffs (%s)" % rcc
def tfunc(ts, fn, fontSize, enc): w1 = stringWidthU(ts, fn, fontSize, enc) w2 = _py_stringWidth(ts, fn, fontSize, enc) assert abs( w1 - w2 ) < 1e-10, "stringWidthU(%r,%r,%s,%r)-->%r != _py_stringWidth(...)-->%r" % ( ts, fn, fontSize, enc, w1, w2)
def testStringWidth(self): from _rl_accel import stringWidthU from reportlab.pdfbase.pdfmetrics import _py_stringWidth, getFont, registerFont, _fonts from reportlab.pdfbase.ttfonts import TTFont ttfn = 'Vera' t1fn = 'Times-Roman' registerFont(TTFont(ttfn, "Vera.ttf")) ttf = getFont(ttfn) t1f = getFont(t1fn) testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % ( chr(169), chr(153), chr(174), chr(0xe9)) enc = 'cp1252' senc = 'utf8' intern(senc) ts = 'ABCDEF\xce\x91\xce\xb2G' utext = 'ABCDEF\xce\x91\xce\xb2G'.decode('utf8') fontSize = 12 stringWidthU(testCp1252, t1fn, fontSize, enc) #avoid obscure startup initialization problems defns = "ttfn t1fn ttf t1f testCp1252 enc senc ts utext fontSize ttf.face ttf.face.charWidths ttf.face.defaultWidth t1f.widths t1f.encName t1f.substitutionFonts _fonts" rcv = getrc(defns) #first count def tfunc(ts, fn, fontSize, enc): w1 = stringWidthU(ts, fn, fontSize, enc) w2 = _py_stringWidth(ts, fn, fontSize, enc) assert abs( w1 - w2 ) < 1e-10, "stringWidthU(%r,%r,%s,%r)-->%r != _py_stringWidth(...)-->%r" % ( ts, fn, fontSize, enc, w1, w2) tfunc(testCp1252, t1fn, fontSize, enc) tfunc(ts, t1fn, fontSize, senc) tfunc(utext, t1fn, fontSize, senc) tfunc(ts, ttfn, fontSize, senc) tfunc(testCp1252, ttfn, fontSize, enc) tfunc(utext, ttfn, fontSize, senc) rcc = checkrc(defns, rcv) #second count and compare assert not rcc, "rc diffs (%s)" % rcc
def tfunc(ts,fn,fontSize,enc): w1 = stringWidthU(ts,fn,fontSize,enc) w2 = _py_stringWidth(ts,fn,fontSize,enc) assert abs(w1-w2)<1e-10,"stringWidthU(%r,%r,%s,%r)-->%r != _py_stringWidth(...)-->%r" % (ts,fn,fontSize,enc,w1,w2)
from reportlab.pdfbase.pdfmetrics import _py_stringWidth from _rl_accel import stringWidthU import time testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % (chr(169), chr(153),chr(174), chr(0xe9)) enc='cp1252' assert stringWidthU('ABCDEF','Times-Roman',12)==_py_stringWidth('ABCDEF','Times-Roman',12) assert stringWidthU(testCp1252,'Times-Roman',12,enc)==_py_stringWidth(testCp1252,'Times-Roman',12,enc) def tim(N,msg,func,*args): t0 = time.time() for i in range(N): x = func(*args) t1 = time.time() return "%s N=%d t=%.3f\n%r" % (msg,N,t1-t0,x) print(tim(1000000,'_py_stringWidth',_py_stringWidth,'ABCDEF','Times-Roman',12)) print(tim(1000000,'stringWidthU',stringWidthU,'ABCDEF','Times-Roman',12))
from reportlab.pdfbase.pdfmetrics import _py_stringWidth from _rl_accel import stringWidthU import time testCp1252 = 'copyright %s trademark %s registered %s ReportLab! Ol%s!' % ( chr(169), chr(153), chr(174), chr(0xe9)) enc = 'cp1252' assert stringWidthU('ABCDEF', 'Times-Roman', 12) == _py_stringWidth('ABCDEF', 'Times-Roman', 12) assert stringWidthU(testCp1252, 'Times-Roman', 12, enc) == _py_stringWidth(testCp1252, 'Times-Roman', 12, enc) def tim(N, msg, func, *args): t0 = time.time() for i in range(N): x = func(*args) t1 = time.time() return "%s N=%d t=%.3f\n%r" % (msg, N, t1 - t0, x) print( tim(1000000, '_py_stringWidth', _py_stringWidth, 'ABCDEF', 'Times-Roman', 12)) print(tim(1000000, 'stringWidthU', stringWidthU, 'ABCDEF', 'Times-Roman', 12))
from reportlab.pdfbase.pdfmetrics import _py_stringWidth from _rl_accel import stringWidthU import time testCp1252 = "copyright %s trademark %s registered %s ReportLab! Ol%s!" % (chr(169), chr(153), chr(174), chr(0xE9)) enc = "cp1252" assert stringWidthU("ABCDEF", "Times-Roman", 12) == _py_stringWidth("ABCDEF", "Times-Roman", 12) assert stringWidthU(testCp1252, "Times-Roman", 12, enc) == _py_stringWidth(testCp1252, "Times-Roman", 12, enc) def tim(N, msg, func, *args): t0 = time.time() for i in range(N): x = func(*args) t1 = time.time() return "%s N=%d t=%.3f\n%r" % (msg, N, t1 - t0, x) print tim(1000000, "_py_stringWidth", _py_stringWidth, "ABCDEF", "Times-Roman", 12) print tim(1000000, "stringWidthU", stringWidthU, "ABCDEF", "Times-Roman", 12)