
    WhBF                        	 d dl Z 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 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$                        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(                        d                      Z e j&                  e j(                  e j(                  e j(                  e j(                  e j4                         e j&                  e j(                  e j(                  e j(                  e j(                         e j&                  e j$                  e j$                  e j$                  e j4                         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                             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(                  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$                        d                                     Ze j                   e j"                  e j4                         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%                             Z!e j                   e j&                  e j4                  e j$                  &       e j&                  e j4                  '       e j&                  e j4                  (       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 j4                  -       e j&                  e j4                  (      d1d.                     Z# e j&                  e j4                  e j4                  e j4                  /       e j&                  e j4                  (      d1d0              Z$y# eef$ r
 d dlm Z  Y 	uw xY w)2    N)cython   )ErrorApproxNotFoundErrorcurve_to_quadraticcurves_to_quadraticd   NaNv1v2resultc                 `    | |j                         z  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   s      O/var/www/html/jupyter_env/lib/python3.12/site-packages/fontTools/cu2qu/cu2qu.pydotr   %   s2     2<<>!''F 6{UM    )zden)zrzic                 V    | j                   }| j                  }t        ||z  ||z        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   s       r   _complex_div_by_realr   ?   s+     
B	
B28R#X&&r   )abcd)_1_2_3_4c                 j    |}t        |d      |z   }t        ||z   d      |z   }| |z   |z   |z   }||||fS N      @)r   )r   r   r    r!   r"   r#   r$   r%   s           r   calc_cubic_pointsr)   P   sN     
B	a	%	)B	a!eS	)B	.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   s           r   calc_cubic_parametersr0   ^   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(  rOt        | |||      \  }}t        t        |d   |d   |d   |d         t        |d   |d   |d   |d         z         S |dk(  rOt        | |||      \  }}t        t        |d   |d   |d   |d         t        |d   |d   |d   |d         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.
             r   r      )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_iterr;   l   s)   , 	Av(RR899Av*2r2r:;;Av#BB31 1qtQqT1Q48"1Q41qtQqT:;
 	
 	Av#BB31"1Q41qtQqT:$QqT1Q41qt<=
 	

 #2r2r155r   )r*   r+   r,   r-   r:   )dtdelta_2delta_3i)a1b1c1d1c              #   &  K   t        | |||      \  }}}}d|z  }	|	|	z  }
|	|
z  }t        |      D ]]  }||	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        ||||       _ y w)Nr   r3   r2   )r0   ranger)   )r*   r+   r,   r-   r:   r   r   r    r!   r<   r=   r>   r?   t1t1_2r@   rA   rB   rC   s                      r   r9   r9      s      'r2r26JAq!Q	
QB2gG7lG1X 0VBw[!ebj1n'!ebj1nq1ut|+r1Vd]QX%B.2BB//0s   BB)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).
    r3         ?      ?r/   )r*   r+   r,   r-   rH   rI   s         r   r7   r7      sq    * R"W"e
+C2glR5(F	b2g_cFlC0	cFlR"WOR0 r   )mid1deriv1mid2deriv2c                     d| z  d|z  z   d|z  z   |z   dz  }|d|z  z   d| z  z
  dz  }| d|z  z   d|z  z   d|z  z   dz  }d|z  d|z  z
  | z
  d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).
          r5   gh/?r3   r4   r2   r(   r/   )r*   r+   r,   r-   rM   rN   rO   rP   s           r   r8   r8      s    : FR"Wq2v%*v6D1r6kAF"v.FRK"r'!AF*v6D"fq2vo"v.F	a"frkS $-6	tf}dVmT2	tf}rAF{c126 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/   )rT   r*   r+   r,   r-   rU   rV   s          r   cubic_approx_controlrX      s;    0 R3
C
R3
C#)q  r   )abcdphc                     || z
  }||z
  }|dz  }	 t        || |z
        t        ||      z  }|||z  z   S # t        $ r* ||k(  r| |k(  s||k(  r|cY S t        t        t              cY S w xY w)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!   rY   rZ   r[   r\   s           r   calc_intersectr`     s    $ 
QB	
QB
RA	!1q5MC2J& rAv:  !
 6qAvaHsC  !s   5 A(A('A()	tolerancer*   r+   r,   r-   c                     t        |      |k  rt        |      |k  ry| d||z   z  z   |z   dz  }t        |      |kD  ry||z   |z
  | z
  dz  }t        | | |z   dz  ||z
  ||      xr 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.
    Tr3   rK   FrL   )r   cubic_farthest_fit_inside)r*   r+   r,   r-   ra   rH   rI   s          r   rc   rc   8  s    : 2w)B9 4 R"W"e
+C
3x)2glR5(F$
R"WOS6\3	 W
#CvR3I
VWr   )ra   )q1c0rB   c2c3c                     t        | d   | d   | d   | d         }t        j                  |j                        ry| d   }| d   }|||z
  dz  z   }|||z
  dz  z   }t	        d|| d   z
  || d   z
  d|      sy|||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   r2   r3   NUUUUUU?)r`   mathisnanr   rc   )cubicra   rd   re   rg   rB   rf   s          r   cubic_approx_quadraticrm   b  s    0 
a%(E!HeAh	?Bzz"''	qB	qB	rBw5!	!B	rBw5!	!B$QU1XrE!H}aSr2:r   )r:   ra   )r?   )all_quadratic)re   rB   rf   rg   )q0rd   next_q1q2rC   c           	      Z   |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z         D ]  }
|\  }}}}|}|}|
|k  rFt        |      }t        |
|dz
  z  |d   |d   |d   |d         }|	j                  |       ||z   dz  }n|}|}||z
  }t        |      |kD  s(t        ||||z
  dz  z   |z
  |||z
  dz  z   |z
  ||      r y	 |	j                  | 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   r2   Fr   r3   y                rL   ri   N)rm   r;   nextrX   rE   appendr   rc   )rl   r:   ra   rn   cubics
next_cubicrp   rq   rC   spliner?   re   rB   rf   rg   ro   rd   d0s                     r   cubic_approx_splinery     s   : 	Av%eY77Av-5($U1XuQxq58QOF fJ"	:a=*Q-A
1G 
qB	BAh F1a!e_ #BB q5fJ*QUZ]JqM:a=*UV-G MM'"w,#%BB "Wr7Y&?"r'e$$r)"r'e$$r)'
 9: MM%(Mr   )max_err)r:   c                     | D cg c]
  }t        |  } }t        dt        dz         D ]:  }t        | |||      }||D cg c]  }|j                  |j
                  f c}c S  t        |       c c}w c c}w )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).
    r   )r   rE   MAX_Nry   r   r   r   )curverz   rn   r[   r:   rw   ss          r   r   r     s    0 #((QWa[(E(1eai  6$UAwF.45QVVQVV$55	6 e
$$ ) 6s   A1A6)llast_ir?   c           
         | D cg c]  }|D cg c]
  }t        |  c} } }}t        |      t        |       k(  sJ t        |       }dg|z  }dx}}d}		 t        | |   |	||   |      }
|
|	t        k(  r	 t        |       |	dz  }	|}4|
||<   |dz   |z  }||k(  r6|D 
cg c](  }
|
D cg c]  }|j                  |j
                  f c}* c}}
S |c c}w c c}}w c c}w c c}}
w )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.
    Nr   r   )r   lenry   r|   r   r   r   )curves
max_errorsrn   r}   r[   r   splinesr   r?   r:   rw   r~   s               r   r   r     s   N 9??uE*qw{*?F?z?c&k)))FAfqjGNFQ	A
$VAY:a=-P>Ez f
%% FAF
UaK;ELM6v6!affaff%6MM  +?& 7Ms-   	CCC!	C#*C	C#CC#)T)%r   AttributeErrorImportErrorfontTools.misccompiledCOMPILEDrj   errorsr   
Cu2QuErrorr   __all__r|   floatr_   cfuncinlinereturnsdoublelocalsr   r   r   r)   r0   r;   intr9   r7   r8   rX   r`   rc   rm   ry   r   r   r/   r   r   <module>r      s  $& ??  <  !6
7El &..V^^FMMJ K   , V]]3&--FMM2' 3 4 ' 6>>V^^v~~V~~&..V^^ W   ~~&..V^^ 6>>V^^v~~V W   ~~&..V^^"6  
"6J ~~~~~~~~jj 6>>V^^v~~V}}fmmV]]fjj ~~&..V^^0 W0  ~~&..V^^ 6>>&..9 :  . ~~~~~~~~	 	>>	>>	  4 mm~~~~~~~~ 6>>v~~6! 7    !$ 6>>V^^v~~V&..V^^v~~W X W    
: 

mm~~~~~~~~ 6>>&..9W :  W@ '~~~~~~~~~~ (  4 v}}5VZZ(~~&..V^^ ~~~~NN~~~~= )  6 =@ v}}%VZZ(% )  &%@ FJJ&**=VZZ(:& ) >:&K 	$ &%%&s   e+ +e<;e<