
    jke^.                     "   d dl m Z  d dlmZmZmZmZm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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 d dlmZ d dlmZ d dlmZ d dlZddgZ  G d d      Z! G d de!e      Z" G d de!ee      Z#y)    )array)AnyCallableDictOptionalTuple)MAX_F2DOT14floatToFixedToFloat)LogMixin)AbstractPointPenotRound)
LoggingPenPenError)TransformPenTransformPointPen)	ttProgram)flagOnCurve	flagCubic)Glyph)GlyphComponent)GlyphCoordinates)dropImpliedOnCurvePointsN
TTGlyphPenTTGlyphPointPenc                       e Zd Z	 ddeeeef      deddfdZdede	e
e
e
e
e
e
f   fdZd	 Zdd
Z	 ddede	e
e
e
e
e
e
f   dee   deddf
dZd Z	 	 deddededee
gef   defdZy)_TTGlyphBasePenglyphSethandleOverflowingTransformsreturnNc                 @    || _         || _        | j                          y)a  
        Construct a new pen.

        Args:
            glyphSet (Dict[str, Any]): A glyphset object, used to resolve components.
            handleOverflowingTransforms (bool): See below.

        If ``handleOverflowingTransforms`` is True, the components' transform values
        are checked that they don't overflow the limits of a F2Dot14 number:
        -2.0 <= v < +2.0. If any transform value exceeds these, the composite
        glyph is decomposed.

        An exception to this rule is done for values that are very close to +2.0
        (both for consistency with the -2.0 case, and for the relative frequency
        these occur in real fonts). When almost +2.0 values occur (and all other
        values are within the range -2.0 <= x <= +2.0), they are clamped to the
        maximum positive value that can still be encoded as an F2Dot14: i.e.
        1.99993896484375.

        If False, no check is done and all components are translated unmodified
        into the glyf table, followed by an inevitable ``struct.error`` once an
        attempt is made to compile them.

        If both contours and components are present in a glyph, the components
        are decomposed.
        N)r   r   init)selfr   r   s      ;/usr/lib/python3/dist-packages/fontTools/pens/ttGlyphPen.py__init__z_TTGlyphBasePen.__init__   s    > !+F(		    	glyphNametransformationc                 z    | j                  | |      } t        | j                  |   | j                        |       y N)transformPengetattrr   
drawMethod)r#   r'   r(   tpens       r$   
_decomposez_TTGlyphBasePen._decompose9   s4    
   ~6:i($//:4@r&   c                     t         )z6
        Check if the current path is closed.
        )NotImplementedErrorr#   s    r$   	_isClosedz_TTGlyphBasePen._isClosedA   s
     "!r&   c                 <    g | _         g | _        g | _        g | _        y r*   )pointsendPtstypes
componentsr2   s    r$   r"   z_TTGlyphBasePen.initG   s    
r&   baseGlyphName
identifierkwargsc                 >    | j                   j                  ||f       y)z"
        Add a sub glyph.
        N)r8   append)r#   r9   r(   r:   r;   s        r$   addComponentz_TTGlyphBasePen.addComponentM   s     	~>?r&   c                 n   | j                   rt        d | j                  D              }g }| j                  D ]  \  }}|| j                  vr | j                  j                  d| d       4| j                  s| j                   rr| j                  ||       at               }||_	        d |dd  D        \  |_
        |_        t        d |d d D              }|dk7  r?| j                   r$t        d |D              rt        d	 |D              }|d d
 |d
d  f|_        ||_        |j                  |        |S )Nc              3   N   K   | ]  \  }}|d d D ]  }|dkD  xs |dk     y w)N       ).0r'   r(   ss       r$   	<genexpr>z3_TTGlyphBasePen._buildComponents.<locals>.<genexpr>]   sF      /Y'+  ARs   #%z skipped non-existing component ''c              3   2   K   | ]  }t        |        y wr*   r   rE   vs     r$   rG   z3_TTGlyphBasePen._buildComponents.<locals>.<genexpr>n   s     'Oq
'Os   rA   c              3   4   K   | ]  }t        |d         yw)   N)r
   rJ   s     r$   rG   z3_TTGlyphBasePen._buildComponents.<locals>.<genexpr>q   s      #/0#Ar*#s   )   r   r   rN   c              3   D   K   | ]  }t         |cxk  xr d k  nc   ywrB   Nr	   rE   rF   s     r$   rG   z3_TTGlyphBasePen._buildComponents.<locals>.<genexpr>u   s"      <-.K!(q((<s    c              3   P   K   | ]  }t         |cxk  rd k  r	n nt         n|   ywrP   rQ   rR   s     r$   rG   z3_TTGlyphBasePen._buildComponents.<locals>.<genexpr>y   s)      + (3Q';!';B+s   $&rB   )r   anyr8   r   logwarningr5   r/   r   r'   xytuple	transformflagsr=   )r#   componentFlagsoverflowingr8   r'   r(   	components          r$   _buildComponentsz _TTGlyphBasePen._buildComponentsY   sX   ++  37?? K
 
)- 	)%I~-  #CI;a!PQ{{t??K	>:&(I"+I'ON12<N'O$IK # #4B2A4F# N -33 <2@< 9 &+ +!/+ &N (6bq'9>!";M&N	#,IOi(9	): r&   roundr\   dropImpliedOnCurvesra   c                0   | j                         st        d      | j                  |      }t               }t	        | j
                        |_        | j                  |_        t        d| j                        |_        | j                          |r||_        d|_        |S t        |j                        |_        t!        j"                         |_        |j$                  j'                  d       |rt)        |       |j                  j+                  |       |S )a  
        Returns a :py:class:`~._g_l_y_f.Glyph` object representing the glyph.

        Args:
            componentFlags: Flags to use for component glyphs. (default: 0x04)

            dropImpliedOnCurves: Whether to remove implied-oncurve points. (default: False)
        zDidn't close last contour.Br&   r`   )r3   r   r_   r   r   r5   coordinatesr6   endPtsOfContoursr   r7   r[   r"   r8   numberOfContourslenr   ProgramprogramfromBytecoder   toInt)r#   r\   rb   ra   r8   glyphs         r$   rn   z_TTGlyphBasePen.glyph   s     ~~788**>:
,T[[9!%C,		  *E%'E"  &))?)?%@E"%--/EMMM&&s+"(/##%#0r&   )Tr    Nr*   )rA   F)__name__
__module____qualname__r   r   strr   boolr%   r   floatr/   r3   r"   r>   r_   r   intr   r   rn   rD   r&   r$   r   r      s    -1!4S>*! &*! 
	!FAA eUE5%FGA" %)	
@
@ eUE5%FG
@ SM	
@
 
@ 

@'V #$)&
 )0&& "&
 %& 
&r&   r   c            	            e Zd ZdZdZeZ	 	 	 ddeee	e
f      dededdf fdZd	eeef   d
eddfdZddZdefdZd	eeef   ddfdZd	eeef   ddfdZddZddZddZddZ xZS )r   a  
    Pen used for drawing to a TrueType glyph.

    This pen can be used to construct or modify glyphs in a TrueType format
    font. After using the pen to draw, use the ``.glyph()`` method to retrieve
    a :py:class:`~._g_l_y_f.Glyph` object representing the glyph.
    drawNr   r   outputImpliedClosingLiner    c                 4    t         |   ||       || _        y r*   )superr%   ry   )r#   r   r   ry   	__class__s       r$   r%   zTTGlyphPen.__init__   s     	#>?(@%r&   pttpc                 p    | j                   j                  |       | j                  j                  |       y r*   )r5   r=   r7   )r#   r}   r~   s      r$   	_addPointzTTGlyphPen._addPoint   s&    2

"r&   c                 l    | j                   j                          | j                  j                          y r*   )r5   popr7   r2   s    r$   	_popPointzTTGlyphPen._popPoint   s    

r&   c                     | j                    xs6 | j                  xr( | j                  d   t        | j                         dz
  k(  S )Nre   rN   )r5   r6   ri   r2   s    r$   r3   zTTGlyphPen._isClosed   s<    KK 
KKCDKKOs4;;/?!/CC	
r&   c                 0    | j                  |t               y r*   )r   r   r#   r}   s     r$   lineTozTTGlyphPen.lineTo   s    r;'r&   c                 f    | j                         st        d      | j                  |t               y )Nz+"move"-type point must begin a new contour.)r3   r   r   r   r   s     r$   moveTozTTGlyphPen.moveTo   s&    ~~HIIr;'r&   c                     t        |      dz  dk(  sJ |d d D ]  }| j                  |t                |d   | j                  |d   d       y y )NrB   rN   re   )ri   r   r   r#   r5   r}   s      r$   curveTozTTGlyphPen.curveTo   s^    6{Q!###"+ 	*BNN2y)	* ":!NN6":q) "r&   c                     t        |      dk\  sJ |d d D ]  }| j                  |d        |d   | j                  |d   d       y y )NrN   re   r   )ri   r   r   s      r$   qCurveTozTTGlyphPen.qCurveTo   sZ    6{a"+ 	"BNN2q!	" ":!NN6":q) "r&   c                    t        | j                        dz
  }|dk(  s!| j                  r&|| j                  d   dz   k(  r| j                          y | j                  sTd}| j                  r| j                  d   dz   }| j                  |   | j                  |   k(  r| j                          |dz  }| j                  j                  |       y )NrN   r   re   )ri   r5   r6   r   ry   r=   )r#   endPtstartPts      r$   	closePathzTTGlyphPen.closePath   s    DKK 1$ A:$++%4;;r?Q3F*FNN,,G{{++b/A-{{7#t{{5'99 
5!r&   c                 $    | j                          y r*   )r   r2   s    r$   endPathzTTGlyphPen.endPath   s    r&   )NTFro   )rp   rq   rr   __doc__r-   r   r+   r   r   rs   r   rt   r%   r   ru   rv   r   r   r3   r   r   r   r   r   r   __classcell__r|   s   @r$   r   r      s     JL .2,0).	A4S>*A &*A #'	A
 
AE%,/ S T 
4 

(ue|, ( ((ue|, ( (
**"&r&   c                        e Zd ZdZdZeZd fdZdefdZ	dde
e   deddfd	Zdd
Z	 	 	 	 ddeeef   de
e   dede
e   de
e   deddfdZ xZS )r   a  
    Point pen used for drawing to a TrueType glyph.

    This pen can be used to construct or modify glyphs in a TrueType format
    font. After using the pen to draw, use the ``.glyph()`` method to retrieve
    a :py:class:`~._g_l_y_f.Glyph` object representing the glyph.
    
drawPointsr    Nc                 0    t         |           d | _        y r*   )r{   r"   _currentContourStartIndex)r#   r|   s    r$   r"   zTTGlyphPointPen.init  s    )-&r&   c                     | j                   d u S r*   )r   r2   s    r$   r3   zTTGlyphPointPen._isClosed  s    --55r&   r:   r;   c                 n    | j                         st        d      t        | j                        | _        y)z'
        Start a new sub path.
        zDidn't close previous contour.N)r3   r   ri   r5   r   )r#   r:   r;   s      r$   	beginPathzTTGlyphPointPen.beginPath  s+     ~~;<<),T[[)9&r&   c                    | j                         rt        d      | j                  t        | j                        k(  rd| _        y| j
                  r| j
                  d   dz   nd}| j
                  j                  t        | j                        dz
         d| _        | j                  }t        |t        |            D ]K  }||   dk(  s|dz
  }||k  rt        |      dz
  }||   dk(  rt        ||<   |dz  }||   dk(  rt        ||<   M y)z+
        End the current sub path.
        zContour is already closed.Nre   rN   r   curve)r3   r   r   ri   r5   r6   r=   r7   ranger   r   )r#   contourStartr[   ijs        r$   r   zTTGlyphPointPen.endPath  s   
 >>788))S-==-1D*.2kkt{{2*q3t{{+a/0)-& 

|SZ0 	'AQx7"E|#E
QAAh!m(E!HFA Ah!m 'a	'r&   r}   segmentTypesmoothnamec                    | j                         rt        d      || j                  j                  d       nu|dv r | j                  j                  t               nQ|dk(  r | j                  j                  t               n,|dk(  r| j                  j                  d       nt        |      | j                  j                  |       y)z6
        Add a point to the current sub path.
        z&Can't add a point to a closed contour.Nr   )linemoveqcurver   )r3   r   r7   r=   r   AssertionErrorr5   )r#   r}   r   r   r   r:   r;   s          r$   addPointzTTGlyphPointPen.addPoint6  s     >>CDDJJa ,,JJk*H$JJk*G#JJg& --2r&   ro   r*   )NFNN)rp   rq   rr   r   r-   r   r+   r"   rt   r3   r   rs   r   r   r   r   ru   r   r   r   s   @r$   r   r      s     J$L.64 6:HSM :C :D :'> &*"$(%, c] 	
 sm SM  
r&   )$r   typingr   r   r   r   r   fontTools.misc.fixedToolsr	   r
   fontTools.misc.loggingToolsr   fontTools.pens.pointPenr   fontTools.misc.roundToolsr   fontTools.pens.basePenr   r   fontTools.pens.transformPenr   r   fontTools.ttLib.tablesr   fontTools.ttLib.tables._g_l_y_fr   r   r   r   r   r   math__all__r   r   r   rD   r&   r$   <module>r      ss     7 7 F 0 4 - 7 G , B 1 : < D  *
+S SlQ* QhPox1A Pr&   