Code

02127059c990d60953aea22fed4b59f5c1b727c3
[inkscape.git] / share / extensions / gimp_xcf.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2006 Aaron Spike, aaron@ekips.org
4 Copyright (C) 2010 Nicolas Dufour, nicoduf@yahoo.fr (Windows support and various fixes)
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 '''
20 import inkex
21 import sys, os, tempfile
23 try:
24     from subprocess import Popen, PIPE
25     bsubprocess = True
26 except:
27     bsubprocess = False
29 class MyEffect(inkex.Effect):
30     def __init__(self):
31         inkex.Effect.__init__(self)
32         self.OptionParser.add_option("--tab",
33                                      action="store", type="string",
34                                      dest="tab")
35         self.OptionParser.add_option("-d", "--guides",
36                                          action="store", type="inkbool",
37                                          dest="saveGuides", default=False,
38                                          help="Save the Guides with the .XCF")
39         self.OptionParser.add_option("-r", "--grid",
40                                          action="store", type="inkbool",
41                                          dest="saveGrid", default=False,
42                                          help="Save the Grid with the .XCF")
43         self.OptionParser.add_option("-b", "--background",
44                                          action="store", type="inkbool",
45                                          dest="layerBackground", default=False,
46                                          help="Add background color to each layer")
48     def output(self):
49         pass
51     def effect(self):
52         svg_file = self.args[-1]
53         docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4]
54         pageHeight = int(inkex.unittouu(self.xpathSingle('/svg:svg/@height').split('.')[0]))
55         pageWidth = int(inkex.unittouu(self.xpathSingle('/svg:svg/@width').split('.')[0]))
57         #create os temp dir
58         tmp_dir = tempfile.mkdtemp()
60         # Guides
61         hGuides = []
62         vGuides = []
63         if self.options.saveGuides:
64             guideXpath = "sodipodi:namedview/sodipodi:guide" #grab all guide tags in the namedview tag
65             for guideNode in self.document.xpath(guideXpath, namespaces=inkex.NSS):
66                 ori = guideNode.get('orientation')
67                 if  ori == '0,1':
68                     #this is a horizontal guide
69                     pos = int(guideNode.get('position').split(',')[1].split('.')[0])
70                     #GIMP doesn't like guides that are outside of the image
71                     if pos > 0 and pos < pageHeight:
72                         #the origin is at the top in GIMP land
73                         hGuides.append(str(pageHeight - pos))
74                 elif ori == '1,0':
75                     #this is a vertical guide
76                     pos = int(guideNode.get('position').split(',')[0].split('.')[0])
77                     #GIMP doesn't like guides that are outside of the image
78                     if pos > 0 and pos < pageWidth:
79                         vGuides.append(str(pos))
81         hGList = ' '.join(hGuides)
82         vGList = ' '.join(vGuides)
84         # Grid
85         gridSpacingFunc = ''
86         gridOriginFunc = '' 
87         #GIMP only allows one rectangular grid
88         gridXpath = "sodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]"
89         if (self.options.saveGrid and self.document.xpath(gridXpath, namespaces=inkex.NSS)):
90             gridNode = self.xpathSingle(gridXpath)
91             if gridNode != None:
92                 #these attributes could be nonexistant
93                 spacingX = gridNode.get('spacingx')
94                 if spacingX == None: spacingX = '1  '
96                 spacingY = gridNode.get('spacingy')
97                 if spacingY == None: spacingY = '1  '
99                 originX = gridNode.get('originx')
100                 if originX == None: originX = '0  '
102                 originY = gridNode.get('originy')
103                 if originY == None: originY = '0  '
105                 gridSpacingFunc = '(gimp-image-grid-set-spacing img %s %s)' % (spacingX[:-2], spacingY[:-2])
106                 gridOriginFunc = '(gimp-image-grid-set-offset img %s %s)'% (originX[:-2], originY[:-2])
108         # Layers
109         area = '--export-area-page'
110         opacity = '--export-background-opacity='
111         if self.options.layerBackground:
112             opacity += "1"
113         else:
114             opacity += "0"
115         pngs = []
116         names = []
117         path = "/svg:svg/*[name()='g' or @style][@id]"
118         for node in self.document.xpath(path, namespaces=inkex.NSS):
119             if len(node) > 0: # Get rid of empty layers
120                 id = node.get('id')
121                 if node.get("{" + inkex.NSS["inkscape"] + "}label"):
122                     name = node.get("{" + inkex.NSS["inkscape"] + "}label")
123                 else:
124                     name = id
125                 filename = os.path.join(tmp_dir, "%s.png" % id)
126                 command = "inkscape -i %s -j %s %s -e %s %s " % (id, area, opacity, filename, svg_file)
127                 if bsubprocess:
128                     p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
129                     return_code = p.wait()
130                     f = p.stdout
131                     err = p.stderr
132                 else:
133                     _,f,err = os.popen3(command,'r')
134                 f.read()
135                 f.close()
136                 err.close()
137                 if os.name == 'nt':
138                     filename = filename.replace("\\", "/")
139                 pngs.append(filename)
140                 names.append(name.encode('utf-8'))
142         filelist = '"%s"' % '" "'.join(pngs)
143         namelist = '"%s"' % '" "'.join(names)
144         xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
145         if os.name == 'nt':
146             xcf = xcf.replace("\\", "/")
147         script_fu = """
148 (tracing 1)
149 (define
150   (png-to-layer img png_filename layer_name)
151   (let*
152     (
153       (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
154       (png_layer (car (gimp-image-get-active-layer png)))
155       (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
156     )
157     (gimp-image-add-layer img xcf_layer -1)
158     (gimp-drawable-set-name xcf_layer layer_name)
159   )
161 (let*
162   (
163     (img (car (gimp-image-new 200 200 RGB)))
164   )
165   (gimp-image-undo-disable img)
166   (for-each
167     (lambda (names)
168       (png-to-layer img (car names) (cdr names))
169     )
170     (map cons '(%s) '(%s))
171   )
173   (gimp-image-resize-to-layers img)
175   (for-each
176     (lambda (hGuide)
177       (gimp-image-add-hguide img hGuide)
178     )
179     '(%s)
180   )
182   (for-each
183     (lambda (vGuide)
184       (gimp-image-add-vguide img vGuide)
185     )
186     '(%s)
187   )
189   %s
190   %s
192   (gimp-image-undo-enable img)
193   (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
194 (gimp-quit 0)
195         """ % (filelist, namelist, hGList, vGList, gridSpacingFunc, gridOriginFunc, xcf, xcf)
197         junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
198         f = os.popen('gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk,'w')
199         f.write(script_fu)
200         f.close()
201         # uncomment these lines to see the output from gimp
202         #err = open(junk, 'r')
203         #inkex.debug(err.read())
204         #err.close()
206         x = open(xcf, 'rb')
207         if os.name == 'nt':
208             try:
209                 import msvcrt
210                 msvcrt.setmode(1, os.O_BINARY)
211             except:
212                 pass
213         sys.stdout.write(x.read())
214         x.close()
216 if __name__ == '__main__':
217     e = MyEffect()
218     e.affect()