7041541cd55e473e8eda42e0530e2b0cd1c11dc2
[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 CustomTTFonts = [ ('Helvetica',"DejaVu Sans", "DejaVuSans.ttf", 'normal'),
26         ('Helvetica',"DejaVu Sans Bold", "DejaVuSans-Bold.ttf", 'bold'),
27         ('Helvetica',"DejaVu Sans Oblique", "DejaVuSans-Oblique.ttf", 'italic'),
28         ('Helvetica',"DejaVu Sans BoldOblique", "DejaVuSans-BoldOblique.ttf", 'bolditalic'),
29         ('Times',"Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
30         ('Times',"Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
31         ('Times',"Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
32         ('Times',"Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
33         ('Times-Roman',"Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
34         ('Times-Roman',"Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
35         ('Times-Roman',"Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
36         ('Times-Roman',"Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
37         ('ZapfDingbats',"DejaVu Serif", "DejaVuSerif.ttf", 'normal'),
38         ('ZapfDingbats',"DejaVu Serif Bold", "DejaVuSerif-Bold.ttf", 'bold'),
39         ('ZapfDingbats',"DejaVu Serif Italic", "DejaVuSerif-Italic.ttf", 'italic'),
40         ('ZapfDingbats',"DejaVu Serif BoldItalic", "DejaVuSerif-BoldItalic.ttf", 'bolditalic'),
41         ('Courier',"FreeMono", "FreeMono.ttf", 'normal'),
42         ('Courier',"FreeMono Bold", "FreeMonoBold.ttf", 'bold'),
43         ('Courier',"FreeMono Oblique", "FreeMonoOblique.ttf", 'italic'),
44         ('Courier',"FreeMono BoldOblique", "FreeMonoBoldOblique.ttf", 'bolditalic'),]
45
46 def SearchFontPath(font_file):    
47     for dirname in rl_config.TTFSearchPath:
48         for root, dirs, files in os.walk(os.path.abspath(dirname)):
49             for file_name in files:
50                 filename = os.path.join(root, file_name)
51                 extension = os.path.splitext(filename)[1]
52                 if extension.lower() in ['.ttf']:
53                       if file_name==font_file:
54                             return True
55     return False
56
57 def SetCustomFonts(rmldoc):
58     for name, font, fname, mode in CustomTTFonts:
59         if SearchFontPath(fname):
60             rmldoc.setTTFontMapping(name, font,filename, mode)
61     return True
62
63
64
65 #eof