def __init__(self,w,h,dpi=72,bg=0xffffff,configPIL=None): '''configPIL dict is passed to image save method''' scale = dpi/72.0 w = int(w*scale+0.5) h = int(h*scale+0.5) self.__dict__['_gs'] = _renderPM.gstate(w,h,bg=bg) self.__dict__['_bg'] = bg self.__dict__['_baseCTM'] = (scale,0,0,scale,0,0) self.__dict__['_clipPaths'] = [] self.__dict__['configPIL'] = configPIL self.__dict__['_dpi'] = dpi self.ctm = self._baseCTM
def __init__(self, w, h, dpi=72, bg=0xFFFFFF, configPIL=None): """configPIL dict is passed to image save method""" scale = dpi / 72.0 w = int(w * scale + 0.5) h = int(h * scale + 0.5) self.__dict__["_gs"] = _renderPM.gstate(w, h, bg=bg) self.__dict__["_bg"] = bg self.__dict__["_baseCTM"] = (scale, 0, 0, scale, 0, 0) self.__dict__["_clipPaths"] = [] self.__dict__["configPIL"] = configPIL self.__dict__["_dpi"] = dpi self.ctm = self._baseCTM
def _drawTimeResize(self,w,h,bg=None): if bg is None: bg = self._bg self._drawing.width, self._drawing.height = w, h A = {'ctm':None, 'strokeWidth':None, 'strokeColor':None, 'lineCap':None, 'lineJoin':None, 'dashArray':None, 'fillColor':None} gs = self._gs fN,fS = gs.fontName, gs.fontSize for k in A.keys(): A[k] = getattr(gs,k) del gs, self._gs gs = self.__dict__['_gs'] = _renderPM.gstate(w,h,bg=bg) for k in A.keys(): setattr(self,k,A[k]) gs.setFont(fN,fS)
def _text2PathDescription(text, x=0, y=0, fontName=_baseGFontName, fontSize=1000, anchor='start', truncate=1, pathReverse=0): global _gs if not _gs: import _renderPM _gs = _renderPM.gstate(1,1) from reportlab.graphics import renderPM renderPM._setFont(_gs,fontName,fontSize) P = [] if not anchor=='start': textLen = stringWidth(text, fontName,fontSize) if anchor=='end': x = x-textLen elif anchor=='middle': x = x - textLen/2. for g in _gs._stringPath(text,x,y): P.extend(_processGlyph(g,truncate=truncate,pathReverse=pathReverse)) return P
def _drawTimeResize(self, w, h, bg=None): if bg is None: bg = self._bg self._drawing.width, self._drawing.height = w, h A = { "ctm": None, "strokeWidth": None, "strokeColor": None, "lineCap": None, "lineJoin": None, "dashArray": None, "fillColor": None, } gs = self._gs fN, fS = gs.fontName, gs.fontSize for k in A.keys(): A[k] = getattr(gs, k) del gs, self._gs gs = self.__dict__["_gs"] = _renderPM.gstate(w, h, bg=bg) for k in A.keys(): setattr(self, k, A[k]) gs.setFont(fN, fS)
from reportlab.rl_config import T1SearchPath T1SearchPath.insert(0,'C:\\Python\\PyArt\\pdffonts') import _renderPM from time import time from reportlab.pdfbase.pdfmetrics import getFont from reportlab.pdfbase._fontdata import standardFonts g = _renderPM.gstate(200,200) g.ctm = (1,0,0,1,0,0) N=20 t0 = time() for i in xrange(N): for k in standardFonts: f = getFont(k) print k, f.face.findT1File() _renderPM.makeT1Font(k,f.face.findT1File(),f.encoding.vector) _renderPM.delCache() print 'Reading %d standard fonts %d times took %.2f seconds' % (len(standardFonts),N,time()-t0)
from reportlab.rl_config import T1SearchPath T1SearchPath.insert(0, 'C:\\Python\\PyArt\\pdffonts') import _renderPM from time import time from reportlab.pdfbase.pdfmetrics import getFont from reportlab.pdfbase._fontdata import standardFonts g = _renderPM.gstate(200, 200) g.ctm = (1, 0, 0, 1, 0, 0) N = 20 t0 = time() for i in range(N): for k in standardFonts: f = getFont(k) print k, f.face.findT1File() _renderPM.makeT1Font(k, f.face.findT1File(), f.encoding.vector) _renderPM.delCache() print 'Reading %d standard fonts %d times took %.2f seconds' % ( len(standardFonts), N, time() - t0)
def test_base(): class dummy: pass g = _renderPM.gstate(1, 1) try: g.aaa = 3 print('Wrong handling of bad attribute') except AttributeError: if verbose: print('bad attribute handled ok') for fontName, fontSize in (('aaa', 10), ('Times-Roman', '10'), ('Times-Roman', -10), (1, 10)): try: g.setFont(fontName, fontSize) print('Wrong handling of setFont(%s,%s)' % (fontName, fontSize)) except _renderPM.Error: if verbose: print('_renderPM.error detected OK') except TypeError: if verbose: print('Type detected OK') for a in ('strokeColor', 'fillColor'): try: setattr(g, a, (1, 2, 3)) print('Wrong handling of bad ' + a) except ValueError: if verbose: print('wrong handling of bad %s detected OK' % a) try: c = dummy() c.red = 0xff / 255. c.green = 0xaf / 255. c.blue = 0xbf / 255. for v, r in ((None, None), (0xfffafb, 0xfffafb), (c, 0xffafbf)): setattr(g, a, v) assert getattr(g, a) == r, "%s should be %s" % (a, hex(r)) if verbose: print('setattr(%s) OK' % a) except: print('wrong handling of good %s' % a) traceback.print_exc() print(hex(getattr(g, a))) for v in ('a', 1, (1, 'a'), ('a', 1), (1, ()), (1, ('a', 2))): try: g.dashArray = v print('Wrong handling of dashArray %s' % v) except ValueError: if verbose: print('Wrong handling of dashArray %s detected OK' % v) try: g.dashArray = 7, (1, 2, 3) assert g.dashArray == (7.0, (1.0, 2.0, 3.0)), "should be (7.0,(1.0,2.0,3.0))" if verbose: print('dashArray obtained OK') except: print('wrong handling of dashArray') traceback.print_exc() print(g.dashArray) try: g.pathBegin() g.moveTo(0, 0) g.lineTo(1, 0) g.lineTo(1, 1) g.lineTo(0, 1) g.pathClose() good = (('moveToClosed', 0.0, 0.0), ('lineTo', 1.0, 0.0), ('lineTo', 1.0, 1.0), ('lineTo', 0.0, 1.0), ('lineTo', 0.0, 0.0)) assert good == g.path, 'Wrong path should be %s' % str(good) if verbose: print('path attribute obtained OK') except: print('wrong handling of path') traceback.print_exc() print(g.path)
def test_base(): class dummy: pass g=_renderPM.gstate(1,1) try: g.aaa = 3 print 'Wrong handling of bad attribute' except AttributeError: if verbose: print 'bad attribute handled ok' for fontName, fontSize in (('aaa',10),('Times-Roman','10'),('Times-Roman',-10),(1,10)): try: g.setFont(fontName,fontSize) print 'Wrong handling of setFont(%s,%s)' % (fontName,fontSize) except _renderPM.Error: if verbose: print '_renderPM.error detected OK' except TypeError: if verbose: print 'Type detected OK' for a in ('strokeColor','fillColor'): try: setattr(g,a,(1,2,3)) print 'Wrong handling of bad '+a except ValueError: if verbose: print 'wrong handling of bad %s detected OK' % a try: c=dummy() c.red=0xff/255. c.green=0xaf/255. c.blue=0xbf/255. for v,r in ((None,None),(0xfffafb,0xfffafb),(c,0xffafbf)): setattr(g,a,v) assert getattr(g,a)==r, "%s should be %s" % (a,hex(r)) if verbose: print 'setattr(%s) OK' % a except: print 'wrong handling of good %s' % a traceback.print_exc() print hex(getattr(g,a)) for v in ('a',1,(1,'a'),('a',1),(1,()),(1,('a',2))): try: g.dashArray=v print 'Wrong handling of dashArray %s' % v except ValueError: if verbose: print 'Wrong handling of dashArray %s detected OK' % v try: g.dashArray=7,(1,2,3) assert g.dashArray==(7.0,(1.0,2.0,3.0)), "should be (7.0,(1.0,2.0,3.0))" if verbose: print 'dashArray obtained OK' except: print 'wrong handling of dashArray' traceback.print_exc() print g.dashArray try: g.pathBegin() g.moveTo(0,0) g.lineTo(1,0) g.lineTo(1,1) g.lineTo(0,1) g.pathClose() good = (('moveToClosed', 0.0, 0.0), ('lineTo', 1.0, 0.0), ('lineTo', 1.0, 1.0), ('lineTo', 0.0, 1.0), ('lineTo', 0.0, 0.0)) assert good==g.path, 'Wrong path should be %s' % str(good) if verbose: print 'path attribute obtained OK' except: print 'wrong handling of path' traceback.print_exc() print g.path