B
    Mô`û  ã               @   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_puncÚsetParseActionÚ_familyÚ_sizeÚ_nameÚ
value_puncÚ_valuer   r   Ú	_familiesÚ_point_sizesr	   Ú	_propertyr   r   Ú_parserr   )	ÚselfÚfamilyÚsizeÚnameÚvalueZfamiliesZpoint_sizesÚpropertyÚpattern© r2   úA/tmp/pip-unpacked-wheel-0c1wfn4v/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*   ÚparseStringr   Ú
ValueErrorÚ
resetCache)r+   r1   ÚpropsÚer2   r2   r3   Úparsew   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=   )rC   rD   r2   r2   r3   rE   ›   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__rH   r4   r;   r"   r#   r$   r&   r'   r(   r)   r2   r2   r2   r3   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=   )rC   rD   )Úescape_funcr2   r3   ú	<genexpr>º   s    z_escape_val.<locals>.<genexpr>)ÚnpÚiterableÚ
isinstancer=   Újoin)rM   rR   r2   )rR   r3   Ú_escape_val±   s    rX   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)ÚstyleÚvariantr   ZstretchÚfiler-   Úget_z:%s=%sÚ )Z
get_familyrJ   rX   Úfamily_escapeÚgetattrÚvalue_escaperW   )Údr9   r,   rL   rM   r2   r2   r3   Úgenerate_fontconfig_pattern¾   s    
rb   )rQ   Ú	functoolsr   ÚreZnumpyrT   Ú	pyparsingr   r   r   r   r   r   r	   r    ÚcompileÚsubr<   r^   r%   rB   r`   r
   r;   Zparse_fontconfig_patternrX   rb   r2   r2   r2   r3   Ú<module>   s   $ 