o
    te,                     @   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 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dS )zGlyphSets returned by a TTFont.    )ABCabstractmethod)Mapping)contextmanagercopy)SimpleNamespace)otRound)deprecateFunction)	Transform)TransformPenTransformPointPenc                   @   sb   e Zd ZdZ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.
    c                 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 | _|roddlm} t|d	d
d | _| jd urq|| jj|d j|| _d S d S d S )Nfvarc                 S   s   i | ]}|j d qS )r   )ZaxisTag).0Zaxis r   JD:\Projects\ConvertPro\env\Lib\site-packages\fontTools/ttLib/ttGlyphSet.py
<dictcomp>       z(_TTGlyphSet.__init__.<locals>.<dictcomp>r   ZhmtxZvmtxmetricsVarStoreInstancerZHVARtable)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   __init__   s0   


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)r*   r   r,   r   r   r   pushLocation/   s    z_TTGlyphSet.pushLocationc              	   c   sB    z| j }|  j d7  _ |V  W |  j d8  _ d S |  j d8  _ w )N   )r   )r*   r   r   r   r   	pushDepthB   s   "z_TTGlyphSet.pushDepthc                 C   
   || j v S r-   r"   r*   	glyphNamer   r   r   __contains__K      
z_TTGlyphSet.__contains__c                 C   s   t | j S r-   )iterr"   keysr*   r   r   r   __iter__N   s   z_TTGlyphSet.__iter__c                 C   s
   t | jS r-   )lenr"   r<   r   r   r   __len__Q   r9   z_TTGlyphSet.__len__z#use 'glyphName in glyphSet' instead)categoryc                 C   r4   r-   r5   r6   r   r   r   has_keyT   s   
z_TTGlyphSet.has_keyN)__name__
__module____qualname____doc__r+   r   boolr1   r3   r8   r=   r?   r
   DeprecationWarningrA   r   r   r   r   r      s    
r   c                       $   e Zd Z fddZdd Z  ZS )_TTGlyphSetGlyfc                    s,   |d | _ t ||| j  |d| _d S )NZglyfZgvar)	glyfTablesuperr+   r%   	gvarTable)r*   r   r   	__class__r   r   r+   \   s   
z_TTGlyphSetGlyf.__init__c                 C   
   t | |S r-   )_TTGlyphGlyfr6   r   r   r   __getitem__a   r9   z_TTGlyphSetGlyf.__getitem__rB   rC   rD   r+   rQ   __classcell__r   r   rM   r   rI   [   s    rI   c                       rH   )_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charStringsrK   r+   blenderr(   r   r$   Z
otVarStorer   ZinterpolateFromDeltas)r*   r   r   ZtableTagr   rU   Z	instancerrM   r   r   r+   f   s   z_TTGlyphSetCFF.__init__c                 C   rO   r-   )_TTGlyphCFFr6   r   r   r   rQ   u   r9   z_TTGlyphSetCFF.__getitem__rR   r   r   rM   r   rT   e   s    rT   c                   @   s,   e Zd ZdZ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.
    c                 C   s   || _ || _|j| \| _| _|jd ur|j| \| _| _nd\| _| _|jrK|j	d urM|j	j
d u r8|j|n|j	j
j| }|  j|j| 7  _d S d S d S )NNN)glyphSetnamer#   widthlsbr&   heighttsbr   r'   ZAdvWidthMapr   Z
getGlyphIDmappingr)   )r*   r]   r7   Zvaridxr   r   r   r+      s   
z_TTGlyph.__init__c                 C   s   t )dDraw the glyph onto ``pen``. See fontTools.pens.basePen for details
        how that works.
        )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.pointPenrj   rh   )r*   rg   rj   r   r   r   
drawPoints   s   z_TTGlyph.drawPointsN)rB   rC   rD   rE   r+   r   rh   rk   r   r   r   r   r[   y   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   ~   |   \}}| j *}|rd}| r$| ||d 	 W d   dS ||| jj| W d   dS 1 s8w   Y  dS )rd   r   FN)_getGlyphAndOffsetr]   r3   isVarComposite_drawVarCompositerh   rJ   r*   rg   glyphoffsetr   r   r   r   rh         	"z_TTGlyphGlyf.drawc                 C   rl   )ri   r   TN)rm   r]   r3   rn   ro   rk   rJ   rp   r   r   r   rk      rs   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_frt   ru   
componentsr]   r1   r   flagsZRESET_UNSPECIFIED_AXESZaddVarComponentr7   Z	transformr   AttributeErrorZtoTransformr   rk   r   rh   )	r*   rq   rg   Z
isPointPenrt   ru   comptZtPenr   r   r   ro      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   )	r]   r   rL   _getGlyphInstancerJ   r^   hasattrr`   r|   )r*   rq   rr   r   r   r   rm      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 u r|| _|| _|S )Nr   )	iup_delta)GlyphCoordinates)supportScalarr\   r2   )ZfontTools.varLib.iupr   rv   r   ZfontTools.varLib.modelsr   r]   rJ   rL   
variationsr^   r#   r&   Z_getCoordinatesAndControlsr   r   coordinatesrV   ranger>   r   _setCoordinatesr`   rb   r'   r_   ra   )r*   r   r   r   r]   rJ   r   r#   r&   r   _Z
origCoordsZendPtsvarZscalardeltacontrolrq   r_   r`   ra   rb   r   r   r   r}      sD   &
z_TTGlyphGlyf._getGlyphInstanceN)rB   rC   rD   rh   rk   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   N)r]   rX   r^   rh   rY   rf   r   r   r   rh     s   z_TTGlyphCFF.drawN)rB   rC   rD   rh   r   r   r   r   rZ     s    rZ   c                 C   sn  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 || _| | t|| }
t|| }t| j| }t|| j }|
|||fS )N   r   r2   c                 S      g | ]}t |qS r   r   r   rz   r   r   r   
<listcomp>(  r   z#_setCoordinates.<locals>.<listcomp>xc                 S   r   r   r   r   r   r   r   r   -  r   )r>   r   ZisCompositerw   zipr~   r   yrn   ZsetCoordinatesZnumberOfContoursr   ZrecalcBoundsr	   r|   ZyMax)rq   coordrJ   Z	leftSideXZ
rightSideXZtopSideYZbottomSideYr   prz   ZhorizontalAdvanceWidthZverticalAdvanceWidthZleftSideBearingZtopSideBearingr   r   r   r     sD   




r   N)rE   abcr   r   collections.abcr   
contextlibr   r   typesr   ZfontTools.misc.fixedToolsr	   ZfontTools.misc.loggingToolsr
   ZfontTools.misc.transformr   ZfontTools.pens.transformPenr   r   r   rI   rT   r[   rP   rZ   r   r   r   r   r   <module>   s"    M
,n