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 self.OptionParser.add_option("-d", "--guides",
26 action="store", type="inkbool",
27 dest="saveGuides", default=False,
28 help="Save the Guides with the .XCF")
29 self.OptionParser.add_option("-r", "--grid",
30 action="store", type="inkbool",
31 dest="saveGrid", default=False,
32 help="Save the Grid with the .XCF")
33 def output(self):
34 pass
35 def effect(self):
36 svg_file = self.args[-1]
37 docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4]
38 pageHeight = int(self.xpathSingle('/svg:svg/@height').split('.')[0])
39 pageWidth = int(self.xpathSingle('/svg:svg/@width').split('.')[0])
41 #create os temp dir
42 tmp_dir = tempfile.mkdtemp()
44 hGuides = []
45 vGuides = []
46 if self.options.saveGuides:
47 guideXpath = "sodipodi:namedview/sodipodi:guide" #grab all guide tags in the namedview tag
48 for guideNode in self.document.xpath(guideXpath, namespaces=inkex.NSS):
49 ori = guideNode.get('orientation')
50 if ori == '0,1':
51 #this is a horizontal guide
52 pos = int(guideNode.get('position').split(',')[1].split('.')[0])
53 #GIMP doesn't like guides that are outside of the image
54 if pos > 0 and pos < pageHeight:
55 #the origin is at the top in GIMP land
56 hGuides.append(str(pageHeight - pos))
57 elif ori == '1,0':
58 #this is a vertical guide
59 pos = int(guideNode.get('position').split(',')[0].split('.')[0])
60 #GIMP doesn't like guides that are outside of the image
61 if pos > 0 and pos < pageWidth:
62 vGuides.append(str(pos))
64 hGList = ' '.join(hGuides)
65 vGList = ' '.join(vGuides)
67 gridSpacingFunc = ''
68 gridOriginFunc = ''
69 #GIMP only allows one rectangular grid
70 if self.options.saveGrid:
71 gridNode = self.xpathSingle("sodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]")
72 if gridNode != None:
73 #these attributes could be nonexistant
74 spacingX = gridNode.get('spacingx')
75 if spacingX == None: spacingX = '1 '
77 spacingY = gridNode.get('spacingy')
78 if spacingY == None: spacingY = '1 '
80 originX = gridNode.get('originx')
81 if originX == None: originX = '0 '
83 originY = gridNode.get('originy')
84 if originY == None: originY = '0 '
86 gridSpacingFunc = '(gimp-image-grid-set-spacing img %s %s)' % (spacingX[:-2], spacingY[:-2])
87 gridOriginFunc = '(gimp-image-grid-set-offset img %s %s)'% (originX[:-2], originY[:-2])
89 area = '--export-area-canvas'
90 pngs = []
91 names = []
92 path = "/svg:svg/*[name()='g' or @style][@id]"
93 for node in self.document.xpath(path, namespaces=inkex.NSS):
94 id = node.get('id')
95 name = "%s.png" % id
96 filename = os.path.join(tmp_dir, name)
97 command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
98 _,f,err = os.popen3(command,'r')
99 f.read()
100 f.close()
101 err.close()
102 pngs.append(filename)
103 names.append(id)
105 filelist = '"%s"' % '" "'.join(pngs)
106 namelist = '"%s"' % '" "'.join(names)
107 xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
108 script_fu = """
109 (tracing 1)
110 (define
111 (png-to-layer img png_filename layer_name)
112 (let*
113 (
114 (png (car (file-png-load RUN-NONINTERACTIVE png_filename png_filename)))
115 (png_layer (car (gimp-image-get-active-layer png)))
116 (xcf_layer (car (gimp-layer-new-from-drawable png_layer img)))
117 )
118 (gimp-image-add-layer img xcf_layer -1)
119 (gimp-drawable-set-name xcf_layer layer_name)
120 )
121 )
122 (let*
123 (
124 (img (car (gimp-image-new 200 200 RGB)))
125 )
126 (gimp-image-undo-disable img)
127 (for-each
128 (lambda (names)
129 (png-to-layer img (car names) (cdr names))
130 )
131 (map cons '(%s) '(%s))
132 )
134 (gimp-image-resize-to-layers img)
136 (for-each
137 (lambda (hGuide)
138 (gimp-image-add-hguide img hGuide)
139 )
140 '(%s)
141 )
143 (for-each
144 (lambda (vGuide)
145 (gimp-image-add-vguide img vGuide)
146 )
147 '(%s)
148 )
150 %s
151 %s
153 (gimp-image-undo-enable img)
154 (gimp-file-save RUN-NONINTERACTIVE img (car (gimp-image-get-active-layer img)) "%s" "%s"))
155 (gimp-quit 0)
156 """ % (filelist, namelist, hGList, vGList, gridSpacingFunc, gridOriginFunc, xcf, xcf)
158 junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
159 f = os.popen('gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk,'w')
160 f.write(script_fu)
161 f.close()
162 # uncomment these lines to see the output from gimp
163 #err = open(junk, 'r')
164 #inkex.debug(err.read())
165 #err.close()
167 x = open(xcf, 'r')
168 sys.stdout.write(x.read())
169 x.close()
171 if __name__ == '__main__':
172 e = MyEffect()
173 e.affect()