Implement #1517 (Please allow installation without non-free prs500 fonts)

This commit is contained in:
Kovid Goyal 2009-01-01 18:49:44 -08:00
parent 352b2b225a
commit 8afce4910c

View File

@ -12,24 +12,61 @@ except ImportError:
''' '''
Default fonts used in the PRS500 Default fonts used in the PRS500
''' '''
from calibre.ebooks.lrf.fonts.prs500 import tt0003m_, tt0011m_, tt0419m_
SYSTEM_FONT_PATH = '/usr/share/fonts/truetype/ttf-liberation/'
FONT_MAP = { FONT_MAP = {
'Swis721 BT Roman' : tt0003m_, 'Swis721 BT Roman' : 'tt0003m_',
'Dutch801 Rm BT Roman' : tt0011m_, 'Dutch801 Rm BT Roman' : 'tt0011m_',
'Courier10 BT Roman' : tt0419m_, 'Courier10 BT Roman' : 'tt0419m_',
} }
LIBERATION_FONT_MAP = {
'Swis721 BT Roman' : 'LiberationSans_Regular',
'Dutch801 Rm BT Roman' : 'LiberationSerif_Regular',
'Courier10 BT Roman' : 'LiberationMono_Regular',
}
SYSTEM_FONT_MAP = {}
for key, val in LIBERATION_FONT_MAP.items():
SYSTEM_FONT_MAP[key] = SYSTEM_FONT_PATH + val.replace('_', '-') + '.ttf'
FONT_FILE_MAP = {} FONT_FILE_MAP = {}
def get_font_path(name): def get_font_path(name):
if FONT_FILE_MAP.has_key(name) and os.access(FONT_FILE_MAP[name].name, os.R_OK): if FONT_FILE_MAP.has_key(name) and os.access(FONT_FILE_MAP[name].name, os.R_OK):
return FONT_FILE_MAP[name].name return FONT_FILE_MAP[name].name
p = PersistentTemporaryFile('.ttf', 'font_')
p.write(FONT_MAP[name].font_data) # translate font into file name
p.close() fname = FONT_MAP[name]
FONT_FILE_MAP[name] = p
return p.name # first, check configuration in /etc/
etc_file = os.path.join(os.path.sep, 'etc', 'calibre', 'fonts', fname + '.ttf')
if os.access(etc_file, os.R_OK):
return etc_file
# then, try calibre shipped ones
try:
try:
font_mod = __import__('calibre.ebooks.lrf.fonts.prs500', {}, {},
[fname], -1)
except ImportError:
font_mod = __import__('calibre.ebooks.lrf.fonts.liberation', {}, {},
[LIBERATION_FONT_MAP[name]], -1)
p = PersistentTemporaryFile('.ttf', 'font_')
p.write(getattr(font_mod, fname).font_data)
p.close()
FONT_FILE_MAP[name] = p
return p.name
except ImportError:
pass
# finally, try system default ones
if SYSTEM_FONT_MAP.has_key(name) and os.access(SYSTEM_FONT_MAP[name], os.R_OK):
return SYSTEM_FONT_MAP[name]
# not found
raise SystemError, 'font %s (in file %s) not installed' % (name, fname)
def get_font(name, size, encoding='unic'): def get_font(name, size, encoding='unic'):
''' '''