Code

6522f94d5c60fa4f33f883da90b8d0aa70309d5d
[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("-d", "--guides",
33                                          action="store", type="inkbool",
34                                          dest="saveGuides", default=False,
35                                          help="Save the Guides with the .XCF")
36         self.OptionParser.add_option("-r", "--grid",
37                                          action="store", type="inkbool",
38                                          dest="saveGrid", default=False,
39                                          help="Save the Grid with the .XCF")
40     def output(self):
41         pass
43     def effect(self):
44         svg_file = self.args[-1]
45         docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4]
46         pageHeight = int(inkex.unittouu(self.xpathSingle('/svg:svg/@height').split('.')[0]))
47         pageWidth = int(inkex.unittouu(self.xpathSingle('/svg:svg/@width').split('.')[0]))
49         #create os temp dir
50         tmp_dir = tempfile.mkdtemp()
52         # Guides
53         hGuides = []
54         vGuides = []
55         if self.options.saveGuides:
56             guideXpath = "sodipodi:namedview/sodipodi:guide" #grab all guide tags in the namedview tag
57             for guideNode in self.document.xpath(guideXpath, namespaces=inkex.NSS):
58                 ori = guideNode.get('orientation')
59                 if  ori == '0,1':
60                     #this is a horizontal guide
61                     pos = int(guideNode.get('position').split(',')[1].split('.')[0])
62                     #GIMP doesn't like guides that are outside of the image
63                     if pos > 0 and pos < pageHeight:
64                         #the origin is at the top in GIMP land
65                         hGuides.append(str(pageHeight - pos))
66                 elif ori == '1,0':
67                     #this is a vertical guide
68                     pos = int(guideNode.get('position').split(',')[0].split('.')[0])
69                     #GIMP doesn't like guides that are outside of the image
70                     if pos > 0 and pos < pageWidth:
71                         vGuides.append(str(pos))
73         hGList = ' '.join(hGuides)
74         vGList = ' '.join(vGuides)
76         # Grid
77         gridSpacingFunc = ''
78         gridOriginFunc = '' 
79         #GIMP only allows one rectangular grid
80         gridXpath = "sodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]"
81         if (self.options.saveGrid and self.document.xpath(gridXpath, namespaces=inkex.NSS)):
82             gridNode = self.xpathSingle(gridXpath)
83             if gridNode != None:
84                 #these attributes could be nonexistant
85                 spacingX = gridNode.get('spacingx')
86                 if spacingX == None: spacingX = '1  '
88                 spacingY = gridNode.get('spacingy')
89                 if spacingY == None: spacingY = '1  '
91                 originX = gridNode.get('originx')
92                 if originX == None: originX = '0  '
94                 originY = gridNode.get('originy')
95                 if originY == None: originY = '0  '
97                 gridSpacingFunc = '(gimp-image-grid-set-spacing img %s %s)' % (spacingX[:-2], spacingY[:-2])
98                 gridOriginFunc = '(gimp-image-grid-set-offset img %s %s)'% (originX[:-2], originY[:-2])
100         # Layers
101         area = '--export-area-page'
102         pngs = []
103         names = []
104         path = "/svg:svg/*[name()='g' or @style][@id]"
105         for node in self.document.xpath(path, namespaces=inkex.NSS):
106             if len(node) > 0: # Get rid of empty layers
107                 id = node.get('id')
108                 if node.get("{" + inkex.NSS["inkscape"] + "}label"):
109                     name = node.get("{" + inkex.NSS["inkscape"] + "}label")
110                 else:
111                     name = id
112                 filename = os.path.join(tmp_dir, "%s.png" % id)
113                 command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
114                 if bsubprocess:
115                     p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
116                     return_code = p.wait()
117                     f = p.stdout
118                     err = p.stderr
119                 else:
120                     _,f,err = os.popen3(command,'r')
121                 f.read()
122                 f.close()
123                 err.close()
124                 if os.name == 'nt':
125                     filename = filename.replace("\\", "/")
126                 pngs.append(filename)
127                 names.append(name.encode('utf-8'))
129         filelist = '"%s"' % '" "'.join(pngs)
130         namelist = '"%s"' % '" "'.join(names)
131         xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
132         if os.name == 'nt':
133             xcf = xcf.replace("\\", "/")
134         script_fu = """
135 (tracing 1)
136 (define
137   (png-to-layer img png_filename layer_name)
138   (let*
139     (
140       (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
141       (png_layer (car (gimp-image-get-active-layer png)))
142       (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
143     )
144     (gimp-image-add-layer img xcf_layer -1)
145     (gimp-drawable-set-name xcf_layer layer_name)
146   )
148 (let*
149   (
150     (img (car (gimp-image-new 200 200 RGB)))
151   )
152   (gimp-image-undo-disable img)
153   (for-each
154     (lambda (names)
155       (png-to-layer img (car names) (cdr names))
156     )
157     (map cons '(%s) '(%s))
158   )
160   (gimp-image-resize-to-layers img)
162   (for-each
163     (lambda (hGuide)
164       (gimp-image-add-hguide img hGuide)
165     )
166     '(%s)
167   )
169   (for-each
170     (lambda (vGuide)
171       (gimp-image-add-vguide img vGuide)
172     )
173     '(%s)
174   )
176   %s
177   %s
179   (gimp-image-undo-enable img)
180   (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
181 (gimp-quit 0)
182         """ % (filelist, namelist, hGList, vGList, gridSpacingFunc, gridOriginFunc, xcf, xcf)
184         junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
185         f = os.popen('gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk,'w')
186         f.write(script_fu)
187         f.close()
188         # uncomment these lines to see the output from gimp
189         #err = open(junk, 'r')
190         #inkex.debug(err.read())
191         #err.close()
193         x = open(xcf, 'rb')
194         if os.name == 'nt':
195             try:
196                 import msvcrt
197                 msvcrt.setmode(1, os.O_BINARY)
198             except:
199                 pass
200         sys.stdout.write(x.read())
201         x.close()
203 if __name__ == '__main__':
204     e = MyEffect()
205     e.affect()