
    BPc                         d Z ddlZddlZddlZddlmZ ddlmZ  G d dej	                  Z
edk    r e
                                             dS dS )a  
A script that slices images.  It might be useful for web design.

You pass it the name of a layer containing rectangles that cover
the areas that you want exported (the default name for this layer
is "slices").  It then sets the opacity to 0 for all the rectangles
defined in that layer and exports as png whatever they covered.
The output filenames are based on the "Id" field of "Object Properties"
right click contextual menu of the rectangles.

One side effect is that after exporting, it sets the slice rectangles
to different colors with a 25% opacity.  (If you want to hide them,
just click on the eye next to the layer).

  * red - overwrote a file
  * green - wrote a new file
  * grey - skipped (not overwriting)

For good pixel exports set the Document Properties, default units to "px"
and the width/height to the real size. (I use 1024x768)

Here's the process I've used for slicing web layout with
Inkscape: Create your webpage layout (set page units to "px",
width/height appropriately and snap to 1 pixel intervals. This should
allow pixel perfect alignment). Then create a new layer, naming it
slices. Draw rectangles over the areas you want to slice (set
x,y,width,height to whole pixel values). Name these rectangles using
the Object Properties found in the right click contextual menu (the
saved images name will be based on that value, so name them something
like "header" instead of the default/non-useful "rect4312").
    N)inkscape)inkex_gettextc                   \     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d	 Zd
 ZddZddZ xZS )ExportSlicesz+Exports all rectangles in the current layerz#00ff00z#555555z#ff0000c                 d    t          t          |                                            i | _        d S )N)superr   __init__	color_map)self	__class__s    +/usr/share/inkscape/extensions/layer2png.pyr	   zExportSlices.__init__B   s*    lD!!**,,,    c                    |                     d           |                     dt          j                            d          d           |                     ddd           |                     d	t          j        d
           |                     ddd           |                     dt          j        d           |                     ddd           d S )Nz--tabz--directory~zExisting destination directory)defaulthelpz--layersliceszLayer with slices (rects) in itz
--iconmodezIcon export mode)typer   z--sizesz128, 64, 48, 32, 24, 16zsizes to export comma separatedz--overwritezOverwrite existing exports?z--dpi300zDots per inch (300 default))add_argumentospath
expanduserinkexBoolean)r   parss     r   add_argumentszExportSlices.add_argumentsF   s   '"""G&&s++1 	 	
 	
 	

 	x.O 	 	
 	
 	
 	,U]ASTTT-2 	 	
 	
 	

 	4Q 	 	
 	
 	
 	'57TUUUUUr   c                    t           j                            | j        j                  st          j        | j        j                   |                     | j        j                  }|>t          j	        t          d                              | j        j                            |D ]}|                     |           t          j        d          \  }}t          |d          5 }|                    | j                                                   d d d            n# 1 swxY w Y   |D ]}| j        j        rq| j        j                            d          D ]P}|                                }|                                r&t1          |          }|                     |||           Q|                     |           |D ]}|                     |           | j        S )NzSlice: '{}' does not exist.z.svgwb,)r   r   isdiroptions	directorymakedirsget_layer_nodeslayerr   AbortExtension_formatclear_colortempfilemkstempopenwritesvgtostringiconmodesizessplitstripisdigitintexport_nodechange_colordocument)r   nodesnode__tmp_svgfoutsizepng_sizes           r   effectzExportSlices.effect[   s   w}}T\344 	0K.///$$T\%788=&/00778JKK  
  	# 	#DT""""
 !(00W'4   	,DJJtx((**+++	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,
  	' 	'D|$ ' L.44S99 C CD::<<D||~~ C#&t99((xBBB	C   &&&&  	$ 	$Dd####}s   (-D!!D%(D%c                     d}| j                             d          }|D ]}|j        }||k    r|}||                    d          S |S )z
        given the name of a layer one that contains the rectangles defining slices,
        return the nodes of the rectangles.
        Nzsvg:gzsvg:rect)r/   findalllabel)r   
layer_name
slice_nodeslice_layerr;   label_values         r   r%   zExportSlices.get_layer_nodes   se     
h&&w// 	" 	"D*Kj((!
!%%j111r   c                 @    |j                             ddd           dS )a  
        set opacity to zero, and stroke to none

        Node looks like this:
        <rect
        style="opacity:0;fill:#eeeeec;fill-opacity:1;stroke:none;stroke-width:4.00099993;stroke-linecap:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;display:inline"
        none0)strokeopacityN)styleupdate)r   r;   s     r   r*   zExportSlices.clear_color   s(     	
V<<=====r   c                 t    |j         d         }| j        |         }|j                            |dd           dS )zB
        set color from color_map and set opacity to 25%

        idz.25)fillrM   N)attribr
   rN   rO   )r   r;   node_idcolors       r   r8   zExportSlices.change_color   s@    
 +d#w'
5U;;<<<<<r   Nc                     |                      |||          \  }}|j        d         }|| j        |<   |t          j        k    rd S | j        j        }t          |fi | d S )NrQ   )get_color_and_command_kwargsrS   r
   r   GREYr"   
input_filer   )r   r;   heightwidthrU   kwargsrT   svg_files           r   r7   zExportSlices.export_node   sn    99$NNv+d#"'wL%%%F<*$$V$$$$$r   c                    | j         j        }|j        d         }|dnd                    ||          }d                    ||          }t          j                            ||          }t          j        }	| j         j	        st          j        
                    |          sqt          j        }	t          j        
                    |          st          j        }	||| j         j        d}
|r$t          |          |
d<   t          |          |
d<   |	|
fS t          j        t#          d                              |                     |	i fS )	NrQ    z-{}x{}z{}{}.png)z	export-idzexport-filenamez
export-dpizexport-heightzexport-widthz#Export exists ({}), not overwriting)r"   r#   rS   r)   r   r   joinr   rX   	overwriteexistsREDGREENdpistrr   errormsgr(   )r   r;   rZ   r[   r#   rT   r?   	file_namefilenamerU   r\   s              r   rW   z)ExportSlices.get_color_and_command_kwargs   s,   L*	+d#^rr)G)G%%gt44	7<<	955!<! 	)A)A 	 $E7>>(++ +$*$#+"l. F
  4*-f++'),U~&&= N1BCCJJ8TTUUU"9r   )NN)__name__
__module____qualname____doc__rd   rX   rc   r	   r   rA   r%   r*   r8   r7   rW   __classcell__)r   s   @r   r   r   ;   s        55ED
C    V V V*$ $ $L  "> > >= = =% % % %       r   r   __main__)rm   r   r+   r   inkex.commandr   inkex.localizationr   r(   EffectExtensionr   rj   run r   r   <module>ru      s   ( > 
			   " " " " " " 1 1 1 1 1 1I I I I I5( I I IX zLNN r   