
    BPc0                         d Z ddlZddlmZ ddlZddlmZ ddlmZm	Z	 ddl
mZ  G d deej                  Zed	k    r e                                             dS dS )
z>
Export to Gimp's XCF file format including Grids and Guides.
    N)OrderedDict)TempDirMixin)take_snapshotcall)inkex_gettextc                   D    e Zd ZdZdZd Zd Zd Zed             Z	d Z
dS )	GimpXcfz
    Provide a quick and dirty way of using gimp to output an xcf from Inkscape.

    Both Inkscape and Gimp must be installed for this extension to work.
    z	gimp-out-c                 D   |                     dd           |                     ddt          j        d           |                     dd	t          j        d
           |                     ddt          j        d           |                     ddt          dd           d S )Nz--tabtab)destz-dz--guideszSave the Guides in the XCF)typehelpz-rz--gridzSave the Grid with the .XCF-bz--backgroundzAdd background colorz-iz--dpi      X@zFile resolution)r   defaultr   )add_argumentinkexBooleanfloat)selfparss     */usr/share/inkscape/extensions/gimp_xcf.pyadd_argumentszGimpXcf.add_arguments,   s    '...*5=7S 	 	
 	
 	
 	(5R 	 	
 	
 	
 	.u};Q 	 	
 	
 	
 	't:K 	 	
 	
 	
 	
 	
    c                    g }g }| j         j                                        D ]}|j        rQd|j        j        cxk     r| j         j        k     r/n -|                    t          |j        j                             Z|j	        rPd|j        j
        cxk     r| j         j        k     r/n |                    t          |j        j
                             dd                    |          fdd                    |          ffS )z5Generate a list of horzontal and vertical only guidesr   h v)svg	namedview
get_guidesis_horizontalpointyviewbox_heightappendstris_verticalxviewbox_widthjoin)r   horz_guidesvert_guidesguides       r   r!   zGimpXcf.get_guides;   s   X'2244 		; 		;E" ;u{}>>>>tx'>>>>>>&&s5;='9'9:::" ;u{}====tx'======&&s5;='9'9:::SXXk**+c388K3H3H-IIIr   c           	   #     K   | j         j        | j        j        dz  z  }d}| j                             |          r| j                             |          }dD ]\  }}}d|i}dD ]r}t          |                    ||z   |                    }	| j                             |	d          |z  }	t          t          t          |	                              ||<   s dj        d	i |V  dS dS )
z5Get the grid if asked for and return as gimpfu scriptr   zQsodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]))spacing   r0   )originr   offsettargetxypxz*(gimp-image-grid-set-{target} img {x} {y})N )r   scaleoptionsdpixpath
getElementr   getuutounitintroundformat)
r   r8   r;   nodeattrr   r4   fmtdimunits
             r   get_gridzGimpXcf.get_gridM   s     DL$4t$;<c8>>%   	Q8&&u--D* 
Q 
Q%gv  ( 7 7C $*g!>!>??D8,,T4885@D"5t#5#566CHHIBIPPCPPPPPP	Q 	Q
Q 
Qr   c                 :    | j                             d          pdS )z)Get the document name suitable for exportzsodipodi:docnamedocument)r   r=   )r   s    r   docnamezGimpXcf.docname`   s     x||.//=:=r   c                    t                      }d}| j                            d          D ]}t          |          sd}|                    d          }|                    d|          }t          | j        | j        |t          | j	        j
                  |ddt          t          | j	        j                                      ||<   |s#t          j        t          d                     d S t           j                            | j        d                    | j                            }d	                    | j	        j
        d
                    |                                          d
                    t-          |                              }| j	        j        r4|                                 D ]\  }	}
|d                    |	|
          z  } | j	        j        r"|                                 D ]}|d|z   dz   z  }|d                    |          z  }t7          ddddd|           t9          |d          5 }|                    |                                           d d d            d S # 1 swxY w Y   d S )NFz%/svg:svg/*[name()='g' or @style][@id]Tidzinkscape:label)dirnamenamer:   	export_idexport_id_onlyexport_area_pageexport_background_opacityz5This extension requires at least one non empty layer.z{}.xcfa  
(tracing 1)
(define
  (png-to-layer img png_filename layer_name)
  (let*
    (
      (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
      (png_layer (car (gimp-image-get-active-layer png)))
      (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
    )
    (gimp-image-add-layer img xcf_layer -1)
    (gimp-drawable-set-name xcf_layer layer_name)
  )
)
(let*
  (
    (img (car (gimp-image-new 200 200 RGB)))
  )
  (gimp-image-set-resolution img {dpi} {dpi})
  (gimp-image-undo-disable img)
  (for-each
    (lambda (names)
      (png-to-layer img (car names) (cdr names))
    )
    (map cons '("{files}") '("{names}"))
  )

  (gimp-image-resize-to-layers img)
z" ")r:   filesnamesze
  (for-each
    (lambda ({d}Guide)
      (gimp-image-add-{d}guide img {d}Guide)
    )
    '({g})
  ))dg
z
  (gimp-image-undo-enable img)
  (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "{xcf}" "{xcf}"))
(gimp-quit 0)
            )xcfgimpr   -zplug-in-script-fu-eval)ibatch_interpreterstdinrb)r   r   r;   lenr=   r   rI   tempdirr?   r9   r:   bool
backgroundr   errormsg_ospathr+   rA   rJ   valueslistguidesr!   gridrG   r   openwriteread)r   streampngsvalidrB   node_idrN   rX   	script_furE   ri   fu_letfhls                r   savezGimpXcf.savee   s   }}HNN#JKK 	 	Dt99 EhhtnnG88,g66D&())!#!%*-d4<3J.K.K*L*L	 	 	DJJ  	N1TUUVVVFgll4<)F)FGG8 F **T[[]]++**T$ZZ((  
 
9 	D < 
	#00 	 	V  vV   		 < 	2--// 2 2TF]T11		    
 
		
	 	6	
 	
 	
 	
 #t__ 	%LL$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   ?(I44I8;I8N)__name__
__module____qualname____doc__
dir_prefixr   r!   rG   propertyrJ   ru   r7   r   r   r	   r	   #   s          J
 
 
J J J$Q Q Q& > > X>c% c% c% c% c%r   r	   __main__)ry   re   collectionsr   r   
inkex.baser   inkex.commandr   r   inkex.localizationr   rd   OutputExtensionr	   rv   runr7   r   r   <module>r      s   ,  
			 # # # # # #  # # # # # # - - - - - - - - 1 1 1 1 1 1e% e% e% e% e%lE1 e% e% e%P zGIIMMOOOOO r   