~openerp-chinese-team/openerp-china/openerp-china

6 by jeff
pdf fonts replace, next step:_get_system_fonts
1
# -*- coding: utf-8 -*-
20 by JoshuaJan
replace fonts in company page header
2
# __author__ = jeff@openerp.cn,joshua@openerp.cn
6 by jeff
pdf fonts replace, next step:_get_system_fonts
3
##############################################################################
4
#
5
#    This program is free software: you can redistribute it and/or modify
6
#    it under the terms of the GNU Affero General Public License as
7
#    published by the Free Software Foundation, either version 3 of the
8
#    License, or (at your option) any later version.
9
#
10
#    This program is distributed in the hope that it will be useful,
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
#    GNU Affero General Public License for more details.
14
#
15
#    You should have received a copy of the GNU Affero General Public License
16
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
#
18
##############################################################################
19
20
from osv import fields, osv
10 by Jeff Wang
finished pdf font module
21
from reportlab.lib.fontfinder import FontFinder
22
from reportlab import rl_config
23
import os
20 by JoshuaJan
replace fonts in company page header
24
import re
25
26
RMLS = ['rml_header','rml_header2','rml_header3']
6 by jeff
pdf fonts replace, next step:_get_system_fonts
27
28
class oecn_base_fonts_map(osv.osv_memory):
29
    _name = 'oecn_base_fonts.map'
30 by Tony Gu
cache the font list in a class variable
30
31
    # try to get the font from the cache first (system_fonts)
32
    system_fonts = []
33
    def __system_fonts_get(self, cr, uid, context=None):
34
        if self.system_fonts:
35
            return self.system_fonts
36
        else:
37
            return self._system_fonts_get(cr, uid)
38
21 by Jeff Wang
remove ids parameter in selection functions
39
    def _system_fonts_get(self, cr, uid, context = None):
6 by jeff
pdf fonts replace, next step:_get_system_fonts
40
        ''' get fonts list on server '''
10 by Jeff Wang
finished pdf font module
41
        # consider both windows and unix like systems
42
        res = []
43
        env = {
44
            'windir': os.environ.get('windir', os.getcwd()),
45
            'home': os.environ.get('HOME', os.getcwd()),
46
            }
47
        FontFolders = [
48
            '%(windir)s/Fonts',
49
            '/usr/share/fonts',
50
            '%(home)s/tmp/corefonts',
51
            ]
52
        folders = [ s % env for s in FontFolders]
53
        # get all folders under fonts directory
54
        ff = FontFinder( useCache = False )
24 by Tony Gu
Loop on a list that is changed in the looping code block is a bad behavior
55
        fontdirs = folders[:]
10 by Jeff Wang
finished pdf font module
56
        for ele in folders:
57
            for root, dirs, files in os.walk(ele):
58
                for name in dirs:
24 by Tony Gu
Loop on a list that is changed in the looping code block is a bad behavior
59
                    fontdirs.append(os.path.join(root, name))
10 by Jeff Wang
finished pdf font module
60
        
24 by Tony Gu
Loop on a list that is changed in the looping code block is a bad behavior
61
        ff.addDirectories(set(fontdirs))
10 by Jeff Wang
finished pdf font module
62
63
        # get fonts name and file path
30 by Tony Gu
cache the font list in a class variable
64
23 by Tony Gu
Font with extention .ttc should also be supported
65
        # ff.search() may resulting error if any AFMFont resided in
66
        # the search path has defined width that is not an integer
67
        # a dirty workaround:
68
        # replace following code around #110 in reportlab.pdfbase.pdfmetrics
69
        # From:
70
        # width = string.atoi(r)
71
        # To:
72
        # width = int(float(r))
10 by Jeff Wang
finished pdf font module
73
        ff.search()
74
        for familyName in ff.getFamilyNames():
75
            for font in ff.getFontsInFamily(familyName):
23 by Tony Gu
Font with extention .ttc should also be supported
76
                if font.fileName[-4:].lower() in (".ttf", ".ttc"):
10 by Jeff Wang
finished pdf font module
77
                    res.append((font.fileName, font.name))
30 by Tony Gu
cache the font list in a class variable
78
79
        #cache the font list in class variable
80
        oecn_base_fonts_map.system_fonts = res
6 by jeff
pdf fonts replace, next step:_get_system_fonts
81
        return res
16 by Jeff Wang
Add dropdown list to pdf fornts field
82
21 by Jeff Wang
remove ids parameter in selection functions
83
    def _pdf_fonts_get(self, cr, uid, context = None):    
16 by Jeff Wang
Add dropdown list to pdf fornts field
84
        return [('Helvetica','Helvetica'),       \
19 by Jeff Wang
add a missed ,
85
                ('DejaVuSans','DejaVuSans'),     \
16 by Jeff Wang
Add dropdown list to pdf fornts field
86
                ('Times','Times'),               \
87
                ('Times-Roman','Times-Roman'),   \
88
                ('Courier', 'Courier')]
89
6 by jeff
pdf fonts replace, next step:_get_system_fonts
90
    _columns = {
91
        'map_id':fields.many2one('oecn_base_fonts.config','Font Map', required=True),
25 by Tony Gu
make CJK wrap a boolean
92
        'pdf_font':fields.selection(_pdf_fonts_get, 'Original Fonts', required=True),
31 by Tony Gu
get font alias from onchange event
93
        'name':fields.char('Font Alias', size=20, required=True, help='use this font alias \
94
                in custom rml report template'),
95
        'new_font':fields.selection(__system_fonts_get, 'Replaced With', required=True),
6 by jeff
pdf fonts replace, next step:_get_system_fonts
96
    }
31 by Tony Gu
get font alias from onchange event
97
98
    def onchange_new_font(self, cr, uid, ids, new_font):
99
        """get the default 'Font Alias'"""
100
101
        for font_path, font_name in self.system_fonts:
102
            if new_font == font_path:
103
                return {'value': {'name': font_name}}
6 by jeff
pdf fonts replace, next step:_get_system_fonts
104
    
105
oecn_base_fonts_map()
106
107
class oecn_base_fonts_config(osv.osv_memory):
108
    _name = 'oecn_base_fonts.config'
109
    _inherit = 'res.config'
110
111
    _columns = {
25 by Tony Gu
make CJK wrap a boolean
112
        'wrap':fields.boolean('CJK wrap', required=True, \
113
            help="If you are using CJK fonts, check this option will wrap your \
114
            words properly at the edge of the  pdf report"),
115
        'map_ids':fields.one2many('oecn_base_fonts.map','map_id','Replace Fonts'),
6 by jeff
pdf fonts replace, next step:_get_system_fonts
116
    }
117
    _defaults = {
25 by Tony Gu
make CJK wrap a boolean
118
        'wrap': True
6 by jeff
pdf fonts replace, next step:_get_system_fonts
119
    }
120
    def execute(self, cr, uid, ids, context=None):
20 by JoshuaJan
replace fonts in company page header
121
        company_obj = self.pool.get('res.company')
122
        company_ids = company_obj.search(cr, uid, [])
123
        p = re.compile('<setFont.*(?=size.*)')
124
        #Replace the font in company rml headrer and footer
6 by jeff
pdf fonts replace, next step:_get_system_fonts
125
        for o in self.browse(cr, uid, ids, context=context):
126
            config_obj = self.pool.get('ir.config_parameter')
127
            maps = []
128
            for map in o.map_ids:
25 by Tony Gu
make CJK wrap a boolean
129
                maps = maps + [(map.pdf_font, map.name ,map.new_font,'all')]
6 by jeff
pdf fonts replace, next step:_get_system_fonts
130
            config_obj.set_param(cr, uid, 'fonts_map', {'wrap':o.wrap,'maps':maps})
20 by JoshuaJan
replace fonts in company page header
131
            for company in company_obj.read(cr, uid, company_ids, RMLS):
132
                value = {}
133
                for rml in RMLS:
134
                    new_font_rml  = '<setFont name="'+o.map_ids[0].name+'" '
135
                    value[rml] = p.sub(new_font_rml, company[rml])
136
                company_obj.write(cr, uid, company['id'], value)   
6 by jeff
pdf fonts replace, next step:_get_system_fonts
137
oecn_base_fonts_config()
138
139
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: