o
    l^UfC                     @   sl  d Z ddlmZmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ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"G dd de"Z#G dd de"Z$dd 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deepcopy)SimpleNamespace)Vector)otRoundfixedToFloat)deprecateFunction)	TransformDecomposedTransform)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).0Zaxis r   :lib/python3.10/site-packages/fontTools/ttLib/ttGlyphSet.py
<dictcomp>       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 | j 	 | _| j	 | _d S | j 	 | _| j	 | _w N)
r*   appendr&   r+   r'   r(   r   r%   updatepop)r4   r&   r6   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)   )r4   r)   r   r   r   	pushDepthH   s   "z_TTGlyphSet.pushDepthc                 C   
   || j v S r7   r,   r4   	glyphNamer   r   r   __contains__Q      
z_TTGlyphSet.__contains__c                 C   s   t | j S r7   )iterr,   keysr4   r   r   r   __iter__T   s   z_TTGlyphSet.__iter__c                 C   s
   t | jS r7   )lenr,   rF   r   r   r   __len__W   rC   z_TTGlyphSet.__len__z#use 'glyphName in glyphSet' instead)categoryc                 C   r>   r7   r?   r@   r   r   r   has_keyZ   s   
z_TTGlyphSet.has_keyN)__name__
__module____qualname____doc__r5   r   boolr;   r=   rB   rG   rI   r   DeprecationWarningrK   r   r   r   r   r      s    
r   c                       &   e Zd Zd fdd	Zdd Z  ZS )_TTGlyphSetGlyfTc                    0   |d | _ t j||| j |d |d| _d S NZglyfr   Zgvar	glyfTablesuperr5   r/   	gvarTabler4   r#   r&   r   	__class__r   r   r5   b      
_TTGlyphSetGlyf.__init__c                 C      t | || jdS Nr   _TTGlyphGlyfr   r@   r   r   r   __getitem__g      _TTGlyphSetGlyf.__getitem__TrL   rM   rN   r5   rc   __classcell__r   r   r[   r   rS   a       rS   c                       rR   )rS   Tc                    rT   rU   rV   rZ   r[   r   r   r5   l   r]   r^   c                 C   r_   r`   ra   r@   r   r   r   rc   q   rd   re   rf   rg   r   r   r[   r   rS   k   ri   c                       $   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 )NZCFF2zCFF r   r    varStorer   )listZcffvaluesZCharStringscharStringsrX   r5   blenderr2   r!   r.   Z
otVarStorer$   ZinterpolateFromDeltas)r4   r#   r&   ZtableTagr!   rl   	instancerr[   r   r   r5   v   s   z_TTGlyphSetCFF.__init__c                 C   s
   t | |S r7   )_TTGlyphCFFr@   r   r   r   rc      rC   z_TTGlyphSetCFF.__getitem__rg   r   r   r[   r   rk   u   s    rk   c                       rj   )_TTGlyphSetVARCc                    s&   || _ t ||| |d j| _d S )NZVARC)glyphSetrX   r5   r"   	varcTable)r4   r#   r&   rt   r[   r   r   r5      s   z_TTGlyphSetVARC.__init__c                 C   s&   | j }||jjvr| j| S t| |S r7   )ru   Coverageglyphsrt   _TTGlyphVARC)r4   rA   varcr   r   r   rc      s   

z_TTGlyphSetVARC.__getitem__rg   r   r   r[   r   rs      s    rs   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)rt   namer   r-   widthlsbr0   heighttsbr&   r1   ZAdvWidthMapr#   Z
getGlyphIDmappingr3   )r4   rt   rA   r   Zvaridxr   r   r   r5      s   
z_TTGlyph.__init__c                 C   s   t )dDraw the glyph onto ``pen``. See fontTools.pens.basePen for details
        how that works.
        )NotImplementedErrorr4   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.pointPenr   r   )r4   r   r   r   r   r   
drawPoints   s   z_TTGlyph.drawPointsN)rL   rM   rN   rO   r5   r   r   r   r   r   r   r   rz      s    	
rz   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
rb   c                 C   V   |   \}}| j }|rd}||| jj| W d   dS 1 s$w   Y  dS )r   r   N)_getGlyphAndOffsetrt   r=   r   rW   r4   r   glyphoffsetr)   r   r   r   r         "z_TTGlyphGlyf.drawc                 C   r   )r   r   N)r   rt   r=   r   rW   r   r   r   r   r      r   z_TTGlyphGlyf.drawPointsc                 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   )	rt   r&   rY   _getGlyphInstancerW   r|   hasattrr~   r   )r4   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)supportScalarr{   r<   r   )ZfontTools.varLib.iupr   ZfontTools.ttLib.tables._g_l_y_fr   ZfontTools.varLib.modelsr   rt   rW   rY   
variationsr|   r-   r0   Z_getCoordinatesAndControlsr&   r$   coordinatesrm   rangerH   r   _setCoordinatesr   r~   r   r1   r}   r   )r4   r   r   r   rt   rW   r   r-   r0   r   _Z
origCoordsZendPtsvarZscalarZdeltaZcontrolr   r}   r~   r   r   r   r   r   r      sH   &

z_TTGlyphGlyf._getGlyphInstanceN)rL   rM   rN   r   r   r   r   r   r   r   r   rb      s
    	rb   c                   @   s   e Zd Zdd ZdS )rr   c                 C   s   | j j| j || j j dS )r   N)rt   ro   r|   r   rp   r   r   r   r   r     s   z_TTGlyphCFF.drawN)rL   rM   rN   r   r   r   r   r   rr     s    rr   c                 C   s   | j dkr%| j}|| j}||d}| j}| j}||  ko"|kS   S | j dkr:| j}	|	|| j d 7 }	|	dkS | j dkrQ| jD ]}
t	|
|||sN dS qBdS | j dkrh| jD ]}
t	|
|||re dS qYdS | j dkrvt	| j
||| S dS )	Nr<   r         FT      )ZFormatZ	AxisIndexr   r/   ZFilterRangeMinValueZFilterRangeMaxValueZDefaultValueZVarIdxConditionTable_evaluateConditionZconditionTable)	conditionfvarAxesr&   rq   Z	axisIndexr   Z	axisValueZminValueZmaxValuevalueZsubconditionr   r   r   r     s8   








r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )rx   c                    sf  ddl m}m} | j}|j}|jj| j}|j	j
| }ddlm}	 ddlm}
 |jd j |	|j | jj}|jD ]}|j|j@ rS|jj|j }t| | jj|sSq:i }|jdur|jj|j }t|j}|j|kru|t ||j d7 }t!|t!|ksJ t!|t!|f fdd	t"||D }|j#|kr||j# }t$|}|%| |j&}|j|j'@ }| jj(||p | j(||X | j|j)k}|sz|*|j)|| jj+ W n t,y   d
}Y nw |r|- }|j)| jkr| jn|j}||j) }|rt.||}|/| n
t0||}|1| W d   n	1 sw   Y  W d   n	1 s+w   Y  q:dS )r   r   )VarComponentFlagsNO_VARIATION_INDEX)MultiVarStoreInstancerr    r   N   c                    s   i | ]
\}} | j |qS r   r   )r   ivr   r   r   r   d  s    z&_TTGlyphVARC._draw.<locals>.<dictcomp>T)2ZfontTools.ttLib.tables.otTablesr   r   rt   ru   rv   rw   indexr|   ZVarCompositeGlyphsZVarCompositeGlyphZfontTools.varLib.multiVarStorer   r2   r!   r#   r$   ZMultiVarStorer&   
componentsflagsZHAVE_CONDITIONZConditionListr   ZconditionIndexr   ZaxisIndicesIndexZAxisIndicesListZItemr	   
axisValuesZaxisValuesVarIndexfi2flrH   zipZtransformVarIndexr   ZapplyTransformDeltas	transformZRESET_UNSPECIFIED_AXESr;   rA   ZaddVarComponentr'   AttributeErrorZtoTransformr   r   r   r   )r4   r   Z
isPointPenr   r   rt   ry   idxr   r   r!   rq   compr   r&   ZaxisIndicesr   Zdeltasr   r6   ZshouldDecomposetZcompGlyphSetgZtPenr   r   r   _draw;  s   











z_TTGlyphVARC._drawc                 C      |  |d d S )NFr   r   r   r   r   r     rd   z_TTGlyphVARC.drawc                 C   r   )NTr   r   r   r   r   r     rd   z_TTGlyphVARC.drawPointsN)rL   rM   rN   r   r   r   r   r   r   r   rx   :  s    Orx   Tr   c                C   s8  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|	\|
_|
_qHn| jdkrgt |dksfJ nt |t | j	ksrJ || _	|r|| 
| t|| }t|| }t| j| }t|| j }||||fS )Nr   r   r<   c                 S   s   g | ]}t |qS r   )r   )r   r   r   r   r   
<listcomp>  r   z#_setCoordinates.<locals>.<listcomp>x)rH   r   ZisCompositer   r   r   r   yZnumberOfContoursr   r   r
   r   ZyMax)r   ZcoordrW   r   Z	leftSideXZ
rightSideXZtopSideYZbottomSideYr   pr   ZhorizontalAdvanceWidthZverticalAdvanceWidthZleftSideBearingZtopSideBearingr   r   r   r     s<   


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 r7   )	glyphset1	glyphset2factor)r4   r   r   r   r   r   r   r5     s   
zLerpGlyphSet.__init__c                 C   s&   || j v r|| jv rt|| S t|r7   )r   r   	LerpGlyphKeyErrorr4   	glyphnamer   r   r   rc     s   
zLerpGlyphSet.__getitem__c                 C   s   || j v o	|| jv S r7   )r   r   r   r   r   r   rB     s   zLerpGlyphSet.__contains__c                 C   "   t | j}t | j}t||S r7   )setr   r   rD   intersectionr4   Zset1Zset2r   r   r   rG        

zLerpGlyphSet.__iter__c                 C   r   r7   )r   r   r   rH   r   r   r   r   r   rI     r   zLerpGlyphSet.__len__N)r   )	rL   rM   rN   rO   r5   rc   rB   rG   rI   r   r   r   r   r     s    
r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s   || _ || _d S r7   )glyphsetr   )r4   r   r   r   r   r   r5     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 r7   )
r   r   r   r   r   r   r   r   r   r   )r4   r   Z
recording1Z
recording2r   r   r   r   r     s   zLerpGlyph.drawN)rL   rM   rN   r5   r   r   r   r   r   r     s    r   N)*rO   abcr   r   Zcollections.abcr   
contextlibr   r   r   typesr   ZfontTools.misc.vectorr	   ZfontTools.misc.fixedToolsr
   r   r   ZfontTools.misc.loggingToolsr   ZfontTools.misc.transformr   r   ZfontTools.pens.transformPenr   r   ZfontTools.pens.recordingPenr   r   r   r   rS   rk   rs   rz   rb   rr   r   rx   r   r   r   r   r   r   r   <module>   s2    M

,M#W&!