
    <`l                        d Z ddlmZmZmZ ddlmZ ddlZddlm	Z	  e	dg d          Z
g dZd;d
Zd Zd Zd;dZdZdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"dd!lm#Z#m$Z$m%Z%m&Z& e#fd"Z'd# Z(d$ Z)d% Z*d& Z+d' Z,d( Z-d) Z.d* Z/d+ Z0d, Z1d- Z2d. Z3d/ Z4d0 Z5d1 Z6d2 Z7d3 Z8	 d<d5Z9d6 Z:d7 Z;d8 Z<d9 Z=e>d:k    r,ddl?Z?ddl@Z@ e?jA         e@jB                    jC                   dS dS )=zNfontTools.misc.bezierTools.py -- tools for working with Bezier path segments.
    )
calcBoundssectRectrectArea)IdentityN)
namedtupleIntersectionptt1t2)approximateCubicArcLengthapproximateCubicArcLengthCapproximateQuadraticArcLengthapproximateQuadraticArcLengthCcalcCubicArcLengthcalcCubicArcLengthCcalcQuadraticArcLengthcalcQuadraticArcLengthCcalcCubicBoundscalcQuadraticBounds	splitLinesplitQuadratic
splitCubicsplitQuadraticAtTsplitCubicAtTsolveQuadratic
solveCubicquadraticPointAtTcubicPointAtTlinePointAtTsegmentPointAtTlineLineIntersectionscurveLineIntersectionscurveCurveIntersectionssegmentSegmentIntersections{Gzt?c                 `    t          t          |  t          | t          | t          | |          S )a  Calculates the arc length for a cubic Bezier segment.

    Whereas :func:`approximateCubicArcLength` approximates the length, this
    function calculates it by "measuring", recursively dividing the curve
    until the divided segments are shorter than ``tolerance``.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.
        tolerance: Controls the precision of the calcuation.

    Returns:
        Arc length value.
    )r   complex)pt1pt2pt3pt4	tolerances        :lib/python3.11/site-packages/fontTools/misc/bezierTools.pyr   r   *   s1     w}gsmWc]I      c                 |    | d||z   z  z   |z   dz  }||z   |z
  | z
  dz  }| | |z   dz  ||z
  |f|||z   ||z   dz  |ffS )N   g      ?      ? )p0p1p2p3midderiv3s         r.   _split_cubic_into_twor:   =   so    R"W"e
+C2glR5(F	b2g_cFlC0	cFlR"WOR0 r/   c                    t          ||z
            }t          ||z
            t          ||z
            z   t          ||z
            z   }|| z  |k    r||z   dz  S t          ||||          \  }}t          | g|R  t          | g|R  z   S Nr2   )absr:   _calcCubicArcLengthCRecurse)	multr4   r5   r6   r7   archboxonetwos	            r.   r>   r>   F   s    rBw<<D
b2g,,R"W
%BG
4Cd{cs
c!!(RR88S*46#6669T:
:
 :
 :
 
 	
r/   c                 8    dd|z  z   }t          || |||          S )zCalculates the arc length for a cubic Bezier segment.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers.
        tolerance: Controls the precision of the calcuation.

    Returns:
        Arc length value.
          ?g      ?)r>   )r)   r*   r+   r,   r-   r?   s         r.   r   r   R   s(     y D&tS#sC@@@r/      g|=c                 :    | |                                 z  j        S N)	conjugatereal)v1v2s     r.   _dotrM   d   s    %%r/   c                 r    | t          j        | dz  dz             z  dz  t          j        |           dz  z   S )N      )mathsqrtasinh)xs    r.   _intSecAtanrU   h   s8     tya!$$$q(4:a==1+<<<r/   c                 N    t          t          |  t          | t          |           S )a  Calculates the arc length for a quadratic Bezier segment.

    Args:
        pt1: Start point of the Bezier as 2D tuple.
        pt2: Handle point of the Bezier as 2D tuple.
        pt3: End point of the Bezier as 2D tuple.

    Returns:
        Arc length value.

    Example::

        >>> calcQuadraticArcLength((0, 0), (0, 0), (0, 0)) # empty segment
        0.0
        >>> calcQuadraticArcLength((0, 0), (50, 0), (80, 0)) # collinear points
        80.0
        >>> calcQuadraticArcLength((0, 0), (0, 50), (0, 80)) # collinear points vertical
        80.0
        >>> calcQuadraticArcLength((0, 0), (50, 20), (100, 40)) # collinear points
        107.70329614269008
        >>> calcQuadraticArcLength((0, 0), (0, 100), (100, 0))
        154.02976155645263
        >>> calcQuadraticArcLength((0, 0), (0, 50), (100, 0))
        120.21581243984076
        >>> calcQuadraticArcLength((0, 0), (50, -10), (80, 50))
        102.53273816445825
        >>> calcQuadraticArcLength((0, 0), (40, 0), (-40, 0)) # collinear points, control point outside
        66.66666666666667
        >>> calcQuadraticArcLength((0, 0), (40, 0), (0, 0)) # collinear points, looping back
        40.0
    )r   r(   r)   r*   r+   s      r.   r   r   n   s#    @ #7C='3-#OOOr/   c                 8   || z
  }||z
  }||z
  }|dz  }t          |          }|dk    rt          || z
            S t          ||          }t          |          t          k     rUt          ||          dk    rt          || z
            S t          |          t          |          }
}	|	|	z  |
|
z  z   |	|
z   z  S t          ||          |z  }t          ||          |z  }t          dt          |          t          |          z
  z  |z  |||z
  z  z            }|S )a$  Calculates the arc length for a quadratic Bezier segment.

    Args:
        pt1: Start point of the Bezier as a complex number.
        pt2: Handle point of the Bezier as a complex number.
        pt3: End point of the Bezier as a complex number.

    Returns:
        Arc length value.
    y              ?        r   rO   )r=   rM   epsilonrU   )r)   r*   r+   d0d1dnscaleorigDistabx0x1Lens                 r.   r   r      s    
sB	sB
RA	BAFFE||39~~Ar{{H
8}}wB<<1sSy>>!2wwB1AA!a%((	ax	B	ax	B
a;r??[__45@ERRTWDUV
W
WCJr/   c                 N    t          t          |  t          | t          |           S )a  Calculates the arc length for a quadratic Bezier segment.

    Uses Gauss-Legendre quadrature for a branch-free approximation.
    See :func:`calcQuadraticArcLength` for a slower but more accurate result.

    Args:
        pt1: Start point of the Bezier as 2D tuple.
        pt2: Handle point of the Bezier as 2D tuple.
        pt3: End point of the Bezier as 2D tuple.

    Returns:
        Approximate arc length value.
    )r   r(   rW   s      r.   r   r      s#     *'3-#QTVVVr/   c                     t          d| z  d|z  z   d|z  z             }t          || z
            dz  }t          d| z  d|z  z
  d|z  z             }||z   |z   S )a  Calculates the arc length for a quadratic Bezier segment.

    Uses Gauss-Legendre quadrature for a branch-free approximation.
    See :func:`calcQuadraticArcLength` for a slower but more accurate result.

    Args:
        pt1: Start point of the Bezier as a complex number.
        pt2: Handle point of the Bezier as a complex number.
        pt3: End point of the Bezier as a complex number.

    Returns:
        Approximate arc length value.
    g̔xb߿gb?gFVW?gqq?gFVWg̔xb?r=   )r)   r*   r+   v0rK   rL   s         r.   r   r      s    , 
S #4s#::=ORU=UU
 
B 
S3Y,	,B	c!$5$;;>ORU>UU
 
B 7R<r/   c                 ,  	
 t          | ||          \  \  \  	
\  dz  }dz  }g }|dk    r|                    	 |z             |dk    r|                    
 |z             	
fd|D             | |gz   }t          |          S )a  Calculates the bounding rectangle for a quadratic Bezier segment.

    Args:
        pt1: Start point of the Bezier as a 2D tuple.
        pt2: Handle point of the Bezier as a 2D tuple.
        pt3: End point of the Bezier as a 2D tuple.

    Returns:
        A four-item tuple representing the bounding rectangle ``(xMin, yMin, xMax, yMax)``.

    Example::

        >>> calcQuadraticBounds((0, 0), (50, 100), (100, 0))
        (0, 0, 100, 50.0)
        >>> calcQuadraticBounds((0, 0), (100, 0), (100, 100))
        (0.0, 0.0, 100, 100)
           @r   c                 t    g | ]4}d |cxk    rdk     n n"|z  |z  |z  z   z   |z  |z  |z  z   z   f5S r   rP   r3   ).0taxaybxbycxcys     r.   
<listcomp>z'calcQuadraticBounds.<locals>.<listcomp>   sk       ::::A::::: 
a!b1f	r	!26A:Q#6#;<::r/   )calcQuadraticParametersappendr   )r)   r*   r+   ax2ay2rootspointsrp   rq   rr   rs   rt   ru   s          @@@@@@r.   r   r      s    $ $;3S#I#I HRhr2R
s(C
s(CE
axxbS3Y
axxbS3Y           
c
	F
 fr/   c                 ^    t          t          |  t          | t          | t          |           S )a  Approximates the arc length for a cubic Bezier segment.

    Uses Gauss-Lobatto quadrature with n=5 points to approximate arc length.
    See :func:`calcCubicArcLength` for a slower but more accurate result.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.

    Returns:
        Arc length value.

    Example::

        >>> approximateCubicArcLength((0, 0), (25, 100), (75, 100), (100, 0))
        190.04332968932817
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, 50), (100, 100))
        154.8852074945903
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, 0), (150, 0)) # line; exact result should be 150.
        149.99999999999991
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, 0), (-50, 0)) # cusp; exact result should be 150.
        136.9267662156362
        >>> approximateCubicArcLength((0, 0), (50, 0), (100, -50), (-50, 0)) # cusp
        154.80848416537057
    )r   r(   )r)   r*   r+   r,   s       r.   r   r     s/    2 &w}gsmWc]  r/   c                 8   t          || z
            dz  }t          d| z  d|z  z   d|z  z   d|z  z             }t          || z
  |z   |z
            dz  }t          d| z  d|z  z
  d|z  z
  d|z  z             }t          ||z
            dz  }||z   |z   |z   |z   S )	zApproximates the arc length for a cubic Bezier segment.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers.

    Returns:
        Arc length value.
    g333333?gc1g85$t?gu|Y?g#$?g?g#$gc1?rh   )	r)   r*   r+   r,   ri   rK   rL   v3v4s	            r.   r   r   #  s    " 
S3Y$	B	S 
c
!	"
c
!	" c
!	"
 
B 
S3Y_s"	#	#&9	9B	S 
c
!	"
c
!	" c
!	"
 
B 
S3Y$	B7R<"r!!r/   c                 H   t          | |||          \  \  \  \  \  dz  }dz  }dz  }dz  }d t          ||          D             }d t          ||          D             }	||	z   }
fd|
D             | |gz   }t          |          S )aX  Calculates the bounding rectangle for a quadratic Bezier segment.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.

    Returns:
        A four-item tuple representing the bounding rectangle ``(xMin, yMin, xMax, yMax)``.

    Example::

        >>> calcCubicBounds((0, 0), (25, 100), (75, 100), (100, 0))
        (0, 0, 100, 75.0)
        >>> calcCubicBounds((0, 0), (50, 0), (100, 50), (100, 100))
        (0.0, 0.0, 100, 100)
        >>> print("%f %f %f %f" % calcCubicBounds((50, 0), (0, 100), (100, 100), (50, 0)))
        35.566243 0.000000 64.433757 75.000000
          @rk   c                 2    g | ]}d |cxk    rdk     n n|S rm   r3   rn   ro   s     r.   rv   z#calcCubicBounds.<locals>.<listcomp>_  -    DDDAa!ar/   c                 2    g | ]}d |cxk    rdk     n n|S rm   r3   r   s     r.   rv   z#calcCubicBounds.<locals>.<listcomp>`  r   r/   c                     g | ]<}|z  |z  |z  |z  |z  z   |z  z   z   |z  |z  |z  |z  |z  z   |z  z   	z   f=S r3   r3   )
rn   ro   rp   rq   rr   rs   rt   ru   dxdys
     r.   rv   z#calcCubicBounds.<locals>.<listcomp>c  s       
  FQJNR!VaZ'"q&025FQJNR!VaZ'"q&025	
  r/   )calcCubicParametersr   r   )r)   r*   r+   r,   ax3ay3bx2by2xRootsyRootsr{   r|   rp   rq   rr   rs   rt   ru   r   r   s               @@@@@@@@r.   r   r   G  s   $ .Ac3PS-T-T*HRhr2R(2r
s(C
s(C
s(C
s(CDDS"55DDDFDDS"55DDDFVOE          
    
c
F fr/   c                     | \  }}|\  }}||z
  }||z
  }	|}
|}||	f|         }|dk    r| |fgS ||
|f|         z
  |z  }d|cxk    rdk     rn n||z  |
z   |	|z  |z   f}| |f||fgS | |fgS )a  Split a line at a given coordinate.

    Args:
        pt1: Start point of line as 2D tuple.
        pt2: End point of line as 2D tuple.
        where: Position at which to split the line.
        isHorizontal: Direction of the ray splitting the line. If true,
            ``where`` is interpreted as a Y coordinate; if false, then
            ``where`` is interpreted as an X coordinate.

    Returns:
        A list of two line segments (each line segment being two 2D tuples)
        if the line was successfully split, or a list containing the original
        line.

    Example::

        >>> printSegments(splitLine((0, 0), (100, 100), 50, True))
        ((0, 0), (50, 50))
        ((50, 50), (100, 100))
        >>> printSegments(splitLine((0, 0), (100, 100), 100, True))
        ((0, 0), (100, 100))
        >>> printSegments(splitLine((0, 0), (100, 100), 0, True))
        ((0, 0), (0, 0))
        ((0, 0), (100, 100))
        >>> printSegments(splitLine((0, 0), (100, 100), 0, False))
        ((0, 0), (0, 0))
        ((0, 0), (100, 100))
        >>> printSegments(splitLine((100, 0), (0, 0), 50, False))
        ((100, 0), (50, 0))
        ((50, 0), (0, 0))
        >>> printSegments(splitLine((0, 100), (0, 0), 50, True))
        ((0, 100), (0, 50))
        ((0, 50), (0, 0))
    r   rP   r3   )r)   r*   whereisHorizontalpt1xpt1ypt2xpt2yrp   rq   rr   rs   ra   ro   midPts                  r.   r   r   m  s    H JD$JD$	B	B	B	B	RAAvvc
|	"b,'	'1,AAzzzzzzzzzQR!Vb[(eucl++c
|r/   c                     t          | ||          \  }}}t          ||         ||         ||         |z
            }t          d |D                       }|s| ||fgS t          |||g|R  S )a  Split a quadratic Bezier curve at a given coordinate.

    Args:
        pt1,pt2,pt3: Control points of the Bezier as 2D tuples.
        where: Position at which to split the curve.
        isHorizontal: Direction of the ray splitting the curve. If true,
            ``where`` is interpreted as a Y coordinate; if false, then
            ``where`` is interpreted as an X coordinate.

    Returns:
        A list of two curve segments (each curve segment being three 2D tuples)
        if the curve was successfully split, or a list containing the original
        curve.

    Example::

        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 150, False))
        ((0, 0), (50, 100), (100, 0))
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 50, False))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (75, 50), (100, 0))
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 25, False))
        ((0, 0), (12.5, 25), (25, 37.5))
        ((25, 37.5), (62.5, 75), (100, 0))
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 25, True))
        ((0, 0), (7.32233, 14.6447), (14.6447, 25))
        ((14.6447, 25), (50, 75), (85.3553, 25))
        ((85.3553, 25), (92.6777, 14.6447), (100, -7.10543e-15))
        >>> # XXX I'm not at all sure if the following behavior is desirable:
        >>> printSegments(splitQuadratic((0, 0), (50, 100), (100, 0), 50, True))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (50, 50), (50, 50))
        ((50, 50), (75, 50), (100, 0))
    c              3   :   K   | ]}d |cxk    rdk     n n|V  dS r   rP   Nr3   r   s     r.   	<genexpr>z!splitQuadratic.<locals>.<genexpr>  6      ::QqAzzzzzzzzzqzzzz::r/   )rw   r   sorted_splitQuadraticAtT)	r)   r*   r+   r   r   ra   rb   c	solutionss	            r.   r   r     s    F &c344GAq!	,<!L/E*A I ::):::::I !c3  aA2	2222r/   c                     t          | |||          \  }}}}	t          ||         ||         ||         |	|         |z
            }
t          d |
D                       }
|
s| |||fgS t          ||||	g|
R  S )a  Split a cubic Bezier curve at a given coordinate.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.
        where: Position at which to split the curve.
        isHorizontal: Direction of the ray splitting the curve. If true,
            ``where`` is interpreted as a Y coordinate; if false, then
            ``where`` is interpreted as an X coordinate.

    Returns:
        A list of two curve segments (each curve segment being four 2D tuples)
        if the curve was successfully split, or a list containing the original
        curve.

    Example::

        >>> printSegments(splitCubic((0, 0), (25, 100), (75, 100), (100, 0), 150, False))
        ((0, 0), (25, 100), (75, 100), (100, 0))
        >>> printSegments(splitCubic((0, 0), (25, 100), (75, 100), (100, 0), 50, False))
        ((0, 0), (12.5, 50), (31.25, 75), (50, 75))
        ((50, 75), (68.75, 75), (87.5, 50), (100, 0))
        >>> printSegments(splitCubic((0, 0), (25, 100), (75, 100), (100, 0), 25, True))
        ((0, 0), (2.29379, 9.17517), (4.79804, 17.5085), (7.47414, 25))
        ((7.47414, 25), (31.2886, 91.6667), (68.7114, 91.6667), (92.5259, 25))
        ((92.5259, 25), (95.202, 17.5085), (97.7062, 9.17517), (100, 1.77636e-15))
    c              3   :   K   | ]}d |cxk    rdk     n n|V  dS r   r3   r   s     r.   r   zsplitCubic.<locals>.<genexpr>  r   r/   )r   r   r   _splitCubicAtT)r)   r*   r+   r,   r   r   ra   rb   r   r]   r   s              r.   r   r     s    6 %S#sC88JAq!Q	,<!L/1\?U;R I ::):::::I &c3$%%!Q11y1111r/   c                 J    t          | ||          \  }}}t          |||g|R  S )a  Split a quadratic Bezier curve at one or more values of t.

    Args:
        pt1,pt2,pt3: Control points of the Bezier as 2D tuples.
        *ts: Positions at which to split the curve.

    Returns:
        A list of curve segments (each curve segment being three 2D tuples).

    Examples::

        >>> printSegments(splitQuadraticAtT((0, 0), (50, 100), (100, 0), 0.5))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (75, 50), (100, 0))
        >>> printSegments(splitQuadraticAtT((0, 0), (50, 100), (100, 0), 0.5, 0.75))
        ((0, 0), (25, 50), (50, 50))
        ((50, 50), (62.5, 50), (75, 37.5))
        ((75, 37.5), (87.5, 25), (100, 0))
    )rw   r   )r)   r*   r+   tsra   rb   r   s          r.   r   r     s5    ( &c344GAq!aA+++++r/   c                 P    t          | |||          \  }}}}t          ||||g|R  S )a   Split a cubic Bezier curve at one or more values of t.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as 2D tuples.
        *ts: Positions at which to split the curve.

    Returns:
        A list of curve segments (each curve segment being four 2D tuples).

    Examples::

        >>> printSegments(splitCubicAtT((0, 0), (25, 100), (75, 100), (100, 0), 0.5))
        ((0, 0), (12.5, 50), (31.25, 75), (50, 75))
        ((50, 75), (68.75, 75), (87.5, 50), (100, 0))
        >>> printSegments(splitCubicAtT((0, 0), (25, 100), (75, 100), (100, 0), 0.5, 0.75))
        ((0, 0), (12.5, 50), (31.25, 75), (50, 75))
        ((50, 75), (59.375, 75), (68.75, 68.75), (77.3438, 56.25))
        ((77.3438, 56.25), (85.9375, 43.75), (93.75, 25), (100, 0))
    )r   r   )	r)   r*   r+   r,   r   ra   rb   r   r]   s	            r.   r   r     s;    ( %S#sC88JAq!Q!Q1*r****r/   c                    t          |          }g }|                    dd           |                    d           | \  }}|\  }}|\  }	}
t          t	          |          dz
            D ]}||         }||dz            }||z
  }||z  }||z  }||z  }d|z  |z  |z   |z  }d|z  |z  |z   |z  }||z  }||z  ||z  z   |	z   }||z  ||z  z   |
z   }t          ||f||f||f          \  }}}|                    |||f           |S )Nr   rY   rE   rP   rO   )listinsertrx   rangelencalcQuadraticPoints)ra   rb   r   r   segmentsrp   rq   rr   rs   rt   ru   ir   r   deltadelta_2a1xa1yb1xb1yt1_2c1xc1yr)   r*   r+   s                             r.   r   r   (  sS   	bBHIIaIIcNNNFBFBFB3r77Q; ) )UAYR%-7l7l2v{R5(2v{R5(Bw4i"r'!B&4i"r'!B&+S#Jc
S#JOOS#c3((((Or/   c                    t          |          }|                    dd           |                    d           g }| \  }}|\  }}	|\  }
}|\  }}t          t	          |          dz
            D ]}||         }||dz            }||z
  }||z  }||z  }||z  }||z  }||z  }||z  }d|z  |z  |z   |z  }d|z  |z  |	z   |z  }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  |	|z  z   ||z  z   |z   }t          ||f||f||f||f          \  }}} }!|                    ||| |!f           |S )Nr   rY   rE   rP   r1   rO   )r   r   rx   r   r   calcCubicPoints)"ra   rb   r   r]   r   r   rp   rq   rr   rs   rt   ru   r   r   r   r   r   r   r   delta_3r   t1_3r   r   r   r   r   r   d1xd1yr)   r*   r+   r,   s"                                     r.   r   r   C  s   	bBIIaIIcNNNHFBFBFBFB3r77Q; . .UAYR%-'/BwDy 7l7l2v{R7*2v{R7*2v{R!b&4-/582v{R!b&4-/584i"t)#b2g-24i"t)#b2g-2,#Jc
S#Jc

 
S#s 	c3,----Or/   )rR   acoscospic                     t          |           t          k     r#t          |          t          k     rg }nB| |z  g}n:||z  d| z  |z  z
  }|dk    r$ ||          }| |z   dz  | z  | |z
  dz  | z  g}ng }|S )uK  Solve a quadratic equation.

    Solves *a*x*x + b*x + c = 0* where a, b and c are real.

    Args:
        a: coefficient of *x²*
        b: coefficient of *x*
        c: constant term

    Returns:
        A list of roots. Note that the returned list is neither guaranteed to
        be sorted nor to contain unique values!
          @rY   rk   )r=   rZ   )ra   rb   r   rR   r{   DDrDDs          r.   r   r   m  s     1vvq66GEE R!VHEE US1Wq[ 99$r((Cb3h#%)QBH+;a+?@EE ELr/   c           
          t          |           t          k     rt          |||          S t          |           } || z  }|| z  }|| z  }||z  d|z  z
  dz  }d|z  |z  |z  d|z  |z  z
  d|z  z   dz  }||z  }	||z  |z  }
|	t          k     rdn|	}	t          |
          t          k     rdn|
}
|	|
z
  }|	dk    r$|
dk    rt	          | dz  t
                    }|||gS |t          dz  k    rt          t          t          |t          |
          z  d	          d
                    }dt          |          z  }|dz  }|t          |dz            z  |z
  }|t          |dt          z  z   dz            z  |z
  }|t          |dt          z  z   dz            z  |z
  }t          |||g          \  }}}||z
  t          k     r1||z
  t          k     r#t	          ||z   |z   dz  t
                    x}x}}n||z
  t          k     r3t	          ||z   dz  t
                    x}}t	          |t
                    }n||z
  t          k     r3t	          |t
                    }t	          ||z   dz  t
                    x}}n?t	          |t
                    }t	          |t
                    }t	          |t
                    }|||gS t          t          |          t          |          z   d          }|||z  z   }|dk    r| }t	          ||dz  z
  t
                    }|gS )ut  Solve a cubic equation.

    Solves *a*x*x*x + b*x*x + c*x + d = 0* where a, b, c and d are real.

    Args:
        a: coefficient of *x³*
        b: coefficient of *x²*
        c: coefficient of *x*
        d: constant term

    Returns:
        A list of roots. Note that the returned list is neither guaranteed to
        be sorted nor to contain unique values!

    Examples::

        >>> solveCubic(1, 1, -6, 0)
        [-3.0, -0.0, 2.0]
        >>> solveCubic(-10.0, -9.0, 48.0, -29.0)
        [-2.9, 1.0, 1.0]
        >>> solveCubic(-9.875, -9.0, 47.625, -28.75)
        [-2.911392, 1.0, 1.0]
        >>> solveCubic(1.0, -4.5, 6.75, -3.375)
        [1.5, 1.5, 1.5]
        >>> solveCubic(-12.0, 18.0, -9.0, 1.50023651123)
        [0.5, 0.5, 0.5]
        >>> solveCubic(
        ...     9.0, 0.0, 0.0, -7.62939453125e-05
        ... ) == [-0.0, -0.0, -0.0]
        True
    r   g      "@rk   g      ;@g      K@r   rY   r2   rE   g      g       r   gUUUUUU?)r=   rZ   r   floatroundepsilonDigitsr   maxminrR   r   r   r   pow)ra   rb   r   r]   a1a2a3QRR2Q3R2_Q3rT   thetarQ2a1_3rc   rd   x2s                      r.   r   r     s0   L 1vv aA&&&aA	
QB	
QB	
QB	b38	s"A	rB	cBhm	+dRi	74?A	
QB	
QB7llB"ggRBGE	SyyR3YY2#)]++1ay	'C-		SQb\3//6677T!WWnCx3us{###d*3b(C/000473b(C/00047RRL))
B7Wb7!2!2 "r'B,#!5}EEEBEbb"WwR"WO];;;Br=))BB"Wwr=))BR"WO];;;Br=))Br=))Br=))BB|Uc!ff$g..AI88A!b3h,..s
r/   c                 v    |\  }}|\  }}| \  }}||z
  dz  }	||z
  dz  }
||z
  |	z
  }||z
  |
z
  }||f|	|
f||ffS )Nrk   r3   )r)   r*   r+   r   y2x3y3rt   ru   rr   rs   rp   rq   s                r.   rw   rw     sj    FBFBFB
r'SB
r'SB	b2B	b2B8b"XBx''r/   c                     |\  }}|\  }}|\  }}	| \  }
}||
z
  dz  }||z
  dz  }||z
  dz  |z
  }||z
  dz  |z
  }||
z
  |z
  |z
  }|	|z
  |z
  |z
  }||f||f||f|
|ffS Nr   r3   )r)   r*   r+   r,   r   r   r   r   x4y4r   r   rt   ru   rr   rs   rp   rq   s                     r.   r   r     s    FBFBFBFB
r'SB
r'SB
r'S2	B
r'S2	B	b2	B	b2	B8b"XBx"b11r/   c                 ~    | \  }}|\  }}|\  }}|}	|}
|dz  |z   }|dz  |z   }||z   |z   }||z   |z   }|	|
f||f||ffS r<   r3   )ra   rb   r   rp   rq   rr   rs   rt   ru   rd   y1r   r   r   r   s                  r.   r   r     st    FBFBFB	B	B
s(bB
s(bB	b2B	b2B8b"XBx''r/   c                     | \  }}|\  }}|\  }}	|\  }
}|
}|}|dz  |
z   }|	dz  |z   }||z   dz  |z   }||	z   dz  |z   }||
z   |z   |z   }||z   |	z   |z   }||f||f||f||ffS r   r3   )ra   rb   r   r]   rp   rq   rr   rs   rt   ru   r   r   rd   r   r   r   r   r   r   r   s                       r.   r   r     s    FBFBFBFB	B	B
s(bB
s(bB
r'S2	B
r'S2	B	b2	B	b2	B8b"XBx"b11r/   c                 j    | d         d|z
  z  |d         |z  z   | d         d|z
  z  |d         |z  z   fS )zFinds the point at time `t` on a line.

    Args:
        pt1, pt2: Coordinates of the line as 2D tuples.
        t: The time along the line.

    Returns:
        A 2D tuple with the coordinates of the point.
    r   rP   r3   )r)   r*   ro   s      r.   r    r    *  sC     Vq1uA
*c!fA.>Q!.KMMr/   c                     d|z
  d|z
  z  | d         z  dd|z
  z  |z  |d         z  z   ||z  |d         z  z   }d|z
  d|z
  z  | d         z  dd|z
  z  |z  |d         z  z   ||z  |d         z  z   }||fS )zFinds the point at time `t` on a quadratic curve.

    Args:
        pt1, pt2, pt3: Coordinates of the curve as 2D tuples.
        t: The time along the curve.

    Returns:
        A 2D tuple with the coordinates of the point.
    rP   r   rO   r3   )r)   r*   r+   ro   rT   ys         r.   r   r   7  s     
Q1q5CF"Q!a%[1_s1v%==AANA	
Q1q5CF"Q!a%[1_s1v%==AANAq6Mr/   c                    d|z
  d|z
  z  d|z
  z  | d         z  dd|z
  z  d|z
  z  |z  |d         z  z   dd|z
  z  |z  |z  |d         z  z   ||z  |z  |d         z  z   }d|z
  d|z
  z  d|z
  z  | d         z  dd|z
  z  d|z
  z  |z  |d         z  z   dd|z
  z  |z  |z  |d         z  z   ||z  |z  |d         z  z   }||fS )zFinds the point at time `t` on a cubic curve.

    Args:
        pt1, pt2, pt3, pt4: Coordinates of the curve as 2D tuples.
        t: The time along the curve.

    Returns:
        A 2D tuple with the coordinates of the point.
    rP   r   r1   r3   )r)   r*   r+   r,   ro   rT   r   s          r.   r   r   F  s'    
Q1q5QU#c!f,
q1u+Q
!
#c!f
,	-
q1u+/A
A
&	' a%!)c!f
	  
Q1q5QU#c!f,
q1u+Q
!
#c!f
,	-
q1u+/A
A
&	' a%!)c!f
	  q6Mr/   c                     t          |           dk    rt          g | |R  S t          |           dk    rt          g | |R  S t          |           dk    rt          g | |R  S t	          d          NrO   r1      Unknown curve degree)r   r    r   r   
ValueError)segro   s     r.   r!   r!   _  s    
3xx1}}$S$!$$$$	SQ )#)q))))	SQ%c%1%%%%
+
,
,,r/   c                     | \  }}|\  }}|\  }}t          j        ||          s||z
  ||z
  z  S t          j        ||          s||z
  ||z
  z  S dS )N)rQ   isclose)	ser
   sxsyexeypxpys	            r.   _line_t_of_ptr   n  sp    FBFBFB<B %RBG$$<B %RBG$$2r/   c                     | d         |d         z
  |d         |d         z
  z  }| d         |d         z
  |d         |d         z
  z  }|dk    o|dk     S )Nr   rP   rY   r3   )ra   rb   originxDiffyDiffs        r.   '_both_points_are_on_same_side_of_originr   z  s`    qTF1I!A$"23EqTF1I!A$"23E-#..r/   c           	         | \  }}|\  }}|\  }}	|\  }
}t          j        ||
          r,t          j        ||          rt          j        ||          sg S t          j        |	|          r,t          j        ||          rt          j        ||	          sg S t          j        ||
          rt          j        |	|          rg S t          j        ||          rt          j        ||          rg S t          j        ||          rM|}||	z
  |
|z
  z  }|||z
  z  |	z   }||f}t          |t          | ||          t          |||                    gS t          j        ||
          rM|}||z
  ||z
  z  }|||z
  z  |z   }||f}t          |t          | ||          t          |||                    gS ||z
  ||z
  z  }||	z
  |
|z
  z  }t          j        ||          rg S ||z  |z
  ||z  z
  |	z   ||z
  z  }|||z
  z  |z   }||f}t	          |||           rBt	          |||          r1t          |t          | ||          t          |||                    gS g S )a  Finds intersections between two line segments.

    Args:
        s1, e1: Coordinates of the first line as 2D tuples.
        s2, e2: Coordinates of the second line as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::

        >>> a = lineLineIntersections( (310,389), (453, 222), (289, 251), (447, 367))
        >>> len(a)
        1
        >>> intersection = a[0]
        >>> intersection.pt
        (374.44882952482897, 313.73458370177315)
        >>> (intersection.t1, intersection.t2)
        (0.45069111555824454, 0.5408153767394238)
    r	   )rQ   r   r   r   r   )s1e1s2e2s1xs1ye1xe1ys2xs2ye2xe2yrT   slope34r   r
   slope12s                    r.   r"   r"     s    . HCHCHCHCS##'<S#9#9BF,sTWBXBX 	S##'<S#9#9BF,sTWBXBX 	|C $,sC"8"8 	|C $,sC"8"8 	|C 	
9s+q3w#%V-B33b"b8Q8Q  
 	

 |C 	
9s+q3w#%V-B33b"b8Q8Q  
 	
 SyS3Y'GSyS3Y'G|GW%% 		3	w}	,s	2w7HIA1s7c!A
QB.
B  

1"b"
=
=
 -B33b"b8Q8Q  
 	

 Ir/   c                     | d         }| d         }t          j        |d         |d         z
  |d         |d         z
            }t          j        |                               |d          |d                    S )Nr   r   rP   )rQ   atan2r   rotate	translate)segmentstartendangles       r.   _alignment_transformationr    so     AJE
"+CJs1va(#a&58*;<<E?E6"",,eAhYq	BBBr/   c                    t          |                              |           }t          |           dk    r1t          | \  }}}t	          |d         |d         |d                   }n[t          |           dk    r9t          | \  }}}}t          |d         |d         |d         |d                   }nt          d          t          d |D                       S )Nr1   rP   r   r   c              3   :   K   | ]}d |cxk    rdk    n n|V  dS )rY   rP   Nr3   rn   r   s     r.   r   z._curve_line_intersections_t.<locals>.<genexpr>  s6      <<cQmmmm!mmmmm!mmmm<<r/   )	r  transformPointsr   rw   r   r   r   r   r   )curvelinealigned_curvera   rb   r   intersectionsr]   s           r.   _curve_line_intersections_tr    s    -d33CCEJJM
5zzQ)=91a&qtQqT1Q488	Uq(-8
1a"1Q41qtQqT::/000<<]<<<<<<r/   c                 0   t          |           dk    rt          }n*t          |           dk    rt          }nt          d          g }t	          | |          D ]=} |g | |R  }|                    t          ||t          g ||R                       >|S )a  Finds intersections between a curve and a line.

    Args:
        curve: List of coordinates of the curve segment as 2D tuples.
        line: List of coordinates of the line segment as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::
        >>> curve = [ (100, 240), (30, 60), (210, 230), (160, 30) ]
        >>> line  = [ (25, 260), (230, 20) ]
        >>> intersections = curveLineIntersections(curve, line)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (84.90010344084885, 189.87306176459828)
    r1   r   r   r	   )r   r   r   r   r  rx   r   r   )r  r  pointFinderr  ro   r
   s         r.   r#   r#     s    * 5zzQ'	Uq#/000M(55 U U[#%####\RA-:R:Rr:R:R:RSSSTTTTr/   c                     t          |           dk    r	t          |  S t          |           dk    r	t          |  S t          d          )Nr1   r   r   )r   r   r   r   )r   s    r.   _curve_boundsr#    sE    
1vv{{"A&&	Q1""
+
,
,,r/   c                    t          |           dk    r| \  }}t          |||          }||f||fgS t          |           dk    rt          g | |R  S t          |           dk    rt          g | |R  S t	          d          r   )r   r    r   r   r   )r   ro   r   r   midpoints        r.   _split_segment_at_tr&    s    
1vv{{11a((H!}--
1vv{{ '!'Q''''	Q1#a#####
+
,
,,r/   MbP?c           	         t          |           }t          |          }|sd}|sd}t          ||          \  }}|sg S d }	t          |          k     r*t          |          k     r |	|           |	|          fgS t          | d          \  }
}|d          |	|          f} |	|          |d         f}t          |d          \  }}|d          |	|          f} |	|          |d         f}g }|                    t          |
|||                     |                    t          ||||                     |                    t          |
|||                     |                    t          ||||                     fd}t                      }g }|D ]<} ||          }||v r|                    |           |                    |           =|S )N)rY   rE   c                 *    d| d         | d         z   z  S )Nr2   r   rP   r3   )rs    r.   r%  z._curve_curve_intersections_t.<locals>.midpoint+  s    adQqTk""r/   r2   r   rP   )range1range2c                 d    t          | d         z            t          | d         z            fS )Nr   rP   )int)r   	precisions    r.   <lambda>z._curve_curve_intersections_t.<locals>.<lambda>P  s.    SA!233SA9J5K5KL r/   )	r#  r   r   r&  extend_curve_curve_intersections_tsetaddrx   )curve1curve2r/  r+  r,  bounds1bounds2
intersects_r%  c11c12	c11_range	c12_rangec21c22	c21_range	c22_rangefound
unique_keyseenunique_valuesr   keys     `                     r.   r2  r2    s    F##GF##G   Wg..MJ 	# # # 9$$'):):Y)F)F&!!88F#3#3455"63//HCHHV,,-I&!!6!9-I"63//HCHHV,,-I&!!6!9-IE	LL$i	)	
 	
 	
  
 
LL$i	)	
 	
 	
  
 
LL$i	)	
 	
 	
  
 
LL$i	)	
 	
 	
   MLLLJ55DM ! !jnn$;;R    r/   c                 @     t           |          } fd|D             S )a  Finds intersections between a curve and a curve.

    Args:
        curve1: List of coordinates of the first curve segment as 2D tuples.
        curve2: List of coordinates of the second curve segment as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::
        >>> curve1 = [ (10,100), (90,30), (40,140), (220,220) ]
        >>> curve2 = [ (5,150), (180,20), (80,250), (210,190) ]
        >>> intersections = curveCurveIntersections(curve1, curve2)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (81.7831487395506, 109.88904552375288)
    c           	      t    g | ]4}t          t          |d                    |d          |d                   5S )r   rP   r	   )r   r!   )rn   r   r5  s     r.   rv   z+curveCurveIntersections.<locals>.<listcomp>t  sN        	1662a5RUKKK  r/   )r2  )r5  r6  intersection_tss   `  r.   r$   r$   ^  s?    * 366BBO   !   r/   c                    d}t          |          t          |           k    r| |} }d}t          |           dk    r5t          |          dk    rt          | |          }nUt          | |          }nDt          |           dk    r"t          |          dk    rt          g | |R  }nt	          d          |s|S d |D             S )a*  Finds intersections between two segments.

    Args:
        seg1: List of coordinates of the first segment as 2D tuples.
        seg2: List of coordinates of the second segment as 2D tuples.

    Returns:
        A list of ``Intersection`` objects, each object having ``pt``, ``t1``
        and ``t2`` attributes containing the intersection point, time on first
        segment and time on second segment respectively.

    Examples::
        >>> curve1 = [ (10,100), (90,30), (40,140), (220,220) ]
        >>> curve2 = [ (5,150), (180,20), (80,250), (210,190) ]
        >>> intersections = segmentSegmentIntersections(curve1, curve2)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (81.7831487395506, 109.88904552375288)
        >>> curve3 = [ (100, 240), (30, 60), (210, 230), (160, 30) ]
        >>> line  = [ (25, 260), (230, 20) ]
        >>> intersections = segmentSegmentIntersections(curve3, line)
        >>> len(intersections)
        3
        >>> intersections[0].pt
        (84.90010344084885, 189.87306176459828)

    FTrO   z4Couldn't work out which intersection function to usec                 P    g | ]#}t          |j        |j        |j                   $S )r	   )r   r
   r   r   r  s     r.   rv   z/segmentSegmentIntersections.<locals>.<listcomp>  s-    KKKLADQTad333KKKr/   )r   r$   r#   r"   r   )seg1seg2swappedr  s       r.   r%   r%   z  s    < G
4yy3t994d
4yy1}}t99q==3D$??MM24>>MM	TaCIINN-;t;d;;;OPPP KK]KKKKr/   c                     	 t          |           }dd                    d |D                       z  S # t          $ r d| z  cY S w xY w)zw
    >>> _segmentrepr([1, [2, 3], [], [[2, [3, 4], [0.1, 2.2]]]])
    '(1, (2, 3), (), ((2, (3, 4), (0.1, 2.2))))'
    z(%s)z, c              3   4   K   | ]}t          |          V  d S rH   )_segmentrepr)rn   rT   s     r.   r   z_segmentrepr.<locals>.<genexpr>  s(      !>!>a,q//!>!>!>!>!>!>r/   z%g)iterjoin	TypeError)objits     r.   rR  rR    sg    
?#YY 		!>!>2!>!>!>>>>>    czs   3 AAc                 H    | D ]}t          t          |                     dS )zlHelper for the doctests, displaying each segment in a list of
    segments on a single line as a tuple.
    N)printrR  )r   r  s     r.   printSegmentsrZ    s6      % %l7##$$$$% %r/   __main__)r&   )r'  NN)D__doc__fontTools.misc.arrayToolsr   r   r   fontTools.misc.transformr   rQ   collectionsr   r   __all__r   r:   r>   r   r   rZ   rM   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rR   r   r   r   r   r   rw   r   r   r   r    r   r   r!   r   r   r"   r  r  r#   r#  r&  r2  r$   r%   rR  rZ  __name__sysdoctestexittestmodfailedr3   r/   r.   <module>rg     s    E D D D D D D D D D - - - - - -  " " " " " "z.*<*<*<==  :   &  	
 	
 	
A A A A 
& & &= = = P  P  PF  @W W W"  B  D  <!" !" !"H# # #L6 6 6r*3 *3 *3Z"2 "2 "2J, , ,0+ + +0  6     N % $ $ $ $ $ $ $ $ $ $ $ "&    BY Y YB( ( (2 2 2
( 
( 
(2 2 2*
N 
N 
N    2- - -	 	 	/ / /K K K\C C C
= 
= 
=  D- - -	- 	- 	- 9=@ @ @ @F  8-L -L -L`
? 
? 
?% % % zJJJNNNCH_W_%&&&&&	 r/   