Coverage for pygments.formatters.img : 75%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- pygments.formatters.img ~~~~~~~~~~~~~~~~~~~~~~~
Formatter for Pixmap output.
:copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """
get_choice_opt, xrange
# Import this carefully except ImportError: pil_available = False
'BmpImageFormatter']
# For some unknown reason every font calls it something different 'NORMAL': ['', 'Roman', 'Book', 'Normal', 'Regular', 'Medium'], 'ITALIC': ['Oblique', 'Italic'], 'BOLD': ['Bold'], 'BOLDITALIC': ['Bold Oblique', 'Bold Italic'], }
# A sane default for modern systems
"""When Python imaging library is not available"""
"""When there are no usable fonts specified"""
""" Manages a set of fonts: normal, italic, bold, etc... """
if not font_name: self.font_name = DEFAULT_FONT_NAME_WIN self._create_win() else:
except ImportError: from subprocess import getstatusoutput (name, style))
else: raise FontNotFound('No usable fonts named: "%s"' % self.font_name) else: if style == 'BOLDITALIC': self.fonts[style] = self.fonts['BOLD'] else: self.fonts[style] = self.fonts['NORMAL']
for suffix in ('', ' (TrueType)'): for style in styles: try: valname = '%s%s%s' % (basename, style and ' '+style, suffix) val, _ = _winreg.QueryValueEx(key, valname) return val except EnvironmentError: continue else: if fail: raise FontNotFound('Font %s (%s) not found in registry' % (basename, styles[0])) return None
try: key = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows NT\CurrentVersion\Fonts') except EnvironmentError: try: key = _winreg.OpenKey( _winreg.HKEY_LOCAL_MACHINE, r'Software\Microsoft\Windows\CurrentVersion\Fonts') except EnvironmentError: raise FontNotFound('Can\'t open Windows font registry key') try: path = self._lookup_win(key, self.font_name, STYLES['NORMAL'], True) self.fonts['NORMAL'] = ImageFont.truetype(path, self.font_size) for style in ('ITALIC', 'BOLD', 'BOLDITALIC'): path = self._lookup_win(key, self.font_name, STYLES[style]) if path: self.fonts[style] = ImageFont.truetype(path, self.font_size) else: if style == 'BOLDITALIC': self.fonts[style] = self.fonts['BOLD'] else: self.fonts[style] = self.fonts['NORMAL'] finally: _winreg.CloseKey(key)
""" Get the character size. """
""" Get the font based on bold and italic flags. """ return self.fonts['BOLDITALIC'] return self.fonts['ITALIC'] else:
""" Create a PNG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
.. versionadded:: 0.10
Additional options accepted:
`image_format` An image format to output to that is recognised by PIL, these include:
* "PNG" (default) * "JPEG" * "BMP" * "GIF"
`line_pad` The extra spacing (in pixels) between each line of text.
Default: 2
`font_name` The font name to be used as the base font from which others, such as bold and italic fonts will be generated. This really should be a monospace font to look sane.
Default: "Bitstream Vera Sans Mono"
`font_size` The font size in points to be used.
Default: 14
`image_pad` The padding, in pixels to be used at each edge of the resulting image.
Default: 10
`line_numbers` Whether line numbers should be shown: True/False
Default: True
`line_number_start` The line number of the first line.
Default: 1
`line_number_step` The step used when printing line numbers.
Default: 1
`line_number_bg` The background colour (in "#123456" format) of the line number bar, or None to use the style background color.
Default: "#eed"
`line_number_fg` The text color of the line numbers (in "#123456"-like format).
Default: "#886"
`line_number_chars` The number of columns of line numbers allowable in the line number margin.
Default: 2
`line_number_bold` Whether line numbers will be bold: True/False
Default: False
`line_number_italic` Whether line numbers will be italicized: True/False
Default: False
`line_number_separator` Whether a line will be drawn between the line number area and the source code area: True/False
Default: True
`line_number_pad` The horizontal padding (in pixels) between the line number margin, and the source code area.
Default: 6
`hl_lines` Specify a list of lines to be highlighted.
.. versionadded:: 1.2
Default: empty list
`hl_color` Specify the color for highlighting lines.
.. versionadded:: 1.2
Default: highlight color of the selected style """
# Required by the pygments mapper
""" See the class docstring for explanation of options. """ raise PilNotAvailable( 'Python Imaging Library is required for this formatter') # Read the style self.background_color = '#fff' else: # Image options options, 'image_format', ['png', 'jpeg', 'gif', 'bmp'], self.default_image_format, normcase=True) # The fonts # Line number options 'line_number_chars', 2) 'line_number_bold', False) 'line_number_italic', False) 'line_number_separator', True) self.line_number_pad * 2) else: self.line_number_width = 0 try: self.hl_lines.append(int(line)) except ValueError: pass self.style.highlight_color) or '#f90'
'formatter. Use -O style=<stylename> instead.')
""" Get the height of a line. """
""" Get the Y coordinate of a line number. """
""" Get the width of a character. """ return self.fontw
""" Get the X coordinate of a character position. """
""" Get the actual position for a character and line position. """
""" Get the actual position for the start of a line number. """
""" Get the correct color for the token from the style. """ else:
""" Get the correct font for the style. """
""" Get the required image size. """ self._get_line_y(maxlineno + 0) + self.image_pad)
""" Remember a line number drawable to paint later. """ self._get_linenumber_pos(posno), str(lineno).rjust(self.line_number_chars), font=self.fonts.get_font(self.line_number_bold, self.line_number_italic), fill=self.line_number_fg, )
""" Remember a single drawable tuple to paint later. """
""" Create drawables for the token content. """ ttype = ttype.parent # TODO: make sure tab expansion happens earlier in the chain. It # really ought to be done on the input, as to do it right here is # quite complex. # print lines self._get_text_pos(charno, lineno), temp, font = self._get_style_font(style), fill = self._get_text_color(style) ) # add a line for each extra line in the value
""" Create drawables for the line numbers. """ return
""" Paint the line number background on the image. """ return return fill=self.line_number_bg)
""" Format ``tokensource``, an iterable of ``(tokentype, tokenstring)`` tuples and write it into ``outfile``.
This implementation calculates where it should draw each token on the pixmap, then calculates the required pixmap size and draws the items. """ 'RGB', self._get_image_size(self.maxcharno, self.maxlineno), self.background_color ) # Highlight x = self.image_pad + self.line_number_width - self.line_number_pad + 1 recth = self._get_line_height() rectw = im.size[0] - x for linenumber in self.hl_lines: y = self._get_line_y(linenumber - 1) draw.rectangle([(x, y), (x + rectw, y + recth)], fill=self.hl_color)
# Add one formatter per format, so that the "-f gif" option gives the correct result # when used in pygmentize.
""" Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
.. versionadded:: 1.0 """
""" Create a JPEG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
.. versionadded:: 1.0 """
""" Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code.
.. versionadded:: 1.0 """
|