a
    ñ»j[  ã                   @   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-present 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   ú=E:\soccer\ml_env\Lib\site-packages\pygments/formatters/img.pyr   4   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   8   s   r   c                   @   sl   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d„ Zdd„ ZdS )ÚFontManagerz>
    Manages a set of fonts: normal, italic, bold, etc...
    é   c                 C   s¶   || _ || _i | _d | _d| _t|dƒs4tj |¡r`t	 
|| j¡}d| _tD ]}|| j|< qLd S tj d¡r€|svt| _ |  ¡  n2tj d¡r |s–t| _ |  ¡  n|sªt| _ |  ¡  d S )NFÚreadTÚwinÚdarwin)Ú	font_nameÚ	font_sizeÚfontsÚencodingÚvariableÚhasattrÚosÚpathÚisfiler	   ÚtruetypeÚSTYLESÚsysÚplatformÚ
startswithÚDEFAULT_FONT_NAME_WINÚ_create_winÚDEFAULT_FONT_NAME_MACÚ_create_macÚDEFAULT_FONT_NAME_NIXÚ_create_nix)Úselfr!   r"   ÚfontÚstyler   r   r   Ú__init__A   s,    

zFontManager.__init__c           	      C   s|   t jd|› d|› �dgt jd d�}| ¡ \}}|jdkrx| ¡ }|D ].}| d¡rTqD| ¡  ¡  d¡}|rD|  S qDd S d S )Nzfc-listz:style=Úfile)ÚstdoutÚstderrr   s   Fontconfig warning:ú:)	Ú
subprocessÚPopenÚPIPEÚcommunicateÚ
returncodeÚ
splitlinesr.   ÚdecodeÚstrip)	r5   Únamer7   Úprocr:   Ú_ÚlinesÚliner(   r   r   r   Ú_get_nix_font_path\   s    ÿ


zFontManager._get_nix_font_pathc                 C   sÂ   t d D ]2}|  | j|¡}|d urt || j¡| jd<  qNqtd| j› d�ƒ‚dD ]j}t | D ]2}|  | j|¡}|d ur^t || j¡| j|<  qRq^|dkr¬| jd | j|< qR| jd | j|< qRd S )Nr   úNo usable fonts named: "ú"©r   r   r   r   r   )r+   rJ   r!   r	   r*   r"   r#   r   )r5   rE   r(   r7   Ú	stylenamer   r   r   r4   j   s    zFontManager._create_nixc                 C   s   |  |d |  ¡  ¡ ¡S )Nú )ÚgetrD   Úlower)r5   Úfont_maprE   r7   r   r   r   Ú_get_mac_font_path~   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<  q’qJtd| j› d	�ƒ‚d
D ]l}t| D ]4}|  || j|¡}|d ur¢t	 
|| j¡| j|<  q–q¢|dkrò| jd | j|< q–| jd | j|< q–d S )NÚHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c                 3   sN   | ]F\}}}|D ]6}|  ¡  d ¡rtj |¡d   ¡ tj ˆ |¡fV  qqdS ))ZttfZttcr   N)rQ   Úendswithr'   r(   ÚsplitextÚjoin)Ú.0rG   ÚfilesÚf©Zfont_dirr   r   Ú	<genexpr>…   s   ýz*FontManager._create_mac.<locals>.<genexpr>r   rK   rL   rM   r   r   )r'   r(   rW   ÚgetenvÚupdateÚwalkr+   rS   r!   r	   r*   r"   r#   r   )r5   rR   rE   r(   r7   rN   r   r[   r   r2   �   s,    ÿþ
zFontManager._create_macFc           
   
   C   s‚   dD ]Z}|D ]P}z4d  ||o"d| |¡}t ||¡\}}	|W     S  tyZ   Y qY q0 qq|r~td|› d|d › d�ƒ‚d S )N)r   z (TrueType)z{}{}{}rO   zFont z (r   z) not found in registry)ÚformatÚ_winregÚQueryValueExÚOSErrorr   )
r5   ÚkeyÚbasenameÚstylesZfailÚsuffixr7   ZvalnameÚvalrG   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 �]}zøt j|Ž }zÜz¢|  || jtd d¡}t || j	¡| j
d< dD ]\}|  || jt| ¡}|r¢t || 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   TrM   r   r   z$Can't open Windows font registry key)ra   ÚHKEY_CURRENT_USERÚHKEY_LOCAL_MACHINEÚOpenKeyri   r!   r+   r	   r*   r"   r#   ÚCloseKeyr   rc   )r5   ZlookuperrorZkeynamesZkeynamerd   r(   r7   Úerrr   r   r   r0   ¬   s<    ý


üzFontManager._create_winc                 C   s
   |   d¡S )z)
        Get the character size.
        ÚM)Úget_text_size©r5   r   r   r   Úget_char_sizeÓ   s    zFontManager.get_char_sizec                 C   s4   | j d }t|dƒr&| |¡dd… S | |¡S dS )z4
        Get the text size (width, height).
        r   Úgetbboxé   é   N)r#   r&   rs   Úgetsize)r5   Útextr6   r   r   r   rp   Ù   s    

zFontManager.get_text_sizec                 C   s|   |r"|r"| j r|  d¡S | jd S |r@| j r6|  d¡S | jd S |r^| j rT|  d¡S | jd S | j rn|  d¡S | jd S dS )z>
        Get the font based on bold and italic flags.
        r   r   r   r   N)r%   Ú	get_styler#   )r5   ÚboldZobliquer   r   r   Úget_fontã   s    






zFontManager.get_fontc              	   C   s\   | j | }t| D ]D}z| |¡ |W   S  ty<   Y q tyT   | Y   S 0 q|S )z}
        Get the specified style of the font if it is a variable font.
        If not found, return the normal font.
        )r#   r+   Zset_variation_by_nameÚ
ValueErrorrc   )r5   r7   r6   Ú
style_namer   r   r   rx   ü   s    


zFontManager.get_styleN)r   )F)r   r   r   r   r8   rJ   r4   rS   r2   ri   r0   rr   rp   rz   rx   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*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.
        If a filename or a file-like object is specified, the user must
        provide different styles of the font.

        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)r}   ZIMGÚpngz*.pngFr~   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&¡�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_format)r~   ÚjpegÚgifÚbmpT)ÚnormcaseÚ	image_padé
   Úline_padrt   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   r8   r$   Údictr7   rf   Úbackground_colorr   Údefault_image_formatr€   r   r…   r‡   r   rP   r#   rr   ÚfontwÚfonthrˆ   r‰   rŠ   r   r‹   rŒ   r�   r�   r�   r‘   r“   Úline_number_widthr”   r   ÚappendÚintr{   Úhighlight_colorr•   Ú	drawables)r5   ÚoptionsZfontsizeZhl_lines_strrI   r   r   r   r8   †  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)r5   Ú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›   r‡   rq   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¥   r…   ©r5   Úlinenor   r   r   Ú_get_line_yÈ  s    zImageFormatter._get_line_yc                 C   s   | j S )z/
        Get the width of a character.
        )rš   rq   r   r   r   Ú_get_char_widthÎ  s    zImageFormatter._get_char_widthc                 C   s   || j  | j S )z?
        Get the X coordinate of a character position.
        )r…   rœ   )r5   Ú
linelengthr   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¨   )r5   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.
        )r…   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.
        ÚcolorNú#z#000r   )r5   r7   Úfillr   r   r   Ú_get_text_coloræ  s    zImageFormatter._get_text_colorc                 C   s"   |d durd|d  }nd}|S )zP
        Get the correct background color for the token from the style.
        ÚbgcolorNr¯   r   )r5   r7   Zbg_colorr   r   r   Ú_get_text_bg_colorð  s    z!ImageFormatter._get_text_bg_colorc                 C   s   | j  |d |d ¡S )z5
        Get the correct font for the style.
        ry   Úitalic)r#   rz   )r5   r7   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«   r…   r¨   )r5   ÚmaxlinelengthÚ	maxlinenor   r   r   Ú_get_image_size   s    ÿzImageFormatter._get_image_sizec                 C   s:   | j |  |¡t|ƒ | j¡| j | j| j¡| j	dd� dS )zA
        Remember a line number drawable to paint later.
        N©r6   Útext_fgÚtext_bg)
Ú
_draw_textr­   ÚstrÚrjustrŠ   r#   rz   r‹   rŒ   rˆ   )r5   Zposnor§   r   r   r   Ú_draw_linenumber  s    
ÿúzImageFormatter._draw_linenumberc                 C   s   | j  |||||f¡ dS )zB
        Remember a single drawable tuple to paint later.
        N)r    r�   )r5   Úposrw   r6   rº   r»   r   r   r   r¼     s    zImageFormatter._draw_textc              
   C   s  d } }}d }}|D ]Ø\}}|| j vr2|j}q | j | }	| d¡}| d¡}
t|
ƒD ]–\}}| d¡}|rÔ| j|  ||¡||  |	¡|  	|	¡|  
|	¡d� | j |¡\}}||7 }t||ƒ}|t|ƒ7 }t||ƒ}| d¡rXd}d}|d7 }qXq|| _|| _|| _dS )z9
        Create drawables for the token content.
        r   ru   TÚ
r¹   r’   N)rf   ÚparentÚ
expandtabsrB   Ú	enumerateÚrstripr¼   r¬   rµ   r±   r³   r#   rp   ÚmaxÚlenrU   r¶   Ú	maxcharnor·   )r5   Útokensourcer§   ZcharnorÈ   r¶   rª   ÚttypeÚvaluer7   rH   ÚirI   ÚtempZ
temp_widthrG   r   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¿   )r5   ÚpÚnr   r   r   Ú_draw_line_numbersB  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�   rˆ   r   ÚDrawÚsizer…   rœ   r�   Ú	rectangler‰   r�   rI   )r5   ÚimÚdrawÚrecthÚrectwr   r   r   Ú_paint_line_number_bgM  s    


ÿz$ImageFormatter._paint_line_number_bgc                 C   sX  |   |¡ |  ¡  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 ]ˆ\}
}}}}|�r.t|dƒræ|j||d�}n| |¡dd… }|j|
d |
d |
d |d  |
d |d  g|d� |j|
|||d	� 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.
        ÚRGBr’   r   rÔ   Útextsize)rw   r6   rt   N)r6   r°   )rÎ   rÒ   r   Únewr¸   r¶   r·   r˜   rÜ   r   rÕ   r”   r…   rœ   r�   r¥   rÖ   r¨   r×   r•   r    r&   rÞ   rs   rw   Úsaver€   Úupper)r5   rÉ   ÚoutfilerØ   rÙ   ÚxrÚ   rÛ   Z
linenumberÚyrÀ   rË   r6   rº   r»   Z	text_sizer   r   r   r`   ^  s4    
ý


ÿ
6zImageFormatter.formatN)r   )r   r   r   r   rE   ÚaliasesÚ	filenamesZunicodeoutputr™   r8   r¤   r¥   r¨   r©   r«   r¬   r­   r±   r³   rµ   r¸   r¿   r¼   rÎ   rÒ   rÜ   r`   r   r   r   r   r
     s0   o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_gifr‚   z*.gifN©r   r   r   r   rE   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jpgr�   z*.jpgNrç   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 bitmap image from source code. This uses the Python Imaging Library to
    generate a pixmap from the source code.

    .. versionadded:: 1.0
    Zimg_bmprƒ   Zbitmapz*.bmpNrç   r   r   r   r   r   £  s
   r   ) r   r'   r,   Zpygments.formatterr   Zpygments.utilr   r   r   r   r=   ZPILr   r   r	   r–   ÚImportErrorra   ÚwinregÚ__all__r+   r3   r/   r1   r   Ú	Exceptionr   r   r
   r   r   r   r   r   r   r   Ú<module>   sH   	
ü S  {