B
    c                 @   s   d Z ddlmZ ddlZddlZddlmZmZm	Z	m
Z
mZmZmZ dZede jZede jZdZede jZede jZG d	d
 d
Ze e jZdd Zdd ZdS )z
A module for parsing and generating `fontconfig patterns`_.

.. _fontconfig patterns:
   https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
    )	lru_cacheN)Literal
ZeroOrMoreOptionalRegex	StringEndParseExceptionSuppressz\\\-:,z\\([%s])z([%s])z\\=_:,c               @   s   e Zd ZdZddddddddddd	d
dddddddddddddZdd Zdd Zdd Zdd Zd d! Z	d"d# Z
d$d% Zd&d' Zd(d) Zd*S )+FontconfigPatternParserz
    A simple pyparsing-based parser for `fontconfig patterns`_.

    .. _fontconfig patterns:
       https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
    )weightlight)r   book)r   regular)r   normal)r   medium)r   demibold)r   semibold)r   bold)r   z
extra bold)r   black)r   heavy)slantr   )r   italic)r   oblique)widthzultra-condensed)r   zextra-condensed)r   	condensed)r   zsemi-condensed)r   expanded)r   zextra-expanded)r   zultra-expanded)ZthinZ
extralightZ
ultralightr   r   r   r   r   r   r   r   Z	extraboldr   r   Zromanr   r   ZultracondensedZextracondensedr   Zsemicondensedr   ZextraexpandedZultraexpandedc       	      C   s   t dttf | j}t d| j}t d| j}t dttf | j}|tt	d|  | j
}|tt	d|  | j}|tt	d | ttt	d|  |B | j}t|tt	d|  tt	d|  t  }|| _t| _d S )Nz([^%s]|(\\[%s]))*z([0-9]+\.?[0-9]*|\.[0-9]+)z[a-z]+,=-:)r   family_puncZsetParseAction_family_size_name
value_punc_valuer   r   	_families_point_sizesr	   	_propertyr   r   _parserr   )	selffamilysizenamevalueZfamiliesZpoint_sizespropertypattern r1   <lib/python3.7/site-packages/matplotlib/fontconfig_pattern.py__init__>   s<    


.z FontconfigPatternParser.__init__c          
   C   sf   i  }| _ y| j| W n6 | jk
rP } ztd||f |W dd}~X Y nX d| _ | j  |S )z
        Parse the given fontconfig *pattern* and return a dictionary
        of key/value pairs useful for initializing a
        `.font_manager.FontProperties` object.
        z$Could not parse font string: '%s'
%sN)_propertiesr)   ZparseStringr   
ValueErrorZ
resetCache)r*   r0   propser1   r1   r2   parsew   s    
"
zFontconfigPatternParser.parsec             C   s   t dt|d gS )Nz\1r   )family_unescapestr)r*   sloctokensr1   r1   r2   r!      s    zFontconfigPatternParser._familyc             C   s   t |d gS )Nr   )float)r*   r;   r<   r=   r1   r1   r2   r"      s    zFontconfigPatternParser._sizec             C   s   t |d gS )Nr   )r:   )r*   r;   r<   r=   r1   r1   r2   r#      s    zFontconfigPatternParser._namec             C   s   t dt|d gS )Nz\1r   )value_unescaper:   )r*   r;   r<   r=   r1   r1   r2   r%      s    zFontconfigPatternParser._valuec             C   s   dd |D | j d< g S )Nc             S   s   g | ]}t |qS r1   )r:   ).0xr1   r1   r2   
<listcomp>   s    z5FontconfigPatternParser._families.<locals>.<listcomp>r+   )r4   )r*   r;   r<   r=   r1   r1   r2   r&      s    z!FontconfigPatternParser._familiesc             C   s   dd |D | j d< g S )Nc             S   s   g | ]}t |qS r1   )r:   )r@   rA   r1   r1   r2   rB      s    z8FontconfigPatternParser._point_sizes.<locals>.<listcomp>r,   )r4   )r*   r;   r<   r=   r1   r1   r2   r'      s    z$FontconfigPatternParser._point_sizesc             C   sn   t |dkrB|d | jkrj| j|d  \}}| j|g | n(|d }|dd  }| j|g | g S )N   r   )len
_constantsr4   
setdefaultappendextend)r*   r;   r<   r=   keyvalr1   r1   r2   r(      s    z!FontconfigPatternParser._propertyN)__name__
__module____qualname____doc__rE   r3   r8   r!   r"   r#   r%   r&   r'   r(   r1   r1   r1   r2   r
      sD   9r
   c                s2   t | rt| tr| g} d fdd| D S )z
    Given a string value or a list of string values, run each value through
    the input escape function to make the values into legal font config
    strings.  The result is returned as a string.
    r   c             3   s$   | ]}|d k	r dt |V  qd S )Nz\\\1)r:   )r@   rA   )escape_funcr1   r2   	<genexpr>   s    z_escape_val.<locals>.<genexpr>)npiterable
isinstancer:   join)rJ   rO   r1   )rO   r2   _escape_val   s    rU   c             C   s|   g }|   }|dk	r,|g kr,|t|t xDdD ]<}t| d|  }|dk	r2|g kr2|d|t|tf  q2W d|S )z[
    Given a dictionary of key/value pairs, generates a fontconfig
    pattern string.
    N)ZstyleZvariantr   Zstretchfiler,   Zget_z:%s=%s )Z
get_familyrG   rU   family_escapegetattrvalue_escaperT   )dr6   r+   rI   rJ   r1   r1   r2   generate_fontconfig_pattern   s    
r\   )rN   	functoolsr   reZnumpyrQ   Z	pyparsingr   r   r   r   r   r   r	   r    compilesubr9   rX   r$   r?   rZ   r
   r8   Zparse_fontconfig_patternrU   r\   r1   r1   r1   r2   <module>   s   $ 