B
    d'                 @   s   d dl m Z  d dl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 d dlmZ d dlmZ ddgZG dd dZG dd deeZG dd dee
eZdS )    )array)AnyDictOptionalTuple)MAX_F2DOT14floatToFixedToFloat)LogMixin)AbstractPointPen)otRound)
LoggingPenPenError)TransformPenTransformPointPen)	ttProgram)Glyph)GlyphComponent)GlyphCoordinates
TTGlyphPenTTGlyphPointPenc            	   @   s   e Zd Zdeeeef  eddddZee	e
e
e
e
e
e
f dddZd	d
 ZddddZdee	e
e
e
e
e
e
f ee eddddZdd ZdeedddZdS )_TTGlyphBasePenTN)glyphSethandleOverflowingTransformsreturnc             C   s   || _ || _|   dS )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    r   f/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/fontTools/pens/ttGlyphPen.py__init__   s    z_TTGlyphBasePen.__init__)	glyphNametransformationc             C   s&   |  | |}t| j| | j| d S )N)transformPengetattrr   
drawMethod)r   r   r    Ztpenr   r   r   
_decompose6   s    z_TTGlyphBasePen._decomposec             C   s   t dS )z6
        Check if the current path is closed.
        N)NotImplementedError)r   r   r   r   	_isClosed>   s    z_TTGlyphBasePen._isClosed)r   c             C   s   g | _ g | _g | _g | _d S )N)pointsendPtstypes
components)r   r   r   r   r   D   s    z_TTGlyphBasePen.init)baseGlyphNamer    
identifierkwargsr   c             K   s   | j ||f dS )z"
        Add a sub glyph.
        N)r*   append)r   r+   r    r,   r-   r   r   r   addComponentJ   s    
z_TTGlyphBasePen.addComponentc             C   s  | j rtdd | jD }g }x| jD ]\}}|| jkrN| jd| d q&| js^| j rl|rl| || q&t }||_	dd |dd  D \|_
|_tdd |d d D }|dkr| j rtd	d |D rtd
d |D }|d d |dd  f|_||_|| q&W |S )Nc             s   s4   | ],\}}|d d D ]}|dkp(|dk V  qqd S )N      r   ).0r   r    sr   r   r   	<genexpr>[   s   z3_TTGlyphBasePen._buildComponents.<locals>.<genexpr>z skipped non-existing component ''c             s   s   | ]}t |V  qd S )N)r   )r3   vr   r   r   r5   k   s    r0   c             s   s   | ]}t |d V  qdS )   N)r   )r3   r7   r   r   r   r5   o   s    )   r   r   r9   c             s   s&   | ]}t |  k od kn  V  qdS )r1   N)r   )r3   r4   r   r   r   r5   s   s    c             s   s.   | ]&}t |  k rd kr"n nt n|V  qdS )r1   N)r   )r3   r4   r   r   r   r5   w   s   r1   )r   anyr*   r   logwarningr'   r$   r   r   xytupleZ	transformflagsr.   )r   componentFlagsZoverflowingr*   r   r    	componentr   r   r   _buildComponentsV   s6    

z _TTGlyphBasePen._buildComponentsr0   )rA   r   c             C   s   |   std| |}t }t| j|_|j  | j|_	t
d| j|_|   |rf||_d|_n"t|j	|_t |_|jd |S )zW
        Returns a :py:class:`~._g_l_y_f.Glyph` object representing the glyph.
        zDidn't close last contour.B    )r&   r   rC   r   r   r'   ZcoordinatesZtoIntr(   ZendPtsOfContoursr   r)   r@   r   r*   ZnumberOfContourslenr   ZProgramprogramZfromBytecode)r   rA   r*   glyphr   r   r   rI      s     


z_TTGlyphBasePen.glyph)T)N)r0   )__name__
__module____qualname__r   r   strr   boolr   r   floatr$   r&   r   r/   rC   intr   rI   r   r   r   r   r      s   !
)r   c               @   s   e Zd ZdZdZeZeeef e	ddddZ
dddd	Zedd
dZeeef ddddZeeef ddddZddddZddddZddddZddddZd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.
    ZdrawN)ptonCurver   c             C   s   | j | | j| d S )N)r'   r.   r)   )r   rQ   rR   r   r   r   	_addPoint   s    zTTGlyphPen._addPoint)r   c             C   s   | j   | j  d S )N)r'   popr)   )r   r   r   r   	_popPoint   s    
zTTGlyphPen._popPointc             C   s&   | j  p$| jo$| jd t| j d kS )NrE   r9   )r'   r(   rG   )r   r   r   r   r&      s    zTTGlyphPen._isClosed)rQ   r   c             C   s   |  |d d S )Nr9   )rS   )r   rQ   r   r   r   lineTo   s    zTTGlyphPen.lineToc             C   s    |   std| |d d S )Nz+"move"-type point must begin a new contour.r9   )r&   r   rS   )r   rQ   r   r   r   moveTo   s    zTTGlyphPen.moveToc             G   s   t d S )N)r%   )r   r'   r   r   r   curveTo   s    zTTGlyphPen.curveToc             G   sR   t |dkstx |d d D ]}| |d qW |d d k	rN| |d d d S )Nr9   rE   r   )rG   AssertionErrorrS   )r   r'   rQ   r   r   r   qCurveTo   s
    zTTGlyphPen.qCurveToc             C   s   t | jd }|dks.| jr:|| jd d kr:|   d S d}| jrR| jd d }| j| | j| krv|   |d8 }| j| d S )Nr9   r   rE   )rG   r'   r(   rU   r.   )r   ZendPtZstartPtr   r   r   	closePath   s     zTTGlyphPen.closePathc             C   s   |    d S )N)r[   )r   r   r   r   endPath   s    zTTGlyphPen.endPath)rJ   rK   rL   __doc__r#   r   r!   r   rO   rP   rS   rU   rN   r&   rV   rW   rX   rZ   r[   r\   r   r   r   r   r      s   	c            	       s   e Zd ZdZdZeZdd fddZedddZ	de
e edd	d
dZddddZdeeef e
e ee
e e
e eddddZ  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.
    Z
drawPointsN)r   c                s   t    d | _d S )N)superr   _currentContourStartIndex)r   )	__class__r   r   r      s    
zTTGlyphPointPen.initc             C   s
   | j d kS )N)r_   )r   r   r   r   r&      s    zTTGlyphPointPen._isClosed)r,   r-   r   c             K   s    |   stdt| j| _dS )z'
        Start a new sub path.
        zDidn't close previous contour.N)r&   r   rG   r'   r_   )r   r,   r-   r   r   r   	beginPath   s    zTTGlyphPointPen.beginPathc             C   sH   |   rtd| jt| jkr(td| jt| jd  d| _dS )z+
        End the current sub path.
        zContour is already closed.zTried to end an empty contour.r9   N)r&   r   r_   rG   r'   r(   r.   )r   r   r   r   r\      s    zTTGlyphPointPen.endPathF)rQ   segmentTypesmoothnamer,   r-   r   c             K   sf   |   rtd|dkr&| jd n0|dkr<| jd n|dkrNtdnt|| j| dS )z6
        Add a point to the current sub path.
        z&Can't add a point to a closed contour.Nr   )Zqcurvelinemover9   Zcurvezcubic curves are not supported)r&   r   r)   r.   r%   rY   r'   )r   rQ   rb   rc   rd   r,   r-   r   r   r   addPoint  s    
zTTGlyphPointPen.addPoint)N)NFNN)rJ   rK   rL   r]   r#   r   r!   r   rN   r&   r   rM   r   ra   r\   r   rO   rg   __classcell__r   r   )r`   r   r      s       N)r   typingr   r   r   r   ZfontTools.misc.fixedToolsr   r   ZfontTools.misc.loggingToolsr	   ZfontTools.pens.pointPenr
   ZfontTools.misc.roundToolsr   ZfontTools.pens.basePenr   r   ZfontTools.pens.transformPenr   r   ZfontTools.ttLib.tablesr   ZfontTools.ttLib.tables._g_l_y_fr   r   r   __all__r   r   r   r   r   r   r   <module>   s     
D