o
    a*                     @   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                   @   sv   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   9/usr/lib/python3/dist-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<      
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@   r   z!Cu2QuPen._check_contour_is_closedc                 C   s&   | j d ur| j| j  d | _ d S d S N)r   r   moveTor   r   r   r   _add_moveToD   s   

zCu2QuPen._add_moveToc                 C   s*   |    | | _| _| js|   d 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  d S |dkr*|   | jj|  |d | _d S td| )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r&t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|  d S |dkr%t|D ]}| j|  qd S |dkr0| j|  d S |dkr=| |d  d S td| )N   r	   r%   r   z%illegal curve segment point count: %d)r   r(   r/   r   r)   r$   r   )r   r*   r+   Zsegmentr   r   r   curveToi   s   zCu2QuPen.curveToc                 C   ,   |    | jd u r| j  d  | _| _d S r   )r   r   r   	closePathr   r   r   r   r   r3   y   s   

zCu2QuPen.closePathc                 C   r2   r   )r   r   r   endPathr   r   r   r   r   r4      s   

zCu2QuPen.endPathc                 C   s   |    | j|| d S r   )r   r   addComponent)r   Z	glyphNametransformationr   r   r   r5      s   zCu2QuPen.addComponent)FNF)__name__
__module____qualname____doc__r   r   r   r!   r    r$   r)   r/   r1   r3   r4   r5   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   s^  t |dksJ |d d dk}g }|d d }|d d }|D ]y\}}|dkr| |D ]]}|d \}	}
}}|d d |d d }}||||	g}t|| j}| jd uritt |d }| j|dd | j|< dd |dd D }||	|
||f |d	|g |d d }q-q |||g |d d }q |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(   _split_super_bezier_segmentsr   r   r   r,   r-   append_drawPoints)r   segmentsclosedZnew_segmentsZprev_pointsZprev_on_curvesegment_typer*   
sub_pointson_curvesmoothnamekwargsZbcp1Zbcp2ZcubicZquadr+   Z
new_pointsr   r   r   _flushContour   s2   
zCu2QuPointPen._flushContourc                 C   s   g }t |d }|dkr|| |S |dkri|d \}}}}|d }ttdd |D D ]8\}	}
g }|
d d D ]}||dd i f q:|	|d krV|||||f n||
d dd i f || q.|S td| )	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(   rB   	enumerater   r   )r   r*   Zsub_segmentsr+   rH   rI   rJ   rK   Znum_sub_segmentsirG   Znew_segmentZpointr   r   r   rA      s,   

z*Cu2QuPointPen._split_super_bezier_segmentsc                 C   sV  | j }|  g }t|D ]\}\}}|dv r:t|dks%J dt| |d \}}}	}
|j||||	fi |
 q|dkrt|dksLJ dt| |d d }|rp|dkr[|}n|D ]\}}}	}
|j|d ||	fi |
 q]|d \}}}	}
|d u r}q|j||||	fi |
 qtd	| |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	beginPathrN   r(   ZaddPointr   r4   )r   rD   r   Zlast_offcurvesrO   rF   r*   r#   rI   rJ   rK   Z	offcurvesr   r   r   rC      s<   

zCu2QuPointPen._drawPointsc                 C   s    | j d u sJ | j|| d S r   )ZcurrentPathr   r5   )r   ZbaseGlyphNamer6   r   r   r   r5     s   zCu2QuPointPen.addComponentr=   )	r7   r8   r9   r:   r   rL   rA   rC   r5   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