U
    6c                     @   s6  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ddd	gZ
d
dddddddddddddddddddddd d!d"gZdhd$dZd%d& Zd'd( Zdid)dZd*Zd+Zd,d- Zd.d/ Zd0d Zd1d Zd2d Zd3d Zd4d Zd5d
 Zd6d Zd7d Zd8d Zd9d Zd:d Zd;d Zd<d Z d=d> Z!d?d@ Z"ddAlm#Z#m$Z$m%Z%m&Z& e#fdBdZ'dCd Z(dDdE Z)dFdG Z*dHdI Z+dJdK Z,dLd Z-dMd Z.dNd Z/dOd Z0dPdQ Z1dRdS Z2dTd Z3dUdV Z4dWdX Z5dYd  Z6dZd[ Z7d\d] Z8djd_d`Z9dad! Z:dbd" Z;dcdd Z<dedf Z=e>dgkr2ddl?Z?ddl@Z@e?Ae@B jC dS )kzNfontTools.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                 C   s    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	tolerance r+   >/tmp/pip-unpacked-wheel-n2hbwplv/fontTools/misc/bezierTools.pyr   *   s        c                 C   s\   | d||   | d }|| | |  d }| | | d || |f||| || d |ffS )N   g      ?      ?r+   )p0p1p2p3ZmidZderiv3r+   r+   r,   _split_cubic_into_two=   s
    r3   c           	      C   sz   t || }t || t ||  t ||  }||  |krH|| d S t||||\}}t| f| t| f|  S d S Nr.   )absr3   _calcCubicArcLengthCRecurse)	multr/   r0   r1   r2   archZboxZoneZtwor+   r+   r,   r6   F   s    $r6   c                 C   s   dd|  }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      ?)r6   )r&   r'   r(   r)   r*   r7   r+   r+   r,   r   R   s    
   g|=c                 C   s   | |   jS N)	conjugatereal)v1v2r+   r+   r,   _dotd   s    r@   c                 C   s(   | t | d d  d t | d  S )N      )mathsqrtasinh)xr+   r+   r,   _intSecAtanh   s    rG   c                 C   s   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(   r+   r+   r,   r   n   s     c                 C   s   ||  }|| }|| }|d }t |}|dkr<t ||  S t||}t |tk rt||dkrlt ||  S t |t | }	}
|	|	 |
|
  |	|
  S t||| }t||| }t dt|t|  | |||   }|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   rA   )r5   r@   epsilonrG   )r&   r'   r(   Zd0d1dnZscaleZorigDistabx0x1ZLenr+   r+   r,   r      s"    
(c                 C   s   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%   rH   r+   r+   r,   r      s    c                 C   sT   t d|  d|  d|  }t ||  d }t d|  d|  d|  }|| | 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?r5   )r&   r'   r(   v0r>   r?   r+   r+   r,   r      s    c                    s   t | ||\\ \\ d }d }g }|dkrJ| |  |dkrb| |   fdd|D | |g }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                    sT   g | ]L}d |  krdk rn q | | |   | | |   fqS r   rB   r+   .0taxaybxbycxcyr+   r,   
<listcomp>   s
    
 z'calcQuadraticBounds.<locals>.<listcomp>)calcQuadraticParametersappendr   )r&   r'   r(   Zax2Zay2rootspointsr+   rY   r,   r      s    c                 C   s   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)   r+   r+   r,   r     s       c           	      C   s   t ||  d }t d|  d|  d|  d|  }t ||  | | d }t d|  d|  d|  d|  }t || d }|| | | | 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?rR   )	r&   r'   r(   r)   rS   r>   r?   Zv3Zv4r+   r+   r,   r   #  s,    c                    s   t | |||\\ \\\ d }d }d }d }dd t||D }dd t||D }	||	 }
 fdd|
D | |g }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
          @rT   c                 S   s(   g | ] }d |  krdk rn q|qS rU   r+   rV   r+   r+   r,   r`   _  s
      
  z#calcCubicBounds.<locals>.<listcomp>c                 S   s(   g | ] }d |  krdk rn q|qS rU   r+   rV   r+   r+   r,   r`   `  s
      
  c                    s\   g | ]T} | | | | |  |   | | | | |  |   fqS r+   r+   rV   rZ   r[   r\   r]   r^   r_   dxdyr+   r,   r`   c  s   &&)calcCubicParametersr   r   )r&   r'   r(   r)   Zax3Zay3Zbx2Zby2ZxRootsZyRootsrc   rd   r+   rf   r,   r   G  s    &c                 C   s   | \}}|\}}|| }|| }	|}
|}||	f| }|dkrF| |fgS ||
|f|  | }d|  krndk rn n(|| |
 |	| | f}| |f||fgS | |fgS d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   rB   Nr+   )r&   r'   whereisHorizontalZpt1xZpt1yZpt2xZpt2yrZ   r[   r\   r]   rN   rX   ZmidPtr+   r+   r,   r   m  s    $
c           	      C   sb   t | ||\}}}t|| || || | }tdd |D }|sP| ||fgS t|||f| 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                 s   s*   | ]"}d |  krdk rn q|V  qdS r   rB   Nr+   rV   r+   r+   r,   	<genexpr>  s
      
  z!splitQuadratic.<locals>.<genexpr>)ra   r   sorted_splitQuadraticAtT)	r&   r'   r(   rj   rk   rN   rO   c	solutionsr+   r+   r,   r     s    #  
c                 C   sp   t | |||\}}}}	t|| || || |	| | }
tdd |
D }
|
s\| |||fgS t||||	f|
 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                 s   s*   | ]"}d |  krdk rn q|V  qdS rl   r+   rV   r+   r+   r,   rm     s
      
  zsplitCubic.<locals>.<genexpr>)ri   r   rn   _splitCubicAtT)r&   r'   r(   r)   rj   rk   rN   rO   rp   rL   rq   r+   r+   r,   r     s       
c                 G   s$   t | ||\}}}t|||f| 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))
    )ra   ro   )r&   r'   r(   tsrN   rO   rp   r+   r+   r,   r     s    c           	      G   s*   t | |||\}}}}t||||f| 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))
    )ri   rr   )	r&   r'   r(   r)   rs   rN   rO   rp   rL   r+   r+   r,   r     s    c                 G   s  t |}g }|dd |d | \}}|\}}|\}	}
tt|d D ]}|| }||d  }|| }|| }|| }|| }d| | | | }d| | | | }|| }|| ||  |	 }|| ||  |
 }t||f||f||f\}}}||||f qJ|S )Nr   rI   r9   rB   rA   )listinsertrb   rangelencalcQuadraticPoints)rN   rO   rp   rs   segmentsrZ   r[   r\   r]   r^   r_   ir	   r
   deltadelta_2a1xa1yb1xb1yt1_2c1xc1yr&   r'   r(   r+   r+   r,   ro   (  s,    
ro   c           "      G   s  t |}|dd |d g }| \}}|\}}	|\}
}|\}}tt|d D ](}|| }||d  }|| }|| }|| }|| }|| }|| }|| }d| | | | }d| | |	 | }d| | |
 d| |  | }d|	 | | d| |  | }|| ||  |
|  | }|| |	|  ||  | }t||f||f||f||f\}}} }!|||| |!f qR|S )Nr   rI   r9   rB   r-   rA   )rt   ru   rb   rv   rw   calcCubicPoints)"rN   rO   rp   rL   rs   ry   rZ   r[   r\   r]   r^   r_   rg   rh   rz   r	   r
   r{   r|   Zdelta_3r   Zt1_3r}   r~   r   r   r   r   Zd1xZd1yr&   r'   r(   r)   r+   r+   r,   rr   C  s@    
     rr   )rD   acoscospic                 C   s~   t | tk r,t |tk rg }qz| | g}nN|| d|  |  }|dkrv||}| | d |  | | d |  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!
          @rI   rT   r5   rJ   )rN   rO   rp   rD   rc   ZDDZrDDr+   r+   r,   r   m  s    &c                 C   s  t | tk rt|||S t| } ||  }||  }||  }|| d|  d }d| | | d| |  d|  d }|| }	|| | }
|	tk rdn|	}	t |
tk rdn|
}
|	|
 }|	dkr|
dkrt| d t}|||gS |td kr@ttt|t	|
 d	d
}dt	| }|d }|t
|d  | }|t
|dt  d  | }|t
|dt  d  | }t|||g\}}}|| tk r|| tk rt|| | d t } }}n~|| tk rt|| d t }}t|t}nN|| tk rt|t}t|| d t }}nt|t}t|t}t|t}|||gS tt	|t | d}|||  }|dkrr| }t||d  t}|gS d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
    re   g      "@rT   g      ;@g      K@r   rI   r.   r9   g      g       r   gUUUUUU?N)r5   rJ   r   floatroundepsilonDigitsr   maxminrD   r   r   rn   pow)rN   rO   rp   rL   Za1Za2a3QRZR2ZQ3ZR2_Q3rF   thetaZrQ2Za1_3rP   rQ   x2r+   r+   r,   r     sT    &(
 





c                 C   s^   |\}}|\}}| \}}|| d }	|| d }
|| |	 }|| |
 }||f|	|
f||ffS )NrT   r+   )r&   r'   r(   r   y2x3y3r^   r_   r\   r]   rZ   r[   r+   r+   r,   ra     s    ra   c                 C   s   |\}}|\}}|\}}	| \}
}||
 d }|| d }|| d | }|| d | }||
 | | }|	| | | }||f||f||f|
|ffS Nre   r+   )r&   r'   r(   r)   r   r   r   r   x4y4rg   rh   r^   r_   r\   r]   rZ   r[   r+   r+   r,   ri     s    ri   c                 C   sf   | \}}|\}}|\}}|}	|}
|d | }|d | }|| | }|| | }|	|
f||f||ffS r4   r+   )rN   rO   rp   rZ   r[   r\   r]   r^   r_   rQ   y1r   r   r   r   r+   r+   r,   rx     s    rx   c                 C   s   | \}}|\}}|\}}	|\}
}|
}|}|d |
 }|	d | }|| d | }||	 d | }||
 | | }|| |	 | }||f||f||f||ffS r   r+   )rN   rO   rp   rL   rZ   r[   r\   r]   r^   r_   rg   rh   rQ   r   r   r   r   r   r   r   r+   r+   r,   r     s    r   c                 C   s8   | d d|  |d |  | d d|  |d |  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   rB   r+   )r&   r'   rX   r+   r+   r,   r   *  s    
c                 C   s   d| d|  | d  dd|  | |d   || |d   }d| d|  | d  dd|  | |d   || |d   }||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.
    rB   r   rA   r+   )r&   r'   r(   rX   rF   yr+   r+   r,   r   7  s    
@@c                 C   s   d| d|  d|  | d  dd|  d|  | |d   dd|  | | |d   || | |d   }d| d|  d|  | d  dd|  d|  | |d   dd|  | | |d   || | |d   }||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.
    rB   r   r-   r+   )r&   r'   r(   r)   rX   rF   r   r+   r+   r,   r   F  s"    c                 C   sZ   t | dkrt| |f S t | dkr4t| |f S t | dkrNt| |f S tdd S NrA   r-      Unknown curve degree)rw   r   r   r   
ValueError)segrX   r+   r+   r,   r   _  s    c           	      C   sx   | \}}|\}}|\}}t || tk r<t || tk r<dS t || t || krd|| ||  S || ||  S d S )Nr   )	ser   ZsxZsyexZeyZpxpyr+   r+   r,   _line_t_of_ptn  s     r   c                 C   sR   | d |d  |d |d   }| d |d  |d |d   }|dkoN|dk S )Nr   rB   rI   r+   )rN   rO   originZxDiffZyDiffr+   r+   r,   '_both_points_are_on_same_side_of_origin|  s      r   c                 C   s  | \}}|\}}|\}}	|\}
}t ||
rHt ||rHt ||sHg S t |	|rpt ||rpt ||	spg S t ||
rt |	|rg S t ||rt ||rg S t ||r|}||	 |
|  }|||  |	 }||f}t|t| ||t|||dgS t ||
r\|}|| ||  }|||  | }||f}t|t| ||t|||dgS || ||  }||	 |
|  }t ||rg S || | ||  |	 ||  }|||  | }||f}t||| rt|||rt|t| ||t|||d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.45069111555824465, 0.5408153767394238)
    r   r	   r
   )rC   iscloser   r   r   )s1e1s2e2Zs1xZs1yZe1xZe1yZs2xZs2yZe2xZe2yrF   Zslope34r   r   Zslope12r+   r+   r,   r      s    





 
 
 
 
   
 
 
c                 C   sT   | d }| d }t |d |d  |d |d  }t| |d  |d  S )Nr   r   rB   )rC   atan2r   rotate	translate)segmentstartendZangler+   r+   r,   _alignment_transformation  s    $r   c                 C   s   t || }t| dkrBt| \}}}t|d |d |d }nDt| dkr~t| \}}}}t|d |d |d |d }ntdtdd |D S )Nr-   rB   r   r   c                 s   s*   | ]"}d |  krdkrn q|V  qdS )rI   rB   Nr+   rW   rz   r+   r+   r,   rm     s
      
  z._curve_line_intersections_t.<locals>.<genexpr>)	r   ZtransformPointsrw   ra   r   ri   r   r   rn   )curvelineZaligned_curverN   rO   rp   intersectionsrL   r+   r+   r,   _curve_line_intersections_t  s     r   c                 C   s   t | dkrt}nt | dkr$t}ntdg }t| |D ]B}|| |f }t||f }t||f }|t|||d q:|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.9000930760723, 189.87306176459828)
    r-   r   r   r   )	rw   r   r   r   r   r   r   rb   r   )r   r   ZpointFinderr   rX   r   Zline_tr+   r+   r,   r!     s    c                 C   s4   t | dkrt|  S t | dkr(t|  S tdd S )Nr-   r   r   )rw   r   r   r   )rp   r+   r+   r,   _curve_bounds  s
    r   c                 C   sp   t | dkr0| \}}t|||}||f||fgS t | dkrJt| |f S t | dkrdt| |f S tdd S r   )rw   r   r   r   r   )rp   rX   r   r   midpointr+   r+   r,   _split_segment_at_t  s    r   MbP?c              	      sx  t | }t |}|sd}|s d}t||\}}|s6g S dd }	t| k rht| k rh|	||	|fgS t| d\}
}|d |	|f}|	||d f}t|d\}}|d |	|f}|	||d f}g }|t|
| ||d |t|| ||d |t|
| ||d |t|| ||d  fdd	}t }g }|D ]0}||}||kr\qB|| || qB|S )
N)rI   r9   c                 S   s   d| d | d   S )Nr.   r   rB   r+   )rr+   r+   r,   r   1  s    z._curve_curve_intersections_t.<locals>.midpointr.   r   rB   )range1range2c                    s    t | d   t | d   fS )Nr   rB   )int)rs   	precisionr+   r,   <lambda>V      z._curve_curve_intersections_t.<locals>.<lambda>)	r   r   r   r   extend_curve_curve_intersections_tsetaddrb   )curve1curve2r   r   r   Zbounds1Zbounds2Z
intersects_r   Zc11Zc12Z	c11_rangeZ	c12_rangeZc21Zc22Z	c21_rangeZ	c22_rangefoundZ
unique_keyseenZunique_valuesrs   keyr+   r   r,   r   !  s                    

r   c                    s   t  |} fd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                    s,   g | ]$}t t |d  |d  |d dqS )r   rB   r   )r   r   )rW   rs   r   r+   r,   r`   z  s   z+curveCurveIntersections.<locals>.<listcomp>)r   )r   r   Zintersection_tsr+   r   r,   r"   d  s    

c                 C   s   d}t |t | kr"| | }} d}t | dkrRt |dkrFt| |}qt| |}n.t | dkrxt |dkrxt| | }ntd|s|S d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.9000930760723, 189.87306176459828)

    FTrA   z4Couldn't work out which intersection function to usec                 S   s    g | ]}t |j|j|jd qS )r   )r   r   r
   r	   r   r+   r+   r,   r`     s     z/segmentSegmentIntersections.<locals>.<listcomp>)rw   r"   r!   r    r   )Zseg1Zseg2Zswappedr   r+   r+   r,   r#     s    
c                 C   sF   zt | }W n tk
r(   d|   Y S X dddd |D  S dS )zw
    >>> _segmentrepr([1, [2, 3], [], [[2, [3, 4], [0.1, 2.2]]]])
    '(1, (2, 3), (), ((2, (3, 4), (0.1, 2.2))))'
    z%gz(%s)z, c                 s   s   | ]}t |V  qd S r;   )_segmentrepr)rW   rF   r+   r+   r,   rm     s     z_segmentrepr.<locals>.<genexpr>N)iter	TypeErrorjoin)objitr+   r+   r,   r     s
    r   c                 C   s   | D ]}t t| qdS )zlHelper for the doctests, displaying each segment in a list of
    segments on a single line as a tuple.
    N)printr   )ry   r   r+   r+   r,   printSegments  s    r   __main__)r$   )r$   )r   NN)D__doc__ZfontTools.misc.arrayToolsr   r   r   ZfontTools.misc.transformr   rC   collectionsr   r   __all__r   r3   r6   r   r   rJ   r@   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   ro   rr   rD   r   r   r   r   r   ra   ri   rx   r   r   r   r   r   r   r   r    r   r   r!   r   r   r   r"   r#   r   r   __name__sysdoctestexittestmodfailedr+   r+   r+   r,   <module>   s   
	
# !"$&9-%'!aN
&     
C0
