3
$ßL]á²  ã               @   sj  d 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yddlmZ W n  ek
r€   ddlmZ Y nX ddlZddlmZmZmZmZ ddlmZmZ ejeƒZd	d
dddddddddœ
Z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ddddd d!d"hZd#Z d$d%gZ!ej"j#e$e	j%ƒ ƒd&ƒej"j#e$e	j%ƒ ƒd'ƒgZ&d(d)d*d+d,e$e	ej'j(d-e	j%ƒ d. ƒƒd/ ƒe$e	j%ƒ d0 ƒgZ)d1d2d3d4e$e	j%ƒ d5 ƒgZ*d6d7„ Z+d8d9„ Z,d:d;„ Z-d<d=„ Z.dod?d@„Z/ej0dAƒdpdBdC„ƒZ1eƒ dDdE„ ƒZ2dqdFdG„Z3drdHdI„Z4G dJdK„ dKe5ƒZ6dLdM„ Z7dNdO„ Z8dsdPdQ„Z9G dRdS„ dSe5ƒZ:G dTdU„ dUej;ƒZ;dVdW„ Z<dXdY„ Z=dZd[„ Z>d\d]„ Z?ej0d^ƒG d_d`„ d`e5ƒƒZ@G dadb„ dbe5ƒZAeƒ dcdd„ ƒZBej"j#ejCƒ dejDeAjEƒƒZFdaGedfƒejHƒZIeJedgƒ�rúejKeIjLdh� dtdidj„ZMdkdl„ ZNye>eFƒaGW n eOk
�r4   eNƒ  Y n,X ePtGdme5ƒ ƒeAjEk�rTeNƒ  nejQdneFƒ tGjRZRdS )ua”  
A module for finding, managing, and using fonts across platforms.

This module provides a single :class:`FontManager` instance that can
be shared across backends and platforms.  The :func:`findfont`
function returns the best TrueType (TTF) font file in the local or
system font path that matches the specified :class:`FontProperties`
instance.  The :class:`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.
é    )Ú	lru_cacheN)ÚNumber)ÚPath)ÚTimer)ÚafmÚcbookÚft2fontÚrcParams)Úparse_fontconfig_patternÚgenerate_fontconfig_patterngºI+‡â?gh‘í|?5æ?g-²�ï§ê?g      ð?g333333ó?g
×£p=
÷?gÙÎ÷Sã¥û?)
zxx-smallzx-smallÚsmallÚmediumZlargezx-largezxx-largeZlargerZsmallerNéd   éÈ   i,  i�  iô  iX  i¼  i   i„  )	zultra-condensedzextra-condensedÚ	condensedzsemi-condensedÚnormalzsemi-expandedÚexpandedzextra-expandedzultra-expanded)Z
ultralightÚlightr   ÚregularZbookr   ZromanZsemiboldZdemiboldZdemiZboldZheavyz
extra boldZblackZserifz
sans-serifz
sans serifZcursiveZfantasyZ	monospaceÚsansz@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dddgdddgdddgdœ|  S )zs
    Return a list of file extensions extensions that are synonyms for
    the given file extension *fileext*.
    r   ÚotfÚttcÚttf)r   r   r   r   © )Úfontextr   r   ú?/tmp/pip-install-edjban26/matplotlib/matplotlib/font_manager.pyÚget_fontext_synonyms…   s
    r   c                s&   dd„ ˆ D ƒ‰ ‡ fdd„t j| ƒ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>—   s    zlist_fonts.<locals>.<listcomp>c                s<   g | ]4\}}}|D ]$}t |ƒjjƒ ˆ krtjj||ƒ‘qqS r   )r   ÚsuffixÚlowerÚosÚpathÚjoin)r    ÚdirpathÚ_Ú	filenamesÚfilename)Ú
extensionsr   r   r"   ˜   s   )r%   Úwalk)Ú	directoryr,   r   )r,   r   Ú
list_fonts’   s    
r/   c              C   s\   ddl } y*| j| jtƒ�}| j|dƒd S Q R X W n$ tk
rV   tjjtj	d dƒS X 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ÚOSErrorr%   r&   r'   Úenviron)r0   Úuserr   r   r   Úwin32FontDirectoryŸ   s    	r8   c             C   sÖ   ddl }tƒ }xÂtD ]º}yš|j| |ƒ�„}x|t|j|ƒd ƒD ]f}|j||ƒ\}}}	t|tƒs^q<|j	ddƒd }yt
||ƒjƒ }
W n tk
r–   w<Y nX |j|
ƒ q<W W dQ R X W q ttfk
rÌ   wY qX qW |S )a  
    Searches 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é   ú )r0   ÚsetÚMSFontDirectoriesr1   ÚrangeZQueryInfoKeyZ	EnumValueÚ
isinstanceÚstrÚsplitr   ÚresolveÚRuntimeErrorÚaddr5   ÚMemoryError)Z
reg_domainÚbase_dirr0   ÚitemsZreg_pathÚlocalÚjÚkeyÚvalueÚtpr&   r   r   r   Ú_win32RegistryFonts°   s$    

rL   r   c                sr   ddl }| dkrtƒ } dd„ tˆ ƒD ƒ‰ tƒ }|jt|j| ƒƒ xtD ]}|jt|j|ƒƒ qFW ‡ 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   )r    r!   r   r   r   r"   ð   s    z'win32InstalledFonts.<locals>.<listcomp>c                s"   g | ]}|j jƒ ˆ krt|ƒ‘qS r   )r#   r$   r?   )r    r&   )r   r   r   r"   ü   s    )	r0   r8   r   r;   ÚupdaterL   ÚHKEY_LOCAL_MACHINEÚMSUserFontDirectoriesr2   )r.   r   r0   rF   Zuserdirr   )r   r   Úwin32InstalledFontsä   s    
rP   z3.1c                s   | dkrt } ‡ fdd„| D ƒS )zGet list of font files on OS X.Nc                s$   g | ]}t |tˆ ƒƒD ]}|‘qqS r   )r/   r   )r    r.   r&   )r   r   r   r"     s   z%OSXInstalledFonts.<locals>.<listcomp>)ÚOSXFontDirectories)Údirectoriesr   r   )r   r   ÚOSXInstalledFontsÿ   s    
rS   c              C   sh   t ddd„ ƒ} | jƒ  z2ytjddgƒ}W n ttjfk
rD   g S X W d| jƒ  X dd„ |jd	ƒD ƒS )
z:Cache and list the font filenames known to `fc-list`.
    é   c               S   s
   t jdƒS )NzLMatplotlib is building the font cache using fc-list. This may take a moment.)Ú_logÚwarningr   r   r   r   Ú<lambda>  s   z_call_fc_list.<locals>.<lambda>zfc-listz--format=%{file}\nNc             S   s   g | ]}t j|ƒ‘qS r   )r%   Úfsdecode)r    Úfnamer   r   r   r"     s    z!_call_fc_list.<locals>.<listcomp>ó   
)r   ÚstartÚ
subprocessÚcheck_outputr5   ÚCalledProcessErrorÚcancelr@   )ZtimerÚoutr   r   r   Ú_call_fc_list	  s    

ra   c                s&   dd„ t ˆ ƒD ƒ‰ ‡ fdd„tƒ D ƒS )zKList the font filenames known to `fc-list` having the given extension.
    c             S   s   g | ]}d | ‘qS )r   r   )r    r!   r   r   r   r"     s    z(get_fontconfig_fonts.<locals>.<listcomp>c                s"   g | ]}t |ƒjjƒ ˆ kr|‘qS r   )r   r#   r$   )r    rY   )r   r   r   r"     s    )r   ra   )r   r   )r   r   Úget_fontconfig_fonts  s    rb   c             C   sª   t ƒ }t|ƒ}| dkrdtjdkr>ttƒ g } |jt|d�ƒ qtt} tjdkrTtt	•} |jt
|ƒƒ nt| tƒrt| g} x&| D ]}|jttjjt||ƒƒƒ qzW d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.
    NÚwin32)r   Údarwinc             S   s   g | ]}t jj|ƒr|‘qS r   )r%   r&   Úexists)r    rY   r   r   r   r"   ?  s    z#findSystemFonts.<locals>.<listcomp>)r;   r   ÚsysÚplatformrO   r8   rM   rP   ÚX11FontDirectoriesrQ   rb   r>   r?   Úmapr%   r&   Úabspathr/   )Z	fontpathsr   Ú	fontfilesZfontextsr&   r   r   r   ÚfindSystemFonts#  s    



rl   c               @   s"   e Zd ZdZd
dd„Zdd„ Zd	S )Ú	FontEntryzj
    A class for storing Font properties.  It is used when populating
    the font lookup dictionary.
    Ú r   r   c             C   sV   || _ || _|| _|| _|| _|| _ytt|ƒƒ| _W n t	k
rP   || _Y nX d S )N)
rY   ÚnameÚstyleÚvariantÚweightÚstretchr?   ÚfloatÚsizeÚ
ValueError)ÚselfrY   ro   rp   rq   rr   rs   ru   r   r   r   Ú__init__G  s    	zFontEntry.__init__c             C   s(   d| j tjj| jƒ| j| j| j| jf S )Nz<Font '%s' (%s) %s %s %s %s>)	ro   r%   r&   ÚbasenamerY   rp   rq   rr   rs   )rw   r   r   r   Ú__repr__[  s    zFontEntry.__repr__N)rn   rn   r   r   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rx   rz   r   r   r   r   rm   B  s         
rm   c       	         s¤  | j }| jƒ }|jddƒjdƒjƒ p8|jddƒjdƒjƒ }|jd dƒjdƒjƒ pd|jd!dƒjdƒjƒ ‰ ˆ jd
ƒdkrzd
}n>ˆ jdƒdkrŽd}n*|jdƒdkr¢d}n| jtj@ r´d}nd}|jƒ d"krÊd}nd}t	‡ fdd„t
D ƒdƒ}|�s| jtj@ �r d}nd}ˆ jdƒdk�s4ˆ jdƒdk�s4ˆ jdƒdk�r:d}n@ˆ jdƒdk�rPd}n*ˆ jdƒdk�spˆ jdƒdk�rvd}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.

    r9   r   é   ó    zlatin-1é   é	  Ú	utf_16_beé   ÚobliqueÚitalicr   r   Úcapitalsú
small-capsc             3   s    | ]}ˆ j |ƒd kr|V  qdS )r   N)Úfind)r    Úw)Úsfnt4r   r   ú	<genexpr>‘  s    z"ttfFontProperty.<locals>.<genexpr>Ni¼  i�  Únarrowr   Úcondz	demi condzsemi-condensedÚwider   z$Non-scalable fonts are not supportedÚscalable)r9   r   r   r   )r�   r9   r‚   r   )r9   r   r   r„   )r�   r9   r‚   r„   )r‡   rˆ   )Úfamily_nameZget_sfntÚgetÚdecoder$   r‰   Zstyle_flagsr   ZITALICÚnextÚweight_dictZBOLDr�   ÚNotImplementedErrorrm   rY   )	Úfontro   ZsfntZsfnt2rp   rq   rr   rs   ru   r   )r‹   r   ÚttfFontPropertya  sF    	  	r˜   c       	      C   sÌ   |j ƒ }|jƒ jƒ }|jƒ dks,d|jƒ kr2d}nd|jƒ krDd}nd}|jƒ dkrZd}nd}|jƒ jƒ }|tkrvd}d|kr„d}n0d	|ks”d
|kršd}nd|ksªd|kr°d}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‡   ú
small-capsz	demi condzsemi-condensedr�   rŽ   r   r�   r   r�   )r‡   r™   )Zget_familynameZget_fontnamer$   Z	get_angleÚ
get_weightr•   rm   )	Zfontpathr—   ro   Zfontnamerp   rq   rr   rs   ru   r   r   r   ÚafmFontProperty·  s,    r›   c       
      C   sÀ  g }t ƒ }�x®| D �]¤}tjd|ƒ tjj|ƒd }||kr>q|dkròy$t|dƒ�}tj|ƒ}W dQ R X W nB t	k
rŒ   tj
d|ƒ wY n" tk
r¬   tj
d|ƒ wY nX yt||ƒ}W n2 tk
rî }	 ztj
d||	ƒ wW Y dd}	~	X nX n²ytj|ƒ}W nX ttfk
�r8 }	 ztj
d	||	ƒ wW Y dd}	~	X n" tk
�rX   tj
d
ƒ wY nX yt|ƒ}W n< ttttfk
�r¢ }	 ztj
d||	ƒ wW Y dd}	~	X nX |j|ƒ |j|ƒ qW |S )z›
    A function to create a font lookup list.  The default is to create
    a list of TrueType fonts.  An AFM font list can optionally be
    created.
    zcreateFontDict: %sr9   r   ÚrbNzCould not open font file %szCould not parse font file %sz'Could not extract properties for %s: %szCould not open font file %s: %szCannot handle unicode filenames)r;   rU   Údebugr%   r&   r@   Úopenr   ZAFMÚEnvironmentErrorÚinforB   r›   ÚKeyErrorr   ÚFT2Fontr5   ÚUnicodeErrorr˜   rv   r–   ÚappendrC   )
rk   r   ÚfontlistÚseenZfpathrY   Úfhr—   ÚpropÚexcr   r   r   ÚcreateFontListû  sR    

rª   c               @   sÞ   e Zd ZdZd3dd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Ze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eZd#d$„ ZeZd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ ZdS )4ÚFontPropertiesa’	  
    A class for storing and manipulating font properties.

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

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

      - style: Either 'normal', 'italic' or 'oblique'.

      - variant: Either 'normal' or 'small-caps'.

      - 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'

      - 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'

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

    The default font property for TrueType fonts (as specified in the
    default rcParams) is::

      sans-serif, normal, normal, normal, normal, scalable.

    Alternatively, a font may be specified using an absolute path to a
    .ttf file, by using the *fname* kwarg.

    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 require fontconfig to be
    installed.  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d ƒ| _td | _td | _td | _td | _td | _d | _t	|t
ƒrŽ|d krŽ|d krŽ|d krŽ|d krŽ|d krŽ|d krŽ| j|ƒ d S | j|ƒ | j|ƒ | j|ƒ | j|ƒ | j|ƒ | j|ƒ | j|ƒ d S )Nzfont.familyz
font.stylezfont.variantzfont.weightzfont.stretchz	font.size)Ú_normalize_font_familyr	   Ú_familyÚ_slantÚ_variantÚ_weightÚ_stretchÚ_sizeÚ_filer>   r?   Úset_fontconfig_patternÚ
set_familyÚ	set_styleÚset_variantÚ
set_weightÚset_stretchÚset_fileÚset_size)rw   Úfamilyrp   rq   rr   rs   ru   rY   r   r   r   rx   l  s.    	












zFontProperties.__init__c             C   s   t |ƒS )N)r
   )rw   Úpatternr   r   r   Ú_parse_fontconfig_pattern‘  s    z(FontProperties._parse_fontconfig_patternc             C   s:   t | jƒ ƒ| jƒ | jƒ | jƒ | jƒ | jƒ | jƒ f}t|ƒS )N)	ÚtupleÚ
get_familyÚ	get_slantÚget_variantrš   Úget_stretchÚget_size_in_pointsÚget_fileÚhash)rw   Úlr   r   r   Ú__hash__”  s    

zFontProperties.__hash__c             C   s   t | ƒt |ƒkS )N)rÆ   )rw   Úotherr   r   r   Ú__eq__ž  s    zFontProperties.__eq__c             C   s   | j ƒ S )N)Úget_fontconfig_pattern)rw   r   r   r   Ú__str__¡  s    zFontProperties.__str__c             C   s   | j S )zL
        Return a list of font names that comprise the font family.
        )r­   )rw   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‘   )rw   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'.
        )r®   )rw   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'.
        )r¯   )rw   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'
        )r°   )rw   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'.
        )r±   )rw   r   r   r   rÃ   Æ  s    zFontProperties.get_stretchc             C   s   | j S )z'
        Return the font size.
        )r²   )rw   r   r   r   Úget_sizeÎ  s    zFontProperties.get_sizec             C   s   | j S )N)r²   )rw   r   r   r   rÄ   Ô  s    z!FontProperties.get_size_in_pointsc             C   s   | j S )z=
        Return the filename of the associated font.
        )r³   )rw   r   r   r   rÅ   ×  s    zFontProperties.get_filec             C   s   t | ƒS )a7  
        Get a fontconfig_ pattern_ suitable for looking up the font as
        specified with fontconfig's ``fc-match`` utility.

        This support does not require fontconfig to be installed or
        support for it to be enabled.  We are merely borrowing its
        pattern syntax for use here.
        )r   )rw   r   r   r   rË   Ý  s    	z%FontProperties.get_fontconfig_patternc             C   s   |dkrt d }t|ƒ| _dS )a7  
        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
        `text.usetex` is `True`.
        Nzfont.family)r	   r¬   r­   )rw   r¼   r   r   r   rµ   è  s    zFontProperties.set_familyc             C   s.   |dkrt d }tjdddg|d� || _dS )zS
        Set the font style.  Values are: 'normal', 'italic' or 'oblique'.
        Nz
font.styler   r†   r…   )rp   )r	   r   Ú_check_in_listr®   )rw   rp   r   r   r   r¶   õ  s    zFontProperties.set_stylec             C   s,   |dkrt d }tjddg|d� || _dS )zN
        Set the font variant.  Values are: 'normal' or 'small-caps'.
        Nzfont.variantr   z
small-caps)rq   )r	   r   rÒ   r¯   )rw   rq   r   r   r   r·   ÿ  s    zFontProperties.set_variantc             C   sb   |dkrt d }y"t|ƒ}|dk s*|dkr0tƒ ‚W n$ tk
rV   |tkrRtdƒ‚Y nX || _dS )a	  
        Set the font weight.  May be either 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'
        Nzfont.weightr   iè  zweight is invalid)r	   Úintrv   r•   r°   )rw   rr   r   r   r   r¸     s    
zFontProperties.set_weightc             C   sb   |dkrt d }y"t|ƒ}|dk s*|dkr0tƒ ‚W n$ tk
rV   |tkrRtdƒ‚Y nX || _dS )a  
        Set the font stretch or width.  Options are: 'ultra-condensed',
        'extra-condensed', 'condensed', 'semi-condensed', 'normal',
        'semi-expanded', 'expanded', 'extra-expanded' or
        'ultra-expanded', or a numeric value in the range 0-1000.
        Nzfont.stretchr   iè  zstretch is invalid)r	   rÓ   rv   Ústretch_dictr±   )rw   rs   r   r   r   r¹     s    
zFontProperties.set_stretchc             C   sš   |dkrt d }yt|ƒ}W nZ tk
rv   yt| }W n, tk
rd   tddjtttƒƒ ƒ‚Y nX |tj	ƒ  }Y nX |dk r�t
jd|ƒ d}|| _dS )z¾
        Set the font size.  Either an relative value of 'xx-small',
        'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'
        or an absolute font size, e.g., 12.
        Nz	font.sizez%Size is invalid. Valid font size are z, g      ð?zHFontsize %1.2f < 1.0 pt not allowed by FreeType. Setting fontsize = 1 pt)r	   rt   rv   Úfont_scalingsr¡   r'   ri   r?   ÚFontManagerÚget_default_sizerU   r    r²   )rw   ru   Zscaler   r   r   r»   ,  s"    zFontProperties.set_sizec             C   s
   || _ dS )zw
        Set the filename of the fontfile to use.  In this case, all
        other properties will be ignored.
        N)r³   )rw   Úfiler   r   r   rº   E  s    zFontProperties.set_filec             C   sV   xP| j |ƒjƒ D ]>\}}t|ƒtkr<t| d| ƒ|d ƒ qt| d| ƒ|ƒ qW dS )zõ
        Set the properties by parsing a fontconfig_ *pattern*.

        This support does not require fontconfig to be installed or
        support for it to be enabled.  We are merely borrowing its
        pattern syntax for use here.
        Úset_r   N)r¾   rF   ÚtypeÚlistÚgetattr)rw   r½   rI   Úvalr   r   r   r´   L  s    z%FontProperties.set_fontconfig_patternc             C   s    t | ƒƒ }t|ƒjt| ƒƒ |S )zReturn a copy of self.)rÚ   ÚvarsrM   )rw   Únewr   r   r   ÚcopyZ  s    
zFontProperties.copy)NNNNNNN)r{   r|   r}   r~   rx   r¾   rÈ   rÊ   rÌ   rÀ   rÏ   rÐ   rÁ   rÂ   rš   rÃ   rÑ   rÄ   rÅ   rË   rµ   Úset_namer¶   Z	set_slantr·   r¸   r¹   r»   rº   r´   rà   r   r   r   r   r«   0  sD   :      

		r«   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚJSONEncoderc                s|   t |tƒrt|jdd�S t |tƒrlt|jdd�}y"tt|d ƒjtj	ƒ ƒƒ|d< W n t
k
rf   Y nX |S tƒ j|ƒS d S )NrÖ   )Ú	__class__rm   rY   )r>   rÖ   ÚdictÚ__dict__rm   r?   r   Zrelative_toÚmplÚget_data_pathrv   ÚsuperÚdefault)rw   ÚoÚd)rã   r   r   ré   b  s    

 zJSONEncoder.default)r{   r|   r}   ré   Ú__classcell__r   r   )rã   r   râ   a  s   râ   c             C   s�   | j dd ƒ}|d kr| S |dkr:tjtƒ}|jj| ƒ |S |dkr€tjtƒ}|jj| ƒ tjj|j	ƒs|tjj
tjƒ |j	ƒ|_	|S td| ƒ‚d S )Nrã   rÖ   rm   z*don't know how to deserialize __class__=%s)ÚpoprÖ   Ú__new__rå   rM   rm   r%   r&   ÚisabsrY   r'   ræ   rç   rv   )rê   ÚclsÚrr   r   r   Ú_json_decodes  s    

rò   c             C   sd   t |dƒ�P}ytj| |tdd� W n2 tk
rT } ztjdj|ƒƒ W Y dd}~X nX W dQ R X dS )a  
    Dumps a data structure as JSON in the named file.

    Handles FontManager and its fields.  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).
    rŠ   r   )rð   Úindentz$Could not save font_manager cache {}N)rž   ÚjsonÚdumprâ   r5   rU   rV   Úformat)Údatar+   r§   Úer   r   r   Ú	json_dump…  s
    rù   c             C   s$   t | dƒ�}tj|td�S Q R X dS )zç
    Loads a data structure as JSON from the named file.

    Handles FontManager and its fields.  Relative file paths are interpreted
    as being relative to the Matplotlib data path, and transformed into
    absolute paths.
    rñ   )Úobject_hookN)rž   rô   Úloadrò   )r+   r§   r   r   r   Ú	json_load”  s    rü   c             C   s   t | tƒr| g} | S )N)r>   r?   )r¼   r   r   r   r¬      s    
r¬   z3.0c               @   s4   e Zd ZdZdZdd„ Zd	d
„ Zdd„ Zdd„ ZdS )Ú	TempCachea>  
    A class to store temporary caches that are (a) not saved to disk
    and (b) invalidated whenever certain font-related
    rcParams---namely the family lookup lists---are changed or the
    font cache is reloaded.  This avoids the expensive linear search
    through all fonts every time a font is looked up.
    ú
font.serifúfont.sans-serifúfont.cursiveúfont.fantasyúfont.monospacec             C   s   i | _ | jƒ | _d S )N)Ú_lookup_cacheÚmake_rcparams_keyÚ_last_rcParams)rw   r   r   r   rx   µ  s    zTempCache.__init__c             C   s   t tƒgdd„ | jD ƒ S )Nc             S   s   g | ]}t | ‘qS r   )r	   )r    Úparamr   r   r   r"   »  s    z/TempCache.make_rcparams_key.<locals>.<listcomp>)ÚidÚfontManagerÚinvalidating_rcparams)rw   r   r   r   r  ¹  s    zTempCache.make_rcparams_keyc             C   s*   | j ƒ }|| jkri | _|| _| jj|ƒS )N)r  r  r  r’   )rw   r¨   rI   r   r   r   r’   ½  s
    
zTempCache.getc             C   s,   | j ƒ }|| jkri | _|| _|| j|< d S )N)r  r  r  )rw   r¨   rJ   rI   r   r   r   r;   Ä  s
    
zTempCache.setN)rþ   rÿ   r   r  r  )	r{   r|   r}   r~   r	  rx   r  r’   r;   r   r   r   r   rý   ¦  s    rý   c               @   sº   e Zd ZdZdZd&dd„Zejdƒedd	„ ƒƒZ	ejdƒe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„ Zd'd"d#„Zeƒ d$d%„ ƒZdS )(rÖ   a}  
    On import, the :class:`FontManager` singleton instance creates a
    list of TrueType fonts based on the font properties: name, style,
    variant, weight, stretch, and size.  The :meth:`findfont` method
    does a nearest neighbor search to find the font that most closely
    matches the specification.  If no good enough match is found, a
    default font is returned.
    i6  Nr   c             C   s  | j | _|| _|| _tjjtd ddƒtjjtd ddƒtjjtd ddƒg}xjdD ]b}|tjkrTtj| }|j	dƒd	krŒ|j
|jdƒƒ qT|j	d
ƒd	kr¬|j
|jd
ƒƒ qT|j|ƒ qTW tjdt|ƒƒ dddœ| _t|ƒtƒ  }t|ƒ| _t|dd�tdd� }t|dd�| _d S )NZdatapathr   r   r   ZpdfcorefontsÚTTFPATHÚAFMPATHú;r   ú:zfont search path %szDejaVu SansZ	Helvetica)r   r   )r   )r
  r  )Ú__version__Ú_versionÚ_FontManager__default_weightÚdefault_sizer%   r&   r'   r	   r6   r‰   Úextendr@   r¤   rU   r�   r?   ÚdefaultFamilyrl   rª   ÚttflistÚafmlist)rw   ru   rr   ÚpathsÚpathnameZttfpathÚttffilesÚafmfilesr   r   r   rx   Ú  s,    





zFontManager.__init__z3.0c             C   s   dd„ | j D ƒS )Nc             S   s   g | ]
}|j ‘qS r   )rY   )r    r—   r   r   r   r"   ÿ  s    z(FontManager.ttffiles.<locals>.<listcomp>)r  )rw   r   r   r   r  ü  s    zFontManager.ttffilesc             C   s   dd„ | j D ƒS )Nc             S   s   g | ]
}|j ‘qS r   )rY   )r    r—   r   r   r   r"     s    z(FontManager.afmfiles.<locals>.<listcomp>)r  )rw   r   r   r   r    s    zFontManager.afmfilesc                s   ‡ fdd„ˆ j jƒ D ƒS )Nc                s    i | ]\}}ˆ j ||d �|“qS ))r   )rÎ   )r    r!   r¼   )rw   r   r   ú
<dictcomp>
  s   z+FontManager.defaultFont.<locals>.<dictcomp>)r  rF   )rw   r   )rw   r   ÚdefaultFont  s    
zFontManager.defaultFontc             C   s   | j S )z1
        Return the default font weight.
        )r  )rw   r   r   r   Úget_default_weight  s    zFontManager.get_default_weightc               C   s   t d S )z/
        Return the default font size.
        z	font.size)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'.
        N)r  )rw   rr   r   r   r   Úset_default_weight  s    zFontManager.set_default_weightc             C   sÂ   t |ttfƒs|g}nt|ƒdkr&dS |jƒ }dt|ƒ }x‚t|ƒD ]v\}}|jƒ }|tkrª|d
krhd}td|  }dd	„ |D ƒ}||krº|j|ƒ}||t|ƒ  | S qD||krD|| S qDW dS )a'  
        Returns 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   g      ð?r9   r   ú
sans serifz
sans-serifzfont.c             S   s   g | ]}|j ƒ ‘qS r   )r$   )r    Úxr   r   r   r"   9  s    z,FontManager.score_family.<locals>.<listcomp>)r   r  )	r>   rÛ   r¿   Úlenr$   Ú	enumerateÚfont_family_aliasesr	   Úindex)rw   ZfamiliesZfamily2ÚstepÚiZfamily1ÚoptionsÚidxr   r   r   Úscore_family"  s&    
zFontManager.score_familyc             C   s$   ||krdS |dkr |dkr dS dS )zÇ
        Returns 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.
        g        r†   r…   gš™™™™™¹?g      ð?)r†   r…   )r†   r…   r   )rw   Zstyle1Zstyle2r   r   r   Úscore_styleC  s    
zFontManager.score_stylec             C   s   ||krdS dS dS )z~
        Returns a match score between *variant1* and *variant2*.

        An exact match returns 0.0, otherwise 1.0.
        g        g      ð?Nr   )rw   Zvariant1Zvariant2r   r   r   Úscore_variantT  s    zFontManager.score_variantc             C   sl   yt |ƒ}W n  tk
r,   tj|dƒ}Y nX yt |ƒ}W n  tk
rZ   tj|dƒ}Y nX t|| ƒd S )zó
        Returns 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.
        iô  g     @�@)rÓ   rv   rÔ   r’   Úabs)rw   Zstretch1Zstretch2Zstretchval1Zstretchval2r   r   r   Úscore_stretch_  s    zFontManager.score_stretchc             C   sT   t j||ƒrdS t|tƒr|nt| }t|tƒr4|nt| }dt|| ƒd  d S )ai  
        Returns 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.
        g        gffffffî?iè  gš™™™™™©?)r   Z
_str_equalr>   r   r•   r+  )rw   Zweight1Zweight2Zw1Zw2r   r   r   Úscore_weightq  s
    zFontManager.score_weightc             C   sn   |dkrdS yt |ƒ}W n" tk
r:   | jt|  }Y nX yt |ƒ}W n tk
r\   dS X t|| ƒd S )až  
        Returns 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�   g        g      ð?éH   )rt   rv   r  rÕ   r+  )rw   Zsize1Zsize2Zsizeval1Zsizeval2r   r   r   Ú
score_sizeƒ  s    zFontManager.score_sizer   Tc             C   s&   t dd„ dD ƒƒ}| j||||||ƒS )	a5  
        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'}, optional, 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 no match
            is found.

        Returns
        -------
        fontfile : 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| ƒV  qd S )N)r¿   r	   )r    rI   r   r   r   rŒ   Ñ  s    z'FontManager.findfont.<locals>.<genexpr>ú
font.serifúfont.sans-serifúfont.cursiveúfont.fantasyúfont.monospace)r0  r1  r2  r3  r4  )r¿   Ú_findfont_cached)rw   r¨   r   r.   Úfallback_to_defaultÚrebuild_if_missingÚ	rc_paramsr   r   r   rÎ   ›  s    6 

zFontManager.findfontc             C   sî  t |tƒst|ƒ}|jƒ }|d k	r&|S |dkr6| j}n| j}d}	d }
tjd|ƒ xÄ|D ]¼}|d k	rxt|ƒt|jƒj	krxqV| j
|jƒ |jƒd | j|jƒ |jƒ | j|jƒ |jƒ | j|jƒ |jƒ | j|jƒ |jƒ | j|jƒ |jƒ }tjd||ƒ ||	k �r|}	|}
|dkrVP qVW |
d k�s*|	dk�r�|�rptjd|jƒ | j| ƒ |jƒ }|j| j| ƒ | j |||d	ƒS tjd
|| j!| ƒ | j!| }ntjd||
j|
j|	ƒ |
j}t"j#j$|ƒ�sê|�râtj%dƒ t&ƒ  t'j |||dd	ƒS t(dƒ‚|S )Nr   gõù?éO8Mzfindfont: Matching %s.é
   zfindfont: score(%s) = %sr   g      $@z7findfont: Font family %s not found. Falling back to %s.Fz+findfont: Could not match %s. Returning %s.z2findfont: Matching %s to %s (%r) with score of %f.z7findfont: Found a missing font file.  Rebuilding cache.TzNo valid font could be found))r>   r«   rÅ   r  r  rU   r�   r   rY   Úparentsr(  rÀ   ro   r)  rÐ   rp   r*  rÂ   rq   r-  rš   rr   r,  rÃ   rs   r/  rÑ   ru   rV   r  rà   rµ   rÎ   r  r%   r&   Úisfiler    Ú_rebuildr  rv   )rw   r¨   r   r.   r6  r7  r8  rY   r¥   Z
best_scoreZ	best_fontr—   ZscoreZdefault_propÚresultr   r   r   r5  Ø  sZ    

p
zFontManager._findfont_cached)Nr   )r   NTT)r{   r|   r}   r~   r  rx   r   Ú
deprecatedÚpropertyr  r  r  r  Ústaticmethodr×   r  r(  r)  r*  r,  r-  r/  rÎ   r   r5  r   r   r   r   rÖ   Ì  s(   
"! 
<rÖ   c          	   C   sB   t jj| ƒd jƒ dkr:t| dƒ�}|jdƒdkS Q R X 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.
    r9   z.otfrœ   r„   s   OTTONF)r%   r&   Úsplitextr$   rž   Úread)r+   Úfdr   r   r   Úis_opentype_cff_font  s    rD  zfontlist-v{}.jsoné@   Úregister_at_fork)Zafter_in_childc             C   s   |d krt d }t| |ƒS )Nztext.hinting_factor)r	   Ú	_get_font)r+   Zhinting_factorr   r   r   rÍ   :  s    rÍ   c            
   C   s4   t ƒ atjtƒ� tttƒ W d Q R X tjdƒ d S )Nzgenerated new fontManager)rÖ   r  r   Z
_lock_pathÚ_fmcacherù   rU   r    r   r   r   r   r<  @  s    r<  r  z"Using fontManager instance from %s)Nr   )Nr   )r   )Nr   )r   )N)Sr~   Ú	functoolsr   rô   ÚloggingZnumbersr   r%   Zpathlibr   r\   rf   Ú	threadingr   ÚImportErrorÚdummy_threadingZ
matplotlibræ   r   r   r   r	   Zmatplotlib.fontconfig_patternr
   r   Ú	getLoggerr{   rU   rÕ   rÔ   r•   r"  r3   r<   r&   r'   r?   ÚhomerO   r6   r’   rh   rQ   r   r/   r8   rL   rP   r>  rS   ra   rb   rl   Úobjectrm   r˜   r›   rª   r«   râ   rò   rù   rü   r¬   rý   rÖ   rD  Zget_cachedirrö   r  rH  r  r¢   rG  ÚhasattrrF  Úcache_clearrÍ   r<  Ú	ExceptionrÜ   r�   rÎ   r   r   r   r   Ú<module>   sì   
4
	

VD
5  3%  T
