U
    6cqE                     @  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'   B/tmp/pip-unpacked-wheel-n2hbwplv/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| ||	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   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   s4    "



rC   Fz)Iterator[Tuple[str, DesignSpaceDocument]]c              	   c  sJ   |   D ]<}t| |}t| |d|||d}|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(   splitVariableFontsx   s    
rI   zDict[str, DesignSpaceDocument])r   r   c                 C  s:   i }t | D ](\}}t|D ]\}}d|_|||< qq|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)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 ]}|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sq|t|
j|
j|
j|
jt||
|
j |
j!|
j"|
j#|
j$|
j%d q|& } & }|d k	rL|d k	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|rt1 |0 }|j!p|j23d
}|j"p|j43d
}|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>pi |j.d n`|5t6|j|j|j|jri 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>)
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'   )
isinstancer3   r   )r0   Z
axisSubsetrK   r'   r(   r2     s   )r3   rU   axisSubsetsrE   r     )rU   rV   rW   r3   userLocationrL   r"   r%   postScriptFontNamestyleMapFamilyNamestyleMapStyleNamelocalisedFamilyNamelocalisedStyleNamelocalisedStyleMapFamilyNamelocalisedStyleMapStyleNamerE   )?r   r8   r3   r\   r   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findDefaultrd   rD   r   r   ZaddVariableFontr   r^   rE   Z	instancesZgetFullUserLocationr   r#   r$   r&   ZaddInstancer   r`   ra   rb   ZstyleMapFamilyNamesrc   re   rf   rg   )r   rK   r7   r,   r-   r.   rB   rN   r1   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  sX   i }| D ]J}| d| d }}t|d k	r0|ntj |d k	rB|ntj||d < q|S )Nr;   r<   r3   )r$   r   mathinf)rp   c	conditionr;   r<   r'   r'   r(   _conditionSetFrom`  s    ru   zList[RuleDescriptor])rk   rn   r   c              	   C  s   g }| D ]}t |jg |jd}|jD ]}t|}g }d}| D ]t\}	}
|	|krRq@t|
ttfrt|
||	 krnqd}q@|
||	 krq@||	 	|
}|d k	r|
|	|j|jd q@d}q@|s$|j
| q$|jr|
| q|S )N)r3   conditionSetssubsFT)r3   r;   r<   )r   r3   rw   rv   ru   itemsr\   floatintintersectionr:   r;   r<   )rk   rn   ZnewRulesZruleZnewRuleZconditionsetcsZnewConditionsetZdiscardConditionsetZselectionNameZselectionValuer{   r'   r'   r(   rj   k  sD      
	rj   zDict[str, float])rK   locationr   c                   s    fdd|  D S )Nc                   s,   i | ]$\}}| krt  | tr||qS r'   )r\   r   )r0   r3   r4   r]   r'   r(   r5     s
     z#_filterLocation.<locals>.<dictcomp>)rx   )rK   r}   r'   r]   r(   rm     s    
rm   )/__doc__
__future__r   r>   loggingrq   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)   rC   rI   rJ   rA   ru   rj   rm   r'   r'   r'   r(   <module>   s6   $(
$

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