NLc@sdZddlZddlmZddlmZddlmZmZm Z m Z y&ddl m Z m Z mZeZWnek reZnXyddlZWnek reZnXddd d gZid d d dddgd6ddgd6dgd6ddgd6ZdZdZdefdYZdefdYZd efd!YZdefd"YZdefd#YZd efd$YZ d efd%YZ!dS(&s pygments.formatters.img ~~~~~~~~~~~~~~~~~~~~~~~ Formatter for Pixmap output. :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. iN(tgetstatusoutput(t Formatter(t get_bool_optt get_int_optt get_list_opttget_choice_opt(tImaget ImageDrawt ImageFonttImageFormattertGifImageFormattertJpgImageFormattertBmpImageFormatterttRomantBooktNormaltRegulartMediumtNORMALtObliquetItalictITALICtBoldtBOLDs Bold Obliques Bold Italict BOLDITALICsBitstream Vera Sans Monos Courier NewtPilNotAvailablecBseZdZRS(s,When Python imaging library is not available(t__name__t __module__t__doc__(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR0st FontNotFoundcBseZdZRS(s(When there are no usable fonts specified(RRR(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR4st FontManagercBsSeZdZddZdZdZedZdZdZ dZ RS( s> Manages a set of fonts: normal, italic, bold, etc... icCsu||_||_i|_d|_tjjdrU|sHt|_n|j n|sgt |_n|j dS(Ntwin( t font_namet font_sizetfontstNonetencodingtsystplatformt startswithtDEFAULT_FONT_NAME_WINt _create_wintDEFAULT_FONT_NAME_NIXt _create_nix(tselfR!R"((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt__init__=s       cCsXtd||f\}}|sT|j}|rT|djjd}|SndS(Nsfc-list "%s:style=%s" fileit:(Rt splitlineststrip(R-tnametstyletexittouttlinestpath((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_nix_font_pathKs cCs xftdD]G}|j|j|}|dk r tj||j|jd Get the font based on bold and italic flags. RRRRN(R#(R-tboldtoblique((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pytget_fonts    ( RRRR.R8R,tFalseRHR*RORR(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR8s     cBseZdZdZdddgZdgZeZdZdZ ddZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZRS(su 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_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 Pygments 1.2.* Default: empty list `hl_color` Specify the color for highlighting lines. *New in Pygments 1.2.* Default: highlight color of the selected style timgtIMGtpngs*.pngcKststdntj||t|j|_|jjd#krUd|_n|jj|_t |dddddg|j dt |_ t |d d |_t |d d |_t |d d}t|jdd||_|jj\|_|_|jdd|_|jdd|_t |dd |_t|dt|_t|dt|_t |dd|_t|dt |_t|dt |_t |dd|_ t |dd|_!|jr|j|j|jd |_"n d|_"g|_#t$|d g}x<|D]4}y|j#j%t&|Wq-t'k r`q-Xq-W|jd!|jj(pd"|_)g|_*d#S($sE See the class docstring for explanation of options. s5Python Imaging Library is required for this formatters#ffft image_formatRVtjpegtgiftbmptnormcaset image_padi tline_padiR"iR!R tline_number_fgs#886tline_number_bgs#eedtline_number_charstline_number_boldtline_number_italictline_number_padit line_numberstline_number_separatortline_number_stepitline_number_startithl_linesthl_colors#f90N(+t pil_availableRRR.tdictR3RBtbackground_colorR$Rtdefault_image_formatRKRWRR\R]RtgetR#ROtfontwtfonthR^R_R`RRSRaRbRcRdReRfRgtline_number_widthRhRtappendtintt ValueErrorthighlight_colorRit drawables(R-toptionstfontsizet hl_lines_strtline((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR.sX         R cCstddS(NsWThe -S option is meaningless for the image formatter. Use -O style= instead.(tNotImplementedError(R-targ((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pytget_style_defsOscCs|j|jS(s+ Get the height of a line. (RpR](R-((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_line_heightSscCs||j|jS(s8 Get the Y coordinate of a line number. (R~R\(R-tlineno((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt _get_line_yYscCs|jS(s/ Get the width of a character. (Ro(R-((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_char_width_scCs||j|j|jS(s? Get the X coordinate of a character position. (RoR\Rq(R-tcharno((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt _get_char_xescCs|j||j|fS(sL Get the actual position for a character and line position. (RR(R-RR((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt _get_text_poskscCs|j|j|fS(sI Get the actual position for the start of a line number. (R\R(R-R((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_linenumber_posqscCs+|ddk r!d|d}nd}|S(sE Get the correct color for the token from the style. tcolort#s#000N(R$(R-R3tfill((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_text_colorwscCs|jj|d|dS(s5 Get the correct font for the style. RPtitalic(R#RR(R-R3((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_style_fontscCs.|j||j|j|d|jfS(s. Get the required image size. i(RR\R(R-t maxcharnot maxlineno((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_image_sizescCsS|j|j|t|j|jd|jj|j|jd|j dS(sA Remember a line number drawable to paint later. tfontRN( t _draw_textRtstrtrjustR`R#RRRaRbR^(R-tposnoR((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_draw_linenumbers   cKs |jj||||fdS(sB Remember a single drawable tuple to paint later. N(RvRr(R-tposttextRtkw((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyRsc Cs5d}}}x|D]\}}x||jkr?|j}q$W|j|}|jd}|jt}xt|D]\} } | jd} | r|j|j||| d|j |d|j ||t | 7}t ||}n| j drxd}|d7}qxqxWqW||_||_dS(s9 Create drawables for the token content. iis RRiN(RBtparentt expandtabsR0RKt enumeratetrstripRRRRtlentmaxtendswithRR( R-t tokensourceRRRtttypetvalueR3R6tiRzttemp((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_create_drawabless,   cCs^|js dSxJt|jD]9}||j}||jdkr|j||qqWdS(s8 Create drawables for the line numbers. Ni(RdtxrangeRRgRfR(R-tptn((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_draw_line_numberss   cCs|js dS|jdkr dStj|}|jd}|j|j|j}|j d||fgd|j |j |df||fgd|j~dS(s@ Paint the line number background on the image. NiiR(ii( RdR^R$RtDrawtsizeR\RqRct rectangleR_Rz(R-timtdrawtrecthtrectw((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt_paint_line_number_bgs    (cCsN|j||jtjd|j|j|j|j}|j|t j |}|j r|j |j |jd}|j}|jd|}xW|j D]I}|j|d} |j|| f||| |fgd|jqWnx6|jD]+\} } } } |j| | d| | qW|j||jjdS(s  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. tRGBiiRRN(RRRtnewRRRRlRRRRhR\RqRcR~RRRRiRvRtsaveRWtupper(R-RtoutfileRRtxRRt linenumbertyRRRR((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pytformats&      &(RRRR2taliasest filenamesRSt unicodeoutputRmR.R}R~RRRRRRRRRRRRRR(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR s.f  7           ! cBs,eZdZdZdgZdgZdZRS(s  Create a GIF image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. *New in Pygments 1.0.* (You could create GIF images before by passing a suitable `image_format` option to the `ImageFormatter`.) timg_gifRYs*.gif(RRRR2RRRm(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR s   cBs/eZdZdZddgZdgZdZRS(s Create a JPEG image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. *New in Pygments 1.0.* (You could create JPEG images before by passing a suitable `image_format` option to the `ImageFormatter`.) timg_jpgtjpgRXs*.jpg(RRRR2RRRm(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR s   cBs/eZdZdZddgZdgZdZRS(s Create a bitmap image from source code. This uses the Python Imaging Library to generate a pixmap from the source code. *New in Pygments 1.0.* (You could create bitmap images before by passing a suitable `image_format` option to the `ImageFormatter`.) timg_bmpRZtbitmaps*.bmp(RRRR2RRRm(((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyR s   ("RR&tcommandsRtpygments.formatterRt pygments.utilRRRRtPILRRRRKRjt ImportErrorRSR=R$t__all__R9R+R)Rt ExceptionRtobjectRR R R R (((s;/usr/lib/python2.7/site-packages/pygments/formatters/img.pyt s< "         oY