3
ƽh                 @   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jde jZejde jZdZejde jZej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/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDd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 )EFontconfigPatternParserz
    A simple pyparsing-based parser for `fontconfig patterns`_.

    .. _fontconfig patterns:
       https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
    weightlightbookregularnormalmediumdemiboldsemiboldbold
extra boldblackheavyslantitalicobliquewidthultra-condensedextra-condensed	condensedsemi-condensedexpandedextra-expanded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| j}t dj| j}t dj| j}t dttf j| j}|tt	d|  j| j
}|tt	d|  j| j}|tt	d | ttt	d|  |B j| 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 r8   C/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/fontconfig_pattern.py__init__>   s<    


.z FontconfigPatternParser.__init__c             C   sf   i  }| _ y| jj| W n6 | jk
rP } ztd||f |W Y dd}~X nX d| _ | j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)_propertiesr0   parseStringr   
ValueError
resetCache)r1   r7   propser8   r8   r9   parsew   s    
"
zFontconfigPatternParser.parsec             C   s   t dt|d gS )Nz\1r   )family_unescapestr)r1   sloctokensr8   r8   r9   r(      s    zFontconfigPatternParser._familyc             C   s   t |d gS )Nr   )float)r1   rD   rE   rF   r8   r8   r9   r)      s    zFontconfigPatternParser._sizec             C   s   t |d gS )Nr   )rC   )r1   rD   rE   rF   r8   r8   r9   r*      s    zFontconfigPatternParser._namec             C   s   t dt|d gS )Nz\1r   )value_unescaperC   )r1   rD   rE   rF   r8   r8   r9   r,      s    zFontconfigPatternParser._valuec             C   s   dd |D | j d< g S )Nc             S   s   g | ]}t |qS r8   )rC   ).0xr8   r8   r9   
<listcomp>   s    z5FontconfigPatternParser._families.<locals>.<listcomp>r2   )r;   )r1   rD   rE   rF   r8   r8   r9   r-      s    z!FontconfigPatternParser._familiesc             C   s   dd |D | j d< g S )Nc             S   s   g | ]}t |qS r8   )rC   )rI   rJ   r8   r8   r9   rK      s    z8FontconfigPatternParser._point_sizes.<locals>.<listcomp>r3   )r;   )r1   rD   rE   rF   r8   r8   r9   r.      s    z$FontconfigPatternParser._point_sizesc             C   sn   t |dkrB|d | jkrj| j|d  \}}| jj|g j| n(|d }|dd  }| jj|g j| g S )N   r   )len
_constantsr;   
setdefaultappendextend)r1   rD   rE   rF   keyvalr8   r8   r9   r/      s    z!FontconfigPatternParser._propertyN)r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r    )r   r!   )__name__
__module____qualname____doc__rN   r:   rA   r(   r)   r*   r,   r-   r.   r/   r8   r8   r8   r9   r
      sD   9r
   c                s4   t j|  st| tr| g} dj 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)rC   )rI   rJ   )escape_funcr8   r9   	<genexpr>   s    z_escape_val.<locals>.<genexpr>)npiterable
isinstancerC   join)rS   rX   r8   )rX   r9   _escape_val   s    r^   c             C   s|   g }| j  }|dk	r,|g kr,|jt|t xDdD ]<}t| d|  }|dk	r2|g kr2|jd	|t|tf  q2W d
j|S )z[
    Given a dictionary of key/value pairs, generates a fontconfig
    pattern string.
    Nstylevariantr   stretchfiler3   get_z:%s=%s )r_   r`   r   ra   rb   r3   )Z
get_familyrP   r^   family_escapegetattrvalue_escaper]   )dr?   r2   rR   rS   r8   r8   r9   generate_fontconfig_pattern   s    
ri   )rW   	functoolsr   renumpyrZ   	pyparsingr   r   r   r   r   r   r	   r&   compilesubrB   re   r+   rH   rg   r
   rA   Zparse_fontconfig_patternr^   ri   r8   r8   r8   r9   <module>   s   $ 