a
    #©bóB  ã                   @  sr  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 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)ÚAxisDescriptorÚDesignSpaceDocumentÚDiscreteAxisDescriptorÚInstanceDescriptorÚRuleDescriptorÚSimpleLocationDictÚSourceDescriptorÚVariableFontDescriptor)Ú	StatNamesÚgetStatNames)ÚRangeÚRegionÚConditionSetÚ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&   úg/home/yuhw/wc/wire-cell-python-pr-11/pr11/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 ]2}t|tƒr(| |¡ qt|j|j|jƒ||j< qt	j
dd„ |D ƒŽ }|D ]>}dd„ t||ƒD ƒ}	t| i |¥|	¥d|||d�}
|	|
fV  qZd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
    c                 S  s   g | ]
}|j ‘qS r&   )Úvalues)Ú.0Úaxisr&   r&   r'   Ú
<listcomp>`   ó    z%splitInterpolable.<locals>.<listcomp>c                 S  s   i | ]\}}|j |“qS r&   )Úname)r/   ZdiscreteAxisÚvaluer&   r&   r'   Ú
<dictcomp>b   s   ÿz%splitInterpolable.<locals>.<dictcomp>T©ÚkeepVFsr+   r,   r-   N)ÚaxesÚ
isinstancer   Úappendr   ÚminimumÚmaximumÚdefaultr3   Ú	itertoolsÚproductÚzipÚ_extractSubSpace)r   r+   r,   r-   ZdiscreteAxesZinterpolableUserRegionr0   ZvalueCombinationsr.   ZdiscreteUserLocationÚsubDocr&   r&   r'   ÚsplitInterpolable5   s,    "

ÿþ
úrC   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
    Fr6   N)ÚgetVariableFontsr   rA   Úlibr3   )r   r+   r,   r-   ÚvfÚvfUserRegionÚvfDocr&   r&   r'   ÚsplitVariableFontsq   s    
úrI   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)rC   rI   ZformatVersion)r   ZvfsZ	_locationrB   ZvfNamerH   r&   r&   r'   Úconvert5to4–   s    	rJ   r   )r   Ú
userRegionr7   r+   r,   r-   r   c                  s"  t ƒ }‡ ‡fdd„}ˆ jD ]x}ˆ|j ‰tˆtƒrt|tƒr| t|j|j|j|j	t
ˆj|jƒˆjph|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�rB|d u�rB|j%|_%|�ršˆ  &¡ D ]H}t'ˆ |ƒ}t(|ˆƒ�rP| )t*|j|j‡fdd„|j+D ƒ|j,d�¡ �qPˆ j-D �]r}t| .ˆ ¡ˆƒ�s¼�q |�r²t/ˆ | .ˆ ¡ƒ}|j�pä|j0 1d	¡}|j �pø|j2 1d	¡}| 3t4|j�p|ˆ ||ƒ|j|j|j�p.|› d
|› �ˆ�r8i n|j5tˆ||ƒƒ|||j6�pX|j6|j7�pj|j8 1d	¡|j9�pv|j9|j%�p‚|j0|j:�pŽ|j2|j;�pš|j8|j<�p¤i |j,d�¡ n`| 3t4|j|j|j|jˆ�rÒi n|j5tˆ||ƒƒ|j|j |j6|j7|j9|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>.maybeExpandDesignLocationc                   s4   g | ],\}}ˆ j |  kr$ˆ jkrn q||f‘qS r&   )r;   r<   )r/   ÚuserZdesign)Úranger&   r'   r1   Î   s   þz$_extractSubSpace.<locals>.<listcomp>)
Útagr3   Ú
labelNamesÚhiddenr;   r=   r<   ÚmapZaxisOrderingZ
axisLabels)ÚfilenameÚpathÚfontr3   rL   Ú	layerNamer!   r$   ÚmuteKerningÚmuteInfoÚmutedGlyphNamesc                   s    g | ]}t ˆ |j tƒr|‘qS r&   )r9   r3   r   )r/   Z
axisSubset©rK   r&   r'   r1     s   þ)r3   rU   ÚaxisSubsetsrE   r   ú )rU   rV   rW   r3   ÚuserLocationrL   r!   r$   ÚpostScriptFontNameÚstyleMapFamilyNameÚstyleMapStyleNameÚlocalisedFamilyNameÚlocalisedStyleNameÚlocalisedStyleMapFamilyNameÚlocalisedStyleMapStyleNamerE   )=r
   r8   r3   r9   r   r	   ZaddAxisrQ   rR   rS   Úmaxr;   r=   Úminr<   rT   r   Ú_subsetRulesBasedOnConditionsÚrulesZrulesProcessingLastÚsourcesr   Zmap_backwardrL   Z	addSourcer   rU   rV   rW   Ú_filterLocationrX   r!   r$   rY   rZ   r[   ZfindDefaultrc   rD   r   r   ZaddVariableFontr   r]   rE   Z	instancesZgetFullUserLocationr   r"   r#   r%   ZaddInstancer   r_   r`   ra   ZstyleMapFamilyNamesrb   rd   re   rf   )r   rK   r7   r+   r,   r-   rB   rN   r0   ÚdesignRegionÚsourceZ	vfDefaultZ
oldDefaultrF   rG   r   r   r!   r$   r&   )r   r,   rP   rK   r'   rA   §   sö    	




þïÿ

ÿóÿ

þøÿ
ÿ
æÿÿîÿrA   zList[Dict[str, Any]]r   )ÚconditionSetr   c                 C  s:   i }| D ],}t | dtj ¡| dtj¡ƒ||d < q|S )Nr;   r<   r3   )r   r#   ÚmathÚinf)ro   ÚcÚ	conditionr&   r&   r'   Ú_conditionSetFromU  s    þrt   zList[RuleDescriptor])rj   rm   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)r3   ÚconditionSetsÚsubsFT)r3   r;   r<   )r   r3   rv   ru   rt   Úitemsr9   ÚfloatÚintÚintersectionr:   r;   r<   )rj   rm   ZnewRulesZruleZnewRuleZconditionsetÚcsZnewConditionsetZdiscardConditionsetZselectionNameZselectionValuerz   r&   r&   r'   ri   _  s@    
ÿ
ýÿ	ri   zDict[str, float])rK   Úlocationr   c                   s   ‡ fdd„|  ¡ D ƒS )Nc                   s,   i | ]$\}}|ˆ v rt ˆ | tƒr||“qS r&   )r9   r   )r/   r3   r4   r\   r&   r'   r5   ¤  s   þz#_filterLocation.<locals>.<dictcomp>)rw   )rK   r|   r&   r\   r'   rl      s    
þrl   ).Ú__doc__Ú
__future__r   r>   Úloggingrp   Útypingr   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(   rC   rI   rJ   rA   rt   ri   rl   r&   r&   r&   r'   Ú<module>   s6    (
$

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