U
    æ‹Ñhî¯  ã                   @   s¤  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	 zddl
Z
W n$ eefk
rh   ddlm
Z
 Y nX e
jZd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$d%d&d'gZd‹d)d„Zd*d+„ Ze
 e
j¡e
je
je
je
je
jd,�e
je
je
je
jd-�d.d/„ ƒƒƒZe
 e
j¡e
je
je
je
je
jd0�e
je
je
jd1�dŒd2d„ƒƒƒZd3Zd4Ze
je
je
 e
j¡e
je
je
jd5�d6d7„ ƒƒƒƒZe
je
je
 e
j¡e
je
jd8�d9d:„ ƒƒƒƒZ d;d„ Z!e
 e
j¡e
je
je
je
je
je
je
je
jd<�e
je
je
je
je
je
je
je
jd=�d>d„ ƒƒƒZ"d?d„ Z#e
 e
j¡e
je
je
je
jd@�e
je
je
je
jdA�dBd„ ƒƒƒZ$dCd„ Z%dDd„ Z&e
 e
j¡e
je
je
je
je
jd0�e
je
je
je
je
je
jdE�dFd„ ƒƒƒZ'dGd„ Z(dHd„ Z)dId„ Z*dJd„ Z+dKd„ Z,dLd„ Z-e
je
je
je
je
je
je
je
je
jdM�dNd„ ƒZ.e
 e
j¡e
je
je
je
je
je
je
je
je
jdO�e
je
je
je
je
jdP�dQd„ ƒƒƒZ/dRdS„ Z0dTdU„ Z1e
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dV�dWdX„ ƒZ2ddYlm3Z3m4Z4m5Z5m6Z6 e3fdZd„Z7d[d„ Z8d\d]„ Z9d^d_„ Z:e
je
je
je
je
je
je
je
je
je
jd`�dadb„ ƒƒƒZ;dcdd„ Z<dedf„ Z=e
je
je
je
je
je
je
je
je
je
jdg�dhdi„ ƒƒƒZ>djd"„ Z?dkd„ Z@dld „ ZAe
 e
j¡e
je
je
je
je
je
jdm�e
je
je
je
jdn�dod!„ ƒƒƒZBdpd#„ ZCdqdr„ ZDdsdt„ ZEdud$„ ZFdvdw„ ZGdxdy„ ZHdzd%„ ZId{d|„ ZJd}d~„ ZKd�d€d�„ZLd‚dƒ„ ZMd„d&„ ZNd…d'„ ZOd†d‡„ ZPdˆd‰„ ZQeRdŠk�r ddlSZSddlTZTeS UeT V¡ jW¡ dS )ŽzNfontTools.misc.bezierTools.py -- tools for working with Bezier path segments.
é    )Ú
calcBoundsÚsectRectÚrectArea)ÚIdentityN)Ú
namedtuple)Úcythong•Ö&è.>ÚIntersectionÚptÚt1Út2ÚapproximateCubicArcLengthÚapproximateCubicArcLengthCÚapproximateQuadraticArcLengthÚapproximateQuadraticArcLengthCÚcalcCubicArcLengthÚcalcCubicArcLengthCÚcalcQuadraticArcLengthÚcalcQuadraticArcLengthCÚcalcCubicBoundsÚcalcQuadraticBoundsÚ	splitLineÚsplitQuadraticÚ
splitCubicÚsplitQuadraticAtTÚsplitCubicAtTÚsplitCubicAtTCÚsplitCubicIntoTwoAtTCÚsolveQuadraticÚ
solveCubicÚquadraticPointAtTÚcubicPointAtTÚcubicPointAtTCÚlinePointAtTÚsegmentPointAtTÚlineLineIntersectionsÚcurveLineIntersectionsÚcurveCurveIntersectionsÚsegmentSegmentIntersectionsç{®Gázt?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/   úH/tmp/user/2202/pip-unpacked-wheel-pq9wd1e4/fontTools/misc/bezierTools.pyr   8   s        ÿc                 C   s\   | d||   | d }|| | |  d }| | | d || |f||| || d |ffS )Né   g      À?ç      à?r/   )Úp0Úp1Úp2Úp3ZmidZderiv3r/   r/   r0   Ú_split_cubic_into_twoK   s
    þr7   )r3   r4   r5   r6   )ÚmultÚarchÚboxc           	      C   s~   t || ƒ}t || ƒt || ƒ t || ƒ }||  t |krL|| d S t||||ƒ\}}t| f|žŽ t| f|žŽ  S d S ©Nr2   )ÚabsÚEPSILONr7   Ú_calcCubicArcLengthCRecurse)	r8   r3   r4   r5   r6   r9   r:   ZoneZtwor/   r/   r0   r>   T   s    	$ÿÿr>   ©r*   r+   r,   r-   )r.   r8   c                 C   s   dd|  }t || |||ƒS )zôCalculates 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.   r8   r/   r/   r0   r   h   s    é   g»½×Ùß|Û=©Úv1Úv2c                 C   s   | |  ¡  jS ©N)Ú	conjugateÚrealrB   r/   r/   r0   Ú_dot…   s    rH   ©Úxc                 C   s(   | t  | d d ¡ d t  | ¡d  S )Né   é   )ÚmathÚsqrtÚasinhrI   r/   r/   r0   Ú_intSecAtan�   s    rP   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/   r0   r   —   s     )r*   r+   r,   Úd0Úd1ÚdÚn)ÚscaleÚorigDistÚaÚbÚx0Úx1ÚLenc                 C   sÞ   ||  }|| }|| }|d }t |ƒ}|dkr<t ||  ƒS t||ƒ}t |ƒtk r–t||ƒ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   rK   )r<   rH   ÚepsilonrP   )r*   r+   r,   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r/   r/   r0   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)   rQ   r/   r/   r0   r   í   s    rQ   )Úv0rC   rD   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Ì”xùbŒß¿g¾ðb�ŠÛ?gF�V¨W°?gÇqÇqÜ?gF�V¨W°¿gÌ”xùbŒß?©r<   )r*   r+   r,   r_   rC   rD   r/   r/   r0   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   rL   r/   ©Ú.0Út©ÚaxÚayÚbxÚbyÚcxÚcyr/   r0   Ú
<listcomp>D  s
    
 þz'calcQuadraticBounds.<locals>.<listcomp>)ÚcalcQuadraticParametersÚappendr   )r*   r+   r,   Zax2Zay2ÚrootsÚpointsr/   rf   r0   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/   r0   r   L  s       ÿ)r_   rC   rD   Úv3Úv4c           	      C   s”   t ||  ƒd }t d|  d|  d|  d|  ƒ}t ||  | | ƒd }t d|  d|  d|  d|  ƒ}t || ƒd }|| | | | S )	z¹Approximates 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Ã?g�c’‰1ãá¿g8Ø5$t×Ô?guÁ|Yù¿Ê?gæâ#$ï˜?gÑ?gæâ#$ï˜¿g�c’‰1ãá?r`   )	r*   r+   r,   r-   r_   rC   rD   rr   rs   r/   r/   r0   r   j  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
    ç      @ra   c                 S   s(   g | ] }d |  krdk rn q|‘qS rb   r/   rc   r/   r/   r0   rm   ´  s
      
  z#calcCubicBounds.<locals>.<listcomp>c                 S   s(   g | ] }d |  krdk rn q|‘qS rb   r/   rc   r/   r/   r0   rm   µ  s
      
  c                    s\   g | ]T}ˆ | | | ˆ| |  ˆ|  ˆ ˆ| | | ˆ| |  ˆ|  ˆ f‘qS r/   r/   rc   ©rg   rh   ri   rj   rk   rl   ÚdxÚdyr/   r0   rm   ¸  s   ý&&þ)ÚcalcCubicParametersr   r   )r*   r+   r,   r-   Zax3Zay3Zbx2Zby2ZxRootsZyRootsrp   rq   r/   ru   r0   r   œ  s    &ûúc                 C   s¨   | \}}|\}}|| }|| }	|}
|}||	f| }|dkrF| |fgS ||
|f|  | }d|  krndk ršn 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   rL   Nr/   )r*   r+   ÚwhereÚisHorizontalZpt1xZpt1yZpt2xZpt2yrg   rh   ri   rj   rX   re   ZmidPtr/   r/   r0   r   Â  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   rL   Nr/   rc   r/   r/   r0   Ú	<genexpr>"  s
      
  z!splitQuadratic.<locals>.<genexpr>)rn   r   ÚsortedÚ_splitQuadraticAtT)	r*   r+   r,   ry   rz   rX   rY   ÚcÚ	solutionsr/   r/   r0   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 r{   r/   rc   r/   r/   r0   r|   G  s
      
  zsplitCubic.<locals>.<genexpr>)rx   r   r}   Ú_splitCubicAtT)r*   r+   r,   r-   ry   rz   rX   rY   r   rT   r€   r/   r/   r0   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))
    )rn   r~   )r*   r+   r,   ÚtsrX   rY   r   r/   r/   r0   r   M  s    c           
      G   sb   t | |||ƒ\}}}}t||||f|žŽ }	| f|	d dd… ˜|	d< |	d dd… |f˜|	d< |	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   rL   Néÿÿÿÿ)rx   r�   )
r*   r+   r,   r-   r‚   rX   rY   r   rT   Úsplitr/   r/   r0   r   e  s
    )r*   r+   r,   r-   rX   rY   r   rT   c           	      g   s4   t | |||ƒ\}}}}t||||f|žŽ E dH  d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 complex numbers..
        *ts: Positions at which to split the curve.

    Yields:
        Curve segments (each curve segment being four complex numbers).
    N)ÚcalcCubicParametersCÚ_splitCubicAtTC)	r*   r+   r,   r-   r‚   rX   rY   r   rT   r/   r/   r0   r   „  s    )re   r*   r+   r,   r-   ÚpointAtTÚoff1Úoff2)r   Ú_1_tÚ_1_t_2Ú_2_t_1_tc                 C   sÀ   || }d| }|| }d| | }|| |  d|| | || |    || |  }	||  ||  ||  }
|| ||  ||  }| ||  |  }||| |  }| ||
|	f|	|||ffS )a  Split a cubic Bezier curve at t.

    Args:
        pt1,pt2,pt3,pt4: Control points of the Bezier as complex numbers.
        t: Position at which to split the curve.

    Returns:
        A tuple of two curve segments (each curve segment being four complex numbers).
    rL   rK   r1   r/   )r*   r+   r,   r-   re   r   rŠ   r‹   rŒ   r‡   rˆ   r‰   r/   r/   r0   r   œ  s    2ÿc                 G   s  t |ƒ}g }| dd¡ | d¡ | \}}|\}}|\}	}
tt|ƒd ƒD ]¾}|| }||d  }|| }|| }|| }|| }d| | | | }d| | | | }|| }|| ||  |	 }|| ||  |
 }t||f||f||fƒ\}}}| |||f¡ qJ|S )Nr   r]   r@   rL   rK   )ÚlistÚinsertro   ÚrangeÚlenÚcalcQuadraticPoints)rX   rY   r   r‚   Úsegmentsrg   rh   ri   rj   rk   rl   Úir
   r   ÚdeltaÚdelta_2Úa1xÚa1yÚb1xÚb1yÚt1_2Úc1xÚc1yr*   r+   r,   r/   r/   r0   r~   Ä  s,    
r~   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   r]   r@   rL   r1   rK   )r�   rŽ   ro   r�   r�   ÚcalcCubicPoints)"rX   rY   r   rT   r‚   r’   rg   rh   ri   rj   rk   rl   rv   rw   r“   r
   r   r”   r•   Údelta_3rš   Út1_3r–   r—   r˜   r™   r›   rœ   Zd1xZd1yr*   r+   r,   r-   r/   r/   r0   r�   ß  s@    
     ÿr�   )rX   rY   r   rT   r
   r   r”   r•   rŸ   Úa1Úb1Úc1rS   c                 g   sð   t |ƒ}| dd¡ | d¡ tt|ƒd ƒD ]¼}|| }||d  }|| }|| }	||	 }
|| }|| }| |
 }d|  | | |	 }d| | | d|  |  | }| | ||  ||  | }t||||ƒ\}}}}||||fV  q.d S r�   )r�   rŽ   ro   r�   r�   ÚcalcCubicPointsC)rX   rY   r   rT   r‚   r“   r
   r   r”   r•   rŸ   rš   r    r¡   r¢   r£   rS   r*   r+   r,   r-   r/   r/   r0   r†     s"    
 r†   )rN   Ú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!
    ç      @r]   ra   ©r<   r^   )rX   rY   r   rN   rp   ZDDZrDDr/   r/   r0   r   /  s    &c                 C   sŽ  t | ƒtk rt|||ƒS t| ƒ} ||  }||  }||  }|| d|  d }d| | | d| |  d|  d }|| }	|| | }
|	tk r”dn|	}	t |
ƒtk r¨dn|
}
|	|
 }|	dkrÞ|
dkrÞt| 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 �r¸t|| | d tƒ } }}n~|| tk �rèt|| 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
    rt   g      "@ra   g      ;@g      K@r   r]   r2   r@   g      ð¿g       Àr¨   çUUUUUUÕ?N)r<   r^   r   ÚfloatÚroundÚepsilonDigitsr¥   ÚmaxÚminrN   r¦   r§   r}   Úpow)rX   rY   r   rT   r¡   Za2Úa3ÚQÚRZR2ZQ3ZR2_Q3rJ   ÚthetaZrQ2Za1_3rZ   r[   Úx2r/   r/   r0   r   P  sT    &(
 





c                 C   s^   |\}}|\}}| \}}|| d }	|| d }
|| |	 }|| |
 }||f|	|
f||ffS )Nra   r/   )r*   r+   r,   rµ   Úy2Úx3Úy3rk   rl   ri   rj   rg   rh   r/   r/   r0   rn   ±  s    rn   c                 C   s”   |\}}|\}}|\}}	| \}
}||
 d }|| d }|| d | }|| d | }||
 | | }|	| | | }||f||f||f|
|ffS ©Nrt   r/   )r*   r+   r,   r-   rµ   r¶   r·   r¸   Úx4Úy4rv   rw   rk   rl   ri   rj   rg   rh   r/   r/   r0   rx   ¼  s    rx   )r*   r+   r,   r-   rX   rY   r   c                 C   s8   ||  d }|| d | }||  | | }|||| fS r¹   r/   )r*   r+   r,   r-   r   rY   rX   r/   r/   r0   r…   Ê  s    r…   c                 C   sf   | \}}|\}}|\}}|}	|}
|d | }|d | }|| | }|| | }|	|
f||f||ffS r;   r/   )rX   rY   r   rg   rh   ri   rj   rk   rl   r[   Úy1rµ   r¶   r·   r¸   r/   r/   r0   r‘   Ü  s    r‘   c                 C   sœ   | \}}|\}}|\}}	|\}
}|
}|}|d |
 }|	d | }|| d | }||	 d | }||
 | | }|| |	 | }||f||f||f||ffS r¹   r/   )rX   rY   r   rT   rg   rh   ri   rj   rk   rl   rv   rw   r[   r¼   rµ   r¶   r·   r¸   rº   r»   r/   r/   r0   rž   é  s    rž   ©rX   rY   r   rT   r5   r6   Zp4c                 C   s8   |d | }|| d | }| | | | }||||fS )Nrª   r/   r½   r/   r/   r0   r¤   ù  s    r¤   c                 C   s8   | d d|  |d |  | d d|  |d |  fS )zÖFinds 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   rL   r/   )r*   r+   re   r/   r/   r0   r"     s    
c                 C   sˆ   d| d|  | d  dd|  | |d   || |d   }d| d|  | d  dd|  | |d   || |d   }||fS )zèFinds 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.
    rL   r   rK   r/   )r*   r+   r,   re   rJ   Úyr/   r/   r0   r     s    
@@c           
      C   s¨   || }d| }|| }|| | d  d|| |d  || |d     || |d   }|| | d  d|| |d  || |d     || |d   }	||	fS )zéFinds 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.
    rL   r   r1   r/   )
r*   r+   r,   r-   re   r   rŠ   r‹   rJ   r¾   r/   r/   r0   r    ,  s     
"ÿþÿ"ÿþÿ)re   r*   r+   r,   r-   )r   rŠ   r‹   c                 C   sL   || }d| }|| }|| |  d|| | || |    || |  S )zõFinds the point at time `t` on a cubic curve.

    Args:
        pt1, pt2, pt3, pt4: Coordinates of the curve as complex numbers.
        t: The time along the curve.

    Returns:
        A complex number with the coordinates of the point.
    rL   r1   r/   )r*   r+   r,   r-   re   r   rŠ   r‹   r/   r/   r0   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 ©NrK   r1   é   úUnknown curve degree)r�   r"   r   r    Ú
ValueError)Úsegre   r/   r/   r0   r#   _  s    c           	      C   sx   | \}}|\}}|\}}t || ƒtk r<t || ƒtk r<dS t || ƒt || ƒkrd|| ||  S || ||  S d S )Nrƒ   r©   )	ÚsÚer	   ZsxZsyÚexZeyZpxÚpyr/   r/   r0   Ú_line_t_of_ptn  s     rÈ   c                 C   sR   | d |d  |d |d   }| d |d  |d |d   }|dkoN|dk S )Nr   rL   r]   r/   )rX   rY   ÚoriginZxDiffZyDiffr/   r/   r0   Ú'_both_points_are_on_same_side_of_origin|  s      rÊ   c                 C   s  | \}}|\}}|\}}	|\}
}t  ||
¡rHt  ||¡rHt  ||¡sHg S t  |	|¡rpt  ||¡rpt  ||	¡spg S t  ||
¡rŒt  |	|¡rŒg S t  ||¡r¨t  ||¡r¨g S t  ||¡�r|}||	 |
|  }|||  |	 }||f}t|t| ||ƒt|||ƒd�gS t  ||
¡�r\|}|| ||  }|||  | }||f}t|t| ||ƒt|||ƒd�gS || ||  }||	 |
|  }t  ||¡�rŽg 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   )rM   Úiscloser   rÈ   rÊ   )Ús1Úe1Ús2Úe2Zs1xZs1yZe1xZe1yZs2xZs2yZe2xZe2yrJ   Zslope34r¾   r	   Zslope12r/   r/   r0   r$   ‚  s‚    
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ 
 
ÿÿ 
 
ÿÿ   ÿ
þ 
 
ÿÿc                 C   sT   | d }| d }t  |d |d  |d |d  ¡}t | ¡ |d  |d  ¡S )Nr   rƒ   rL   )rM   Úatan2r   ÚrotateÚ	translate)ÚsegmentÚstartÚendZangler/   r/   r0   Ú_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 )Nr1   rL   rÀ   rÁ   c                 s   s*   | ]"}d |  krdkrn q|V  qdS )r]   rL   Nr/   ©rd   r“   r/   r/   r0   r|   ä  s
      
  z._curve_line_intersections_t.<locals>.<genexpr>)	r×   ÚtransformPointsr�   rn   r   rx   r   rÂ   r}   )ÚcurveÚlineZaligned_curverX   rY   r   ÚintersectionsrT   r/   r/   r0   Ú_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)
    r1   rÀ   rÁ   rË   )	r�   r   r    rÂ   rÝ   rÈ   r"   ro   r   )rÚ   rÛ   ZpointFinderrÜ   re   r	   Zline_tr/   r/   r0   r%   ç  s    c                 C   s4   t | ƒdkrt| Ž S t | ƒdkr(t| Ž S tdƒ‚d S )Nr1   rÀ   rÁ   )r�   r   r   rÂ   )r   r/   r/   r0   Ú_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¿   )r�   r"   r   r   rÂ   )r   re   rÄ   rÅ   Úmidpointr/   r/   r0   Ú_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)r]   r@   c                 S   s   d| d | d   S )Nr2   r   rL   r/   )Úrr/   r/   r0   rß   1  s    z._curve_curve_intersections_t.<locals>.midpointr2   r   rL   )Úrange1Úrange2c                    s    t | d ˆ  ƒt | d ˆ  ƒfS )Nr   rL   )Úint)r‚   ©Ú	precisionr/   r0   Ú<lambda>V  ó    z._curve_curve_intersections_t.<locals>.<lambda>)	rÞ   r   r   rà   ÚextendÚ_curve_curve_intersections_tÚsetÚaddro   )Ú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_valuesr‚   Úkeyr/   ræ   r0   rë   !  s‚        ÿÿ    ÿÿ    ÿÿ    ÿÿ

rë   c                 C   s    t | ƒ | ¡}tdd„ |D ƒƒS )Nc                 s   s   | ]}t  |d  d¡V  qdS )rL   r]   N)rM   rÌ   )rd   Úpr/   r/   r0   r|   f  s     z_is_linelike.<locals>.<genexpr>)r×   rÙ   Úall)rÔ   Z	maybeliner/   r/   r0   Ú_is_lineliked  s    rö   c                    s†   t ˆ ƒrHˆ d ˆ d f}t |ƒr<|d |d f}t||žŽ S t||ƒS n"t |ƒrj|d |d f}tˆ |ƒ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)
    r   rƒ   c                    s,   g | ]$}t tˆ |d  ƒ|d  |d d�‘qS )r   rL   rË   )r   r#   )rd   r‚   ©rî   r/   r0   rm   Š  s   ÿz+curveCurveIntersections.<locals>.<listcomp>)rö   r$   r%   rë   )rî   rï   Zline1Zline2Zintersection_tsr/   r÷   r0   r&   i  s    


þc                 C   s–   d}t |ƒt | ƒkr"| | }} d}t | ƒdkrRt |ƒdkrFt| |ƒ}q€t| |ƒ}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)

    FTrK   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/   r0   rm   ½  s     z/segmentSegmentIntersections.<locals>.<listcomp>)r�   r&   r%   r$   rÂ   )Zseg1Zseg2ZswappedrÜ   r/   r/   r0   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 rE   )Ú_segmentrepr)rd   rJ   r/   r/   r0   r|   Ê  s     z_segmentrepr.<locals>.<genexpr>N)ÚiterÚ	TypeErrorÚjoin)ÚobjÚitr/   r/   r0   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ø   )r’   rÔ   r/   r/   r0   ÚprintSegmentsÍ  s    rÿ   Ú__main__)r(   )r(   )rá   NN)XÚ__doc__ZfontTools.misc.arrayToolsr   r   r   ZfontTools.misc.transformr   rM   Úcollectionsr   r   ÚAttributeErrorÚImportErrorZfontTools.miscZcompiledZCOMPILEDr=   r   Ú__all__r   r7   ZreturnsÚdoubleÚlocalsr)   r>   r   r­   r^   ZcfuncÚinlinerH   rP   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r�   r†   rN   r¥   r¦   r§   r   r   rn   rx   r…   r‘   rž   r¤   r"   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/   r0   Ú<module>   sê  ä 
	
ü
üþ

#
ù	ù	 
ýý!"
üû$&9-%ø


ø
   ÿ#ó
!aù	ù	
ûN
&     ÿ
C'0
