o
    teh$                     @  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Zd"ddZd#ddZ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SourceDescriptorZ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   RD:\Projects\ConvertPro\env\Lib\site-packages\fontTools/designspaceLib/statNames.pyr   (   s   
 r   docr
   userLocationr   returnc                   s  i }|   }|du rtd n|jdu rtd nd|ji|j}i }| |}|dur7d|ji|j}n5t| j	|}|rlt
dd |D }|d |D ] d fdd|D }|sg| jdurg| j}|| < qOd|vstd|vr}t||di dd	S |d  d
|d  dd}	t| |\}
}|}||krt| |}|j}i }t
|| D ] | |d }| |d }|d |  | < qt|||	||
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"   )Zelidabler#   getdefaultName)r$   r%   r&   r   r   r'   `   s    
)r   r   r   r   r   - )ZfindDefaultLOGGERwarning
familyNameZlocalisedFamilyNameZlabelForUserLocationnamer#   _getAxisLabelsForUserLocationaxessetaddjoinZelidedFallbackNamer   replace_getRibbiStylegetStatNamesr   unionkeysr)   strip)r   r   r   ZdefaultSourcer   r%   labels	languagesZ	styleNamer   r   regularUserLocationZstyleNamesForStyleMapZregularStatNamesr   r0   r   r+   r   r9   3   sn   




r9   r3   3list[Union[AxisDescriptor, DiscreteAxisDescriptor]]$Dict[str, list[AxisLabelDescriptor]]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'      s    z'_getSortedAxisLabels.<locals>.<genexpr>c                 S  s   | j S r"   rB   )ar   r   r   <lambda>   s    z&_getSortedAxisLabels.<locals>.<lambda>)keyc                 S  s   i | ]}|j |jqS r   )r1   
axisLabelsrD   r   r   r   
<dictcomp>   s    
z(_getSortedAxisLabels.<locals>.<dictcomp>N)sortedrC   rI   r1   )r3   Z
sortedAxesZsortedLabelsrE   r   r   r   _getSortedAxisLabels   s   
rL   list[AxisLabelDescriptor]c                   s   g }t | }| | krtd|  d|  d | D ]*\}}||  t fdd|D d }|d u rGtd| d  d q"|| q"|S )Nz Mismatch between user location 'z' and available labels for 'z'.c                 3  sN    | ]"}|j  ks!|jd ur$|jd ur$|j   kr|jkrn n|V  qd S r"   )	userValueZuserMinimumZuserMaximum)r$   lrN   r   r   r'      s    


z0_getAxisLabelsForUserLocation.<locals>.<genexpr>z!Document needs a label for axis 'z', user value ')rL   r;   r.   r/   itemsnextdebugappend)r3   r   r=   ZallAxisLabelsZaxisNamerI   r%   r   rP   r   r2      s,   

r2   self%Tuple[RibbiStyle, SimpleLocationDict]c           	      C  s   i }dd | j D }d}d}|d}|dur5|jD ]}|j||j kr4|j|jk r4|j||j< d} nq|dp>|d}|dure|jD ]}|j||j krdt|jt|jk rd|j||j< d} nqFt||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   )tagrD   r   r   r   rJ      s    z"_getRibbiStyle.<locals>.<dictcomp>FZwghtNTZitalZslnt)r3   r)   rI   ZlinkedUserValuer1   rN   absBOLD_ITALIC_TO_RIBBI_STYLE)	rU   r   r?   Zaxes_by_tagr   r   rE   Zregular_labelZupright_labelr   r   r   r8      s6   


r8   )r   r
   r   r   r    r   )r3   r@   r    rA   )r3   r@   r   r   r    rM   )rU   r
   r   r   r    rV   )r   
__future__r   dataclassesr   typingr   r   r   r   loggingZfontTools.designspaceLibr   r	   r
   r   r   r   r   	getLoggerr   r.   strZ
RibbiStylerY   r   r9   rL   r2   r8   r   r   r   r   <module>   s&    
$




X
&