
    BPc              
      F   d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZmZ ddlmZmZmZmZmZmZmZmZmZ ddlmZmZ ed	eeef         f         Zee eeeeef         eeeef         f         eeeeeeef         d
f         Z!edeeef         ef         Z"dZ# edddd          Z$ edddd          Z%ddddddZ&g dZ' G d d	          Z( G d de(          Z) G d d
          Z* G d d          Z+ G d  d!          Z, G d" d#          Z-d$ Z.d% Z/dS )&z.
Provide transformation parsing to extensions
    )annotationsN)Decimal)
cosradianssinsqrttanfabsatan2hypotpiisfinite)	overloadcastCallable	GeneratorIteratorTupleUnionOptionalList   )strargsKeyDictImmutableVector2d	TransformBoundingInterval)BoundingBoxDirectedLineSegmentr   r   Vector2dleftrightcenter_x)lrmtopbottomcenter_y)tbr&   tbbtlrrl)i  Z   r   ih     )r,   r-   r.   r/   roric                  p   e Zd ZdZdZdZ ed           Z ed           Ze	d             Z
e	d/d            Z
e	d             Z
dd	d
Z
e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d0dZd Zd Zd1d Zd! Zd2d%Z d& Z!	 	 d3d4d,Z"ed5d-            Z#ed.             Z$dS )6r   z@Represents an immutable element of 2-dimensional Euclidean space        c                    | j         S N)_xselfs    2/usr/share/inkscape/extensions/inkex/transforms.py<lambda>zImmutableVector2d.<lambda>T       dg     c                    | j         S r7   )_yr9   s    r;   r<   zImmutableVector2d.<lambda>U   r=   r>   c                    d S r7    r9   s    r;   __init__zImmutableVector2d.__init__W   	     	r>   Nc                    d S r7   rB   )r:   vfallbacks      r;   rC   zImmutableVector2d.__init__\   rD   r>   c                    d S r7   rB   r:   xys      r;   rC   zImmutableVector2d.__init__a   rD   r>   )rG   c                  	 t          |          dk    rd\  }}nmt          |          dk    r|                     |d                   \  }}n;t          |          dk    rt          t          |          \  }}nt	          d          nB# t          t
          f$ r.}|t	          d          |t          |          \  }}Y d }~nd }~ww xY wt          |          t          |          c| _        | _        d S )Nr   )r5   r5   r      ztoo many argumentsz)Cannot parse vector and no fallback given)	len_parsemapfloat
ValueError	TypeErrorr   r8   r@   )r:   rG   argsrJ   rK   errors         r;   rC   zImmutableVector2d.__init__f   s    	/4yyA~~11Ta{{47++11Ta5$''11 !5666I& 	/ 	/ 	/ !LMMSXX$X..DAqqqqqq	/ !88U1XXs   BB	 	C$CCc                   t          | t                    r| j        | j        }}nt          | t          t
          f          r,t          |           dk    rt          t          |           \  }}nyt          | t                    rE| 
                    d          dk    r,t          t          |                     d                    \  }}nt          dt          |                      ||fS )NrM   ,r   zCan't parse )
isinstancer   rJ   rK   tuplelistrN   rP   rQ   strcountsplitrR   repr)pointrJ   rK   s      r;   rO   zImmutableVector2d._parsev   s     e.// 	;7EGqAAt}-- 	;#e**//ue$$DAqqs## 	;C(8(8A(=(=uekk#..//DAqq9DKK99:::!tr>   c                t    t          |          }t          | j        |j        z   | j        |j        z             S r7   r    rJ   rK   r:   others     r;   __add__zImmutableVector2d.__add__   /    ($&57*:;;;r>   c                t    t          |          }t          | j        |j        z   | j        |j        z             S r7   ra   rb   s     r;   __radd__zImmutableVector2d.__radd__   re   r>   c                t    t          |          }t          | j        |j        z
  | j        |j        z
            S r7   ra   rb   s     r;   __sub__zImmutableVector2d.__sub__   re   r>   c                x    t          |          }t          | j         |j        z   | j         |j        z             S r7   ra   rb   s     r;   __rsub__zImmutableVector2d.__rsub__   s3    %')DF7UW+<===r>   c                :    t          | j         | j                   S r7   ra   r9   s    r;   __neg__zImmutableVector2d.__neg__   s    $&)))r>   c                6    t          | j        | j                  S r7   ra   r9   s    r;   __pos__zImmutableVector2d.__pos__   s    '''r>   c                v    t          | j        t          |          z  | j        t          |          z            S r7   r    rJ   rQ   rK   r:   factors     r;   __floordiv__zImmutableVector2d.__floordiv__   ,    v.v0FGGGr>   c                v    t          | j        t          |          z  | j        t          |          z            S r7   rq   rr   s     r;   __truediv__zImmutableVector2d.__truediv__   ru   r>   c                v    t          | j        t          |          z  | j        t          |          z            S r7   rq   rr   s     r;   __div__zImmutableVector2d.__div__   ru   r>   c                B    t          | j        |z  | j        |z            S r7   ra   rr   s     r;   __mul__zImmutableVector2d.__mul__       &999r>   c                    | j         S r7   )lengthr9   s    r;   __abs__zImmutableVector2d.__abs__   s
    {r>   c                B    t          | j        |z  | j        |z            S r7   ra   rr   s     r;   __rmul__zImmutableVector2d.__rmul__   r|   r>   c                ,    d| j         dd| j        ddS )Nz	Vector2d(.6g, )rJ   rK   r9   s    r;   __repr__zImmutableVector2d.__repr__   s%    64666666666r>   c                (    | j         dd| j        dS )Nr   r   r   r9   s    r;   __str__zImmutableVector2d.__str__   s     &,,,,,,,r>   returnGenerator[float, None, None]c              #  .   K   | j         V  | j        V  d S r7   r   r9   s    r;   __iter__zImmutableVector2d.__iter__          ffr>   c                    dS )NrM   rB   r9   s    r;   __len__zImmutableVector2d.__len__   s    qr>   c                *    | j         | j        f|         S r7   r   )r:   items     r;   __getitem__zImmutableVector2d.__getitem__   s    %%r>   Tuple[float, float]c                j    t          t          t          t          f         t          |                     S )z"A tuple of the vector's components)r   r   rQ   rY   r9   s    r;   to_tuplezImmutableVector2d.to_tuple   s!    E%,'t555r>   c                    | j         | j        fS )zNA tuple of the vector's magnitude and direction

        .. versionadded:: 1.1)r~   angler9   s    r;   to_polar_tuplez ImmutableVector2d.to_polar_tuple   s    
 {DJ&&r>   rc   
VectorLikerQ   c                ^    t          |          }| j        |j        z  | j        |j        z  z   S )zMultiply Vectors component-wisera   rb   s     r;   dotzImmutableVector2d.dot   s*    v$&57"222r>   c                ^    t          |          }| j        |j        z  | j        |j        z  z
  S )z_Z component of the cross product of the vectors extended into 3D

        .. versionadded:: 1.1ra   rb   s     r;   crosszImmutableVector2d.cross   s,    
 v$&57"222r>   h㈵>:0yE>+Union[VectorLike, str, Tuple[float, float]]rtolatolc                V    t          |          }| |z
  j        }||||j        z  z   k     S )zaChecks if two vectors are (almost) identical, up to both absolute and
        relative tolerance.)r    r~   )r:   rc   r   r   deltas        r;   is_closezImmutableVector2d.is_close   s2     %tel2233r>   c                F    t          |                     |                     S )z Returns the length of the vector)r   r   r9   s    r;   r~   zImmutableVector2d.length   s     DHHTNN###r>   c                f    | j         dk    r| j        dk    rdS t          | j        | j                   S )z\The angle of the vector when represented in polar coordinates

        .. versionadded:: 1.1r   N)rJ   rK   r   r9   s    r;   r   zImmutableVector2d.angle   s2     6Q;;46Q;;4TVTV$$$r>   r7   r   r   )r   r   )rc   r   r   rQ   )r   r   )rc   r   r   rQ   r   rQ   r   rQ   )r   rQ   )%__name__
__module____qualname____doc__r8   r@   propertyrJ   rK   r   rC   staticmethodrO   rd   rg   ri   rk   rm   ro   rt   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r~   r   rB   r>   r;   r   r   N   s       JJ	B	B%%&&A%%&&A  X    X   X (, . . . . .  
 
 \
< < <
< < <
< < <
> > >
* * *( ( (H H HH H HH H H: : :  : : :7 7 7- - -     & & &6 6 6 6' ' '3 3 3 3
3 3 3 	
4 
4 
4 
4 
4 $ $ $ X$ % % X% % %r>   c                      e Zd ZdZed             Zej        j        d             Zej	        j        d             Z	d Z
d Zd Zd Zd	 Zd
 Zed             Zed             Zd ZdS )r    z6Represents an element of 2-dimensional Euclidean spacec                    | dk    rt          dd          S |0t          | t          |          z  | t          |          z            S dS )zCreates a Vector2d from polar coordinates

        None is returned when theta is None and radius is not zero.

        .. versionadded:: 1.1
        r5   N)r    r   r   )radiusthetas     r;   
from_polarzVector2d.from_polar  sN     S==C%%%FSZZ/#e**1DEEEtr>   c                .    t          |          | _        d S r7   )rQ   r8   r:   values     r;   rJ   z
Vector2d.x       ,,r>   c                .    t          |          | _        d S r7   )rQ   r@   r   s     r;   rK   z
Vector2d.y  r   r>   c                x    t          |          }| xj        |j        z  c_        | xj        |j        z  c_        | S r7   ra   rb   s     r;   __iadd__zVector2d.__iadd__  6    %'%'r>   c                x    t          |          }| xj        |j        z  c_        | xj        |j        z  c_        | S r7   ra   rb   s     r;   __isub__zVector2d.__isub__#  r   r>   c                F    | xj         |z  c_         | xj        |z  c_        | S r7   r   rr   s     r;   __imul__zVector2d.__imul__*  '    &&r>   c                F    | xj         |z  c_         | xj        |z  c_        | S r7   r   rr   s     r;   __idiv__zVector2d.__idiv__0  r   r>   c                F    | xj         |z  c_         | xj        |z  c_        | S r7   r   rr   s     r;   __itruediv__zVector2d.__itruediv__6  r   r>   c                F    | xj         |z  c_         | xj        |z  c_        | S r7   r   rr   s     r;   __ifloordiv__zVector2d.__ifloordiv__<  r   r>   c                    d S r7   rB   rI   s      r;   assignzVector2d.assignB  rD   r>   c                    d S r7   rB   rb   s     r;   r   zVector2d.assignG  rD   r>   c                2    t          | \  | _        | _        | S )z#Assigns a different vector in placera   r:   rT   s     r;   r   zVector2d.assignL  s    !4r>   N)r   r   r   r   r   r   r   rJ   setterrK   r   r   r   r   r   r   r   r   rB   r>   r;   r    r       s       @@  \                       X   X    r>   r    c                  L   e Zd ZdZ ej        d          ZdZ	 	 d8dZd Z	 e
d           Z e
d           Z e
d	           Z e
d
           Z e
d           Z e
d           Zd ZeZed             Zed9d            Zed             Zd Zd Zed             Zed:d            Zd Zd;dZed              Zed!             Zed"             Zed#             Zed$             Zd% Zd& Zd' Zd( Zd<d*Zd<d+Zd<d,Zd- Zd. Z d=d0Z!d1 Z"d2 Z#d3 Z$d4 Z%d5 Z&d<d6Z'd7 Z(dS )>r   a  A transformation object which will always reduce to a matrix and can
    then be used in combination with other transformations for reducing
    finding a point and printing svg ready output.

    Use with svg transform attribute input:

      tr = Transform("scale(45, 32)")

    Use with triad matrix input (internal representation):

      tr = Transform(((1.0, 0.0, 0.0), (0.0, 1.0, 0.0)))

    Use with hexad matrix input (i.e. svg matrix(...)):

      tr = Transform((1.0, 0.0, 0.0, 1.0, 0.0, 0.0))

    Once you have a transformation you can operate tr * tr to compose,
    any of the above inputs are also valid operators for composing.
    z>(translate|scale|rotate|skewX|skewY|matrix)\s*\(([^)]*)\)\s*,?r   Nc                x    d | _         d| _        ||                     |            | j        di | || _         d S )N)      ?r5   r5   r5   r   r5   rB   )callbackmatrix_set_matrix
add_kwargs)r:   r   r   extras       r;   rC   zTransform.__init__j  sO     8V$$$  %    r>   c                   t          |t                    ri| j                            |                                          D ]:\  }} t          | d|                                z             t          |            ;dS t          |t                    r|j	        | _	        dS t          |t          t          f          rMt          |          dk    r9|d         }|d         }t          |t          t          f          rt          |t          t          f          rt          |          dk    rt          |          dk    rt          t          t          t          t          f         t          t!          t          |                              }t          t          t          t          t          f         t          t!          t          |                              }||f| _	        dS t#          d| d          t#          d| d          t          |t          t          f          rt          |          dk    rt          t$          t&          t                   t          t          t          t          t          t          t          f         f         |          }t          |d                   t          |d                   t          |d	                   f}t          |d                   t          |d                   t          |d
                   f}||f| _	        dS t          |t          t          f          s$t#          dt)          |          j                   t#          d| d          )zYParse a given string as an svg transformation instruction.

        .. versionadded:: 1.1add_rM   r   r      zMatrix 'z&' is not a valid transformation matrix         zInvalid transform type: N)rX   r[   TRMfindallstripgetattrlowerr   r   r   rY   rZ   rN   r   r   rQ   rP   rR   r   r   typer   )r:   r   funcvaluesrow1row2tmatrixs          r;   r   zTransform._set_matrixz  s   
 fc""  	X $ 0 0 @ @ G Gf4ftzz||344gfooFFFG G	** 	X -DKKK.. 	X3v;;!3C3C!9D!9D$.. :dUDM3R3R t99>>c$ii1nneUE&9 :E#eTBRBR<S<STTDeUE&9 :E#eTBRBR<S<STTD"&*DKKK$Q6QQQ   !MvMMM   u.. 	X3v;;!3C3Cd5k5ueUE)Q#RRS G '!*%%uWQZ'8'8%
:K:KLD'!*%%uWQZ'8'8%
:K:KLD*DKKKFT5M22 	XOV8MOOPPPVVVVWWWr>   c                (    | j         d         d         S )Nr   r   r9   s    r;   r<   zTransform.<lambda>      dk!nQ/ r>   c                (    | j         d         d         S )Nr   r   r   r9   s    r;   r<   zTransform.<lambda>  r   r>   c                (    | j         d         d         S )Nr   r   r   r9   s    r;   r<   zTransform.<lambda>  r   r>   c                (    | j         d         d         S )Nr   r   r9   s    r;   r<   zTransform.<lambda>  r   r>   c                (    | j         d         d         S )Nr   rM   r   r9   s    r;   r<   zTransform.<lambda>  r   r>   c                (    | j         d         d         S )Nr   rM   r   r9   s    r;   r<   zTransform.<lambda>  r   r>   c                F    |                      t                                 S r7   )__eq__r   r9   s    r;   __bool__zTransform.__bool__  s    ;;y{{++++r>   c                    d S r7   rB   )r:   as     r;   
add_matrixzTransform.add_matrix  rD   r>   r   rQ   r+   cdefr   c                    d S r7   rB   )r:   r   r+   r   r   r   r   s          r;   r   zTransform.add_matrix  s	     	r>   c                    d S r7   rB   )r:   r   r+   s      r;   r   zTransform.add_matrix  rD   r>   c                4   t          |          dk    r)|                     t          |d                              n[t          |          dk    st          |          dk    r#|                     t          |                     nt          d|           | S )z0Add matrix in order they appear in the svg hexadr   r   rM   r   zInvalid number of arguments )rN   __imatmul__r   rR   r   s     r;   r   zTransform.add_matrix  s    t99>>YtAw//0000YY!^^s4yyA~~Yt__----BDBBCCCr>   c                    t          t          |                                                    D ]@\  }}t          | d|z             }t	          |t
                    r ||  3| ||           A| S )z@Add translations, scales, rotations etc using key word argumentsr   )reversedrZ   itemsr   rX   rY   )r:   kwargskeyr   r   s        r;   r   zTransform.add_kwargs  sx    "4#7#788 	 	JC4#..D%'' e"Ur>   c                    d S r7   rB   )r:   drs     r;   add_translatezTransform.add_translate  rD   r>   r5   c                    d S r7   rB   )r:   tr_xtr_ys      r;   r  zTransform.add_translate  rD   r>   c                    t          |          dk    r-t          |d         t          t          f          r|d         d}}nt	          | \  }}|                     dd|fdd|ff           | S )z$Add translate to this transformationr   r   r5   r   )rN   rX   intrQ   r    r   )r:   rT   r  r	  s       r;   r  zTransform.add_translate  so    t99>>ja3,??>a#$DD!4JD$3T*S#t,<=>>>r>   c                L    ||n|}|                      |ddfd|dff           | S )z Add scale to this transformationNr5   )r   )r:   sc_xsc_ys      r;   	add_scalezTransform.add_scale  s;    |tt4c*S$,<=>>>r>   c                    d S r7   rB   )r:   degcenters      r;   
add_rotatezTransform.add_rotate  rD   r>   c                    d S r7   rB   )r:   r  r#   r)   s       r;   r  zTransform.add_rotate  rD   r>   c                    d S r7   rB   r:   r  s     r;   r  zTransform.add_rotate  rD   r>   c                    d S r7   rB   )r:   r  r   s      r;   r  zTransform.add_rotate  rD   r>   c                    d S r7   rB   )r:   r  r   r+   s       r;   r  zTransform.add_rotate  rD   r>   c                   t          | \  }}t          t          |                    t          t          |                    }}|                     || |f|||ff           |                     dd| fdd| ff           | S )z#Add rotation to this transformationr   r5   )r    r   r   r   r   )r:   r  rT   r#   r)   _cos_sins          r;   r  zTransform.add_rotate	  s    %t_(&&GCLL(9(9d4$1D$3IJKKK3hY/#sXI1FGHHHr>   c                n    |                      dt          t          |                    dfdf           | S )z!Add skew x to this transformationr   r5   r   r   r	   r   r  s     r;   	add_skewxzTransform.add_skewx  s7     	3GCLL 1 137IJJJr>   c                n    |                      dt          t          |                    ddff           | S )z!Add skew y to this transformationr   r   r5   r  r  s     r;   	add_skewyzTransform.add_skewy  s7     	/C,=,=sC+HIJJJr>   c                2    d t          | j         D             S )z5Returns the transform as a hexad matrix (used in svg)c              3  $   K   | ]}|D ]}|V  d S r7   rB   ).0lstvals      r;   	<genexpr>z%Transform.to_hexad.<locals>.<genexpr>   s/      @@C@@S@@@@@@@r>   )zipr   r9   s    r;   to_hexadzTransform.to_hexad  s      A@sDK0@@@@r>   Fc                    |s| j         nd}t          | j        dz
            |k    oJt          | j        dz
            |k    o/t          | j                  |k    ot          | j                  |k    S )z5Returns True if this transformation is ONLY translater5   r   )absolute_tolerancer
   r   absr   r+   r   r:   exactlytols      r;   is_translatezTransform.is_translate"  sv     .5=d%%#!# $DFQJ3&$TV#$ TV#		
r>   c                    |s| j         nd}t          | j                  |k    oGt          | j                  |k    o/t          | j                  |k    ot          | j                  |k    S )z1Returns True if this transformation is ONLY scaler5   )r*  r
   r   r   r+   r   r,  s      r;   is_scalezTransform.is_scale-  sl     .5=d%%#LLC $TV#$TV#$ TV#		
r>   c                    |s| j         nd}|                     |          oXt          | j                  |k    o@t          | j                  |k    o(t          | j        dz  | j        dz  z   dz
            |k    S )z2Returns True if this transformation is ONLY rotater5   r-  rM   r   )r*  _is_URTr
   r   r   r   r+   r,  s      r;   	is_rotatezTransform.is_rotate8  s     .5=d%%#LLL)) 7TV#7TV#7 TVQY*Q.//36		
r>   c                    |                      d          st          d          t          | j        | j                  dz  t
          z  S )z/Return the amount of rotation in this transformFr3  zGRotation angle is undefined for non-uniformly scaled or skewed matricesr1   )r4  rR   r   r+   r   r   r9   s    r;   rotation_degreeszTransform.rotation_degreesC  sQ     ||E|** 	   TVTV$$s*R//r>   c                   t          |                                           }|                                 r| sdS d| j        dd| j        ddS |                                 rd| j        dd| j        ddS |                                 rd| 	                                ddS dd	
                    d
 |D                        dS )z<Format the given matrix into a string representation for svg z
translate(r   r   r   zscale(zrotate(zmatrix( c              3     K   | ]}|d V  	dS r   NrB   r#  vars     r;   r&  z$Transform.__str__.<locals>.<genexpr>Y  s&      !@!@3S,,!@!@!@!@!@!@r>   )rY   r(  r/  r   r   r1  r   r   r5  r7  join)r:   hexads     r;   r   zTransform.__str__M  s     dmmoo&& 	< r;;;;df;;;;;==?? 	87DF777$&77777>> 	<;T2244;;;;;C!@!@%!@!@!@@@CCCCr>   r[   c           	         t          |           j         dd                    d | j        d         D                        dd                    d | j        d         D                        dS )	z$String representation of this objectz(((r   c              3     K   | ]}|d V  	dS r<  rB   r=  s     r;   r&  z%Transform.__repr__.<locals>.<genexpr>_  &      AA3S,,AAAAAAr>   r   ), (c              3     K   | ]}|d V  	dS r<  rB   r=  s     r;   r&  z%Transform.__repr__.<locals>.<genexpr>`  rC  r>   r   z))))r   r   r?  r   r9   s    r;   r   zTransform.__repr__[  s     Dzz" G G		AA$+a.AAAAAG G		AA$+a.AAAAAG G G	
r>   c           	         t          |t          t          t          t          f          r[t           fdt                                           t	          |                                                    D                       }nd}|S )z8Test if this transformation is equal to the given matrixc              3  T   K   | ]"\  }}t          ||z
            j        k    V  #d S r7   )r
   r*  )r#  r$   r%   r:   s      r;   r&  z#Transform.__eq__.<locals>.<genexpr>h  sM        Aq QUt66     r>   F)rX   r[   rY   rZ   r   allr'  r(  )r:   r   r%  s   `  r;   r   zTransform.__eq__c  s     fsE4;<< 	    61B1B1K1K1M1MNN    CC
 C
r>   c           
        t          |          }t          | j        |j        z  | j        |j        z  z   | j        |j        z  | j        |j        z  z   | j        |j        z  | j        |j        z  z   | j        |j        z  | j        |j        z  z   | j        |j        z  | j        |j        z  z   | j        z   | j        |j        z  | j        |j        z  z   | j        z   f          S )z>Combine this transform's internal matrix with the given matrix)r   r   r   r+   r   r   r   )r:   r   rc   s      r;   
__matmul__zTransform.__matmul__p  s     &!! 46EG#33 46EG#33 46EG#33 46EG#33 46EG#33df< 46EG#33df<	
 	
 		
r>   c                \    | |z  j         | _         | j        |                     |            | S )z-In place multiplication of transform matrices)r   r   )r:   r   s     r;   r   zTransform.__imatmul__  s1     f},=$MM$r>   c                   | j         | j        z  | j        | j        z  z
  }| j        |z  }| j         |z  }| j         |z  }| j         |z  }|| j        z  || j        z  z    }|| j        z  || j        z  z    }t          ||||||f          S )z"Returns an inverted transformation)r   r   r   r+   r   r   r   )r:   detnew_anew_dnew_cnew_bnew_enew_fs           r;   rm   zTransform.__neg__  s     v46DF?3##$&.546>12$&.546>12%ueUCDDDr>   c                $   t          |t                    rt          d| d          t          |          }t          | j        |j        z  | j        |j        z  z   | j        z   | j	        |j        z  | j
        |j        z  z   | j        z             S )zTransform a tuple (X, Y)zWill not transform string '')rX   r[   rR   r    r   rJ   r   rK   r   r+   r   r   )r:   r_   s     r;   apply_to_pointzTransform.apply_to_point  s     eS!! 	EC5CCCDDDFUWtv//$&8FUWtv//$&8
 
 	
r>   c                    |s| j         nd}t          | j        | j        z
            |k    ot          | j        | j        z             |k    S )z
        Checks that transformation can be decomposed into product of
        Uniform scale (U), Rotation around origin (R) and translation (T)

        :return: decomposition as U*R*T is possible
        r5   )r*  r
   r   r   r+   r   r,  s      r;   r4  zTransform._is_URT  sK     .5=d%%#TVdf_%%,P43H3HC3OPr>   c                L    ddl m}  || |                              |          S )zKInterpolate with another Transform.

        .. versionadded:: 1.1
        r   )TransformInterpolator)tweenrY  interpolate)r:   rc   fractionrY  s       r;   r[  zTransform.interpolate  s7     	100000$$T511==hGGGr>   NN)r   rQ   r+   rQ   r   rQ   r   rQ   r   rQ   r   rQ   r   r   )r5   r7   )Fr   r[   ))r   r   r   r   recompiler   r*  rC   r   r   r   r+   r   r   r   r   r   __nonzero__r   r   r   r  r  r  r  r   r(  r/  r1  r5  r7  r   r   r   rJ  r   rm   rV  r4  r[  rB   r>   r;   r   r   R  sr        ( "*V
W
WC ! ! ! ! %X %X %XX 	//00A//00A//00A//00A//00A//00A, , , K  X    X
   X       X    X        X   X   X   X   X      A A A
	
 	
 	
 	
	
 	
 	
 	
	
 	
 	
 	
0 0 0D D D
 
 
 
  
 
 
"  E E E	
 	
 	
	Q 	Q 	Q 	QH H H H Hr>   c                     e Zd ZdZedd            Zed             Zed             Zed             Zd dZd ZeZd	 Zd
 Z	d Z
d Zd!dZd Zd Zd"dZd"dZd#dZd$dZd%dZd&dZed             Zed             ZdS )'r   z@A pair of numbers that represent the minimum and maximum values.Nc                    d S r7   rB   rb   s     r;   rC   zBoundingInterval.__init__  rD   r>   c                    d S r7   rB   )r:   pairs     r;   rC   zBoundingInterval.__init__  rD   r>   c                    d S r7   rB   r   s     r;   rC   zBoundingInterval.__init__  rD   r>   c                    d S r7   rB   rI   s      r;   rC   zBoundingInterval.__init__  rD   r>   c           
        |  |  |  |  |t          |t          t          t          f          rLt          |t          t          t          f          r*t          |          | _        t          |          | _        d S t          dt          ||f           dt          |          j	         dt          |          j	         d          |}|+t          d          t          d          c| _        | _        d S t          |t                    r|j        | _        |j        | _        d S t          |t          t          f          r>t          |          dk    r+t          |          t          |          c| _        | _        d S t          |t          t          t          f          rt          |          x| _        | _        d S t          dt          |           dt          |          j	         d          )NNot a number for scaling:  (rW   r   +inf-infrM   )rX   r  rQ   r   minimummaximumrR   r[   r   r   r   rY   rZ   rN   minmax)r:   rJ   rK   r   s       r;   rC   zBoundingInterval.__init__  s   **=!c5'233 	
C(9 9 	  %Qxx$Qxx ?aV ? ?Q(? ?+/77+;? ? ?   E}-26]]E&MM*dlllE#344 
$}$}EE4=11 c%jjAoo-0ZZU*dlllEC#899 .3Ell:t||| VUVVtE{{?SVVV  r>   c                R    t          | j                  ot          | j                  S r7   )r   rm  rn  r9   s    r;   r   zBoundingInterval.__bool__  s!    %%@(4<*@*@@r>   c                D    t          d| j        z  d| j        z  f          S )N)r   rn  rm  r9   s    r;   rm   zBoundingInterval.__neg__  s#    dl!2B4E FGGGr>   c                2    t          |           }|||z  }|S )zICalculate the bounding interval that covers both given bounding intervalsr   r:   rc   news      r;   rd   zBoundingInterval.__add__  %     t$$5LC
r>   c                    t          |          }t          | j        |j        f          | _        t          | j        |j        f          | _        | S r7   )r   ro  rm  rp  rn  rb   s     r;   r   zBoundingInterval.__iadd__  sD     ''DL%-899DL%-899r>   c                .    |t          |           S | |z   S r7   ru  rb   s     r;   rg   zBoundingInterval.__radd__
      =#D)))e|r>   rc   r   c                2    t          |           }|||z  }|S )zSCalculate the bounding interval where both given bounding intervals
        overlapru  rv  s      r;   __and__zBoundingInterval.__and__  rx  r>   c                   t          |          }t          | j        |j        f          | _        t          | j        |j        f          | _        | j        | j        k    r)t          d          t          d          c| _        | _        | S )Nrk  rl  )r   rp  rm  ro  rn  rQ   rb   s     r;   __iand__zBoundingInterval.__iand__  so     ''DL%-899DL%-899<$,&&).vf&DL$,r>   c                .    |t          |           S | |z  S r7   ru  rb   s     r;   __rand__zBoundingInterval.__rand__!  r{  r>   rQ   c                2    t          |           }|||z  }|S r7   ru  rv  s      r;   r{   zBoundingInterval.__mul__'  s#    t$$5LC
r>   c                F    | xj         |z  c_         | xj        |z  c_        | S r7   rm  rn  rb   s     r;   r   zBoundingInterval.__imul__-  s'    r>   r   c              #  .   K   | j         V  | j        V  d S r7   r  r9   s    r;   r   zBoundingInterval.__iter__2  s(      llr>   boolc                \    t          |           t          t          |                    k    S r7   )rY   r   rb   s     r;   r   zBoundingInterval.__eq__6  s$    T{{e$4U$;$;<<<<r>   r   c                6    | j         |cxk    o
| j        k    nc S r7   r  r   s     r;   __contains__zBoundingInterval.__contains__9  s(    |u444444444r>   r[   c                (    d| j          d| j         dS )NzBoundingInterval(r   r   r  r9   s    r;   r   zBoundingInterval.__repr__<  s    B4<BB4<BBBBr>   c                6    | j         | j        | j         z
  dz  z   S )zPick the middle of the linerM   r  r9   s    r;   r  zBoundingInterval.center?  s     |t| ;q@AAr>   c                     | j         | j        z
  S )z.Return the size difference minimum and maximum)rn  rm  r9   s    r;   sizezBoundingInterval.sizeE  s     |dl**r>   r7   r]  )rc   r   r   r   )rc   rQ   r   r   r   )r   r  )r   rQ   r   r  r^  )r   r   r   r   r   rC   r   ra  rm   rd   r   rg   r}  r  r  r{   r   r   r   r  r   r   r  r  rB   r>   r;   r   r     s       JJ   X   X   X   X       DA A A KH H H                   
   = = = =5 5 5 5C C C C B B XB
 + + X+ + +r>   c                     e Zd ZdZ ed           Z ed           Z ed           Z ed           Z ed           Z	 ed           Z
 ed           Z ed	           Zed4d            Zed             Zd5dZed6d            Zd ZeZd Zd Zd Zd Zd Zd7dZd Zd Zd  Zd! Zd8d#Zed$             Zed%             Zed&             Z d' Z!ed(             Z"ed)             Z#d9d+Z$e	 	 d9d:d/            Z%d4d;d3Z&d
S )<r   z
    Some functions to compute a rough bbox of a given list of objects.

    BoundingBox(other)
    BoundingBox(x, y)
    BoundingBox((x1, x2), (y1, y2))
    c                    | j         j        S r7   )rJ   r  r9   s    r;   r<   zBoundingBox.<lambda>U  s
    $&+ r>   c                    | j         j        S r7   )rK   r  r9   s    r;   r<   zBoundingBox.<lambda>V  s
    46; r>   c                    | j         j        S r7   )rK   rm  r9   s    r;   r<   zBoundingBox.<lambda>W  s
     r>   c                    | j         j        S r7   )rJ   rm  r9   s    r;   r<   zBoundingBox.<lambda>X  s
     r>   c                    | j         j        S r7   )rK   rn  r9   s    r;   r<   zBoundingBox.<lambda>Y  s
    46> r>   c                    | j         j        S r7   )rJ   rn  r9   s    r;   r<   zBoundingBox.<lambda>Z  s
    $&. r>   c                    | j         j        S r7   )rJ   r  r9   s    r;   r<   zBoundingBox.<lambda>[  
    TV] r>   c                    | j         j        S r7   )rK   r  r9   s    r;   r<   zBoundingBox.<lambda>\  r  r>   Nc                    d S r7   rB   rb   s     r;   rC   zBoundingBox.__init__^  rD   r>   c                    d S r7   rB   rI   s      r;   rC   zBoundingBox.__init__c  rD   r>   c                   |\|nYt          |t                    r|j        |j        }}n5t	          dt          |           dt          |          j         d          t          |          | _        t          |          | _        d S )Nri  rj  r   )	rX   r   rJ   rK   rR   r[   r   r   r   rI   s      r;   rC   zBoundingBox.__init__h  s    9yA{++ sAC1 NQNN477;KNNN   "!$$!!$$r>   rJ   rQ   rK   widthheightr   c                6    t          | | |z   f|||z   f          S )zQCreate a bounding box using x, y, width and height

        .. versionadded:: 1.2r   )rJ   rK   r  r  s       r;   new_xywhzBoundingBox.new_xywhv  s$    
 Aq5y>Aq6z?;;;r>   c                R    t          | j                  ot          | j                  S r7   )r  rJ   rK   r9   s    r;   r   zBoundingBox.__bool__}  s    DF||,TV,r>   c                :    t          | j         | j                   S r7   r   rJ   rK   r9   s    r;   rm   zBoundingBox.__neg__  s    DF7TVG,,,r>   c                H    t          |           }|t          |          z  }|S )z@Calculate the bounding box that covers both given bounding boxesr  rv  s      r;   rd   zBoundingBox.__add__  (     ${5!!!
r>   c                x    t          |          }| xj        |j        z  c_        | xj        |j        z  c_        | S r7   r  rb   s     r;   r   zBoundingBox.__iadd__  s8    E""%'%'r>   c                    | |z   S r7   rB   rb   s     r;   rg   zBoundingBox.__radd__      e|r>   c                H    t          |           }|t          |          z  }|S )zBCalculate the bounding box where both given bounding boxes overlapr  rv  s      r;   r}  zBoundingBox.__and__  r  r>   rc   Optional[BoundingBox]c                    t          |          }| j        |j        z  | _        | j        |j        z  | _        | j        r| j        s't                      t                      c| _        | _        | S r7   )r   rJ   rK   r   rb   s     r;   r  zBoundingBox.__iand__  se    E""%'!%'!v 	DTV 	D-//1A1C1CNDFDFr>   c                    | |z  S r7   rB   rb   s     r;   r  zBoundingBox.__rand__  r  r>   c                .    t          |           }||z  }|S r7   r  )r:   rs   rw  s      r;   r{   zBoundingBox.__mul__  s    $v
r>   c                F    | xj         |z  c_         | xj        |z  c_        | S r7   r   rr   s     r;   r   zBoundingBox.__imul__  r   r>   c                p    t          |t                    r t          |           t          |          k    S dS NF)rX   r   rY   rb   s     r;   r   zBoundingBox.__eq__  s0    e[)) 	/;;%,,..ur>   "Generator[BoundingBox, None, None]c              #  .   K   | j         V  | j        V  d S r7   r   r9   s    r;   r   zBoundingBox.__iter__  r   r>   c                     | j         | j        z  S )z>Return area of the bounding box

        .. versionadded:: 1.2)r  r  r9   s    r;   areazBoundingBox.area  s    
 zDK''r>   c                J    t          | j        j        | j        j                  S )zReturn the minimum x,y coords)r    rJ   rm  rK   r9   s    r;   rm  zBoundingBox.minimum       777r>   c                J    t          | j        j        | j        j                  S )zReturn the maximum x,y coords)r    rJ   rn  rK   r9   s    r;   rn  zBoundingBox.maximum  r  r>   c                \    dt          | j                   dt          | j                   dS )NzBoundingBox(rW   r   )rY   rJ   rK   r9   s    r;   r   zBoundingBox.__repr__  s+    >eDFmm>>eDFmm>>>>r>   c                J    t          | j        j        | j        j                  S )z&Returns the middle of the bounding box)r    rJ   r  rK   r9   s    r;   r  zBoundingBox.center  s     tv}555r>   c                J    t          | j        j        | j        j                  S )z_Returns a vector containing width and height of the bounding box

        .. versionadded:: 1.2)r    rJ   r  rK   r9   s    r;   r  zBoundingBox.size  s    
 TV[111r>   r   c                    |                      t          | t          |                   t          | t          |                   ||          S )z0Calls get_distance with the given anchor options)	directionselbox)anchor_distancer   XANYAN)r:   xanchoryanchorr  r  s        r;   
get_anchorzBoundingBox.get_anchor  sI     ##D#g,''D#g,''	 $ 
 
 	
r>   r  Union[int, str]r  c                   d}t          |t          t          f          rU|t          vr?t	          | |          t          t          |           t          ||           z
            z  S t          |         }|dv r;|t          d          t	          |j	        j
        | z
  |j        j
        |z
            }|| | |  || gt                              |                   S )a  Using the x,y returns a single sortable value based on direction and angle

        Args:
            x (float): input x coordinate
            y (float): input y coordinate
            direction (Union[int, str], optional): int/float (custom angle),
                tb/bt (top/bottom), lr/rl (left/right), ri/ro (radial). Defaults to 0.
            selbox (Optional[BoundingBox], optional): The bounding box of the whole
                selection for radial anchors. Defaults to None.

        Raises:
            ValueError: if radial distance is requested without the optional selbox
                parameter.

        Returns:
            float: the anchor distance with respect to the direction.
        r5   )r2   r3   Nz<Radial distance not available without selection bounding box)rX   r  rQ   CUSTOM_DIRECTIONr   r   r   r   rR   rJ   r  rK   	DIRECTIONindex)rJ   rK   r  r  rots        r;   r  zBoundingBox.anchor_distance  s    2 i#u.. 	4 000Q{{c'9**=*=a*K&L&LMM(3I$$~ R   !+VX_q-@AACA2q1"cC4()C)CDDr>   delta_xdelta_yOptional[float]c                    |p|}t          | j        j        |z
  | j        j        |z   f| j        j        |z
  | j        j        |z   f          S )zEnlarges / shrinks a bounding box by a constant value. If only delta_x
        is given, each side is moved by the same amount; if delta_y is given,
        different deltas are applied to horizontal and vertical intervals.

        .. versionadded:: 1.2)r   rJ   rm  rn  rK   )r:   r  r  s      r;   resizezBoundingBox.resize  sT     $WV^g%tv~'?@V^g%tv~'?@
 
 	
r>   r7   r]  )
rJ   rQ   rK   rQ   r  rQ   r  rQ   r   r   )rc   r  r   r   )r   r  )r   N)
rJ   rQ   rK   rQ   r  r  r  r  r   rQ   )r  rQ   r  r  r   r   )'r   r   r   r   r   r  r  r'   r!   r(   r"   r#   r)   r   rC   r   r  r   ra  rm   rd   r   rg   r}  r  r  r{   r   r   r   r  rm  rn  r   r  r  r  r  r  rB   r>   r;   r   r   L  s         H--..EX..//F
(..
/
/C8//00DX1122FH0011Ex2233Hx2233H   X   X% % % % < < < \<- - - K- - -                       ( ( X( 8 8 X8
 8 8 X8
? ? ? 6 6 X6
 2 2 X2
 
 
 
  &'(,	%E %E %E %E \%EN

 

 

 

 

 

 

r>   r   c                     e Zd ZdZ e            Z e            Z ed           Z ed           Z	 ed           Z
 ed           Z ed           Z ed           Zed             Zed	             Zed
             Zd Zd Zd Zed             Zed             Zed             Zd Zd Zd Zd Zd Zd Zd Zd ZdS )r   zP
    A directed line segment

    DirectedLineSegment(((x0, y0), (x1, y1)))
    c                    | j         j        S r7   )startrJ   r9   s    r;   r<   zDirectedLineSegment.<lambda>/  
    tz| r>   c                    | j         j        S r7   )r  rK   r9   s    r;   r<   zDirectedLineSegment.<lambda>0  r  r>   c                    | j         j        S r7   )endrJ   r9   s    r;   r<   zDirectedLineSegment.<lambda>1  
    txz r>   c                    | j         j        S r7   )r  rK   r9   s    r;   r<   zDirectedLineSegment.<lambda>2  r  r>   c                    | j         j        S r7   )vectorrJ   r9   s    r;   r<   zDirectedLineSegment.<lambda>3  
    t{} r>   c                    | j         j        S r7   )r  rK   r9   s    r;   r<   zDirectedLineSegment.<lambda>4  r  r>   c                    d S r7   rB   r9   s    r;   rC   zDirectedLineSegment.__init__6  rD   r>   c                    d S r7   rB   rb   s     r;   rC   zDirectedLineSegment.__init__;  rD   r>   c                    d S r7   rB   )r:   r  r  s      r;   rC   zDirectedLineSegment.__init__@  rD   r>   c                6   |st                      t                      }}nQt          |          dk    r|\  }|j        |j        }}n+t          |          dk    r|\  }}nt	          d|           t          |          | _        t          |          | _        d S )Nr   rM   z0DirectedLineSegment() can't be constructed from )r    rN   r  r  rR   )r:   rT   r  r  rc   s        r;   rC   zDirectedLineSegment.__init__E  s     	X!XZZ3EEYY!^^HUei3EEYY!^^JE33VPTVVWWWe__
C==r>   c                ~    t          |t          t          f          r t          |           t          |          k    S dS r  )rX   rY   r   rb   s     r;   r   zDirectedLineSegment.__eq__S  s6    ee%89:: 	/;;%,,..ur>   c              #  R   K   | j         V  | j        V  | j        V  | j        V  d S r7   )x0x1y0y1r9   s    r;   r   zDirectedLineSegment.__iter__Y  s6      ggggr>   c                     | j         | j        z
  S )zThe vector of the directed line segment.

        The vector of the directed line segment represents the length
        and direction of segment, but not the starting point.

        .. versionadded:: 1.1
        )r  r  r9   s    r;   r  zDirectedLineSegment.vector`  s     x$*$$r>   c                    | j         j        S )z"Get the length of the line segment)r  r~   r9   s    r;   r~   zDirectedLineSegment.lengthl  s     {!!r>   c                6    t          | j        | j                  S )z1Get the angle of the line created by this segment)r   dydxr9   s    r;   r   zDirectedLineSegment.angler  s     TWdg&&&r>   c                4   t          | j        ||f          }|                    |           }|dk    rt          ||f| j                  j        S |                     |           |k    rt          ||f| j                  j        S |                     ||          S )z*Get the distance to the given point (x, y)r   )r   r  r   r~   r  perp_distance)r:   rJ   rK   segment2dot2s        r;   distance_to_pointz%DirectedLineSegment.distance_to_pointx  s     'tzAq6::||D!!199&1vtz::AA88D>>T!!&1vtx88??!!!Q'''r>   c                    | j         dk    rdS t          | j        | j        |z
  z  | j        |z
  | j        z  z
            | j         z  S )z)Perpendicular distance to the given pointr   N)r~   r
   r  r  r  r  rI   s      r;   r  z!DirectedLineSegment.perp_distance  sJ     ;!4TW!,$'A+1HIJJT[XXr>   c                @    | j                             |j                   S )z1Get the dot product with the segment with another)r  r   rb   s     r;   r   zDirectedLineSegment.dot  s     {u|,,,r>   c                J    | j         || j        z  z   | j        || j        z  z   fS )z/Get the point at the given ratio along the line)r  r  r  r  )r:   ratios     r;   point_at_ratioz"DirectedLineSegment.point_at_ratio  s)     w($'EDGO*CCCr>   c                <    |                      || j        z            S )z*Get the point as the length along the line)r  r~   )r:   r~   s     r;   point_at_lengthz#DirectedLineSegment.point_at_length  s     ""6DK#7888r>   c                J    t          || j        z   || j        z   f||f          S )zCreate parallel Segment)r   r  r  rI   s      r;   parallelzDirectedLineSegment.parallel  s)     #AKTW#=1vFFFr>   c                   t          |          }| j                            |j                  }|j                            | j        |j        z
            }|dk    r%t	          |                     ||z                      S dS )z)Get the intersection between two segmentsr   N)r   r  r   r  r    r  )r:   rc   denomnums       r;   	intersectzDirectedLineSegment.intersect  st     $E**!!%,//l  ek!9::A::D//e<<===tr>   c                (    d| j          d| j         dS )NzDirectedLineSegment((rD  z)))r  r  r9   s    r;   r   zDirectedLineSegment.__repr__  s    CtzCCtxCCCCr>   N)r   r   r   r   r    r  r  r   r  r  r  r  r  r  r   rC   r   r   r  r~   r   r  r  r   r  r  r  r  r   rB   r>   r;   r   r   %  s         HJJE
(**C	++	,	,B	++	,	,B	))	*	*B	))	*	*B	,,	-	-B	,,	-	-B  X   X   X! ! !     	% 	% X	% " " X"
 ' ' X'
	( 	( 	(Y Y Y- - -
D D D
9 9 9
G G G
	 	 	D D D D Dr>   r   c                   
 d}t                     t                     c
 z
  }z
  }z
  }
 fd}t          |d|z  z
  |z             |k    re||z  ||z  k    rXt          ||z  ||z  z
            }	 |||z
  |	z   |d|z  z
  |z   z            \  
 |||z
  |	z
  |d|z  z
  |z   z            \  
n.t          ||z
            |k    r || d||z
  z  z            \  

fS )z<Returns the extreme value, given a set of bezier coordinates&.>c                    d| cxk     rdk     rjn ngd| z
  z  d| z
  z  d| z
  z  dz  | z  d| z
  z  d| z
  z  z   dz  | z  | z  d| z
  z  z   | z  | z  | z  z   }t          |          t          |          fS fS )Nr   r   r   ro  rp  )r_   pyxcmaxcminpy0py1py2py3s     r;   
_is_biggerz!cubic_extrema.<locals>._is_bigger  s    u====q=====q5y!QY/1u9=c'E/QY/1u9=>c'E/E)QY78 +%-.  tS>>3tS>>11Tzr>   rM   )ro  rp  r
   r   )r  r  r  r  r   pd1pd2pd3r  pdsr  r  s   ````      @@r;   cubic_extremar    sb    DS#CJD$
)C
)C
)C	 	 	 	 	 	 	 	 	 	 C!c'MC  4''9sSy  sSy39,--C#S3Y_q3w9L$MNNJD$#S3Y_q3w9L$MNNJD$	cCi4		ZS3Y 899
d:r>   c                     d}t                     t                     c fd}t           z   dz  z
            |k    r | z
   z   dz  z
  z            \  fS )zFReturns the extreme value, given a set of quadratic bezier coordinatesr  c                    d| cxk     rdk     rIn nFd| z
  z  d| z
  z  dz  | z  d| z
  z  z   | z  | z  z   }t          |          t          |          fS fS )Nr   r   rM   r   )r_   r  r  r  r  r  r  s     r;   r  z%quadratic_extrema.<locals>._is_bigger  s    u====q=====q5y!QY/c'E/QY/0+%& 
 tS>>3tS>>11Tzr>   rM   )ro  rp  r
   )r  r  r  r   r  r  r  s   ```  @@r;   quadratic_extremar    s     DS#CJD$         C#IC  4''ZssSy1s7/B CDD
d:r>   )0r   
__future__r   r_  decimalr   mathr   r   r   r   r	   r
   r   r   r   r   typingr   r   r   r   r   r   r   r   r   utilsr   r   rQ   r   r[   
MatrixLikeBoundingIntervalArgs__all__r  r  r  r  r   r    r   r   r   r   r  r  rB   r>   r;   <module>r     sX  0  # " " " " " 				       O O O O O O O O O O O O O O O O O O O O O O O O
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 $ # # # # # # # ue|,,
 	%ue#
$eE5%,?&@
@A	%ueU
23
 eUl+U2 
 gFz::;;gEz::;;4Dt$GG 000	n% n% n% n% n% n% n% n%bP P P P P  P P PfcH cH cH cH cH cH cH cHLQ+ Q+ Q+ Q+ Q+ Q+ Q+ Q+hV
 V
 V
 V
 V
 V
 V
 V
rFD FD FD FD FD FD FD FDR  B    r>   