o
    iBF                     @   s  zd dl Z W n eefy   d dlm Z  Y nw e jZd dlZddlmZ	m
Z
 ddgZdZedZe je je e je j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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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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e jde je je je je jd!d"d# Ze j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 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/d0d1 Z!e je je je jd2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d7d8 Z"e je jd9e je jd:e je jd4d?d<dZ#e je je je jd=e je jd4d?d>dZ$dS )@    N)cython   )ErrorApproxNotFoundErrorcurve_to_quadraticcurves_to_quadraticd   NaNZv1Zv2resultc                 C   s"   | |   j}t|dk rd}|S )zReturn the dot product of two vectors.

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

    Returns:
        double: Dot product.
    gV瞯<g        )	conjugaterealabsr
    r   d/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/fontTools/cu2qu/cu2qu.pydot%   s   r   )zden)zrzic                 C   s   | j }| j}t|| || S )a`  Divide complex by real using Python's method (two separate divisions).

    This ensures bit-exact compatibility with Python's complex division,
    avoiding C's multiply-by-reciprocal optimization that can cause 1 ULP differences
    on some platforms/compilers (e.g. clang on macOS arm64).

    https://github.com/fonttools/fonttools/issues/3928
    )r   imagcomplex)r   r   r   r   r   r   r   _complex_div_by_real?   s   r   )abcd)_1_2_3_4c                 C   s@   |}t |d| }t || d| }| | | | }||||fS N      @)r   )r   r   r   r   r   r   r   r    r   r   r   calc_cubic_pointsP   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_parameters^   s
   r(   c                 C   s   |dkrt t| |||S |dkrt t| |||S |dkrGt| |||\}}t t|d |d |d |d t|d |d |d |d  S |dkrtt| |||\}}t t|d |d |d |d t|d |d |d |d  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.
             r   r      )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_iterl   s&   r2   )r$   r%   r&   r'   r1   )dtdelta_2delta_3i)a1b1c1d1c                 c   s    t | |||\}}}}d| }	|	|	 }
|	|
 }t|D ]@}||	 }|| }|| }d| | | |
 }d| | | d| |  |	 }|| | ||  ||  | }t||||V  qd S )Nr   r*   r)   )r(   ranger#   )r$   r%   r&   r'   r1   r   r   r   r   r3   r4   r5   r6   t1Zt1_2r7   r8   r9   r:   r   r   r   r0      s     r0   )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'   r=   r>   r   r   r   r.      s
   r.   )mid1deriv1mid2deriv2c                 C   s   d|  d|  d|  | d }|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,   gh/?r*   r+   r)   r"   r   )r$   r%   r&   r'   rA   rB   rC   rD   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   )rG   r$   r%   r&   r'   rH   rI   r   r   r   cubic_approx_control   s   rJ   )abcdphc                 C   s~   ||  }|| }|d }zt || | t || }W n ty8   ||kr1| |ks-||kr1| Y S ttt Y S w |||  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   ZeroDivisionErrorr   NAN)r   r   r   r   rK   rL   rM   rN   r   r   r   calc_intersect  s   rQ   )	tolerancer$   r%   r&   r'   c                 C   s   t ||krt ||krdS | d||   | d }t ||kr"dS || | |  d }t| | | d || ||oGt||| || 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*   r?   Fr@   )r   cubic_farthest_fit_inside)r$   r%   r&   r'   rR   r=   r>   r   r   r   rS   8  s   rS   )rR   )q1c0r9   c2c3c                 C   s   t | d | d | d | d }t|jrdS | d }| d }||| d  }||| d  }td|| d  || d  d|sAd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.
    r   r   r)   r*   NUUUUUU?)rQ   mathisnanr   rS   )cubicrR   rT   rU   rW   r9   rV   r   r   r   cubic_approx_quadraticb  s    
r\   )r1   rR   )r6   )all_quadratic)rU   r9   rV   rW   )q0rT   next_q1q2r:   c                 C   sb  |dkr	t | |S |dkr|dkr| S t| d | d | d | d |}t|}td|d |d |d |d }| d }d}| d |g}	td|d D ]]}
|\}}}}|}|}|
|k r~t|}t|
|d  |d |d |d |d }|	| || d }n|}|}|| }t||kst|||| d  | ||| d  | ||s d	S qJ|	| 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)   Fr   r*   y                r@   rX   N)r\   r2   nextrJ   r;   appendr   rS   )r[   r1   rR   r]   ZcubicsZ
next_cubicr_   r`   r:   spliner6   rU   r9   rV   rW   r^   rT   Zd0r   r   r   cubic_approx_spline  sJ   
 "
rd   )max_err)r1   Tc                 C   sR   dd | D } t dtd D ]}t| |||}|dur$dd |D   S qt| )a5  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.
        all_quadratic (bool): If True (default) returned value is a
            quadratic spline. If False, it's either a single quadratic
            curve or a single cubic curve.

    Returns:
        If all_quadratic is True: 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.

        If all_quadratic is False: Either a quadratic curve (if length
        of output is 3), or a cubic curve (if length of output is 4).
    c                 S      g | ]}t | qS r   r   .0rM   r   r   r   
<listcomp>      z&curve_to_quadratic.<locals>.<listcomp>r   Nc                 S      g | ]}|j |jfqS r   r   r   ri   sr   r   r   rj         )r;   MAX_Nrd   r   )curvere   r]   r1   rc   r   r   r   r     s   )llast_ir6   c           	      C   s   dd | D } t |t | ksJ t | }dg| }d }}d}	 t| | ||| |}|du r@|tkr9	 t| |d7 }|}q |||< |d | }||krUdd |D S q!)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.
        all_quadratic (bool): If True (default) returned values are a
            quadratic spline. If False, they are either a single quadratic
            curve or a single cubic curve.

    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:
        If all_quadratic is True, a list of splines, each spline being a list
        of 2D tuples.

        If all_quadratic is False, a list of curves, each curve being a quadratic
        (length 3), or cubic (length 4).

    Raises:
        fontTools.cu2qu.Errors.ApproxNotFoundError: if no suitable approximation
        can be found for all curves with the given parameters.
    c                 S      g | ]	}d d |D qS )c                 S   rf   r   rg   rh   r   r   r   rj     rk   2curves_to_quadratic.<locals>.<listcomp>.<listcomp>r   )ri   rr   r   r   r   rj         z'curves_to_quadratic.<locals>.<listcomp>Nr   r   Tc                 S   ru   )c                 S   rl   r   rm   rn   r   r   r   rj   1  rp   rv   r   )ri   rc   r   r   r   rj   1  rw   )lenrd   rq   r   )	ZcurvesZ
max_errorsr]   rs   Zsplinesrt   r6   r1   rc   r   r   r   r     s(   '

)T)%r   AttributeErrorImportErrorZfontTools.misccompiledZCOMPILEDrY   errorsr   Z
Cu2QuErrorr   __all__rq   floatrP   Zcfuncinlinereturnsdoublelocalsr   r   r   r#   r(   r2   intr0   r.   r/   rJ   rQ   rS   r\   rd   r   r   r   r   r   r   <module>   s  
%


 @ 