Code

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