[FIX] font: add columns for family, path and mode

bzr revid: mat@openerp.com-20131203151753-ycz8pyp1eg382qps
This commit is contained in:
Martin Trigaux 2013-12-03 16:17:53 +01:00
parent 3e94f7ab1b
commit eda37d12e6
4 changed files with 32 additions and 36 deletions

View File

@ -298,8 +298,9 @@ class res_company(osv.osv):
if res:
return res[0]
font_obj.init_no_scan(cr, uid)
return font_obj.search(cr, uid, [('name', '=', 'Helvetica')], limit=1)[0]
font_obj.init_scan(cr, uid)
res = font_obj.search(cr, uid, [('name', '=', 'Helvetica')], limit=1)
return res and res[0] or False
_header = """
<header>
@ -396,7 +397,7 @@ class res_company(osv.osv):
return {'value': {'rml_header': self._header_a4}}
def act_discover_fonts(self, cr, uid, ids, context=None):
return self.pool.get("res.font").discover_fonts(cr, uid, ids, context)
return self.pool.get("res.font").init_scan(cr, uid, context)
_defaults = {
'currency_id': _get_euro,

View File

@ -85,7 +85,7 @@
<label for="font" />
<div>
<div>
<field name="font" class="oe_inline" colspan="2" on_change="onchange_font_name(font, rml_header, rml_header2, rml_header3)"/>
<field name="font" class="oe_inline" colspan="2" on_change="onchange_font_name(font, rml_header, rml_header2, rml_header3)" domain="[('mode', 'in', ('normal', 'regular', 'all'))]" />
<button string="(reload fonts)" name="act_discover_fonts" type="object" class="oe_link" colspan="1"/>
</div>
</div>

View File

@ -40,20 +40,27 @@ _logger = logging.getLogger(__name__)
class res_font(osv.Model):
_name = "res.font"
_description = 'Fonts available'
_order = 'name'
_order = 'name,family,id'
_columns = {
'name': fields.char("Name", required=True),
'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),
}
_sql_constraints = [
('name_font_uniq', 'unique(name)', 'You can not register two fonts with the same name'),
('name_font_uniq', 'unique(family, name)', 'You can not register two fonts with the same name'),
]
def discover_fonts(self, cr, uid, ids, context=None):
def init_scan(self, cr, uid, context=None):
self.discover_fonts(cr, uid, context=context)
return self.register_fonts(cr, uid, context=context)
def discover_fonts(self, cr, uid, context=None):
"""Scan fonts on the file system, add them to the list of known fonts
and create font object for the new ones"""
customfonts.CustomTTFonts = customfonts.BaseCustomTTFonts
customfonts.CustomTTFonts = list(customfonts.BaseCustomTTFonts)
found_fonts = {}
for font_path in customfonts.list_all_sysfonts():
@ -69,28 +76,16 @@ class res_font(osv.Model):
except ttfonts.TTFError:
_logger.warning("Could not register Font %s", font_path)
# add default PDF fonts
for family in customfonts.BasePDFFonts:
if not found_fonts.get(family):
found_fonts[family] = {'name': family}
def register_fonts(self, cr, uid, context=None):
# add new custom fonts
for family, name, path, mode in customfonts.CustomTTFonts:
if not self.search(cr, uid, [('family', '=', family), ('name', '=', name)], context=context):
self.create(cr, uid, {
'family': family, 'name': name,
'path': path, 'mode': mode,
}, context=context)
# remove deleted fonts
existing_font_ids = self.search(cr, uid, [], context=context)
existing_font_names = []
for font in self.browse(cr, uid, existing_font_ids):
existing_font_names.append(font.name)
if font.name not in found_fonts.keys():
self.unlink(cr, uid, font.id, context=context)
# add unknown fonts
for family, vals in found_fonts.items():
if family not in existing_font_names:
self.create(cr, uid, vals, context=context)
return True
def init_no_scan(self, cr, uid, context=None):
"""Add demo data for PDF fonts without scan (faster for db creation)"""
for font in customfonts.BasePDFFonts:
if not self.search(cr, uid, [('name', '=', font)], context=context):
self.create(cr, uid, {'name':font}, context=context)
return True
# remove fonts not present on disk
existing_font_names = [name for (family, name, path, mode) in customfonts.CustomTTFonts]
inexistant_fonts = self.search(cr, uid, [('name', 'not in', existing_font_names)], context=context)
return self.unlink(cr, uid, inexistant_fonts, context=context)

View File

@ -47,7 +47,7 @@ BasePDFFonts = [
]
# List of fonts found on the disk
CustomTTFonts = BaseCustomTTFonts = [ ('Helvetica', "DejaVu Sans", "DejaVuSans.ttf", 'normal'),
BaseCustomTTFonts = [ ('Helvetica', "DejaVu Sans", "DejaVuSans.ttf", 'normal'),
('Helvetica', "DejaVu Sans Bold", "DejaVuSans-Bold.ttf", 'bold'),
('Helvetica', "DejaVu Sans Oblique", "DejaVuSans-Oblique.ttf", 'italic'),
('Helvetica', "DejaVu Sans BoldOblique", "DejaVuSans-BoldOblique.ttf", 'bolditalic'),
@ -60,14 +60,14 @@ CustomTTFonts = BaseCustomTTFonts = [ ('Helvetica', "DejaVu Sans", "DejaVuSans.t
('Courier', "FreeMono Oblique", "FreeMonoOblique.ttf", 'italic'),
('Courier', "FreeMono BoldOblique", "FreeMonoBoldOblique.ttf", 'bolditalic'),
]
CustomTTFonts = list(BaseCustomTTFonts)
# Search path for TTF files, in addition of rl_config.TTFSearchPath
TTFSearchPath = [
'/usr/share/fonts/truetype', # SuSE
'/usr/share/fonts/dejavu', '/usr/share/fonts/liberation', # Fedora, RHEL
'/usr/share/fonts/truetype/*','/usr/local/share/fonts' # Ubuntu,
'/usr/share/fonts/TTF/*', # at Mandriva/Mageia
'/usr/share/fonts/TTF/*', # Mandriva/Mageia
'/usr/share/fonts/TTF', # Arch Linux
'/usr/lib/openoffice/share/fonts/truetype/',
'~/.fonts',