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 names = []
38 path = "/svg/*[name()='g' or @style][@id]"
39 for node in inkex.xml.xpath.Evaluate(path,self.document):
40 id = node.attributes.getNamedItem('id').value
41 name = "%s.png" % id
42 filename = os.path.join(tmp_dir, name)
43 command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
44 _,f,err = os.popen3(command,'r')
45 f.read()
46 f.close()
47 err.close()
48 pngs.append(filename)
49 names.append(id)
51 filelist = '"%s"' % '" "'.join(pngs)
52 namelist = '"%s"' % '" "'.join(names)
53 xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
54 script_fu = """
55 (define
56 (png-to-layer img png_filename layer_name)
57 (let*
58 (
59 (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
60 (png_layer (car (gimp-image-get-active-layer png)))
61 (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
62 )
63 (gimp-image-add-layer img xcf_layer -1)
64 (gimp-drawable-set-name xcf_layer layer_name)
65 )
66 )
67 (let*
68 (
69 (img (car (gimp-image-new 200 200 RGB)))
70 )
71 (gimp-image-undo-disable img)
72 (let* ((filelist '(%s))(namelist '(%s)))
73 (while filelist
74 (let* ((filename (car filelist))(layername (car namelist)))
75 (png-to-layer img filename layername)
76 )
77 (set! filelist (cdr filelist))
78 (set! namelist (cdr namelist))
79 )
80 )
81 (gimp-image-resize-to-layers img)
82 (gimp-image-undo-enable img)
83 (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
84 (gimp-quit 0)
85 """ % (filelist, namelist, xcf, xcf)
87 junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
88 f = os.popen('gimp -i -b - > %s' % junk,'w')
89 f.write(script_fu)
90 f.close()
92 x = open(xcf, 'r')
93 sys.stdout.write(x.read())
94 x.close()
96 e = MyEffect()
97 e.affect()