a
    Qhm#                     @  s   d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
Z
ddlmZmZmZmZmZmZmZ e
eZeZddd	d
dZeG dd dZddddddZdddddZddddddZddddddZdS ) a-  Compute name information for a given location in user-space coordinates
using STAT data. This can be used to fill-in automatically the names of an
instance:

.. code:: python

    instance = doc.instances[0]
    names = getStatNames(doc, instance.getFullUserLocation(doc))
    print(names.styleNames)
    )annotations)	dataclass)DictOptionalTupleUnionN)AxisDescriptorAxisLabelDescriptorDesignSpaceDocumentDesignSpaceDocumentErrorDiscreteAxisDescriptorSimpleLocationDictSourceDescriptorregularitalicboldzbold italic))FF)FT)TF)TTc                   @  s:   e Zd ZU dZded< ded< ded< ded< ded	< d
S )	StatNamesz4Name data generated from the STAT table information.zDict[str, str]familyNames
styleNameszOptional[str]postScriptFontNamestyleMapFamilyNameszOptional[RibbiStyle]styleMapStyleNameN)__name__
__module____qualname____doc____annotations__ r   r   [/var/www/sistema_ama/venv/lib/python3.9/site-packages/fontTools/designspaceLib/statNames.pyr   )   s   
r   r
   r   )docuserLocationreturnc                   s  i }|   }|du r td n&|jdu r6td nd|ji|j}i }| |}|durnd|ji|j}njt| j	|}|rt
dd |D }|d |D ]8 d fdd|D }|s| jdur| j}|| < qd|vsd|vrt||di dd	S |d  d
|d  dd}	t| |\}
}|}||krDt| |}|j}i }t
|| D ]: | |d }| |d }|d |  | < qZt|||	||
d	S )aq  Compute the family, style, PostScript names of the given ``userLocation``
    using the document's STAT information.

    Also computes localizations.

    If not enough STAT data is available for a given name, either its dict of
    localized names will be empty (family and style names), or the name will be
    None (PostScript name).

    .. versionadded:: 5.0
    Nz7Cannot determine default source to look up family name.zTCannot look up family name, assign the 'familyname' attribute to the default source.enc                 s  s   | ]}|j D ]
}|V  qqd S N)
labelNames).0labellanguager   r   r   	<genexpr>\   s   zgetStatNames.<locals>.<genexpr> c                 3  s$   | ]}|j s|j |jV  qd S r#   )elidabler$   getdefaultName)r%   r&   r'   r   r   r(   a   s   )r   r   r   r   r   - )findDefaultLOGGERwarning
familyNamelocalisedFamilyNamelabelForUserLocationnamer$   _getAxisLabelsForUserLocationaxessetaddjoinelidedFallbackNamer   replace_getRibbiStylegetStatNamesr   unionkeysr+   strip)r   r    r   ZdefaultSourcer   r&   labels	languages	styleNamer   r   regularUserLocationZstyleNamesForStyleMapZregularStatNamesr   r3   r   r-   r   r?   4   sn    





r?   z3list[Union[AxisDescriptor, DiscreteAxisDescriptor]]z$Dict[str, list[AxisLabelDescriptor]])r8   r!   c                 C  sL   t dd | D dd d}dd |D }| D ]}|jdu r,|j||j< q,|S )	zbReturns axis labels sorted by their ordering, with unordered ones appended as
    they are listed.c                 s  s   | ]}|j d ur|V  qd S r#   axisOrderingr%   axisr   r   r   r(          z'_getSortedAxisLabels.<locals>.<genexpr>c                 S  s   | j S r#   rG   )ar   r   r   <lambda>   rK   z&_getSortedAxisLabels.<locals>.<lambda>)keyc                 S  s   i | ]}|j |jqS r   )r6   
axisLabelsrI   r   r   r   
<dictcomp>   s   z(_getSortedAxisLabels.<locals>.<dictcomp>N)sortedrH   rO   r6   )r8   Z
sortedAxesZsortedLabelsrJ   r   r   r   _getSortedAxisLabels   s    
rR   zlist[AxisLabelDescriptor])r8   r    r!   c                   s   g }t | }| | kr<td|  d|  d | D ]T\}}||  t fdd|D d }|d u rtd| d  d qD|| qD|S )Nz Mismatch between user location 'z' and available labels for 'z'.c                 3  sL   | ]D}|j  ks@|jd ur|jd ur|j   kr<|jkrn q|V  qd S r#   )	userValueuserMinimumuserMaximum)r%   lrS   r   r   r(      s   


z0_getAxisLabelsForUserLocation.<locals>.<genexpr>z!Document needs a label for axis 'z', user value ')rR   rA   r1   r2   itemsnextdebugappend)r8   r    rC   ZallAxisLabelsaxisNamerO   r&   r   rW   r   r7      s,    

r7   z%Tuple[RibbiStyle, SimpleLocationDict])selfr    r!   c           	      C  s   i }dd | j D }d}d}|d}|durj|jD ]4}|j||j kr4|j|jk r4|j||j< d} qjq4|dp||d}|dur|jD ]<}|j||j krt|jt|jk r|j||j< d} qqt||f i ||fS )	zCompute the RIBBI style name of the given user location,
    return the location of the matching Regular in the RIBBI group.

    .. versionadded:: 5.0
    c                 S  s   i | ]}|j |qS r   )tagrI   r   r   r   rP      rK   z"_getRibbiStyle.<locals>.<dictcomp>FwghtNTitalslnt)r8   r+   rO   linkedUserValuer6   rS   absBOLD_ITALIC_TO_RIBBI_STYLE)	r]   r    rF   Zaxes_by_tagr   r   rJ   Zregular_labelZupright_labelr   r   r   r>      s:    


	
	r>   )r   
__future__r   dataclassesr   typingr   r   r   r   loggingfontTools.designspaceLibr   r	   r
   r   r   r   r   	getLoggerr   r1   strZ
RibbiStylerd   r   r?   rR   r7   r>   r   r   r   r   <module>   s$   $


X&