
    <`A                        	 d dl Z n# e$ r	 d dlm Z  Y nw xY wd dlZddlmZmZ ddgZdZ	 e
d          Ze j        rd	Znd
Ze j        e j         e j        e j                   e j        e j        e j                  d                                                 Ze j        e j         e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                  d                                                 Ze j        e j         e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                  d                                                 Ze j         e j        e j        e j        e j        e j                  d                         Z e j        e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                  d                                                 Z e j        e j        e j        e j        e j                   e j        e j        e j                  d                         Z e j        e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                  d;d                        Z e j        e j                   e j        e j        e j        e j        e j        e j                   e j        e j        e j                  d                                     Z e j        e j                   e j        e j        e j        e j        e j                   e j        e j        e j        e j        e j                   d!                                     Ze j         e j        e j                   e j        e j        e j        e j        e j        e j        "           e j        e j        e j                  d#                                                 Ze j         e j        e j        e j        $           e j        e j        e j        e j        e j        e j        %          d<d'                                    Ze j         e j        e j        e j        e j        (           e j        e j        )           e j        e j        e j        e j        e j        *           e j        e j        e j        e j        e j        e j        +          d<d,                                                            Z  e j        e j        -           e j        e j        .          d/                         Z! e j        e j        e j        e j        0          d1             Z"e#d2k    r7d dl$Z$d dl%Z%d3Z&d4 Z'd5 Z(d6 Z)	 d=d9Z*d: Z+ e$j,        d            e+             dS dS )>    N)cython   )ErrorApproxNotFoundErrorcurve_to_quadraticcurves_to_quadraticd   NaNTFv1v2c                 :    | |                                 z  j        S )zReturn the dot product of two vectors.

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

    Returns:
        double: Dot product.
    )	conjugaterealr   s     5lib/python3.11/site-packages/fontTools/cu2qu/cu2qu.pydotr   ,   s     %%    )abcd)_1_2_3_4c                 N    |}|dz  |z   }||z   dz  |z   }| |z   |z   |z   }||||fS N      @ )r   r   r   r   r   r   r   r   s           r   calc_cubic_pointsr    =   sG    
 
B
c'QB
a%3	B	
QQBr2r>r   )p0p1p2p3c                 N    || z
  dz  }||z
  dz  |z
  }| }||z
  |z
  |z
  }||||fS r   r   )r!   r"   r#   r$   r   r   r   r   s           r   calc_cubic_parametersr&   I   sG    
 
bCA	bC!A
A
Q
QAaA:r   c                    |dk    rt          t          | |||                    S |dk    rt          t          | |||                    S |dk    r5t          | |||          \  }}t          t          | t          | z             S |dk    r5t          | |||          \  }}t          t          | t          | z             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   s          r   split_cubic_into_n_iterr1   U   s    & 	Avv(RR88999Avv*2r2r::;;;Avv#BB331(!,/CQ/GGHHHAvv#BB331*A.1G1KKLLL"2bBq111r   )r!   r"   r#   r$   r0   )dtdelta_2delta_3i)a1b1c1d1c              #   8  K   t          | |||          \  }}}}d|z  }	|	|	z  }
|	|
z  }t          |          D ]a}||	z  }||z  }||z  }d|z  |z  |z   |
z  }d|z  |z  |z   d|z  |z  z   |	z  }||z  |z  ||z  z   ||z  z   |z   }t          ||||          V  bd S )Nr   r)   r(   )r&   ranger    )r!   r"   r#   r$   r0   r   r   r   r   r2   r3   r4   r5   t1t1_2r6   r7   r8   r9   s                      r   r/   r/   v   s      
 'r2r266JAq!Q	
QB2gG7lG1XX 0 0VBw[c"fqjG#c"fqj1Q3t8#r)rT$Y4!B$&*BB//////0 0r   )midderiv3c                 |    | d||z   z  z   |z   dz  }||z   |z
  | z
  dz  }| | |z   dz  ||z
  |f|||z   ||z   dz  |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?   s         r   r-   r-      sq    " R"W"d
*C2glR4'F"r'Rvs3#,bB35 5r   )r!   r"   r#   r$   _27)mid1deriv1mid2deriv2h/?c                     d| z  d|z  z   d|z  z   |z   |z  }|d|z  z   d| z  z
  |z  }| d|z  z   d|z  z   d|z  z   |z  }d|z  d|z  z
  | z
  |z  }| d| z  |z   dz  ||z
  |f|||z   ||z
  |f|||z   |d|z  z   dz  |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$   rC   rD   rE   rF   rG   s	            r   r.   r.      s    & bD2b5L1R4"$+D1R4i!B$#%F2I2"$+DdQrTkB#%F!B$)s"D6M484&=$-64&=2"9"3R8: :r   )tr!   r"   r#   r$   )_p1_p2c                 D    |||z
  dz  z   }|||z
  dz  z   }|||z
  | z  z   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   )rL   r!   r"   r#   r$   rM   rN   s          r   cubic_approx_controlrP      s;      R3
C
R3
C#)q  r   )abcdphc                     || z
  }||z
  }|dz  }	 t          || |z
            t          ||          z  }n*# t          $ r t          t          t                    cY S w xY w|||z  z   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   rQ   rR   rS   rT   s           r   calc_intersectrY      s      
QB	
QB
RA!1q5MMC2JJ& ! ! !sC     !rAv:s   $6 $AA)	tolerancer!   r"   r#   r$   c                 *   t          |          |k    rt          |          |k    rdS | d||z   z  z   |z   dz  }t          |          |k    rdS ||z   |z
  | z
  dz  }t          | | |z   dz  ||z
  ||          ot          |||z   ||z   dz  ||          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)   rA   FrB   )abscubic_farthest_fit_inside)r!   r"   r#   r$   rZ   r>   r?   s          r   r]   r]      s    . 2ww)B9 4 4t R"W"d
*C
3xx)u2glR4'F%b2b5"*c&j#yQQ R%c3v:2rz2yQQSr   )rZ   _2_3)q1c0r8   c2c3UUUUUU?c                     t          |  }t          j        |j                  rdS | d         }| d         }|||z
  |z  z   }|||z
  |z  z   }t	          d|| d         z
  || d         z
  d|          sd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(   )rY   mathisnanimagr]   )cubicrZ   r^   r_   r`   rb   r8   ra   s           r   cubic_approx_quadraticri     s    & 
	Bz"' t	qB	qB	rBw$	B	rBw$	B$Q%'%(]%'%(]%&	3 3  tr2:r   )r0   rZ   r^   )r5   )r`   r8   ra   rb   )q0r_   next_q1q2r9   c                    |dk    rt          | |          S t          | d         | d         | d         | d         |          }t          |          }t          dg|R  }| d         }d}| d         |g}	t	          d|dz             D ]}
|\  }}}}|}|}|
|k     r@t          |          }t          |
|dz
  z  g|R  }|	                    |           ||z   dz  }n|}|}||z
  }t          |          |k    s+t          ||||z
  |z  z   |z
  |||z
  |z  z   |z
  ||          s dS |	                    | 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                rB   N)ri   r1   nextrP   r;   appendr\   r]   )rh   r0   rZ   r^   cubics
next_cubicrk   rl   r9   spliner5   r`   r8   ra   rb   rj   r_   d0s                     r   cubic_approx_splinert   1  s   , 	Avv%eY777$U1XuQxq58QOOF fJ"12z222G	qB	BAh F1ac]]   $BB q55fJ*1!9BzBBBGMM'"""w,"$BBB "WGGi)"*,R4/?*?"*D*,R4/?*?"*D*,*3	5 5   44   MM%(Mr   )max_err)r0   c                     d | D             } t          dt          dz             D ]#}t          | ||          }|d |D             c S $t          |           )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                      g | ]}t          | S r   rW   .0rS   s     r   
<listcomp>z&curve_to_quadratic.<locals>.<listcomp>  s    (((QWa[(((r   r   Nc                 *    g | ]}|j         |j        fS r   r   rg   rz   ss     r   r{   z&curve_to_quadratic.<locals>.<listcomp>  s!    555QVQV$555r   )r;   MAX_Nrt   r   )curveru   r0   rr   s       r   r   r   r  sz      )(%(((E1eai   6 6$UAw7755f555555  e
$
$$r   )llast_ir5   c                 Z   d | D             } t          |          t          |           k    sJ t          |           }dg|z  }dx}}d}	 t          | |         |||                   }||t          k    rn(|dz  }|}4|||<   |dz   |z  }||k    rd |D             S St          |           )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                 &    g | ]}d  |D             S )c                      g | ]}t          | S r   rx   ry   s     r   r{   z2curves_to_quadratic.<locals>.<listcomp>.<listcomp>  s    ***qw{***r   r   rz   r   s     r   r{   z'curves_to_quadratic.<locals>.<listcomp>  s'    ???u**E***???r   Nr   r   Tc                 &    g | ]}d  |D             S )c                 *    g | ]}|j         |j        fS r   r}   r~   s     r   r{   z2curves_to_quadratic.<locals>.<listcomp>.<listcomp>  s!    666!afaf%666r   r   )rz   rr   s     r   r{   z'curves_to_quadratic.<locals>.<listcomp>  s'    MMM666v666MMMr   )lenrt   r   r   )curves
max_errorsr   splinesr   r5   r0   rr   s           r   r   r     s    > @????Fz??c&kk))))FAfqjGNFQ	AN$VAY:a=AA>EzzFAF
UaK;;MMWMMMMN f
%
%%r   __main__   c                  4    d t          d          D             S )Nc                 Z    g | ](}t          d  t          d          D                       )S )c              3   Z   K   | ]&}t          t          j        d d                    V  'dS )r   i   N)floatrandomrandint)rz   coords     r   	<genexpr>z,generate_curve.<locals>.<listcomp>.<genexpr>  s6      GGU%q$//00GGGGGGr   r(   )tupler;   )rz   points     r   r{   z"generate_curve.<locals>.<listcomp>  sE     # # # GGeAhhGGGGG# # #r   r*   )r;   r   r   r   generate_curver     s)    # #q# # # 	#r   c                  ,    t                      t          fS N)r   MAX_ERRr   r   r   setup_curve_to_quadraticr     s    ((r   c                  N    d} d t          |           D             t          g| z  fS )Nr)   c                 *    g | ]}t                      S r   )r   r   s     r   r{   z-setup_curves_to_quadratic.<locals>.<listcomp>  s    ===%^===r   )r;   r   )
num_curvess    r   setup_curves_to_quadraticr     s4    
==5+<+<===I
"$ 	$r      c                    d|z   }|r t          |d|dd           |d|z   z  }nt          d|z  d           d }t          j         |||          ||	          }t          d
t          |          dz  |z  z             d S )Nsetup_z with :r   )end_z%s:c                 f     t                                 t                                fd}|S )Nc                                      S r   r   )function
setup_funcs   r   wrappedz/run_benchmark.<locals>.wrapper.<locals>.wrapped  s    x..r   )globals)r   r   r   s   `` r   wrapperzrun_benchmark.<locals>.wrapper  sC    yy*H :.J/ / / / / /Nr   )repeatnumberz	%5.1fusg    .A)printtimeitr   min)	benchmark_modulemoduler   setup_suffixr   r   r   r   resultss	            r   run_benchmarkr     s    (
 	,888\\\:CCCC#,,JJ%("++++	 	 	 -* = =fU[\\\kS\\H4v=>?????r   c                  J    t          ddd           t          ddd           d S )Nzcu2qu.benchmarkcu2qur   r   )r   r   r   r   mainr     s0    '2FGGG'2GHHHHHr   )rH   )rc   )r   r   r   )-r   ImportErrorfontTools.miscre   errorsr   
Cu2QuErrorr   __all__r   r   rX   compiledCOMPILEDcfuncinlinereturnsdoublelocalsrW   r   r    r&   r1   intr/   r-   r.   rP   rY   r]   ri   rt   r   r   __name__r   r   r   r   r   r   r   r   seedr   r   r   <module>r      s  $&MMMM & & &%%%%%%%%&  < < < < < < < <  !6
7eEll 
? HH H &.V^444
& 
& 54   
& 6>V^v~VVV&.V^6>ZZZ  [Z WV   &.V^6>ZZZ6>V^v~VVV  WV [Z   &.V^6>ZZZ2 2 [Z 2> &.V^6>]c]ghhh6>V^v~VVV&-QWQ[\\\&.V^6>ZZZ0 0 [Z ]\ WV ih0  &.V^6>ZZZ6>&.9995 5 :9 [Z5* &.V^6>_e_lmmmFN6>W]Wefff: : : gf nm:4 6>fn\b\jkkk6>v~666! ! 76 lk  !$ 6>V^v~VVV&.V^v~WWW  XW WV  . 
6>fnQWQ_djdrsss6>&.999S S :9 ts  S> V];;;&.V^6>^d^lmmm   nm <; > v}6=III&.V^6>ZZZ&.V^V^PVP^cicqrrr9 9 9 sr [Z  JI 
9x v}%%%% %  &%%4 FJ&*===3& 3& >=3&l zMMMMMMG# # #
) ) )$ $ $ SW@ @ @ @$I I I FKNNNDFFFFFW s    