o
    OZh<                     @   s   d Z ddlZddlZddlZddlmZmZmZmZm	Z	 ddl
mZ ejdkr4ejdkr0ddlZddlZedZi Zi Zdad	d
 Zdd Z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"d#Z"d$d% Z#d*d&d'Z$dS )+z5sysfont, used in the font module to find system fonts    N)basenamedirnameexistsjoinsplitext)Font
emscriptennt)z.ttfz.ttcz.otfFc                 C   s   d dd | D S )z&create simple version of the font name c                 s   s     | ]}|  r| V  qd S )N)isalnumlower).0c r   =/var/www/auris/lib/python3.10/site-packages/pygame/sysfont.py	<genexpr>-   s    z_simplename.<locals>.<genexpr>)r   )namer   r   r   _simplename*   s   r   c                 C   s$   | |vri || < |||  ||f< dS )z0insert a font and style into the font dictionaryNr   )r   bolditalicfontZfontdictr   r   r   _addfont0   s   r   c            
      C   s   t tjddd} i }ddg}tjtjfD ]]}|D ]X}zt||}W n	 ty-   Y qw t	t
|d D ]:}zt||\}}}	W n
 tyN   Y  n$w t|d  tvrZq7t|sct | |}|dD ]}t||| qhq7qq|S )z&initialize fonts dictionary on WindowsZWINDIRz
C:\WindowsZFontsz2SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fontsz/SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts   &)r   osenvironget_winregHKEY_LOCAL_MACHINEHKEY_CURRENT_USEROpenKeyFileNotFoundErrorrangeZQueryInfoKeyZ	EnumValueOSErrorr   r   OpenType_extensionsr   split_parse_font_entry_win)
ZfontdirfontsZmicrosoft_font_dirsdomainZfont_dirkeyir   r   _r   r   r   initsysfonts_win327   s8   
r,   c                 C   s   d}d}|  |r| | } |   } d }}|D ]}|| v r'| | qd| v r3| d d}d| v r>| d d}d| } t| } t| |||| dS )	z
    Parse out a simpler name and the font style from the initial file name.

    :param name: The font name
    :param font: The font file path
    :param fonts: The pygame font dictionary
    z
(TrueType))ZdemiboldZnarrowZlightunicodeZbtmtFr   Tr   r
   N)endswithrstripr   r%   remover   r   r   )r   r   r'   Ztrue_type_suffixmodsr   r   modr   r   r   r&   _   s&   




r&   c                 C   sv   t | } d}|D ]}|| v r| |d} qd }}d| v r%| dd} d}d| v r1| dd} d}t| |||| dS )z
    Parses a font entry for macOS

    :param name: The filepath without extensions or directories
    :param filepath: The full path to the font
    :param fonts: The pygame font dictionary to add the parsed font data to.
    )Zregularr
   Fr   Tr   N)r   replacer   )r   filepathr'   r2   r3   r   r   r   r   r   _parse_font_entry_darwin|   s   	r6   c                     s   g d} t d}|r| d| d d t r(|  fddt  D 7 } i }| D ]$}t|s3q,t |}|D ]}t|\}}|tv rOt|t||| q:q,|S )N)/Library/Fontsz/Network/Library/Fontsz/System/Library/Fontsz"/System/Library/Fonts/SupplementalUSERz/Users/r7   z2/System/Library/Assets/com_apple_MobileAsset_Font3c                    s   g | ]	}  d | qS )/r   )r   locZstrange_rootr   r   
<listcomp>   s    z'_font_finder_darwin.<locals>.<listcomp>)	r   getenvappendr   listdirr   r$   r6   r   )	locationsusernamer'   locationfilesfiler   	extensionr   r;   r   _font_finder_darwin   s&   

rF   c                  C   s@   i } ddg}|D ]}t |rt|}  nqt| dkrt } | S )z"Read the fonts on MacOS, and OS X.z/usr/X11/bin/fc-listz/usr/X11R6/bin/fc-listr   )r   initsysfonts_unixlenrF   )r'   Zfclist_locationsZbin_locationr   r   r   initsysfonts_darwin   s   rI   fc-listc                 C   s  i }t jdkr	|S ztj| ddddgtjtjddd}W nI ty/   td	|  d
 Y |S  tjyB   td|  d Y |S  tj	ye } ztd	|  d|j
 d|j  W Y d}~|S d}~ww |jdd D ]}zt|| W qo ty   Y qow |S )z6use the fc-list from fontconfig to get a list of fontsr   :rD   familystyleTr   )stdoutstderrchecktimeout'z<' is missing, system fonts cannot be loaded on your platformzProcess running 'z;' timed-out! System fonts cannot be loaded on your platformz' failed with error code z@! System fonts cannot be loaded on your platform. Error log is:
Nasciiignore)sysplatform
subprocessrunPIPEr!   warningswarnTimeoutExpiredCalledProcessError
returncoderO   rN   decode
splitlines_parse_font_entry_unix
ValueError)pathr'   proceentryr   r   r   rG      sJ   



rG   c           	      C   s   |  dd\}}}t|d  tv rCd|v }d|v }d|v }|  dD ]}|r, n	q&tt|d }tt|||p=||| d	S d	S )
z
    Parses an entry in the unix font data to add to the pygame font
    dictionary.

    :param entry: A entry from the unix font list.
    :param fonts: The pygame font dictionary to add the parsed font data to.

    rK      r   ZBoldZItalicZOblique,r   N)r%   r   r   r$   stripr   r   r   )	rf   r'   filenamerL   rM   r   r   Zobliquer   r   r   r   ra      s   	ra   c                  C   sL   d} | D ]}|D ]}|t v rt | } nqq|D ]
}|t vr"|t|< qqdS )zkMap common fonts that are absent from the system to similar fonts
    that are installed in the system
    ))Z	monospacez
misc-fixedZcourierZ
couriernewconsolefixedmonoZfreemonoZbitstreamverasansmonoZverasansmonoZmonotypeZlucidaconsoleZconsolasZdejavusansmonoZliberationmono)sansZarialZ	helveticaZswissZfreesansZbitstreamverasansZverasansZverdanaZtahomaZcalibriZgillsansZsegoeuiZtrebuchetmsZubuntuZ
dejavusansZliberationsans)ZseriftimesZ	freeserifZbitstreamveraserifromanZ
timesromanZtimesnewromandutchZ	veraserifgeorgiaZcambriaZ
constantiaZdejavuserifZliberationserif)Z	wingdingsZwingbats)ZcomicsansmsZ	comicsansN)SysfontsSysalias)Zalias_groupsZ	alias_setr   foundr   r   r   create_aliases  s   7rv   c                  C   sJ   t rdS tjdkrt } ntjdkrt } nt } t|  t  da dS )z
    Initialise the sysfont module, called once. Locates the installed fonts
    and creates some aliases for common font categories.

    Has different initialisation functions for different platforms.
    Nwin32darwinT)	is_initrU   rV   r,   rI   rG   rs   updaterv   )r'   r   r   r   initsysfontsV  s   


r{   c                 C   s*   t | |}|r|d |r|d |S )z
    pygame.font specific declarations

    :param fontpath: path to a font.
    :param size: size of a font.
    :param bold: bold style, True or False.
    :param italic: italic style, True or False.

    :return: A font.Font object.
    T)r   set_bold
set_italic)Zfontpathsizer   r   r   r   r   r   font_constructorn  s   


r   c                 C   s.  |du rt }t  d }}d}| rt| ttfr$| t| tr!dnd} | D ]Y}t|tr1| }t|}t	|}	|	sAt
	|}	|	r{|		d}
|		||f}|sn|
snt|	 d \}}|re|d red}|rm|d rmd}n|ss|
}n|
|kr{|}|}|r nq&d }}|r|sd}|r|sd}|||||S )	a  pygame.font.SysFont(name, size, bold=False, italic=False, constructor=None) -> Font
    Create a pygame Font from system font resources.

    This will search the system fonts for the given font
    name. You can also enable bold or italic styles, and
    the appropriate system font will be selected if available.

    This will always return a valid Font object, and will
    fallback on the builtin pygame font if the given font
    is not found.

    Name can also be an iterable of font names, a string of
    comma-separated font names, or a bytes of comma-separated
    font names, in which case the set of names will be searched
    in order. Pygame uses a small set of common font aliases. If the
    specific font you ask for is not available, a reasonable
    alternative may be used.

    If optional constructor is provided, it must be a function with
    signature constructor(fontpath, size, bold, italic) which returns
    a Font instance. If None, a pygame.font.Font object is created.
    NF   ,rh   FFr   Tr   )r   r{   
isinstancestrbytesr%   r_   r   rs   r   rt   listitems)r   r~   r   r   constructorZgotboldZ	gotitalicfontnamesingle_namestylesZ	plainnamerM   r|   r}   r   r   r   SysFont  sN   



r   c                   C   s   t   ttS )a  pygame.font.get_fonts() -> list
    get a list of system font names

    Returns the list of all found system fonts. Note that
    the names of the fonts will be all lowercase with spaces
    removed. This is how pygame internally stores the font
    names for matching.
    )r{   r   rs   r   r   r   r   	get_fonts  s   	r   c                 C   s   t   d}t| ttfr| t| trdnd} | D ]A}t|tr%| }t|}t|}|s5t	|}|rV|sV|||f}|rEd}n|rJd}n
|sTt
| d }|r9|r[ |S q|S )a  pygame.font.match_font(name, bold=0, italic=0) -> name
    find the filename for the named system font

    This performs the same font search as the SysFont()
    function, only it returns the path to the TTF file
    that would be loaded. The font name can also be an
    iterable of font names or a string/bytes of comma-separated
    font names to try.

    If no match is found, None is returned.
    Nr   rh   r   )r{   r   r   r   r%   r_   r   rs   r   rt   r   values)r   r   r   r   r   r   r   r   r   
match_font  s4   


	r   )rJ   )FFNr   )%__doc__r   rU   rZ   Zos.pathr   r   r   r   r   Zpygame.fontr   rV   r   winregr   rW   	frozensetr$   rs   rt   ry   r   r   r,   r&   r6   rF   rI   rG   ra   rv   r{   r   r   r   r   r   r   r   r   <module>   s:   

(
,G
I