o
    hDD                     @   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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$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).0axis r   n/var/www/html/construction_image-detection-poc/venv/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*   r0   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)r9   r*   r;   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-   )r9   r-   r   r   r   	pushDepthH   s   "z_TTGlyphSet.pushDepthc                 C   
   || j v S r<   r0   r9   	glyphNamer   r   r   __contains__Q      
z_TTGlyphSet.__contains__c                 C   s   t | j S r<   )iterr0   keysr9   r   r   r   __iter__T   s   z_TTGlyphSet.__iter__c                 C   s
   t | jS r<   )lenr0   rK   r   r   r   __len__W   rH   z_TTGlyphSet.__len__z#use 'glyphName in glyphSet' instead)categoryc                 C   rC   r<   rD   rE   r   r   r   has_keyZ   s   
z_TTGlyphSet.has_keyN)__name__
__module____qualname____doc__r:   r   boolr@   rB   rG   rL   rN   r   DeprecationWarningrP   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superr:   r3   	gvarTable)r9   r'   r*   r   	__class__r   r   r:   b   s   
z_TTGlyphSetGlyf.__init__c                 C   s   t | || jdS )Nr   )_TTGlyphGlyfr   rE   r   r   r   __getitem__g      z_TTGlyphSetGlyf.__getitem__)TrQ   rR   rS   r:   r`   __classcell__r   r   r]   r   rW   a   s    rW   c                       s>   e Zd Z fddZdd Zdd Zedefdd	Z  Z	S )
_TTGlyphSetCFFc                    sJ   d|v rdnd}t || j d j| _t ||| j | | d S )NCFF2zCFF r   )listcffvaluesCharStringscharStringsr[   r:   setLocation)r9   r'   r*   tableTagr]   r   r   r:   l   s   z_TTGlyphSetCFF.__init__c                 C   s
   t | |S r<   )_TTGlyphCFFrE   r   r   r   r`   r   rH   z_TTGlyphSetCFF.__getitem__c                 C   s\   d | _ |r)ddlm} t| jdd }|d ur'||j| jd j|}|j| _ d S d S d | _ d S )Nr   r#   varStorer   )	blenderr6   r$   r2   rj   
otVarStorer'   r(   interpolateFromDeltas)r9   r*   r$   rn   	instancerr   r   r   rk   u   s   
z_TTGlyphSetCFF.setLocationr;   c              
   c   sf    |  | t| ||}z|V  W |  | j n|  | j w W d    d S 1 s,w   Y  d S r<   )rk   r   r@   r*   )r9   r*   r;   valuer   r   r   r@      s   
"z_TTGlyphSetCFF.pushLocation)
rQ   rR   rS   r:   r`   rk   r   rU   r@   rc   r   r   r]   r   rd   k   s    rd   c                       s$   e Zd Z fddZdd Z  ZS )_TTGlyphSetVARCc                    s&   || _ t ||| |d j| _d S )NVARC)glyphSetr[   r:   r&   	varcTable)r9   r'   r*   rv   r]   r   r   r:      s   z_TTGlyphSetVARC.__init__c                 C   s&   | j }||jjvr| j| S t| |S r<   )rw   Coverageglyphsrv   _TTGlyphVARC)r9   rF   varcr   r   r   r`      s   

z_TTGlyphSetVARC.__getitem__rb   r   r   r]   r   rt      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 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)rv   namer   r1   widthlsbr4   heighttsbr*   r5   AdvWidthMapr'   
getGlyphIDmappingr8   )r9   rv   rF   r   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r9   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   )r9   r   r   r   r   r   
drawPoints   s   z_TTGlyph.drawPointsN)rQ   rR   rS   rT   r:   r   r   r   r   r   r   r   r|      s    	
r|   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
r_   c                 C   V   |   \}}| j }|rd}||| jj| W d   dS 1 s$w   Y  dS )r   r   N)_getGlyphAndOffsetrv   rB   r   rZ   r9   r   glyphoffsetr-   r   r   r   r         "z_TTGlyphGlyf.drawc                 C   r   )r   r   N)r   rv   rB   r   rZ   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   )	rv   r*   r\   _getGlyphInstancerZ   r~   hasattrr   r   )r9   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}   rA   r   )fontTools.varLib.iupr   fontTools.ttLib.tables._g_l_y_fr   fontTools.varLib.modelsr   rv   rZ   r\   
variationsr~   r1   r4   _getCoordinatesAndControlsr*   r(   coordinatesrf   rangerM   r   _setCoordinatesr   r   r   r5   r   r   )r9   r   r   r   rv   rZ   r   r1   r4   r   _
origCoordsendPtsvarscalardeltacontrolr   r   r   r   r   r   r   r   r      sH   &

z_TTGlyphGlyf._getGlyphInstanceN)rQ   rR   rS   r   r   r   r   r   r   r   r   r_      s
    	r_   c                   @   s   e Zd Zdd ZdS )rm   c                 C   s   | j j| j || j j dS )r   N)rv   rj   r~   r   ro   r   r   r   r   r     s   z_TTGlyphCFF.drawN)rQ   rR   rS   r   r   r   r   r   rm     s    rm   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 )	NrA   r         FT      )Format	AxisIndexr   r3   FilterRangeMinValueFilterRangeMaxValueDefaultValueVarIdxConditionTable_evaluateConditionconditionTable)	conditionfvarAxesr*   rr   	axisIndexr   	axisValueminValuemaxValuers   subconditionr   r   r   r     s8   








r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )rz   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   i  s    z&_TTGlyphVARC._draw.<locals>.<dictcomp>T)2fontTools.ttLib.tables.otTablesr   r   rv   rw   rx   ry   indexr~   VarCompositeGlyphsVarCompositeGlyphfontTools.varLib.multiVarStorer   r6   r$   r'   r(   MultiVarStorer*   
componentsflagsHAVE_CONDITIONConditionListr   conditionIndexr   axisIndicesIndexAxisIndicesListItemr	   
axisValuesaxisValuesVarIndexfi2flrM   ziptransformVarIndexr   applyTransformDeltas	transformRESET_UNSPECIFIED_AXESr@   rF   addVarComponentr+   AttributeErrortoTransformr   r   r   r   )r9   r   
isPointPenr   r   rv   r{   idxr   r   r$   rr   compr   r*   axisIndicesr   deltasr   r;   shouldDecomposetcompGlyphSetgtPenr   r   r   _drawA  s   











z_TTGlyphVARC._drawc                 C      |  |d d S )NFr   r   r   r   r   r     ra   z_TTGlyphVARC.drawc                 C   r   )NTr   r   r   r   r   r     ra   z_TTGlyphVARC.drawPointsN)rQ   rR   rS   r   r   r   r   r   r   r   rz   @  s    Nrz   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   rA   c                 S   s   g | ]}t |qS r   )r   )r   r   r   r   r   
<listcomp>  r   z#_setCoordinates.<locals>.<listcomp>x)rM   r   isCompositer   r   r   r   ynumberOfContoursr   r   r
   r   yMax)r   coordrZ   r   	leftSideX
rightSideXtopSideYbottomSideYr   pr   horizontalAdvanceWidthverticalAdvanceWidthleftSideBearing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 r<   )	glyphset1	glyphset2factor)r9   r  r  r  r   r   r   r:     s   
zLerpGlyphSet.__init__c                 C   s&   || j v r|| jv rt|| S t|r<   )r  r  	LerpGlyphKeyErrorr9   	glyphnamer   r   r   r`     s   
zLerpGlyphSet.__getitem__c                 C   s   || j v o	|| jv S r<   )r  r  r  r   r   r   rG     s   zLerpGlyphSet.__contains__c                 C   "   t | j}t | j}t||S r<   )setr  r  rI   intersectionr9   set1set2r   r   r   rL        

zLerpGlyphSet.__iter__c                 C   r	  r<   )r
  r  r  rM   r  r  r   r   r   rN     r  zLerpGlyphSet.__len__N)r  )	rQ   rR   rS   rT   r:   r`   rG   rL   rN   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 r<   )glyphsetr  )r9   r  r  r   r   r   r:     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 r<   )
r   r  r  r  r   r  r  r   r   rs   )r9   r   
recording1
recording2r  r   r   r   r     s   zLerpGlyph.drawN)rQ   rR   rS   r:   r   r   r   r   r   r    s    r  N)*rT   abcr   r   collections.abcr   
contextlibr   r   r   typesr   fontTools.misc.vectorr	   fontTools.misc.fixedToolsr
   r   r   fontTools.misc.loggingToolsr   fontTools.misc.transformr   r   fontTools.pens.transformPenr   r   fontTools.pens.recordingPenr   r   r   r   rW   rd   rt   r|   r_   rm   r   rz   r   r   r  r   r   r   r   <module>   s0    M
$,M#V&!