B
    dqE                 @  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 )/zAllows 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 )zDefault 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'   k/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/fontTools/designspaceLib/split.pydefaultMakeInstanceFilename(   s    r)   TboolMakeInstanceFilenameCallablez8Iterator[Tuple[SimpleLocationDict, DesignSpaceDocument]])r   	makeNamesexpandLocationsmakeInstanceFilenamer   c          	   c  s   g }i }xP| j D ]F}t|dr4tt|}|| qtt|}t|j|j|j	||j
< qW tjdd |D  }xB|D ]:}dd t||D }	t| ||	d|||d}
|	|
fV  qtW d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   s    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defaultr3   	itertoolsproductzip_extractSubSpace)r   r,   r-   r.   ZdiscreteAxesZinterpolableUserRegionr1   ZvalueCombinationsr/   ZdiscreteUserLocationsubDocr'   r'   r(   splitInterpolable5   s.    "



rB   Fz)Iterator[Tuple[str, DesignSpaceDocument]]c          	   c  sN   xH|   D ]<}t| |}t| |d|||d}|j|j|_|j|fV  q
W 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
    F)r6   r,   r-   r.   N)getVariableFontsr   r@   libr3   )r   r,   r-   r.   vfvfUserRegionvfDocr'   r'   r(   splitVariableFontsx   s    
rH   zDict[str, DesignSpaceDocument])r   r   c             C  sB   i }x8t | D ],\}}x"t|D ]\}}d|_|||< q W qW |S )zConvert 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)rB   rH   ZformatVersion)r   ZvfsZ	_locationrA   ZvfNamerG   r'   r'   r(   convert5to4   s    	rI   r   )r   
userRegionr6   r,   r-   r.   r   c              s>  t  } fdd}x jD ]}|j ttrt|drtt|}|t|j	|j|j
|jtj|jjpt|jtj|jfdd|jD d d d
 qW t }	t j|	|_ j|_xd jD ]Z}
t |
jsq|t|
j|
j|
j|
jt||
|
j |
j!|
j"|
j#|
j$|
j%d qW |& } & }|d k	rT|d k	rT|j'|_'|rxT ( D ]H}t) |}t*|rd|+t,|j|jfdd|j-D |j.d	 qdW x~ j/D ]r}t|0 s֐q|rt1 |0 }|j!p|j23d
}|j"p|j43d
}|5t6|jp,| |||j|j|jpH| d| rRi n|j7t|||||j8pr|j8|j9p|j:3d
|j;p|j;|j'p|j2|j<p|j4|j=p|j:|j>pi |j.d n`|5t6|j|j|j|jri n|j7t|||j!|j"|j8|j9|j;|j'|j<|j=|j>|j.d qW  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>)
tagr3   
labelNameshiddenr:   r<   r;   mapZaxisOrderingZ
axisLabels)filenamepathfontr3   rK   	layerNamer"   r%   muteKerningmuteInfomutedGlyphNamesc               s    g | ]}t  |j tr|qS r'   )
isinstancer3   r   )r0   Z
axisSubset)rJ   r'   r(   r2     s   )r3   rT   axisSubsetsrD   r     )rT   rU   rV   r3   userLocationrK   r"   r%   postScriptFontNamestyleMapFamilyNamestyleMapStyleNamelocalisedFamilyNamelocalisedStyleNamelocalisedStyleMapFamilyNamelocalisedStyleMapStyleNamerD   )?r   r7   r3   r[   r   r8   r	   r
   ZaddAxisrP   rQ   rR   maxr:   r<   minr;   rS   r   _subsetRulesBasedOnConditionsrulesZrulesProcessingLastsourcesr   Zmap_backwardrK   Z	addSourcer   rT   rU   rV   _filterLocationrW   r"   r%   rX   rY   rZ   ZfindDefaultrb   rC   r   r   ZaddVariableFontr   r\   rD   Z	instancesZgetFullUserLocationr   r#   r$   r&   ZaddInstancer   r^   r_   r`   ZstyleMapFamilyNamesra   rc   rd   re   )r   rJ   r6   r,   r-   r.   rA   rM   r1   designRegionsourceZ	vfDefaultZ
oldDefaultrE   rF   r   r   r"   r%   r'   )r   r-   rO   rJ   r(   r@      s    	












r@   zList[Dict[str, Any]]r   )conditionSetr   c             C  s\   i }xR| D ]J}| d| d }}t|d k	r2|ntj |d k	rD|ntj||d < q
W |S )Nr:   r;   r3   )r$   r   mathinf)rn   c	conditionr:   r;   r'   r'   r(   _conditionSetFrom`  s    
 rs   zList[RuleDescriptor])ri   rl   r   c          	   C  s   g }x| D ]}t |jg |jd}x|jD ]}t|}g }d}x| D ]t\}	}
|	|krXqFt|
ttfrz|
||	 krtqd}qF|
||	 krqF||	 	|
}|d k	r|
|	|j|jd qFd}qFW |s(|j
| q(W |jr
|
| q
W |S )N)r3   conditionSetssubsFT)r3   r:   r;   )r   r3   ru   rt   rs   itemsr[   floatintintersectionr9   r:   r;   )ri   rl   ZnewRulesruleZnewRuleZconditionsetcsZnewConditionsetZdiscardConditionsetZselectionNameZselectionValuery   r'   r'   r(   rh   k  s:    
rh   zDict[str, float])rJ   locationr   c               s    fdd|  D S )Nc               s,   i | ]$\}}| krt  | tr||qS r'   )r[   r   )r0   r3   r4   )rJ   r'   r(   r5     s   z#_filterLocation.<locals>.<dictcomp>)rv   )rJ   r|   r'   )rJ   r(   rk     s    
rk   )/__doc__
__future__r   r=   loggingro   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__LOGGERr   r+   r)   rB   rH   rI   r@   rs   rh   rk   r'   r'   r'   r(   <module>   s0   $(
$

A! 3A