Back to index

moin  1.9.0~rc2
img.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.formatters.img
00004     ~~~~~~~~~~~~~~~~~~~~~~~
00005 
00006     Formatter for Pixmap output.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 
00012 import sys
00013 from commands import getstatusoutput
00014 
00015 from pygments.formatter import Formatter
00016 from pygments.util import get_bool_opt, get_int_opt, get_choice_opt
00017 
00018 # Import this carefully
00019 try:
00020     import Image, ImageDraw, ImageFont
00021     pil_available = True
00022 except ImportError:
00023     pil_available = False
00024 
00025 try:
00026     import _winreg
00027 except ImportError:
00028     _winreg = None
00029 
00030 __all__ = ['ImageFormatter', 'GifImageFormatter', 'JpgImageFormatter',
00031            'BmpImageFormatter']
00032 
00033 
00034 # For some unknown reason every font calls it something different
00035 STYLES = {
00036     'NORMAL':     ['', 'Roman', 'Book', 'Normal', 'Regular', 'Medium'],
00037     'ITALIC':     ['Oblique', 'Italic'],
00038     'BOLD':       ['Bold'],
00039     'BOLDITALIC': ['Bold Oblique', 'Bold Italic'],
00040 }
00041 
00042 # A sane default for modern systems
00043 DEFAULT_FONT_NAME_NIX = 'Bitstream Vera Sans Mono'
00044 DEFAULT_FONT_NAME_WIN = 'Courier New'
00045 
00046 
00047 class PilNotAvailable(ImportError):
00048     """When Python imaging library is not available"""
00049 
00050 
00051 class FontNotFound(Exception):
00052     """When there are no usable fonts specified"""
00053 
00054 
00055 class FontManager(object):
00056     """
00057     Manages a set of fonts: normal, italic, bold, etc...
00058     """
00059 
00060     def __init__(self, font_name, font_size=14):
00061         self.font_name = font_name
00062         self.font_size = font_size
00063         self.fonts = {}
00064         self.encoding = None
00065         if sys.platform.startswith('win'):
00066             if not font_name:
00067                 self.font_name = DEFAULT_FONT_NAME_WIN
00068             self._create_win()
00069         else:
00070             if not font_name:
00071                 self.font_name = DEFAULT_FONT_NAME_NIX
00072             self._create_nix()
00073 
00074     def _get_nix_font_path(self, name, style):
00075         exit, out = getstatusoutput('fc-list "%s:style=%s" file' %
00076                                     (name, style))
00077         if not exit:
00078             lines = out.splitlines()
00079             if lines:
00080                 path = lines[0].strip().strip(':')
00081                 return path
00082 
00083     def _create_nix(self):
00084         for name in STYLES['NORMAL']:
00085             path = self._get_nix_font_path(self.font_name, name)
00086             if path is not None:
00087                 self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
00088                 break
00089         else:
00090             raise FontNotFound('No usable fonts named: "%s"' %
00091                                self.font_name)
00092         for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
00093             for stylename in STYLES[style]:
00094                 path = self._get_nix_font_path(self.font_name, stylename)
00095                 if path is not None:
00096                     self.fonts[style] = ImageFont.truetype(path, self.font_size)
00097                     break
00098             else:
00099                 if style == 'BOLDITALIC':
00100                     self.fonts[style] = self.fonts['BOLD']
00101                 else:
00102                     self.fonts[style] = self.fonts['NORMAL']
00103 
00104     def _lookup_win(self, key, basename, styles, fail=False):
00105         for suffix in ('', ' (TrueType)'):
00106             for style in styles:
00107                 try:
00108                     valname = '%s%s%s' % (basename, style and ' '+style, suffix)
00109                     val, _ = _winreg.QueryValueEx(key, valname)
00110                     return val
00111                 except EnvironmentError:
00112                     continue
00113         else:
00114             if fail:
00115                 raise FontNotFound('Font %s (%s) not found in registry' %
00116                                    (basename, styles[0]))
00117             return None
00118 
00119     def _create_win(self):
00120         try:
00121             key = _winreg.OpenKey(
00122                 _winreg.HKEY_LOCAL_MACHINE,
00123                 r'Software\Microsoft\Windows NT\CurrentVersion\Fonts')
00124         except EnvironmentError:
00125             try:
00126                 key = _winreg.OpenKey(
00127                     _winreg.HKEY_LOCAL_MACHINE,
00128                     r'Software\Microsoft\Windows\CurrentVersion\Fonts')
00129             except EnvironmentError:
00130                 raise FontNotFound('Can\'t open Windows font registry key')
00131         try:
00132             path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True)
00133             self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size)
00134             for style in ('ITALIC', 'BOLD', 'BOLDITALIC'):
00135                 path = self._lookup_win(key, self.font_name, STYLES[style])
00136                 if path:
00137                     self.fonts[style] = ImageFont.truetype(path, self.font_size)
00138                 else:
00139                     if style == 'BOLDITALIC':
00140                         self.fonts[style] = self.fonts['BOLD']
00141                     else:
00142                         self.fonts[style] = self.fonts['NORMAL']
00143         finally:
00144             _winreg.CloseKey(key)
00145 
00146     def get_char_size(self):
00147         """
00148         Get the character size.
00149         """
00150         return self.fonts['NORMAL'].getsize('M')
00151 
00152     def get_font(self, bold, oblique):
00153         """
00154         Get the font based on bold and italic flags.
00155         """
00156         if bold and oblique:
00157             return self.fonts['BOLDITALIC']
00158         elif bold:
00159             return self.fonts['BOLD']
00160         elif oblique:
00161             return self.fonts['ITALIC']
00162         else:
00163             return self.fonts['NORMAL']
00164 
00165 
00166 class ImageFormatter(Formatter):
00167     """
00168     Create a PNG image from source code. This uses the Python Imaging Library to
00169     generate a pixmap from the source code.
00170 
00171     *New in Pygments 0.10.*
00172 
00173     Additional options accepted:
00174 
00175     `image_format`
00176         An image format to output to that is recognised by PIL, these include:
00177 
00178         * "PNG" (default)
00179         * "JPEG"
00180         * "BMP"
00181         * "GIF"
00182 
00183     `line_pad`
00184         The extra spacing (in pixels) between each line of text.
00185 
00186         Default: 2
00187 
00188     `font_name`
00189         The font name to be used as the base font from which others, such as
00190         bold and italic fonts will be generated.  This really should be a
00191         monospace font to look sane.
00192 
00193         Default: "Bitstream Vera Sans Mono"
00194 
00195     `font_size`
00196         The font size in points to be used.
00197 
00198         Default: 14
00199 
00200     `image_pad`
00201         The padding, in pixels to be used at each edge of the resulting image.
00202 
00203         Default: 10
00204 
00205     `line_numbers`
00206         Whether line numbers should be shown: True/False
00207 
00208         Default: True
00209 
00210     `line_number_step`
00211         The step used when printing line numbers.
00212 
00213         Default: 1
00214 
00215     `line_number_bg`
00216         The background colour (in "#123456" format) of the line number bar, or
00217         None to use the style background color.
00218 
00219         Default: "#eed"
00220 
00221     `line_number_fg`
00222         The text color of the line numbers (in "#123456"-like format).
00223 
00224         Default: "#886"
00225 
00226     `line_number_chars`
00227         The number of columns of line numbers allowable in the line number
00228         margin.
00229 
00230         Default: 2
00231 
00232     `line_number_bold`
00233         Whether line numbers will be bold: True/False
00234 
00235         Default: False
00236 
00237     `line_number_italic`
00238         Whether line numbers will be italicized: True/False
00239 
00240         Default: False
00241 
00242     `line_number_separator`
00243         Whether a line will be drawn between the line number area and the
00244         source code area: True/False
00245 
00246         Default: True
00247 
00248     `line_number_pad`
00249         The horizontal padding (in pixels) between the line number margin, and
00250         the source code area.
00251 
00252         Default: 6
00253     """
00254 
00255     # Required by the pygments mapper
00256     name = 'img'
00257     aliases = ['img', 'IMG', 'png']
00258     filenames = ['*.png']
00259 
00260     unicodeoutput = False
00261 
00262     default_image_format = 'png'
00263 
00264     def __init__(self, **options):
00265         """
00266         See the class docstring for explanation of options.
00267         """
00268         if not pil_available:
00269             raise PilNotAvailable(
00270                 'Python Imaging Library is required for this formatter')
00271         Formatter.__init__(self, **options)
00272         # Read the style
00273         self.styles = dict(self.style)
00274         if self.style.background_color is None:
00275             self.background_color = '#fff'
00276         else:
00277             self.background_color = self.style.background_color
00278         # Image options
00279         self.image_format = get_choice_opt(
00280             options, 'image_format', ['png', 'jpeg', 'gif', 'bmp'],
00281             self.default_image_format, normcase=True)
00282         self.image_pad = get_int_opt(options, 'image_pad', 10)
00283         self.line_pad = get_int_opt(options, 'line_pad', 2)
00284         # The fonts
00285         fontsize = get_int_opt(options, 'font_size', 14)
00286         self.fonts = FontManager(options.get('font_name', ''), fontsize)
00287         self.fontw, self.fonth = self.fonts.get_char_size()
00288         # Line number options
00289         self.line_number_fg = options.get('line_number_fg', '#886')
00290         self.line_number_bg = options.get('line_number_bg', '#eed')
00291         self.line_number_chars = get_int_opt(options,
00292                                         'line_number_chars', 2)
00293         self.line_number_bold = get_bool_opt(options,
00294                                         'line_number_bold', False)
00295         self.line_number_italic = get_bool_opt(options,
00296                                         'line_number_italic', False)
00297         self.line_number_pad = get_int_opt(options, 'line_number_pad', 6)
00298         self.line_numbers = get_bool_opt(options, 'line_numbers', True)
00299         self.line_number_separator = get_bool_opt(options,
00300                                         'line_number_separator', True)
00301         self.line_number_step = get_int_opt(options, 'line_number_step', 1)
00302         if self.line_numbers:
00303             self.line_number_width = (self.fontw * self.line_number_chars +
00304                                    self.line_number_pad * 2)
00305         else:
00306             self.line_number_width = 0
00307         self.drawables = []
00308 
00309     def get_style_defs(self, arg=''):
00310         raise NotImplementedError('The -S option is meaningless for the image '
00311                                   'formatter. Use -O style=<stylename> instead.')
00312 
00313     def _get_line_height(self):
00314         """
00315         Get the height of a line.
00316         """
00317         return self.fonth + self.line_pad
00318 
00319     def _get_line_y(self, lineno):
00320         """
00321         Get the Y coordinate of a line number.
00322         """
00323         return lineno * self._get_line_height() + self.image_pad
00324 
00325     def _get_char_width(self):
00326         """
00327         Get the width of a character.
00328         """
00329         return self.fontw
00330 
00331     def _get_char_x(self, charno):
00332         """
00333         Get the X coordinate of a character position.
00334         """
00335         return charno * self.fontw + self.image_pad + self.line_number_width
00336 
00337     def _get_text_pos(self, charno, lineno):
00338         """
00339         Get the actual position for a character and line position.
00340         """
00341         return self._get_char_x(charno), self._get_line_y(lineno)
00342 
00343     def _get_linenumber_pos(self, lineno):
00344         """
00345         Get the actual position for the start of a line number.
00346         """
00347         return (self.image_pad, self._get_line_y(lineno))
00348 
00349     def _get_text_color(self, style):
00350         """
00351         Get the correct color for the token from the style.
00352         """
00353         if style['color'] is not None:
00354             fill = '#' + style['color']
00355         else:
00356             fill = '#000'
00357         return fill
00358 
00359     def _get_style_font(self, style):
00360         """
00361         Get the correct font for the style.
00362         """
00363         return self.fonts.get_font(style['bold'], style['italic'])
00364 
00365     def _get_image_size(self, maxcharno, maxlineno):
00366         """
00367         Get the required image size.
00368         """
00369         return (self._get_char_x(maxcharno) + self.image_pad,
00370                 self._get_line_y(maxlineno + 0) + self.image_pad)
00371 
00372     def _draw_linenumber(self, lineno):
00373         """
00374         Remember a line number drawable to paint later.
00375         """
00376         self._draw_text(
00377             self._get_linenumber_pos(lineno),
00378             str(lineno + 1).rjust(self.line_number_chars),
00379             font=self.fonts.get_font(self.line_number_bold,
00380                                      self.line_number_italic),
00381             fill=self.line_number_fg,
00382         )
00383 
00384     def _draw_text(self, pos, text, font, **kw):
00385         """
00386         Remember a single drawable tuple to paint later.
00387         """
00388         self.drawables.append((pos, text, font, kw))
00389 
00390     def _create_drawables(self, tokensource):
00391         """
00392         Create drawables for the token content.
00393         """
00394         lineno = charno = maxcharno = 0
00395         for ttype, value in tokensource:
00396             while ttype not in self.styles:
00397                 ttype = ttype.parent
00398             style = self.styles[ttype]
00399             # TODO: make sure tab expansion happens earlier in the chain.  It
00400             # really ought to be done on the input, as to do it right here is
00401             # quite complex.
00402             value = value.expandtabs(4)
00403             lines = value.splitlines(True)
00404             #print lines
00405             for i, line in enumerate(lines):
00406                 temp = line.rstrip('\n')
00407                 if temp:
00408                     self._draw_text(
00409                         self._get_text_pos(charno, lineno),
00410                         temp,
00411                         font = self._get_style_font(style),
00412                         fill = self._get_text_color(style)
00413                     )
00414                     charno += len(temp)
00415                     maxcharno = max(maxcharno, charno)
00416                 if line.endswith('\n'):
00417                     # add a line for each extra line in the value
00418                     charno = 0
00419                     lineno += 1
00420         self.maxcharno = maxcharno
00421         self.maxlineno = lineno
00422 
00423     def _draw_line_numbers(self):
00424         """
00425         Create drawables for the line numbers.
00426         """
00427         if not self.line_numbers:
00428             return
00429         for i in xrange(self.maxlineno):
00430             if ((i + 1) % self.line_number_step) == 0:
00431                 self._draw_linenumber(i)
00432 
00433     def _paint_line_number_bg(self, im):
00434         """
00435         Paint the line number background on the image.
00436         """
00437         if not self.line_numbers:
00438             return
00439         if self.line_number_fg is None:
00440             return
00441         draw = ImageDraw.Draw(im)
00442         recth = im.size[-1]
00443         rectw = self.image_pad + self.line_number_width - self.line_number_pad
00444         draw.rectangle([(0, 0),
00445                         (rectw, recth)],
00446              fill=self.line_number_bg)
00447         draw.line([(rectw, 0), (rectw, recth)], fill=self.line_number_fg)
00448         del draw
00449 
00450     def format(self, tokensource, outfile):
00451         """
00452         Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
00453         tuples and write it into ``outfile``.
00454 
00455         This implementation calculates where it should draw each token on the
00456         pixmap, then calculates the required pixmap size and draws the items.
00457         """
00458         self._create_drawables(tokensource)
00459         self._draw_line_numbers()
00460         im = Image.new(
00461             'RGB',
00462             self._get_image_size(self.maxcharno, self.maxlineno),
00463             self.background_color
00464         )
00465         self._paint_line_number_bg(im)
00466         draw = ImageDraw.Draw(im)
00467         for pos, value, font, kw in self.drawables:
00468             draw.text(pos, value, font=font, **kw)
00469         im.save(outfile, self.image_format.upper())
00470 
00471 
00472 # Add one formatter per format, so that the "-f gif" option gives the correct result
00473 # when used in pygmentize.
00474 
00475 class GifImageFormatter(ImageFormatter):
00476     """
00477     Create a GIF image from source code. This uses the Python Imaging Library to
00478     generate a pixmap from the source code.
00479 
00480     *New in Pygments 1.0.* (You could create GIF images before by passing a
00481     suitable `image_format` option to the `ImageFormatter`.)
00482     """
00483 
00484     name = 'img_gif'
00485     aliases = ['gif']
00486     filenames = ['*.gif']
00487     default_image_format = 'gif'
00488 
00489 
00490 class JpgImageFormatter(ImageFormatter):
00491     """
00492     Create a JPEG image from source code. This uses the Python Imaging Library to
00493     generate a pixmap from the source code.
00494 
00495     *New in Pygments 1.0.* (You could create JPEG images before by passing a
00496     suitable `image_format` option to the `ImageFormatter`.)
00497     """
00498 
00499     name = 'img_jpg'
00500     aliases = ['jpg', 'jpeg']
00501     filenames = ['*.jpg']
00502     default_image_format = 'jpeg'
00503 
00504 
00505 class BmpImageFormatter(ImageFormatter):
00506     """
00507     Create a bitmap image from source code. This uses the Python Imaging Library to
00508     generate a pixmap from the source code.
00509 
00510     *New in Pygments 1.0.* (You could create bitmap images before by passing a
00511     suitable `image_format` option to the `ImageFormatter`.)
00512     """
00513 
00514     name = 'img_bmp'
00515     aliases = ['bmp', 'bitmap']
00516     filenames = ['*.bmp']
00517     default_image_format = 'bmp'