o
    e3                     @   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def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v rdd | jd jD ni | _|d ur|ni | _i | _|d ur+|ni | _d| _g | _g | _	|| _
|d j| _t|ddd | _d | _|rrddlm} t|d	d
d | _| jd urt|| jj|d j|| _d S d S d S )Nfvarc                 S   s   i | ]}|j d qS )r   )axisTag).0axis r   I/var/www/myenv/lib/python3.10/site-packages/fontTools/ttLib/ttGlyphSet.py
<dictcomp>       z(_TTGlyphSet.__init__.<locals>.<dictcomp>r   hmtxvmtxmetricsVarStoreInstancerHVARtable)r   fontaxesdefaultLocationNormalizedlocationrawLocationoriginalLocationdepthlocationStackrawLocationStackglyphsMappingr   hMetricsgetattrgetvMetrics	hvarTablefontTools.varLib.varStorer    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 | j 	 | _| j	 | _d S | j 	 | _| j	 | _w N)
r*   appendr&   r+   r'   r(   r   r%   updatepop)r5   r&   r7   r   r   r   pushLocation5   s    z_TTGlyphSet.pushLocationc              	   c   sB    z| j }|  j d7  _ |V  W |  j d8  _ d S |  j d8  _ w )N   )r)   )r5   r)   r   r   r   	pushDepthH   s   "z_TTGlyphSet.pushDepthc                 C   
   || j v S r8   r,   r5   	glyphNamer   r   r   __contains__Q      
z_TTGlyphSet.__contains__c                 C   s   t | j S r8   )iterr,   keysr5   r   r   r   __iter__T   s   z_TTGlyphSet.__iter__c                 C   s
   t | jS r8   )lenr,   rG   r   r   r   __len__W   rD   z_TTGlyphSet.__len__z#use 'glyphName in glyphSet' instead)categoryc                 C   r?   r8   r@   rA   r   r   r   has_keyZ   s   
z_TTGlyphSet.has_keyN)__name__
__module____qualname____doc__r6   r   boolr<   r>   rC   rH   rJ   r
   DeprecationWarningrL   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 )Nglyfr   gvar)	glyfTablesuperr6   r/   	gvarTable)r5   r#   r&   r   	__class__r   r   r6   b   s   
z_TTGlyphSetGlyf.__init__c                 C   s   t | || jdS )Nr   )_TTGlyphGlyfr   rA   r   r   r   __getitem__g   s   z_TTGlyphSetGlyf.__getitem__)TrM   rN   rO   r6   r\   __classcell__r   r   rY   r   rS   a   s    rS   c                       s$   e Zd Z fddZdd Z  ZS )_TTGlyphSetCFFc                    s   d|v rdnd}t || j d j| _t ||| j d | _|rDddlm	} t
| jdd }|d urF||j|d j|}|j| _d S d S d S )NCFF2zCFF r   r   varStorer   )listcffvaluesCharStringscharStringsrW   r6   blenderr2   r    r.   
otVarStorer$   interpolateFromDeltas)r5   r#   r&   tableTagr    ra   	instancerrY   r   r   r6   l   s   z_TTGlyphSetCFF.__init__c                 C   s
   t | |S r8   )_TTGlyphCFFrA   r   r   r   r\   {   rD   z_TTGlyphSetCFF.__getitem__r]   r   r   rY   r   r_   k   s    r_   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 ur!|j| \| _| _nd\| _| _|j	rN|j
d urP|j
jd u r;|j|n|j
jj| }|  j|j| 7  _d S d S d S )NNN)glyphSetnamer   r-   widthlsbr0   heighttsbr&   r1   AdvWidthMapr#   
getGlyphIDmappingr4   )r5   ro   rB   r   varidxr   r   r   r6      s   
z_TTGlyph.__init__c                 C   s   t )dDraw the glyph onto ``pen``. See fontTools.pens.basePen for details
        how that works.
        )NotImplementedErrorr5   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)fontTools.pens.pointPenr   r}   )r5   r|   r   r   r   r   
drawPoints   s   z_TTGlyph.drawPointsN)rM   rN   rO   rP   r6   r   r}   r   r   r   r   r   rm      s    	
rm   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )r[   c                 C   ~   |   \}}| j *}|rd}| r$| ||d 	 W d   dS ||| jj| W d   dS 1 s8w   Y  dS )ry   r   FN)_getGlyphAndOffsetro   r>   isVarComposite_drawVarCompositer}   rV   r5   r|   glyphoffsetr)   r   r   r   r}         "z_TTGlyphGlyf.drawc                 C   r   )r~   r   TN)r   ro   r>   r   r   r   rV   r   r   r   r   r      r   z_TTGlyphGlyf.drawPointsc           	   
   C   s   ddl m}m} |jD ]Y}| j|j|j|j@ C z|	|j
|j| jj W n- tyT   |j }|rDt||}| j|j
 | nt||}| j|j
 | Y nw W d    n1 s_w   Y  qd S )Nr   )VarComponentFlagsVAR_COMPONENT_TRANSFORM_MAPPING)fontTools.ttLib.tables._g_l_y_fr   r   
componentsro   r<   r&   flagsRESET_UNSPECIFIED_AXESaddVarComponentrB   	transformr'   AttributeErrortoTransformr   r   r   r}   )	r5   r   r|   
isPointPenr   r   compttPenr   r   r   r      s,   



z_TTGlyphGlyf._drawVarCompositec                 C   sN   | j jr| j jd ur|  }n| j j| j }t|dr!| j|j nd}||fS )NxMinr   )	ro   r&   rX   _getGlyphInstancerV   rp   hasattrrr   r   )r5   r   r   r   r   r   r      s
   
z_TTGlyphGlyf._getGlyphAndOffsetc                 C   s@  ddl m} ddlm} ddlm} | j}|j}|jj	| j
 }|j}|j}|| j
||\}	}
d\}}|D ]C}||j|j}|sAq5|j}d |v rp|d u rj|| j
||\}}|d dkr`|d n	ttt|d }||||}|	||| 7 }	q5t|| j
 }t||	|| jd\}}}}|| _|| _|jd u r|| _|| _|S )Nr   )	iup_delta)GlyphCoordinates)supportScalarrn   r=   r   )fontTools.varLib.iupr   r   r   fontTools.varLib.modelsr   ro   rV   rX   
variationsrp   r-   r0   _getCoordinatesAndControlsr&   r$   coordinatesrb   rangerI   r   _setCoordinatesr   rr   rt   r1   rq   rs   )r5   r   r   r   ro   rV   r   r-   r0   r   _
origCoordsendPtsvarscalardeltacontrolr   rq   rr   rs   rt   r   r   r   r      sH   &

z_TTGlyphGlyf._getGlyphInstanceN)rM   rN   rO   r}   r   r   r   r   r   r   r   r   r[      s    	r[   c                   @   s   e Zd Zdd ZdS )rl   c                 C   s   | j j| j || j j dS )ry   N)ro   rf   rp   r}   rg   r{   r   r   r   r}     s   z_TTGlyphCFF.drawN)rM   rN   rO   r}   r   r   r   r   rl     s    rl   Tr   c                C   sr  t |dksJ |d d }|d d }|d d }|d d }tdD ]}|d= q$|  rYt |t | jks9J dd	 | jD | _t|| jD ]\}	}
t|
d
rW|	\|
_|
_qHn9|  rvdd	 | jD | _| jD ]}
|
	|}qi|ruJ n| j
dkrt |dksJ nt |t | jksJ || _|r| | t|| }t|| }t| j| }t|| j }||||fS )N   r   r=   c                 S      g | ]}t |qS r   r   r   r   r   r   r   
<listcomp>-  r   z#_setCoordinates.<locals>.<listcomp>xc                 S   r   r   r   r   r   r   r   r   2  r   )rI   r   isCompositer   zipr   r   yr   setCoordinatesnumberOfContoursr   r   r	   r   yMax)r   coordrV   r   	leftSideX
rightSideXtopSideYbottomSideYr   pr   horizontalAdvanceWidthverticalAdvanceWidthleftSideBearingtopSideBearingr   r   r   r      sF   




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 r8   )	glyphset1	glyphset2factor)r5   r   r   r   r   r   r   r6   T  s   
zLerpGlyphSet.__init__c                 C   s&   || j v r|| jv rt|| S t|r8   )r   r   	LerpGlyphKeyErrorr5   	glyphnamer   r   r   r\   Y  s   
zLerpGlyphSet.__getitem__c                 C   s   || j v o	|| jv S r8   )r   r   r   r   r   r   rC   ^  s   zLerpGlyphSet.__contains__c                 C   "   t | j}t | j}t||S r8   )setr   r   rE   intersectionr5   set1set2r   r   r   rH   a     

zLerpGlyphSet.__iter__c                 C   r   r8   )r   r   r   rI   r   r   r   r   r   rJ   f  r   zLerpGlyphSet.__len__N)r   )	rM   rN   rO   rP   r6   r\   rC   rH   rJ   r   r   r   r   r   K  s    
r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S r8   )glyphsetr   )r5   r   r   r   r   r   r6   m  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 r8   )
r   r   r   r   r}   r   r   r   r   value)r5   r|   
recording1
recording2r   r   r   r   r}   q  s   zLerpGlyph.drawN)rM   rN   rO   r6   r}   r   r   r   r   r   l  s    r   N)!rP   abcr   r   collections.abcr   
contextlibr   r   typesr   fontTools.misc.fixedToolsr	   fontTools.misc.loggingToolsr
   fontTools.misc.transformr   fontTools.pens.transformPenr   r   fontTools.pens.recordingPenr   r   r   r   rS   r_   rm   r[   rl   r   r   r   r   r   r   r   <module>   s(    N
-l+!