U
    Dvf                     @  s   d Z ddlmZ ddlmZmZmZ ddlZddl	m
Z
mZmZmZ ddlmZmZmZ ddlmZ dd	d
ddddZd	dddddZd	dddddZdddddZdddddZdS )zFExtra methods for DesignSpaceDocument to generate its STAT table data.    )annotations)DictListUnionN)AxisLabelDescriptorDesignSpaceDocumentDesignSpaceDocumentErrorLocationLabelDescriptor)RegiongetVFUserRegionlocationInRegion)TTFontr   r   strNone)ttFontdocvfNamereturnc                 C  sd   |  D ]}|j|kr q*qtd| t||}tjj| t||t	|||j
dk	r^|j
ndS )a  Build the STAT table for the variable font identified by its name in
    the given document.

    Knowing which variable we're building STAT data for is needed to subset
    the STAT locations to only include what the variable font actually ships.

    .. versionadded:: 5.0

    .. seealso::
        - :func:`getStatAxes()`
        - :func:`getStatLocations()`
        - :func:`fontTools.otlLib.builder.buildStatTable()`
    z&Cannot find the variable font by name N   )ZgetVariableFontsnamer   r   	fontToolsZotlLibZbuilderZbuildStatTablegetStatAxesgetStatLocationsZelidedFallbackName)r   r   r   ZvfZregion r   9/tmp/pip-unpacked-wheel-qlge9rch/fontTools/varLib/stat.pybuildVFStatTable   s    

r   r
   z
List[Dict])r   
userRegionr   c                   sl   t dd | jD dd}g }| jD ].}|jdk	r>||j q"|d7 }|| q" fddt| j|D S )	zReturn a list of axis dicts suitable for use as the ``axes``
    argument to :func:`fontTools.otlLib.builder.buildStatTable()`.

    .. versionadded:: 5.0
    c                 s  s   | ]}|j d k	r|j V  qd S )N)axisOrdering.0axisr   r   r   	<genexpr>;   s     
 zgetStatAxes.<locals>.<genexpr>)defaultN   c              	     s@   g | ]8\ }t  jd  ji j| fdd jD dqS )enc                   s&   g | ]}t  j|jirt|qS r   )r   r   	userValue_axisLabelToStatLocationr   label)r    r   r   r   
<listcomp>J   s   z*getStatAxes.<locals>.<listcomp>.<listcomp>)tagr   orderingvalues)dictr+   r   
labelNamesZ
axisLabels)r   r,   r   )r    r   r*   E   s   zgetStatAxes.<locals>.<listcomp>)maxaxesr   appendzip)r   r   ZmaxOrderingZaxisOrderingsr    r   r0   r   r   2   s    



r   c                   s(   dd j D   fddjD S )zReturn a list of location dicts suitable for use as the ``locations``
    argument to :func:`fontTools.otlLib.builder.buildStatTable()`.

    .. versionadded:: 5.0
    c                 S  s   i | ]}|j |qS r   )r   r   r   r   r   
<dictcomp>Z   s      z$getStatLocations.<locals>.<dictcomp>c                   sR   g | ]J}t |rtd |ji|j fdd| D t|dqS )r%   c                   s   i | ]\}} | j |qS r   )r+   )r   r   value)
axesByNamer   r   r5   `   s    z/getStatLocations.<locals>.<listcomp>.<dictcomp>)r   locationflags)r   ZgetFullUserLocationr.   r   r/   items_labelToFlagsr(   r7   r   r   r   r   r*   [   s   
z$getStatLocations.<locals>.<listcomp>)r2   ZlocationLabels)r   r   r   r<   r   r   T   s    r   z3Union[AxisLabelDescriptor, LocationLabelDescriptor]int)r)   r   c                 C  s$   d}| j r|dO }| jr |dO }|S )Nr   r$   r   )ZolderSiblingZelidable)r)   r9   r   r   r   r;   k   s    r;   r   r   c                 C  s   |   }d| ji| j}t| }|dkr8t|| j|dS |dkrTt|| j| j|dS |dkrt|| j|d}| jd k	r| j|d< | jd k	r| j|d	< |S t	d
d S )Nr%   r$   )r   r6   r9      )r   r6   ZlinkedValuer9   r   )r   ZnominalValuer9   ZrangeMinValueZrangeMaxValuezUnknown STAT label format)
Z	getFormatr   r/   r;   r.   r&   ZlinkedUserValueZuserMinimumZuserMaximumNotImplementedError)r)   Zlabel_formatr   r9   resr   r   r   r'   t   s0    



r'   )__doc__
__future__r   typingr   r   r   ZfontTools.otlLib.builderr   ZfontTools.designspaceLibr   r   r   r	   ZfontTools.designspaceLib.typesr
   r   r   ZfontTools.ttLibr   r   r   r   r;   r'   r   r   r   r   <module>   s    "	