U
    Dvf'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 )/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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 )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-qlge9rch/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 )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)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 sqtfdd
|
j D std|
j|
j qt }||
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 qt }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|rt< |; }|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pi |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rnqd}q@|
||	 krq@||	 	|
}|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newRulesZ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