
    Mh[                        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
	 ddlmZmZmZ dZn# e$ r dZY nw xY w	 ddlZn# e$ r 	 ddlZn# e$ r dZY nw xY wY nw xY 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e          Z G d de          Z G d d          Z G d de          Z G d de          Z G d de          Z G d d e          ZdS )!z
    pygments.formatters.img
    ~~~~~~~~~~~~~~~~~~~~~~~

    Formatter for Pixmap output.

    :copyright: Copyright 2006-2025 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__     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pygments/formatters/img.pyr   r   4   s        6666r%   r   c                       e Zd ZdZdS )FontNotFoundz(When there are no usable fonts specifiedNr   r$   r%   r&   r(   r(   8   s        2222r%   r(   c                   X    e Zd ZdZddZd Zd Zd Zd Zdd	Z	d
 Z
d Zd Zd Zd ZdS )FontManagerz>
    Manages a set of fonts: normal, italic, bold, etc...
       c                 h   || _         || _        i | _        d | _        d| _        t          |d          st          j                            |          r7t          j
        || j                  }d| _        t          D ]}|| j        |<   d S t          j                            d          r$|st          | _         |                                  d S t          j                            d          r$|st"          | _         |                                  d S |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)selfr0   r1   fontstyles        r&   __init__zFontManager.__init__A   s/   ""
9f%% 		)B)B 	%i@@D DM ) )$(
5!!F<""5)) 	 7!6\$$X.. 	 7!6 7!6r%   c                 |   t          j        d| d| dgt           j        d           }|                                \  }}|j        dk    rp|                                }|D ]W}|                    d          r|                                                                                    d          }|r|c S Xd S d S )Nzfc-listz:style=file)stdoutstderrr   s   Fontconfig warning::)	
subprocessPopenPIPEcommunicate
returncode
splitlinesr=   decodestrip)	rD   namerF   procrJ   _linesliner7   s	            r&   _get_nix_font_pathzFontManager._get_nix_font_path\   s    t,C,CE,C,CV L'1tE E E$$&&	?a%%''E    ??#9:: {{}}**,,22377  KKK 4  r%   c                    t           d         D ]C}|                     | j        |          }|$t          j        || j                  | j        d<    nDt          d| j         d          dD ]}t           |         D ]C}|                     | j        |          }|$t          j        || j                  | j        |<    n2D|dk    r| j        d         | j        |<   o| j        d         | j        |<   d S )Nr   No usable fonts named: ""r   r   r   r   r   )r:   rZ   r0   r
   r9   r1   r2   r(   )rD   rU   r7   rF   	stylenames        r&   rC   zFontManager._create_nixj   s   8$ 	M 	MD**4>4@@D'0'9$'O'O
8$   K$.KKKLLL5 
	= 
	=E#E] 	= 	=	..t~yII#(1(:4(P(PDJu%E $ L(((,
6(:DJu%%(,
8(<DJu%
	= 
	=r%   c                     |                     |dz   |z                                                                             S )N )getrT   lower)rD   font_maprU   rF   s       r&   _get_mac_font_pathzFontManager._get_mac_font_path~   s5    ||TCZ%/6688>>@@AAAr%   c                    i }t           j                            t          j        d          d          ddfD ]5|                    fdt          j                  D                        6t          d         D ]D}|                     || j        |          }|$t          j
        || j                  | j        d<    nEt          d| j         d          d	D ]}t          |         D ]D}|                     || j        |          }|$t          j
        || j                  | j        |<    n2E|d
k    r| j        d         | j        |<   p| j        d         | j        |<   d S )NHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c              3   &  K   | ]\  }}}|D ]}|                                                     d           )t          j                            |          d                                          t          j                            |          fV  dS ))ttfttcr   N)rc   endswithr6   r7   splitextjoin).0rW   filesffont_dirs       r&   	<genexpr>z*FontManager._create_mac.<locals>.<genexpr>   s       7 7Aq%7 7 7799%%n55	7!!!$$Q'--//h1J1JK7 7 7 7 7 7 7r%   r   r\   r]   r^   r   r   )r6   r7   rm   getenvupdatewalkr:   re   r0   r
   r9   r1   r2   r(   )rD   rd   rU   r7   rF   r_   rq   s         @r&   rA   zFontManager._create_mac   s   bi&7&79IJJ*,DF 	7 	7HOO 7 7 7 7#%78#4#47 7 7 7 7 7 7 8$ 	M 	MD**8T^TJJD'0'9$'O'O
8$   K$.KKKLLL5 
	= 
	=E#E] 	= 	=	..xSS#(1(:4(P(PDJu%E $ L(((,
6(:DJu%%(,
8(<DJu%
	= 
	=r%   Fc                     dD ]Q}|D ]L}	 d                     ||od|z   |          }t          j        ||          \  }}	|c c S # t          $ r Y Iw xY wR|rt	          d| d|d          d          d S )N)r   z (TrueType)z{}{}{}ra   zFont z (r   z) not found in registry)format_winregQueryValueExOSErrorr(   )
rD   keybasenamestylesfailsuffixrF   valnamevalrW   s
             r&   _lookup_winzFontManager._lookup_win   s    ) 	 	F  &ooh8K#e)VTTG$1#w??FCJJJJJ   H  ["#Y8#Y#Yvay#Y#Y#YZZZ4s   5A
AAc                 @   d }t           j        dft           j        dft           j        dft           j        dfg}|D ]P}	 t          j        | }	 |                     || j        t          d         d          }t          j        || j	                  | j
        d<   dD ]}|                     || j        t          |                   }|r#t          j        || j	                  | j
        |<   N|dk    r| j
        d         | j
        |<   j| j
        d         | j
        |<   	 t          j        |            d S # t          $ r}|}Y d }~nd }~ww xY w	 t          j        |           n# t          j        |           w xY wA# t          $ r Y Nw xY w|r|t          d          )	Nz2Software\Microsoft\Windows NT\CurrentVersion\Fontsz/Software\Microsoft\Windows\CurrentVersion\Fontsr   Tr^   r   r   z$Can't open Windows font registry key)rx   HKEY_CURRENT_USERHKEY_LOCAL_MACHINEOpenKeyr   r0   r:   r
   r9   r1   r2   CloseKeyr(   rz   )rD   lookuperrorkeynameskeynamer{   r7   rF   errs           r&   r?   zFontManager._create_win   s   /1fg/1cd02gh02deh   	H 	HGow/*++CAQSWXXD+4+=dDN+S+SDJx(!A I I#//T^VE]SS I090B40X0XDJu--$4448Jv4F
5 1 148Jx4H
5 1 1 $S)))))) $ & & &"%KKKKKK&$S))))G$S)))))     "!!FGGGsN   E<CD2E<2
E<E>E!EE!E<!E77E<<
F
	F
c                 ,    |                      d          S )z)
        Get the character size.
        M)get_text_sizerD   s    r&   get_char_sizezFontManager.get_char_size   s     !!#&&&r%   c                     | j         d         }t          |d          r|                    |          dd         S |                    |          S )z4
        Get the text size (width, height).
        r   getbbox      )r2   r5   r   getsize)rD   textrE   s      r&   r   zFontManager.get_text_size   sO     z(#4## 	&<<%%ac**<<%%%r%   c                 Z   |r+|r)| j         r|                     d          S | j        d         S |r)| j         r|                     d          S | j        d         S |r)| j         r|                     d          S | j        d         S | j         r|                     d          S | j        d         S )z>
        Get the font based on bold and italic flags.
        r   r   r   r   )r4   	get_styler2   )rD   boldobliques      r&   get_fontzFontManager.get_font   s      	(G 	(} 4~~l333:l++ 	(} .~~f---:f%% 		(} 0~~h///:h''} 0~~h///:h''r%   c                     | j         |         }t          |         D ]:}	 |                    |           |c S # t          $ r Y (t          $ r |cY c S w xY w|S )z}
        Get the specified style of the font if it is a variable font.
        If not found, return the normal font.
        )r2   r:   set_variation_by_name
ValueErrorrz   )rD   rF   rE   
style_names       r&   r   zFontManager.get_style   s    
 z%  - 	 	J**:666       s   7
AAAN)r+   )F)r    r!   r"   r#   rG   rZ   rC   re   rA   r   r?   r   r   r   r   r$   r%   r&   r*   r*   <   s            6  = = =(B B B= = =:   %H %H %HN' ' '& & &( ( (2    r%   r*   c                       e Zd ZdZdZg dZdgZd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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   IMGpngz*.pngFr   c                 f   t           st          d          t          j        | fi | d| _        t          | j                  | _        | j        j        d| _        n| j        j        | _        t          |dg d| j
        d          | _        t          |d	d
          | _        t          |dd          | _        t          |dd          }t          |                    dd          |          | _        | j                                        \  | _        | _        |                    dd          | _        |                    dd          | _        t          |dd          | _        t1          |dd          | _        t1          |dd          | _        t          |dd          | _        t1          |dd          | _        t1          |dd          | _        t          |dd          | _        t          |dd          | _        | j        r | j        | j        z  | j        dz  z   | _         nd | _         g | _!        tE          |d!g           }|D ]:}	 | j!        #                    tI          |                     +# tJ          $ r Y 7w xY 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_padr   r1   r+   r0   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   rG   r3   dictrF   r}   background_colorr   default_image_formatr   r   r   r   r*   rb   r2   r   fontwfonthr   r   r   r   r   r   r   r   r   r   r   line_number_widthr   r   appendintr   highlight_colorr   	drawables)rD   optionsfontsizehl_lines_strrY   s        r&   rG   zImageFormatter.__init__  s     	I!GI I I4++7+++ 4:&&:&.$*D!!$(J$?D!*^%B%B%B%6 6 6 %Wk2>>#GZ;;wR88 ["!=!=xHH
!%!9!9!;!;
DJ%kk*:FCC%kk*:FCC!,W-@!"E "E ,W-?!H !H".w/CU#L #L*74EqII(.$GG%1'2I4&Q &Q" +G5G K K!,W6I1!M!M 	'&*j43I&I&*&:Q&>'?D"" &'D"#GZ<<  	 	D$$SYY////   J$(J$>@ @ JCI 	s   'I33
J ?J r   c                      t          d          )NzWThe -S option is meaningless for the image formatter. Use -O style=<stylename> instead.)NotImplementedError)rD   args     r&   get_style_defszImageFormatter.get_style_defs  s    ! #Q R R 	Rr%   c                      | j         | j        z   S )z+
        Get the height of a line.
        )r   r   r   s    r&   _get_line_heightzImageFormatter._get_line_height  s     zDM))r%   c                 @    ||                                  z  | j        z   S )z8
        Get the Y coordinate of a line number.
        )r   r   rD   linenos     r&   _get_line_yzImageFormatter._get_line_y  s"     --///$.@@r%   c                     | j         S )z/
        Get the width of a character.
        )r   r   s    r&   _get_char_widthzImageFormatter._get_char_width  s     zr%   c                 &    || j         z   | j        z   S )z?
        Get the X coordinate of a character position.
        )r   r   )rD   
linelengths     r&   _get_char_xzImageFormatter._get_char_x  s     DN*T-CCCr%   c                 V    |                      |          |                     |          fS )zL
        Get the actual position for a character and line position.
        )r   r   )rD   r   r   s      r&   _get_text_poszImageFormatter._get_text_pos  s+     
++T-=-=f-E-EEEr%   c                 :    | j         |                     |          fS )zI
        Get the actual position for the start of a line number.
        )r   r   r   s     r&   _get_linenumber_posz"ImageFormatter._get_linenumber_pos  s      0 0 8 899r%   c                 2    |d         d|d         z   }nd}|S )zE
        Get the correct color for the token from the style.
        colorN#z#000r$   )rD   rF   fills      r&   _get_text_colorzImageFormatter._get_text_color  s(     >%w'DDDr%   c                 2    |d         d|d         z   }nd}|S )zP
        Get the correct background color for the token from the style.
        bgcolorNr   r$   )rD   rF   bg_colors      r&   _get_text_bg_colorz!ImageFormatter._get_text_bg_color  s*     'U9--HHHr%   c                 P    | j                             |d         |d                   S )z5
        Get the correct font for the style.
        r   italic)r2   r   )rD   rF   s     r&   _get_style_fontzImageFormatter._get_style_font  s$     z""5=%/BBBr%   c                 |    |                      |          | j        z   |                     |dz             | j        z   fS )z.
        Get the required image size.
        r   )r   r   r   )rD   maxlinelength	maxlinenos      r&   _get_image_sizezImageFormatter._get_image_size   sD       //$.@  Q//$.@B 	Br%   c                     |                      |                     |          t          |                              | j                  | j                            | j        | j                  | j	        d           dS )zA
        Remember a line number drawable to paint later.
        NrE   text_fgtext_bg)

_draw_textr   strrjustr   r2   r   r   r   r   )rD   posnor   s      r&   _draw_linenumberzImageFormatter._draw_linenumber  s}     	$$U++KKd455$$T%:%)%<> >' 	 	
 	
 	
 	
 	
r%   c                 D    | j                             |||||f           dS )zB
        Remember a single drawable tuple to paint later.
        N)r   r   )rD   posr   rE   r   r   s         r&   r   zImageFormatter._draw_text  s+     	sD$ABBBBBr%   c                    dx}x}}dx}}|D ][\  }}|| j         vr|j        }|| j         v| j         |         }	|                    d          }|                    d          }
t	          |
          D ]\  }}|                    d          }|r|                     |                     ||          ||                     |	          | 	                    |	          | 
                    |	                     | j                            |          \  }}||z  }t          ||          }|t          |          z  }t          ||          }|                    d          r	d}d}|dz  }]|| _        || _        || _        dS )z9
        Create drawables for the token content.
        r   r   T
r   r   N)r}   parent
expandtabsrR   	enumeraterstripr   r   r   r   r   r2   r   maxlenrk   r   	maxcharnor   )rD   tokensourcer   charnor   r   r   ttypevaluerF   rX   irY   temp
temp_widthrW   s                   r&   _create_drawablesz ImageFormatter._create_drawables  s    '('')%&&
' 	  	 LE5t{** t{**K&E $$Q''E$$T**E$U++    4{{4(( 7OO**:v>>#33E::"&"6"6u"="="&"9"9%"@"@ $    %)J$<$<T$B$BMJ*,J$'z$B$BMc$ii'F #Iv 6 6I==&&  !"JFaKF' ( +"r%   c                     | j         sdS t          | j                  D ]0}|| j        z   }|| j        z  dk    r|                     ||           1dS )z8
        Create drawables for the line numbers.
        Nr   )r   ranger   r   r   r   )rD   pns      r&   _draw_line_numbersz!ImageFormatter._draw_line_numbersB  sn       	Ft~&& 	, 	,AD**AD))a//%%a+++	, 	,r%   c                 .   | j         sdS | j        dS t          j        |          }|j        d         }| j        | j        z   | j        z
  }|                    d||fg| j	                   | j
        r"|                    |df||fg| j                   ~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   rY   )rD   imdrawrecthrectws        r&   _paint_line_number_bgz$ImageFormatter._paint_line_number_bgM  s       	F&F~b!!!77$:NN/ / 	 	1 	1 	1% 	NIIqzE5>29LIMMMDDr%   c           	         |                      |           |                                  t          j        d|                     | j        | j                  | j                  }|                     |           t          j
        |          }| j        r| j        | j        z   | j        z
  dz   }|                                 }|j        d         |z
  }| j        D ]B}|                     |dz
            }	|                    ||	f||z   |	|z   fg| j                   C| j        D ]\  }
}}}}|rt+          |d          r|                    ||          }n|                    |          dd         }|                    |
d         |
d         |
d         |d         z   |
d         |d         z   g|           |                    |
|||	           |                    || 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)r   rE   r   N)rE   r   )r  r  r   newr   r   r   r   r  r	   r  r   r   r   r   r   r  r   r  r   r   r5   r  r   r   saver   upper)rD   r  outfiler  r  xr  r  
linenumberyr   r  rE   r   r   	text_sizes                   r&   rw   zImageFormatter.format^  s     	{+++!!!Y  !3T^DD!
 

 	""2&&&~b!!= 	3!77$:NNQRRA))++EGAJNE"m 3 3
$$Z!^44AUAI(>?$(M  3 3 3 326. 	; 	;.Cgw m4,, 8 $5t D DII $U 3 3ABB 7IAAA10Es1vPYZ[P\G\]dklllIIc5t'I::::
*002233333r%   N)r   )r    r!   r"   r#   rU   aliases	filenamesunicodeoutputr   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  rw   r$   r%   r&   r   r     sp       l l^ D###G	IM 6 6 6pR R R R* * *A A A  D D DF F F: : :    C C CB B B
 
 
C C C&  &  & P	, 	, 	,  "#4 #4 #4 #4 #4r%   r   c                   &    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#   rU   r$  r%  r   r$   r%   r&   r   r     s5          DgG	I r%   r   c                   (    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     s7          DfoG	I!r%   r   c                   (    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     s8          DhG	I r%   r   ) r#   r6   r;   pygments.formatterr   pygments.utilr   r   r   r   rM   PILr   r	   r
   r   ImportErrorrx   winreg__all__r:   rB   r>   r@   r   	Exceptionr(   r*   r   r   r   r   r$   r%   r&   <module>r7     s    
			 



 ( ( ( ( ( (               //////////MM   MMMNNNN                 GFFh'(!=1	
 
 + %  7 7 7 7 7k 7 7 73 3 3 3 39 3 3 3O O O O O O O Ods4 s4 s4 s4 s4Y s4 s4 s4r! ! ! ! ! ! ! !" " " " " " " "! ! ! ! ! ! ! ! ! !sA   / 99A AAAAAAAA