a
    #©bÑ  ã                   @  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Ú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   úk/home/yuhw/wc/wire-cell-python-pr-11/pr11/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¥}nft| j	|ƒ}t
dd„ |D ƒƒ}| d¡ |D ]8‰ d ‡ fdd„|D ƒ¡}|sÊ| jdurÊ| j}||ˆ < qšd}	d|v �r
d|v �r
|d › d	|d › � dd
¡}	t| |ƒ\}
}|}||k�r6t| |ƒ}|j}i }t
|ƒ | ¡ ¡D ]:‰ | ˆ |d ¡}| ˆ |d ¡}|d |  ¡ |ˆ < �qLt|||	||
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>[   ó    zgetStatNames.<locals>.<genexpr>ú c                 3  s$   | ]}|j s|j ˆ |j¡V  qd S r"   )Zelidabler#   ÚgetZ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elidedFallbackNameÚreplaceÚ_getRibbiStyleÚgetStatNamesr   ÚunionÚkeysr*   Ústripr   )r   r   r   ZdefaultSourcer   r%   ÚlabelsÚ	languagesZ	styleNamer   r   ÚregularUserLocationZstyleNamesForStyleMapZregularStatNamesr   r0   r   r+   r   r9   4   s\    
ÿÿþ

þ


ûr9   z3list[Union[AxisDescriptor, DiscreteAxisDescriptor]]z$Dict[str, list[AxisLabelDescriptor]])r3   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'   ‰   r(   z'_getSortedAxisLabels.<locals>.<genexpr>c                 S  s   | j S r"   r@   )Úar   r   r   Ú<lambda>Š   r(   z&_getSortedAxisLabels.<locals>.<lambda>)Úkeyc                 S  s   i | ]}|j |j“qS r   )r1   Ú
axisLabelsrB   r   r   r   Ú
<dictcomp>Œ   s   z(_getSortedAxisLabels.<locals>.<dictcomp>N)ÚsortedrA   rG   r1   )r3   Z
sortedAxesZsortedLabelsrC   r   r   r   Ú_getSortedAxisLabels�   s    þÿ
rJ   zlist[AxisLabelDescriptor])r3   r   r    c                   sž   g }t | ƒ}| ¡ | ¡ kr<t d| ¡ › d| ¡ › d�¡ | ¡ D ]T\}}|| ‰ t‡ fdd„|D ƒd ƒ}|d u rŽt 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"   )Ú	userValueZuserMinimumZuserMaximum)r$   Úl©rK   r   r   r'   ©   s   


úz0_getAxisLabelsForUserLocation.<locals>.<genexpr>z!Document needs a label for axis 'z', user value ')rJ   r;   r.   r/   ÚitemsÚnextÚdebugÚappend)r3   r   r=   ZallAxisLabelsZaxisNamerG   r%   r   rM   r   r2   ™   s,    ÿÿ
þ
õÿr2   z%Tuple[RibbiStyle, SimpleLocationDict])Úselfr   r    c           	      C  sÂ   i }dd„ | j D ƒ}d}d}| d¡}|dur^|jD ](}|j||j kr4|j||j< d} q^q4| d¡pp| d¡}|durª|jD ](}|j||j kr€|j||j< d} qªq€t||f i |¥|¥fS )	zœCompute 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   )ÚtagrB   r   r   r   rH   È   r(   z"_getRibbiStyle.<locals>.<dictcomp>FZwghtNTZitalZslnt)r3   r*   rG   ZlinkedUserValuer1   rK   ÚBOLD_ITALIC_TO_RIBBI_STYLE)	rR   r   r?   Zaxes_by_tagr   r   rC   Zregular_labelZurpright_labelr   r   r   r8   ¿   s.    


ÿþr8   )r   Ú
__future__r   Zdataclassesr   Útypingr   r   r   r   ÚloggingZfontTools.designspaceLibr   r	   r
   r   r   r   r   Ú	getLoggerr   r.   ÚstrZ
RibbiStylerT   r   r9   rJ   r2   r8   r   r   r   r   Ú<module>   s$   
$

üM&