o
    i2                     @   s   d dl 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
mZ G dd	 d	eZG d
d deZG dd dZdS )    N)curve_to_quadraticcurves_to_quadratic)decomposeSuperBezierSegment)	FilterPenReverseContourPen)BasePointToSegmentPen)ReverseContourPointPenc                       s<   e Zd ZdZ			d fdd	Z fddZd	d
 Z  Z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.
        all_quadratic: if True (default), only quadratic b-splines are generated.
            if False, quadratic curves or cubic curves are generated depending
            on which one is more economical.
    FNTc                    s.   |rt |}t | || _|| _|| _d S N)r   super__init__max_errstatsall_quadratic)selfZ	other_penr   reverse_directionr   r   	__class__ f/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/fontTools/pens/cu2quPen.pyr   )   s   
zCu2QuPen.__init__c                    s   | j |||f}t|| j| j}| jd ur(tt|d }| j|dd | j|< | jr6| j|dd    d S t|dkrG| j|dd    d S t|dksOJ t	 j
|dd    d S )N   r            )
current_ptr   r   r   r   strlengetqCurveTor   curveTo)r   Zpt1Zpt2Zpt3curveresultnr   r   r   _convert_curve8   s   
zCu2QuPen._convert_curvec                 G   sP   t |}|dkr| j|  d S |dkr!t|D ]}| j|  qd S | j|  d S )Nr   )r   r$   r   r   )r   pointsr#   segmentr   r   r   r    G   s   zCu2QuPen.curveToFNT)__name__
__module____qualname____doc__r   r$   r    __classcell__r   r   r   r   r
      s    r
   c                   @   sf   e Zd ZdZdejfdejfdejfdejfdZ			dd	d
Zdd Z	dd Z
dd Zdd ZdS )Cu2QuPointPena  A filter pen to convert cubic bezier curves to quadratic b-splines
    using the FontTools 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.
        all_quadratic: if True (default), only quadratic b-splines are generated.
            if False, quadratic curves or cubic curves are generated depending
            on which one is more economical.
    r   r   r   )movelineqcurver!   FNTc                 C   s6   t |  |rt|| _n|| _|| _|| _|| _d S r   )r   r   r	   penr   r   r   )r   Zother_point_penr   r   r   r   r   r   r   r   j   s   

zCu2QuPointPen.__init__c                 C   s  t |dksJ |d d dk}g }|d d }|d d }|D ]\}}|dkr| |D ]p}|d \}	}
}}|d d |d d }}||||	g}t|| j| j}| jd urktt |d }| j|dd | j|< dd |dd D }||	|
||f | jst |dkr|d	|g n|d|g |d d }q-q |||g |d d }q |r|dd  |d d  }| 	| d S )
Nr   r   r.   r!   r   c                 S   s   g | ]}|d di fqS )FNr   ).0ptr   r   r   
<listcomp>       z/Cu2QuPointPen._flushContour.<locals>.<listcomp>r0   )
r   _split_super_bezier_segmentsr   r   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{   s6   
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   r2   c                 S   s   g | ]\}}}}|qS r   r   )r3   r4   _r   r   r   r5      r6   z>Cu2QuPointPen._split_super_bezier_segments.<locals>.<listcomp>FTz$expected 2 control points, found: %d)r   r8   	enumerater   AssertionError)r   r%   Zsub_segmentsr#   r?   r@   rA   rB   Znum_sub_segmentsir>   Znew_segmentpointr   r   r   r7      s(   
z*Cu2QuPointPen._split_super_bezier_segmentsc              	   C   s2  | j }|  g }| j}t|D ]m\}\}}||v rx|| \}}	|	t||s6J d|d| dt| |d d }
|dkrC|
}n|
D ]\}}}}|j|d ||fi | qE|d \}}}}|d u rk|dksjJ q|j||||fi | qtd| |D ]\}}}}|j|d ||fi | q|  d S )Nzillegal z segment point count: expected z, got r2   r   r0   zunexpected segment type: %r)r1   Z	beginPath_Cu2QuPointPen__points_requiredrF   r   ZaddPointrG   endPath)r   r:   r1   Zlast_offcurvesZpoints_requiredrH   r=   r%   r#   opZ	offcurvesr4   r@   rA   rB   r   r   r   r9      s8   zCu2QuPointPen._drawPointsc                 C   s    | j d u sJ | j|| d S r   )ZcurrentPathr1   addComponent)r   ZbaseGlyphNameZtransformationr   r   r   rM      s   zCu2QuPointPen.addComponentr'   )r(   r)   r*   r+   operatoreqgerJ   r   rD   r7   r9   rM   r   r   r   r   r-   S   s    
!#r-   c                   @   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 )Cu2QuMultiPena  A filter multi-pen to convert cubic bezier curves to quadratic b-splines
    in a interpolation-compatible manner, using the FontTools SegmentPen protocol.

    Args:

        other_pens: list of SegmentPens used to draw the transformed outlines.
        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.

    This pen does not follow the normal SegmentPen protocol. Instead, its
    moveTo/lineTo/qCurveTo/curveTo methods take a list of tuples that are
    arguments that would normally be passed to a SegmentPen, one item for
    each of the pens in other_pens.
    Fc                 C   s.   |r	dd |D }|| _ || _d | _d | _d S )Nc                 S   s   g | ]}t |d dqS )T)ZoutputImpliedClosingLiner   )r3   r1   r   r   r   r5      s    
z*Cu2QuMultiPen.__init__.<locals>.<listcomp>)pensr   	start_ptscurrent_pts)r   Z
other_pensr   r   r   r   r   r      s   
zCu2QuMultiPen.__init__c                 C   s   | j d u r	tdd S )NzmoveTo is requiredrT   rG   r   r   r   r   _check_contour_is_open      
z$Cu2QuMultiPen._check_contour_is_openc                 C   s   | j d ur	tdd S )Nz closePath or endPath is requiredrU   rV   r   r   r   _check_contour_is_closed  rX   z&Cu2QuMultiPen._check_contour_is_closedc                 C   s:   | j d urt| j | jD ]	\}}|j|  qd | _ d S d S r   )rS   ziprR   moveTo)r   r4   r1   r   r   r   _add_moveTo  s
   

zCu2QuMultiPen._add_moveToc                 C   s    |    | | _| _|   d S r   )rY   rS   rT   r\   )r   ptsr   r   r   r[     s   zCu2QuMultiPen.moveToc                 C   s:   |    |   t|| jD ]	\}}|j|  q|| _d S r   )rW   r\   rZ   rR   lineTorT   )r   r]   r4   r1   r   r   r   r^     s
   
zCu2QuMultiPen.lineToc                 C   sv   |    t|d dkr| dd |D  d S |   g }t|| jD ]\}}|j|  ||d f q$|| _d S )Nr   r   c                 S   s   g | ]}|d  fqS )r   r   )r3   r%   r   r   r   r5     s    z*Cu2QuMultiPen.qCurveTo.<locals>.<listcomp>r2   )	rW   r   r^   r\   rZ   rR   r   r8   rT   )r   
pointsListrT   r%   r1   r   r   r   r     s   

zCu2QuMultiPen.qCurveToc                 C   sl   g }t | j|D ]\}}|||  qt|| jgt| }g }|D ]}||dd   q#| | d S )Nr   )rZ   rT   r8   r   r   r   r   )r   r_   Zcurvesr   r%   Z
quadraticsZ	quadraticr   r   r   _curves_to_quadratic&  s   z"Cu2QuMultiPen._curves_to_quadraticc                 C   s   |    | | d S r   )rW   r`   )r   r_   r   r   r   r    0  s   zCu2QuMultiPen.curveToc                 C   6   |    | jd u r| jD ]}|  qd  | _| _d S r   )rW   rS   rR   	closePathrT   r   r1   r   r   r   rb   4  
   


zCu2QuMultiPen.closePathc                 C   ra   r   )rW   rS   rR   rK   rT   rc   r   r   r   rK   ;  rd   zCu2QuMultiPen.endPathc                 C   s.   |    t|| jD ]
\}}||| q
d S r   )rY   rZ   rR   rM   )r   Z	glyphNameZtransformationsZtransr1   r   r   r   rM   B  s   zCu2QuMultiPen.addComponentN)F)r(   r)   r*   r+   r   rW   rY   r\   r[   r^   r   r`   r    rb   rK   rM   r   r   r   r   rQ      s    

rQ   )rN   ZfontTools.cu2qur   r   ZfontTools.pens.basePenr   ZfontTools.pens.filterPenr   Z fontTools.pens.reverseContourPenr   ZfontTools.pens.pointPenr   r	   r
   r-   rQ   r   r   r   r   <module>   s   ; 