a
    w=ic*                     @   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   h/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/fontTools/pens/cu2quPen.py__init__+   s    zCu2QuPen.__init__c                 C   s   | j d u 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 urtdd S )Nz closePath or endPath is requiredr   r   r   r   r   _check_contour_is_closed@   s    
z!Cu2QuPen._check_contour_is_closedc                 C   s"   | j d ur| j| j  d | _ d S N)r   r   moveTor   r   r   r   _add_moveToD   s    
zCu2QuPen._add_moveToc                 C   s&   |    | | _| _| js"|   d S r   )r   r   r   r   r    r   ptr   r   r   r   I   s    zCu2QuPen.moveToc                 C   s&   |    |   | j| || _d S r   )r   r    r   lineTor   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   &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 u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|  nZ|dkrFt|D ]}| j|  q4n8|dkrZ| j|  n$|dkrr| |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 u r| j  d  | _| _d S r   )r   r   r   	closePathr   r   r   r   r   r2   y   s    

zCu2QuPen.closePathc                 C   s,   |    | jd u r| j  d  | _| _d S r   )r   r   r   endPathr   r   r   r   r   r3      s    

zCu2QuPen.endPathc                 C   s   |    | j|| d S r   )r   r   addComponent)r   Z	glyphNametransformationr   r   r   r4      s    zCu2QuPen.addComponent)FNF)__name__
__module____qualname____doc__r   r   r   r    r   r#   r(   r.   r1   r2   r3   r4   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 r   )r   r   r   r   r   r   )r   Zother_point_penr   r   r   r   r   r   r      s    
zCu2QuPointPen.__init__c                 C   sb  t |dksJ |d d dk}g }|d d }|d d }|D ]\}}|dkr| |D ]}|d \}	}
}}|d d |d d  }}||||	g}t|| j}| jd urtt |d }| j|dd | j|< dd |dd D }||	|
||f |d	|g |d d }q\q@|||g |d d }q@|rT|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>       z/Cu2QuPointPen._flushContour.<locals>.<listcomp>qcurve)	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 }ttdd |D D ]p\}	}
g }|
d d D ]}||dd i f qr|	|d kr|||||f n||
d dd i f || qZntd| |S )	Nr$   r	   r%   c                 S   s   g | ]\}}}}|qS r   r   )r<   r"   _r   r   r   r=      s   z>Cu2QuPointPen._split_super_bezier_segments.<locals>.<listcomp>FTz$expected 2 control points, found: %d)r'   rA   	enumerater   r   )r   r)   Zsub_segmentsr*   rH   rI   rJ   rK   Znum_sub_segmentsirG   Znew_segmentpointr   r   r   r@      s*    
z*Cu2QuPointPen._split_super_bezier_segmentsc                 C   s^  | j }|  g }t|D ]\}\}}|dv rvt|dksLJ dt| |d \}}}	}
|j||||	fi |
 q|dkrt|dksJ dt| |d d }|r|dkr|}n*|D ]$\}}}	}
|j|d ||	fi |
 q|d \}}}	}
|d u rn|j||||	fi |
 qtd	| q|D ]&\}}}	}
|j|d ||	fi |
 q*|  d S )
N)r;   liner$   z$illegal line segment point count: %dr   r?   r	   r&   r%   zunexpected segment type: %r)r   Z	beginPathrO   r'   ZaddPointr   r3   )r   rC   r   Zlast_offcurvesrP   rF   r)   r"   rI   rJ   rK   Z	offcurvesr   r   r   rB      s<    


zCu2QuPointPen._drawPointsc                 C   s    | j d u sJ | j|| d S r   )ZcurrentPathr   r4   )r   ZbaseGlyphNamer5   r   r   r   r4     s    zCu2QuPointPen.addComponent)FN)	r6   r7   r8   r9   r   rM   r@   rB   r4   r   r   r   r   r:      s     

&r:   N)ZfontTools.cu2qur   ZfontTools.pens.basePenr   r   Z fontTools.pens.reverseContourPenr   ZfontTools.pens.pointPenr   r   r   r:   r   r   r   r   <module>   s   u