B
    d*                 @   sd   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	 G dd deZ
G dd	 d	eZd
S )    )curve_to_quadratic)AbstractPendecomposeSuperBezierSegment)ReverseContourPen)BasePointToSegmentPen)ReverseContourPointPenc               @   sr   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )Cu2QuPena   A filter pen to convert cubic bezier curves to quadratic b-splines
    using the FontTools SegmentPen protocol.

    Args:

        other_pen: another SegmentPen used to draw the transformed outline.
        max_err: maximum approximation error in font units. For optimal results,
            if you know the UPEM of the font, we recommend setting this to a
            value equal, or close to UPEM / 1000.
        reverse_direction: flip the contours' direction but keep starting point.
        stats: a dictionary counting the point numbers of quadratic segments.
        ignore_single_points: don't emit contours containing only a single point

    NOTE: The "ignore_single_points" argument is deprecated since v1.3.0,
    which dropped Robofab subpport. It's no longer needed to special-case
    UFO2-style anchors (aka "named points") when using ufoLib >= 2.0,
    as these are no longer drawn onto pens as single-point contours,
    but are handled separately as anchors.
    FNc             C   sT   |rt || _n|| _|| _|| _|r>dd l}|jdtdd || _d | _d | _	d S )Nr   zIignore_single_points is deprecated and will be removed in future versions   )
stacklevel)
r   penmax_errstatswarningswarnUserWarningignore_single_pointsstart_pt
current_pt)selfZ	other_penr   reverse_directionr   r   r    r   d/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/fontTools/pens/cu2quPen.py__init__+   s    
zCu2QuPen.__init__c             C   s   | j d krtdd S )NzmoveTo is required)r   AssertionError)r   r   r   r   _check_contour_is_open<   s    
zCu2QuPen._check_contour_is_openc             C   s   | j d k	rtdd S )Nz closePath or endPath is required)r   r   )r   r   r   r   _check_contour_is_closed@   s    
z!Cu2QuPen._check_contour_is_closedc             C   s"   | j d k	r| j| j  d | _ d S )N)r   r   moveTo)r   r   r   r   _add_moveToD   s    
zCu2QuPen._add_moveToc             C   s&   |    | | _| _| js"|   d S )N)r   r   r   r   r   )r   ptr   r   r   r   I   s    zCu2QuPen.moveToc             C   s&   |    |   | j| || _d S )N)r   r   r   lineTor   )r   r   r   r   r   r   O   s    zCu2QuPen.lineToc             G   s`   |    t|}|dkr(| |d  n4|dkrP|   | jj|  |d | _ntd| d S )N   r   z&illegal qcurve segment point count: %d)r   lenr   r   r   qCurveTor   r   )r   pointsnr   r   r   r#   U   s    zCu2QuPen.qCurveToc             C   sb   | j |||f}t|| j}| jd k	rLtt|d }| j|dd | j|< | j|dd    d S )Nr	   r   r    )r   r   r   r   strr"   getr#   )r   Zpt1Zpt2Zpt3curveZ	quadraticr%   r   r   r   _curve_to_quadratica   s    
zCu2QuPen._curve_to_quadraticc             G   s   |    t|}|dkr$| j|  n^|dkrJxTt|D ]}| j|  q6W n8|dkr^| j|  n$|dkrv| |d  ntd| d S )N   r	   r    r   z%illegal curve segment point count: %d)r   r"   r)   r   r#   r   r   )r   r$   r%   segmentr   r   r   curveToi   s    zCu2QuPen.curveToc             C   s,   |    | jd kr| j  d  | _| _d S )N)r   r   r   	closePathr   )r   r   r   r   r-   y   s    

zCu2QuPen.closePathc             C   s,   |    | jd kr| j  d  | _| _d S )N)r   r   r   endPathr   )r   r   r   r   r.      s    

zCu2QuPen.endPathc             C   s   |    | j|| d S )N)r   r   addComponent)r   Z	glyphNametransformationr   r   r   r/      s    zCu2QuPen.addComponent)FNF)__name__
__module____qualname____doc__r   r   r   r   r   r   r#   r)   r,   r-   r.   r/   r   r   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 )Cu2QuPointPena4   A filter pen to convert cubic bezier curves to quadratic b-splines
    using the RoboFab PointPen protocol.

    Args:
        other_point_pen: another PointPen used to draw the transformed outline.
        max_err: maximum approximation error in font units. For optimal results,
            if you know the UPEM of the font, we recommend setting this to a
            value equal, or close to UPEM / 1000.
        reverse_direction: reverse the winding direction of all contours.
        stats: a dictionary counting the point numbers of quadratic segments.
    FNc             C   s0   t |  |rt|| _n|| _|| _|| _d S )N)r   r   r   r   r   r   )r   Zother_point_penr   r   r   r   r   r   r      s    
zCu2QuPointPen.__init__c             C   sl  t |dkst|d d dk}g }|d d }|d d }x |D ]\}}|dkr"x| |D ]}|d \}	}
}}|d d |d d  }}||||	g}t|| j}| jd k	rtt |d }| j|dd | j|< dd |dd D }||	|
||f |d	|g |d d }qbW qD|||g |d d }qDW |r^|dd  |d d  }| 	| d S )
Nr    r   mover!   r(   r	   c             S   s   g | ]}|d di fqS )FNr   ).0r   r   r   r   
<listcomp>   s    z/Cu2QuPointPen._flushContour.<locals>.<listcomp>qcurve)
r"   r   _split_super_bezier_segmentsr   r   r   r&   r'   append_drawPoints)r   segmentsclosednew_segmentsZprev_pointsZprev_on_curvesegment_typer$   
sub_pointson_curvesmoothnamekwargsZbcp1Zbcp2Zcubicquadr%   Z
new_pointsr   r   r   _flushContour   s0    

zCu2QuPointPen._flushContourc             C   s   g }t |d }|dkr$|| n|dkr|d \}}}}|d }xttdd |D D ]t\}	}
g }x&|
d d D ]}||dd i f qvW |	|d kr|||||f n||
d dd i f || q\W ntd| |S )	Nr    r	   r!   c             S   s   g | ]\}}}}|qS r   r   )r7   r   _r   r   r   r8      s    z>Cu2QuPointPen._split_super_bezier_segments.<locals>.<listcomp>FTz$expected 2 control points, found: %d)r"   r;   	enumerater   r   )r   r$   Zsub_segmentsr%   rB   rC   rD   rE   Znum_sub_segmentsirA   Znew_segmentpointr   r   r   r:      s&    
z*Cu2QuPointPen._split_super_bezier_segmentsc             C   s\  | j }|  g }xt|D ] \}\}}|dkrvt|dksPtdt| |d \}}}	}
|j||||	f|
 q|dkrt|dkstdt| |d d }|r|dkr|}n*x(|D ] \}}}	}
|j|d ||	f|
 qW |d \}}}	}
|d krn|j||||	f|
 qtd	| qW x*|D ]"\}}}	}
|j|d ||	f|
 q*W |  d S )
N)r6   liner    z$illegal line segment point count: %dr   r9   r	   z&illegal qcurve segment point count: %dr!   zunexpected segment type: %r)r   Z	beginPathrI   r"   r   ZaddPointr.   )r   r=   r   Zlast_offcurvesrJ   r@   r$   r   rC   rD   rE   Z	offcurvesr   r   r   r<      s6    
zCu2QuPointPen._drawPointsc             C   s    | j d kst| j|| d S )N)ZcurrentPathr   r   r/   )r   ZbaseGlyphNamer0   r   r   r   r/     s    zCu2QuPointPen.addComponent)FN)	r1   r2   r3   r4   r   rG   r:   r<   r/   r   r   r   r   r5      s    
	&r5   N)ZfontTools.cu2qur   ZfontTools.pens.basePenr   r   Z fontTools.pens.reverseContourPenr   ZfontTools.pens.pointPenr   r   r   r5   r   r   r   r   <module>   s   u