At reportlab, allow a custom font mapping table, system-wide..
[odoo/odoo.git] / bin / report / render / rml2pdf / trml2pdf.py
index 92aba87..aea46ea 100644 (file)
@@ -40,7 +40,7 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import sys
-import StringIO
+from StringIO import StringIO
 import xml.dom.minidom
 import copy
 
@@ -48,7 +48,7 @@ import reportlab
 import re
 from reportlab.pdfgen import canvas
 from reportlab import platypus
-
+import cStringIO
 import utils
 import color
 import os
@@ -194,6 +194,26 @@ class _rml_doc(object):
                 addMapping(name, 1, 0, name)    #bold
                 addMapping(name, 1, 1, name)    #italic and bold
 
+    def setTTFontMapping(self,face, fontname,filename, mode='all'):
+        from reportlab.lib.fonts import addMapping
+        from reportlab.pdfbase import pdfmetrics
+        from reportlab.pdfbase.ttfonts import TTFont
+       
+       pdfmetrics.registerFont(TTFont(fontname, filename ))
+       if (mode == 'all'):
+               addMapping(face, 0, 0, fontname)    #normal
+               addMapping(face, 0, 1, fontname)    #italic
+               addMapping(face, 1, 0, fontname)    #bold
+               addMapping(face, 1, 1, fontname)    #italic and bold
+       elif (mode== 'normal') or (mode == 'regular'):
+               addMapping(face, 0, 0, fontname)    #normal
+       elif (mode == 'italic'):
+               addMapping(face, 0, 1, fontname)    #italic
+       elif (mode == 'bold'):
+               addMapping(face, 1, 0, fontname)    #bold
+       elif (mode == 'bolditalic'):
+               addMapping(face, 1, 1, fontname)    #italic and bold
+
     def _textual_image(self, node):
         import base64
         rc = ''
@@ -351,26 +371,28 @@ class _rml_canvas(object):
         import urllib
         from reportlab.lib.utils import ImageReader
 
+#        s = StringIO()
         if not node.hasAttribute('file'):
 
             if node.hasAttribute('name'):
                 image_data = self.images[node.getAttribute('name')]
-                s = StringIO.StringIO(image_data)
+                s = cStringIO.StringIO(image_data)
             else:
                 import base64
                 image_data = base64.decodestring(node.firstChild.nodeValue)
                 if not image_data: return False
-                s = StringIO.StringIO(image_data)
+                s = cStringIO.StringIO(image_data)                
+#                s.write(image_data)
         else:
             if node.getAttribute('file') in self.images:
-                s = StringIO.StringIO(self.images[node.getAttribute('file')])
+                s = cStringIO.StringIO(self.images[node.getAttribute('file')])                
+#                s.write(self.images[node.getAttribute('file')])
             else:
                 try:
                     u = urllib.urlopen(str(node.getAttribute('file')))
-                    s = StringIO.StringIO(u.read())
                 except:
                     u = file(os.path.join(self.path,str(node.getAttribute('file'))), 'rb')
-                    s = StringIO.StringIO(u.read())
+                s = cStringIO.StringIO(u.read())
         img = ImageReader(s)
         (sx,sy) = img.getSize()
 
@@ -639,7 +661,10 @@ class _rml_flowable(object):
                 else:
                     import base64
                     image_data = base64.decodestring(node.firstChild.nodeValue)
-                image = StringIO.StringIO(image_data)
+
+                image = StringIO()
+                image.write(image_data)
+                image.seek(0)
                 return platypus.Image(image, mask=(250,255,250,255,250,255), **(utils.attr_get(node, ['width','height'])))
             else:
                 return platypus.Image(node.getAttribute('file'), mask=(250,255,250,255,250,255), **(utils.attr_get(node, ['width','height'])))
@@ -788,13 +813,21 @@ class _rml_template(object):
 
 def parseString(data, fout=None, images={}, path='.',title=None):
     r = _rml_doc(data, images, path, title=title)
+    
+    #try to override some font mappings
+    try:
+       from customfonts import SetCustomFonts
+       SetCustomFonts(r)
+    except:
+       pass
+    
     if fout:
         fp = file(fout,'wb')
         r.render(fp)
         fp.close()
         return fout
     else:
-        fp = StringIO.StringIO()
+        fp = StringIO()
         r.render(fp)
         return fp.getvalue()