Fix rml2pdf custom ttf fonts.
[odoo/odoo.git] / bin / report / render / rml2pdf / customfonts.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 P. Christeas, Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 from reportlab import rl_config
23 import os
24
25 """This module allows the mapping of some system-available TTF fonts to
26 the reportlab engine.
27
28 This file could be customized per distro (although most Linux/Unix ones)
29 should have the same filenames, only need the code below).
30 """
31
32 CustomTTFonts = [ ('Helvetica',"DejaVu Sans", "DejaVuSans.ttf", 'normal'),
33         ('Helvetica',"DejaVu Sans Bold", "DejaVuSans-Bold.ttf", 'bold'),
34         ('Helvetica',"DejaVu Sans Oblique", "DejaVuSans-Oblique.ttf", 'italic'),
35         ('Helvetica',"DejaVu Sans BoldOblique", "DejaVuSans-BoldOblique.ttf", 'bolditalic'),
36         ('Times',"Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
37         ('Times',"Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
38         ('Times',"Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
39         ('Times',"Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
40         ('Times-Roman',"Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
41         ('Times-Roman',"Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
42         ('Times-Roman',"Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
43         ('Times-Roman',"Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
44         ('ZapfDingbats',"DejaVu Serif", "DejaVuSerif.ttf", 'normal'),
45         ('ZapfDingbats',"DejaVu Serif Bold", "DejaVuSerif-Bold.ttf", 'bold'),
46         ('ZapfDingbats',"DejaVu Serif Italic", "DejaVuSerif-Italic.ttf", 'italic'),
47         ('ZapfDingbats',"DejaVu Serif BoldItalic", "DejaVuSerif-BoldItalic.ttf", 'bolditalic'),
48         ('Courier',"FreeMono", "FreeMono.ttf", 'normal'),
49         ('Courier',"FreeMono Bold", "FreeMonoBold.ttf", 'bold'),
50         ('Courier',"FreeMono Oblique", "FreeMonoOblique.ttf", 'italic'),
51         ('Courier',"FreeMono BoldOblique", "FreeMonoBoldOblique.ttf", 'bolditalic'),]
52
53 __foundFonts = []
54
55 def FindCustomFonts():
56     """Fill the __foundFonts list with those filenames, whose fonts
57        can be found in the reportlab ttf font path.
58        
59        This process needs only be done once per loading of this module,
60        it is cached. But, if the system admin adds some font in the
61        meanwhile, the server must be restarted eventually.
62     """
63     dirpath =  []
64     global __foundFonts
65     for dirname in rl_config.TTFSearchPath:
66         abp = os.path.abspath(dirname)
67         if os.path.isdir(abp):
68             dirpath.append(abp)
69         
70     for k, (name, font, fname, mode) in enumerate(CustomTTFonts):
71         if fname in __foundFonts:
72             continue
73         for d in dirpath:
74             if os.path.exists(os.path.join(d, fname)):
75                 print "found font %s in %s" % (fname, d)
76                 __foundFonts.append(fname)
77                 break
78                 
79     # print "Found fonts:", __foundFonts
80
81
82 def SetCustomFonts(rmldoc):
83     """ Map some font names to the corresponding TTF fonts
84     
85         The ttf font may not even have the same name, as in
86         Times -> Liberation Serif.
87         This function is called once per report, so it should
88         avoid system-wide processing (cache it, instead).
89     """
90     if not len(__foundFonts):
91         FindCustomFonts()
92     global __foundFonts
93     for name, font, fname, mode in CustomTTFonts:
94         if fname in __foundFonts:
95             rmldoc.setTTFontMapping(name, font, fname, mode)
96     return True
97
98 #eof