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