ttfonts.py: allow for _rl_accel._instanceStringWidthTTF
authorrgbecker
Mon, 15 May 2006 16:37:44 +0000
changeset 2606 185b990e8e60
parent 2605 ad7b5468898f
child 2607 aca8a844b645
ttfonts.py: allow for _rl_accel._instanceStringWidthTTF
reportlab/pdfbase/ttfonts.py
--- a/reportlab/pdfbase/ttfonts.py	Mon May 15 16:35:13 2006 +0000
+++ b/reportlab/pdfbase/ttfonts.py	Mon May 15 16:37:44 2006 +0000
@@ -1003,12 +1003,14 @@
         self._dynamicFont = 1   # We want dynamic subsetting
         self.state = {}
 
-    def stringWidth(self, text, size, encoding='utf-8'):
+    def _py_stringWidth(self, text, size, encoding='utf-8'):
         "Calculate text width"
         if type(text) is not UnicodeType:
             text = unicode(text, encoding or 'utf-8')   # encoding defaults to utf-8
-        width = self.face.getCharWidth
-        return 0.001*size*sum([width(ord(u)) for u in text])
+        g = self.face.charWidths.get
+        dw = self.face.defaultWidth
+        return 0.001*size*sum([g(ord(u),dw) for u in text])
+    stringWidth = _py_stringWidth
 
     def splitString(self, text, doc, encoding='utf-8'):
         """Splits text into a number of chunks, each of which belongs to a
@@ -1103,3 +1105,9 @@
             fontDict = doc.idToObject['BasicFonts'].dict
             fontDict[internalName] = pdfFont
         del self.state[doc]
+try:
+    from _rl_accel import _instanceStringWidthTTF
+    import new
+    TTFont.stringWidth = new.instancemethod(_instanceStringWidthTTF,None,TTFont)
+except ImportError:
+    pass