a
    T_P                     @   sZ  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	 ddl
Z
zddlmZmZmZ dZW n eyr   dZY n0 zddlZW n6 ey   zddlZW n ey   dZY n0 Y n0 g dZg d	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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-2020 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)Image	ImageDraw	ImageFontTF)ImageFormatterGifImageFormatterJpgImageFormatterBmpImageFormatter) ZRomanZBookZNormalZRegularZMediumZObliqueZItalicZBoldzBold ObliquezBold Italic)NORMALITALICBOLD
BOLDITALICzDejaVu Sans MonozCourier NewZMenloc                   @   s   e Zd ZdZdS )PilNotAvailablez,When Python imaging library is not availableN__name__
__module____qualname____doc__ r   r   9/usr/lib/python3/dist-packages/pygments/formatters/img.pyr   6   s   r   c                   @   s   e Zd ZdZdS )FontNotFoundz(When there are no usable fonts specifiedNr   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dr8|s.t| _ |   n2tjdrX|sNt	| _ | 
  n|sbt| _ |   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dkrv| }|D ].}|drRqB|  d}|rB|  S qBd S d S )Nzfc-listz%s:style=%sfile)stdoutstderrr   s   Fontconfig warning::)	
subprocessPopenPIPEZ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   t d D ]2}| | j|}|d urt|| j| jd<  qJqtd| j dD ]j}t | D ]2}| | j|}|d urZt|| j| j|<  qNqZ|dkr| jd | j|< qN| jd | j|< qNd S )Nr   No usable fonts named: "%s"r   r   r   r   r   )STYLESrA   r    r	   truetyper!   r"   r   )r-   r:   r@   r;   	stylenamer   r   r   r,   c   s"    zFontManager._create_nixc                 C   s   | |d |   S )N )getr9   lower)r-   font_mapr:   r;   r   r   r   _get_mac_font_pathx   s    zFontManager._get_mac_font_pathc                    s  i }t jt ddddfD ]" | fddt  D  qtd D ]4}| || j|}|d urJt	
|| j| jd<  qqJtd| j d	D ]l}t| D ]4}| || j|}|d urt	
|| j| j|<  qq|d
kr| jd | j|< q| jd | j|< qd S )NHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c                 3   s>   | ]6}|  d rtj|d   tj |fV  qdS )Zttfr   N)rI   endswithosr@   splitextjoin).0fZfont_dirr   r   	<genexpr>   s   z*FontManager._create_mac.<locals>.<genexpr>r   rB   rC   r   r   )rN   r@   rP   getenvupdatelistdirrD   rK   r    r	   rE   r!   r"   r   )r-   rJ   r:   r@   r;   rF   r   rS   r   r*   {   s0    
zFontManager._create_macFc           
   
   C   s|   dD ]Z}|D ]P}z4d||o d| |f }t ||\}}	|W     S  tyZ   Y qY q0 qq|rxtd||d f d S )N)r   z (TrueType)z%s%s%srG   z"Font %s (%s) not found in registryr   )_winregZQueryValueExEnvironmentErrorr   )
r-   keybasenamestylesZfailsuffixr;   Zvalnamevalr=   r   r   r   _lookup_win   s    
zFontManager._lookup_winc                 C   sX  d }t jdft jdft jdft jdfg}|D ]}zt j| }zz| || jtd d}t|| j	| j
d< dD ]\}| || jt| }|rt|| j	| j
|< qp|dkr| j
d | j
|< qp| j
d | j
|< qpW W t | W  d S  ty } z|}W Y d }~n
d }~0 0 W t | nt | 0 W q, ty>   Y q,0 q,|rL|tdd S )	Nz2Software\Microsoft\Windows NT\CurrentVersion\Fontsz/Software\Microsoft\Windows\CurrentVersion\Fontsr   TrC   r   r   z$Can't open Windows font registry key)rX   HKEY_CURRENT_USERHKEY_LOCAL_MACHINEOpenKeyr_   r    rD   r	   rE   r!   r"   ZCloseKeyr   rY   )r-   ZlookuperrorZkeynamesZkeynamerZ   r@   r;   errr   r   r   r(      s<    


zFontManager._create_winc                 C   s   | j d 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.   rA   r,   rK   r*   r_   r(   rg   ri   r   r   r   r   r   >   s   

'r   c                   @   s   e Zd ZdZdZg d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: "Courier New" on Windows, "Menlo" on Mac OS, and
                 "DejaVu Sans Mono" 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
    img)rj   ZIMGpngz*.pngFrk   c              	   K   s  t stdtj| fi | d| _t| j| _| jjdu rDd| _n
| jj| _t	|dg d| j
dd| _t|d	d
| _t|dd| _t|dd}t|dd|| _| j \| _| _|dd| _|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 }|D ]0}z| j!#t$| W n t%y   Y n0 q|d"| jj&pd#| _'g | _(dS )$zE
        See the class docstring for explanation of options.
        z5Python Imaging Library is required for this formatterlatin1Nz#fffimage_format)rk   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   rH   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-   ZoptionsZfontsizeZhl_lines_strr?   r   r   r   r.   W  sn    



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   rf   r   r   r   _get_line_height  s    zImageFormatter._get_line_heightc                 C   s   ||    | 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   rf   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   |  || |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 | |fS )zI
        Get the actual position for the start of a line number.
        )rr   r   r   r   r   r   _get_linenumber_pos  s    z"ImageFormatter._get_linenumber_posc                 C   s"   |d durd|d  }nd}|S )zE
        Get the correct color for the token from the style.
        ZcolorN#z#000r   )r-   r;   fillr   r   r   _get_text_color  s    zImageFormatter._get_text_colorc                 C   s   | 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 | |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 | |t|| 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 ||||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 } }}|D ]\}}|| j vr*|j}q| j | }|d}|d}t|D ]h\}	}
|
d}|r| j| |||| || 	|d |t
|7 }t||}|
drPd}|d7 }qPq|| _|| _dS )z9
        Create drawables for the token content.
        r      T
r   r   N)r\   parent
expandtabsr7   	enumeraterstripr   r   r   r   lenmaxrM   r   r   )r-   tokensourcer   r   r   Zttypevaluer;   r>   ir?   Ztempr   r   r   _create_drawables  s.    







z ImageFormatter._create_drawablesc                 C   sB   | j s
dS t| jD ](}|| j }|| j dkr| || qdS )z8
        Create drawables for the line numbers.
        Nr   )r}   ranger   r   r   r   )r-   pnr   r   r   _draw_line_numbers  s    
z!ImageFormatter._draw_line_numbersc                 C   s~   | j s
dS | jdu rdS t|}|jd }| j| j | j }|jd||fg| j	d | j
rx|j|df||fg| jd ~dS )z@
        Paint the line number background on the image.
        N)r   r   r   r   )r}   rv   r   Drawsizerr   r   r{   	rectanglerw   r~   r?   )r-   imdrawrecthrectwr   r   r   _paint_line_number_bg  s    


z$ImageFormatter._paint_line_number_bgc                 C   s   |  | |   td| | j| j| j}| | t	
|}| jr| j| j | j d }|  }|jd | }| jD ]6}| |d }	|j||	f|| |	| fg| jd qz| jD ]$\}
}}}|j|
|fd|i| q||| 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   Zsaverm   upper)r-   r   Z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.   m8

!r
   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   ?  s
   r   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*.jpgNr   r   r   r   r   r   M  s
   r   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*.bmpNr   r   r   r   r   r   [  s
   r   ) r   rN   r$   Zpygments.formatterr   Zpygments.utilr   r   r   r   r3   ZPILr   r   r	   r   ImportErrorrX   winreg__all__rD   r+   r'   r)   r   	Exceptionr   r   r
   r   r   r   r   r   r   r   <module>   sH   

 $  `