<s> """<STR_LIT>""" <EOL> import sys <EOL> from pygments . formatter import Formatter <EOL> from pygments . util import get_bool_opt , get_int_opt , get_list_opt , get_choice_opt <EOL> try : <EOL> from PIL import Image , ImageDraw , ImageFont <EOL> pil_available = True <EOL> except ImportError : <EOL> pil_available = False <EOL> try : <EOL> import _winreg <EOL> except ImportError : <EOL> _winreg = None <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> STYLES = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> DEFAULT_FONT_NAME_NIX = '<STR_LIT>' <EOL> DEFAULT_FONT_NAME_WIN = '<STR_LIT>' <EOL> class PilNotAvailable ( ImportError ) : <EOL> """<STR_LIT>""" <EOL> class FontNotFound ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class FontManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , font_name , font_size = <NUM_LIT> ) : <EOL> self . font_name = font_name <EOL> self . font_size = font_size <EOL> self . fonts = { } <EOL> self . encoding = None <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> if not font_name : <EOL> self . font_name = DEFAULT_FONT_NAME_WIN <EOL> self . _create_win ( ) <EOL> else : <EOL> if not font_name : <EOL> self . font_name = DEFAULT_FONT_NAME_NIX <EOL> self . _create_nix ( ) <EOL> def _get_nix_font_path ( self , name , style ) : <EOL> from commands import getstatusoutput <EOL> exit , out = getstatusoutput ( '<STR_LIT>' % <EOL> ( name , style ) ) <EOL> if not exit : <EOL> lines = out . splitlines ( ) <EOL> if lines : <EOL> path = lines [ <NUM_LIT:0> ] . strip ( ) . strip ( '<STR_LIT::>' ) <EOL> return path <EOL> def _create_nix ( self ) : <EOL> for name in STYLES [ '<STR_LIT>' ] : <EOL> path = self . _get_nix_font_path ( self . font_name , name ) <EOL> if path is not None : <EOL> self . fonts [ '<STR_LIT>' ] = ImageFont . truetype ( path , self . font_size ) <EOL> break <EOL> else : <EOL> raise FontNotFound ( '<STR_LIT>' % <EOL> self . font_name ) <EOL> for style in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for stylename in STYLES [ style ] : <EOL> path = self . _get_nix_font_path ( self . font_name , stylename ) <EOL> if path is not None : <EOL> self . fonts [ style ] = ImageFont . truetype ( path , self . font_size ) <EOL> break <EOL> else : <EOL> if style == '<STR_LIT>' : <EOL> self . fonts [ style ] = self . fonts [ '<STR_LIT>' ] <EOL> else : <EOL> self . fonts [ style ] = self . fonts [ '<STR_LIT>' ] <EOL> def _lookup_win ( self , key , basename , styles , fail = False ) : <EOL> for suffix in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for style in styles : <EOL> try : <EOL> valname = '<STR_LIT>' % ( basename , style and '<STR_LIT:U+0020>' + style , suffix ) <EOL> val , _ = _winreg . QueryValueEx ( key , valname ) <EOL> return val <EOL> except EnvironmentError : <EOL> continue <EOL> else : <EOL> if fail : <EOL> raise FontNotFound ( '<STR_LIT>' % <EOL> ( basename , styles [ <NUM_LIT:0> ] ) ) <EOL> return None <EOL> def _create_win ( self ) : <EOL> try : <EOL> key = _winreg . OpenKey ( <EOL> _winreg . HKEY_LOCAL_MACHINE , <EOL> r'<STR_LIT>' ) <EOL> except EnvironmentError : <EOL> try : <EOL> key = _winreg . OpenKey ( <EOL> _winreg . HKEY_LOCAL_MACHINE , <EOL> r'<STR_LIT>' ) <EOL> except EnvironmentError : <EOL> raise FontNotFound ( '<STR_LIT>' ) <EOL> try : <EOL> path = self . _lookup_win ( key , self . font_name , STYLES [ '<STR_LIT>' ] , True ) <EOL> self . fonts [ '<STR_LIT>' ] = ImageFont . truetype ( path , self . font_size ) <EOL> for style in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> path = self . _lookup_win ( key , self . font_name , STYLES [ style ] ) <EOL> if path : <EOL> self . fonts [ style ] = ImageFont . truetype ( path , self . font_size ) <EOL> else : <EOL> if style == '<STR_LIT>' : <EOL> self . fonts [ style ] = self . fonts [ '<STR_LIT>' ] <EOL> else : <EOL> self . fonts [ style ] = self . fonts [ '<STR_LIT>' ] <EOL> finally : <EOL> _winreg . CloseKey ( key ) <EOL> def get_char_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fonts [ '<STR_LIT>' ] . getsize ( '<STR_LIT:M>' ) <EOL> def get_font ( self , bold , oblique ) : <EOL> """<STR_LIT>""" <EOL> if bold and oblique : <EOL> return self . fonts [ '<STR_LIT>' ] <EOL> elif bold : <EOL> return self . fonts [ '<STR_LIT>' ] <EOL> elif oblique : <EOL> return self . fonts [ '<STR_LIT>' ] <EOL> else : <EOL> return self . fonts [ '<STR_LIT>' ] <EOL> class ImageFormatter ( Formatter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> filenames = [ '<STR_LIT>' ] <EOL> unicodeoutput = False <EOL> default_image_format = '<STR_LIT>' <EOL> def __init__ ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> if not pil_available : <EOL> raise PilNotAvailable ( <EOL> '<STR_LIT>' ) <EOL> Formatter . __init__ ( self , ** options ) <EOL> self . styles = dict ( self . style ) <EOL> if self . style . background_color is None : <EOL> self . background_color = '<STR_LIT>' <EOL> else : <EOL> self . background_color = self . style . background_color <EOL> self . image_format = get_choice_opt ( <EOL> options , '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> self . default_image_format , normcase = True ) <EOL> self . image_pad = get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:10> ) <EOL> self . line_pad = get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:2> ) <EOL> fontsize = get_int_opt ( options , '<STR_LIT>' , <NUM_LIT> ) <EOL> self . fonts = FontManager ( options . get ( '<STR_LIT>' , '<STR_LIT>' ) , fontsize ) <EOL> self . fontw , self . fonth = self . fonts . get_char_size ( ) <EOL> self . line_number_fg = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . line_number_bg = options . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . line_number_chars = get_int_opt ( options , <EOL> '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . line_number_bold = get_bool_opt ( options , <EOL> '<STR_LIT>' , False ) <EOL> self . line_number_italic = get_bool_opt ( options , <EOL> '<STR_LIT>' , False ) <EOL> self . line_number_pad = get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:6> ) <EOL> self . line_numbers = get_bool_opt ( options , '<STR_LIT>' , True ) <EOL> self . line_number_separator = get_bool_opt ( options , <EOL> '<STR_LIT>' , True ) <EOL> self . line_number_step = get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . line_number_start = get_int_opt ( options , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if self . line_numbers : <EOL> self . line_number_width = ( self . fontw * self . line_number_chars + <EOL> self . line_number_pad * <NUM_LIT:2> ) <EOL> else : <EOL> self . line_number_width = <NUM_LIT:0> <EOL> self . hl_lines = [ ] <EOL> hl_lines_str = get_list_opt ( options , '<STR_LIT>' , [ ] ) <EOL> for line in hl_lines_str : <EOL> try : <EOL> self . hl_lines . append ( int ( line ) ) <EOL> except ValueError : <EOL> pass <EOL> self . hl_color = options . get ( '<STR_LIT>' , <EOL> self . style . highlight_color ) or '<STR_LIT>' <EOL> self . drawables = [ ] <EOL> def get_style_defs ( self , arg = '<STR_LIT>' ) : <EOL> raise NotImplementedError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _get_line_height ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fonth + self . line_pad <EOL> def _get_line_y ( self , lineno ) : <EOL> """<STR_LIT>""" <EOL> return lineno * self . _get_line_height ( ) + self . image_pad <EOL> def _get_char_width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . fontw <EOL> def _get_char_x ( self , charno ) : <EOL> """<STR_LIT>""" <EOL> return charno * self . fontw + self . image_pad + self . line_number_width <EOL> def _get_text_pos ( self , charno , lineno ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_char_x ( charno ) , self . _get_line_y ( lineno ) <EOL> def _get_linenumber_pos ( self , lineno ) : <EOL> """<STR_LIT>""" <EOL> return ( self . image_pad , self . _get_line_y ( lineno ) ) <EOL> def _get_text_color ( self , style ) : <EOL> """<STR_LIT>""" <EOL> if style [ '<STR_LIT>' ] is not None : <EOL> fill = '<STR_LIT:#>' + style [ '<STR_LIT>' ] <EOL> else : <EOL> fill = '<STR_LIT>' <EOL> return fill <EOL> def _get_style_font ( self , style ) : <EOL> """<STR_LIT>""" <EOL> return self . fonts . get_font ( style [ '<STR_LIT>' ] , style [ '<STR_LIT>' ] ) <EOL> def _get_image_size ( self , maxcharno , maxlineno ) : <EOL> """<STR_LIT>""" <EOL> return ( self . _get_char_x ( maxcharno ) + self . image_pad , <EOL> self . _get_line_y ( maxlineno + <NUM_LIT:0> ) + self . image_pad ) <EOL> def _draw_linenumber ( self , posno , lineno ) : <EOL> """<STR_LIT>""" <EOL> self . _draw_text ( <EOL> self . _get_linenumber_pos ( posno ) , <EOL> str ( lineno ) . rjust ( self . line_number_chars ) , <EOL> font = self . fonts . get_font ( self . line_number_bold , <EOL> self . line_number_italic ) , <EOL> fill = self . line_number_fg , <EOL> ) <EOL> def _draw_text ( self , pos , text , font , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . drawables . append ( ( pos , text , font , kw ) ) <EOL> def _create_drawables ( self , tokensource ) : <EOL> """<STR_LIT>""" <EOL> lineno = charno = maxcharno = <NUM_LIT:0> <EOL> for ttype , value in tokensource : <EOL> while ttype not in self . styles : <EOL> ttype = ttype . parent <EOL> style = self . styles [ ttype ] <EOL> value = value . expandtabs ( <NUM_LIT:4> ) <EOL> lines = value . splitlines ( True ) <EOL> for i , line in enumerate ( lines ) : <EOL> temp = line . rstrip ( '<STR_LIT:\n>' ) <EOL> if temp : <EOL> self . _draw_text ( <EOL> self . _get_text_pos ( charno , lineno ) , <EOL> temp , <EOL> font = self . _get_style_font ( style ) , <EOL> fill = self . _get_text_color ( style ) <EOL> ) <EOL> charno += len ( temp ) <EOL> maxcharno = max ( maxcharno , charno ) <EOL> if line . endswith ( '<STR_LIT:\n>' ) : <EOL> charno = <NUM_LIT:0> <EOL> lineno += <NUM_LIT:1> <EOL> self . maxcharno = maxcharno <EOL> self . maxlineno = lineno <EOL> def _draw_line_numbers ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . line_numbers : <EOL> return <EOL> for p in xrange ( self . maxlineno ) : <EOL> n = p + self . line_number_start <EOL> if ( n % self . line_number_step ) == <NUM_LIT:0> : <EOL> self . _draw_linenumber ( p , n ) <EOL> def _paint_line_number_bg ( self , im ) : <EOL> """<STR_LIT>""" <EOL> if not self . line_numbers : <EOL> return <EOL> if self . line_number_fg is None : <EOL> return <EOL> draw = ImageDraw . Draw ( im ) <EOL> recth = im . size [ - <NUM_LIT:1> ] <EOL> rectw = self . image_pad + self . line_number_width - self . line_number_pad <EOL> draw . rectangle ( [ ( <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ( rectw , recth ) ] , <EOL> fill = self . line_number_bg ) <EOL> draw . line ( [ ( rectw , <NUM_LIT:0> ) , ( rectw , recth ) ] , fill = self . line_number_fg ) <EOL> del draw <EOL> def format ( self , tokensource , outfile ) : <EOL> """<STR_LIT>""" <EOL> self . _create_drawables ( tokensource ) <EOL> self . _draw_line_numbers ( ) <EOL> im = Image . new ( <EOL> '<STR_LIT>' , <EOL> self . _get_image_size ( self . maxcharno , self . maxlineno ) , <EOL> self . background_color <EOL> ) <EOL> self . _paint_line_number_bg ( im ) <EOL> draw = ImageDraw . Draw ( im ) <EOL> if self . hl_lines : <EOL> x = self . image_pad + self . line_number_width - self . line_number_pad + <NUM_LIT:1> <EOL> recth = self . _get_line_height ( ) <EOL> rectw = im . size [ <NUM_LIT:0> ] - x <EOL> for linenumber in self . hl_lines : <EOL> y = self . _get_line_y ( linenumber - <NUM_LIT:1> ) <EOL> draw . rectangle ( [ ( x , y ) , ( x + rectw , y + recth ) ] , <EOL> fill = self . hl_color ) <EOL> for pos , value , font , kw in self . drawables : <EOL> draw . text ( pos , value , font = font , ** kw ) <EOL> im . save ( outfile , self . image_format . upper ( ) ) <EOL> class GifImageFormatter ( ImageFormatter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT>' ] <EOL> filenames = [ '<STR_LIT>' ] <EOL> default_image_format = '<STR_LIT>' <EOL> class JpgImageFormatter ( ImageFormatter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> filenames = [ '<STR_LIT>' ] <EOL> default_image_format = '<STR_LIT>' <EOL> class BmpImageFormatter ( ImageFormatter ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> aliases = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> filenames = [ '<STR_LIT>' ] <EOL> default_image_format = '<STR_LIT>' </s>
