U
    Dvf3                     @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZm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G dd deZddddZG dd deZG dd dZ d S )!zGlyphSets returned by a TTFont.    )ABCabstractmethod)Mapping)contextmanagercopy)SimpleNamespace)otRound)deprecateFunction)	Transform)TransformPenTransformPointPen)DecomposingRecordingPenlerpRecordingsreplayRecordingc                   @   sh   e Zd ZdZddddZeedddZed	d
 Zdd Z	dd Z
dd Zededdd ZdS )_TTGlyphSetzlGeneric dict-like GlyphSet class that pulls metrics from hmtx and
    glyph shape from TrueType or CFF.
    TrecalcBoundsc                C   s   || _ || _d| jkr,dd | jd jD ni | _|d k	r>|ni | _i | _|d k	rV|ni | _d| _g | _g | _	|| _
|d j| _t|ddd | _d | _|rddlm} t|d	d
d | _| jd k	r|| jj|d j|| _d S )Nfvarc                 S   s   i | ]}|j d qS )r   )ZaxisTag).0Zaxis r   >/tmp/pip-unpacked-wheel-qlge9rch/fontTools/ttLib/ttGlyphSet.py
<dictcomp>   s      z(_TTGlyphSet.__init__.<locals>.<dictcomp>r   ZhmtxZvmtxmetricsVarStoreInstancerZHVARtable)r   fontaxesdefaultLocationNormalizedlocationrawLocationoriginalLocationdepthlocationStackrawLocationStackglyphsMappingr   hMetricsgetattrgetvMetrics	hvarTablefontTools.varLib.varStorer   ZVarStorehvarInstancer)selfr   r    r&   r   r   r   r   r   __init__   s2    
  z_TTGlyphSet.__init__)resetc                 c   s   | j | j | j| j |r:| j | _| j | _n| j | _i | _| j| | j| z
d V  W 5 | j 	 | _| j	 | _X d S N)
r$   appendr    r%   r!   r"   r   r   updatepop)r.   r    r0   r   r   r   pushLocation4   s    
z_TTGlyphSet.pushLocationc              	   c   s4   z| j }|  j d7  _ |V  W 5 |  j d8  _ X d S )N   )r#   )r.   r#   r   r   r   	pushDepthG   s
    
z_TTGlyphSet.pushDepthc                 C   s
   || j kS r1   r&   r.   	glyphNamer   r   r   __contains__P   s    z_TTGlyphSet.__contains__c                 C   s   t | j S r1   )iterr&   keysr.   r   r   r   __iter__S   s    z_TTGlyphSet.__iter__c                 C   s
   t | jS r1   )lenr&   r>   r   r   r   __len__V   s    z_TTGlyphSet.__len__z#use 'glyphName in glyphSet' instead)categoryc                 C   s
   || j kS r1   r8   r9   r   r   r   has_keyY   s    z_TTGlyphSet.has_keyN)__name__
__module____qualname____doc__r/   r   boolr5   r7   r;   r?   rA   r
   DeprecationWarningrC   r   r   r   r   r      s   
 r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )_TTGlyphSetGlyfTc                    s0   |d | _ t j||| j |d |d| _d S )NZglyfr   Zgvar)	glyfTablesuperr/   r)   	gvarTable)r.   r   r    r   	__class__r   r   r/   a   s    
z_TTGlyphSetGlyf.__init__c                 C   s   t | || jdS )Nr   )_TTGlyphGlyfr   r9   r   r   r   __getitem__f   s    z_TTGlyphSetGlyf.__getitem__)TrD   rE   rF   r/   rQ   __classcell__r   r   rN   r   rJ   `   s   rJ   c                       s$   e Zd Z fddZdd Z  ZS )_TTGlyphSetCFFc                    s   d|krdnd}t || j d j| _t ||| j d | _|rddlm	} t
| jdd }|d k	r||j|d j|}|j| _d S )NZCFF2zCFF r   r   varStorer   )listZcffvaluesZCharStringscharStringsrL   r/   blenderr,   r   r(   Z
otVarStorer   ZinterpolateFromDeltas)r.   r   r    ZtableTagr   rU   Z	instancerrN   r   r   r/   k   s      z_TTGlyphSetCFF.__init__c                 C   s
   t | |S r1   )_TTGlyphCFFr9   r   r   r   rQ   z   s    z_TTGlyphSetCFF.__getitem__rR   r   r   rN   r   rT   j   s   rT   c                   @   s2   e Zd ZdZddddZedd Zdd	 Zd
S )_TTGlyphaS  Glyph object that supports the Pen protocol, meaning that it has
    .draw() and .drawPoints() methods that take a pen object as their only
    argument. Additionally there are 'width' and 'lsb' attributes, read from
    the 'hmtx' table.

    If the font contains a 'vmtx' table, there will also be 'height' and 'tsb'
    attributes.
    Tr   c                C   s   || _ || _|| _|j| \| _| _|jd k	rB|j| \| _| _nd\| _| _|j	r|j
d k	r|j
jd krv|j|n|j
jj| }|  j|j| 7  _d S )NNN)glyphSetnamer   r'   widthlsbr*   heighttsbr    r+   ZAdvWidthMapr   Z
getGlyphIDmappingr-   )r.   r]   r:   r   Zvaridxr   r   r   r/      s    

z_TTGlyph.__init__c                 C   s   t dS dDraw the glyph onto ``pen``. See fontTools.pens.basePen for details
        how that works.
        N)NotImplementedErrorr.   penr   r   r   draw   s    z_TTGlyph.drawc                 C   s   ddl m} | || dS )eDraw the glyph onto ``pen``. See fontTools.pens.pointPen for details
        how that works.
        r   )SegmentToPointPenN)ZfontTools.pens.pointPenrk   ri   )r.   rh   rk   r   r   r   
drawPoints   s    z_TTGlyph.drawPointsN)rD   rE   rF   rG   r/   r   ri   rl   r   r   r   r   r[   ~   s
   	
r[   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )rP   c              	   C   sd   |   \}}| j D}|r d}| rD| ||d W 5 Q R  dS ||| jj| W 5 Q R X dS )re   r   FN)_getGlyphAndOffsetr]   r7   isVarComposite_drawVarCompositeri   rK   r.   rh   glyphoffsetr#   r   r   r   ri      s    z_TTGlyphGlyf.drawc              	   C   sd   |   \}}| j D}|r d}| rD| ||d W 5 Q R  dS ||| jj| W 5 Q R X dS )rj   r   TN)rm   r]   r7   rn   ro   rl   rK   rp   r   r   r   rl      s    z_TTGlyphGlyf.drawPointsc           	   
   C   s   ddl m}m} |jD ]}| j|j|j|j@ ~ z|	|j
|j| jj W n\ tk
r   |j }|rt||}| j|j
 | nt||}| j|j
 | Y nX W 5 Q R X qd S )Nr   )VarComponentFlagsVAR_COMPONENT_TRANSFORM_MAPPING)fontTools.ttLib.tables._g_l_y_frs   rt   
componentsr]   r5   r    flagsZRESET_UNSPECIFIED_AXESZaddVarComponentr:   Z	transformr!   AttributeErrorZtoTransformr   rl   r   ri   )	r.   rq   rh   Z
isPointPenrs   rt   comptZtPenr   r   r   ro      s&    
 
  


z_TTGlyphGlyf._drawVarCompositec                 C   sN   | j jr| j jd k	r|  }n| j j| j }t|drB| j|j nd}||fS )NxMinr   )	r]   r    rM   _getGlyphInstancerK   r^   hasattrr`   r{   )r.   rq   rr   r   r   r   rm      s
    
z_TTGlyphGlyf._getGlyphAndOffsetc                 C   sB  ddl m} ddlm} ddlm} | j}|j}|jj	| j
 }|j}|j}|| j
||\}	}
d\}}|D ]}||j|j}|sqj|j}d |kr|d kr|| j
||\}}|d dkr|d nttt|d }||||}|	||| 7 }	qjt|| j
 }t||	|| jd\}}}}|| _|| _|jd kr>|| _|| _|S )Nr   )	iup_delta)GlyphCoordinates)supportScalarr\   r6   r   )ZfontTools.varLib.iupr~   ru   r   ZfontTools.varLib.modelsr   r]   rK   rM   
variationsr^   r'   r*   Z_getCoordinatesAndControlsr    r   coordinatesrV   ranger@   r   _setCoordinatesr   r`   rb   r+   r_   ra   )r.   r~   r   r   r]   rK   r   r'   r*   r   _Z
origCoordsZendPtsvarZscalardeltacontrolrq   r_   r`   ra   rb   r   r   r   r|      sV        &   z_TTGlyphGlyf._getGlyphInstanceN)rD   rE   rF   ri   rl   ro   rm   r|   r   r   r   r   rP      s
   	rP   c                   @   s   e Zd Zdd ZdS )rZ   c                 C   s   | j j| j || j j dS rd   )r]   rX   r^   ri   rY   rg   r   r   r   ri     s    z_TTGlyphCFF.drawN)rD   rE   rF   ri   r   r   r   r   rZ     s   rZ   Tr   c                C   sz  t |dkst|d d }|d d }|d d }|d d }tdD ]
}|d= qH|  rt |t | jksrtdd	 | jD | _t|| jD ]\}	}
t|
d
r|	\|
_|
_qnx| 	 rdd	 | jD | _| jD ]}
|

|}q|rtn>| jdkrt |dks*tnt |t | jks$t|| _|r:| | t|| }t|| }t| j| }t|| j }||||fS )N   r   r6   c                 S   s   g | ]}t |qS r   r   r   ry   r   r   r   
<listcomp>+  s     z#_setCoordinates.<locals>.<listcomp>xc                 S   s   g | ]}t |qS r   r   r   r   r   r   r   0  s     )r@   AssertionErrorr   ZisCompositerv   zipr}   r   yrn   ZsetCoordinatesZnumberOfContoursr   r   r	   r{   ZyMax)rq   ZcoordrK   r   Z	leftSideXZ
rightSideXZtopSideYZbottomSideYr   pry   ZhorizontalAdvanceWidthZverticalAdvanceWidthZleftSideBearingZtopSideBearingr   r   r   r     sB    



r   c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )LerpGlyphSeta+  A glyphset that interpolates between two other glyphsets.

    Factor is typically between 0 and 1. 0 means the first glyphset,
    1 means the second glyphset, and 0.5 means the average of the
    two glyphsets. Other values are possible, and can be useful to
    extrapolate. Defaults to 0.5.
          ?c                 C   s   || _ || _|| _d S r1   )	glyphset1	glyphset2factor)r.   r   r   r   r   r   r   r/   R  s    zLerpGlyphSet.__init__c                 C   s*   || j kr|| jkrt|| S t|d S r1   )r   r   	LerpGlyphKeyErrorr.   	glyphnamer   r   r   rQ   W  s    
zLerpGlyphSet.__getitem__c                 C   s   || j ko|| jkS r1   )r   r   r   r   r   r   r;   \  s    zLerpGlyphSet.__contains__c                 C   s"   t | j}t | j}t||S r1   )setr   r   r<   intersectionr.   Zset1Zset2r   r   r   r?   _  s    

zLerpGlyphSet.__iter__c                 C   s"   t | j}t | j}t||S r1   )r   r   r   r@   r   r   r   r   r   rA   d  s    

zLerpGlyphSet.__len__N)r   )	rD   rE   rF   rG   r/   rQ   r;   r?   rA   r   r   r   r   r   I  s   
r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S r1   )glyphsetr   )r.   r   r   r   r   r   r/   k  s    zLerpGlyph.__init__c                 C   sb   t | jj}| jj| j | t | jj}| jj| j | | jj}tt|j	|j	|| d S r1   )
r   r   r   r   ri   r   r   r   r   value)r.   rh   Z
recording1Z
recording2r   r   r   r   ri   o  s    zLerpGlyph.drawN)rD   rE   rF   r/   ri   r   r   r   r   r   j  s   r   N)!rG   abcr   r   collections.abcr   
contextlibr   r   typesr   ZfontTools.misc.fixedToolsr	   ZfontTools.misc.loggingToolsr
   ZfontTools.misc.transformr   ZfontTools.pens.transformPenr   r   ZfontTools.pens.recordingPenr   r   r   r   rJ   rT   r[   rP   rZ   r   r   r   r   r   r   r   <module>   s&   M
,l+!