2013-10-14 09:41:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2013-10-14 12:38:15 +00:00
|
|
|
from reportlab.pdfbase import ttfonts
|
2013-10-14 09:41:22 +00:00
|
|
|
from openerp.osv import fields, osv
|
2013-10-14 12:38:15 +00:00
|
|
|
from openerp.report.render.rml2pdf import customfonts
|
2013-10-14 09:41:22 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
"""This module allows the mapping of some system-available TTF fonts to
|
|
|
|
the reportlab engine.
|
|
|
|
|
|
|
|
This file could be customized per distro (although most Linux/Unix ones)
|
|
|
|
should have the same filenames, only need the code below).
|
|
|
|
|
|
|
|
Due to an awful configuration that ships with reportlab at many Linux
|
|
|
|
and Ubuntu distros, we have to override the search path, too.
|
|
|
|
"""
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class res_font(osv.Model):
|
|
|
|
_name = "res.font"
|
|
|
|
_description = 'Fonts available'
|
2013-12-09 15:14:54 +00:00
|
|
|
_order = 'family,name,id'
|
2013-12-09 13:21:09 +00:00
|
|
|
_rec_name = 'family'
|
2013-10-14 09:41:22 +00:00
|
|
|
|
|
|
|
_columns = {
|
2013-12-03 15:17:53 +00:00
|
|
|
'family': fields.char("Font family", required=True),
|
|
|
|
'name': fields.char("Font Name", required=True),
|
|
|
|
'path': fields.char("Path", required=True),
|
|
|
|
'mode': fields.char("Mode", required=True),
|
2013-10-14 09:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_sql_constraints = [
|
2013-12-03 15:17:53 +00:00
|
|
|
('name_font_uniq', 'unique(family, name)', 'You can not register two fonts with the same name'),
|
2013-10-14 09:41:22 +00:00
|
|
|
]
|
|
|
|
|
2013-12-09 12:57:05 +00:00
|
|
|
def font_scan(self, cr, uid, lazy=False, context=None):
|
2013-12-09 15:14:54 +00:00
|
|
|
"""Action of loading fonts
|
|
|
|
In lazy mode will scan the filesystem only if there is no founts in the database and sync if no font in CustomTTFonts
|
|
|
|
In not lazy mode will force scan filesystem and sync
|
|
|
|
"""
|
2013-12-09 12:57:05 +00:00
|
|
|
if lazy:
|
|
|
|
# lazy loading, scan only if no fonts in db
|
|
|
|
found_fonts_ids = self.search(cr, uid, [('path', '!=', '/dev/null')], context=context)
|
|
|
|
if not found_fonts_ids:
|
|
|
|
# no scan yet or no font found on the system, scan the filesystem
|
2013-12-09 15:14:54 +00:00
|
|
|
self._scan_disk(cr, uid, context=context)
|
|
|
|
elif len(customfonts.CustomTTFonts) == 0:
|
|
|
|
# CustomTTFonts list is empty
|
|
|
|
self._sync(cr, uid, context=context)
|
2013-12-09 12:57:05 +00:00
|
|
|
else:
|
2013-12-09 15:14:54 +00:00
|
|
|
self._scan_disk(cr, uid, context=context)
|
2013-12-09 12:57:05 +00:00
|
|
|
return True
|
2013-12-03 15:17:53 +00:00
|
|
|
|
2013-12-09 15:14:54 +00:00
|
|
|
def _scan_disk(self, cr, uid, context=None):
|
|
|
|
"""Scan the file system and register the result in database"""
|
|
|
|
found_fonts = []
|
2013-10-14 12:38:15 +00:00
|
|
|
for font_path in customfonts.list_all_sysfonts():
|
2013-10-14 09:41:22 +00:00
|
|
|
try:
|
|
|
|
font = ttfonts.TTFontFile(font_path)
|
|
|
|
_logger.debug("Found font %s at %s", font.name, font_path)
|
2013-12-09 15:14:54 +00:00
|
|
|
found_fonts.append((font.familyName, font.name, font_path, font.styleName))
|
2013-10-14 09:41:22 +00:00
|
|
|
except ttfonts.TTFError:
|
|
|
|
_logger.warning("Could not register Font %s", font_path)
|
|
|
|
|
2013-12-09 15:14:54 +00:00
|
|
|
for family, name, path, mode in found_fonts:
|
2013-12-03 15:17:53 +00:00
|
|
|
if not self.search(cr, uid, [('family', '=', family), ('name', '=', name)], context=context):
|
|
|
|
self.create(cr, uid, {
|
2013-12-09 11:05:11 +00:00
|
|
|
'family': family, 'name': name,
|
|
|
|
'path': path, 'mode': mode,
|
|
|
|
}, context=context)
|
2013-12-03 15:17:53 +00:00
|
|
|
|
2013-12-09 15:14:54 +00:00
|
|
|
# remove fonts not present on the disk anymore
|
|
|
|
existing_font_names = [name for (family, name, path, mode) in found_fonts]
|
2013-12-09 13:08:44 +00:00
|
|
|
inexistant_fonts = self.search(cr, uid, [('name', 'not in', existing_font_names), ('path', '!=', '/dev/null')], context=context)
|
2013-12-09 11:05:11 +00:00
|
|
|
if inexistant_fonts:
|
2013-12-09 15:14:54 +00:00
|
|
|
self.unlink(cr, uid, inexistant_fonts, context=context)
|
|
|
|
|
|
|
|
self.clear_caches()
|
|
|
|
self._sync(cr, uid, context=context)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _sync(self, cr, uid, context=None):
|
|
|
|
"""Set the customfonts.CustomTTFonts list to the content of the database"""
|
|
|
|
customfonts.CustomTTFonts = []
|
|
|
|
found_fonts_ids = self.search(cr, uid, [('path', '!=', '/dev/null')], context=context)
|
|
|
|
for font in self.browse(cr, uid, found_fonts_ids, context=None):
|
|
|
|
customfonts.CustomTTFonts.append((font.family, font.name, font.path, font.mode))
|
2013-12-09 11:05:11 +00:00
|
|
|
return True
|
2013-12-09 15:14:54 +00:00
|
|
|
|
|
|
|
def clear_caches(self):
|
|
|
|
"""Force worker to resync at next report loading by setting an empty font list"""
|
|
|
|
customfonts.CustomTTFonts = []
|
|
|
|
return super(res_font, self).clear_caches()
|