B
    ]t\                 @   s   d Z ddlm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
eZe e jZdd ZdS )z
A module for parsing and generating fontconfig patterns.

See the `fontconfig pattern specification
<https://www.freedesktop.org/software/fontconfig/fontconfig-user.html>`_ for
more information.
    )	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 )+FontconfigPatternParserzA simple pyparsing-based parser for fontconfig-style patterns.

    See the `fontconfig pattern specification
    <https://www.freedesktop.org/software/fontconfig/fontconfig-user.html>`_
    for more information.
    )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valuefamiliesZpoint_sizespropertypattern r2   <lib/python3.7/site-packages/matplotlib/fontconfig_pattern.py__init__A   s8    



.z FontconfigPatternParser.__init__c          
   C   sd   i  }| _ y| j| W n4 | jk
rN } 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
        :class:`font_manager.FontProperties` object.
        z$Could not parse font string: '%s'
%sN)_propertiesr)   ZparseStringr   
ValueErrorZ
resetCache)r*   r1   propser2   r2   r3   parseq   s    
 
zFontconfigPatternParser.parsec             C   s   t dt|d gS )Nz\1r   )family_unescapestr)r*   sloctokensr2   r2   r3   r!      s    zFontconfigPatternParser._familyc             C   s   t |d gS )Nr   )float)r*   r<   r=   r>   r2   r2   r3   r"      s    zFontconfigPatternParser._sizec             C   s   t |d gS )Nr   )r;   )r*   r<   r=   r>   r2   r2   r3   r#      s    zFontconfigPatternParser._namec             C   s   t dt|d gS )Nz\1r   )value_unescaper;   )r*   r<   r=   r>   r2   r2   r3   r%      s    zFontconfigPatternParser._valuec             C   s   dd |D | j d< g S )Nc             S   s   g | ]}t |qS r2   )r;   ).0xr2   r2   r3   
<listcomp>   s    z5FontconfigPatternParser._families.<locals>.<listcomp>r+   )r5   )r*   r<   r=   r>   r2   r2   r3   r&      s    z!FontconfigPatternParser._familiesc             C   s   dd |D | j d< g S )Nc             S   s   g | ]}t |qS r2   )r;   )rA   rB   r2   r2   r3   rC      s    z8FontconfigPatternParser._point_sizes.<locals>.<listcomp>r,   )r5   )r*   r<   r=   r>   r2   r2   r3   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
_constantsr5   
setdefaultappendextend)r*   r<   r=   r>   keyvalr2   r2   r3   r(      s    z!FontconfigPatternParser._propertyN)__name__
__module____qualname____doc__rF   r4   r9   r!   r"   r#   r%   r&   r'   r(   r2   r2   r2   r3   r
      sD   0r
   c             C   s   g }d}d}xnd  D ]b}t| d|  }|dk	r|g krt|tkrfdd |D }|g krfd|}|d||f  qW d|S )	z[
    Given a dictionary of key/value pairs, generates a fontconfig
    pattern string.
     z-family style variant weight stretch file sizeZget_Nc             S   s"   g | ]}|d k	rt dt|qS )Nz\\\1)value_escaper;   )rA   rB   r2   r2   r3   rC      s    z/generate_fontconfig_pattern.<locals>.<listcomp>r   z:%s=%s)splitgetattrtypelistjoinrH   )dr7   r/   r,   rJ   rK   r2   r2   r3   generate_fontconfig_pattern   s    
rX   )rO   	functoolsr   reZ	pyparsingr   r   r   r   r   r   r	   r    compilesubr:   Zfamily_escaper$   r@   rQ   objectr
   r9   Zparse_fontconfig_patternrX   r2   r2   r2   r3   <module>   s   	$ 