3
.ßL]EM  ã               @   sr  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 dZW n ek
rx   dZY nX yddlZW n: ek
rÀ   yddlZW n ek
rº   dZY nX Y nX dd	d
dgZddddddgddgdgddgdœZdZdZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d d„ deƒZG d!d	„ d	eƒZG d"d
„ d
eƒZ G d#d„ deƒZ!dS )$zÏ
    pygments.formatters.img
    ~~~~~~~~~~~~~~~~~~~~~~~

    Formatter for Pixmap output.

    :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
é    N)Ú	Formatter)Úget_bool_optÚget_int_optÚget_list_optÚget_choice_optÚxrange)ÚImageÚ	ImageDrawÚ	ImageFontTFÚImageFormatterÚGifImageFormatterÚJpgImageFormatterÚBmpImageFormatterÚ ZRomanZBookZNormalZRegularZMediumZObliqueZItalicZBoldzBold ObliquezBold Italic)ÚNORMALÚITALICÚBOLDÚ
BOLDITALICzBitstream Vera Sans MonozCourier Newc               @   s   e Zd ZdZdS )ÚPilNotAvailablez,When Python imaging library is not availableN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú=/tmp/pip-install-5sebvq2j/pygments/pygments/formatters/img.pyr   6   s   r   c               @   s   e Zd ZdZdS )ÚFontNotFoundz(When there are no usable fonts specifiedN)r   r   r   r   r   r   r   r   r   :   s   r   c               @   s\   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	dd„ Z
dd„ Zdd„ ZdS )ÚFontManagerz>
    Manages a set of fonts: normal, italic, bold, etc...
    é   c             C   sn   || _ || _i | _d | _tjjdƒr8|s.t| _ | jƒ  n2tjjdƒrX|sNt	| _ | j
ƒ  n|sbt| _ | jƒ  d S )NÚwinÚdarwin)Ú	font_nameÚ	font_sizeÚfontsÚencodingÚsysÚplatformÚ
startswithÚDEFAULT_FONT_NAME_WINÚ_create_winÚDEFAULT_FONT_NAME_MACÚ_create_macÚDEFAULT_FONT_NAME_NIXÚ_create_nix)Úselfr    r!   r   r   r   Ú__init__C   s    

zFontManager.__init__c       	      C   sz   t jdd||f dgt jd d�}|jƒ \}}|jdkrv|jƒ }x2|D ]*}|jdƒrTqD|jƒ jƒ jdƒ}|rD|S qDW d S d S )Nzfc-listz%s:style=%sÚfile)ÚstdoutÚstderrr   s   Fontconfig warning:ú:)	Ú
subprocessÚPopenÚPIPEÚcommunicateÚ
returncodeÚ
splitlinesr&   ÚdecodeÚstrip)	r-   ÚnameÚstyleÚprocr0   Ú_ÚlinesÚlineÚpathr   r   r   Ú_get_nix_font_pathU   s    


zFontManager._get_nix_font_pathc             C   sÆ   xJt d D ]0}| j| j|ƒ}|d k	r
tj|| jƒ| jd< P q
W td| j ƒ‚xtdD ]l}xft | D ]0}| j| j|ƒ}|d k	r`tj|| jƒ| j|< P q`W |dkr®| jd | j|< qR| jd | j|< qRW d S )Nr   zNo usable fonts named: "%s"r   r   r   )r   r   r   )ÚSTYLESrB   r    r
   Útruetyper!   r"   r   )r-   r;   rA   r<   Ú	stylenamer   r   r   r,   c   s     

zFontManager._create_nixc             C   s   |j |d | jƒ jƒ ƒS )Nú )Úgetr:   Úlower)r-   Úfont_mapr;   r<   r   r   r   Ú_get_mac_font_pathx   s    zFontManager._get_mac_font_pathc                s  i }x@t jjt jdƒdƒddfD ]"‰ |j‡ fdd„t jˆ ƒD ƒƒ q W xLtd D ]2}| j|| j|ƒ}|d k	rPt	j
|| jƒ| jd< P qPW td| j ƒ‚xvdD ]n}xht| D ]2}| j|| j|ƒ}|d k	r¨t	j
|| jƒ| j|< P q¨W |dkrø| jd
 | j|< qš| jd | j|< qšW d S )NÚHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c             3   s>   | ]6}|j ƒ jd ƒrtjj|ƒd j ƒ tjjˆ |ƒfV  qdS )Zttfr   N)rH   ÚendswithÚosrA   ÚsplitextÚjoin)Ú.0Úf)Úfont_dirr   r   ú	<genexpr>€   s   z*FontManager._create_mac.<locals>.<genexpr>r   zNo usable fonts named: "%s"r   r   r   )r   r   r   )rM   rA   rO   ÚgetenvÚupdateÚlistdirrC   rJ   r    r
   rD   r!   r"   r   )r-   rI   r;   rA   r<   rE   r   )rR   r   r*   {   s,    


zFontManager._create_macFc       
      C   s~   xxdD ]T}xN|D ]F}y*d||o$d| |f }t j||ƒ\}}	|S  tk
rT   wY qX qW qW |rvtd||d f ƒ‚d S d S )Nr   ú (TrueType)z%s%s%srF   z"Font %s (%s) not found in registryr   )r   rW   )Ú_winregÚQueryValueExÚEnvironmentErrorr   )
r-   ÚkeyÚbasenameÚstylesÚfailÚsuffixr<   ZvalnameÚvalr>   r   r   r   Ú_lookup_win—   s    

zFontManager._lookup_winc             C   sþ   yt jt jdƒ}W nD tk
rV   yt jt jdƒ}W n tk
rP   tdƒ‚Y nX Y nX z”| j|| jtd dƒ}tj	|| j
ƒ| jd< xdd	D ]\}| j|| jt| ƒ}|r¼tj	|| j
ƒ| j|< qŠ|dkrÖ| jd | j|< qŠ| jd | j|< qŠW W d t j|ƒ X d S )
Nz2Software\Microsoft\Windows NT\CurrentVersion\Fontsz/Software\Microsoft\Windows\CurrentVersion\Fontsz$Can't open Windows font registry keyr   Tr   r   r   )r   r   r   )rX   ÚOpenKeyÚHKEY_LOCAL_MACHINErZ   r   ra   r    rC   r
   rD   r!   r"   ÚCloseKey)r-   r[   rA   r<   r   r   r   r(   ¦   s,    


zFontManager._create_winc             C   s   | j d jdƒS )z)
        Get the character size.
        r   ÚM)r"   Úgetsize)r-   r   r   r   Úget_char_sizeÁ   s    zFontManager.get_char_sizec             C   s<   |r|r| j d S |r | j d S |r.| j d S | j d S dS )z>
        Get the font based on bold and italic flags.
        r   r   r   r   N)r"   )r-   ÚboldZobliquer   r   r   Úget_fontÇ   s    


zFontManager.get_fontN)r   )F)r   r   r   r   r.   rB   r,   rJ   r*   ra   r(   rg   ri   r   r   r   r   r   >   s   

r   c               @   s¶   e Zd ZdZdZdddgZdgZdZdZdd„ Z	d+d
d„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*S ),r   a£	  
    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
    ÚimgZIMGÚpngz*.pngFc             K   sÚ  t stdƒ‚tj| f|Ž d| _t| jƒ| _| jjdkr@d| _n
| jj| _t	|ddddd	g| j
d
d�| _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dƒ| _t|ddƒ| _t|ddƒ| _t|dd
ƒ| _t|dd
ƒ| _t|d d!ƒ| _t|d"d!ƒ| _| j�rd| j| j | jd  | _ nd#| _ g | _!t"|d$g ƒ}x:|D ]2}y| j!j#t$|ƒƒ W n t%k
�r°   Y nX �q‚W |jd%| jj&ƒ�pÌd&| _'g | _(dS )'zE
        See the class docstring for explanation of options.
        z5Python Imaging Library is required for this formatterÚlatin1Nz#fffÚimage_formatrk   ÚjpegÚgifÚbmpT)ÚnormcaseÚ	image_padé
   Úline_padé   r!   r   r    r   Úline_number_fgz#886Úline_number_bgz#eedÚline_number_charsÚline_number_boldFÚline_number_italicÚline_number_padé   Úline_numbersÚline_number_separatorÚline_number_stepé   Úline_number_startr   Úhl_linesÚhl_colorz#f90))Úpil_availabler   r   r.   r#   Údictr<   r]   Úbackground_colorr   Údefault_image_formatrm   r   rr   rt   r   rG   r"   rg   ÚfontwÚfonthrv   rw   rx   r   ry   rz   r{   r}   r~   r   r�   Úline_number_widthr‚   r   ÚappendÚintÚ
ValueErrorZhighlight_colorrƒ   Ú	drawables)r-   ÚoptionsZfontsizeZhl_lines_strr@   r   r   r   r.   J  sZ    






zImageFormatter.__init__r   c             C   s   t dƒ‚d S )NzWThe -S option is meaningless for the image formatter. Use -O style=<stylename> instead.)ÚNotImplementedError)r-   Úargr   r   r   Úget_style_defs‚  s    zImageFormatter.get_style_defsc             C   s   | j | j S )z+
        Get the height of a line.
        )r‰   rt   )r-   r   r   r   Ú_get_line_height†  s    zImageFormatter._get_line_heightc             C   s   || j ƒ  | j S )z8
        Get the Y coordinate of a line number.
        )r“   rr   )r-   Úlinenor   r   r   Ú_get_line_yŒ  s    zImageFormatter._get_line_yc             C   s   | j S )z/
        Get the width of a character.
        )rˆ   )r-   r   r   r   Ú_get_char_width’  s    zImageFormatter._get_char_widthc             C   s   || j  | j | j S )z?
        Get the X coordinate of a character position.
        )rˆ   rr   rŠ   )r-   Úcharnor   r   r   Ú_get_char_x˜  s    zImageFormatter._get_char_xc             C   s   | j |ƒ| j|ƒfS )zL
        Get the actual position for a character and line position.
        )r˜   r•   )r-   r—   r”   r   r   r   Ú_get_text_posž  s    zImageFormatter._get_text_posc             C   s   | j | j|ƒfS )zI
        Get the actual position for the start of a line number.
        )rr   r•   )r-   r”   r   r   r   Ú_get_linenumber_pos¤  s    z"ImageFormatter._get_linenumber_posc             C   s"   |d dk	rd|d  }nd}|S )zE
        Get the correct color for the token from the style.
        ÚcolorNú#z#000r   )r-   r<   Úfillr   r   r   Ú_get_text_colorª  s    zImageFormatter._get_text_colorc             C   s   | j j|d |d ƒS )z5
        Get the correct font for the style.
        rh   Zitalic)r"   ri   )r-   r<   r   r   r   Ú_get_style_font´  s    zImageFormatter._get_style_fontc             C   s$   | j |ƒ| j | j|d ƒ| j fS )z.
        Get the required image size.
        r   )r˜   rr   r•   )r-   Ú	maxcharnoÚ	maxlinenor   r   r   Ú_get_image_sizeº  s    zImageFormatter._get_image_sizec             C   s8   | j | j|ƒt|ƒj| jƒ| jj| j| jƒ| j	d� dS )zA
        Remember a line number drawable to paint later.
        )Úfontr�   N)
Ú
_draw_textrš   ÚstrÚrjustrx   r"   ri   ry   rz   rv   )r-   Zposnor”   r   r   r   Ú_draw_linenumberÁ  s    
zImageFormatter._draw_linenumberc             K   s   | j j||||fƒ dS )zB
        Remember a single drawable tuple to paint later.
        N)rŽ   r‹   )r-   ÚposÚtextr£   Úkwr   r   r   r¤   Í  s    zImageFormatter._draw_textc             C   sØ   d } }}xº|D ]²\}}x|| j kr.|j}qW | j | }|jdƒ}|jdƒ}xtt|ƒD ]h\}	}
|
jdƒ}|rª| j| j||ƒ|| j|ƒ| j	|ƒd� |t
|ƒ7 }t||ƒ}|
jdƒrXd}|d7 }qXW qW || _|| _dS )z9
        Create drawables for the token content.
        r   é   TÚ
)r£   r�   r€   N)r]   ÚparentÚ
expandtabsr8   Ú	enumerateÚrstripr¤   r™   rŸ   rž   ÚlenÚmaxrL   r    r¡   )r-   Útokensourcer”   r—   r    ZttypeÚvaluer<   r?   Úir@   Útempr   r   r   Ú_create_drawablesÓ  s,    







z ImageFormatter._create_drawablesc             C   sF   | j s
dS x6t| jƒD ](}|| j }|| j dkr| j||ƒ qW dS )z8
        Create drawables for the line numbers.
        Nr   )r}   r   r¡   r�   r   r§   )r-   ÚpÚnr   r   r   Ú_draw_line_numbersô  s    
z!ImageFormatter._draw_line_numbersc             C   sx   | j s
dS | jdkrdS tj|ƒ}|jd }| j| j | j }|jd||fg| j	d� |j
|df||fg| jd� ~dS )z@
        Paint the line number background on the image.
        Nr€   r   )r�   éÿÿÿÿ)r   r   )r}   rv   r	   ÚDrawÚsizerr   rŠ   r{   Ú	rectanglerw   r@   )r-   ÚimÚdrawÚrecthÚrectwr   r   r   Ú_paint_line_number_bgÿ  s    



z$ImageFormatter._paint_line_number_bgc             C   sü   | j |ƒ | jƒ  tjd| j| j| jƒ| jƒ}| j|ƒ t	j
|ƒ}| jr¶| j| j | j d }| jƒ }|jd | }x@| jD ]6}| j|d ƒ}	|j||	f|| |	| fg| jd� q|W x.| jD ]$\}
}}}|j|
|fd|i|—Ž q¾W |j|| jjƒ ƒ dS )a   
        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.
        ZRGBr€   r   )r�   r£   N)r·   rº   r   Únewr¢   r    r¡   r†   rÃ   r	   r¼   r‚   rr   rŠ   r{   r“   r½   r•   r¾   rƒ   rŽ   r©   Úsaverm   Úupper)r-   r³   Úoutfiler¿   rÀ   ÚxrÁ   rÂ   Z
linenumberÚyr¨   r´   r£   rª   r   r   r   Úformat  s&    


zImageFormatter.formatN)r   )r   r   r   r   r;   ÚaliasesÚ	filenamesZunicodeoutputr‡   r.   r’   r“   r•   r–   r˜   r™   rš   rž   rŸ   r¢   r§   r¤   r·   rº   rÃ   rÊ   r   r   r   r   r   Õ   s.   j
8

!c               @   s$   e Zd ZdZdZdgZdgZdZdS )r   z�
    Create a GIF image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    Zimg_gifro   z*.gifN)r   r   r   r   r;   rË   rÌ   r‡   r   r   r   r   r   1  s
   c               @   s&   e Zd ZdZdZddgZdgZdZdS )r   zž
    Create a JPEG image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    Zimg_jpgZjpgrn   z*.jpgN)r   r   r   r   r;   rË   rÌ   r‡   r   r   r   r   r   ?  s
   c               @   s&   e Zd ZdZdZddgZdgZdZdS )r   z 
    Create a bitmap image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    Zimg_bmprp   Zbitmapz*.bmpN)r   r   r   r   r;   rË   rÌ   r‡   r   r   r   r   r   M  s
   )"r   rM   r$   Zpygments.formatterr   Zpygments.utilr   r   r   r   r   r3   ZPILr   r	   r
   r„   ÚImportErrorrX   ÚwinregÚ__all__rC   r+   r'   r)   r   Ú	Exceptionr   Úobjectr   r   r   r   r   r   r   r   r   Ú<module>
   sH   
   ^