U
    »mœd'K  ã                   @  sz  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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ÚAxisMappingDescriptorÚ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(   úW/home/sam/Atlas/atlas_env/lib/python3.8/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| ||	–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(   )r0   ©Ú.0Úaxisr(   r(   r)   Ú
<listcomp>h   s     z%splitInterpolable.<locals>.<listcomp>c                 S  s   i | ]\}}|j |“qS r(   ©Úname)r2   ZdiscreteAxisÚvaluer(   r(   r)   Ú
<dictcomp>j   s   ÿ z%splitInterpolable.<locals>.<dictcomp>T©ÚkeepVFsr-   r.   r/   N)ÚaxesÚhasattrr	   r   Úappendr
   r   ÚminimumÚmaximumÚdefaultr6   Ú	itertoolsÚproductÚzipÚ_extractSubSpace)r   r-   r.   r/   ZdiscreteAxesZinterpolableUserRegionr3   ZvalueCombinationsr0   ZdiscreteUserLocationÚsubDocr(   r(   r)   ÚsplitInterpolable6   s4    "



ýþúrF   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
    Fr9   N)ÚgetVariableFontsr   rD   Úlibr6   )r   r-   r.   r/   ÚvfÚvfUserRegionÚvfDocr(   r(   r)   ÚsplitVariableFontsy   s    
úrL   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)rF   rL   ZformatVersion)r   ZvfsZ	_locationrE   ZvfNamerK   r(   r(   r)   Úconvert5to4ž   s    	rM   r   )r   Ú
userRegionr:   r-   r.   r/   r    c                  sV  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g  |_}	dd„ |jD ƒ‰ˆ jD �] }
t‡fd	d
„|
j ¡ D ƒƒsâq¾t‡fdd
„|
j ¡ D ƒƒ�st d|
j|
j¡ q¾tƒ }| |
j ¡ ¡ | |
j ¡ ¡ ˆ jD ]h}|j|k�rT�q@ˆ|j ‰ˆj|jk�s”ˆjd k	�r†ˆj|jk�s”ˆj|jk�r@t d|j¡ �q@�q@|	 t|
j|
jd�¡ q¾tˆ ˆƒ}tˆ j|ƒ|_ˆ j |_ ˆ j!D ]`}t"ˆ  #|j$¡ˆƒ�s�qè| %t&|j'|j(|j)|jt*ˆ||ƒƒ|j+|j,|j-|j.|j/|j0d�¡ �qè| 1¡ }ˆ  1¡ }|d k	�rv|d k	�rv|j2|_2|�rÎˆ  3¡ D ]H}t4ˆ |ƒ}t5|ˆƒ�r„| 6t7|j|j'‡fdd„|j8D ƒ|j9d�¡ �q„ˆ j:D �]r}t"| ;ˆ ¡ˆƒ�sð�qÔ|�ræt<ˆ | ;ˆ ¡ƒ}|j,�p|j= >d¡}|j-�p,|j? >d¡}| @tA|j'�pF|ˆ ||ƒ|j(|j)|j�pb|› d|› �ˆ�rli n|jBt*ˆ||ƒƒ|||jC�pŒ|jC|jD�pž|jE >d¡|jF�pª|jF|j2�p¶|j=|jG�pÂ|j?|jH�pÎ|jE|jI�pØi |j9d�¡ n`| @tA|j'|j(|j)|jˆ�ri n|jBt*ˆ||ƒƒ|j,|j-|jC|jD|jF|j2|jG|jH|jI|j9d�¡ �qÔˆ j9|_9|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?   )r2   ÚuserZdesign)Úranger(   r)   r4   Ú   s
    
 þz$_extractSubSpace.<locals>.<listcomp>)
Útagr6   Ú
labelNamesÚhiddenr>   r@   r?   ÚmapZaxisOrderingZ
axisLabelsc                 S  s   h | ]
}|j ’qS r(   r5   r1   r(   r(   r)   Ú	<setcomp>æ   s     z#_extractSubSpace.<locals>.<setcomp>c                 3  s   | ]}|ˆ kV  qd S rO   r(   r1   ©Ú
subDocAxesr(   r)   Ú	<genexpr>è   s     z#_extractSubSpace.<locals>.<genexpr>c                 3  s   | ]}|ˆ kV  qd S rO   r(   r1   rZ   r(   r)   r\   ê   s     zPIn axis mapping from input %s, some output axes are not in the variable-font: %szALimiting axis ranges used in <mapping> elements not supported: %s)ÚinputLocationÚoutputLocation)ÚfilenameÚpathÚfontr6   rP   Ú	layerNamer#   r&   ÚmuteKerningÚmuteInfoÚmutedGlyphNamesc                   s    g | ]}t ˆ |j tƒr|‘qS r(   )Ú
isinstancer6   r   )r2   Z
axisSubset©rN   r(   r)   r4   :  s   þ)r6   r_   ÚaxisSubsetsrH   r!   ú )r_   r`   ra   r6   ÚuserLocationrP   r#   r&   ÚpostScriptFontNameÚstyleMapFamilyNameÚstyleMapStyleNameÚlocalisedFamilyNameÚlocalisedStyleNameÚlocalisedStyleMapFamilyNameÚlocalisedStyleMapStyleNamerH   )Jr   r;   r6   rf   r   r<   r	   r
   ZaddAxisrU   rV   rW   Úmaxr>   r@   Úminr?   rX   ZaxisMappingsÚallr]   Úkeysr^   ÚLOGGERÚerrorÚsetÚupdater=   r   r   Ú_subsetRulesBasedOnConditionsÚrulesZrulesProcessingLastÚsourcesr   Zmap_backwardrP   Z	addSourcer   r_   r`   ra   Ú_filterLocationrb   r#   r&   rc   rd   re   ZfindDefaultrn   rG   r   r   ZaddVariableFontr   rh   rH   Z	instancesZgetFullUserLocationr   r$   r%   r'   ZaddInstancer   rj   rk   rl   ZstyleMapFamilyNamesrm   ro   rp   rq   )r   rN   r:   r-   r.   r/   rE   rR   r3   ZmappingsÚmappingZmappingAxesÚdesignRegionÚsourceZ	vfDefaultZ
oldDefaultrI   rJ   r   r   r#   r&   r(   )r   r.   rT   r[   rN   r)   rD   ¯   sJ   	





þïÿ
ý


ÿþ
þ
ýþþÿ

 ÿóÿ

þøÿ
 ÿ
æÿ ÿîÿrD   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?   r6   )r%   r   ÚmathÚinf)r�   ÚcÚ	conditionr>   r?   r(   r(   r)   Ú_conditionSetFrom‡  s    þr†   zList[RuleDescriptor])r{   r   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rnq´d}q@|
||	 kr‚q@||	  	|
¡}|d k	r°| 
|	|j|jdœ¡ q@d}q@|s$|j 
|¡ q$|jr| 
|¡ q|S )N)r6   ÚconditionSetsÚsubsFT)r6   r>   r?   )r   r6   rˆ   r‡   r†   Úitemsrf   ÚfloatÚintÚintersectionr=   r>   r?   )r{   r   ZnewRulesÚruleZnewRuleZconditionsetÚcsZnewConditionsetZdiscardConditionsetZselectionNameZselectionValuerŒ   r(   r(   r)   rz   ’  sD      ÿ
ýÿ	rz   zDict[str, float])rN   Úlocationr    c                   s   ‡ fdd„|  ¡ D ƒS )Nc                   s,   i | ]$\}}|ˆ krt ˆ | tƒr||“qS r(   )rf   r   )r2   r6   r7   rg   r(   r)   r8   ×  s
    þ z#_filterLocation.<locals>.<dictcomp>)r‰   )rN   r�   r(   rg   r)   r}   Ó  s    
þr}   )0Ú__doc__Ú
__future__r   rA   Úloggingr‚   Útypingr   r   r   r   r   r   r	   ZfontTools.designspaceLibr
   r   r   r   r   r   r   r   r   Z"fontTools.designspaceLib.statNamesr   r   ZfontTools.designspaceLib.typesr   r   r   r   r   r   r   Ú	getLoggerÚ__name__rv   r   r,   r*   rF   rL   rM   rD   r†   rz   r}   r(   r(   r(   r)   Ú<module>   s6   $,$

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