a
    —ÆàgÏ2  ã                   @   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                       s6   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__© úP/var/www/sistema_ama/venv/lib/python3.9/site-packages/fontTools/pens/cu2quPen.pyr   )   s    zCu2QuPen.__init__c                    s²   | j |||f}t|| j| jƒ}| jd urPtt|ƒd ƒ}| j |d¡d | j|< | jrj| j|dd … Ž  nDt|ƒdkrŠ| j|dd … Ž  n$t|ƒdksšJ ‚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   sL   t |ƒ}|dkr| j|Ž  n,|dkr>t|ƒD ]}| j|Ž  q,n
| 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                   @   s`   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�rF|  |¡D ]ä}|d \}	}
}}|d d |d d  }}||||	g}t|| j| jƒ}| jd urÚtt |ƒd ƒ}| j |d¡d | j|< dd„ |dd… D ƒ}| |	|
||f¡ | j�st |ƒ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>r/   )
r   Ú_split_super_bezier_segmentsr   r   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{   s4    

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   r1   c                 S   s   g | ]\}}}}|‘qS r   r   )r2   r3   Ú_r   r   r   r4   ¨   r5   z>Cu2QuPointPen._split_super_bezier_segments.<locals>.<listcomp>FTz$expected 2 control points, found: %d)r   r7   Ú	enumerater   ÚAssertionError)r   r%   Zsub_segmentsr#   r=   r>   r?   r@   Znum_sub_segmentsÚir<   Znew_segmentZpointr   r   r   r6   œ   s&    ÿz*Cu2QuPointPen._split_super_bezier_segmentsc              	   C   s6  | j }| ¡  g }| j}t|ƒD ]Ü\}\}}||v rð|| \}}	|	t|ƒ|ƒslJ d|›d|› dt|ƒ› �ƒ‚|d d… }
|dkr†|
}n*|
D ]$\}}}}|j|d ||fi |¤Ž qŠ|d \}}}}|d u rÖ|dksîJ ‚qü|j||||fi |¤Ž q td| ƒ‚q |D ]&\}}}}|j|d ||fi |¤Ž �q| ¡  d S )Nzillegal z segment point count: expected z, got r1   r   r/   zunexpected segment type: %r)r0   Z	beginPathÚ_Cu2QuPointPen__points_requiredrC   r   ZaddPointrD   ÚendPath)r   r9   r0   Zlast_offcurvesZpoints_requiredrE   r;   r%   r#   ÚopZ	offcurvesr3   r>   r?   r@   r   r   r   r8   ¸   s8    ÿÿÿzCu2QuPointPen._drawPointsc                 C   s    | j d u sJ ‚| j ||¡ d S r   )ZcurrentPathr0   ÚaddComponent)r   ZbaseGlyphNameZtransformationr   r   r   rI   Û   s    zCu2QuPointPen.addComponent)FNT)r'   r(   r)   r*   ÚoperatorÚeqÚgerF   r   rA   r6   r8   rI   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   )r2   r0   r   r   r   r4   ÷   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©rP   rD   ©r   r   r   r   Ú_check_contour_is_open   s    
z$Cu2QuMultiPen._check_contour_is_openc                 C   s   | j d urtdƒ‚d S )Nz closePath or endPath is requiredrQ   rR   r   r   r   Ú_check_contour_is_closed  s    
z&Cu2QuMultiPen._check_contour_is_closedc                 C   s6   | j d ur2t| j | jƒD ]\}}|j|Ž  qd | _ d S r   )rO   ÚziprN   ÚmoveTo)r   r3   r0   r   r   r   Ú_add_moveTo  s    
zCu2QuMultiPen._add_moveToc                 C   s    |   ¡  | | _| _|  ¡  d S r   )rT   rO   rP   rW   )r   Úptsr   r   r   rV     s    zCu2QuMultiPen.moveToc                 C   s:   |   ¡  |  ¡  t|| jƒD ]\}}|j|Ž  q|| _d S r   )rS   rW   rU   rN   ÚlineTorP   )r   rX   r3   r0   r   r   r   rY     s
    zCu2QuMultiPen.lineToc                 C   sv   |   ¡  t|d ƒdkr0|  dd„ |D ƒ¡ d S |  ¡  g }t|| jƒD ]"\}}|j|Ž  | |d f¡ qH|| _d S )Nr   r   c                 S   s   g | ]}|d  f‘qS )r   r   )r2   r%   r   r   r   r4     r5   z*Cu2QuMultiPen.qCurveTo.<locals>.<listcomp>r1   )	rS   r   rY   rW   rU   rN   r   r7   rP   )r   Ú
pointsListrP   r%   r0   r   r   r   r     s    
zCu2QuMultiPen.qCurveToc                 C   sl   g }t | j|ƒD ]\}}| || ¡ qt|| jgt|ƒ ƒ}g }|D ]}| |dd … ¡ qF|  |¡ d S )Nr   )rU   rP   r7   r   r   r   r   )r   rZ   Zcurvesr   r%   Z
quadraticsZ	quadraticr   r   r   Ú_curves_to_quadratic&  s    z"Cu2QuMultiPen._curves_to_quadraticc                 C   s   |   ¡  |  |¡ d S r   )rS   r[   )r   rZ   r   r   r   r    0  s    zCu2QuMultiPen.curveToc                 C   s6   |   ¡  | jd u r&| jD ]}| ¡  qd  | _| _d S r   )rS   rO   rN   Ú	closePathrP   ©r   r0   r   r   r   r\   4  s
    


zCu2QuMultiPen.closePathc                 C   s6   |   ¡  | jd u r&| jD ]}| ¡  qd  | _| _d S r   )rS   rO   rN   rG   rP   r]   r   r   r   rG   ;  s
    


zCu2QuMultiPen.endPathc                 C   s.   |   ¡  t|| jƒD ]\}}| ||¡ qd S r   )rT   rU   rN   rI   )r   Z	glyphNameZtransformationsZtransr0   r   r   r   rI   B  s    zCu2QuMultiPen.addComponentN)F)r'   r(   r)   r*   r   rS   rT   rW   rV   rY   r   r[   r    r\   rG   rI   r   r   r   r   rM   à   s   

rM   )rJ   ZfontTools.cu2qur   r   ZfontTools.pens.basePenr   ZfontTools.pens.filterPenr   Z fontTools.pens.reverseContourPenr   ZfontTools.pens.pointPenr   r	   r
   r,   rM   r   r   r   r   Ú<module>   s   ; 