o
    \h[                     @   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-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__ r#   r#   Z/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/pygments/formatters/img.pyr   4       r   c                   @   r   )FontNotFoundz(When there are no usable fonts specifiedNr   r#   r#   r#   r$   r&   8   r%   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stj|r0t	
|| j}d| _tD ]}|| j|< q&d S tjdrA|s;t| _ |   d S tjdrR|sLt| _ |   d S |sWt| _ |   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r<| }|D ]}|dr*q"|  d}|r9|  S q"d S d S )Nzfc-listz:style=file)stdoutstderrr   s   Fontconfig warning::)	
subprocessPopenPIPEcommunicate
returncode
splitlinesr9   decodestrip)	r@   namerB   procrE   _linesliner3   r#   r#   r$   _get_nix_font_path\   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D ]5}t | D ]}| | j|}|d urHt|| j| j|<  nq/|dkrV| jd | j|< q)| jd | j|< q)d S )Nr   No usable fonts named: ""r   r   r   r   r   )r6   rU   r,   r	   r5   r-   r.   r&   )r@   rP   r3   rB   	stylenamer#   r#   r$   r?   j   s&   zFontManager._create_nixc                 C   s   | |d |   S )N )getrO   lower)r@   font_maprP   rB   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 ]}| || j|}|d ur?t	
|| j| jd<  n
q%td| j d	d
D ]6}t| D ]}| || j|}|d urkt	
|| j| j|<  nqQ|dkry| jd | j|< qK| jd | j|< qKd S )NHOMEzLibrary/Fonts/z/Library/Fonts/z/System/Library/Fonts/c                 3   sP    | ]#\}}}|D ]}|  d r	tj|d   tj |fV  q	qdS ))ttfttcr   N)r\   endswithr2   r3   splitextjoin).0rR   filesffont_dirr#   r$   	<genexpr>   s     z*FontManager._create_mac.<locals>.<genexpr>r   rV   rW   rX   r   r   )r2   r3   rd   getenvupdatewalkr6   r^   r,   r	   r5   r-   r.   r&   )r@   r]   rP   r3   rB   rY   r#   rh   r$   r=      s4   
zFontManager._create_macFc           
   
   C   s|   dD ]*}|D ]%}zd ||od| |}t||\}}	|W     S  ty+   Y qw q|r<td| d|d  dd S )N)r   z (TrueType)z{}{}{}rZ   zFont z (r   z) not found in registry)format_winregQueryValueExOSErrorr&   )
r@   keybasenamestylesfailsuffixrB   valnamevalrR   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   TrX   r   r   z$Can't open Windows font registry key)ro   HKEY_CURRENT_USERHKEY_LOCAL_MACHINEOpenKeyry   r,   r6   r	   r5   r-   r.   CloseKeyr&   rq   )r@   lookuperrorkeynameskeynamerr   r3   rB   errr#   r#   r$   r;      sB   
	zFontManager._create_winc                 C   s
   |  dS )z)
        Get the character size.
        M)get_text_sizer@   r#   r#   r$   get_char_size   s   
zFontManager.get_char_sizec                 C   s0   | j d }t|dr||dd S ||S )z4
        Get the text size (width, height).
        r   getbbox      )r.   r1   r   getsize)r@   textrA   r#   r#   r$   r      s   


zFontManager.get_text_sizec                 C   sx   |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 r7| dS | jd S )z>
        Get the font based on bold and italic flags.
        r   r   r   r   )r0   	get_styler.   )r@   boldobliquer#   r#   r$   get_font   s   







zFontManager.get_fontc              	   C   sZ   | j | }t| D ]!}z
|| |W   S  ty   Y q	 ty*   | Y   S w |S )z}
        Get the specified style of the font if it is a variable font.
        If not found, return the normal font.
        )r.   r6   set_variation_by_name
ValueErrorrq   )r@   rB   rA   
style_namer#   r#   r$   r      s   


zFontManager.get_styleN)r(   )F)r   r    r!   r"   rC   rU   r?   r^   r=   ry   r;   r   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.
        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              	   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 qw |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)r   jpeggifbmpT)normcase	image_pad
   line_padr   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   rC   r/   dictrB   rt   background_colorr   default_image_formatr   r   r   r   r'   r[   r.   r   fontwfonthr   r   r   r   r   r   r   r   r   r   r   line_number_widthr   r   appendintr   highlight_colorr   	drawables)r@   optionsfontsizehl_lines_strrT   r#   r#   r$   rC     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)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   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   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 S )z?
        Get the X coordinate of a character position.
        )r   r   )r@   
linelengthr#   r#   r$   _get_char_x     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.
        )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#   )r@   rB   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#   )r@   rB   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   )r@   rB   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   )r@   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rA   text_fgtext_bg)

_draw_textr   strrjustr   r.   r   r   r   r   )r@   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   )r@   posr   rA   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   r   T
r   r   N)rt   parent
expandtabsrM   	enumeraterstripr   r   r   r   r   r.   r   maxlenrb   r   	maxcharnor   )r@   tokensourcer   charnor   r   r   ttypevaluerB   rS   irT   temp
temp_widthrR   r#   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   )r@   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
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   rT   )r@   imdrawrecthrectwr#   r#   r$   _paint_line_number_bgM  s   


z$ImageFormatter._paint_line_number_bgc                 C   sV  |  | |   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 ]C\}
}}}}|rt|drr|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)r   rA   r   N)rA   r   )r   r   r   newr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1   r   r   r   saver   upper)r@   r   outfiler   r   xr   r   
linenumberyr   r   rA   r   r   	text_sizer#   r#   r$   rn   ^  s4   




6zImageFormatter.formatN)r   )r   r    r!   r"   rP   aliases	filenamesunicodeoutputr   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rn   r#   r#   r#   r$   r
     s2    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
    img_gifr   z*.gifNr   r    r!   r"   rP   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
    img_jpgjpgr   z*.jpgNr	  r#   r#   r#   r$   r         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     r  r   ) r"   r2   r7   pygments.formatterr   pygments.utilr   r   r   r   rH   PILr   r   r	   r   ImportErrorro   winreg__all__r6   r>   r:   r<   r   	Exceptionr&   r'   r
   r   r   r   r#   r#   r#   r$   <module>   sR    	 S  {