a
    MˆcqE  ã                   @  sv  d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZmZmZm Z m!Z! e "e#¡Z$eeeege%f Z&dd	d
ddœdd„Z'dde'fddddddœdd„Z(dde'fddddddœdd„Z)dddœdd„Z*dddd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 )/zŒAllows building all the variable fonts of a DesignSpace version 5 by
splitting the document into interpolable sub-space, then into each VF.
é    )ÚannotationsN)ÚAnyÚCallableÚDictÚIteratorÚListÚTupleÚcast)ÚAxisDescriptorÚDesignSpaceDocumentÚDiscreteAxisDescriptorÚInstanceDescriptorÚRuleDescriptorÚSimpleLocationDictÚSourceDescriptorÚVariableFontDescriptor)Ú	StatNamesÚgetStatNames)ÚConditionSetÚRangeÚRegionÚgetVFUserRegionÚlocationInRegionÚregionInRegionÚuserRegionToDesignRegionr   r   r   Ústr)ÚdocÚinstanceÚ	statNamesÚreturnc                 C  s4   |j p|j d¡}|jp"|j d¡}|› d|› d�S )zÿDefault callable to synthesize an instance filename
    when makeNames=True, for instances that don't specify an instance name
    in the designspace. This part of the name generation can be overriden
    because it's not specified by the STAT table.
    Úenú-z.ttf)Ú
familyNameÚfamilyNamesÚgetÚ	styleNameÚ
styleNames)r   r   r   r"   r%   © r'   úa/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/fontTools/designspaceLib/split.pyÚdefaultMakeInstanceFilename(   s    r)   TÚboolÚMakeInstanceFilenameCallablez8Iterator[Tuple[SimpleLocationDict, DesignSpaceDocument]])r   Ú	makeNamesÚexpandLocationsÚmakeInstanceFilenamer   c              	   c  s²   g }i }| j D ]F}t|dƒr2tt|ƒ}| |¡ qtt|ƒ}t|j|j|j	ƒ||j
< qtjdd„ |D ƒŽ }|D ]>}dd„ t||ƒD ƒ}	t| i |¥|	¥d|||d�}
|	|
fV  qndS )	aì  Split the given DS5 into several interpolable sub-designspaces.
    There are as many interpolable sub-spaces as there are combinations of
    discrete axis values.

    E.g. with axes:
        - italic (discrete) Upright or Italic
        - style (discrete) Sans or Serif
        - weight (continuous) 100 to 900

    There are 4 sub-spaces in which the Weight axis should interpolate:
    (Upright, Sans), (Upright, Serif), (Italic, Sans) and (Italic, Serif).

    The sub-designspaces still include the full axis definitions and STAT data,
    but the rules, sources, variable fonts, instances are trimmed down to only
    keep what falls within the interpolable sub-space.

    Args:
      - ``makeNames``: Whether to compute the instance family and style
        names using the STAT data.
      - ``expandLocations``: Whether to turn all locations into "full"
        locations, including implicit default axis values where missing.
      - ``makeInstanceFilename``: Callable to synthesize an instance filename
        when makeNames=True, for instances that don't specify an instance name
        in the designspace. This part of the name generation can be overridden
        because it's not specified by the STAT table.

    .. versionadded:: 5.0
    Úvaluesc                 S  s   g | ]
}|j ‘qS r'   )r/   )Ú.0Úaxisr'   r'   r(   Ú
<listcomp>g   ó    z%splitInterpolable.<locals>.<listcomp>c                 S  s   i | ]\}}|j |“qS r'   )Úname)r0   ZdiscreteAxisÚvaluer'   r'   r(   Ú
<dictcomp>i   s   ÿz%splitInterpolable.<locals>.<dictcomp>T©ÚkeepVFsr,   r-   r.   N)ÚaxesÚhasattrr	   r   Úappendr
   r   ÚminimumÚmaximumÚdefaultr4   Ú	itertoolsÚproductÚzipÚ_extractSubSpace)r   r,   r-   r.   ZdiscreteAxesZinterpolableUserRegionr1   ZvalueCombinationsr/   ZdiscreteUserLocationÚsubDocr'   r'   r(   ÚsplitInterpolable5   s4    "



ýþ
úrD   Fz)Iterator[Tuple[str, DesignSpaceDocument]]c              	   c  sN   |   ¡ D ]@}t| |ƒ}t| |d|||d�}i |j¥|j¥|_|j|fV  qdS )a$  Convert each variable font listed in this document into a standalone
    designspace. This can be used to compile all the variable fonts from a
    format 5 designspace using tools that can only deal with 1 VF at a time.

    Args:
      - ``makeNames``: Whether to compute the instance family and style
        names using the STAT data.
      - ``expandLocations``: Whether to turn all locations into "full"
        locations, including implicit default axis values where missing.
      - ``makeInstanceFilename``: Callable to synthesize an instance filename
        when makeNames=True, for instances that don't specify an instance name
        in the designspace. This part of the name generation can be overridden
        because it's not specified by the STAT table.

    .. versionadded:: 5.0
    Fr7   N)ÚgetVariableFontsr   rB   Úlibr4   )r   r,   r-   r.   ÚvfÚvfUserRegionÚvfDocr'   r'   r(   ÚsplitVariableFontsx   s    
úrJ   zDict[str, DesignSpaceDocument])r   r   c                 C  s:   i }t | ƒD ](\}}t|ƒD ]\}}d|_|||< qq|S )züConvert each variable font listed in this document into a standalone
    format 4 designspace. This can be used to compile all the variable fonts
    from a format 5 designspace using tools that only know about format 4.

    .. versionadded:: 5.0
    z4.1)rD   rJ   ZformatVersion)r   ZvfsZ	_locationrC   ZvfNamerI   r'   r'   r(   Úconvert5to4�   s    	rK   r   )r   Ú
userRegionr8   r,   r-   r.   r   c                  s,  t ƒ }‡ ‡fdd„}ˆ jD ]‚}ˆ|j ‰tˆtƒrt|dƒrtt|ƒ}| t|j	|j|j
|jtˆj|jƒˆjpr|jtˆj|jƒ‡fdd„|jD ƒd d d�
¡ qtˆ ˆƒ}	tˆ j|	ƒ|_ˆ j|_ˆ jD ]Z}
tˆ  |
j¡ˆƒsÜqÄ| t|
j|
j|
j|
jtˆ||
ƒƒ|
j |
j!|
j"|
j#|
j$|
j%d�¡ qÄ| &¡ }ˆ  &¡ }|d u�rL|d u�rL|j'|_'|�r¤ˆ  (¡ D ]H}t)ˆ |ƒ}t*|ˆƒ�rZ| +t,|j|j‡fdd„|j-D ƒ|j.d	�¡ �qZˆ j/D �]r}t| 0ˆ ¡ˆƒ�sÆ�qª|�r¼t1ˆ | 0ˆ ¡ƒ}|j!�pî|j2 3d
¡}|j"�p|j4 3d
¡}| 5t6|j�p|ˆ ||ƒ|j|j|j�p8|› d|› �ˆ�rBi n|j7tˆ||ƒƒ|||j8�pb|j8|j9�pt|j: 3d
¡|j;�p€|j;|j'�pŒ|j2|j<�p˜|j4|j=�p¤|j:|j>�p®i |j.d�¡ n`| 5t6|j|j|j|jˆ�rÜi n|j7tˆ||ƒƒ|j!|j"|j8|j9|j;|j'|j<|j=|j>|j.d�¡ �qªˆ j.|_.|S )Nc                   s   ˆr|   ˆ ¡S | jS d S )N)ZgetFullDesignLocationÚdesignLocation)Úobject)r   r-   r'   r(   ÚmaybeExpandDesignLocationÁ   s    
z3_extractSubSpace.<locals>.maybeExpandDesignLocationr<   c                   s4   g | ],\}}ˆ j |  kr$ˆ jkrn q||f‘qS r'   )r<   r=   )r0   ÚuserZdesign)Úranger'   r(   r2   Ù   s   þz$_extractSubSpace.<locals>.<listcomp>)
Útagr4   Ú
labelNamesÚhiddenr<   r>   r=   ÚmapZaxisOrderingZ
axisLabels)ÚfilenameÚpathÚfontr4   rM   Ú	layerNamer"   r%   ÚmuteKerningÚmuteInfoÚmutedGlyphNamesc                   s    g | ]}t ˆ |j tƒr|‘qS r'   )Ú
isinstancer4   r   )r0   Z
axisSubset©rL   r'   r(   r2     s   þ)r4   rV   ÚaxisSubsetsrF   r    ú )rV   rW   rX   r4   ÚuserLocationrM   r"   r%   ÚpostScriptFontNameÚstyleMapFamilyNameÚstyleMapStyleNameÚlocalisedFamilyNameÚlocalisedStyleNameÚlocalisedStyleMapFamilyNameÚlocalisedStyleMapStyleNamerF   )?r   r9   r4   r]   r   r:   r	   r
   ZaddAxisrR   rS   rT   Úmaxr<   r>   Úminr=   rU   r   Ú_subsetRulesBasedOnConditionsÚrulesZrulesProcessingLastÚsourcesr   Zmap_backwardrM   Z	addSourcer   rV   rW   rX   Ú_filterLocationrY   r"   r%   rZ   r[   r\   ZfindDefaultre   rE   r   r   ZaddVariableFontr   r_   rF   Z	instancesZgetFullUserLocationr   r#   r$   r&   ZaddInstancer   ra   rb   rc   ZstyleMapFamilyNamesrd   rf   rg   rh   )r   rL   r8   r,   r-   r.   rC   rO   r1   ÚdesignRegionÚsourceZ	vfDefaultZ
oldDefaultrG   rH   r   r   r"   r%   r'   )r   r-   rQ   rL   r(   rB   ®   sø    	





þïÿ

ÿóÿ

þøÿ
ÿ
æÿÿîÿrB   zList[Dict[str, Any]]r   )ÚconditionSetr   c                 C  sX   i }| D ]J}|  d¡|  d¡ }}t|d ur0|ntj |d urB|ntjƒ||d < q|S )Nr<   r=   r4   )r$   r   ÚmathÚinf)rq   ÚcÚ	conditionr<   r=   r'   r'   r(   Ú_conditionSetFrom`  s    þrv   zList[RuleDescriptor])rl   ro   r   c              	   C  sÞ   g }| D ]Ð}t |jg |jd�}|jD ]¢}t|ƒ}g }d}| ¡ D ]t\}	}
|	|vrRq@t|
ttfƒrt|
||	 v rnq´d}q@|
||	 v r‚q@||	  	|
¡}|d ur°| 
|	|j|jdœ¡ q@d}q@|s$|j 
|¡ q$|jr| 
|¡ q|S )N)r4   ÚconditionSetsÚsubsFT)r4   r<   r=   )r   r4   rx   rw   rv   Úitemsr]   ÚfloatÚintÚintersectionr;   r<   r=   )rl   ro   ZnewRulesZruleZnewRuleZconditionsetÚcsZnewConditionsetZdiscardConditionsetZselectionNameZselectionValuer|   r'   r'   r(   rk   k  s@    
ÿ
ýÿ	rk   zDict[str, float])rL   Úlocationr   c                   s   ‡ fdd„|  ¡ D ƒS )Nc                   s,   i | ]$\}}|ˆ v rt ˆ | tƒr||“qS r'   )r]   r   )r0   r4   r5   r^   r'   r(   r6   °  s   þz#_filterLocation.<locals>.<dictcomp>)ry   )rL   r~   r'   r^   r(   rn   ¬  s    
þrn   )/Ú__doc__Ú
__future__r   r?   Úloggingrr   Útypingr   r   r   r   r   r   r	   ZfontTools.designspaceLibr
   r   r   r   r   r   r   r   Z"fontTools.designspaceLib.statNamesr   r   ZfontTools.designspaceLib.typesr   r   r   r   r   r   r   Ú	getLoggerÚ__name__ZLOGGERr   r+   r)   rD   rJ   rK   rB   rv   rk   rn   r'   r'   r'   r(   Ú<module>   s6   $(
$

ÿüEü% 3A