
xi\c           @   s  d  Z  d d l 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 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-2019 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__(    (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyR   6   s   t   FontNotFoundc           B   s   e  Z d  Z RS(   s(   When there are no usable fonts specified(   R   R   R   (    (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyR   :   s   t   FontManagerc           B   se   e  Z d  Z d d  Z d   Z 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   s   | |  _  | |  _ i  |  _ d  |  _ t j j d  rU | sH t |  _  n  |  j	   nM t j j d  r | sy t
 |  _  n  |  j   n | s t |  _  n  |  j   d  S(   Nt   wint   darwin(   t	   font_namet	   font_sizet   fontst   Nonet   encodingt   syst   platformt
   startswitht   DEFAULT_FONT_NAME_WINt   _create_wint   DEFAULT_FONT_NAME_MACt   _create_mact   DEFAULT_FONT_NAME_NIXt   _create_nix(   t   selfR"   R#   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   __init__C   s    				c   	      C   s   t  j d d | | f d g d t  j d d  } | j   \ } } | j d k r | j   } xH | D]@ } | j d  r qe n  | j   j	   j	 d  } | re | Sqe Wd  Sd  S(	   Ns   fc-lists   %s:style=%st   filet   stdoutt   stderri    s   Fontconfig warning:t   :(
   t
   subprocesst   Popent   PIPER%   t   communicatet
   returncodet
   splitlinesR)   t   decodet   strip(	   R0   t   namet   stylet   procR3   t   _t   linest   linet   path(    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _get_nix_font_pathU   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   STYLESRE   R"   R%   R   t   truetypeR#   R$   R   (   R0   R>   RD   R?   t	   stylename(    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyR/   c   s     c         C   s!   | j  | d | j   j    S(   Nt    (   t   getR=   t   lower(   R0   t   font_mapR>   R?   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _get_mac_font_pathx   s    c            so  i  } xU t  j j t  j d  d  d d f D],   | j   f d   t  j    D  q. Wxi t d D]J } |  j | |  j |  } | d  k	 ri t
 j | |  j  |  j d <Pqi qi Wt d |  j   x d D] } x t | D]J } |  j | |  j |  } | d  k	 r t
 j | |  j  |  j | <Pq q W| d
 k rS|  j d	 |  j | <q |  j d |  j | <q Wd  S(   Nt   HOMEs   Library/Fonts/s   /Library/Fonts/s   /System/Library/Fonts/c         3   sU   |  ]K } | j    j d   r t j j |  d j    t j j   |  f Vq d S(   t   ttfi    N(   RK   t   endswitht   osRD   t   splitextt   join(   t   .0t   f(   t   font_dir(    s6   lib/python2.7/site-packages/pygments/formatters/img.pys	   <genexpr>   s   R   s   No usable fonts named: "%s"R   R   R   (   R   R   R   (   RQ   RD   RS   t   getenvt   updatet   listdirRF   RM   R"   R%   R   RG   R#   R$   R   (   R0   RL   R>   RD   R?   RH   (    (   RV   s6   lib/python2.7/site-packages/pygments/formatters/img.pyR-   {   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%sRI   s"   Font %s (%s) not found in registryi    (   R   s    (TrueType)(   t   _winregt   QueryValueExt   EnvironmentErrorR   R%   (
   R0   t   keyt   basenamet   stylest   failt   suffixR?   t   valnamet   valRA   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _lookup_win   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   (   R   R   R   (   RZ   t   OpenKeyt   HKEY_LOCAL_MACHINER\   R   Rd   R"   RF   t   TrueR   RG   R#   R$   t   CloseKey(   R0   R]   RD   R?   (    (    s6   lib/python2.7/site-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(   R0   (    (    s6   lib/python2.7/site-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$   (   R0   t   boldt   oblique(    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   get_font   s    (   R   R   R   R1   RE   R/   RM   R-   t   FalseRd   R+   Rk   Rn   (    (    (    s6   lib/python2.7/site-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" on Windows, Courier New 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.

        .. 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_formatRr   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    R1   R&   t   dictR?   R_   t   background_colorR%   R   t   default_image_formatRg   Rt   R   Ry   Rz   R   RJ   R$   Rk   t   fontwt   fonthR{   R|   R}   R   Ro   R~   R   R   R   R   R   R   t   line_number_widthR   R   t   appendt   intt
   ValueErrort   highlight_colorR   t	   drawables(   R0   t   optionst   fontsizet   hl_lines_strRC   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyR1   J  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(   R0   t   arg(    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   get_style_defs  s    c         C   s   |  j  |  j S(   s+   
        Get the height of a line.
        (   R   Rz   (   R0   (    (    s6   lib/python2.7/site-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   Ry   (   R0   t   lineno(    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _get_line_y  s    c         C   s   |  j  S(   s/   
        Get the width of a character.
        (   R   (   R0   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _get_char_width  s    c         C   s   | |  j  |  j |  j S(   s?   
        Get the X coordinate of a character position.
        (   R   Ry   R   (   R0   t   charno(    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _get_char_x  s    c         C   s   |  j  |  |  j |  f S(   sL   
        Get the actual position for a character and line position.
        (   R   R   (   R0   R   R   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   _get_text_pos  s    c         C   s   |  j  |  j |  f S(   sI   
        Get the actual position for the start of a line number.
        (   Ry   R   (   R0   R   (    (    s6   lib/python2.7/site-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%   (   R0   R?   t   fill(    (    s6   lib/python2.7/site-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.
        Rl   t   italic(   R$   Rn   (   R0   R?   (    (    s6   lib/python2.7/site-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   Ry   R   (   R0   t	   maxcharnot	   maxlineno(    (    s6   lib/python2.7/site-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   rjustR}   R$   Rn   R~   R   R{   (   R0   t   posnoR   (    (    s6   lib/python2.7/site-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   R   (   R0   t   post   textR   t   kw(    (    s6   lib/python2.7/site-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(   R_   t   parentt
   expandtabsR;   Rg   t	   enumeratet   rstripR   R   R   R   t   lent   maxRP   R   R   (   R0   t   tokensourceR   R   R   t   ttypet   valueR?   RB   t   iRC   t   temp(    (    s6   lib/python2.7/site-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    (   R   R   R   R   R   R   (   R0   t   pt   n(    (    s6   lib/python2.7/site-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    (   R   R{   R%   R   t   Drawt   sizeRy   R   R   t	   rectangleR|   RC   (   R0   t   imt   drawt   rectht   rectw(    (    s6   lib/python2.7/site-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   R   R   R   R   R   Ry   R   R   R   R   R   R   R   R   R   t   saveRt   t   upper(   R0   R   t   outfileR   R   t   xR   R   t
   linenumbert   yR   R   R   R   (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   format  s&    
	&(   R   R   R   R>   t   aliasest	   filenamesRo   t   unicodeoutputR   R1   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-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_gifRv   s   *.gif(   R   R   R   R>   R   R   R   (    (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyR
   1  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   jpgRu   s   *.jpg(   R   R   R   R>   R   R   R   (    (    (    s6   lib/python2.7/site-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_bmpRw   t   bitmaps   *.bmp(   R   R   R   R>   R   R   R   (    (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyR   M  s
   	(%   R   RQ   R'   t   pygments.formatterR    t   pygments.utilR   R   R   R   R   R6   t   PILR   R   R   Rg   R   t   ImportErrorRo   RZ   t   winregR%   t   __all__RF   R.   R*   R,   R   t	   ExceptionR   t   objectR   R	   R
   R   R   (    (    (    s6   lib/python2.7/site-packages/pygments/formatters/img.pyt   <module>
   sF   (

		
 ]