o
    :a                     @   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i 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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/d0 Zd1d2 Z	d3d4 Z
d5d6 Zd7d8 Zd9d: Zd;S )<FontconfigPatternParserz
    A simple pyparsing-based parser for `fontconfig patterns`_.

    .. _fontconfig patterns:
       https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
    Zthin)weightlightZ
extralightZ
ultralightr   book)r   r   regular)r   r   normal)r   r   medium)r   r   demibold)r   r   semibold)r   r   bold)r   r   Z	extrabold)r   z
extra boldblack)r   r   heavy)r   r   Zroman)slantr   italic)r   r   oblique)r   r   )widthzultra-condensed)r   zextra-condensed)r   	condensed)r   zsemi-condensed)r   expanded)r   zextra-expanded)r   zultra-expanded)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   ?/usr/lib/python3/dist-packages/matplotlib/fontconfig_pattern.py__init__>   s   




z FontconfigPatternParser.__init__c              
   C   s\   i  }| _ z| j| W n | jy# } z	td||f |d}~ww 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      t dt|d gS Nz\1r   )family_unescapestrr*   sloctokensr1   r1   r2   r!         zFontconfigPatternParser._familyc                 C      t |d gS Nr   )floatr=   r1   r1   r2   r"         zFontconfigPatternParser._sizec                 C   rB   rC   r<   r=   r1   r1   r2   r#      rE   zFontconfigPatternParser._namec                 C   r9   r:   )value_unescaper<   r=   r1   r1   r2   r%      rA   zFontconfigPatternParser._valuec                 C      dd |D | j d< g S )Nc                 S      g | ]}t |qS r1   rF   .0xr1   r1   r2   
<listcomp>       z5FontconfigPatternParser._families.<locals>.<listcomp>r+   r4   r=   r1   r1   r2   r&         z!FontconfigPatternParser._familiesc                 C   rH   )Nc                 S   rI   r1   rF   rJ   r1   r1   r2   rM      rN   z8FontconfigPatternParser._point_sizes.<locals>.<listcomp>r,   rO   r=   r1   r1   r2   r'      rP   z$FontconfigPatternParser._point_sizesc                 C   sp   t |dkr"|d | jv r | j|d  \}}| j|g | g S |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__rS   r3   r8   r!   r"   r#   r%   r&   r'   r(   r1   r1   r1   r2   r
      sl    	
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 ur dt |V  qd S )Nz\\\1rF   rJ   escape_funcr1   r2   	<genexpr>   s    z_escape_val.<locals>.<genexpr>)npiterable
isinstancer<   join)rX   r^   r1   r]   r2   _escape_val   s   rd   c                 C   sx   g }|   }|dur|g kr|t|t dD ]}t| d|  }|dur6|g kr6|d|t|tf  q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_familyrU   rd   family_escapegetattrvalue_escaperc   )dr6   r+   rW   rX   r1   r1   r2   generate_fontconfig_pattern   s   
rk   )r\   	functoolsr   reZnumpyr`   Z	pyparsingr   r   r   r   r   r   r	   r    compilesubr;   rg   r$   rG   ri   r
   r8   Zparse_fontconfig_patternrd   rk   r1   r1   r1   r2   <module>   s     $ 