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 |
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': |