1 #!/usr/bin/env python
2 '''
3 Copyright (C) 2006 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 '''
19 import inkex
20 import sys, os, tempfile
22 class MyEffect(inkex.Effect):
23 def __init__(self):
24 inkex.Effect.__init__(self)
25 def output(self):
26 pass
27 def effect(self):
28 svg_file = self.args[-1]
29 node = inkex.xml.xpath.Evaluate('/svg',self.document)[0]
30 docname = node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docname').value[:-4]
32 #create os temp dir
33 tmp_dir = tempfile.mkdtemp()
35 area = '--export-area-canvas'
36 pngs = []
37 path = "/svg/*[name()='g' or @style][@id]"
38 for node in inkex.xml.xpath.Evaluate(path,self.document):
39 id = node.attributes.getNamedItem('id').value
40 name = "%s.png" % id
41 filename = os.path.join(tmp_dir, name)
42 command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
43 f = os.popen(command,'r')
44 f.read()
45 f.close()
46 pngs.append(filename)
48 filelist = '"%s"' % '" "'.join(pngs)
49 xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
50 script_fu = """
51 (define
52 (png-to-layer img png_filename)
53 (let*
54 (
55 (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
56 (png_layer (car (gimp-image-get-active-layer png)))
57 (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
58 )
59 (gimp-image-add-layer img xcf_layer -1)
60 (gimp-drawable-set-name xcf_layer png_filename)
61 )
62 )
63 (let*
64 (
65 (img (car (gimp-image-new 200 200 RGB)))
66 )
67 (gimp-image-undo-disable img)
68 (let* ((filelist '(%s)))
69 (while filelist
70 (let* ((filename (car filelist)))
71 (png-to-layer img filename)
72 )
73 (set! filelist (cdr filelist))
74 )
75 )
76 (gimp-image-resize-to-layers img)
77 (gimp-image-undo-enable img)
78 (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
79 (gimp-quit 0)
80 """ % (filelist, xcf, xcf)
82 junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
83 f = os.popen('gimp -i -b - > %s' % junk,'w')
84 f.write(script_fu)
85 f.close()
87 x = open(xcf, 'r')
88 sys.stdout.write(x.read())
89 x.close()
91 e = MyEffect()
92 e.affect()