a
    
d+                     @   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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.
    c                 C   s   || _ d| j v r&dd | j d jD ni | _|d ur8|ni | _i | _|d urP|ni | _d| _g | _g | _|| _	|d j
| _t|ddd | _d | _|rddlm} t|d	d
d | _| jd ur|| jj|d j|| _d S )Nfvarc                 S   s   i | ]}|j d qS )r   )ZaxisTag).0Zaxis r   c/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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__   s,    
z_TTGlyphSet.__init__)resetc                 c   s   | j | j | j| j |r:| j | _| j | _n| j | _i | _| j| | j| z"d V  W | j 	 | _| j	 | _n| j 	 | _| j	 | _0 d S 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  _ n|  j d8  _ 0 d S )N   )r   )r*   r   r   r   r   	pushDepthB   s
    z_TTGlyphSet.pushDepthc                 C   s
   || j v S r-   r"   r*   	glyphNamer   r   r   __contains__K   s    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   s    z_TTGlyphSet.__len__z#use 'glyphName in glyphSet' instead)categoryc                 C   s
   || j v S r-   r4   r5   r   r   r   has_keyT   s    z_TTGlyphSet.has_keyN)__name__
__module____qualname____doc__r+   r   boolr1   r3   r7   r;   r=   r
   DeprecationWarningr?   r   r   r   r   r      s   
r   c                       s$   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   s
   t | |S r-   )_TTGlyphGlyfr5   r   r   r   __getitem__a   s    z_TTGlyphSetGlyf.__getitem__r@   rA   rB   r+   rM   __classcell__r   r   rJ   r   rF   [   s   rF   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 | _|rddlm	} t
| jdd }|d ur||j|d j|}|j| _d S )NZCFF2zCFF r   r   varStorer   )listZcffvaluesZCharStringscharStringsrH   r+   blenderr(   r   r$   Z
otVarStorer   ZinterpolateFromDeltas)r*   r   r   ZtableTagr   rQ   Z	instancerrJ   r   r   r+   f   s    z_TTGlyphSetCFF.__init__c                 C   s
   t | |S r-   )_TTGlyphCFFr5   r   r   r   rM   u   s    z_TTGlyphSetCFF.__getitem__rN   r   r   rJ   r   rP   e   s   rP   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r|j	d ur|j	j
d u rp|j|n|j	j
j| }|  j|j| 7  _d S )NNN)glyphSetnamer#   widthlsbr&   heighttsbr   r'   ZAdvWidthMapr   Z
getGlyphIDmappingr)   )r*   rY   r6   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.pointPenrg   re   )r*   rd   rg   r   r   r   
drawPoints   s    z_TTGlyph.drawPointsN)r@   rA   rB   rC   r+   r   re   rh   r   r   r   r   rW   y   s
   	
rW   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )rL   c                 C   sz   |   \}}| j P}|r d}| rF| ||d W d   dS ||| jj| W d   n1 sl0    Y  dS )ra   r   FN)_getGlyphAndOffsetrY   r3   isVarComposite_drawVarCompositere   rG   r*   rd   glyphoffsetr   r   r   r   re      s    z_TTGlyphGlyf.drawc                 C   sz   |   \}}| j P}|r d}| rF| ||d W d   dS ||| jj| W d   n1 sl0    Y  dS )rf   r   TN)ri   rY   r3   rj   rk   rh   rG   rl   r   r   r   rh      s    z_TTGlyphGlyf.drawPointsc           	   
   C   s   ddl m}m} |jD ]}| j|j|j|j@  z|	|j
|j| jj W nZ ty   |j }|rt||}| j|j
 | nt||}| j|j
 | Y n0 W d    q1 s0    Y  qd S )Nr   )VarComponentFlagsVAR_COMPONENT_TRANSFORM_MAPPING)fontTools.ttLib.tables._g_l_y_fro   rp   
componentsrY   r1   r   flagsZRESET_UNSPECIFIED_AXESZaddVarComponentr6   Z	transformr   AttributeErrorZtoTransformr   rh   r   re   )	r*   rm   rd   Z
isPointPenro   rp   comptZtPenr   r   r   rk      s     



z_TTGlyphGlyf._drawVarCompositec                 C   sN   | j jr| j jd ur|  }n| j j| j }t|drB| j|j nd}||fS )NxMinr   )	rY   r   rI   _getGlyphInstancerG   rZ   hasattrr\   rw   )r*   rm   rn   r   r   r   ri      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 ]}||j|j}|sqj|j}d |v r|d u r|| j
||\}}|d dkr|d nttt|d }||||}|	||| 7 }	qjt|| j
 }t||	|\}}}}|| _|| _|jd u r8|| _|| _|S )Nr   )	iup_delta)GlyphCoordinates)supportScalarrX   r2   )ZfontTools.varLib.iuprz   rq   r{   ZfontTools.varLib.modelsr|   rY   rG   rI   
variationsrZ   r#   r&   Z_getCoordinatesAndControlsr   r   coordinatesrR   ranger<   r   _setCoordinatesr\   r^   r'   r[   r]   )r*   rz   r{   r|   rY   rG   r}   r#   r&   r~   _Z
origCoordsZendPtsvarZscalardeltacontrolrm   r[   r\   r]   r^   r   r   r   rx      sD    &z_TTGlyphGlyf._getGlyphInstanceN)r@   rA   rB   re   rh   rk   ri   rx   r   r   r   r   rL      s
   	rL   c                   @   s   e Zd Zdd ZdS )rV   c                 C   s   | j j| j || j j dS r`   )rY   rT   rZ   re   rU   rc   r   r   r   re     s    z_TTGlyphCFF.drawN)r@   rA   rB   re   r   r   r   r   rV     s   rV   c                 C   st  t |dksJ |d d }|d d }|d d }|d d }tdD ]
}|d= qH|  rt |t | jksrJ dd	 | jD | _t|| jD ]\}}	t|	d
r|\|	_|	_qnx|  rdd	 | jD | _| jD ]}	|		|}q|rJ n>| j
dkrt |dks*J nt |t | jks$J || _| | t|| }
t|| }t| j| }t|| j }|
|||fS )N   r   r2   c                 S   s   g | ]}t |qS r   r   r   ru   r   r   r   
<listcomp>(  r   z#_setCoordinates.<locals>.<listcomp>xc                 S   s   g | ]}t |qS r   r   r   r   r   r   r   -  r   )r<   r   ZisCompositerr   zipry   r   yrj   ZsetCoordinatesZnumberOfContoursr~   ZrecalcBoundsr	   rw   ZyMax)rm   ZcoordrG   Z	leftSideXZ
rightSideXZtopSideYZbottomSideYr   pru   ZhorizontalAdvanceWidthZverticalAdvanceWidthZleftSideBearingZtopSideBearingr   r   r   r     s@    



r   N)rC   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   rF   rP   rW   rL   rV   r   r   r   r   r   <module>   s    M
,n