2009-11-27 07:23:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-02-07 19:27:20 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
2009-11-25 13:41:16 +00:00
|
|
|
# Copyright (C) 2004-2009 P. Christeas, Tiny SPRL (<http://tiny.be>).
|
2010-11-26 10:34:59 +00:00
|
|
|
# Copyright (C) 2010 OpenERP SA. (http://www.openerp.com)
|
2009-02-07 19:27:20 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-11-27 07:23:48 +00:00
|
|
|
# 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.
|
2009-02-07 19:27:20 +00:00
|
|
|
#
|
|
|
|
# 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
|
2009-11-27 07:23:48 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2009-02-07 19:27:20 +00:00
|
|
|
#
|
2009-11-27 07:23:48 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-12-20 05:48:43 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-07 19:27:20 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2010-11-26 10:34:59 +00:00
|
|
|
import glob
|
2010-11-23 15:11:24 +00:00
|
|
|
import logging
|
2010-11-26 17:07:05 +00:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
from reportlab import rl_config
|
|
|
|
|
2011-02-07 12:57:23 +00:00
|
|
|
from openerp.tools import config
|
2009-02-07 19:27:20 +00:00
|
|
|
|
2011-06-23 09:04:57 +00:00
|
|
|
#.apidoc title: TTF Font Table
|
|
|
|
|
2010-11-23 15:09:45 +00:00
|
|
|
"""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).
|
2010-11-26 10:34:59 +00:00
|
|
|
|
2010-12-20 05:48:43 +00:00
|
|
|
Due to an awful configuration that ships with reportlab at many Linux
|
2010-11-26 10:34:59 +00:00
|
|
|
and Ubuntu distros, we have to override the search path, too.
|
2010-11-23 15:09:45 +00:00
|
|
|
"""
|
|
|
|
|
2009-02-07 19:27:20 +00:00
|
|
|
CustomTTFonts = [ ('Helvetica',"DejaVu Sans", "DejaVuSans.ttf", 'normal'),
|
2009-11-24 14:44:05 +00:00
|
|
|
('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'),]
|
2009-02-07 19:27:20 +00:00
|
|
|
|
2010-11-26 10:34:59 +00:00
|
|
|
|
2010-12-20 05:48:43 +00:00
|
|
|
TTFSearchPath_Linux = [
|
2010-11-26 10:34:59 +00:00
|
|
|
'/usr/share/fonts/truetype', # SuSE
|
|
|
|
'/usr/share/fonts/dejavu', '/usr/share/fonts/liberation', # Fedora, RHEL
|
|
|
|
'/usr/share/fonts/truetype/*', # Ubuntu,
|
|
|
|
'/usr/share/fonts/TTF/*', # at Mandriva/Mageia
|
2010-12-20 05:48:43 +00:00
|
|
|
]
|
2010-11-26 10:34:59 +00:00
|
|
|
|
2010-12-20 05:48:43 +00:00
|
|
|
TTFSearchPath_Windows = [
|
2010-11-26 17:07:05 +00:00
|
|
|
'c:/winnt/fonts',
|
|
|
|
'c:/windows/fonts'
|
2010-12-20 05:48:43 +00:00
|
|
|
]
|
2010-11-26 17:07:05 +00:00
|
|
|
|
2010-12-20 05:48:43 +00:00
|
|
|
TTFSearchPath_Darwin = [
|
2010-11-26 17:07:05 +00:00
|
|
|
#mac os X - from
|
|
|
|
#http://developer.apple.com/technotes/tn/tn2024.html
|
|
|
|
'~/Library/Fonts',
|
|
|
|
'/Library/Fonts',
|
|
|
|
'/Network/Library/Fonts',
|
|
|
|
'/System/Library/Fonts',
|
2010-12-20 05:48:43 +00:00
|
|
|
]
|
2010-11-26 17:07:05 +00:00
|
|
|
|
|
|
|
TTFSearchPathMap = {
|
|
|
|
'Darwin': TTFSearchPath_Darwin,
|
|
|
|
'Windows': TTFSearchPath_Windows,
|
|
|
|
'Linux': TTFSearchPath_Linux,
|
|
|
|
}
|
|
|
|
|
2010-11-26 10:34:59 +00:00
|
|
|
# ----- The code below is less distro-specific, please avoid editing! -------
|
2010-11-26 17:07:05 +00:00
|
|
|
__foundFonts = None
|
2010-11-23 15:09:45 +00:00
|
|
|
|
|
|
|
def FindCustomFonts():
|
|
|
|
"""Fill the __foundFonts list with those filenames, whose fonts
|
|
|
|
can be found in the reportlab ttf font path.
|
2010-12-20 05:48:43 +00:00
|
|
|
|
2010-11-23 15:09:45 +00:00
|
|
|
This process needs only be done once per loading of this module,
|
|
|
|
it is cached. But, if the system admin adds some font in the
|
|
|
|
meanwhile, the server must be restarted eventually.
|
|
|
|
"""
|
|
|
|
dirpath = []
|
2010-11-23 15:11:24 +00:00
|
|
|
log = logging.getLogger('report.fonts')
|
2010-11-23 15:09:45 +00:00
|
|
|
global __foundFonts
|
2010-11-26 17:07:05 +00:00
|
|
|
__foundFonts = {}
|
2010-11-26 10:34:59 +00:00
|
|
|
searchpath = []
|
|
|
|
|
2010-11-26 17:07:05 +00:00
|
|
|
if config.get('fonts_search_path'):
|
|
|
|
searchpath += map(str.strip, config.get('fonts_search_path').split(','))
|
2010-11-26 10:34:59 +00:00
|
|
|
|
2010-11-26 17:07:05 +00:00
|
|
|
local_platform = platform.system()
|
|
|
|
if local_platform in TTFSearchPathMap:
|
|
|
|
searchpath += TTFSearchPathMap[local_platform]
|
2010-11-26 10:34:59 +00:00
|
|
|
|
2010-11-26 17:07:05 +00:00
|
|
|
# Append the original search path of reportlab (at the end)
|
|
|
|
searchpath += rl_config.TTFSearchPath
|
|
|
|
|
|
|
|
# Perform the search for font files ourselves, as reportlab's
|
|
|
|
# TTFOpenFile is not very good at it.
|
2010-11-26 10:34:59 +00:00
|
|
|
for dirglob in searchpath:
|
|
|
|
dirglob = os.path.expanduser(dirglob)
|
|
|
|
for dirname in glob.iglob(dirglob):
|
|
|
|
abp = os.path.abspath(dirname)
|
|
|
|
if os.path.isdir(abp):
|
|
|
|
dirpath.append(abp)
|
2010-11-26 17:07:05 +00:00
|
|
|
|
|
|
|
for k, (name, font, filename, mode) in enumerate(CustomTTFonts):
|
|
|
|
if filename in __foundFonts:
|
2010-11-23 15:09:45 +00:00
|
|
|
continue
|
|
|
|
for d in dirpath:
|
2010-11-26 17:07:05 +00:00
|
|
|
abs_filename = os.path.join(d, filename)
|
|
|
|
if os.path.exists(abs_filename):
|
|
|
|
log.debug("Found font %s at %s", filename, abs_filename)
|
|
|
|
__foundFonts[filename] = abs_filename
|
2010-11-23 15:09:45 +00:00
|
|
|
break
|
2010-11-26 10:34:59 +00:00
|
|
|
|
2009-02-07 19:27:20 +00:00
|
|
|
def SetCustomFonts(rmldoc):
|
2010-11-23 15:09:45 +00:00
|
|
|
""" Map some font names to the corresponding TTF fonts
|
2010-12-20 05:48:43 +00:00
|
|
|
|
2010-11-23 15:09:45 +00:00
|
|
|
The ttf font may not even have the same name, as in
|
|
|
|
Times -> Liberation Serif.
|
|
|
|
This function is called once per report, so it should
|
|
|
|
avoid system-wide processing (cache it, instead).
|
|
|
|
"""
|
2010-11-23 15:11:10 +00:00
|
|
|
global __foundFonts
|
2010-11-26 17:07:05 +00:00
|
|
|
if __foundFonts is None:
|
2010-11-23 15:09:45 +00:00
|
|
|
FindCustomFonts()
|
2010-11-26 17:07:05 +00:00
|
|
|
for name, font, filename, mode in CustomTTFonts:
|
2010-11-26 18:30:43 +00:00
|
|
|
if os.path.isabs(filename) and os.path.exists(filename):
|
2010-11-26 17:07:05 +00:00
|
|
|
rmldoc.setTTFontMapping(name, font, filename, mode)
|
|
|
|
elif filename in __foundFonts:
|
|
|
|
rmldoc.setTTFontMapping(name, font, __foundFonts[filename], mode)
|
2009-11-26 06:30:20 +00:00
|
|
|
return True
|
2009-11-25 13:41:16 +00:00
|
|
|
|
2009-11-24 14:44:05 +00:00
|
|
|
#eof
|
2011-11-22 08:58:48 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|