o
    ‡ÎÖa#U  ã                   @   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y9   dZY nw zddlZW n ey[   zddlZW n eyX   dZY nw Y nw 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-2021 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)Ú ÚRomanÚBookÚNormalÚRegularÚMediumÚObliqueÚItalicÚBoldzBold ObliquezBold Italic)ÚNORMALÚITALICÚBOLDÚ
BOLDITALICzDejaVu Sans MonozCourier NewÚMenloc                   @   ó   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   5   ó    r   c                   @   r   )ÚFontNotFoundz(When there are no usable fonts specifiedNr   r#   r#   r#   r$   r&   9   r%   r&   c                   @   sd   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S )ÚFontManagerz>
    Manages a set of fonts: normal, italic, bold, etc...
    é   c                 C   sr   || _ || _i | _d | _tj d¡r|st| _ |  ¡  d S tj d¡r.|s(t	| _ |  
¡  d S |s3t| _ |  ¡  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__B   s   zFontManager.__init__c           	      C   sz   t jdd||f dgt jd d�}| ¡ \}}|jdkr;| ¡ }|D ]}| d¡r)q!| ¡  ¡  d¡}|r8|  S q!d S d S )Nzfc-listz%s:style=%sÚfile)ÚstdoutÚstderrr   s   Fontconfig warning:ú:)	Ú
subprocessÚPopenÚPIPEÚcommunicateÚ
returncodeÚ
splitlinesr1   ÚdecodeÚstrip)	r8   ÚnameÚstyleÚprocr;   Ú_ÚlinesÚlineÚpathr#   r#   r$   Ú_get_nix_font_pathT   s   ÿ

ÿøzFontManager._get_nix_font_pathc                 C   s¾   t d D ]}|  | j|¡}|d urt || j¡| jd<  nqtd| j ƒ‚dD ]5}t | D ]}|  | j|¡}|d urFt || j¡| j|<  nq-|dkrT| jd | j|< q'| jd | j|< q'd S )Nr   úNo usable fonts named: "%s"©r   r   r   r   r   )ÚSTYLESrM   r+   r	   Útruetyper,   r-   r&   )r8   rF   rL   rG   Ú	stylenamer#   r#   r$   r7   b   s*   þÿþ€özFontManager._create_nixc                 C   s   |  |d |  ¡  ¡ ¡S )Nú )ÚgetrE   Úlower)r8   Úfont_maprF   rG   r#   r#   r$   Ú_get_mac_font_pathw   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 ]}|  || j|¡}|d ur?t	 
|| j¡| jd<  nq%td| j ƒ‚d	D ]6}t| D ]}|  || j|¡}|d urit	 
|| j¡| j|<  nqO|d
krw| jd | j|< qI| jd | j|< qId S )NÚHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c                 3   s@   � | ]}|  ¡  d ¡rtj |¡d   ¡ tj ˆ |¡fV  qdS ))ÚttfÚttcr   N)rU   ÚendswithÚosrL   ÚsplitextÚjoin)Ú.0Úf©Úfont_dirr#   r$   Ú	<genexpr>~   s   € ý 
ÿz*FontManager._create_mac.<locals>.<genexpr>r   rN   rO   r   r   )r\   rL   r^   ÚgetenvÚupdateÚlistdirrP   rW   r+   r	   rQ   r,   r-   r&   )r8   rV   rF   rL   rG   rR   r#   ra   r$   r5   z   s8   ÿ
þþÿþ€özFontManager._create_macFc           
   
   C   sv   dD ]*}|D ]%}zd||od| |f }t  ||¡\}}	|W     S  ty+   Y qw q|r9td||d f ƒ‚d S )N)r   z (TrueType)z%s%s%srS   z"Font %s (%s) not found in registryr   )Ú_winregÚQueryValueExÚOSErrorr&   )
r8   ÚkeyÚbasenameÚstylesÚfailÚsuffixrG   ÚvalnameÚvalrI   r#   r#   r$   Ú_lookup_win—   s   ÿû
ÿzFontManager._lookup_winc                 C   sJ  d }t jdft jdft jdft jdfg}|D ]†}z{t j|Ž }zmzQ|  || jtd d¡}t || j	¡| j
d< dD ].}|  || jt| ¡}|rPt || j	¡| j
|< q7|dkr]| j
d | j
|< q7| j
d | j
|< q7W W t  |¡ W  d S  ty„ } z|}W Y d }~nd }~ww W t  |¡ nt  |¡ w W q tyœ   Y qw |r¡|‚tdƒ‚)	Nz2Software\Microsoft\Windows NT\CurrentVersion\Fontsz/Software\Microsoft\Windows\CurrentVersion\Fontsr   TrO   r   r   z$Can't open Windows font registry key)rg   ÚHKEY_CURRENT_USERÚHKEY_LOCAL_MACHINEÚOpenKeyrq   r+   rP   r	   rQ   r,   r-   ÚCloseKeyr&   ri   )r8   ÚlookuperrorÚkeynamesÚkeynamerj   rL   rG   Úerrr#   r#   r$   r3   ¦   sB   ý
ý€ÿÿ	zFontManager._create_winc                 C   s   | j d  d¡S )z)
        Get the character size.
        r   ÚM©r-   Úgetsize©r8   r#   r#   r$   Úget_char_sizeÍ   ó   zFontManager.get_char_sizec                 C   s   | j d  |¡S )z3
        Get the text size(width, height).
        r   r{   )r8   Útextr#   r#   r$   Úget_text_sizeÓ   r   zFontManager.get_text_sizec                 C   s8   |r	|r	| j d S |r| j d S |r| j d S | j d S )z>
        Get the font based on bold and italic flags.
        r   r   r   r   )r-   )r8   ÚboldÚobliquer#   r#   r$   Úget_fontÙ   s   



zFontManager.get_fontN)r(   )F)r   r    r!   r"   r9   rM   r7   rW   r5   rq   r3   r~   r�   r„   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.

        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…   ÚIMGÚpngz*.pngFr‡   c              	   K   sÊ  t stdƒ‚tj| fi |¤Ž d| _t| jƒ| _| jjdu r"d| _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r±| j| j | jd  | _ nd | _ g | _!t"|d!g ƒ}|D ]}z
| j! #t$|ƒ¡ W q¿ t%yÔ   Y q¿w | 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_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   r9   r.   ÚdictrG   rl   Úbackground_colorr   Údefault_image_formatr‰   r   rŽ   r�   r'   rT   r-   r~   ÚfontwÚfonthr’   r“   r”   r   r•   r–   r—   r™   rš   r›   r�   Úline_number_widthrž   r   ÚappendÚintÚ
ValueErrorÚhighlight_colorrŸ   Ú	drawables)r8   ÚoptionsÚfontsizeÚhl_lines_strrK   r#   r#   r$   r9   ]  sp   ÿ

þÿÿÿÿ
ÿÿÿÿ
zImageFormatter.__init__r   c                 C   s   t dƒ‚)NzWThe -S option is meaningless for the image formatter. Use -O style=<stylename> instead.)ÚNotImplementedError)r8   Ú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�   r}   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Ž   ©r8   Ú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 S )z?
        Get the X coordinate of a character position.
        )rŽ   r¦   )r8   Ú
linelengthr#   r#   r$   Ú_get_char_x«  r   zImageFormatter._get_char_xc                 C   s   |   |¡|  |¡fS )zL
        Get the actual position for a character and line position.
        )r¸   rµ   )r8   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·  r   z"ImageFormatter._get_linenumber_posc                 C   s$   |d durd|d  }|S d}|S )zE
        Get the correct color for the token from the style.
        ÚcolorNú#z#000r#   )r8   rG   Úfillr#   r#   r$   Ú_get_text_color½  ó
   ÿzImageFormatter._get_text_colorc                 C   s$   |d durd|d  }|S d}|S )zP
        Get the correct background color for the token from the style.
        ÚbgcolorNr¼   r#   )r8   rG   Úbg_colorr#   r#   r$   Ú_get_text_bg_colorÇ  r¿   z!ImageFormatter._get_text_bg_colorc                 C   s   | j  |d |d ¡S )z5
        Get the correct font for the style.
        r‚   Úitalic)r-   r„   )r8   rG   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µ   )r8   Ú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©ÚfontÚtext_fgÚtext_bg)
Ú
_draw_textrº   ÚstrÚrjustr”   r-   r„   r•   r–   r’   )r8   Ú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§   )r8   Úposr€   rÉ   rÊ   rË   r#   r#   r$   rÌ   ë  s   zImageFormatter._draw_textc              
   C   s  d } }}d }}|D ]p\}}|| j vr|j}|| j vs| j | }	| d¡}| d¡}
t|
ƒD ]K\}}| d¡}|rn| j|  ||¡||  |	¡|  	|	¡|  
|	¡d� | j |¡\}}||7 }t||ƒ}|t|ƒ7 }t||ƒ}| d¡r{d}d}|d7 }q0q|| _|| _|| _dS )z9
        Create drawables for the token content.
        r   é   TÚ
rÈ   rœ   N)rl   ÚparentÚ
expandtabsrC   Ú	enumerateÚrstriprÌ   r¹   rÄ   r¾   rÂ   r-   r�   ÚmaxÚlenr[   rÅ   Ú	maxcharnorÆ   )r8   Útokensourcer´   ÚcharnorÚ   rÅ   r·   ÚttypeÚvaluerG   rJ   ÚirK   ÚtempÚ
temp_widthÚ
temp_hightr#   r#   r$   Ú_create_drawablesñ  sB   

ÿ




û


€í
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Ð   )r8   Ú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
r<|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š   rK   )r8   ÚimÚdrawÚrecthÚrectwr#   r#   r$   Ú_paint_line_number_bg$  s   


ÿz$ImageFormatter._paint_line_number_bgc                 C   s8  |   |¡ |  ¡  t d|  | j| j¡| j¡}|  |¡ t	 
|¡}| jrY| j| j | j d }|  ¡ }|jd | }| jD ]}|  |d ¡}	|j||	f|| |	| fg| jd� q=| jD ]4\}
}}}}|r‡|j||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é   )r€   rÉ   )rÉ   r½   N)rã   rç   r   ÚnewrÇ   rÅ   rÆ   r¢   rñ   r   rê   rž   rŽ   r¦   r—   r²   rë   rµ   rì   rŸ   r«   Útextsizer€   Úsaver‰   Úupper)r8   rÛ   Úoutfilerí   rî   Úxrï   rð   Ú
linenumberÚyrÑ   rÞ   rÉ   rÊ   rË   Ú	text_sizer#   r#   r$   Úformat5  s0   
ý


ÿ6zImageFormatter.formatN)r   )r   r    r!   r"   rF   ÚaliasesÚ	filenamesÚunicodeoutputr£   r9   r±   r²   rµ   r¶   r¸   r¹   rº   r¾   rÂ   rÄ   rÇ   rÐ   rÌ   rã   rç   rñ   rü   r#   r#   r#   r$   r
   ç   s2    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
    Úimg_gifr‹   z*.gifN©r   r    r!   r"   rF   rý   rþ   r£   r#   r#   r#   r$   r   Z  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
    Úimg_jpgÚjpgrŠ   z*.jpgNr  r#   r#   r#   r$   r   h  ó    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
    Úimg_bmprŒ   Úbitmapz*.bmpNr  r#   r#   r#   r$   r   v  r  r   ) r"   r\   r/   Úpygments.formatterr   Úpygments.utilr   r   r   r   r>   ÚPILr   r   r	   r    ÚImportErrorrg   ÚwinregÚ__all__rP   r6   r2   r4   r   Ú	Exceptionr&   r'   r
   r   r   r   r#   r#   r#   r$   Ú<module>   sR    
ÿÿ€ýü +  u