a
    èÝEböA  ã                   @   s\  zd dl Z W n ey*   d dlm Z  Y n0 d dlZddlmZmZ ddgZdZ	e
dƒZe jrdd	Znd
Ze je je  e j¡e je je jd�dd„ ƒƒƒƒZe je je je je je je jd�e je je je je jd�dd„ ƒƒƒƒZe je je je je je je jd�e je je je je jd�dd„ ƒƒƒƒZe je je je je je jd�dd„ ƒƒZe je je je je je jd�e je je je je jd�e je je je je jd�e je je je je jd�dd„ ƒƒƒƒZe je je je je jd�e je je jd�dd„ ƒƒZe je je je je je jd�e je je je je jd �dLd"d#„ƒƒZe  e j¡e je je je je je jd$�e je je jd%�d&d'„ ƒƒƒZe  e j¡e je je je je jd�e je je je je jd(�d)d*„ ƒƒƒZe je  e j¡e je je je je je jd+�e je je jd�d,d-„ ƒƒƒƒZe je je je jd.�e je je je je je jd/�dMd1d2„ƒƒƒZe je je je je jd3�e je jd4�e je je je je jd5�e je je je je je jd6�dNd7d8„ƒƒƒƒƒZ e je jd9�e je jd:�d;d„ ƒƒZ!e je je je jd<�d=d„ ƒZ"e#d>k�rXd dl$Z$d dl%Z%d?Z&d@dA„ Z'dBdC„ Z(dDdE„ Z)dOdHdI„Z*dJdK„ Z+e$ ,d¡ e+ƒ  dS )Pé    N)Úcythoné   )ÚErrorÚApproxNotFoundErrorÚcurve_to_quadraticÚcurves_to_quadraticéd   ÚNaNTF©Úv1Úv2c                 C   s   | |  ¡  jS )zªReturn the dot product of two vectors.

    Args:
        v1 (complex): First vector.
        v2 (complex): Second vector.

    Returns:
        double: Dot product.
    )Ú	conjugateÚrealr
   © r   úU/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/fontTools/cu2qu/cu2qu.pyÚdot,   s    r   )ÚaÚbÚcÚd)Ú_1Ú_2Ú_3Ú_4c                 C   s<   |}|d | }|| d | }| | | | }||||fS ©Nç      @r   )r   r   r   r   r   r   r   r   r   r   r   Úcalc_cubic_points=   s
    r   )Úp0Úp1Úp2Úp3c                 C   s<   ||  d }|| d | }| }|| | | }||||fS r   r   )r   r   r   r    r   r   r   r   r   r   r   Úcalc_cubic_parametersI   s
    r!   c                 C   s    |dkrt t| |||ƒƒS |dkr4t t| |||ƒƒS |dkrbt| |||ƒ\}}t t|Ž t|Ž  ƒS |dkr�t| |||ƒ\}}t t|Ž t|Ž  ƒS t| ||||ƒS )a±  Split a cubic Bezier into n equal parts.

    Splits the curve into `n` equal parts by curve time.
    (t=0..1/n, t=1/n..2/n, ...)

    Args:
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.

    Returns:
        An iterator yielding the control points (four complex values) of the
        subcurves.
    é   é   é   é   )ÚiterÚsplit_cubic_into_twoÚsplit_cubic_into_threeÚ_split_cubic_into_n_gen)r   r   r   r    Únr   r   r   r   r   Úsplit_cubic_into_n_iterU   s    r+   )r   r   r   r    r*   )ÚdtÚdelta_2Údelta_3Úi)Úa1Úb1Úc1Úd1c                 c   s¼   t | |||ƒ\}}}}d| }	|	|	 }
|	|
 }t|ƒD ]€}||	 }|| }|| }d| | | |
 }d| | | d| |  |	 }|| | ||  ||  | }t||||ƒV  q6d S )Nr   r#   r"   )r!   Úranger   )r   r   r   r    r*   r   r   r   r   r,   r-   r.   r/   Út1Zt1_2r0   r1   r2   r3   r   r   r   r)   v   s      r)   )ÚmidÚderiv3c                 C   s\   | d||   | d }|| | |  d }| | | d || |f||| || d |ffS )aŒ  Split a cubic Bezier into two equal parts.

    Splits the curve into two equal parts at t = 0.5

    Args:
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.

    Returns:
        tuple: Two cubic Beziers (each expressed as a tuple of four complex
        values).
    r#   ç      À?ç      à?r   )r   r   r   r    r6   r7   r   r   r   r'   Š   s
    ÿr'   )r   r   r   r    Ú_27)Úmid1Úderiv1Úmid2Úderiv2çh/¡½„ö¢?c           	      C   sº   d|  d|  d|  | | }|d|  d|   | }| d|  d|  d|  | }d| d|  |  | }| d|  | d || |f||| || |f||| |d|  d |ffS )až  Split a cubic Bezier into three equal parts.

    Splits the curve into three equal parts at t = 1/3 and t = 2/3

    Args:
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.

    Returns:
        tuple: Three cubic Beziers (each expressed as a tuple of four complex
        values).
    é   é   r%   r#   r$   r"   r   r   )	r   r   r   r    r:   r;   r<   r=   r>   r   r   r   r(   ¡   s      þr(   )Útr   r   r   r    )Ú_p1Ú_p2c                 C   s0   ||| d  }||| d  }||| |   S )ax  Approximate a cubic Bezier using a quadratic one.

    Args:
        t (double): Position of control point.
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.

    Returns:
        complex: Location of candidate control point on quadratic curve.
    g      ø?r   )rB   r   r   r   r    rC   rD   r   r   r   Úcubic_approx_control½   s    rE   )ÚabÚcdÚpÚhc                 C   s^   ||  }|| }|d }zt || | ƒt ||ƒ }W n tyP   tttƒ Y S 0 |||  S )ay  Calculate the intersection of two lines.

    Args:
        a (complex): Start point of first line.
        b (complex): End point of first line.
        c (complex): Start point of second line.
        d (complex): End point of second line.

    Returns:
        complex: Location of intersection if one present, ``complex(NaN,NaN)``
        if no intersection was found.
    y              ð?)r   ÚZeroDivisionErrorÚcomplexÚNAN)r   r   r   r   rF   rG   rH   rI   r   r   r   Úcalc_intersectÒ   s    rM   )Ú	tolerancer   r   r   r    c                 C   s�   t |ƒ|krt |ƒ|krdS | d||   | d }t |ƒ|krDdS || | |  d }t| | | d || ||ƒoŽt||| || d ||ƒS )a�  Check if a cubic Bezier lies within a given distance of the origin.

    "Origin" means *the* origin (0,0), not the start of the curve. Note that no
    checks are made on the start and end positions of the curve; this function
    only checks the inside of the curve.

    Args:
        p0 (complex): Start point of curve.
        p1 (complex): First handle of curve.
        p2 (complex): Second handle of curve.
        p3 (complex): End point of curve.
        tolerance (double): Distance from origin.

    Returns:
        bool: True if the cubic Bezier ``p`` entirely lies within a distance
        ``tolerance`` of the origin, False otherwise.
    Tr#   r8   Fr9   )ÚabsÚcubic_farthest_fit_inside)r   r   r   r    rN   r6   r7   r   r   r   rP   ì   s    ÿrP   )rN   Ú_2_3)Úq1Úc0r2   Úc2Úc3çUUUUUUå?c                 C   sv   t | Ž }t |j¡rdS | d }| d }||| |  }||| |  }td|| d  || d  d|ƒsldS |||fS )aã  Approximate a cubic Bezier with a single quadratic within a given tolerance.

    Args:
        cubic (sequence): Four complex numbers representing control points of
            the cubic Bezier curve.
        tolerance (double): Permitted deviation from the original curve.

    Returns:
        Three complex numbers representing control points of the quadratic
        curve if it fits within the given tolerance, or ``None`` if no suitable
        curve could be calculated.
    Nr   r#   r   r"   )rM   ÚmathÚisnanÚimagrP   )ÚcubicrN   rQ   rR   rS   rU   r2   rT   r   r   r   Úcubic_approx_quadratic  s    

ýr[   )r*   rN   rQ   )r/   )rS   r2   rT   rU   )Úq0rR   Únext_q1Úq2r3   c                 C   s0  |dkrt | |ƒS t| d | d | d | d |ƒ}t|ƒ}tdg|¢R Ž }| d }d}| d |g}	td|d ƒD ]¬}
|\}}}}|}|}|
|k rÈt|ƒ}t|
|d  g|¢R Ž }|	 |¡ || d }n|}|}|| }t|ƒ|k�st|||| |  | ||| |  | ||ƒsp dS qp|	 | d ¡ |	S )a'  Approximate a cubic Bezier curve with a spline of n quadratics.

    Args:
        cubic (sequence): Four complex numbers representing control points of
            the cubic Bezier curve.
        n (int): Number of quadratic Bezier curves in the spline.
        tolerance (double): Permitted deviation from the original curve.

    Returns:
        A list of ``n+2`` complex numbers, representing control points of the
        quadratic spline if it fits within the given tolerance, or ``None`` if
        no suitable spline could be calculated.
    r   r   r"   r#   y                r9   N)r[   r+   ÚnextrE   r4   ÚappendrO   rP   )rZ   r*   rN   rQ   ZcubicsZ
next_cubicr]   r^   r3   Úspliner/   rS   r2   rT   rU   r\   rR   Zd0r   r   r   Úcubic_approx_spline1  s>    
 
üÿrb   )Úmax_err)r*   c                 C   sT   dd„ | D ƒ} t dtd ƒD ]*}t| ||ƒ}|durdd„ |D ƒ  S qt| ƒ‚dS )aÏ  Approximate a cubic Bezier curve with a spline of n quadratics.

    Args:
        cubic (sequence): Four 2D tuples representing control points of
            the cubic Bezier curve.
        max_err (double): Permitted deviation from the original curve.

    Returns:
        A list of 2D tuples, representing control points of the quadratic
        spline if it fits within the given tolerance, or ``None`` if no
        suitable spline could be calculated.
    c                 S   s   g | ]}t |Ž ‘qS r   ©rK   ©Ú.0rH   r   r   r   Ú
<listcomp>‚  ó    z&curve_to_quadratic.<locals>.<listcomp>r   Nc                 S   s   g | ]}|j |jf‘qS r   ©r   rY   ©rf   Úsr   r   r   rg   ˆ  rh   )r4   ÚMAX_Nrb   r   )Úcurverc   r*   ra   r   r   r   r   r  s    )ÚlÚlast_ir/   c                 C   s¬   dd„ | D ƒ} t |ƒt | ƒks"J ‚t | ƒ}dg| }d }}d}t| | ||| ƒ}|du rt|tkrfq |d7 }|}q@|||< |d | }||kr@dd„ |D ƒS q@t| ƒ‚dS )aÄ  Return quadratic Bezier splines approximating the input cubic Beziers.

    Args:
        curves: A sequence of *n* curves, each curve being a sequence of four
            2D tuples.
        max_errors: A sequence of *n* floats representing the maximum permissible
            deviation from each of the cubic Bezier curves.

    Example::

        >>> curves_to_quadratic( [
        ...   [ (50,50), (100,100), (150,100), (200,50) ],
        ...   [ (75,50), (120,100), (150,75),  (200,60) ]
        ... ], [1,1] )
        [[(50.0, 50.0), (75.0, 75.0), (125.0, 91.66666666666666), (175.0, 75.0), (200.0, 50.0)], [(75.0, 50.0), (97.5, 75.0), (135.41666666666666, 82.08333333333333), (175.0, 67.5), (200.0, 60.0)]]

    The returned splines have "implied oncurve points" suitable for use in
    TrueType ``glif`` outlines - i.e. in the first spline returned above,
    the first quadratic segment runs from (50,50) to
    ( (75 + 125)/2 , (120 + 91.666..)/2 ) = (100, 83.333...).

    Returns:
        A list of splines, each spline being a list of 2D tuples.

    Raises:
        fontTools.cu2qu.Errors.ApproxNotFoundError: if no suitable approximation
        can be found for all curves with the given parameters.
    c                 S   s   g | ]}d d„ |D ƒ‘qS )c                 S   s   g | ]}t |Ž ‘qS r   rd   re   r   r   r   rg   ­  rh   ú2curves_to_quadratic.<locals>.<listcomp>.<listcomp>r   ©rf   rm   r   r   r   rg   ­  rh   z'curves_to_quadratic.<locals>.<listcomp>Nr   r   c                 S   s   g | ]}d d„ |D ƒ‘qS )c                 S   s   g | ]}|j |jf‘qS r   ri   rj   r   r   r   rg   À  rh   rp   r   )rf   ra   r   r   r   rg   À  rh   )Úlenrb   rl   r   )ZcurvesZ
max_errorsrn   Zsplinesro   r/   r*   ra   r   r   r   r   Ž  s$    
Ú__main__é   c                   C   s   dd„ t dƒD ƒS )Nc                 S   s"   g | ]}t d d„ tdƒD ƒƒ‘qS )c                 s   s   | ]}t t d d¡ƒV  qdS )r   i   N)ÚfloatÚrandomÚrandint)rf   Zcoordr   r   r   Ú	<genexpr>Í  rh   z,generate_curve.<locals>.<listcomp>.<genexpr>r"   )Útupler4   )rf   Úpointr   r   r   rg   Ì  s   ÿz"generate_curve.<locals>.<listcomp>r$   )r4   r   r   r   r   Úgenerate_curveË  s    þr{   c                   C   s
   t ƒ tfS ©N)r{   ÚMAX_ERRr   r   r   r   Úsetup_curve_to_quadraticÐ  s    r~   c                  C   s    d} dd„ t | ƒD ƒtg|  fS )Nr#   c                 S   s   g | ]
}t ƒ ‘qS r   )r{   rq   r   r   r   rg   Ö  rh   z-setup_curves_to_quadratic.<locals>.<listcomp>)r4   r}   )Z
num_curvesr   r   r   Úsetup_curves_to_quadraticÓ  s    þr   Ú éè  c           	      C   sx   d| }|r.t d||f dd� |d| 7 }nt d| dd� dd„ }tj|||ƒ||d	�}t d
t|ƒd |  ƒ d S )NZsetup_z%s with %s:r€   )ÚendÚ_z%s:c                    s&   t ƒ ˆ  ‰ t ƒ ˆ ‰‡ ‡fdd„}|S )Nc                      s
   ˆ ˆƒ Ž S r|   r   r   ©ÚfunctionÚ
setup_funcr   r   Úwrappedå  s    z/run_benchmark.<locals>.wrapper.<locals>.wrapped)Úglobals)r…   r†   r‡   r   r„   r   Úwrapperâ  s    

zrun_benchmark.<locals>.wrapper)ÚrepeatÚnumberz	%5.1fusg    €„.A)ÚprintÚtimeitrŠ   Úmin)	Zbenchmark_moduleÚmoduler…   Zsetup_suffixrŠ   r‹   r†   r‰   Úresultsr   r   r   Úrun_benchmarkÙ  s    r‘   c                   C   s   t dddƒ t dddƒ d S )Nzcu2qu.benchmarkZcu2qur   r   )r‘   r   r   r   r   Úmainë  s    r’   )r?   )rV   )rV   )r€   rt   r�   )-r   ÚImportErrorZfontTools.miscrW   Úerrorsr   Z
Cu2QuErrorr   Ú__all__rl   ru   rL   ÚcompiledZCOMPILEDZcfuncÚinlineÚreturnsÚdoubleÚlocalsrK   r   r   r!   r+   Úintr)   r'   r(   rE   rM   rP   r[   rb   r   r   Ú__name__rv   r�   r}   r{   r~   r   r‘   r’   Úseedr   r   r   r   Ú<module>   s    



<
6
 ÿ

