class documentation

class ImageFormatter(Formatter):

Known subclasses: pygments.formatters.img.BmpImageFormatter, pygments.formatters.img.GifImageFormatter, pygments.formatters.img.JpgImageFormatter

View In Hierarchy

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

New in version 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: "Courier New" on Windows, "Menlo" on Mac OS, and
"DejaVu Sans Mono" on *nix
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.

New in version 1.2.

Default: empty list

hl_color

Specify the color for highlighting lines.

New in version 1.2.

Default: highlight color of the selected style

Method __init__ See the class docstring for explanation of options.
Method ​_create​_drawables Create drawables for the token content.
Method ​_draw​_line​_numbers Create drawables for the line numbers.
Method ​_draw​_linenumber Remember a line number drawable to paint later.
Method ​_draw​_text Remember a single drawable tuple to paint later.
Method ​_get​_char​_width Get the width of a character.
Method ​_get​_char​_x Get the X coordinate of a character position.
Method ​_get​_image​_size Get the required image size.
Method ​_get​_line​_height Get the height of a line.
Method ​_get​_line​_y Get the Y coordinate of a line number.
Method ​_get​_linenumber​_pos Get the actual position for the start of a line number.
Method ​_get​_style​_font Get the correct font for the style.
Method ​_get​_text​_bg​_color Get the correct background color for the token from the style.
Method ​_get​_text​_color Get the correct color for the token from the style.
Method ​_get​_text​_pos Get the actual position for a character and line position.
Method ​_paint​_line​_number​_bg Paint the line number background on the image.
Method format Format tokensource, an iterable of (tokentype, tokenstring) tuples and write it into outfile.
Method get​_style​_defs Return the style definitions for the current style as a string.
Class Variable aliases Undocumented
Class Variable default​_image​_format Undocumented
Class Variable filenames Undocumented
Class Variable name Undocumented
Class Variable unicodeoutput Undocumented
Instance Variable background​_color Undocumented
Instance Variable drawables Undocumented
Instance Variable encoding Undocumented
Instance Variable fonth Undocumented
Instance Variable fonts Undocumented
Instance Variable fontw Undocumented
Instance Variable hl​_color Undocumented
Instance Variable hl​_lines Undocumented
Instance Variable image​_format Undocumented
Instance Variable image​_pad Undocumented
Instance Variable line​_number​_bg Undocumented
Instance Variable line​_number​_bold Undocumented
Instance Variable line​_number​_chars Undocumented
Instance Variable line​_number​_fg Undocumented
Instance Variable line​_number​_italic Undocumented
Instance Variable line​_number​_pad Undocumented
Instance Variable line​_number​_separator Undocumented
Instance Variable line​_number​_start Undocumented
Instance Variable line​_number​_step Undocumented
Instance Variable line​_number​_width Undocumented
Instance Variable line​_numbers Undocumented
Instance Variable line​_pad Undocumented
Instance Variable maxcharno Undocumented
Instance Variable maxlinelength Undocumented
Instance Variable maxlineno Undocumented
Instance Variable styles Undocumented

Inherited from Formatter:

Instance Variable full Undocumented
Instance Variable options Undocumented
Instance Variable style Undocumented
Instance Variable title Undocumented
def __init__(self, **options):
See the class docstring for explanation of options.
def _create_drawables(self, tokensource):
Create drawables for the token content.
def _draw_line_numbers(self):
Create drawables for the line numbers.
def _draw_linenumber(self, posno, lineno):
Remember a line number drawable to paint later.
def _draw_text(self, pos, text, font, text_fg, text_bg):
Remember a single drawable tuple to paint later.
def _get_char_width(self):
Get the width of a character.
def _get_char_x(self, linelength):
Get the X coordinate of a character position.
def _get_image_size(self, maxlinelength, maxlineno):
Get the required image size.
def _get_line_height(self):
Get the height of a line.
def _get_line_y(self, lineno):
Get the Y coordinate of a line number.
def _get_linenumber_pos(self, lineno):
Get the actual position for the start of a line number.
def _get_style_font(self, style):
Get the correct font for the style.
def _get_text_bg_color(self, style):
Get the correct background color for the token from the style.
def _get_text_color(self, style):
Get the correct color for the token from the style.
def _get_text_pos(self, linelength, lineno):
Get the actual position for a character and line position.
def _paint_line_number_bg(self, im):
Paint the line number background on the image.
def 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.

def get_style_defs(self, arg=''):

Return the style definitions for the current style as a string.

arg is an additional argument whose meaning depends on the formatter used. Note that arg can also be a list or tuple for some formatters like the html formatter.

unicodeoutput: bool =
background_color: str =

Undocumented

drawables: list =

Undocumented

encoding: str =

Undocumented

fonth =

Undocumented

fonts =

Undocumented

fontw =

Undocumented

hl_color =

Undocumented

hl_lines: list =

Undocumented

image_format =

Undocumented

image_pad =

Undocumented

line_number_bg =

Undocumented

line_number_bold =

Undocumented

line_number_chars =

Undocumented

line_number_fg =

Undocumented

line_number_italic =

Undocumented

line_number_pad =

Undocumented

line_number_separator =

Undocumented

line_number_start =

Undocumented

line_number_step =

Undocumented

line_number_width =

Undocumented

line_numbers =

Undocumented

line_pad =

Undocumented

maxcharno =

Undocumented

maxlinelength =

Undocumented

maxlineno =

Undocumented

styles =

Undocumented