B
    ]t\                 @   s  d Z ddlmZ ddlZddl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
rt   ddlmZ Y nX 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mZ eeZd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"d&d'd(d)d*gZ#d+d,d-d.gZ$ese	j%d/kre$&e'e( d0  e#&e'e( d1  d2d3 Z)d4d5 Z*d6d7 Z+dkd9d:Z,dld;d<Z-e d=d> Z.dmd?d@Z/dndAdBZ0G dCdD dDe1Z2dEdF Z3dGdH Z4dodIdJZ5G dKdL dLe1Z6G dMdN dNej7Z7dOdP Z8dQdR Z9dSdT Z:dUdV Z;e<dWG dXdY dYe1Z=G dZd[ d[e1Z>e d\d] Z?da@dZAed^ejBZCdpd_d`ZDere	j%d/krdadb ZEi ZFdqdcddZGndZAe ZHeHdk	rejIJeHdeKe>jLZAda@dfdg ZMeArxy@e:eAa@eNt@dhr&t@jOe>jLkr.eM  ndt@_PeQdieA W n2 eRk
rZ    Y n eSk
rt   eM  Y nX neM  djdd ZGdS )ra  
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.

Experimental support is included for using `fontconfig` on Unix
variant platforms (Linux, OS X, Solaris).  To enable it, set the
constant ``USE_FONTCONFIG`` in this file to ``True``.  Fontconfig has
the advantage that it is the standard way to look up fonts on X11
platforms, so if a font is installed, it is much more likely to be
found.
    )	lru_cacheN)Path)Timer)afmcbookft2fontrcParamsget_cachedir)parse_fontconfig_patterngenerate_fontconfig_patternFgI+?gh|?5?g-?g      ?g333333?g
ףp=
?gS?)
zxx-smallzx-smallZsmallmediumZlargezx-largezxx-largeZlargerZsmallerNd      i,  i  i  iX  i  i   i  )	zultra-condensedzextra-condensed	condensedzsemi-condensednormalzsemi-expandedexpandedzextra-expandedzultra-expanded)Z
ultralightZ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/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/z/Library/Fonts/z/Network/Library/Fonts/z/System/Library/Fonts/z/opt/local/share/fontswin32zLibrary/Fontsz.fontsc             C   s   dddd|  S )zs
    Return a list of file extensions extensions that are synonyms for
    the given file extension *fileext*.
    )ttfotf)r   )r   r   r    )fontextr   r   6lib/python3.7/site-packages/matplotlib/font_manager.pyget_fontext_synonyms   s    r   c                s&   dd  D   fddt | 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  krt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t}| |dd S Q R X W n$ tk
rV   t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	MSFoldersZQueryValueExOSErrorr!   r"   r#   environ)r,   userr   r   r   win32FontDirectory   s    	r3   r   c             C   s  ddl }| dkrt } dd t|D }t }xtD ]}y||j|}xt||d D ]|}|	||\}}}	t
|tsq^|ddd }yt| | }
W n ttfk
r   w^Y nX |
j |kr^|t|
 q^W W dQ R X W q4 ttfk
r   w4Y q4X q4W t|S )z
    Search for fonts in the specified font directory, or use the
    system directories if none given.  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>    )r,   r3   r   setMSFontDirectoriesr-   HKEY_LOCAL_MACHINErangeZQueryInfoKeyZ	EnumValue
isinstancestrsplitr   ZresolveFileNotFoundErrorRuntimeErrorr   r    addr0   MemoryErrorlist)r*   r   r,   itemsZfontdirZlocaljkeyZdirectpr"   r   r   r   win32InstalledFonts   s,    

 rF   c                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)Zdirectoriesr   r   )r   r   OSXInstalledFonts   s    
rH   c           	   C   sh   t ddd } |   z2ytddg}W n ttjfk
rD   g S X W d|   X dd |d	D S )
z:Cache and list the font filenames known to `fc-list`.
       c               S   s
   t dS )NzLMatplotlib is building the font cache using fc-list. This may take a moment.)warningswarnr   r   r   r   <lambda>   s   z_call_fc_list.<locals>.<lambda>zfc-listz--format=%{file}\nNc             S   s   g | ]}t |qS r   )r!   fsdecode)r   fnamer   r   r   r      s    z!_call_fc_list.<locals>.<listcomp>   
)r   start
subprocessZcheck_outputr0   ZCalledProcessErrorZcancelr<   )Ztimeroutr   r   r   _call_fc_list   s    

rS   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  kr|qS r   )r   r   r    )r   rN   )r   r   r   r      s    )r   rS   )r   r   )r   r   get_fontconfig_fonts   s    rT   c             C   s   t  }t|}| dkrhtjdkr:t g} |t|d qxt} |t| tjdkrx|t	|d nt
| trx| g} x&| D ]}|ttjjt|| q~W 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.
    Nr   )r   darwinc             S   s   g | ]}t j|r|qS r   )r!   r"   exists)r   rN   r   r   r   r     s    z#findSystemFonts.<locals>.<listcomp>)r6   r   sysplatformr3   updaterF   X11FontDirectoriesrT   rH   r:   r;   mapr!   r"   abspathr+   )Z	fontpathsr   	fontfilesfontextsr"   r   r   r   findSystemFonts   s    



r_   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)
rN   namestylevariantweightstretchr;   floatsize
ValueError)selfrN   rb   rc   rd   re   rf   rh   r   r   r   __init__!  s    	zFontEntry.__init__c             C   s(   d| j tj| j| j| j| j| jf S )Nz<Font '%s' (%s) %s %s %s %s>)	rb   r!   r"   basenamerN   rc   rd   re   rf   )rj   r   r   r   __repr__5  s    zFontEntry.__repr__N)ra   ra   r   r   r   r   r   )__name__
__module____qualname____doc__rk   rm   r   r   r   r   r`     s         
r`   c       	         st  | j }|  }|ddd }|ddd   ddkrNd}n> ddkrbd}n*|ddkrvd	}n| jtj@ rd}nd	}| d
krd}nd	}t	 fddt
D d}|s| jtj@ rd}nd} ddks ddks ddkr
d}n@ ddkr d}n* ddks@ ddkrFd}nd	}| jsZt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.

    )r4   r   r          zlatin-1)r4   r   r      obliquer   italicr   r   )capitalsz
small-capsz
small-capsc             3   s    | ]}  |d kr|V  qdS )r   N)find)r   w)sfnt4r   r   	<genexpr>g  s    z"ttfFontProperty.<locals>.<genexpr>Ni  i  narrowr   condz	demi condzsemi-condensedwider   z$Non-scalable fonts are not supportedscalable)family_nameZget_sfntgetdecoder    rx   Zstyle_flagsr   ZITALICnextweight_dictZBOLDr   NotImplementedErrorr`   rN   )	fontrb   ZsfntZsfnt2rc   rd   re   rf   rh   r   )rz   r   ttfFontProperty;  sB    	  	r   c       	      C   s   |  }|  }| dks,d| kr2d}nd| krDd}nd}| dkrZd}nd}|  }d|krxd}n0d	|ksd
|krd}nd|ksd|k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   rv   ru   r   )rw   z
small-capsz
small-capsz	demi condzsemi-condensedr|   r}   r   r~   r   r   )Zget_familynameZget_fontnamer    Z	get_angle
get_weightr`   )	Zfontpathr   rb   Zfontnamerc   rd   re   rf   rh   r   r   r   afmFontProperty  s(    r   c       	      C   s  g }t  }x| D ]|}td| tj|d }||kr@qn
|| |dkry$t|d}t	|}W dQ R X W nB t
k
r   td| wY n" tk
r   td| wY nX yt||}W n tk
r   wY nX nyt|}W nf tk
r   td| wY nD tk
r4   td wY n$ tk
rV   td	| wY nX yt|}W n  ttttfk
r   wY nX || 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: %sr4   r   rbNzCould not open font file %szCould not parse font file %szCannot handle unicode filenamesz#IO error - cannot open font file %s)r6   _logdebugr!   r"   r<   r?   openr   ZAFMEnvironmentErrorinfor>   r   KeyErrorr   FT2FontUnicodeErrorr0   r   ri   r   append)	r]   r   fontlistseenZfpathrN   fhr   propr   r   r   createFontList  sN    


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
    <https://www.freedesktop.org/wiki/Software/fontconfig/>`_ pattern, if it is
    the only argument provided.  See the documentation on `fontconfig patterns
    <https://www.freedesktop.org/software/fontconfig/fontconfig-user.html>`_.
    This support does not require fontconfig to be installed.  We are merely
    borrowing its pattern syntax for use here.

    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 | _|d k	r`| j	
|j	 d S t|tr|d kr|d kr|d kr|d kr|d kr|d kr| | d S | | | | | | | | | | | | | | d S )Nzfont.familyz
font.stylezfont.variantzfont.weightzfont.stretchz	font.size)_normalize_font_familyr   _family_slant_variant_weight_stretch_size_file__dict__rY   r:   r;   set_fontconfig_pattern
set_family	set_styleset_variant
set_weightset_stretchset_fileset_size)	rj   familyrc   rd   re   rf   rh   rN   _initr   r   r   rk   ?  s4    













zFontProperties.__init__c             C   s   t |S )N)r
   )rj   patternr   r   r   _parse_fontconfig_patternj  s    z(FontProperties._parse_fontconfig_patternc             C   s:   t |  |  |  |  |  |  |  f}t|S )N)	tuple
get_family	get_slantget_variantr   get_stretchget_size_in_pointsget_filehash)rj   lr   r   r   __hash__m  s    

zFontProperties.__hash__c             C   s   t | t |kS )N)r   )rj   otherr   r   r   __eq__w  s    zFontProperties.__eq__c             C   s   |   S )N)get_fontconfig_pattern)rj   r   r   r   __str__z  s    zFontProperties.__str__c             C   s   | j S )zL
        Return a list of font names that comprise the font family.
        )r   )rj   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   )rj   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   )rj   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   )rj   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   )rj   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   )rj   r   r   r   r     s    zFontProperties.get_stretchc             C   s   | j S )z'
        Return the font size.
        )r   )rj   r   r   r   get_size  s    zFontProperties.get_sizec             C   s   | j S )N)r   )rj   r   r   r   r     s    z!FontProperties.get_size_in_pointsc             C   s   | j S )z=
        Return the filename of the associated font.
        )r   )rj   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.

        See the documentation on `fontconfig patterns
        <https://www.freedesktop.org/software/fontconfig/fontconfig-user.html>`_.

        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   )rj   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   )rj   r   r   r   r   r     s    zFontProperties.set_familyc             C   s*   |dkrt d }|dkr td|| _dS )zS
        Set the font style.  Values are: 'normal', 'italic' or 'oblique'.
        Nz
font.style)r   rv   ru   z'style must be normal, italic or oblique)r   ri   r   )rj   rc   r   r   r   r     s
    zFontProperties.set_stylec             C   s*   |dkrt d }|dkr td|| _dS )zN
        Set the font variant.  Values are: 'normal' or 'small-caps'.
        Nzfont.variant)r   z
small-capsz$variant must be normal or small-caps)r   ri   r   )rj   rd   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   intri   r   r   )rj   re   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   ri   stretch_dictr   )rj   rf   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ttt Y nX |t	  }Y nX |dk rt
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   rg   ri   font_scalingsr   r#   r[   r;   FontManagerget_default_sizer   r   r   )rj   rh   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   )rj   filer   r   r   r   #  s    zFontProperties.set_filec             C   sV   xP|  | D ]>\}}t|tkr<t| d| |d  qt| d| | qW dS )a}  
        Set the properties by parsing a fontconfig *pattern*.

        See the documentation on `fontconfig patterns
        <https://www.freedesktop.org/software/fontconfig/fontconfig-user.html>`_.

        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.
        Zset_r   N)r   rB   typerA   getattr)rj   r   rD   valr   r   r   r   *  s    z%FontProperties.set_fontconfig_patternc             C   s
   t | dS )zReturn a deep copy of self)r   )r   )rj   r   r   r   copy;  s    zFontProperties.copy)NNNNNNNN)rn   ro   rp   rq   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zset_namer   Z	set_slantr   r   r   r   r   r   r   r   r   r   r   r     sF   9       
#
		
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 t	 |d< W n t
k
rf   Y nX |S t |S d S )Nr   )	__class__r`   rN   )r:   r   dictr   r`   r;   r   Zrelative_tomplget_data_pathri   superdefault)rj   od)r   r   r   r   A  s    

 zJSONEncoder.default)rn   ro   rp   r   __classcell__r   r   )r   r   r   @  s   r   c             C   s   |  dd }|d k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   rY   r`   r!   r"   isabsrN   r#   r   r   ri   )r   clsrr   r   r   _json_decodeR  s    

r   c             C   sd   t |dP}ytj| |tdd W n2 tk
rT } ztd| W dd}~X Y 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).
    ry   rr   )r   indentz$Could not save font_manager cache {}N)r   jsondumpr   r0   rJ   rK   format)datar'   r   er   r   r   	json_dumpd  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   )Zobject_hookN)r   r   loadr   )r'   r   r   r   r   	json_loads  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.
    )z
font.serifzfont.sans-serifzfont.cursivezfont.fantasyzfont.monospacec             C   s   i | _ |  | _d S )N)_lookup_cachemake_rcparams_key_last_rcParams)rj   r   r   r   rk     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   Zparamr   r   r   r     s    z/TempCache.make_rcparams_key.<locals>.<listcomp>)idfontManagerinvalidating_rcparams)rj   r   r   r   r     s    zTempCache.make_rcparams_keyc             C   s*   |   }|| jkri | _|| _| j|S )N)r   r   r   r   )rj   r   rD   r   r   r   r     s
    
zTempCache.getc             C   s,   |   }|| jkri | _|| _|| j|< d S )N)r   r   r   )rj   r   valuerD   r   r   r   r6     s
    
zTempCache.setN)	rn   ro   rp   rq   r   rk   r   r   r6   r   r   r   r   r     s   r   c               @   s   e Zd ZdZdZd&ddZeeddd	 Z	eed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 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.
    i,  Nr   c             C   sR  | j | _|| _|| _tjtd ddtjtd ddtjtd ddg}xjdD ]b}|tjkrTtj| }|	ddkr|
|d qT|	d	dkr|
|d	 qT|| qTW td
t| ddd| _i | _t|t  }tdd |D |d | jd< t|| _t|ddtdd }t|dd| _|rD|d nd | jd< d S )NZdatapathZfontsr   r   Zpdfcorefonts)ZTTFPATHZAFMPATH;r   :zfont search path %szDejaVu SansZ	Helvetica)r   r   c             s   s    | ]}|  d r|V  qdS )zdejavusans.ttfN)r    endswith)r   rN   r   r   r   r{     s    z'FontManager.__init__.<locals>.<genexpr>)r   )__version___version_FontManager__default_weightdefault_sizer!   r"   r#   r   r1   rx   extendr<   r   r   r   r;   defaultFamilydefaultFontr_   r   r   ttflistafmlist)rj   rh   re   pathspathnameZttfpathttffilesafmfilesr   r   r   rk     s6    





zFontManager.__init__z3.0c             C   s   dd | j D S )Nc             S   s   g | ]
}|j qS r   )rN   )r   r   r   r   r   r     s    z(FontManager.ttffiles.<locals>.<listcomp>)r   )rj   r   r   r   r     s    zFontManager.ttffilesc             C   s   dd | j D S )Nc             S   s   g | ]
}|j qS r   )rN   )r   r   r   r   r   r     s    z(FontManager.afmfiles.<locals>.<listcomp>)r   )rj   r   r   r   r     s    zFontManager.afmfilesc             C   s   | j S )z1
        Return the default font weight.
        )r   )rj   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   )rj   re   r   r   r   set_default_weight  s    zFontManager.set_default_weightc             C   s   t dS )zd
        Update the font dictionary with new font files.
        Currently not implemented.
        N)r   )rj   r&   r   r   r   update_fonts  s    zFontManager.update_fontsc             C   s   t |ttfs|g}nt|dkr&dS | }dt| }xt|D ]v\}}| }|tkr|dkrhd}td|  }dd |D }||kr||}||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      ?r4   )r   z
sans serifz
sans-serifzfont.c             S   s   g | ]}|  qS r   )r    )r   xr   r   r   r     s    z,FontManager.score_family.<locals>.<listcomp>)	r:   rA   r   lenr    	enumeratefont_family_aliasesr   index)rj   ZfamiliesZfamily2stepiZfamily1Z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        )rv   ru   g?g      ?r   )rj   Zstyle1Zstyle2r   r   r   score_style)  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   )rj   Zvariant1Zvariant2r   r   r   score_variant:  s    zFontManager.score_variantc             C   sl   yt |}W n  tk
r,   t|d}Y nX yt |}W n  tk
rZ   t|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   ri   r   r   abs)rj   Zstretch1Zstretch2Zstretchval1Zstretchval2r   r   r   score_stretchE  s    zFontManager.score_stretchc             C   s   t |tr t |tr ||kr dS yt|}W n  tk
rL   t|d}Y nX yt|}W n  tk
rz   t|d}Y nX 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        i  gffffff?g     @@g?)r:   r;   r   ri   r   r   r  )rj   Zweight1Zweight2Z
weightval1Z
weightval2r   r   r   score_weightW  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      ?g      R@)rg   ri   r   r   r  )rj   Zsize1Zsize2Zsizeval1Zsizeval2r   r   r   
score_sizer  s    zFontManager.score_sizer   Tc             C   s&   t dd dD }| ||||||S )a  
        Search the font list for the font that most closely matches
        the :class:`FontProperties` *prop*.

        :meth:`findfont` 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.

        `directory`, is specified, will only return fonts from the
        given directory (or subdirectory of that directory).

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

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

        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.
        c             s   s   | ]}t t| V  qd S )N)r   r   )r   rD   r   r   r   r{     s    z'FontManager.findfont.<locals>.<genexpr>)z
font.serifzfont.sans-serifzfont.cursivezfont.fantasyzfont.monospace)r   _findfont_cached)rj   r   r   r*   fallback_to_defaultrebuild_if_missing	rc_paramsr   r   r   r     s    
zFontManager.findfontc             C   s  t |tst|}| }|d k	r2td| |S |dkrB| j}n| j}d}	d }
x|D ]}|d k	rxt|t|jj	krxqV| 
| |jd | | |j | | |j | | |j | | |j | | |j }||	k r|}	|}
|dkrVP qVW |
d ks|	dkr|rdtd| | j| f  | }| | j|  | !|||dS td|| j"| f t# | j"| }ntd	||
j|
j|	 |
j}t$j%&|s|rt'd
 t(  t)!|||ddS t*d|S )Nzfindfont returning %sr   g?O8Mg      $@r   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   r   r   r   rN   parentsr  r   rb   r	  r   rc   r
  r   rd   r  r   re   r  r   rf   r  r   rh   rJ   rK   r   r   r   r   r   UserWarningr!   r"   isfiler   _rebuildr   ri   )rj   r   r   r*   r  r  r  rN   r   Z
best_scoreZ	best_fontr   ZscoreZdefault_propresultr   r   r   r    s\    

\zFontManager._findfont_cached)Nr   )r   NTT)rn   ro   rp   rq   r   rk   propertyr   
deprecatedr   r   r   staticmethodr   r   r   r  r	  r
  r  r  r  r   r   r  r   r   r   r   r     s(   
(
! 
#r   c          	   C   sB   t j| d  dkr:t| d}|ddkS Q R X ndS dS )z
    Returns True if 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.
    r4   z.otfr   rt   s   OTTONF)r!   r"   splitextr    r   read)r'   fdr   r   r   is_opentype_cff_font  s    r  @   c             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   s   t |}d| }y,tjddd| gtjtjd}| d }W n tk
rP   d S X |jdkrx:ttj	|
dD ]$}tj|d dd  |krp|S qpW d S )	Nr   zfc-matchz-sz--format=%{file}\n)stdoutstderrr   rO   r4   )r   rQ   PopenPIPEZcommunicater0   
returncoder[   r!   rM   r<   r"   r  )r   r   r^   r   pipeoutputrN   r   r   r   fc_match  s    


r(  c             C   sP   t | ts|  } t| }|d k	r(|S t| |}|d krDtd|}|t| < |S )Nr   )r:   r;   r   _fc_match_cacher   r(  )r   r   cachedr  r   r   r   r   (  s    



r   zfontlist-v{}.jsonc            	   C   s8   t  atr*tt ttt W d Q R X td d S )Nzgenerated new fontManager)r   r   _fmcacher   Z
_lock_pathr   r   r   r   r   r   r   r  @  s
    r  r   z"Using fontManager instance from %sc             K   s   t j| f|}|S )N)r   r   )r   kwr   r   r   r   r   Z  s    )Nr   )Nr   )r   )Nr   )r   )N)r   )Trq   	functoolsr   r   Zloggingr!   Zpathlibr   rQ   rW   Z	threadingr   ImportErrorZdummy_threadingrJ   Z
matplotlibr   r   r   r   r   r	   Zmatplotlib.fontconfig_patternr
   r   Z	getLoggerrn   r   ZUSE_FONTCONFIGr   r   r   r  r/   r7   rZ   rG   rX   r   r;   homer   r+   r3   rF   rH   rS   rT   r_   objectr`   r   r   r   r   r   r   r   r   r   r  r   r   r  r   r+  r   r   r   r(  r)  r   Zcachedirr"   r#   r   r   r  hasattrr   r   r   TimeoutError	Exceptionr   r   r   r   <module>   s   


&
	

 RC
4  >%  L


