a
    èÝEbwâ  ã                   @   sF  d dl mZ d dlmZmZ d dlZd dlmZmZ d dl	Z	d dl
Z
d dlmZ d dlmZ g d¢ZdZed	 Zd
d„ Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zdd„ ZG dd„ deƒZd d!„ ZG d"d#„ d#eƒZ G d$d%„ d%eƒZ!G d&d'„ d'eƒZ"G d(d)„ d)eeƒZ#dS )*é    )ÚLogMixin)ÚtobytesÚtostrN)ÚBytesIOÚStringIO)Úetree)Úplistlib)ÚDesignSpaceDocumentErrorÚDesignSpaceDocumentÚSourceDescriptorÚInstanceDescriptorÚAxisDescriptorÚRuleDescriptorÚBaseDocReaderÚBaseDocWriterz&{http://www.w3.org/XML/1998/namespace}Úlangc                 C   s>   t j|  tjj¡Ž }|  d¡r(d| }n|  d¡r:d| }|S )z<Normalize paths using forward slash to work also on Windows.ú/z\\z//)Ú	posixpathÚjoinÚsplitÚosÚpathÚsepÚ
startswith)r   Únew_path© r   úa/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/fontTools/designspaceLib/__init__.pyÚposix   s    


r   c                    s"   ‡ fdd„}‡ fdd„}t ||ƒS )Nc                    s
   t | ˆ ƒS ©N)Úgetattr©Úself©Úprivate_namer   r   Úgetter+   s    z"posixpath_property.<locals>.getterc                    s    |d urt |ƒ}t| ˆ |ƒ d S r   )r   Úsetattr)r!   Úvaluer"   r   r   Úsetter/   s    z"posixpath_property.<locals>.setter)Úproperty)r#   r$   r'   r   r"   r   Úposixpath_property*   s    r)   c                   @   s   e Zd Zddd„Zdd„ ZdS )r	   Nc                 C   s   || _ || _d S r   )ÚmsgÚobj)r!   r*   r+   r   r   r   Ú__init__9   s    z!DesignSpaceDocumentError.__init__c                 C   s"   t | jƒ| jd urd| j nd S )Nz: %rÚ )Ústrr*   r+   r    r   r   r   Ú__str__=   s    ÿz DesignSpaceDocumentError.__str__)N)Ú__name__Ú
__module__Ú__qualname__r,   r/   r   r   r   r   r	   8   s   
r	   c                   @   s   e Zd Zdd„ ZdS )ÚAsDictMixinc                 C   s\   i }| j  ¡ D ]H\}}| d¡r"qt|dƒr6| ¡ }nt|tƒrNdd„ |D ƒ}|||< q|S )NÚ_Úasdictc                 S   s"   g | ]}t |d ƒr| ¡ n|‘qS )r5   )Úhasattrr5   ©Ú.0Úvr   r   r   Ú
<listcomp>L   s   z&AsDictMixin.asdict.<locals>.<listcomp>)Ú__dict__Úitemsr   r6   r5   Ú
isinstanceÚlist)r!   ÚdÚattrr&   r   r   r   r5   D   s    



ÿ
zAsDictMixin.asdictN)r0   r1   r2   r5   r   r   r   r   r3   B   s   r3   c                   @   s   e Zd ZdZdd„ ZdS )ÚSimpleDescriptorz% Containers for a bunch of attributesc                 C   s\   | j D ]P}zt| |ƒt||ƒks$J ‚W q tyT   td|t| |ƒdt||ƒƒ Y q0 qd S )Nzfailed attributez!=)Ú_attrsr   ÚAssertionErrorÚprint)r!   Úotherr@   r   r   r   ÚcompareX   s
    
zSimpleDescriptor.compareN)r0   r1   r2   Ú__doc__rF   r   r   r   r   rA   S   s   rA   c                   @   sV   e Zd ZdZdZg d¢Zddddddddddddddddœdd„Zed	ƒZed
ƒZ	dS )r   z/Simple container for data related to the sourceÚsource)Úfilenamer   ÚnameÚ	layerNameÚlocationÚcopyLibÚ
copyGroupsÚcopyFeaturesÚmuteKerningÚmuteInfoÚmutedGlyphNamesÚ
familyNameÚ	styleNameNF©rI   r   ÚfontrJ   rL   rK   rS   rT   rM   ÚcopyInforN   rO   rP   rQ   rR   c                C   sb   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _|| _|| _|pZg | _d S r   rU   )r!   rI   r   rV   rJ   rL   rK   rS   rT   rM   rW   rN   rO   rP   rQ   rR   r   r   r   r,   k   s    zSourceDescriptor.__init__Ú_pathÚ	_filename)
r0   r1   r2   rG   ÚflavorrB   r,   r)   r   rI   r   r   r   r   r   a   s*   
ï3r   c                   @   s*   e Zd ZdZg d¢Zddddœdd„ZdS )r   a`  Represents the rule descriptor element

    .. code-block:: xml

        <!-- optional: list of substitution rules -->
        <rules>
            <rule name="vertical.bars">
                <conditionset>
                    <condition minimum="250.000000" maximum="750.000000" name="weight"/>
                    <condition minimum="100" name="width"/>
                    <condition minimum="10" maximum="40" name="optical"/>
                </conditionset>
                <sub name="cent" with="cent.alt"/>
                <sub name="dollar" with="dollar.alt"/>
            </rule>
        </rules>
    ©rJ   ÚconditionSetsÚsubsNc                C   s   || _ |pg | _|pg | _d S r   r[   )r!   rJ   r\   r]   r   r   r   r,   ¶   s    
zRuleDescriptor.__init__)r0   r1   r2   rG   rB   r,   r   r   r   r   r   ¢   s   r   c                    s   t ‡ fdd„| jD ƒƒS )zK Return True if any of the rule's conditionsets matches the given location.c                 3   s   | ]}t |ˆ ƒV  qd S r   )ÚevaluateConditions)r8   Úc©rL   r   r   Ú	<genexpr>À   ó    zevaluateRule.<locals>.<genexpr>)Úanyr\   )ÚrulerL   r   r`   r   ÚevaluateRule¾   s    re   c                 C   s‚   | D ]x}||d  }|  d¡du r6||d kr| dS q|  d¡du rX|d |kr| dS q|d |  krt|d ksn  dS qdS )z» Return True if all the conditions matches the given location.
        If a condition has no minimum, check for < maximum.
        If a condition has no maximum, check for > minimum.
    rJ   ÚminimumNÚmaximumFT)Úget)Ú
conditionsrL   Úcdr&   r   r   r   r^   Ã   s    r^   c           	      C   sl   g }| D ]^}t ||ƒr|D ]B}d}|jD ]\}}||kr(d} qBq(|rR| |¡ q| |¡ q|}g }q|S )zY Apply these rules at this location to these glyphnames
        - rule order matters
    FT)re   r]   Úappend)	ÚrulesrL   Z
glyphNamesZnewNamesrd   rJ   ZswapÚaÚbr   r   r   ÚprocessRulesÕ   s    
ro   c                   @   s°   e Zd ZdZdZdZg d¢Zdddddddddddddddddddœdd	„Zed
ƒZ	edƒZ
ddd„Zddd„Zddd„Zddd„Zd dd„Zd!dd„Zd"dd„Zd#dd„ZdS )$r   z1Simple container for data related to the instanceÚinstanceÚen)r   rJ   rL   rS   rT   ÚpostScriptFontNameÚstyleMapFamilyNameÚstyleMapStyleNameÚkerningÚinfoÚlibNT©rI   r   rV   rJ   rL   rS   rT   rr   rs   rt   ÚlocalisedFamilyNameÚlocalisedStyleNameÚlocalisedStyleMapFamilyNameÚlocalisedStyleMapStyleNameÚglyphsru   rv   rw   c                C   sˆ   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|pBi | _
|pLi | _|pVi | _|p`i | _|pji | _|| _|| _|p€i | _d S r   rx   )r!   rI   r   rV   rJ   rL   rS   rT   rr   rs   rt   ry   rz   r{   r|   r}   ru   rv   rw   r   r   r   r,   û   s&    





zInstanceDescriptor.__init__rX   rY   c                 C   s   t |ƒ| j|< d S r   )r   rz   )r!   rT   ÚlanguageCoder   r   r   ÚsetStyleName,  s    zInstanceDescriptor.setStyleNamec                 C   s   | j  |¡S r   )rz   rh   ©r!   r~   r   r   r   ÚgetStyleName/  s    zInstanceDescriptor.getStyleNamec                 C   s   t |ƒ| j|< d S r   )r   ry   )r!   rS   r~   r   r   r   ÚsetFamilyName2  s    z InstanceDescriptor.setFamilyNamec                 C   s   | j  |¡S r   )ry   rh   r€   r   r   r   ÚgetFamilyName5  s    z InstanceDescriptor.getFamilyNamec                 C   s   t |ƒ| j|< d S r   )r   r|   )r!   rt   r~   r   r   r   ÚsetStyleMapStyleName8  s    z'InstanceDescriptor.setStyleMapStyleNamec                 C   s   | j  |¡S r   )r|   rh   r€   r   r   r   ÚgetStyleMapStyleName;  s    z'InstanceDescriptor.getStyleMapStyleNamec                 C   s   t |ƒ| j|< d S r   )r   r{   )r!   rs   r~   r   r   r   ÚsetStyleMapFamilyName>  s    z(InstanceDescriptor.setStyleMapFamilyNamec                 C   s   | j  |¡S r   )r{   rh   r€   r   r   r   ÚgetStyleMapFamilyNameA  s    z(InstanceDescriptor.getStyleMapFamilyName)rq   )rq   )rq   )rq   )rq   )rq   )rq   )rq   )r0   r1   r2   rG   rZ   Z_defaultLanguageCoderB   r,   r)   r   rI   r   r�   r‚   rƒ   r„   r…   r†   r‡   r   r   r   r   r   ë   sB   ì.






r   c                 C   s–   dt dd�fdt dd�fdt dd�fdt d	d�fd
t dd�fdœ}|  ¡ |v rZ||  ¡  S t| ƒdk r|| ddt| ƒ   }n| d d… }|t | d�fS )NZwghtZWeight)rq   ZwdthZWidthZopszzOptical SizeZslntZSlantZitalZItalic)ÚweightÚwidthZopticalZslantÚitalicé   Ú*)ÚdictÚlowerÚlen)rJ   ÚnamesÚtagr   r   r   ÚtagForAxisNameE  s    ûr’   c                	   @   sP   e Zd ZdZdZg d¢Zdddddddddœdd„Zd	d
„ Zdd„ Zdd„ Z	dS )r   zH Simple container for the axis data
        Add more localisations?
    Úaxis)r‘   rJ   rg   rf   ÚdefaultÚmapNF)r‘   rJ   Ú
labelNamesrf   r”   rg   Úhiddenr•   c          	      C   s<   || _ || _|pi | _|| _|| _|| _|| _|p4g | _d S r   )r‘   rJ   r–   rf   rg   r”   r—   r•   )	r!   r‘   rJ   r–   rf   r”   rg   r—   r•   r   r   r   r,   ^  s    
zAxisDescriptor.__init__c              
   C   s(   t | j| j| j| j| j| j| j| jd�S )N)r‘   rJ   r–   rg   rf   r”   r—   r•   )	r�   r‘   rJ   r–   rg   rf   r”   r—   r•   r    r   r   r   Ú	serializev  s    øzAxisDescriptor.serializec                 C   s,   ddl m} | js|S ||dd„ | jD ƒƒS )Nr   ©ÚpiecewiseLinearMapc                 S   s   i | ]\}}||“qS r   r   ©r8   Úkr9   r   r   r   Ú
<dictcomp>ˆ  rb   z.AxisDescriptor.map_forward.<locals>.<dictcomp>©ÚfontTools.varLib.modelsrš   r•   ©r!   r9   rš   r   r   r   Úmap_forwardƒ  s    zAxisDescriptor.map_forwardc                 C   s,   ddl m} | js|S ||dd„ | jD ƒƒS )Nr   r™   c                 S   s   i | ]\}}||“qS r   r   r›   r   r   r   r�   �  rb   z/AxisDescriptor.map_backward.<locals>.<dictcomp>rž   r    r   r   r   Úmap_backwardŠ  s    zAxisDescriptor.map_backward)
r0   r1   r2   rG   rZ   rB   r,   r˜   r¡   r¢   r   r   r   r   r   W  s   ör   c                   @   s¤   e Zd ZdZeZeZeZ	e
Zedd„ ƒZedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Zd!dd„Zd"dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ ZdS )#r   z    c                 C   s   |   ¡ S r   )ÚaxisDescriptorClass©Úclsr   r   r   ÚgetAxisDecriptor™  s    zBaseDocWriter.getAxisDecriptorc                 C   s   |   ¡ S r   )ÚsourceDescriptorClassr¤   r   r   r   ÚgetSourceDescriptor�  s    z!BaseDocWriter.getSourceDescriptorc                 C   s   |   ¡ S r   )ÚinstanceDescriptorClassr¤   r   r   r   ÚgetInstanceDescriptor¡  s    z#BaseDocWriter.getInstanceDescriptorc                 C   s   |   ¡ S r   )ÚruleDescriptorClassr¤   r   r   r   ÚgetRuleDescriptor¥  s    zBaseDocWriter.getRuleDescriptorc                 C   s<   || _ || _d| _t d¡| _| j| jjd< g | _g | _d S )Nz4.1ZdesignspaceÚformat)	r   ÚdocumentObjectZdocumentVersionÚETÚElementÚrootÚattribÚ_axesÚ_rules)r!   ÚdocumentPathr®   r   r   r   r,   ©  s    zBaseDocWriter.__init__TúUTF-8c           
      C   s"  | j jr| j t d¡¡ | j jD ]}|  |¡ q"| j jrjt| j ddƒrRddi}ni }| j t d|¡¡ | j jD ]}|  	|¡ qr| j j
rœ| j t d¡¡ | j j
D ]}|  |¡ q¤| j jrÎ| j t d¡¡ | j jD ]}|  |¡ qÖ| j jrü|  | j j¡ t | j¡}	|	j| j|d	||d
� d S )NÚaxesÚrulesProcessingLastFÚ
processingÚlastrl   ÚsourcesÚ	instancesÚxml)ÚencodingÚmethodÚxml_declarationZpretty_print)r®   r·   r±   rk   r¯   r°   Ú_addAxisrl   r   Ú_addRuler»   Ú
_addSourcer¼   Ú_addInstancerw   Ú_addLibÚElementTreeÚwriter   )
r!   Úprettyr¾   rÀ   Ú
axisObjectÚ
attributesÚ
ruleObjectÚsourceObjectÚinstanceObjectÚtreer   r   r   rÇ   ²  s:    
ûzBaseDocWriter.writeNc           
      C   sÆ   t  d¡}|dur||jd< | j ¡ }| ¡ D ]\}}||v r.|||< q.| ¡ D ]l\}}t  d¡}	||	jd< t|ƒtkr¢|  |d ¡|	jd< |  |d ¡|	jd< n|  |¡|	jd< | 	|	¡ qP||fS )	z, Convert Location dict to a locationElement.rL   NrJ   Ú	dimensionr   Úxvalueé   Úyvalue)
r¯   r°   r²   r®   ÚnewDefaultLocationr<   ÚtypeÚtupleÚ
intOrFloatrk   )
r!   ZlocationObjectrJ   Z
locElementZvalidatedLocationZaxisNameZ	axisValueZdimensionNameZdimensionValueZ
dimElementr   r   r   Ú_makeLocationElement×  s     





z"BaseDocWriter._makeLocationElementc                 C   s   t |ƒ|krd| S d| S )Nz%dz%f©Úint)r!   Únumr   r   r   rÖ   ì  s    zBaseDocWriter.intOrFloatc           	      C   sH  | j  |¡ t d¡}|jd ur,|j|jd< |jD ]´}t d¡}|D ]Ž}| d¡d u rf| d¡d u rfqDt d¡}| d¡|jd< | d¡d ur¤|  | d¡¡|jd< | d¡d urÈ|  | d¡¡|jd< | |¡ qDt	|ƒr2| |¡ q2|j
D ]4}t d¡}|d |jd< |d	 |jd
< | |¡ qît	|ƒ�rD| j d¡d  |¡ d S )Nrd   rJ   Zconditionsetrf   rg   Ú	conditionÚsubr   rÑ   Úwithú.rules)r´   rk   r¯   r°   rJ   r²   r\   rh   rÖ   r�   r]   r±   Úfindall)	r!   rË   ÚruleElementri   ZconditionsetElementÚcondÚconditionElementrÜ   Ú
subElementr   r   r   rÂ   ñ  s2    







zBaseDocWriter._addRulec           	      C   s  | j  |¡ t d¡}|j|jd< |j|jd< |  |j¡|jd< |  |j	¡|jd< |  |j
¡|jd< |jrtd|jd< t|j ¡ ƒD ],\}}t d	¡}||jt< ||_| |¡ q‚|jrú|jD ]<\}}t d
¡}|  |¡|jd< |  |¡|jd< | |¡ q¼| j d¡d  |¡ d S )Nr“   r‘   rJ   rf   rg   r”   Ú1r—   Ú	labelnamer•   ÚinputÚoutputz.axesr   )r³   rk   r¯   r°   r‘   r²   rJ   rÖ   rf   rg   r”   r—   Úsortedr–   r<   ÚXML_LANGÚtextr•   r±   rß   )	r!   rÉ   ÚaxisElementr~   Z	labelNameZlanguageElementÚ
inputValueÚoutputValueÚ
mapElementr   r   r   rÁ     s*    




zBaseDocWriter._addAxisc                 C   s@  t  d¡}|jd ur |j|jd< |jd ur6|j|jd< |jd urL|j|jd< |jr¦t|j ¡ ƒ}| 	¡  |D ]8}|dkrzqlt  d¡}||jt
< | |¡|_| |¡ ql|j�rt|j ¡ ƒ}| 	¡  |D ]8}|dkrÖqÈt  d¡}||jt
< | |¡|_| |¡ qÈ|j�rdt|j ¡ ƒ}| 	¡  |D ]>}|dk�r6�q$t  d¡}||jt
< | |¡|_| |¡ �q$|j�rÆt|j ¡ ƒ}| 	¡  |D ]>}|dk�r˜�q†t  d¡}||jt
< | |¡|_| |¡ �q†|jd u�rî|  |j¡\}	|_| |	¡ |jd u�r|j|jd< |jd u�r|j|jd	< |jd u�r6|j|jd< |jd u�rN|j|jd< |j�r¼| d
¡g k�rzt  d¡}
| |
¡ | d
¡d }
t|j ¡ ƒD ]$\}}|  ||||¡}|
 |¡ �q–|j�rØt  d¡}| |¡ |j �rôt  d¡}| |¡ |j!�r&t  d¡}| t"j#|j!dd�¡ | |¡ | j$ d¡d  |¡ d S )Nrp   rJ   Ú
familynameÚ	stylenamerq   ÚstylemapstylenameÚstylemapfamilynamerI   Úpostscriptfontnamez.glyphsr}   r   ru   rv   rw   r‹   ©Úindent_levelz
.instances)%r¯   r°   rJ   r²   rS   rT   rz   r>   ÚkeysÚsortré   r�   rê   rk   ry   rƒ   r|   r…   r{   r‡   rL   r×   rI   rr   rs   rt   r}   rß   rè   r<   Ú_writeGlyphElementru   rv   rw   r   Útotreer±   )r!   rÍ   ÚinstanceElementZlanguageCodesÚcodeZlocalisedStyleNameElementZlocalisedFamilyNameElementZ!localisedStyleMapStyleNameElementZ"localisedStyleMapFamilyNameElementÚlocationElementZglyphsElementÚ	glyphNameÚdataÚglyphElementÚkerningElementÚinfoElementÚ
libElementr   r   r   rÄ   %  sš    






















zBaseDocWriter._addInstancec                 C   sÖ  t  d¡}|jd ur |j|jd< |jd urF|j d¡dkrF|j|jd< |jd ur\|j|jd< |jd urr|j|jd< |jd urˆ|j|jd< |j	r¬t  d	¡}d
|jd< | 
|¡ |jrÐt  d¡}d
|jd< | 
|¡ |jrôt  d¡}d
|jd< | 
|¡ |j�s|j�r<t  d¡}|j�r d
|jd< |j�r2d
|jd< | 
|¡ |j�rbt  d¡}d
|jd< | 
|¡ |j�r |jD ].}t  d¡}	||	jd< d
|	jd< | 
|	¡ �qp|  |j¡\}
|_| 
|
¡ | j d¡d  
|¡ d S )NrH   rI   Ztemp_masterr   rJ   rï   rð   Úlayerrw   rä   ÚcopyÚgroupsÚfeaturesrv   Úmuteru   Úglyphz.sources)r¯   r°   rI   r²   rJ   ÚfindrS   rT   rK   rM   rk   rN   rO   rW   rQ   rP   rR   r×   rL   r±   rß   )r!   rÌ   ÚsourceElementr  ÚgroupsElementÚfeaturesElementr  r   rJ   rÿ   rü   r   r   r   rÃ   v  sX    


























zBaseDocWriter._addSourcec                 C   s.   t  d¡}| tj|dd�¡ | j |¡ d S )Nrw   é   rô   )r¯   r°   rk   r   rù   r±   )r!   r�   r  r   r   r   rÅ   ¥  s    
zBaseDocWriter._addLibc                 C   sz  t  d¡}| d¡rd|jd< | d¡d urLd dd„ | d¡D ƒ¡|jd< | d	¡d ur||  | d	¡¡\}|d	< | |¡ |d urŽ||jd
< | d¡d ur¼t  d¡}| d¡|_| |¡ | d¡d u�rvt  d¡}| d¡D ]Š}	t  d¡}
|	 d¡d u�r|	 d¡|
jd< |	 d¡d u�r.|	 d¡|
jd< |	 d¡d u�r`|  |	 d¡¡\}|	d< |
 |¡ | |
¡ qà| |¡ |S )Nr  r  rä   Úunicodesú c                 S   s   g | ]}t |ƒ‘qS r   )Úhex©r8   Úur   r   r   r:   ¯  rb   z4BaseDocWriter._writeGlyphElement.<locals>.<listcomp>ÚunicodeÚinstanceLocationrJ   ÚnoteÚmastersZmasterrý   Ú	glyphnamerV   rH   rL   )r¯   r°   rh   r²   r   r×   rk   rê   )r!   rú   rÍ   rý   rþ   rÿ   rü   ÚnoteElementZmastersElementÚmÚmasterElementr   r   r   rø   ª  s8    


 







z BaseDocWriter._writeGlyphElement)Tr¶   T)N)r0   r1   r2   Z_whiteSpacer   r«   r   r£   r   r§   r   r©   Úclassmethodr¦   r¨   rª   r¬   r,   rÇ   r×   rÖ   rÂ   rÁ   rÄ   rÃ   rÅ   rø   r   r   r   r   r   ’  s.   



	
%
Q/r   c                   @   s¦   e Zd ZeZeZeZe	Z
dd„ Zedd„ ƒZdd„ Zdd„ Zd#d
d„Zdd„ Zdd„ Zdd„ Zdd„ Zd$dd„Zd%dd„Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd	S )&r   c                 C   s^   || _ || _t | j ¡}| ¡ | _| jj dd¡| j_g | _	g | _
g | _g | _i | _d| _d S )Nr­   z3.0T)r   r®   r¯   ÚparseÚgetrootr±   r²   rh   ÚformatVersionr³   rl   r»   r¼   ÚaxisDefaultsÚ_strictAxisNames)r!   rµ   r®   rÎ   r   r   r   r,   Ï  s    
zBaseDocReader.__init__c                 C   s$   t t|dd�ƒ}| ||ƒ}d |_|S )Núutf-8)r¾   )r   r   r   )r¥   Ústringr®   Úfr!   r   r   r   Ú
fromstringÜ  s    
zBaseDocReader.fromstringc                 C   s,   |   ¡  |  ¡  |  ¡  |  ¡  |  ¡  d S r   )ÚreadAxesÚ	readRulesÚreadSourcesÚreadInstancesÚreadLibr    r   r   r   Úreadã  s
    zBaseDocReader.readc                 C   s  g }| j  d¡}|d urF|j dd¡}|dvr:td| ƒ‚|dk| j_| j  d¡D ]´}|  ¡ }|j d¡ }|_	|  
||¡}|r˜|j |¡ | j d	¡ | d
¡D ]$}|  
||¡}	|	d ur¢|j |	¡ q¢| d¡D ](}
|
jd }|
jd }|j ||f¡ qÒ| |¡ qR|| j_d S )NrÞ   r¹   Úfirst>   rº   r+  zO<rules> processing attribute value is not valid: %r, expected 'first' or 'last'rº   z.rules/rulerJ   zWFound stray rule conditions outside a conditionset. Wrapped them in a new conditionset.z.conditionsetz.subrÝ   )r±   r	  r²   rh   r	   r®   r¸   rß   r«   rJ   Ú_readConditionElementsr\   rk   Úlogrv   r]   rl   )r!   rl   ZrulesElementZprocessingValuerà   rË   ÚruleNameZexternalConditionsZconditionSetElementZconditionSetrã   rm   rn   r   r   r   r&  ê  sH    ÿÿþÿþ

zBaseDocReader.readRulesNc                 C   sÂ   g }|  d¡D ]®}i }|j d¡}|d ur8t|ƒ|d< nd |d< |j d¡}|d urbt|ƒ|d< nd |d< |j d¡|d< | d¡d u r²| d¡d u r²td|d urªd| nd ƒ‚| |¡ q|S )Nz
.conditionrf   rg   rJ   z5condition missing required minimum or maximum in rulez '%s'r-   )rß   r²   rh   Úfloatr	   rk   )r!   ZparentElementr.  Zcdsrâ   rj   ZcdMinZcdMaxr   r   r   r,    s(    ÿÿz$BaseDocReader._readConditionElementsc           
      C   s$  | j  d¡}|sd S |D �]}|  ¡ }|j d¡|_t|j d¡ƒ|_t|j d¡ƒ|_|j dd¡rld|_	t|j d¡ƒ|_
|j d	¡|_| d
¡D ]0}t|jd ƒ}t|jd ƒ}|j ||f¡ q–| d¡D ].}| ¡ D ] \}}	|tkrÞt|jƒ|j|	< qÞqÒ| jj |¡ |j
| j|j< qd S )Nz
.axes/axisrJ   rf   rg   r—   FTr”   r‘   r•   ræ   rç   rå   )r±   rß   r£   r²   rh   rJ   r/  rf   rg   r—   r”   r‘   r•   rk   r<   ré   r   rê   r–   r®   r·   r  )
r!   ZaxisElementsrë   rÉ   rî   rm   rn   ZlabelNameElementÚkeyr   r   r   r   r%  +  s,    
zBaseDocReader.readAxesc                 C   s2  t | j d¡ƒD �]\}}|j d¡}|d urZ| jd urZtj tj tj 	| j¡|¡¡}nd }|j d¡}|d u rzd| }|  
¡ }||_||_||_|j d¡}|d ur®||_|j d¡}|d urÈ||_|  |¡|_|j d¡}	|	d urî|	|_| d¡D ]}
|
j d	¡d
krød|_qø| d¡D ]}|j d	¡d
k�rd|_�q| d¡D ]6}|j d	¡d
k�rdd|_|j d¡d
k�rHd|_�qH| d¡D ]}|j d	¡d
k�rŠd|_�qŠ| d¡D ]>}|j d¡}|d u �rÒ�q´|j d¡d
k�r´|j |¡ �q´| d¡D ]}|j d¡d
k�rþd|_�qþ| jj |¡ qd S )Nz.sources/sourcerI   rJ   ztemp_master.%drï   rð   r  ú.libr  rä   Tz.groupsz.infor  z	.featuresz.glyphz.kerning)Ú	enumerater±   rß   r²   rh   r   r   Úabspathr   Údirnamer§   rI   rJ   rS   rT   ÚlocationFromElementrL   rK   rM   rN   rW   rQ   rO   rR   rk   rP   r®   r»   )r!   ZsourceCountr
  rI   Z
sourcePathZ
sourceNamerÌ   rS   rT   rK   r  r  r  r  rÿ   rý   r   r   r   r   r'  F  s\    "




zBaseDocReader.readSourcesc                 C   s&   d }|  d¡D ]}|  |¡} q"q|S )Nz	.location)rß   ÚreadLocationElement)r!   ÚelementZelementLocationrü   r   r   r   r5  x  s
    
z!BaseDocReader.locationFromElementc              	   C   sî   | j r| jjstdƒ‚i }| d¡D ]Ä}|j d¡}| j rT|| jvrT| j 	d|¡ q$d }}z|j d¡}t
|ƒ}W n  ty”   | j 	d|¡ Y n0 z |j d¡}|dur´t
|ƒ}W n tyÈ   Y n0 |durà||f||< q$|||< q$|S )	z Format 0 location reader zNo axes definedz
.dimensionrJ   z#Location with undefined axis: "%s".NrÐ   z%KeyError in readLocation xValue %3.3frÒ   )r   r®   r·   r	   rß   r²   rh   r  r-  Úwarningr/  Ú
ValueError)r!   rü   ÚlocZdimensionElementZdimNameZxValueZyValuer   r   r   r6    s0    
z!BaseDocReader.readLocationElementTc                 C   s,   | j  d¡}|D ]}| j||||d� qd S )Nz.instances/instance)Ú
makeGlyphsÚmakeKerningÚmakeInfo)r±   rß   Ú_readSingleInstanceElement)r!   r;  r<  r=  ZinstanceElementsrú   r   r   r   r(  œ  s    zBaseDocReader.readInstancesc                 C   s|  |j  d¡}|d ur<| jjd ur<tj tj | jj¡|¡}nd }|  ¡ }||_||_|j  d¡}|d urn||_	|j  d¡}	|	d urˆ|	|_
|j  d¡}
|
d ur¢|
|_|j  d¡}|d ur¼||_|j  d¡}|d urÖ||_|j  d¡}|d urð||_| d¡D ]4}| ¡ D ]&\}}|tk�r|j}| ||¡ �qqú| d¡D ]6}| ¡ D ]&\}}|tk�rF|j}| ||¡ �qF�q:| d¡D ]6}| ¡ D ]&\}}|tk�rˆ|j}| ||¡ �qˆ�q|| d¡D ]6}| ¡ D ]&\}}|tk�rÊ|j}| ||¡ �qÊ�q¾|  |¡}|d u�r||_| d¡D ]}|  ||¡ �q| d	¡D ]}|  ||¡ �q8| d
¡D ]}|  ||¡ �qV| jj |¡ d S )NrI   rJ   rï   rð   ró   rò   rñ   z.glyphs/glyphrv   rw   )r²   rh   r®   r   r   r   r4  r©   rI   rJ   rS   rT   rr   rs   rt   rß   r<   ré   rê   r   r‚   r„   r†   r5  rL   ÚreadGlyphElementÚreadInfoElementÚreadLibElementr¼   rk   )r!   rú   r;  r<  r=  rI   ZinstancePathrÍ   rJ   rï   rð   rr   rs   rt   ZstyleNameElementr0  r   rT   ZfamilyNameElementrS   ZstyleMapStyleNameElementZstyleMapFamilyNameElementr  rÿ   r  r  r   r   r   r>  ¡  sn    





z(BaseDocReader._readSingleInstanceElementc                 C   s   t  |d ¡|_dS )z,Read the lib element for the given instance.r   N)r   Úfromtreerw   )r!   r  rÍ   r   r   r   rA  Ü  s    zBaseDocReader.readLibElementc                 C   s
   d|_ dS )z Read the info element.TN)rv   )r!   r  rÍ   r   r   r   r@  à  s    zBaseDocReader.readInfoElementc                 C   s   |   |¡}| |¡ dS )z Read the kerning element.N)r5  Z
addKerning)r!   r   rÍ   ZkerningLocationr   r   r   ÚreadKerningElementä  s    
z BaseDocReader.readKerningElementc                 C   sR  i }|j  d¡}|du r tdƒ‚|j  d¡}|dkr<d|d< |j  d¡}|dur�z dd	„ | d
¡D ƒ}||d< W n tyŽ   td| ƒ‚Y n0 | d¡D ]}|j|d<  q®qš|  |¡}|durÈ||d< d}	| d¡D ]Z}
|
j  d¡}|  |
¡}|
j  d¡}|du �r
|}t|||d�}|	du �r&g }	|	 	|¡ qÖ|	du�rD|	|d< ||j
|< dS )a÷  
        Read the glyph element:

        .. code-block:: xml

            <glyph name="b" unicode="0x62"/>
            <glyph name="b"/>
            <glyph name="b">
                <master location="location-token-bbb" source="master-token-aaa2"/>
                <master glyphname="b.alt1" location="location-token-ccc" source="master-token-aaa3"/>
                <note>
                    This is an instance from an anisotropic interpolation.
                </note>
            </glyph>
        rJ   Nz#Glyph object without name attributer  rä   Tr  c                 S   s   g | ]}t |d ƒ‘qS )é   rØ   r  r   r   r   r:     rb   z2BaseDocReader.readGlyphElement.<locals>.<listcomp>r  r  z"unicode values %s are not integersz.noter  r  z.masters/masterrH   r  )rV   rL   rý   r  )r²   rh   r	   r   r9  rß   rê   r5  r�   rk   r}   )r!   rÿ   rÍ   Ú	glyphDatarý   r  r  r  r  ZglyphSourcesr  ZfontSourceNameZsourceLocationZmasterGlyphNamer?   r   r   r   r?  é  sJ    



þ

zBaseDocReader.readGlyphElementc                 C   s(   | j  d¡D ]}t |d ¡| j_qdS )z,Read the lib element for the whole document.r1  r   N)r±   rß   r   rB  r®   rw   )r!   r  r   r   r   r)  !  s    zBaseDocReader.readLib)N)TTT)TTT)r0   r1   r2   r   r«   r   r£   r   r§   r   r©   r,   r  r$  r*  r&  r,  r%  r'  r5  r6  r(  r>  rA  r@  rC  r?  r)  r   r   r   r   r   É  s*   
(
2

;8r   c                   @   sú   e Zd ZdZd;dd„Zed<dd„ƒZed=dd„ƒZd>d	d
„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd?d'd(„Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ ZdS )@r
   z+ Read, write data from the designspace fileNc                 C   sl   d | _ d | _d | _g | _g | _g | _g | _d| _d | _i | _	|d urL|| _
nt| _
|d urb|| _nt| _d S )NF)r   rI   r  r»   r¼   r·   rl   r¸   r”   rw   ÚreaderClassr   ÚwriterClassr   )r!   rF  rG  r   r   r   r,   )  s     	zDesignSpaceDocument.__init__c                 C   s   | ||d�}|  |¡ |S ©N)rF  rG  )r*  )r¥   r   rF  rG  r!   r   r   r   ÚfromfileI  s    
zDesignSpaceDocument.fromfilec                 C   s4   | ||d�}|j  ||¡}| ¡  |jr0| ¡  |S rH  )rF  r$  r*  r»   ÚfindDefault)r¥   r"  rF  rG  r!   Úreaderr   r   r   r$  O  s    zDesignSpaceDocument.fromstringc                 C   sv   |t u s|d ur(| ¡ dkr(tƒ }d}n,|d u s8|dkrHtƒ }d}d}ntd| ƒ‚|  || ¡}|j||d� | ¡ S )Nr  Fr!  r¶   Tzunsupported encoding: '%s')r¾   rÀ   )r.   rŽ   r   r   r9  rG  rÇ   Úgetvalue)r!   r¾   r#  rÀ   Úwriterr   r   r   ÚtostringX  s    ÿ
ÿzDesignSpaceDocument.tostringc                 C   sL   t |dƒr| ¡ }|| _tj |¡| _|  || ¡}| ¡  | jrH|  	¡  d S ©NÚ
__fspath__)
r6   rP  r   r   ÚbasenamerI   rF  r*  r»   rJ  )r!   r   rK  r   r   r   r*  h  s    
zDesignSpaceDocument.readc                 C   sF   t |dƒr| ¡ }|| _tj |¡| _|  ¡  |  || ¡}| ¡  d S rO  )	r6   rP  r   r   rQ  rI   ÚupdatePathsrG  rÇ   )r!   r   rM  r   r   r   rÇ   r  s    
zDesignSpaceDocument.writec                 C   s    t j |t j | j¡¡}t|ƒS r   )r   r   Úrelpathr4  r   )r!   Z	otherPathÚrelativer   r   r   Ú_posixRelativePath{  s    z&DesignSpaceDocument._posixRelativePathc                 C   s<   | j dusJ ‚| j| j D ]}|j dur|  |j ¡|_qdS )a  
            Right before we save we need to identify and respond to the following situations:
            In each descriptor, we have to do the right thing for the filename attribute.

            case 1.
            descriptor.filename == None
            descriptor.path == None

            -- action:
            write as is, descriptors will not have a filename attr.
            useless, but no reason to interfere.


            case 2.
            descriptor.filename == "../something"
            descriptor.path == None

            -- action:
            write as is. The filename attr should not be touched.


            case 3.
            descriptor.filename == None
            descriptor.path == "~/absolute/path/there"

            -- action:
            calculate the relative path for filename.
            We're not overwriting some other value for filename, it should be fine


            case 4.
            descriptor.filename == '../somewhere'
            descriptor.path == "~/absolute/path/there"

            -- action:
            there is a conflict between the given filename, and the path.
            So we know where the file is relative to the document.
            Can't guess why they're different, we just choose for path to be correct and update filename.


        N)r   r»   r¼   rU  rI   )r!   Ú
descriptorr   r   r   rR    s    *
zDesignSpaceDocument.updatePathsc                 C   s   | j  |¡ d S r   )r»   rk   )r!   ÚsourceDescriptorr   r   r   Ú	addSource¯  s    zDesignSpaceDocument.addSourcec                 K   s    | j jf i |¤Ž}|  |¡ |S r   )rG  r§   rX  )r!   ÚkwargsrH   r   r   r   ÚaddSourceDescriptor²  s    
z'DesignSpaceDocument.addSourceDescriptorc                 C   s   | j  |¡ d S r   )r¼   rk   )r!   ZinstanceDescriptorr   r   r   ÚaddInstance·  s    zDesignSpaceDocument.addInstancec                 K   s    | j jf i |¤Ž}|  |¡ |S r   )rG  r©   r[  )r!   rY  rp   r   r   r   ÚaddInstanceDescriptorº  s    
z)DesignSpaceDocument.addInstanceDescriptorc                 C   s   | j  |¡ d S r   )r·   rk   )r!   ÚaxisDescriptorr   r   r   ÚaddAxis¿  s    zDesignSpaceDocument.addAxisc                 K   s    | j jf i |¤Ž}|  |¡ |S r   )rG  r£   r^  )r!   rY  r“   r   r   r   ÚaddAxisDescriptorÂ  s    
z%DesignSpaceDocument.addAxisDescriptorc                 C   s   | j  |¡ d S r   )rl   rk   )r!   ZruleDescriptorr   r   r   ÚaddRuleÇ  s    zDesignSpaceDocument.addRulec                 K   s    | j jf i |¤Ž}|  |¡ |S r   )rG  r«   r`  )r!   rY  rd   r   r   r   ÚaddRuleDescriptorÊ  s    
z%DesignSpaceDocument.addRuleDescriptorc                 C   s*   t  ¡ }| jD ]}| |j¡||j< q|S )z(Return default location in design space.)ÚcollectionsÚOrderedDictr·   r¡   r”   rJ   )r!   r:  r]  r   r   r   rÓ   Ï  s    
ÿz&DesignSpaceDocument.newDefaultLocationTFc                 C   st   |r8| j D ],}|jd ur|sq
| jd ur
|  |j¡|_q
|rp| jD ],}|jd urV|sVqB| jd urB|  |j¡|_qBd S r   )r»   rI   r   rU  r¼   )r!   r  r¼   ÚforcerV  r   r   r   ÚupdateFilenameFromPathÚ  s    



z*DesignSpaceDocument.updateFilenameFromPathc                 C   s
   | j  ¡ S r   )rG  r¦   r    r   r   r   ÚnewAxisDescriptorê  s    z%DesignSpaceDocument.newAxisDescriptorc                 C   s
   | j  ¡ S r   )rG  r¨   r    r   r   r   ÚnewSourceDescriptorî  s    z'DesignSpaceDocument.newSourceDescriptorc                 C   s
   | j  ¡ S r   )rG  rª   r    r   r   r   ÚnewInstanceDescriptorò  s    z)DesignSpaceDocument.newInstanceDescriptorc                 C   s    g }| j D ]}| |j¡ q
|S r   )r·   rk   rJ   )r!   r�   r]  r   r   r   ÚgetAxisOrderö  s    
z DesignSpaceDocument.getAxisOrderc                 C   s"   | j D ]}|j|kr|  S qd S r   )r·   rJ   )r!   rJ   r]  r   r   r   ÚgetAxisü  s    


zDesignSpaceDocument.getAxisc                 C   s6   d| _ |  ¡ }| jD ]}|j|kr|| _ |  S qdS )z­Set and return SourceDescriptor at the default location or None.

        The default location is the set of all `default` values in user space
        of all axes.
        N)r”   rÓ   r»   rL   )r!   Zdefault_location_designrW  r   r   r   rJ    s    


zDesignSpaceDocument.findDefaultc                    sv   ddl m} i }| jD ]Z‰ ˆ j|vr&q|ˆ j }t|tƒrB|d }‡ fdd„ˆ jˆ jˆ jfD ƒ}|||ƒ|ˆ j< q|S )Nr   )ÚnormalizeValuec                    s   g | ]}ˆ   |¡‘qS r   )r¡   r7   ©r“   r   r   r:   !  s   z9DesignSpaceDocument.normalizeLocation.<locals>.<listcomp>)	rŸ   rk  r·   rJ   r=   rÕ   rf   r”   rg   )r!   rL   rk  Únewr&   Ztripler   rl  r   ÚnormalizeLocation  s    




ÿz%DesignSpaceDocument.normalizeLocationc              
   C   sò  | j D ]}|  |j¡|_q| jD ]X}|j ¡ D ]:\}}|  |d ¡|d< |d D ]}|  |d ¡|d< qPq.|  |j¡|_q | jD ]¨}g }|jD ].\}}|  |j|i¡ 	|j¡}	| 
||	f¡ qŽ|rÈ||_|  |j|ji¡ 	|j¡}
|  |j|ji¡ 	|j¡}|  |j|ji¡ 	|j¡}|
|_||_||_q€| jD ]¼}g }|jD ]¤}g }|D ]Š}| 	d¡d u�r€|  |d |d i¡ 	|d ¡}
nd }
| 	d¡d u�r¶|  |d |d i¡ 	|d ¡}nd }| 
t|d |
|d�¡ �qJ| 
|¡ �q>||_�q0d S )Nr  r  rL   rf   rJ   rg   )rJ   rf   rg   )r»   rn  rL   r¼   r}   r<   r·   r•   rJ   rh   rk   rf   rg   r”   rl   r\   r�   )r!   Úitemr4   rE  ZglyphMasterr“   ZnewMaprì   rí   ZnewOutputValuerf   rg   r”   rd   ZnewConditionSetsri   ZnewConditionsrá   r   r   r   Ú	normalize'  sF    




""zDesignSpaceDocument.normalizec                 K   s�   i }g }| j D ]|}|jdur*| |j¡ q|j|v rB||j |_n<|jdu r^td|jpXd ƒ‚||jfi |¤Ž|_|j||j< | |j¡ q|S )a¢  Ensure SourceDescriptor.font attributes are loaded, and return list of fonts.

        Takes a callable which initializes a new font object (e.g. TTFont, or
        defcon.Font, etc.) from the SourceDescriptor.path, and sets the
        SourceDescriptor.font attribute.
        If the font attribute is already not None, it is not loaded again.
        Fonts with the same path are only loaded once and shared among SourceDescriptors.

        For example, to load UFO sources using defcon:

            designspace = DesignSpaceDocument.fromfile("path/to/my.designspace")
            designspace.loadSourceFonts(defcon.Font)

        Or to load masters as FontTools binary fonts, including extra options:

            designspace.loadSourceFonts(ttLib.TTFont, recalcBBoxes=False)

        Args:
            opener (Callable): takes one required positional argument, the source.path,
                and an optional list of keyword arguments, and returns a new font object
                loaded from the path.
            **kwargs: extra options passed on to the opener function.

        Returns:
            List of font objects in the order they appear in the sources list.
        Nz/Designspace source '%s' has no 'path' attributez	<Unknown>)r»   rV   rk   r   r	   rJ   )r!   ÚopenerrY  ZloadedZfontsrH   r   r   r   ÚloadSourceFontsY  s$    



ÿÿz#DesignSpaceDocument.loadSourceFonts)NN)NN)NN)N)TTF) r0   r1   r2   rG   r,   r  rI  r$  rN  r*  rÇ   rU  rR  rX  rZ  r[  r\  r^  r_  r`  ra  rÓ   re  rf  rg  rh  ri  rj  rJ  rn  rp  rr  r   r   r   r   r
   '  s<   
 

	0
2r
   )$ZfontTools.misc.loggingToolsr   ZfontTools.misc.textToolsr   r   rb  Úior   r   r   r   ZfontTools.miscr   r¯   r   Ú__all__ZXML_NSré   r   r)   Ú	Exceptionr	   Úobjectr3   rA   r   r   re   r^   ro   r   r’   r   r   r   r
   r   r   r   r   Ú<module>   s<   
AZ;  9  `