Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
MoinMoin.support.pygments.formatters.img.ImageFormatter Class Reference
Inheritance diagram for MoinMoin.support.pygments.formatters.img.ImageFormatter:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.support.pygments.formatters.img.ImageFormatter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def get_style_defs
def format

Public Attributes

 styles
 background_color
 image_format
 image_pad
 line_pad
 fonts
 fonth
 line_number_fg
 line_number_bg
 line_number_chars
 line_number_bold
 line_number_italic
 line_number_pad
 line_numbers
 line_number_separator
 line_number_step
 line_number_width
 drawables
 maxcharno
 maxlineno

Static Public Attributes

string name = 'img'
list aliases = ['img', 'IMG', 'png']
list filenames = ['*.png']
 unicodeoutput = False
string default_image_format = 'png'

Private Member Functions

def _get_line_height
def _get_line_y
def _get_char_width
def _get_char_x
def _get_text_pos
def _get_linenumber_pos
def _get_text_color
def _get_style_font
def _get_image_size
def _draw_linenumber
def _draw_text
def _create_drawables
def _draw_line_numbers
def _paint_line_number_bg

Detailed Description

Create a PNG image from source code. This uses the Python Imaging Library to
generate a pixmap from the source code.

*New in Pygments 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_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

Definition at line 166 of file img.py.


Constructor & Destructor Documentation

See the class docstring for explanation of options.

Definition at line 264 of file img.py.

00264 
00265     def __init__(self, **options):
00266         """
00267         See the class docstring for explanation of options.
00268         """
00269         if not pil_available:
00270             raise PilNotAvailable(
00271                 'Python Imaging Library is required for this formatter')
00272         Formatter.__init__(self, **options)
00273         # Read the style
00274         self.styles = dict(self.style)
00275         if self.style.background_color is None:
00276             self.background_color = '#fff'
00277         else:
00278             self.background_color = self.style.background_color
00279         # Image options
00280         self.image_format = get_choice_opt(
00281             options, 'image_format', ['png', 'jpeg', 'gif', 'bmp'],
00282             self.default_image_format, normcase=True)
00283         self.image_pad = get_int_opt(options, 'image_pad', 10)
00284         self.line_pad = get_int_opt(options, 'line_pad', 2)
00285         # The fonts
00286         fontsize = get_int_opt(options, 'font_size', 14)
00287         self.fonts = FontManager(options.get('font_name', ''), fontsize)
00288         self.fontw, self.fonth = self.fonts.get_char_size()
00289         # Line number options
00290         self.line_number_fg = options.get('line_number_fg', '#886')
00291         self.line_number_bg = options.get('line_number_bg', '#eed')
00292         self.line_number_chars = get_int_opt(options,
00293                                         'line_number_chars', 2)
00294         self.line_number_bold = get_bool_opt(options,
00295                                         'line_number_bold', False)
00296         self.line_number_italic = get_bool_opt(options,
00297                                         'line_number_italic', False)
00298         self.line_number_pad = get_int_opt(options, 'line_number_pad', 6)
00299         self.line_numbers = get_bool_opt(options, 'line_numbers', True)
00300         self.line_number_separator = get_bool_opt(options,
00301                                         'line_number_separator', True)
00302         self.line_number_step = get_int_opt(options, 'line_number_step', 1)
00303         if self.line_numbers:
00304             self.line_number_width = (self.fontw * self.line_number_chars +
00305                                    self.line_number_pad * 2)
00306         else:
00307             self.line_number_width = 0
00308         self.drawables = []


Member Function Documentation

Create drawables for the token content.

Definition at line 390 of file img.py.

00390 
00391     def _create_drawables(self, tokensource):
00392         """
00393         Create drawables for the token content.
00394         """
00395         lineno = charno = maxcharno = 0
00396         for ttype, value in tokensource:
00397             while ttype not in self.styles:
00398                 ttype = ttype.parent
00399             style = self.styles[ttype]
00400             # TODO: make sure tab expansion happens earlier in the chain.  It
00401             # really ought to be done on the input, as to do it right here is
00402             # quite complex.
00403             value = value.expandtabs(4)
00404             lines = value.splitlines(True)
00405             #print lines
00406             for i, line in enumerate(lines):
00407                 temp = line.rstrip('\n')
00408                 if temp:
00409                     self._draw_text(
00410                         self._get_text_pos(charno, lineno),
00411                         temp,
00412                         font = self._get_style_font(style),
00413                         fill = self._get_text_color(style)
00414                     )
00415                     charno += len(temp)
00416                     maxcharno = max(maxcharno, charno)
00417                 if line.endswith('\n'):
00418                     # add a line for each extra line in the value
00419                     charno = 0
00420                     lineno += 1
00421         self.maxcharno = maxcharno
00422         self.maxlineno = lineno

Here is the call graph for this function:

Here is the caller graph for this function:

Create drawables for the line numbers.

Definition at line 423 of file img.py.

00423 
00424     def _draw_line_numbers(self):
00425         """
00426         Create drawables for the line numbers.
00427         """
00428         if not self.line_numbers:
00429             return
00430         for i in xrange(self.maxlineno):
00431             if ((i + 1) % self.line_number_step) == 0:
00432                 self._draw_linenumber(i)

Here is the call graph for this function:

Here is the caller graph for this function:

Remember a line number drawable to paint later.

Definition at line 372 of file img.py.

00372 
00373     def _draw_linenumber(self, lineno):
00374         """
00375         Remember a line number drawable to paint later.
00376         """
00377         self._draw_text(
00378             self._get_linenumber_pos(lineno),
00379             str(lineno + 1).rjust(self.line_number_chars),
00380             font=self.fonts.get_font(self.line_number_bold,
00381                                      self.line_number_italic),
00382             fill=self.line_number_fg,
00383         )

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.pygments.formatters.img.ImageFormatter._draw_text (   self,
  pos,
  text,
  font,
  kw 
) [private]
Remember a single drawable tuple to paint later.

Definition at line 384 of file img.py.

00384 
00385     def _draw_text(self, pos, text, font, **kw):
00386         """
00387         Remember a single drawable tuple to paint later.
00388         """
00389         self.drawables.append((pos, text, font, kw))

Here is the caller graph for this function:

Get the width of a character.

Definition at line 325 of file img.py.

00325 
00326     def _get_char_width(self):
00327         """
00328         Get the width of a character.
00329         """
00330         return self.fontw

Get the X coordinate of a character position.

Definition at line 331 of file img.py.

00331 
00332     def _get_char_x(self, charno):
00333         """
00334         Get the X coordinate of a character position.
00335         """
00336         return charno * self.fontw + self.image_pad + self.line_number_width

Here is the caller graph for this function:

def MoinMoin.support.pygments.formatters.img.ImageFormatter._get_image_size (   self,
  maxcharno,
  maxlineno 
) [private]
Get the required image size.

Definition at line 365 of file img.py.

00365 
00366     def _get_image_size(self, maxcharno, maxlineno):
00367         """
00368         Get the required image size.
00369         """
00370         return (self._get_char_x(maxcharno) + self.image_pad,
00371                 self._get_line_y(maxlineno + 0) + self.image_pad)

Here is the call graph for this function:

Here is the caller graph for this function:

Get the height of a line.

Definition at line 313 of file img.py.

00313 
00314     def _get_line_height(self):
00315         """
00316         Get the height of a line.
00317         """
00318         return self.fonth + self.line_pad

Here is the caller graph for this function:

Get the Y coordinate of a line number.

Definition at line 319 of file img.py.

00319 
00320     def _get_line_y(self, lineno):
00321         """
00322         Get the Y coordinate of a line number.
00323         """
00324         return lineno * self._get_line_height() + self.image_pad

Here is the call graph for this function:

Here is the caller graph for this function:

Get the actual position for the start of a line number.

Definition at line 343 of file img.py.

00343 
00344     def _get_linenumber_pos(self, lineno):
00345         """
00346         Get the actual position for the start of a line number.
00347         """
00348         return (self.image_pad, self._get_line_y(lineno))

Here is the call graph for this function:

Here is the caller graph for this function:

Get the correct font for the style.

Definition at line 359 of file img.py.

00359 
00360     def _get_style_font(self, style):
00361         """
00362         Get the correct font for the style.
00363         """
00364         return self.fonts.get_font(style['bold'], style['italic'])

Here is the caller graph for this function:

Get the correct color for the token from the style.

Definition at line 349 of file img.py.

00349 
00350     def _get_text_color(self, style):
00351         """
00352         Get the correct color for the token from the style.
00353         """
00354         if style['color'] is not None:
00355             fill = '#' + style['color']
00356         else:
00357             fill = '#000'
00358         return fill

Here is the caller graph for this function:

def MoinMoin.support.pygments.formatters.img.ImageFormatter._get_text_pos (   self,
  charno,
  lineno 
) [private]
Get the actual position for a character and line position.

Definition at line 337 of file img.py.

00337 
00338     def _get_text_pos(self, charno, lineno):
00339         """
00340         Get the actual position for a character and line position.
00341         """
00342         return self._get_char_x(charno), self._get_line_y(lineno)

Here is the call graph for this function:

Here is the caller graph for this function:

Paint the line number background on the image.

Definition at line 433 of file img.py.

00433 
00434     def _paint_line_number_bg(self, im):
00435         """
00436         Paint the line number background on the image.
00437         """
00438         if not self.line_numbers:
00439             return
00440         if self.line_number_fg is None:
00441             return
00442         draw = ImageDraw.Draw(im)
00443         recth = im.size[-1]
00444         rectw = self.image_pad + self.line_number_width - self.line_number_pad
00445         draw.rectangle([(0, 0),
00446                         (rectw, recth)],
00447              fill=self.line_number_bg)
00448         draw.line([(rectw, 0), (rectw, recth)], fill=self.line_number_fg)
00449         del draw

Here is the caller graph for this function:

def MoinMoin.support.pygments.formatters.img.ImageFormatter.format (   self,
  tokensource,
  outfile 
)
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.

Definition at line 450 of file img.py.

00450 
00451     def format(self, tokensource, outfile):
00452         """
00453         Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
00454         tuples and write it into ``outfile``.
00455 
00456         This implementation calculates where it should draw each token on the
00457         pixmap, then calculates the required pixmap size and draws the items.
00458         """
00459         self._create_drawables(tokensource)
00460         self._draw_line_numbers()
00461         im = Image.new(
00462             'RGB',
00463             self._get_image_size(self.maxcharno, self.maxlineno),
00464             self.background_color
00465         )
00466         self._paint_line_number_bg(im)
00467         draw = ImageDraw.Draw(im)
00468         for pos, value, font, kw in self.drawables:
00469             draw.text(pos, value, font=font, **kw)
00470         im.save(outfile, self.image_format.upper())
00471 
00472 
00473 # Add one formatter per format, so that the "-f gif" option gives the correct result
00474 # when used in pygmentize.

Here is the call graph for this function:

Definition at line 309 of file img.py.

00309 
00310     def get_style_defs(self, arg=''):
00311         raise NotImplementedError('The -S option is meaningless for the image '
00312                                   'formatter. Use -O style=<stylename> instead.')

Here is the caller graph for this function:


Member Data Documentation

Definition at line 275 of file img.py.

Definition at line 307 of file img.py.

Definition at line 287 of file img.py.

Definition at line 286 of file img.py.

Definition at line 279 of file img.py.

Definition at line 282 of file img.py.

Definition at line 290 of file img.py.

Definition at line 293 of file img.py.

Definition at line 291 of file img.py.

Definition at line 289 of file img.py.

Definition at line 295 of file img.py.

Definition at line 297 of file img.py.

Definition at line 299 of file img.py.

Definition at line 301 of file img.py.

Definition at line 303 of file img.py.

Definition at line 298 of file img.py.

Definition at line 283 of file img.py.

Definition at line 420 of file img.py.

Definition at line 421 of file img.py.

Definition at line 273 of file img.py.

Definition at line 260 of file img.py.


The documentation for this class was generated from the following file: