B
    Scl#                 @  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   Alib/python3.7/site-packages/fontTools/designspaceLib/statNames.pyr   (   s   
r   r
   r   )docuserLocationreturnc               s  i }|   }|dkr td n&|jdkr6td nd|ji|j}i }| |}|dk	rnd|ji|j}nnt| j	|}|rt
dd |D }|d x@|D ]8 d fdd|D }|s| jdk	r| j}|| < qW d|ksd|krt||di dd	S |d  d
|d  dd}	t| |\}
}|}||krHt| |}|j}i }xPt
|| D ]: | |d }| |d }|d |  | < q`W t|||	||
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.Z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 )N)Zelidabler!   getZdefaultName)r"   r#   )r$   r   r   r%   a   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#   labelsZ	languagesZ	styleNamer   r   regularUserLocationZstyleNamesForStyleMapZregularStatNamesr   r,   r   )r$   r   r5   3   sb    







r5   z3list[Union[AxisDescriptor, DiscreteAxisDescriptor]]z$Dict[str, list[AxisLabelDescriptor]])r/   r    c             C  sP   t dd | D dd d}dd |D }x"| D ]}|jdkr.|j||j< q.W |S )	zbReturns axis labels sorted by their ordering, with unordered ones appended as
    they are listed.c             s  s   | ]}|j d k	r|V  qd S )N)axisOrdering)r"   axisr   r   r   r%      s    z'_getSortedAxisLabels.<locals>.<genexpr>c             S  s   | j S )N)r;   )ar   r   r   <lambda>       z&_getSortedAxisLabels.<locals>.<lambda>)keyc             S  s   i | ]}|j |jqS r   )
axisLabelsr-   )r"   r<   r   r   r   
<dictcomp>   s   z(_getSortedAxisLabels.<locals>.<dictcomp>N)sortedr;   rA   r-   )r/   Z
sortedAxesZsortedLabelsr<   r   r   r   _getSortedAxisLabels   s    

rD   zlist[AxisLabelDescriptor])r/   r   r    c               s   g }t | }| | kr<td|  d|  d x`| D ]T\}}||  t fdd|D d }|d krtd| d  d qF|| qFW |S )Nz Mismatch between user location 'z' and available labels for 'z'.c             3  sL   | ]D}|j  ks@|jd k	r|jd k	r|j   kr<|jkrn q|V  qd S )N)	userValueZuserMinimumZuserMaximum)r"   l)rE   r   r   r%      s
   


z0_getAxisLabelsForUserLocation.<locals>.<genexpr>z!Document needs a label for axis 'z', user value ')rD   r7   r*   r+   itemsnextdebugappend)r/   r   r9   ZallAxisLabelsZaxisNamerA   r#   r   )rE   r   r.      s     
r.   z%Tuple[RibbiStyle, SimpleLocationDict])selfr   r    c       	      C  s   i }dd | j D }d}d}|d}|dk	rlx<|jD ]2}|j||j kr6|j|jk r6|j||j< d}P q6W |dp~|d}|dk	rxD|jD ]:}|j||j krt|jt|jk r|j||j< d}P qW t||f ||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   )tag)r"   r<   r   r   r   rB      s    z"_getRibbiStyle.<locals>.<dictcomp>FZwghtNTZitalZslnt)r/   r'   rA   ZlinkedUserValuer-   rE   absBOLD_ITALIC_TO_RIBBI_STYLE)	rK   r   r:   Zaxes_by_tagr   r   r<   Zregular_labelZupright_labelr   r   r   r4      s.    

r4   )r   Z
__future__r   Zdataclassesr   typingr   r   r   r   ZloggingZfontTools.designspaceLibr   r	   r
   r   r   r   r   Z	getLoggerr   r*   strZ
RibbiStylerN   r   r5   rD   r.   r4   r   r   r   r   <module>
   s    $

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