a
    Sic!                     @   s  d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	Z	ddl
Z
ddlmZ ddlZddlmZ ddlZddlZddlZddlZddlZddlmZmZmZmZ dd	lmZmZ dd
lmZ e
e Z!ddddddddddd
Z"ddddddddddddddZ#dddddddddddddddZ$g dZ%h dZ&ze' Z(W n e)yd   eej*Z(Y n0 d Z+d!d"gZ,e-e(d# e-e(d$ gZ.d%d&d'd(d)e-eej/0d*pe(d+ d, e-e(d- gZ1d.d/d0d1e-e(d2 gZ2d3d4 Z3d5d6 Z4d7d8 Z5d9d: Z6e7d;dd=d>Z8d?d@ Z9e dAdB Z:e7d;ddCdDZ;ddEdFZ<dGdH Z=dIdJ Z>ej?dKdLe-ej@dMdNfdOe-ej@dMdNfdPe-ej@dQdNfdRe-ej@dQdNfdSe-ej@dQdNfdTe-ej@dQdNfdUe-ej@dVdNfgdWdXdY dZdY d[d\ZAd]d^ ZBd_d` ZCG dadb dbZDG dcdd dde	jEZFdedf ZGdgdh ZHdidj ZIG dkdl dlZJe dmdn ZKedodpdq ZLeMedrrnejNeLjOds edodtdu ZPeQdvdwdxddydzZRd{d|d}d~ZSeS ZTeTjUZUeTjVZVdS )a  
A module for finding, managing, and using fonts across platforms.

This module provides a single `FontManager` instance, ``fontManager``, that can
be shared across backends and platforms.  The `findfont`
function returns the best TrueType (TTF) font file in the local or
system font path that matches the specified `FontProperties`
instance.  The `FontManager` also handles Adobe Font Metrics
(AFM) font files for use by the PostScript backend.

The design is based on the `W3C Cascading Style Sheet, Level 1 (CSS1)
font specification <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_.
Future versions may implement the Level 2 or 2.1 specifications.
    )	b64encodeN)	lru_cache)BytesIO)Number)Path)_api_afmcbookft2font)parse_fontconfig_patterngenerate_fontconfig_pattern)_validatorsgI+?gh|?5?g-?      ?g333333?g
ףp=
?gS?)
zxx-smallzx-smallsmallmediumlargezx-largezxx-largelargersmallerNd      ,      X         )zultra-condensedzextra-condensed	condensedsemi-condensednormalzsemi-expandedzsemi-extendedexpandedextendedzextra-expandedzextra-extendedzultra-expandedzultra-extended)
ultralightlightr   regularbookr   romansemibolddemibolddemiboldheavyz
extra boldblack))thinr   )
extralightr   )r"   r   )Z	demilight^  )Z	semilightr/   )r#   r   )r%   i|  )r$   r   )r   r   )r   r   )r(   r   )r)   r   )r'   r   )	extraboldr   )Z	superboldr   )Z	ultraboldr   )r*   r   )Z
ultrablack  )Z
superblackr1   )Z
extrablackr1   )z\bultrar1   )r,   r   )r+   r   >   
sans serifsans
sans-serifZ	monospaceZcursiveZfantasyZserifz@Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Foldersz2SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fontsz/SOFTWARE\Microsoft\Windows\CurrentVersion\Fontsz%AppData/Local/Microsoft/Windows/Fontsz'AppData/Roaming/Microsoft/Windows/Fontsz/usr/X11R6/lib/X11/fonts/TTF/z/usr/X11/lib/X11/fontsz/usr/share/fonts/z/usr/local/share/fonts/z)/usr/lib/openoffice/share/fonts/truetype/XDG_DATA_HOMEz.local/sharefontsz.fontsz/Library/Fonts/z/Network/Library/Fonts/z/System/Library/Fonts/z/opt/local/share/fontszLibrary/Fontsc                 C   s    dgg dg dg dd|  S )zh
    Return a list of file extensions that are synonyms for
    the given file extension *fileext*.
    afm)otfttcttf)r7   r8   r9   r:    fontextr;   r;   S/var/www/html/django/DPS/env/lib/python3.9/site-packages/matplotlib/font_manager.pyget_fontext_synonyms   s    r?   c                    sV   dd D t jdkr: t kr: fddt D S fddt D S dS )zo
    Return a list of all fonts matching any of the extensions, found
    recursively under the directory.
    c                 S   s   g | ]}d | qS .r;   .0extr;   r;   r>   
<listcomp>       zlist_fonts.<locals>.<listcomp>win32c                    s&   g | ]}t j|rt j |qS r;   )ospathisfilejoin)rC   filename)	directoryr;   r>   rE      s   c                    s<   g | ]4\}}}|D ]$}t |j  v rtj||qqS r;   )r   suffixlowerrH   rI   rK   )rC   dirpath_	filenamesrL   )
extensionsr;   r>   rE      s   N)sysplatformwin32FontDirectoryrH   listdirwalkrM   rS   r;   rY   r>   
list_fonts   s    

rZ   c                  C   sx   ddl } zD| | jt"}| |dd W  d   W S 1 s@0    Y  W n& tyr   tjtj	d d Y S 0 dS )a  
    Return the user-specified font directory for Win32.  This is
    looked up from the registry key ::

      \\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts

    If the key is not found, ``%WINDIR%\Fonts`` will be returned.
    r   NZFontsZWINDIR)
winregOpenKeyHKEY_CURRENT_USER	MSFoldersQueryValueExOSErrorrH   rI   rK   environ)r[   userr;   r;   r>   rV      s    	4rV   c                 C   s   ddl }t }tD ]}z|| |z}t||d D ]V}|||\}}}	t|tsZq8zt	||
 }
W n ty   Y q8Y n0 ||
 q8W d   n1 s0    Y  W q ttfy   Y qY q0 q|S )a  
    Search for fonts in the Windows registry.

    Parameters
    ----------
    reg_domain : int
        The top level registry domain (e.g. HKEY_LOCAL_MACHINE).

    base_dir : str
        The path to the folder where the font files are usually located (e.g.
        C:\Windows\Fonts). If only the filename of the font is stored in the
        registry, the absolute path is built relative to this base directory.

    Returns
    -------
    `set`
        `pathlib.Path` objects with the absolute path to the font files found.

    r   N   )r[   setMSFontDirectoriesr\   rangeQueryInfoKey	EnumValue
isinstancestrr   resolveRuntimeErroraddr`   MemoryError)Z
reg_domainbase_dirr[   itemsreg_pathlocaljkeyvaluetprI   r;   r;   r>   _win32RegistryFonts   s"    

.rw   z3.5r:   c                    sn   ddl }| du rt } dd t D  t }|t|j|  tD ]}|t|j| qD fdd|D S )a
  
    Search for fonts in the specified font directory, or use the
    system directories if none given. Additionally, it is searched for user
    fonts installed. A list of TrueType font filenames are returned by default,
    or AFM fonts if *fontext* == 'afm'.
    r   Nc                 S   s   g | ]}d | qS r@   r;   rB   r;   r;   r>   rE     rF   z'win32InstalledFonts.<locals>.<listcomp>c                    s"   g | ]}|j   v rt|qS r;   rN   rO   rj   rC   rI   r<   r;   r>   rE   #  rF   )	r[   rV   r?   rd   updaterw   HKEY_LOCAL_MACHINEMSUserFontDirectoriesr]   )rM   r=   r[   rp   Zuserdirr;   r<   r>   win32InstalledFonts
  s    r}   c                  C   s   ddl } t }| jt gf| jtffD ]\}}|D ]}tD ]}z| ||z}t| 	|d D ]V}| 
||\}}	}
t|	tsq`zt||	 }W n ty   Y q`Y n0 || q`W d   n1 s0    Y  W q: ttfy   Y q:Y q:0 q:q2q&|S )z2List the font paths known to the Windows registry.r   Nrc   )r[   rd   r{   rV   r]   r|   re   r\   rf   rg   rh   ri   rj   r   rk   rl   rm   r`   rn   )r[   rp   domainZ	base_dirsro   rq   rr   rs   rt   ru   rv   rI   r;   r;   r>   _get_win32_installed_fonts&  s*    

.r   c               	   C   sf   z4dt ddgvr$td g W S t ddg} W n tt jfyP   g  Y S 0 dd | dD S )	z3Cache and list the font paths known to ``fc-list``.s   --formatzfc-listz--helpz7Matplotlib needs fontconfig>=2.7 to query system fonts.z--format=%{file}\nc                 S   s   g | ]}t t|qS r;   )r   rH   fsdecoderC   fnamer;   r;   r>   rE   Q  rF   z)_get_fontconfig_fonts.<locals>.<listcomp>   
)
subprocesscheck_output_logwarningr`   CalledProcessErrorsplit)outr;   r;   r>   _get_fontconfig_fontsF  s    
r   c                    s&   dd t  D   fddt D S )zDList font filenames known to ``fc-list`` having the given extension.c                 S   s   g | ]}d | qS r@   r;   rB   r;   r;   r>   rE   W  rF   z(get_fontconfig_fonts.<locals>.<listcomp>c                    s"   g | ]}|j   v rt|qS r;   rx   ry   r<   r;   r>   rE   X  s   )r?   r   r<   r;   r<   r>   get_fontconfig_fontsT  s    r   c                    s   t  }t| | du rptjdkr4t }tt g } n"t }tjdkrRg tt	} nt} |
 fdd|D  nt| tr| g} | D ]}|
ttjjt|  qdd |D S )a4  
    Search for fonts in the specified font paths.  If no paths are
    given, will use a standard set of system paths, as well as the
    list of fonts tracked by fontconfig if fontconfig is installed and
    available.  A list of TrueType fonts are returned by default with
    AFM fonts as an option.
    NrG   darwinc                 3   s,   | ]$}|j  d d  v rt|V  qdS )rc   Nrx   ry   Zfontextsr;   r>   	<genexpr>q  s   z"findSystemFonts.<locals>.<genexpr>c                 S   s   g | ]}t j|r|qS r;   )rH   rI   existsr   r;   r;   r>   rE   z  rF   z#findSystemFonts.<locals>.<listcomp>)rd   r?   rT   rU   r   r|   rV   r   X11FontDirectoriesOSXFontDirectoriesrz   ri   rj   maprH   rI   abspathrZ   )Z	fontpathsr=   Z	fontfilesZinstalled_fontsrI   r;   r   r>   findSystemFonts\  s     


r   c                 C   s   ddl m} | }| jdkr&t| jnd }|jdd| j|d t (}|j|ddd | W  d    S 1 sr0    Y  d S )Nr   )Figure )fonttightT)bbox_inchestransparent)	matplotlib.figurer   r   r   textnamer   savefiggetvalue)fontentr   fig	font_pathbufr;   r;   r>   _fontentry_helper_repr_png}  s    r   c                 C   s    t | }t| }d| dS )Nz!<img src="data:image/png;base64, z" />)r   r   decode)r   Z
png_streamZpng_b64r;   r;   r>   _fontentry_helper_repr_html  s    r   	FontEntryr   r   )defaultr   styler   variantweightstretchsizer   zj
    A class for storing Font properties.

    It is used when populating the font lookup dictionary.
    c                 C   s   t | S N)r   selfr;   r;   r>   <lambda>  rF   r   c                 C   s   t | S r   )r   r   r;   r;   r>   r     rF   )__doc___repr_html_
_repr_png_)	namespacec              	      sT   j }  }d}d}|g |dR dd pT|g |dR dd }|g |dR dd p|g |dR dd dd	krd}n>d
d	krd
}n*|dd	krd}n jtj@ rd
}nd}| dv rd}nd}d}d}	d}
|g ||R dd|g ||	R dd|g ||
R dd|g ||R dd|g ||	R dd|g ||
R ddgg t	dp j
g fdd}t| }tfdddD rd}n2dv rd}n"tfdddD r&d}nd} js:tdd}t j||||||S ) a  
    Extract information from a TrueType font file.

    Parameters
    ----------
    font : `.FT2Font`
        The TrueType font file from which information will be extracted.

    Returns
    -------
    `FontEntry`
        The extracted font properties.

    )rc   r   r   )   rc   i	     rF   zlatin-1	utf_16_be   obliquer   italicr$   r   capitals
small-capsr         z	utf-16-beNc                     s     d} | r"| d dkr"| d S z  d ddp:d}W n tyP   Y n(0 tD ] \}}t||tjrV|  S qVD ]:}|dd}tD ]$\}}t||tjr|    S qq| j	t
j@ rdS d	S )
NzOS/2versioni  ZusWeightClassr    r   r   r   )get_sfnt_tableget_ps_font_inforeplace
ValueError_weight_regexesre	fullmatchIsearchstyle_flagsr
   BOLD)Zos2Zps_font_info_weightregexr   r   )r   stylesr;   r>   
get_weight  s&    

z#ttfFontProperty.<locals>.get_weightc                 3   s   | ]}| v V  qd S r   r;   rC   wordsfnt4r;   r>   r     rF   z"ttfFontProperty.<locals>.<genexpr>)narrowr   condr   	demi condr   c                 3   s   | ]}| v V  qd S r   r;   r   r   r;   r>   r     rF   wider    r!   r    z$Non-scalable fonts are not supportedscalable)family_nameget_sfntgetr   rO   findr   r
   ITALICfilter
style_nameintanyr   NotImplementedErrorr   r   )r   r   ZsfntZmac_keyZms_keyZsfnt2r   r   Zwws_subfamilyZtypographic_subfamilyZfont_subfamilyr   r   r   r   r;   )r   r   r   r>   ttfFontProperty  s\      
	
	r   c                    s   |  }|   | dks,d| v r2d}nd| v rDd}nd}| dv rZd}nd}|  }|tvrvd}d v rd}n<t fd	d
dD rd}n t fdd
dD rd}nd}d}t| ||||||S )z
    Extract information from an AFM font file.

    Parameters
    ----------
    font : AFM
        The AFM font file from which information will be extracted.

    Returns
    -------
    `FontEntry`
        The extracted font properties.
    r   r   r   r   r   r   r   r   c                 3   s   | ]}| v V  qd S r   r;   r   fontnamer;   r>   r   Q  rF   z"afmFontProperty.<locals>.<genexpr>)r   r   r   c                 3   s   | ]}| v V  qd S r   r;   r   r   r;   r>   r   S  rF   r   r    r   )Zget_familynameZget_fontnamerO   Z	get_angler   weight_dictr   r   )Zfontpathr   r   r   r   r   r   r   r;   r   r>   afmFontProperty"  s,    r   c                   @   s   e Zd ZdZd5ddZe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'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 ZeZeZeZ eZ!dS )6FontPropertiesa  
    A class for storing and manipulating font properties.

    The font properties are the six properties described in the
    `W3C Cascading Style Sheet, Level 1
    <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ font
    specification and *math_fontfamily* for math fonts:

    - family: A list of font names in decreasing order of priority.
      The items may include a generic font family name, either 'sans-serif',
      'serif', 'cursive', 'fantasy', or 'monospace'.  In that case, the actual
      font to be used will be looked up from the associated rcParam during the
      search process in `.findfont`. Default: :rc:`font.family`

    - style: Either 'normal', 'italic' or 'oblique'.
      Default: :rc:`font.style`

    - variant: Either 'normal' or 'small-caps'.
      Default: :rc:`font.variant`

    - stretch: A numeric value in the range 0-1000 or one of
      'ultra-condensed', 'extra-condensed', 'condensed',
      'semi-condensed', 'normal', 'semi-expanded', 'expanded',
      'extra-expanded' or 'ultra-expanded'. Default: :rc:`font.stretch`

    - weight: A numeric value in the range 0-1000 or one of
      'ultralight', 'light', 'normal', 'regular', 'book', 'medium',
      'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy',
      'extra bold', 'black'. Default: :rc:`font.weight`

    - size: Either an relative value of 'xx-small', 'x-small',
      'small', 'medium', 'large', 'x-large', 'xx-large' or an
      absolute font size, e.g., 10. Default: :rc:`font.size`

    - math_fontfamily: The family of fonts used to render math text.
      Supported values are: 'dejavusans', 'dejavuserif', 'cm',
      'stix', 'stixsans' and 'custom'. Default: :rc:`mathtext.fontset`

    Alternatively, a font may be specified using the absolute path to a font
    file, by using the *fname* kwarg.  However, in this case, it is typically
    simpler to just pass the path (as a `pathlib.Path`, not a `str`) to the
    *font* kwarg of the `.Text` object.

    The preferred usage of font sizes is to use the relative values,
    e.g.,  'large', instead of absolute font sizes, e.g., 12.  This
    approach allows all text sizes to be made larger or smaller based
    on the font manager's default font size.

    This class will also accept a fontconfig_ pattern_, if it is the only
    argument provided.  This support does not depend on fontconfig; we are
    merely borrowing its pattern syntax for use here.

    .. _fontconfig: https://www.freedesktop.org/wiki/Software/fontconfig/
    .. _pattern:
       https://www.freedesktop.org/software/fontconfig/fontconfig-user.html

    Note that Matplotlib's internal font manager and fontconfig use a
    different algorithm to lookup fonts, so the results of the same pattern
    may be different in Matplotlib than in other applications that use
    fontconfig.
    Nc	           	      C   s   |  | | | | | | | | | | | | | | | t|t	r|d u r|d u r|d u r|d u r|d u r|d u r| 
| d S r   )
set_family	set_styleset_variant
set_weightset_stretchset_fileset_sizeset_math_fontfamilyri   rj   set_fontconfig_pattern)	r   familyr   r   r   r   r   r   math_fontfamilyr;   r;   r>   __init__  s,    








zFontProperties.__init__c                 C   sV   t || r|S |du r|  S t |tjr2| |dS t |trD| |S | f i |S dS )a  
        Generic constructor which can build a `.FontProperties` from any of the
        following:

        - a `.FontProperties`: it is passed through as is;
        - `None`: a `.FontProperties` using rc values is used;
        - an `os.PathLike`: it is used as path to the font file;
        - a `str`: it is parsed as a fontconfig pattern;
        - a `dict`: it is passed as ``**kwargs`` to `.FontProperties`.
        N)r   )ri   rH   PathLikerj   )clsargr;   r;   r>   	_from_any  s    


zFontProperties._from_anyc              	   C   s@   t |  |  |  |  |  |  |  |  f}t	|S r   )
tuple
get_family	get_slantget_variantr   get_stretchget_sizeget_fileget_math_fontfamilyhash)r   lr;   r;   r>   __hash__  s    
zFontProperties.__hash__c                 C   s   t | t |kS r   )r  )r   otherr;   r;   r>   __eq__  s    zFontProperties.__eq__c                 C   s   |   S r   )get_fontconfig_patternr   r;   r;   r>   __str__  s    zFontProperties.__str__c                 C   s   | j S )a  
        Return a list of individual font family names or generic family names.

        The font families or generic font families (which will be resolved
        from their respective rcParams when searching for a matching font) in
        the order of preference.
        )_familyr   r;   r;   r>   r     s    zFontProperties.get_familyc                 C   s   t t| jS )zT
        Return the name of the font that best matches the font properties.
        )get_fontfindfontr   r   r;   r;   r>   get_name  s    zFontProperties.get_namec                 C   s   | j S )zV
        Return the font style.  Values are: 'normal', 'italic' or 'oblique'.
        )_slantr   r;   r;   r>   	get_style  s    zFontProperties.get_stylec                 C   s   | j S )zQ
        Return the font variant.  Values are: 'normal' or 'small-caps'.
        )_variantr   r;   r;   r>   r     s    zFontProperties.get_variantc                 C   s   | j S )z
        Set the font weight.  Options are: A numeric value in the
        range 0-1000 or one of 'light', 'normal', 'regular', 'book',
        'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold',
        'heavy', 'extra bold', 'black'
        )_weightr   r;   r;   r>   r     s    zFontProperties.get_weightc                 C   s   | j S )z
        Return the font stretch or width.  Options are: 'ultra-condensed',
        'extra-condensed', 'condensed', 'semi-condensed', 'normal',
        'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.
        )_stretchr   r;   r;   r>   r     s    zFontProperties.get_stretchc                 C   s   | j S )z'
        Return the font size.
        )_sizer   r;   r;   r>   r    s    zFontProperties.get_sizec                 C   s   | j S )z=
        Return the filename of the associated font.
        )_filer   r;   r;   r>   r    s    zFontProperties.get_filec                 C   s   t | S )a   
        Get a fontconfig_ pattern_ suitable for looking up the font as
        specified with fontconfig's ``fc-match`` utility.

        This support does not depend on fontconfig; we are merely borrowing its
        pattern syntax for use here.
        )r   r   r;   r;   r>   r	    s    z%FontProperties.get_fontconfig_patternc                 C   s,   |du rt jd }t|tr"|g}|| _dS )aV  
        Change the font family.  May be either an alias (generic name
        is CSS parlance), such as: 'serif', 'sans-serif', 'cursive',
        'fantasy', or 'monospace', a real font name or a list of real
        font names.  Real font names are not supported when
        :rc:`text.usetex` is `True`. Default: :rc:`font.family`
        Nzfont.family)mplrcParamsri   rj   r  )r   r   r;   r;   r>   r   $  s
    

zFontProperties.set_familyc                 C   s.   |du rt jd }tjg d|d || _dS )z
        Set the font style.

        Parameters
        ----------
        style : {'normal', 'italic', 'oblique'}, default: :rc:`font.style`
        Nz
font.style)r   r   r   )r   )r  r  r   check_in_listr  )r   r   r;   r;   r>   r   2  s    
zFontProperties.set_stylec                 C   s.   |du rt jd }tjddg|d || _dS )z
        Set the font variant.

        Parameters
        ----------
        variant : {'normal', 'small-caps'}, default: :rc:`font.variant`
        Nzfont.variantr   r   )r   )r  r  r   r  r  )r   r   r;   r;   r>   r   ?  s    
zFontProperties.set_variantc                 C   sz   |du rt jd }|tv r$|| _dS zt|}W n tyB   Y n$0 d|  krXdkrfn n
|| _dS td|ddS )aC  
        Set the font weight.

        Parameters
        ----------
        weight : int or {'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}, default: :rc:`font.weight`
            If int, must be in the range  0-1000.
        Nzfont.weightr   r1   zweight= is invalid)r  r  r   r  r   r   r   r   r;   r;   r>   r   L  s    
zFontProperties.set_weightc                 C   sz   |du rt jd }|tv r$|| _dS zt|}W n tyB   Y n$0 d|  krXdkrfn n
|| _dS td|ddS )aP  
        Set the font stretch or width.

        Parameters
        ----------
        stretch : int or {'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}, default: :rc:`font.stretch`
            If int, must be in the range  0-1000.
        Nzfont.stretchr   r1   zstretch=r  )r  r  stretch_dictr  r   r   )r   r   r;   r;   r>   r   f  s    
zFontProperties.set_stretchc                 C   s   |du rt jd }zt|}W nj ty   zt| }W n> tyv } z&tddttt |W Y d}~nd}~0 0 |t	
  }Y n0 |dk rtd| d}|| _dS )aE  
        Set the font size.

        Parameters
        ----------
        size : float or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}, default: :rc:`font.size`
            If float, the font size in points. The string values denote sizes
            relative to the default font size.
        N	font.sizez%Size is invalid. Valid font size are , r   zHFontsize %1.2f < 1.0 pt not allowed by FreeType. Setting fontsize = 1 pt)r  r  floatr   font_scalingsKeyErrorrK   r   rj   FontManagerget_default_sizer   infor  )r   r   scaleerrr;   r;   r>   r     s,    
zFontProperties.set_sizec                 C   s   |durt |nd| _dS )zw
        Set the filename of the fontfile to use.  In this case, all
        other properties will be ignored.
        N)rH   fspathr  )r   filer;   r;   r>   r     s    zFontProperties.set_filec                 C   sP   t | D ]>\}}t|tkr8t| d| |d  qt| d| | qdS )z
        Set the properties by parsing a fontconfig_ *pattern*.

        This support does not depend on fontconfig; we are merely borrowing its
        pattern syntax for use here.
        set_r   N)r   rp   typelistgetattr)r   patternrt   valr;   r;   r>   r     s    z%FontProperties.set_fontconfig_patternc                 C   s   | j S )z}
        Return the name of the font family used for math text.

        The default font is :rc:`mathtext.fontset`.
        )_math_fontfamilyr   r;   r;   r>   r    s    z"FontProperties.get_math_fontfamilyc                 C   s:   |du rt jd }ntd j }tj||d || _dS )a  
        Set the font family for text in math mode.

        If not set explicitly, :rc:`mathtext.fontset` will be used.

        Parameters
        ----------
        fontfamily : str
            The name of the font family.

            Available font families are defined in the
            matplotlibrc.template file
            :ref:`here <customizing-with-matplotlibrc-files>`

        See Also
        --------
        .text.Text.get_math_fontfamily
        Nzmathtext.fontset)r   )r  r  r   validvaluesr   r  r.  )r   Z
fontfamilyZvalid_fontsr;   r;   r>   r     s
    z"FontProperties.set_math_fontfamilyc                 C   s
   t  | S )zReturn a copy of self.)copyr   r;   r;   r>   r1    s    zFontProperties.copy)NNNNNNNN)"__name__
__module____qualname__r   r   classmethodr   r  r  r
  r   r  r  r   r   r   r  r  r	  r   r   r   r   r   r   r   r   r  r   r1  set_namer   Z	set_slantget_size_in_pointsr;   r;   r;   r>   r   f  sF   >    


	
r   c                       s   e Zd Z fddZ  ZS )_JSONEncoderc                    sz   t |trt|jddS t |trjt|jdd}z"tt|d t	 |d< W n t
yd   Y n0 |S t |S d S )Nr!  	__class__r   r   )ri   r!  dict__dict__r   rj   r   relative_tor  get_data_pathr   superr   )r   odr9  r;   r>   r     s    

z_JSONEncoder.default)r2  r3  r4  r   __classcell__r;   r;   r9  r>   r8    s   r8  c                 C   s   |  dd }|d u r| S |dkr:tt}|j|  |S |dkrtt}|j|  tj|j	s|tj
t |j	|_	|S td| d S )Nr:  r!  r   z*Don't know how to deserialize __class__=%s)popr!  __new__r<  rz   r   rH   rI   isabsr   rK   r  r>  r   )r@  r   rr;   r;   r>   _json_decode  s    

rG  c                 C   s   t | t|d\}ztj| |tdd W n4 tyb } ztd	| W Y d}~n
d}~0 0 W d   n1 sx0    Y  W d   n1 s0    Y  dS )a  
    Dump `FontManager` *data* as JSON to the file named *filename*.

    See Also
    --------
    json_load

    Notes
    -----
    File paths that are children of the Matplotlib data path (typically, fonts
    shipped with Matplotlib) are stored relative to that data path (to remain
    valid across virtualenvs).

    This function temporarily locks the output file to prevent multiple
    processes from overwriting one another's output.
    wr   )r   indentz$Could not save font_manager cache {}N)
r	   
_lock_pathopenjsondumpr8  r`   r   r   format)datarL   fher;   r;   r>   	json_dump  s
    rR  c                 C   s:   t | }tj|tdW  d   S 1 s,0    Y  dS )zl
    Load a `FontManager` from the JSON file named *filename*.

    See Also
    --------
    json_dump
    )object_hookN)rK  rL  loadrG  )rL   rP  r;   r;   r>   	json_load  s    
rU  c                   @   s   e Zd ZdZdZd*ddZdd Zed	d
 Zdd Z	e
dd Zdd Ze
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%d&Zed'd(d) ZdS )-r!  aQ  
    On import, the `FontManager` singleton instance creates a list of ttf and
    afm fonts and caches their `FontProperties`.  The `FontManager.findfont`
    method does a nearest neighbor search to find the font that most closely
    matches the specification.  If no good enough match is found, the default
    font is returned.
    iJ  Nr   c                 C   s  | j | _|| _|| _dd dD }tdt| ddd| _g | _g | _	t
dd	d
 }|  zdD ]}g t||dt|dD ]v}z| | W q ty } ztd|| W Y d }~qd }~0  ty } ztd|| W Y d }~qd }~0 0 qqhW |  n
|  0 d S )Nc                 S   s   g | ]}t d |qS )r6   )r	   _get_data_path)rC   subdirr;   r;   r>   rE   =  s   z(FontManager.__init__.<locals>.<listcomp>)r:   r7   Zpdfcorefontszfont search path %szDejaVu SansZ	Helvetica)r:   r7      c                   S   s
   t dS )Nz>Matplotlib is building the font cache; this may take a moment.)r   r   r;   r;   r;   r>   r   I  s   z&FontManager.__init__.<locals>.<lambda>)r7   r:   r<   zFailed to open font file %s: %sz-Failed to extract font properties from %s: %s)__version___version_FontManager__default_weightdefault_sizer   debugrj   defaultFamilyafmlistttflist	threadingTimerstartr   addfontr`   r#  	Exceptioncancel)r   r   r   pathstimerr=   rI   excr;   r;   r>   r   6  s8    " zFontManager.__init__c                 C   s   t |}t|j dkrht|d}t|}W d   n1 sF0    Y  t||}| j	
| nt|}t|}| j
| | j  dS )z
        Cache the properties of the font at *path* to make it available to the
        `FontManager`.  The type of font is inferred from the path suffix.

        Parameters
        ----------
        path : str or path-like
        z.afmrbN)rH   r   r   rN   rO   rK  r   ZAFMr   r_  appendr
   FT2Fontr   r`  _findfont_cachedcache_clear)r   rI   rP  r   propr;   r;   r>   rd  Z  s    
(

zFontManager.addfontc                    s    fdd j  D S )Nc                    s    i | ]\}}| j ||d qS )r<   )r  )rC   rD   r   r   r;   r>   
<dictcomp>u  s   z+FontManager.defaultFont.<locals>.<dictcomp>)r^  rp   r   r;   r   r>   defaultFontq  s    
zFontManager.defaultFontc                 C   s   | j S )z1
        Return the default font weight.
        r[  r   r;   r;   r>   get_default_weightx  s    zFontManager.get_default_weightc                   C   s
   t jd S )z/
        Return the default font size.
        r  r  r  r;   r;   r;   r>   r"  ~  s    zFontManager.get_default_sizec                 C   s
   || _ dS )zN
        Set the default font weight.  The initial value is 'normal'.
        Nrr  r  r;   r;   r>   set_default_weight  s    zFontManager.set_default_weightc                 C   s   | dv rd} t jd|   S )N)r3   r2   r4   zfont.rt  )r   r;   r;   r>   _expand_aliases  s    zFontManager._expand_aliasesc                 C   s   t |ttfs|g}nt|dkr&dS | }dt| }t|D ]n\}}| }|tv rg ttj| 	|}||v r|
|}||t|  |   S qB||krB||   S qBdS )a&  
        Return a match score between the list of font families in
        *families* and the font family name *family2*.

        An exact match at the head of the list returns 0.0.

        A match further down the list will return between 0 and 1.

        No match will return 1.0.
        r   r   rc   )ri   r*  r   lenrO   	enumeratefont_family_aliasesr   rj   rv  index)r   familiesZfamily2stepiZfamily1optionsidxr;   r;   r>   score_family  s     
zFontManager.score_familyc                 C   s$   ||krdS |dv r |dv r dS dS )z
        Return a match score between *style1* and *style2*.

        An exact match returns 0.0.

        A match between 'italic' and 'oblique' returns 0.1.

        No match returns 1.0.
                )r   r   g?r   r;   )r   Zstyle1Zstyle2r;   r;   r>   score_style  s    
zFontManager.score_stylec                 C   s   ||krdS dS dS )z}
        Return a match score between *variant1* and *variant2*.

        An exact match returns 0.0, otherwise 1.0.
        r  r   Nr;   )r   Zvariant1Zvariant2r;   r;   r>   score_variant  s    zFontManager.score_variantc                 C   sh   zt |}W n ty*   t|d}Y n0 zt |}W n tyV   t|d}Y n0 t|| d S )z
        Return a match score between *stretch1* and *stretch2*.

        The result is the absolute value of the difference between the
        CSS numeric values of *stretch1* and *stretch2*, normalized
        between 0.0 and 1.0.
        r   g     @@)r   r   r  r   abs)r   Zstretch1Zstretch2Zstretchval1Zstretchval2r;   r;   r>   score_stretch  s    zFontManager.score_stretchc                 C   sT   t ||rdS t|tr|nt| }t|tr4|nt| }dt|| d  d S )ah  
        Return a match score between *weight1* and *weight2*.

        The result is 0.0 if both weight1 and weight 2 are given as strings
        and have the same value.

        Otherwise, the result is the absolute value of the difference between
        the CSS numeric values of *weight1* and *weight2*, normalized between
        0.05 and 1.0.
        r  gffffff?r1   g?)r	   
_str_equalri   r   r   r  )r   Zweight1Zweight2w1w2r;   r;   r>   score_weight  s
    zFontManager.score_weightc                 C   sl   |dkrdS zt |}W n  ty8   | jt|  }Y n0 zt |}W n tyZ   Y dS 0 t|| d S )a  
        Return a match score between *size1* and *size2*.

        If *size2* (the size specified in the font file) is 'scalable', this
        function always returns 0.0, since any font size can be generated.

        Otherwise, the result is the absolute distance between *size1* and
        *size2*, normalized so that the usual range of font sizes (6pt -
        72pt) will lie between 0.0 and 1.0.
        r   r  r   H   )r  r   r\  r  r  )r   size1size2Zsizeval1Zsizeval2r;   r;   r>   
score_size  s    zFontManager.score_sizer:   Tc                 C   s&   t dd dD }| ||||||S )a  
        Find a font that most closely matches the given font properties.

        Parameters
        ----------
        prop : str or `~matplotlib.font_manager.FontProperties`
            The font properties to search for. This can be either a
            `.FontProperties` object or a string defining a
            `fontconfig patterns`_.

        fontext : {'ttf', 'afm'}, default: 'ttf'
            The extension of the font file:

            - 'ttf': TrueType and OpenType fonts (.ttf, .ttc, .otf)
            - 'afm': Adobe Font Metrics (.afm)

        directory : str, optional
            If given, only search this directory and its subdirectories.

        fallback_to_default : bool
            If True, will fallback to the default font family (usually
            "DejaVu Sans" or "Helvetica") if the first lookup hard-fails.

        rebuild_if_missing : bool
            Whether to rebuild the font cache and search again if the first
            match appears to point to a nonexisting font (i.e., the font cache
            contains outdated entries).

        Returns
        -------
        str
            The filename of the best matching font.

        Notes
        -----
        This performs a nearest neighbor search.  Each font is given a
        similarity score to the target font properties.  The first font with
        the highest score is returned.  If no matches below a certain
        threshold are found, the default font (usually DejaVu Sans) is
        returned.

        The result is cached, so subsequent lookups don't have to
        perform the O(n) nearest neighbor search.

        See the `W3C Cascading Style Sheet, Level 1
        <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
        for a description of the font finding algorithm.

        .. _fontconfig patterns:
           https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
        c                 s   s   | ]}t tj| V  qd S r   )r   r  r  )rC   rt   r;   r;   r>   r   A  rF   z'FontManager.findfont.<locals>.<genexpr>)z
font.serifzfont.sans-serifzfont.cursivezfont.fantasyzfont.monospace)r   rm  )r   ro  r=   rM   fallback_to_defaultrebuild_if_missing	rc_paramsr;   r;   r>   r  	  s
    8
zFontManager.findfontc                 C   s   t tdd | jD S )z#Return the list of available fonts.c                 S   s   g | ]
}|j qS r;   )r   )rC   r   r;   r;   r>   rE   J  rF   z.FontManager.get_font_names.<locals>.<listcomp>)r*  rd   r`  r   r;   r;   r>   get_font_namesH  s    zFontManager.get_font_namesc           
      C   s   t |}g }| D ]x}| }|| z|| j|||d|d W q ty   |tv r|t	
d|d| | nt	
d| Y q0 q|s|r| j| }	| }||	 || j|||d|d ntd|S )as  
        Find font families that most closely match the given properties.

        Parameters
        ----------
        prop : str or `~matplotlib.font_manager.FontProperties`
            The font properties to search for. This can be either a
            `.FontProperties` object or a string defining a
            `fontconfig patterns`_.

        fontext : {'ttf', 'afm'}, default: 'ttf'
            The extension of the font file:

            - 'ttf': TrueType and OpenType fonts (.ttf, .ttc, .otf)
            - 'afm': Adobe Font Metrics (.afm)

        directory : str, optional
            If given, only search this directory and its subdirectories.

        fallback_to_default : bool
            If True, will fallback to the default font family (usually
            "DejaVu Sans" or "Helvetica") if none of the families were found.

        rebuild_if_missing : bool
            Whether to rebuild the font cache and search again if the first
            match appears to point to a nonexisting font (i.e., the font cache
            contains outdated entries).

        Returns
        -------
        list[str]
            The paths of the fonts found

        Notes
        -----
        This is an extension/wrapper of the original findfont API, which only
        returns a single font for given font properties. Instead, this API
        returns an dict containing multiple fonts and their filepaths
        which closely match the given font properties.  Since this internally
        uses the original API, there's no change to the logic of performing the
        nearest neighbor search.  See `findfont` for more details.
        F)r  r  [findfont: Generic family %r not found because none of the following families were found: %sr  z#findfont: Font family %r not found.TzFFailed to find any font, and fallback to the default font was disabled)r   r   r   r1  r   rk  r  r   ry  r   r   rK   rv  r^  )
r   ro  r=   rM   r  r  Zfpathsr   ZcpropZdfamilyr;   r;   r>   _find_fonts_by_propsL  sD    -



z FontManager._find_fonts_by_propsi   c              
   C   s.  t |}| }|d ur|S |dkr.| j}n| j}d}	d }
td| |D ]}|d urnt|t|jj	vrnqL| 
| |jd | | |j | | |j | | |j | | |j | | |j }td|| ||	k r|}	|}
|dkrL qqL|
d u s |	dkr|rtd| | j|  ttj | D ]*}|t!v rNtd	|d
"| #| qN|$ }|%| j|  | j&|||ddS t'd| dntd||
j|
j|	 |
j}t(j)*|s&|rt+d t,dd}t-| .t-| | j&|||ddS t'dt/|S )Nr7   g?O8Mzfindfont: Matching %s.
   zfindfont: score(%s) = %sr   g      $@z7findfont: Font family %s not found. Falling back to %s.r  r  F)r  zFailed to find font z/, and fallback to the default font was disabledz2findfont: Matching %s to %s (%r) with score of %f.z7findfont: Found a missing font file.  Rebuilding cache.try_read_cache)r  zNo valid font could be found)0r   r   r  r_  r`  r   r]  r   r   parentsr  r   r   r  r  r   r  r   r   r  r   r   r  r   r   r  r  r   r   r^  r   rj   rO   ry  rK   rv  r1  r   r  r   rH   rI   rJ   r#  _load_fontmanagervarsrz   _cached_realpath)r   ro  r=   rM   r  r  r  r   ZfontlistZ
best_scoreZ	best_fontr   scorer   Zdefault_propresultZnew_fmr;   r;   r>   rm    s    



zFontManager._findfont_cached)Nr   )r:   NTT)r:   NTT)r2  r3  r4  r   rY  r   rd  propertyrq  rs  staticmethodr"  ru  rv  r  r  r  r  r  r  r  r  r  r   rm  r;   r;   r;   r>   r!  )  s6   

$


  
?  
Zr!  c                 C   sZ   t j| d  dkrRt| d}|ddkW  d   S 1 sF0    Y  ndS dS )z
    Return whether the given font is a Postscript Compact Font Format Font
    embedded in an OpenType wrapper.  Used by the PostScript and PDF backends
    that can not subset these fonts.
    rc   z.otfrj  r   s   OTTONF)rH   rI   splitextrO   rK  read)rL   fdr;   r;   r>   is_opentype_cff_font  s    .r  @   c                   s*   | ^}}t j| fdd|D  dS )Nc                    s   g | ]}t j| d qS ))_kerning_factorr
   rl  )rC   fpathr  hinting_factorr;   r>   rE     s
   z_get_font.<locals>.<listcomp>)Z_fallback_listr  r  )font_filepathsr  r  	thread_idZfirst_fontpathrestr;   r  r>   	_get_font  s    r  register_at_fork)after_in_childc                 C   s   t j| S r   )rH   rI   realpath)rI   r;   r;   r>   r    s    r  z3.6filepathr  c                 C   sZ   t | tttfrt| f}ntdd | D }|du r@tjd }t||tjd t	
 dS )a=  
    Get an `.ft2font.FT2Font` object given a list of file paths.

    Parameters
    ----------
    font_filepaths : Iterable[str, Path, bytes], str, Path, bytes
        Relative or absolute paths to the font files to be used.

        If a single string, bytes, or `pathlib.Path`, then it will be treated
        as a list with that entry only.

        If more than one filepath is passed, then the returned FT2Font object
        will fall back through the fonts, in the order given, to find a needed
        glyph.

    Returns
    -------
    `.ft2font.FT2Font`

    c                 s   s   | ]}t |V  qd S r   )r  r   r;   r;   r>   r   ;  rF   zget_font.<locals>.<genexpr>Nztext.hinting_factorztext.kerning_factor)r  r  )ri   rj   r   bytesr  r   r  r  r  ra  	get_ident)r  r  rg  r;   r;   r>   r  "  s    
r  Tr  c                 C   s~   t t dtj d}| r`zt|}W n ty:   Y n&0 t|dt tjkr`t	
d| |S t }t|| t	d |S )Nz
fontlist-vz.jsonrZ  z"Using fontManager instance from %szgenerated new fontManager)r   r  get_cachedirr!  rY  rU  re  r+  objectr   r]  rR  r#  )r  Zfm_pathfmr;   r;   r>   r  J  s    

r  )Nr:   )r:   )Nr:   )N)Wr   base64r   r1  dataclasses	functoolsr   ior   rL  loggingnumbersr   rH   pathlibr   r   r   rT   ra  
matplotlibr  r   r   r	   r
   matplotlib._fontconfig_patternr   r   matplotlib.rcsetupr   	getLoggerr2  r   r  r  r   r   ry  homeZ_HOMEre  devnullr^   re   rj   r|   ra   r   r   r   r?   rZ   rV   rw   
deprecatedr}   r   r   r   r   r   r   make_dataclassfieldr   r   r   r   JSONEncoderr8  rG  rR  rU  r!  r  r  hasattrr  rn  r  rename_parameterr  r  fontManagerr  r  r;   r;   r;   r>   <module>   s"  




0 

!

D  }   N


'