Y-Qc@sdZddlZddlmZddlmZmZmZmZy&ddl m Z m Z m Z e ZWnek reZnXyddlZWnek reZnXdddd gZid d d d ddgd6ddgd6dgd6ddgd6ZdZdZdefdYZdefdYZdefd YZdefd!YZdefd"YZdefd#YZd efd$YZdS(%s pygments.formatters.img ~~~~~~~~~~~~~~~~~~~~~~~ Formatter for Pixmap output. :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. iN(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__(((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyR/st FontNotFoundcBseZdZRS(s(When there are no usable fonts specified(RRR(((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyR3st 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!((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt__init__<s       cCshddlm}|d||f\}}|sd|j}|rd|djjd}|SndS(Ni(tgetstatusoutputsfc-list "%s:style=%s" fileit:(tcommandsR.t splitlineststrip(R,tnametstyleR.texittouttlinestpath((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_nix_font_pathJs cCs xftdD]G}|j|j|}|dk r tj||j|jdtOpenKeytHKEY_LOCAL_MACHINER@RRIR R:tTrueRR;R!R"tCloseKey(R,RAR8R4((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyR)xs,      cCs|jdjdS(s) Get the character size. RtM(R"tgetsize(R,((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt get_char_sizescCsH|r|r|jdS|r(|jdS|r9|jdS|jdSdS(s> Get the font based on bold and italic flags. RRRRN(R"(R,tboldtoblique((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pytget_fonts    ( RRRR-R9R+tFalseRIR)RPRS(((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyR7s     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_formatRWtjpegtgiftbmptnormcaset 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-tdictR4RCtbackground_colorR#Rtdefault_image_formatRLRXRR]R^RtgetR"RPtfontwtfonthR_R`RaRRTRbRcRdReRfRgRhtline_number_widthRiRtappendtintt ValueErrorthighlight_colorRjt drawables(R,toptionstfontsizet hl_lines_strtline((sA/usr/local/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((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pytget_style_defsOscCs|j|jS(s+ Get the height of a line. (RqR^(R,((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_line_heightSscCs||j|jS(s8 Get the Y coordinate of a line number. (RR](R,tlineno((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt _get_line_yYscCs|jS(s/ Get the width of a character. (Rp(R,((sA/usr/local/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. (RpR]Rr(R,tcharno((sA/usr/local/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((sA/usr/local/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((sA/usr/local/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,R4tfill((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt_get_text_colorwscCs|jj|d|dS(s5 Get the correct font for the style. RQtitalic(R"RS(R,R4((sA/usr/local/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((sA/usr/local/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_textRtstrtrjustRaR"RSRbRcR_(R,tposnoR((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt_draw_linenumbers   cKs |jj||||fdS(sB Remember a single drawable tuple to paint later. N(RwRs(R,tposttextRtkw((sA/usr/local/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(RCtparentt expandtabsR1RLt enumeratetrstripRRRRtlentmaxtendswithRR( R,t tokensourceRRRtttypetvalueR4R7tiR{ttemp((sA/usr/local/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(RetxrangeRRhRgR(R,tptn((sA/usr/local/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( ReR_R#RtDrawtsizeR]RrRdt rectangleR`R{(R,timtdrawtrecthtrectw((sA/usr/local/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(RRRtnewRRRRmRRRRiR]RrRdRRRRRjRwRtsaveRXtupper(R,RtoutfileRRtxRRt linenumbertyRRRR((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pytformats&      &(RRRR3taliasest filenamesRTt unicodeoutputRnR-R~RRRRRRRRRRRRRRR(((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyRs.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_gifRZs*.gif(RRRR3RRRn(((sA/usr/local/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_jpgtjpgRYs*.jpg(RRRR3RRRn(((sA/usr/local/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_bmpR[tbitmaps*.bmp(RRRR3RRRn(((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyR s   ( RR%tpygments.formatterRt pygments.utilRRRRtPILRRRRLRkt ImportErrorRTR>R#t__all__R:R*R(Rt ExceptionRtobjectRRR R R (((sA/usr/local/lib/python2.7/site-packages/pygments/formatters/img.pyt s: "         pY