diff options
Diffstat (limited to 'pygments/formatters/img.py')
-rw-r--r-- | pygments/formatters/img.py | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/pygments/formatters/img.py b/pygments/formatters/img.py index cc95ce24..6fafc476 100644 --- a/pygments/formatters/img.py +++ b/pygments/formatters/img.py @@ -5,10 +5,11 @@ Formatter for Pixmap output. - :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ +import os import sys from pygments.formatter import Formatter @@ -47,6 +48,7 @@ STYLES = { # A sane default for modern systems DEFAULT_FONT_NAME_NIX = 'Bitstream Vera Sans Mono' DEFAULT_FONT_NAME_WIN = 'Courier New' +DEFAULT_FONT_NAME_MAC = 'Courier New' class PilNotAvailable(ImportError): @@ -71,6 +73,10 @@ class FontManager(object): if not font_name: self.font_name = DEFAULT_FONT_NAME_WIN self._create_win() + elif sys.platform.startswith('darwin'): + if not font_name: + self.font_name = DEFAULT_FONT_NAME_MAC + self._create_mac() else: if not font_name: self.font_name = DEFAULT_FONT_NAME_NIX @@ -111,6 +117,37 @@ class FontManager(object): else: self.fonts[style] = self.fonts['NORMAL'] + def _get_mac_font_path(self, font_map, name, style): + return font_map.get((name + ' ' + style).strip().lower()) + + def _create_mac(self): + font_map = {} + for font_dir in (os.path.join(os.getenv("HOME"), 'Library/Fonts/'), + '/Library/Fonts/', '/System/Library/Fonts/'): + font_map.update( + ((os.path.splitext(f)[0].lower(), os.path.join(font_dir, f)) + for f in os.listdir(font_dir) if f.lower().endswith('ttf'))) + + for name in STYLES['NORMAL']: + path = self._get_mac_font_path(font_map, self.font_name, name) + if path is not None: + self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size) + break + else: + raise FontNotFound('No usable fonts named: "%s"' % + self.font_name) + for style in ('ITALIC', 'BOLD', 'BOLDITALIC'): + for stylename in STYLES[style]: + path = self._get_mac_font_path(font_map, self.font_name, stylename) + if path is not None: + self.fonts[style] = ImageFont.truetype(path, self.font_size) + break + else: + if style == 'BOLDITALIC': + self.fonts[style] = self.fonts['BOLD'] + else: + self.fonts[style] = self.fonts['NORMAL'] + def _lookup_win(self, key, basename, styles, fail=False): for suffix in ('', ' (TrueType)'): for style in styles: @@ -200,7 +237,7 @@ class ImageFormatter(Formatter): bold and italic fonts will be generated. This really should be a monospace font to look sane. - Default: "Bitstream Vera Sans Mono" on Windows, Courier New on \*nix + Default: "Bitstream Vera Sans Mono" on Windows, Courier New on \\*nix `font_size` The font size in points to be used. |