
    BPc
*                     N   d Z ddlZddlZddlmZmZ ddlmZmZm	Z	 ddl
mZ ddlmZ ddlZddlmZmZmZmZmZmZmZmZ  G d d	e          Zd
ej        fdZd Zi g dg dgfdZddZ G d deej                  Z e!dk    r e             "                                 dS dS )z5
Simplifies SVG files in preparation for sif export.
    N)PIPEPopen)inkscape	write_svgProgramRunError)inkex_gettext)TempDirMixin)load_svgGroupPathElementShapeElementAnchorSwitchSvgDocumentElement	Transformc                       e Zd ZdZd Zd ZdS )MalformedSVGErrorzHRaised when the SVG document is invalid or contains unsupported featuresc                     || _         d S )N)value)selfr   s     0/usr/share/inkscape/extensions/synfig_prepare.py__init__zMalformedSVGError.__init__1   s    


    c                 J    t          d          t          | j                  z  S )Na  SVG document is invalid or contains unsupported features

Error message: %s

The SVG to Synfig converter is designed to handle SVG files that were created using Inkscape. Unsupported features are most likely to occur in SVG files written by other programs.
)_reprr   )r   s    r   __str__zMalformedSVGError.__str__4   s-      4:
	
r   N)__name__
__module____qualname____doc__r   r    r   r   r   r   .   s8        RR  
 
 
 
 
r   r   pathc                    t          j                    }g }t          |                                           D ]\  }}|j        dvr|                    |j                   )|dk    r"|                    |j                   |j        }Q|j        }|j        dv r9|j        |k    r(|                    t          j	        j
        |            |}||gz  }|                    t          j	        j
        |j                    |j        }||gz  }|ddd         D ](}|                    t          j	        j
        |            )|S )ad  Fuse subpaths of a path. Should only be used on unstroked paths.
    The idea is to replace every moveto by a lineto, and then walk all the extra lines
    backwards to get the same fill. For unfilled paths, this gives visually good results
    in cases with not-too-complex paths, i.e. no intersections.
    There may be extra zero-length Lineto commands.zZmMr   mMN)inkexPath	enumerateproxy_iteratorletterappendcommand	end_pointprevious_end_pointpathsLine)r#   resultreturn_stackisegfirst	return_topoints           r   fuse_subpathsr:   G   sN    Z\\FLD//1122 " "3:V##MM#+&&&&!VVMM#+&&&MEE .IzT!!)U22 MM%+"2E":;;; %I+MM%+*CM:;;;MEE UGL ddd# 0 0ek&.////Mr   c                 ,   t          t          j                            |                     dd                              }d|v r$|d         dk    rd|vs|d         dk    rddgS d| gS d|                                vs|d         dk    r| dgS t                      }|                    t                                }|                    t                                }| 	                    d          }|t          d          | 	                    d	d          }| 	                    d
t          t          |                               }| 	                    dd          }| 	                    d           | j                                        D ]\  }	}
|                    |	|
           |                    d
|           i }ddd}ddd}|                                D ]}|                    d          r||         ||<   #|                    d          r||         ||<   D|                    d          r||         ||<   e|                    d          r||         ||<   ||         ||<   ||         ||<   t#          |          dk    r5|                    dt          t          j        |                               |                    dt          t          j        |                               |                    dt          t          j        |                               |                    d|           |                    d|           |,|                    d	|           |                    d	|           |                    d
|dz              |                    d
|dz              |,|                    d|           |                    d|           |                                                     | |           ||gS )zSplit a path into two paths, one filled and one stroked

    Returns a the list [fill, stroke], where each is the XML element of the
    fill or stroke, or None.
    style fillnonestrokeNdz0Cannot split stroke and fill of non-path elementzsodipodi:nodetypesid	transformz#000000)r@   r>   )r>   r@   markerfilterr   z-fillz-stroke)dictr(   Style	parse_strgetkeysr   addr   popAssertionErrorstrrB   attribitemsset
startswithlen	getparentreplace)	path_noder<   groupr>   r@   rA   	nodetypespath_idrC   attrib_nameattrib_valuestyle_group
style_fillstyle_strokekeys                  r   split_fill_and_stroker`   p   s    &&y}}Wb'A'ABBCCE 5=F225  E(Ov$=$=$<)$$uzz||##uX&'@'@4  GGE99[]]##DYY{}}%%FcAyOPPP2D99ImmD#bmm"4"455Gk400IMM' &/%5%;%;%=%= - -!\		+|,,,,	IIdG K"I66J"f55Lzz|| + +>>&!! 
	+#CjJsOO^^H%% 	+ %c
L^^H%% 	+ %c
L^^H%% 	+$SzK#CjJsO %c
L
;1		'3u{;7788999HHWc%+j1122333
JJwEK5566777 	HHS!
JJsA%y111

'333HHT7W$%%%
JJtWy()))i(((

;	*** !!)U333&>r   )      ?        rb   )rb   ra   rb   c                 
   t          | t          t          f          sdS t          | t                    r|                     d          rd |                                 D             \  }}}}t          |                     d|                    }t          |                     d|                    }t          | | f||z  ||z  f          }	| j        d= nt          |          }	|	| j        z  }	t          t          j                            |                     dd                              }
i }d	g}|D ]&}||
                                v r|
|         ||<   |
|= '|                                }|                    |
           |}
d
dg}|D ]7}|                     |          r |                     |          |
|<   | j        |= 8t          | t          t           t"          t$          f          rp|r|| _        nd|                                 v r| j        d= d|                                 v r| j        d= |                                 D ]}t+          ||
|	           dS |
                    |           |
| _        |	| _        dS )z3Propagate style and transform to remove inheritanceNviewBoxc                 ,    g | ]}t          |          S r"   )get_dimension).0xs     r   
<listcomp>z%propagate_attribs.<locals>.<listcomp>   s     GGGq-**GGGr   widthheight)	translatescaler<   r=   rE   r>   r@   rC   )
isinstancer   r   rI   get_viewboxrf   r   rO   rC   rF   r(   rG   rH   rJ   copyupdater   r   r   r<   iterchildrenpropagate_attribs)nodeparent_styleparent_transformvxvyvwvhdwdhthis_transform
this_styleremaining_stylenon_propagatedr_   parent_style_copystyle_attribsrO   childs                     r   rs   rs      s    d\+=>??  $*++ 50C0C 5GGD4D4D4F4FGGGBB488GR0011488Hb1122"rcB3ZRb?QRRRK	"""#344dn$N ek++DHHWb,A,ABBCCJOZN    *//#####-c?OC 3 %))++Z((("J X&M $ $88F 	$!%&!1!1JvF#$+UFFCDD ( 	)(DJJ$))++%%K( $))++%%K( &&(( 	A 	AEeZ@@@@	A 	A 	/***  
'r   1024c                 B    t           j                            | d          S )z;Convert an SVG length string from arbitrary units to pixelspx)r(   unitsconvert_unit)ss    r   rf   rf   	  s    ;##At,,,r   c                       e Zd Zd Zd ZdS )
SynfigPrepc                    |                                   t          | j                                                   | j                                                            d          D ]q}|                    dd                                                              d          dk    r0t          |          d         }|t          |j
                  |_
        rdS )zITransform document in preparation for exporting it into the Synfig formatz
//svg:pathrA   r=   m   r   N)
preprocessrs   documentgetrootxpathrI   lowercountr`   r:   r#   )r   rt   r>   s      r   effectzSynfigPrep.effect  s     	 	$-//11222 M))++11,?? 	9 	9DxxR  &&((..s33a77,T2215# -di 8 8DI	9 	9r   c                 h   g d}g d}|d |D             z  }|ddgz  }|ddgz  }|dd	gz  }t           j                            | j        d
          }t	          | j        |           	 t          |d                    |                     nd# t          $ rW}t          j	        t          d                     t          j	        |j                            d                     Y d }~nd }~ww xY wt          |d          5 }t          |          | _        d d d            n# 1 swxY w Y   | j        S )N)z
unlock-allzselect-by-element:flowRootobject-to-pathselect-clear)rectcircleellipselinepolylinepolygontextc                     g | ]}d |z   S )zselect-by-element:r"   )rg   r5   s     r   ri   z)SynfigPrep.preprocess.<locals>.<listcomp>-  s    ???(1,???r   r   r   zselect-by-element:usezobject-unlink-cloneszexport-overwritez	export-doz	input.svg;)actionsz-An error occurred during document preparationzutf-8r)osr#   jointempdirr   r   r   r   r(   errormsgr   stderrdecodeopenr
   )r   r   elementsinfileerrstreams         r   r   zSynfigPrep.preprocess   s   
 
 
 XWW??h????$n55+-CDD&44dlK88$-(((	7VSXXg%6%677777 	7 	7 	7N1LMMNNNN3:,,W5566666666	7 &# 	-&$V,,DM	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- }s+   ($B 
C.AC))C.D""D&)D&N)r   r   r    r   r   r"   r   r   r   r     s2        9 9 9     r   r   __main__)r   )#r!   r   tempfile
subprocessr   r   inkex.commandr   r   r   inkex.localizationr   r   
inkex.baser	   r(   r
   r   r   r   r   r   r   r   	Exceptionr   r)   r:   r`   rs   rf   EffectExtensionr   r   runr"   r   r   <module>r      s  (  
			  " " " " " " " " > > > > > > > > > > 1 1 1 1 1 1 # # # # # # 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 
 
 
 
	 
 
 
2&
 & & & &RK K Kd ___ooo,NB( B( B( B(P- - - -0 0 0 0 0u4 0 0 0f zJLL r   