home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
- __docformat__ = 'restructuredtext en'
- import os
- import sys
- from threading import Thread
- from calibre.constants import plugins, iswindows
- (_fc, _fc_err) = plugins['fontconfig']
- if _fc is None:
- raise RuntimeError('Failed to load fontconfig with error:' + _fc_err)
- _fc is None
-
- class FontConfig(Thread):
-
- def __init__(self):
- Thread.__init__(self)
- self.daemon = True
- self.failed = False
-
-
- def run(self):
- config = None
- if getattr(sys, 'frameworks_dir', False):
- config_dir = os.path.join(os.path.dirname(getattr(sys, 'frameworks_dir')), 'Resources', 'fonts')
- if isinstance(config_dir, unicode):
- config_dir = config_dir.encode(sys.getfilesystemencoding())
-
- config = os.path.join(config_dir, 'fonts.conf')
-
- if iswindows and getattr(sys, 'frozen', False):
- config_dir = os.path.join(os.path.dirname(sys.executable), 'fontconfig')
- if isinstance(config_dir, unicode):
- config_dir = config_dir.encode(sys.getfilesystemencoding())
-
- config = os.path.join(config_dir, 'fonts.conf')
-
-
- try:
- _fc.initialize(config)
- except:
- import traceback
- traceback.print_exc()
- self.failed = True
-
-
-
- def wait(self):
- self.join()
- if self.failed:
- raise RuntimeError('Failed to initialize fontconfig')
- self.failed
-
-
- def find_font_families(self, allowed_extensions = [
- 'ttf',
- 'otf']):
- self.wait()
- ans = []([ bytes('.' + x) for x in allowed_extensions ])
- ans = sorted(set(ans), cmp = (lambda x, y: cmp(x.lower(), y.lower())))
- ans2 = []
- for x in ans:
-
- try:
- ans2.append(x.decode('utf-8'))
- continue
- except UnicodeDecodeError:
- []
- []
- _fc.find_font_families
- continue
- continue
-
-
-
- return ans2
-
-
- def files_for_family(self, family, normalize = True):
- self.wait()
- if isinstance(family, unicode):
- family = family.encode('utf-8')
-
- fonts = { }
- ofamily = str(family).decode('utf-8')
- for fullname, path, style, nfamily, weight, slant in _fc.files_for_family(str(family)):
- style = (slant, weight)
- if normalize:
- italic = slant > 0
- normal = weight == 80
- bold = weight > 80
- if italic:
- if normal:
- pass
- elif bold:
- pass
-
- style = 'li'
- elif normal:
- pass
- elif bold:
- pass
-
- style = 'light'
-
-
- try:
- fullname = fullname.decode('utf-8')
- path = path.decode('utf-8')
- nfamily = nfamily.decode('utf-8')
- except UnicodeDecodeError:
- continue
-
- if style in fonts:
- if nfamily.lower().strip() == ofamily.lower().strip() and 'Condensed' not in fullname and 'ExtraLight' not in fullname:
- fonts[style] = (path, fullname)
-
- 'ExtraLight' not in fullname
- fonts[style] = (path, fullname)
-
- return fonts
-
-
- def match(self, name, all = False, verbose = False):
- self.wait()
- if isinstance(name, unicode):
- name = name.encode('utf-8')
-
- fonts = []
- for fullname, path, style, family, weight, slant in _fc.match(str(name), bool(all), bool(verbose)):
-
- try:
- fullname = fullname.decode('utf-8')
- path = path.decode('utf-8')
- style = style.decode('utf-8')
- family = family.decode('utf-8')
- fonts.append({
- 'fullname': fullname,
- 'path': path,
- 'style': style,
- 'family': family,
- 'weight': weight,
- 'slant': slant })
- continue
- except UnicodeDecodeError:
- continue
- continue
-
-
-
- if all:
- return fonts
- if fonts:
- return fonts[0]
-
-
- fontconfig = FontConfig()
- fontconfig.start()
-
- def test():
- pprint = pprint
- import pprint
- pprint(fontconfig.find_font_families())
- pprint(fontconfig.files_for_family('liberation serif'))
- m = None if iswindows else 'liberation serif'
- pprint(fontconfig.match(m + ':slant=italic:weight=bold', verbose = True))
-
- if __name__ == '__main__':
- test()
-
-