home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1396 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.8 KB  |  174 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import os
  9. import sys
  10. from threading import Thread
  11. from calibre.constants import plugins, iswindows
  12. (_fc, _fc_err) = plugins['fontconfig']
  13. if _fc is None:
  14.     raise RuntimeError('Failed to load fontconfig with error:' + _fc_err)
  15. _fc is None
  16.  
  17. class FontConfig(Thread):
  18.     
  19.     def __init__(self):
  20.         Thread.__init__(self)
  21.         self.daemon = True
  22.         self.failed = False
  23.  
  24.     
  25.     def run(self):
  26.         config = None
  27.         if getattr(sys, 'frameworks_dir', False):
  28.             config_dir = os.path.join(os.path.dirname(getattr(sys, 'frameworks_dir')), 'Resources', 'fonts')
  29.             if isinstance(config_dir, unicode):
  30.                 config_dir = config_dir.encode(sys.getfilesystemencoding())
  31.             
  32.             config = os.path.join(config_dir, 'fonts.conf')
  33.         
  34.         if iswindows and getattr(sys, 'frozen', False):
  35.             config_dir = os.path.join(os.path.dirname(sys.executable), 'fontconfig')
  36.             if isinstance(config_dir, unicode):
  37.                 config_dir = config_dir.encode(sys.getfilesystemencoding())
  38.             
  39.             config = os.path.join(config_dir, 'fonts.conf')
  40.         
  41.         
  42.         try:
  43.             _fc.initialize(config)
  44.         except:
  45.             import traceback
  46.             traceback.print_exc()
  47.             self.failed = True
  48.  
  49.  
  50.     
  51.     def wait(self):
  52.         self.join()
  53.         if self.failed:
  54.             raise RuntimeError('Failed to initialize fontconfig')
  55.         self.failed
  56.  
  57.     
  58.     def find_font_families(self, allowed_extensions = [
  59.         'ttf',
  60.         'otf']):
  61.         self.wait()
  62.         ans = []([ bytes('.' + x) for x in allowed_extensions ])
  63.         ans = sorted(set(ans), cmp = (lambda x, y: cmp(x.lower(), y.lower())))
  64.         ans2 = []
  65.         for x in ans:
  66.             
  67.             try:
  68.                 ans2.append(x.decode('utf-8'))
  69.             continue
  70.             except UnicodeDecodeError:
  71.                 []
  72.                 []
  73.                 _fc.find_font_families
  74.                 continue
  75.                 continue
  76.             
  77.  
  78.         
  79.         return ans2
  80.  
  81.     
  82.     def files_for_family(self, family, normalize = True):
  83.         self.wait()
  84.         if isinstance(family, unicode):
  85.             family = family.encode('utf-8')
  86.         
  87.         fonts = { }
  88.         ofamily = str(family).decode('utf-8')
  89.         for fullname, path, style, nfamily, weight, slant in _fc.files_for_family(str(family)):
  90.             style = (slant, weight)
  91.             if normalize:
  92.                 italic = slant > 0
  93.                 normal = weight == 80
  94.                 bold = weight > 80
  95.                 if italic:
  96.                     if normal:
  97.                         pass
  98.                     elif bold:
  99.                         pass
  100.                     
  101.                     style = 'li'
  102.                 elif normal:
  103.                     pass
  104.                 elif bold:
  105.                     pass
  106.                 
  107.                 style = 'light'
  108.             
  109.             
  110.             try:
  111.                 fullname = fullname.decode('utf-8')
  112.                 path = path.decode('utf-8')
  113.                 nfamily = nfamily.decode('utf-8')
  114.             except UnicodeDecodeError:
  115.                 continue
  116.  
  117.             if style in fonts:
  118.                 if nfamily.lower().strip() == ofamily.lower().strip() and 'Condensed' not in fullname and 'ExtraLight' not in fullname:
  119.                     fonts[style] = (path, fullname)
  120.                 
  121.             'ExtraLight' not in fullname
  122.             fonts[style] = (path, fullname)
  123.         
  124.         return fonts
  125.  
  126.     
  127.     def match(self, name, all = False, verbose = False):
  128.         self.wait()
  129.         if isinstance(name, unicode):
  130.             name = name.encode('utf-8')
  131.         
  132.         fonts = []
  133.         for fullname, path, style, family, weight, slant in _fc.match(str(name), bool(all), bool(verbose)):
  134.             
  135.             try:
  136.                 fullname = fullname.decode('utf-8')
  137.                 path = path.decode('utf-8')
  138.                 style = style.decode('utf-8')
  139.                 family = family.decode('utf-8')
  140.                 fonts.append({
  141.                     'fullname': fullname,
  142.                     'path': path,
  143.                     'style': style,
  144.                     'family': family,
  145.                     'weight': weight,
  146.                     'slant': slant })
  147.             continue
  148.             except UnicodeDecodeError:
  149.                 continue
  150.                 continue
  151.             
  152.  
  153.         
  154.         if all:
  155.             return fonts
  156.         if fonts:
  157.             return fonts[0]
  158.  
  159.  
  160. fontconfig = FontConfig()
  161. fontconfig.start()
  162.  
  163. def test():
  164.     pprint = pprint
  165.     import pprint
  166.     pprint(fontconfig.find_font_families())
  167.     pprint(fontconfig.files_for_family('liberation serif'))
  168.     m = None if iswindows else 'liberation serif'
  169.     pprint(fontconfig.match(m + ':slant=italic:weight=bold', verbose = True))
  170.  
  171. if __name__ == '__main__':
  172.     test()
  173.  
  174.