
E~Vc           @   s  d  Z  d d l Z d d l m Z d d l m Z m Z m Z m Z m	 Z	 d d l
 Z
 y& d d l m Z m Z m Z e Z Wn e k
 r e Z n Xy d d l Z Wn; e k
 r y d d l Z Wn e k
 r e Z n Xn Xd d d d	 g Z i d
 d d d d d g d 6d d g d 6d g d 6d d g d 6Z d Z d Z d e f d     YZ d e f d     YZ d e f d      YZ d e f d!     YZ d e f d"     YZ  d e f d#     YZ! d	 e f d$     YZ" d S(%   s   
    pygments.formatters.img
    ~~~~~~~~~~~~~~~~~~~~~~~

    Formatter for Pixmap output.

    :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iN(   t	   Formatter(   t   get_bool_optt   get_int_optt   get_list_optt   get_choice_optt   xrange(   t   Imaget	   ImageDrawt	   ImageFontt   ImageFormattert   GifImageFormattert   JpgImageFormattert   BmpImageFormattert    t   Romant   Bookt   Normalt   Regulart   Mediumt   NORMALt   Obliquet   Italict   ITALICt   Boldt   BOLDs   Bold Obliques   Bold Italict
   BOLDITALICs   Bitstream Vera Sans Monos   Courier Newt   PilNotAvailablec           B   s   e  Z d  Z RS(   s,   When Python imaging library is not available(   t   __name__t
   __module__t   __doc__(    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR   4   s   t   FontNotFoundc           B   s   e  Z d  Z RS(   s(   When there are no usable fonts specified(   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR   8   s   t   FontManagerc           B   sS   e  Z d  Z d d  Z d   Z d   Z e d  Z d   Z d   Z	 d   Z
 RS(	   s>   
    Manages a set of fonts: normal, italic, bold, etc...
    i   c         C   su   | |  _  | |  _ i  |  _ d  |  _ t j j d  rU | sH t |  _  n  |  j	   n | sg t
 |  _  n  |  j   d  S(   Nt   win(   t	   font_namet	   font_sizet   fontst   Nonet   encodingt   syst   platformt
   startswitht   DEFAULT_FONT_NAME_WINt   _create_wint   DEFAULT_FONT_NAME_NIXt   _create_nix(   t   selfR!   R"   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   __init__A   s    				c         C   s   t  j d d | | f d g d t  j d d  } | j   \ } } | j d k r | j   } | r | d j   j d  } | Sn  d  S(   Ns   fc-lists   %s:style=%st   filet   stdoutt   stderri    t   :(   t
   subprocesst   Popent   PIPER$   t   communicatet
   returncodet
   splitlinest   strip(   R-   t   namet   stylet   procR0   t   _t   linest   path(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_nix_font_pathO   s    c         C   s  xf t  d D]G } |  j |  j |  } | d  k	 r t j | |  j  |  j d <Pq q Wt d |  j   x d D] } x t  | D]G } |  j |  j |  } | d  k	 r t j | |  j  |  j | <Pq q W| d k r |  j d |  j | <qp |  j d |  j | <qp Wd  S(   NR   s   No usable fonts named: "%s"R   R   R   (   R   R   R   (	   t   STYLESR@   R!   R$   R   t   truetypeR"   R#   R   (   R-   R:   R?   R;   t	   stylename(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR,   Y   s     c   
      C   s   x d D]k } xb | D]Z } y= d | | o0 d | | f } t  j | |  \ } }	 | SWq t k
 rm q q Xq Wq W| r t d | | d f   n  d  Sd  S(   NR   s    (TrueType)s   %s%s%st    s"   Font %s (%s) not found in registryi    (   R   s    (TrueType)(   t   _winregt   QueryValueExt   EnvironmentErrorR   R$   (
   R-   t   keyt   basenamet   stylest   failt   suffixR;   t   valnamet   valR=   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _lookup_winn   s    c         C   sC  y t  j t  j d  } WnJ t k
 re y t  j t  j d  } Wqf t k
 ra t d   qf Xn Xz |  j | |  j t d t  } t	 j
 | |  j  |  j d <x d D]~ } |  j | |  j t |  } | r t	 j
 | |  j  |  j | <q | d k r|  j d |  j | <q |  j d |  j | <q WWd  t  j |  Xd  S(	   Ns2   Software\Microsoft\Windows NT\CurrentVersion\Fontss/   Software\Microsoft\Windows\CurrentVersion\Fontss$   Can't open Windows font registry keyR   R   R   R   (   s   ITALICs   BOLDs
   BOLDITALIC(   RE   t   OpenKeyt   HKEY_LOCAL_MACHINERG   R   RO   R!   RA   t   TrueR   RB   R"   R#   t   CloseKey(   R-   RH   R?   R;   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR*   }   s,    c         C   s   |  j  d j d  S(   s)   
        Get the character size.
        R   t   M(   R#   t   getsize(   R-   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   get_char_size   s    c         C   sH   | r | r |  j  d S| r( |  j  d S| r9 |  j  d S|  j  d Sd S(   s>   
        Get the font based on bold and italic flags.
        R   R   R   R   N(   R#   (   R-   t   boldt   oblique(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   get_font   s    (   R   R   R   R.   R@   R,   t   FalseRO   R*   RV   RY   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR   <   s   	
			c           B   s   e  Z d  Z d Z d d d g Z d g Z e Z d Z d   Z	 d d  Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s	  
    Create a PNG image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 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.

        .. versionadded:: 1.2

        Default: empty list

    `hl_color`
        Specify the color for highlighting lines.

        .. versionadded:: 1.2

        Default: highlight color of the selected style
    t   imgt   IMGt   pngs   *.pngc         K   s  t  s t d   n  t j |  |  d |  _ t |  j  |  _ |  j j d$ k r^ d |  _ n |  j j |  _ t
 | d d d d d g |  j d	 t |  _ t | d
 d  |  _ t | d d  |  _ t | d d  } t | j d d  |  |  _ |  j j   \ |  _ |  _ | j d d  |  _ | j d d  |  _ t | d d  |  _ t | d t  |  _ t | d t  |  _ t | d d  |  _ t | d t  |  _ t | d t  |  _  t | d d  |  _! t | d d  |  _" |  j r|  j |  j |  j d |  _# n	 d  |  _# g  |  _$ t% | d! g   } x< | D]4 } y |  j$ j& t' |   Wq6t( k
 riq6Xq6W| j d" |  j j)  pd# |  _* g  |  _+ d$ S(%   sE   
        See the class docstring for explanation of options.
        s5   Python Imaging Library is required for this formattert   latin1s   #ffft   image_formatR]   t   jpegt   gift   bmpt   normcaset	   image_padi
   t   line_padi   R"   i   R!   R   t   line_number_fgs   #886t   line_number_bgs   #eedt   line_number_charst   line_number_boldt   line_number_italict   line_number_padi   t   line_numberst   line_number_separatort   line_number_stepi   t   line_number_starti    t   hl_linest   hl_colors   #f90N(,   t   pil_availableR   R    R.   R%   t   dictR;   RJ   t   background_colorR$   R   t   default_image_formatRR   R_   R   Rd   Re   R   t   getR#   RV   t   fontwt   fonthRf   Rg   Rh   R   RZ   Ri   Rj   Rk   Rl   Rm   Rn   Ro   t   line_number_widthRp   R   t   appendt   intt
   ValueErrort   highlight_colorRq   t	   drawables(   R-   t   optionst   fontsizet   hl_lines_strt   line(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR.   !  sZ    					R   c         C   s   t  d   d  S(   NsW   The -S option is meaningless for the image formatter. Use -O style=<stylename> instead.(   t   NotImplementedError(   R-   t   arg(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   get_style_defsY  s    c         C   s   |  j  |  j S(   s+   
        Get the height of a line.
        (   Rx   Re   (   R-   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_line_height]  s    c         C   s   | |  j    |  j S(   s8   
        Get the Y coordinate of a line number.
        (   R   Rd   (   R-   t   lineno(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_line_yc  s    c         C   s   |  j  S(   s/   
        Get the width of a character.
        (   Rw   (   R-   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_char_widthi  s    c         C   s   | |  j  |  j |  j S(   s?   
        Get the X coordinate of a character position.
        (   Rw   Rd   Ry   (   R-   t   charno(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_char_xo  s    c         C   s   |  j  |  |  j |  f S(   sL   
        Get the actual position for a character and line position.
        (   R   R   (   R-   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_text_posu  s    c         C   s   |  j  |  j |  f S(   sI   
        Get the actual position for the start of a line number.
        (   Rd   R   (   R-   R   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_linenumber_pos{  s    c         C   s+   | d d k	 r! d | d } n d } | S(   sE   
        Get the correct color for the token from the style.
        t   colort   #s   #000N(   R$   (   R-   R;   t   fill(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_text_color  s    c         C   s   |  j  j | d | d  S(   s5   
        Get the correct font for the style.
        RW   t   italic(   R#   RY   (   R-   R;   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_style_font  s    c         C   s.   |  j  |  |  j |  j | d  |  j f S(   s.   
        Get the required image size.
        i    (   R   Rd   R   (   R-   t	   maxcharnot	   maxlineno(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _get_image_size  s    c         C   sS   |  j  |  j |  t |  j |  j  d |  j j |  j |  j  d |  j	 d S(   sA   
        Remember a line number drawable to paint later.
        t   fontR   N(
   t
   _draw_textR   t   strt   rjustRh   R#   RY   Ri   Rj   Rf   (   R-   t   posnoR   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _draw_linenumber  s    c         K   s    |  j  j | | | | f  d S(   sB   
        Remember a single drawable tuple to paint later.
        N(   R~   Rz   (   R-   t   post   textR   t   kw(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR     s    c      
   C   s5  d } } } x| D]\ } } x | |  j  k r? | j } q$ W|  j  | } | j d  } | j t  } x t |  D] \ }	 }
 |
 j d  } | r |  j |  j | |  | d |  j	 |  d |  j
 |  | t |  7} t | |  } n  |
 j d  rx d } | d 7} qx qx Wq W| |  _ | |  _ d S(   s9   
        Create drawables for the token content.
        i    i   s   
R   R   i   N(   RJ   t   parentt
   expandtabsR8   RR   t	   enumeratet   rstripR   R   R   R   t   lent   maxt   endswithR   R   (   R-   t   tokensourceR   R   R   t   ttypet   valueR;   R>   t   iR   t   temp(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _create_drawables  s,    	c         C   s^   |  j  s d SxJ t |  j  D]9 } | |  j } | |  j d k r |  j | |  q q Wd S(   s8   
        Create drawables for the line numbers.
        Ni    (   Rl   R   R   Ro   Rn   R   (   R-   t   pt   n(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _draw_line_numbers  s    	c         C   s   |  j  s d S|  j d k r  d St j |  } | j d } |  j |  j |  j } | j	 d | | f g d |  j
 | j | d f | | f g d |  j ~ d S(   s@   
        Paint the line number background on the image.
        Nii    R   (   i    i    (   Rl   Rf   R$   R   t   Drawt   sizeRd   Ry   Rk   t	   rectangleRg   R   (   R-   t   imt   drawt   rectht   rectw(    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   _paint_line_number_bg  s    	
(c         C   sN  |  j  |  |  j   t j d |  j |  j |  j  |  j  } |  j |  t	 j
 |  } |  j r |  j |  j |  j d } |  j   } | j d | } xW |  j D]I } |  j | d  }	 | j | |	 f | | |	 | f g d |  j q Wn  x6 |  j D]+ \ }
 } } } | j |
 | d | | qW| j | |  j j    d S(   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.
        t   RGBi   i    R   R   N(   R   R   R   t   newR   R   R   Rt   R   R   R   Rp   Rd   Ry   Rk   R   R   R   R   Rq   R~   R   t   saveR_   t   upper(   R-   R   t   outfileR   R   t   xR   R   t
   linenumbert   yR   R   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   format  s&    
	&(   R   R   R   R:   t   aliasest	   filenamesRZ   t   unicodeoutputRu   R.   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR	      s.   j		8							
					!		c           B   s,   e  Z d  Z d Z d g Z d g Z d Z RS(   s   
    Create a GIF image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    t   img_gifRa   s   *.gif(   R   R   R   R:   R   R   Ru   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR
     s
   		c           B   s/   e  Z d  Z d Z d d g Z d g Z d Z RS(   s   
    Create a JPEG image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    t   img_jpgt   jpgR`   s   *.jpg(   R   R   R   R:   R   R   Ru   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR     s
   	c           B   s/   e  Z d  Z d Z d d g Z d g Z d Z RS(   s   
    Create a bitmap image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    t   img_bmpRb   t   bitmaps   *.bmp(   R   R   R   R:   R   R   Ru   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyR   $  s
   	(#   R   R&   t   pygments.formatterR    t   pygments.utilR   R   R   R   R   R3   t   PILR   R   R   RR   Rr   t   ImportErrorRZ   RE   t   winregR$   t   __all__RA   R+   R)   R   t	   ExceptionR   t   objectR   R	   R
   R   R   (    (    (    s;   /usr/lib/python2.7/dist-packages/pygments/formatters/img.pyt   <module>
   sB   (

		
p ]