[IMP] one global list, remove hard coded list of font

bzr revid: mat@openerp.com-20131002101956-y6jt4d13bbku12g3
This commit is contained in:
Martin Trigaux
2013-10-02 12:19:56 +02:00
parent ff4643678b
commit aa30437603
2 changed files with 20 additions and 66 deletions
+1 -1
View File
@@ -128,6 +128,7 @@ class res_company(osv.osv):
'rml_footer': fields.text('Report Footer', help="Footer text displayed at the bottom of all reports."),
'rml_footer_readonly': fields.related('rml_footer', type='text', string='Report Footer', readonly=True),
'custom_footer': fields.boolean('Custom Footer', help="Check this to define the report footer manually. Otherwise it will be filled in automatically."),
'font': fields.selection(_get_font, "Font",help="Set your favorite font into company header"),
'logo': fields.related('partner_id', 'image', string="Logo", type="binary"),
'logo_web': fields.function(_get_logo_web, string="Logo Web", type="binary", store={
'res.company': (lambda s, c, u, i, x: i, ['partner_id'], 10),
@@ -151,7 +152,6 @@ class res_company(osv.osv):
'vat': fields.related('partner_id', 'vat', string="Tax ID", type="char", size=32),
'company_registry': fields.char('Company Registry', size=64),
'paper_format': fields.selection([('a4', 'A4'), ('us_letter', 'US Letter')], "Paper Format", required=True),
'font': fields.selection(_get_font, "Font",help="Set your favorite font into company header"),
}
_sql_constraints = [
('name_uniq', 'unique (name)', 'The company name must be unique !')
+19 -65
View File
@@ -36,72 +36,32 @@ 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.
"""
_fonts_cache = {'registered_fonts':[], 'total_system_fonts': 0}
_fonts_cache = {'registered_fonts': [], 'total_system_fonts': 0}
_logger = logging.getLogger(__name__)
CustomTTFonts = [ ('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'),
('Times', "Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
('Times', "Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
('Times', "Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
('Times', "Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
('Times-Roman', "Liberation Serif", "LiberationSerif-Regular.ttf", 'normal'),
('Times-Roman', "Liberation Serif Bold", "LiberationSerif-Bold.ttf", 'bold'),
('Times-Roman', "Liberation Serif Italic", "LiberationSerif-Italic.ttf", 'italic'),
('Times-Roman', "Liberation Serif BoldItalic", "LiberationSerif-BoldItalic.ttf", 'bolditalic'),
('Courier', "FreeMono", "FreeMono.ttf", 'normal'),
('Courier', "FreeMono Bold", "FreeMonoBold.ttf", 'bold'),
('Courier', "FreeMono Oblique", "FreeMonoOblique.ttf", 'italic'),
('Courier', "FreeMono BoldOblique", "FreeMonoBoldOblique.ttf", 'bolditalic'),
# Sun-ExtA can be downloaded from http://okuc.net/SunWb/
('Sun-ExtA', "Sun-ExtA", "Sun-ExtA.ttf", 'normal'),
]
TTFSearchPath_Linux = [
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', # Arch Linux
'/usr/lib/openoffice/share/fonts/truetype/',
]
'~/.fonts',
TTFSearchPath_Windows = [
'c:/winnt/fonts',
'c:/windows/fonts'
]
TTFSearchPath_Darwin = [
#mac os X - from
#http://developer.apple.com/technotes/tn/tn2024.html
# mac os X - from
# http://developer.apple.com/technotes/tn/tn2024.html
'~/Library/Fonts',
'/Library/Fonts',
'/Network/Library/Fonts',
'/System/Library/Fonts',
]
TTFSearchPathMap = {
'Darwin': TTFSearchPath_Darwin,
'Windows': TTFSearchPath_Windows,
'Linux': TTFSearchPath_Linux,
}
# windows
'c:/winnt/fonts',
'c:/windows/fonts'
]
__foundFonts = None
def linux_home_fonts():
"""
This function appends local font directory in TTFSearchPath_Linux.
"""
home = os.environ.get('HOME')
if home is not None:
# user fonts on OSX
path = os.path.join(home, 'Library', 'Fonts')
path = os.path.join(home, '.fonts')
TTFSearchPath_Linux.append(path)
def all_sysfonts_list():
"""
This function returns list of font directories of system.
@@ -110,20 +70,16 @@ def all_sysfonts_list():
filepath = []
global __foundFonts
__foundFonts = {}
local_platform = platform.system()
if local_platform in TTFSearchPathMap:
if local_platform == 'Linux':
linux_home_fonts()
searchpath += TTFSearchPathMap[local_platform]
# Append the original search path of reportlab (at the end)
searchpath += rl_config.TTFSearchPath
searchpath = list(set(searchpath))
# Append the original search path of reportlab
searchpath = list(set(TTFSearchPath + rl_config.TTFSearchPath))
for dirname in searchpath:
dirname = os.path.expanduser(dirname)
if os.path.exists(dirname):
for filename in [x for x in os.listdir(dirname) if x.lower().endswith('.ttf')]:
filepath.append(os.path.join(dirname, filename))
__foundFonts[filename]=os.path.join(dirname, filename)
return filepath
return sorted(filepath)
def init_new_font(familyName, name, font_dir):
return {
@@ -138,7 +94,7 @@ def RegisterCustomFonts():
This function prepares a list for all system fonts to be registered
in reportlab and returns the updated list with new fonts.
"""
all_system_fonts = sorted(all_sysfonts_list())
all_system_fonts = all_sysfonts_list()
if len(all_system_fonts) > _fonts_cache['total_system_fonts']:
font_modes, last_family, registered_font_list, _fonts_cache['registered_fonts'] = {}, "", [], []
#Prepares a list of registered fonts. Remove such fonts those don't have cmap for Unicode.
@@ -147,7 +103,7 @@ def RegisterCustomFonts():
font_info = ttfonts.TTFontFile(dirname)
if font_info.styleName in ('Regular','Normal','Book','Medium'):
_fonts_cache['registered_fonts'].append((font_info.name, font_info.name))
registered_font_list.append((font_info.familyName, font_info.name, dirname.split('/')[-1], font_info.styleName.lower().replace(" ", "")))
registered_font_list.append((font_info.familyName, font_info.name, os.path.basename(dirname), font_info.styleName.lower().replace(" ", "")))
_logger.debug("Found font %s at %s", font_info.name, dirname)
except:
_logger.warning("Could not register Font %s", dirname)
@@ -163,7 +119,6 @@ def RegisterCustomFonts():
font_modes['italic'] = font_modes['regular'][:3]+('italic',)
if not font_modes['bolditalic']:
font_modes['bolditalic'] = font_modes['bold'][:3]+('bolditalic',)
CustomTTFonts.extend(font_modes.values())
font_modes = init_new_font(familyName, name, font_dir)
if (mode== 'normal') or (mode == 'regular') or (mode == 'medium') or (mode == 'book'):
font_modes['regular'] = (familyName, name, font_dir, 'regular')
@@ -176,7 +131,7 @@ def RegisterCustomFonts():
last_family = familyName
_fonts_cache['total_system_fonts'] = len(all_system_fonts)
return _fonts_cache['registered_fonts']
def SetCustomFonts(rmldoc):
""" Map some font names to the corresponding TTF fonts
@@ -187,12 +142,11 @@ def SetCustomFonts(rmldoc):
"""
if not _fonts_cache['registered_fonts']:
RegisterCustomFonts()
for name, font, filename, mode in CustomTTFonts:
for name, font, filename, mode in _fonts_cache['registered_fonts']:
if os.path.isabs(filename) and os.path.exists(filename):
rmldoc.setTTFontMapping(name, font, filename, mode)
elif filename in __foundFonts:
rmldoc.setTTFontMapping(name, font, __foundFonts[filename], mode)
rmldoc.setTTFontMapping(name, font, __foundFonts[filename], mode)
return True
# eof
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: