ThirdParty/Pygments/pygments/formatters/img.py

changeset 5713
6762afd9f963
parent 5072
aab59042fefb
child 6651
e8f3b5568b21
equal deleted inserted replaced
5712:f0d08bdeacf4 5713:6762afd9f963
3 pygments.formatters.img 3 pygments.formatters.img
4 ~~~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 Formatter for Pixmap output. 6 Formatter for Pixmap output.
7 7
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import os
12 import sys 13 import sys
13 14
14 from pygments.formatter import Formatter 15 from pygments.formatter import Formatter
15 from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \ 16 from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
16 get_choice_opt, xrange 17 get_choice_opt, xrange
45 } 46 }
46 47
47 # A sane default for modern systems 48 # A sane default for modern systems
48 DEFAULT_FONT_NAME_NIX = 'Bitstream Vera Sans Mono' 49 DEFAULT_FONT_NAME_NIX = 'Bitstream Vera Sans Mono'
49 DEFAULT_FONT_NAME_WIN = 'Courier New' 50 DEFAULT_FONT_NAME_WIN = 'Courier New'
51 DEFAULT_FONT_NAME_MAC = 'Courier New'
50 52
51 53
52 class PilNotAvailable(ImportError): 54 class PilNotAvailable(ImportError):
53 """When Python imaging library is not available""" 55 """When Python imaging library is not available"""
54 56
69 self.encoding = None 71 self.encoding = None
70 if sys.platform.startswith('win'): 72 if sys.platform.startswith('win'):
71 if not font_name: 73 if not font_name:
72 self.font_name = DEFAULT_FONT_NAME_WIN 74 self.font_name = DEFAULT_FONT_NAME_WIN
73 self._create_win() 75 self._create_win()
76 elif sys.platform.startswith('darwin'):
77 if not font_name:
78 self.font_name = DEFAULT_FONT_NAME_MAC
79 self._create_mac()
74 else: 80 else:
75 if not font_name: 81 if not font_name:
76 self.font_name = DEFAULT_FONT_NAME_NIX 82 self.font_name = DEFAULT_FONT_NAME_NIX
77 self._create_nix() 83 self._create_nix()
78 84
100 raise FontNotFound('No usable fonts named: "%s"' % 106 raise FontNotFound('No usable fonts named: "%s"' %
101 self.font_name) 107 self.font_name)
102 for style in ('ITALIC', 'BOLD', 'BOLDITALIC'): 108 for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
103 for stylename in STYLES[style]: 109 for stylename in STYLES[style]:
104 path = self._get_nix_font_path(self.font_name, stylename) 110 path = self._get_nix_font_path(self.font_name, stylename)
111 if path is not None:
112 self.fonts[style] = ImageFont.truetype(path, self.font_size)
113 break
114 else:
115 if style == 'BOLDITALIC':
116 self.fonts[style] = self.fonts['BOLD']
117 else:
118 self.fonts[style] = self.fonts['NORMAL']
119
120 def _get_mac_font_path(self, font_map, name, style):
121 return font_map.get((name + ' ' + style).strip().lower())
122
123 def _create_mac(self):
124 font_map = {}
125 for font_dir in (os.path.join(os.getenv("HOME"), 'Library/Fonts/'),
126 '/Library/Fonts/', '/System/Library/Fonts/'):
127 font_map.update(
128 ((os.path.splitext(f)[0].lower(), os.path.join(font_dir, f))
129 for f in os.listdir(font_dir) if f.lower().endswith('ttf')))
130
131 for name in STYLES['NORMAL']:
132 path = self._get_mac_font_path(font_map, self.font_name, name)
133 if path is not None:
134 self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
135 break
136 else:
137 raise FontNotFound('No usable fonts named: "%s"' %
138 self.font_name)
139 for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
140 for stylename in STYLES[style]:
141 path = self._get_mac_font_path(font_map, self.font_name, stylename)
105 if path is not None: 142 if path is not None:
106 self.fonts[style] = ImageFont.truetype(path, self.font_size) 143 self.fonts[style] = ImageFont.truetype(path, self.font_size)
107 break 144 break
108 else: 145 else:
109 if style == 'BOLDITALIC': 146 if style == 'BOLDITALIC':

eric ide

mercurial