
    BPc!                         d Z ddlZddlmZmZmZ ddlmZmZ ddlm	Z	  G d dej
                  Z G d d	e          ZdS )
a{  
This code defines a basic class (PathModifier) of effects whose purpose is
to somehow deform given objects: one common tasks for all such effect is to
convert shapes, groups, clones to paths. The class has several functions to
make this (more or less!) easy.
As an example, a second class (Diffeo) is derived from it,
to implement deformations of the form X=f(x,y), Y=g(x,y)...
    N)PathElementGroupUse)pointdistancebeziersplitatt)zSortc                       e Zd ZdZddZddZddZd Zed             Z	ed             Z
ed	             Zed
             Zedd            ZddZdS )PathModifierzSelect list manipulationTc           	         t          |                                          D ]\  }}t          |t          j                  r|j        }|D ]P}|r||j        z  |_        |                    |                     |                    d          |i                     Q|r|                    d          r|j	        d= |
                    |           |S )Nid	transform)listitems
isinstanceinkexr   r   updateexpand_groupsgetattribpop)selfelementstransferTransformnode_idnodematchilds          ./usr/share/inkscape/extensions/pathmodifier.pyr   zPathModifier.expand_groups*   s    !(.."2"233 
	& 
	&MGT$,, 	&n! R RE( @*-*?OOD$6$6		$7O$P$PQQQQ$ 1+)>)> 1K0W%%%    c                    t          |                                          D ]\  }}t          |t                    r.|                     ||           |                     |||           Ht          |t                    rj|                                }|                    |           |	                    d          }|
                    |                     ||i||                     |S )Nr   )r   r   r   r   r   expand_clonesr   unlinkr   r   r   )r   r   r   replacer   r   newnodenewids           r   r!   zPathModifier.expand_clones8   s    !(.."2"233 	 	MGT$&& ""8->???""8->HHHH D#&& ++--W%%%D))&&w'79JGTT   r   c                 &   t          |                                          D ]n}|                                }|r>|                    |           |                    d|                    d                     |||                    d          <   odS )z'Replace all non-paths with path objectsr   N)r   valuesto_path_elementreplace_withsetr   )r   r   r#   r   elems        r   objects_to_pathszPathModifier.objects_to_pathsH   s    **++ 	, 	,D''))D /!!$'''txx~~...'+HTXXd^^$$	, 	,r   c                      t          d          )Nz#overwrite this method in subclasses)
NotImplementedErrorr,   svg	selectionbounding_boxbboxfilterr   pathto_superpath)r   r   r4   s      r   effectzPathModifier.effectQ   s    !"GHHHr   c                 J   |r| t          |          z  } | dk    rd| |d         z  fS d}|t          |          k     r;||         | k    r/| ||         z  } |dz  }|t          |          k     r||         | k    /| |t          |t          |          dz
                     z  }||fS )a  
        Receives an arc length l, and returns the index of the segment in skelcomp
        containing the corresponding point, to gether with the position of the point on this segment.

        If the deformer is closed, do computations modulo the toal length.
        r      )sumlenmin)llengthsisclosedits        r   lengthtotimezPathModifier.lengthtotimeZ   s      	!CLL A66a'!*n$$3w<<gajAooOAFA 3w<<gajAoo As7||a/0011!tr   c                 X    | D ]&}|D ]!}|D ]}|d         }|d          |d<   | |d<   "'dS )z-Swaps x and y coordinate of all path verticesr   r8   N )r4   pathcompctlpttmps        r   flipxyzPathModifier.flipxym   sj      	! 	!H ! ! ! !BQ%CUFBqE DBqEE!!	! 	!r   c                 Z    | D ]'}|D ]"}|dxx         |z  cc<   |dxx         |z  cc<   #(dS )zShifts a subpath by (dx, dy)r   r8   NrC   )rD   dxdyrE   rF   s        r   offsetzPathModifier.offsetw   s]      	 	C  11	 	r   c                     | D ]G}|D ]B}|d         |d         |d         z
  |z  z   |d<   |d         |d         |d         z
  |z  z   |d<   CHdS )z>Stretches a subpath by (xscale, yscale) relative to origin orgr   r8   NrC   )rD   xscaleyscaleorgrE   rF   s         r   stretchzPathModifier.stretch   sy      	; 	;C ; ;A"Q%#a&.F!::1A"Q%#a&.F!::1;	; 	;r   MbP?c                    	
 d
d}d}g 	|t                     dz
  k     rt           |         d          |         d                   }|t           |         d          |dz            d                   z  }|t           |dz            d          |dz            d                   z  }t           |         d          |dz            d                   }||z
  |k    rt           |         d          |         d          |dz            d          |dz            d         gd          \  }}|d         \   |         d         d<    |         d         d<   |d         \   |dz            d         d<    |dz            d         d<                        |dz   |d         d         |d         d         g|d         d         |d         d         g|d         d         |d         d         gg           n"||z   dz  }	                    |           |dz  }|t                     dz
  k     	 
fdt          dt                     dz
            D             }|                     d         d                    
fd		D             	|	fS )
a  
        This function receives a component of a 'cubicsuperpath' and returns two things:
        The path subdivided in many straight segments, and an array containing the length of each segment.

        We could work with bezier path as well, but bezier arc lengths are (re)computed for each point
        in the deformed object. For complex paths, this might take a while.
        gư>r   r8      g      ?   c                 D    g | ]}|         k    |         d          S )r8   rC   ).0r?   r=   pzeros     r   
<listcomp>z*PathModifier.linearize.<locals>.<listcomp>   s/    JJJ1
T8I8IqtAw8I8I8Ir   c                      g | ]
}|k    |S rC   rC   )rW   r<   rY   s     r   rZ   z*PathModifier.linearize.<locals>.<listcomp>   s    222T1r   )r:   r   r   insertappendrange)rX   	tolerancer?   dboxchordb1b2newr=   rY   s   `        @@r   	linearizezPathModifier.linearize   s    #a&&1*nn!Q1a11C=1a!AE(1+666C=1q5!aAhqk:::C!!A$q'1QU8A;77Eey(('qT!Wad1gqQx{Aa!eHQK@# B *,A&!Q
AaDGAJ13A.!a%A!a%AEeAh1a)BqE!HbeAh+?"Q%(BqERSHAUV   
 5[A%q!!!Q% #a&&1*nn& KJJJJJaQ! 4 4JJJ

1R582222g222G|r   Fc                    | j         j                                        }|                                }|r|                                }|rC|                                |i}|                     |dd           |                     |           n|}|                     |dd           |                     |           ||                                fS )zKGets the pattern node and skeleton (with applied transforms) from selectionTF)	r/   r0   rendering_orderr   	duplicateget_idr!   r,   id_dict)r   expand_patternsrj   	skeletonsr+   patternss         r   get_patterns_and_skeletonsz'PathModifier.get_patterns_and_skeletons   s     H&6688	}} 	$>>##D 	t,Hxu555!!(++++H9dE222i(((**,,,,r   N)T)TT)rR   )FF)__name__
__module____qualname____doc__r   r!   r,   r6   staticmethodrA   rH   rL   rQ   rg   rp   rC   r   r   r
   r
   '   s       ""       , , , ,     \$ ! ! \!   \ ; ; \; " " " \"H- - - - - -r   r
   c                       e Zd ZddZd ZdS )DiffeorC   c                     |D ].}|dxx         |d         z  cc<   |dxx         |d         z  cc<   /|D ].}|dxx         |d         z  cc<   |dxx         |d         z  cc<   /d S )Nr   r8   rC   )r   bptvectsvs       r   applyDiffeozDiffeo.applyDiffeo   s      	 	AaDDDCFNDDDaDDDCFNDDDD  	 	AaDDDCFNDDDaDDDCFNDDDD	 	r   c           	         |                      | j        j        d           |                     | j        j        d           |                     | j        j        d           | j        j                                        | _        | j        j                            t                    D ]V}|j	        
                                }|D ]1}|D ],}|                     |d         |d         |d         f           -2||_	        Wd S )NTr8   r   rT   )r!   r/   r0   r   r,   r1   r2   r3   r   r4   r5   r|   )r   r   r4   subctlpts        r   r6   zDiffeo.effect   s   48-t44448-t444dh0$777H&3355	H&--k:: 	 	D9))++D E E  E EE$$U1Xa%(/CDDDDEDII	 	r   N)rC   )rq   rr   rs   r|   r6   rC   r   r   rw   rw      s7           ,
 
 
 
 
r   rw   )rt   r   r   r   r   inkex.bezierr   r   inkex.deprecatedr   EffectExtensionr
   rw   rC   r   r   <module>r      s   ,   ) ) ) ) ) ) ) ) ) ) 6 6 6 6 6 6 6 6 # " " " " "W- W- W- W- W-5( W- W- W-t! ! ! ! !\ ! ! ! ! !r   