[IMP] miscellaneous improvements
bzr revid: dizzy.zala@gmail.com-20130917055129-zvh1tbhghy1vr9oc
This commit is contained in:
parent
58fcaa9d40
commit
b1647e2d32
|
@ -114,9 +114,7 @@ class res_company(osv.osv):
|
|||
return self.pool['res.company'].search(cr, uid, [('partner_id', 'in', ids)], context=context)
|
||||
|
||||
def _get_font(self, cr, uid, context=None):
|
||||
if not customfonts.supported_fonts:
|
||||
customfonts.RegisterCustomFonts()
|
||||
return sorted(customfonts.supported_fonts)
|
||||
return sorted(customfonts.RegisterCustomFonts())
|
||||
|
||||
_columns = {
|
||||
'name': fields.related('partner_id', 'name', string='Company Name', size=128, required=True, store=True, type='char'),
|
||||
|
|
|
@ -20,14 +20,13 @@
|
|||
#
|
||||
##############################################################################
|
||||
|
||||
import logging
|
||||
import os
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase import ttfonts
|
||||
import platform
|
||||
from reportlab import rl_config
|
||||
from reportlab.pdfbase import pdfmetrics, ttfonts
|
||||
import logging
|
||||
import matplotlib.font_manager
|
||||
import os
|
||||
|
||||
#.apidoc title: TTF Font Table
|
||||
# .apidoc title: TTF Font Table
|
||||
|
||||
"""This module allows the mapping of some system-available TTF fonts to
|
||||
the reportlab engine.
|
||||
|
@ -38,85 +37,47 @@ 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.
|
||||
"""
|
||||
supported_fonts = []
|
||||
_fonts_info = {'supported_fonts':[], 'font_len': 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'),
|
||||
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'),
|
||||
('Sun-ExtA', "Sun-ExtA", "Sun-ExtA.ttf", 'normal'),
|
||||
]
|
||||
|
||||
TTFSearchPath_Linux = [
|
||||
'/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
|
||||
'/usr/share/fonts/TTF', # Arch Linux
|
||||
]
|
||||
|
||||
TTFSearchPath_Windows = [
|
||||
'c:/winnt/fonts',
|
||||
'c:/windows/fonts'
|
||||
]
|
||||
|
||||
TTFSearchPath_Darwin = [
|
||||
#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,
|
||||
}
|
||||
|
||||
|
||||
def RegisterCustomFonts():
|
||||
searchpath = []
|
||||
local_platform = platform.system()
|
||||
if local_platform in TTFSearchPathMap:
|
||||
searchpath += TTFSearchPathMap[local_platform]
|
||||
# Append the original search path of reportlab (at the end)
|
||||
searchpath += rl_config.TTFSearchPath
|
||||
addToPdfmetrics(searchpath)
|
||||
return True
|
||||
|
||||
def addToPdfmetrics(path):
|
||||
global supported_fonts
|
||||
for dirname in path:
|
||||
if os.path.exists(dirname):
|
||||
for filename in [x for x in os.listdir(dirname) if x.lower().endswith('.ttf')]:
|
||||
filepath = os.path.join(dirname, filename)
|
||||
try:
|
||||
face = ttfonts.TTFontFace(filepath)
|
||||
font_info = ttfonts.TTFontFile(filepath)
|
||||
if (face.name,face.name) not in supported_fonts:
|
||||
pdfmetrics.registerFont(ttfonts.TTFont(face.name, filepath, asciiReadable=0))
|
||||
supported_fonts.append((face.name,face.name))
|
||||
CustomTTFonts.append((font_info.familyName,font_info.name,filename,font_info.styleName.lower().replace(" ","")))
|
||||
_logger.debug("Found font %s at %s", face.name, filename)
|
||||
except:
|
||||
_logger.warning("Could not register Font %s",filename)
|
||||
return True
|
||||
global _fonts_info
|
||||
all_system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
|
||||
if len(all_system_fonts) > _fonts_info['font_len']:
|
||||
for dirname in all_system_fonts:
|
||||
try:
|
||||
face = ttfonts.TTFontFace(dirname)
|
||||
if (face.name, face.name) not in _fonts_info['supported_fonts']:
|
||||
font_info = ttfonts.TTFontFile(dirname)
|
||||
pdfmetrics.registerFont(ttfonts.TTFont(face.name, dirname, asciiReadable=0))
|
||||
_fonts_info['supported_fonts'].append((face.name, face.name))
|
||||
CustomTTFonts.append((font_info.familyName, font_info.name, dirname.split('/')[-1], font_info.styleName.lower().replace(" ", "")))
|
||||
_logger.debug("Found font %s at %s", face.name, dirname)
|
||||
except:
|
||||
_logger.warning("Could not register Font %s", dirname)
|
||||
_fonts_info['font_len'] = len(all_system_fonts)
|
||||
return _fonts_info['supported_fonts']
|
||||
|
||||
def SetCustomFonts(rmldoc):
|
||||
""" Map some font names to the corresponding TTF fonts
|
||||
|
@ -126,13 +87,13 @@ def SetCustomFonts(rmldoc):
|
|||
This function is called once per report, so it should
|
||||
avoid system-wide processing (cache it, instead).
|
||||
"""
|
||||
global supported_fonts
|
||||
if not supported_fonts:
|
||||
global _fonts_info
|
||||
if not _fonts_info['supported_fonts']:
|
||||
RegisterCustomFonts()
|
||||
for name, font, filename, mode in CustomTTFonts:
|
||||
if os.path.isabs(filename) and os.path.exists(filename):
|
||||
rmldoc.setTTFontMapping(name, font, filename, mode)
|
||||
return True
|
||||
#eof
|
||||
# eof
|
||||
|
||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||
|
|
Loading…
Reference in New Issue