
    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 )aj  
This script slices an inkscape drawing along the guides, similarly to
the GIMP plugin called "guillotine". It can optionally export to the
same directory as the SVG file with the same name, but with a number
suffix. e.g.

/home/foo/drawing.svg

will export to:

/home/foo/drawing0.png
/home/foo/drawing1.png
/home/foo/drawing2.png
/home/foo/drawing3.png

etc.

    N)inkscape)inkex_gettextc                   T    e 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S )
Guillotinez Exports slices made using guidesc                     |                     dt          d           |                     dt          d           |                     dt          j        d           d S )Nz--directory	directory)typedestz--imageimagez--ignoreignore)add_argumentstrinkexBoolean)selfparss     ,/usr/share/inkscape/extensions/guillotine.pyadd_argumentszGuillotine.add_arguments2   s[    -cDDD)#G<<<*5=xHHHHH    c              #   v   K   | j         j                                        D ]}|j        r|j        j        V  dS )z
        Returns all horizontal guides as a list of floats stored as
        strings. Each value is the position from 0 in pixels.
        N)svg	namedview
get_guidesis_horizontalpointyr   guides     r   get_all_horizontal_guidesz$Guillotine.get_all_horizontal_guides7   sN      
 X'2244 	$ 	$E" $km###	$ 	$r   c              #   v   K   | j         j                                        D ]}|j        r|j        j        V  dS )z
        Returns all vertical guides as a list of floats stored as
        strings. Each value is the position from 0 in pixels.
        N)r   r   r   is_verticalr   xr   s     r   get_all_vertical_guidesz"Guillotine.get_all_vertical_guides@   sN      
 X'2244 	$ 	$E  $km###	$ 	$r   c                     dg}t          | j        j                  }|                                 D ]*}d|cxk     r|k    rn |                    ||z
             +|                    |           t          |          S )z
        Make a sorted list of all horizontal guide positions,
        including 0 and the document height, but not including
        those outside of the canvas
                )floatr   viewbox_heightr   appendsorted)r   horizontalsheightr   s       r   get_horizontal_slice_positionsz)Guillotine.get_horizontal_slice_positionsI   s     etx.////11 	/ 	/AQ    &     ""6A:...6"""k"""r   c                     dg}t          | j        j                  }|                                 D ]'}d|cxk     r|k    rn |                    |           (|                    |           t          |          S )z
        Make a sorted list of all vertical guide positions,
        including 0 and the document width, but not including
        those outside of the canvas.
        r%   )r&   r   viewbox_widthr#   r(   r)   )r   	verticalswidthr"   s       r   get_vertical_slice_positionsz'Guillotine.get_vertical_slice_positionsW   s     E	dh,----// 	$ 	$AQ%  ###i   r   c           
                                            }                                 } fd|D             } fd|D             }g }t          t          |          dz
            D ][}t          t          |          dz
            D ]9}|                    ||         ||         ||dz            ||dz            g           :\|S )z
        Returns a list of all "slices" as denoted by the guides
        on the page. Each slice is really just a 4 element list of
        floats (stored as strings), consisting of the X and Y start
        position and the X and Y end position.
        c                 D    g | ]}j                             |          S  r   unit_to_viewport).0ir   s     r   
<listcomp>z)Guillotine.get_slices.<locals>.<listcomp>o   )    777qdh''**777r   c                 D    g | ]}j                             |          S r4   r5   )r7   jr   s     r   r9   z)Guillotine.get_slices.<locals>.<listcomp>p   r:   r      )r,   r1   rangelenr(   )r   hsvsslicesr8   r<   s   `     r   
get_sliceszGuillotine.get_slicese   s     0022..007777B7777777B777s2ww{## 	D 	DA3r77Q;'' D Dr!ubeRAY1q5	BCCCCDr   c                    | j         j        sU| j         j        dk    s| j         j        !t          j        t          d                    | j         j        | j         j        fS 	 	 | j                            d          }n/# t          $ r" t          j        t          d                    w xY wt          j                            |          \  }}|                    dd          d         }||fS )	z
        Attempts to get directory and image as passed in by the inkscape
        dialog. If the boolean ignore flag is set, then it will ignore
        these settings and try to use the settings from the export
        filename.
         NzPlease enter an image namezinkscape:export-filenamezsTo use the export hints option, you need to have previously exported the document. Otherwise no export hints exist!.r=   r   )optionsr   r   r   AbortExtension_r   r   getKeyErrorospathsplitrsplit)r   export_filedirnamefilenames       r   get_filename_partszGuillotine.get_filename_partsw   s     |" 	%|!R''4<+=+E*1-I+J+JKKK<)4<+===	"hll+EFF   *;    !#k : :GXsA..q1HH$$s   $A? ?,B+c                 H    t          j        dt          |          d          S )Nz%.fr   )localeformat_stringr&   )r   names     r   get_localised_stringzGuillotine.get_localised_string   s    #E5;;:::r   c                      d                      fd|D                       }t          j        |           t           j        j        ||           dS )z
        Runs inkscape's command line interface and exports the image
        slice from the 4 coordinates in s, and saves as the filename
        given.
        :c                 :    g | ]}                     |          S r4   )rX   )r7   dimr   s     r   r9   z+Guillotine.export_slice.<locals>.<listcomp>   s'    IIIc444S99IIIr   )export_areaexport_filenameN)joinr   errormsgr   rG   
input_file)r   slirR   coordss   `   r   export_slicezGuillotine.export_slice   s[     IIIISIIIJJv(fhWWWWWWr   c                 N   |                                  \  }}|                    d          s|                    d          r|                    dd          d         }d|vr|dz  }t          j                            t          j                            t          j                            |pd                              }t          j                            |          st          j	        |           g }t          |          D ]f\  }}t          j                            ||                    |          dz             }|                    |           |                     ||           g|                     t!          d	          d
                    |          z              dS )z{
        Takes the slices list and passes each one with a calculated
        filename/directory into export_slice.
        z.svgz.pngrF   r=   r   {z_{}z./z(The sliced bitmaps have been saved as:


N)rS   endswithrO   rL   rM   abspath
expanduser
expandvarsisdirmakedirs	enumerater_   formatr(   rd   debugrI   )r   rB   rQ   rR   output_filesr8   slicofnames           r   export_sliceszGuillotine.export_slices   s   
 !3355V$$ 	2(9(9&(A(A 	2sA..q1HhH'//Grw11'/TBBCC
 
 w}}W%% 	!K   !&)) 	, 	,HAuGLL(//!*<*<v*EFFE&&&eU++++

:;;dii>U>UU	
 	
 	
 	
 	
r   c                 T    |                      |                                            d S )N)rt   rC   )r   s    r   effectzGuillotine.effect   s&    4??,,-----r   N)__name__
__module____qualname____doc__r   r   r#   r,   r1   rC   rS   rX   rd   rt   rv   r4   r   r   r   r   /   s        **I I I
$ $ $$ $ $# # #! ! !  $ %  %  %D; ; ;X X X
 
 
8. . . . .r   r   __main__)rz   rL   rU   r   inkex.commandr   inkex.localizationr   rI   EffectExtensionr   rw   runr4   r   r   <module>r      s   ( & 
			   " " " " " " 1 1 1 1 1 1T. T. T. T. T.& T. T. T.n zJLL r   