~ubuntu-branches/ubuntu/utopic/inkscape/utopic-proposed

« back to all changes in this revision

Viewing changes to share/extensions/gimp_xcf.py

Tags: upstream-0.48.0
ImportĀ upstreamĀ versionĀ 0.48.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python 
2
2
'''
3
3
Copyright (C) 2006 Aaron Spike, aaron@ekips.org
4
 
Copyright (C) 2010 Nicolas Dufour, nicoduf@yahoo.fr (Windows support and various fixes)
5
4
 
6
5
This program is free software; you can redistribute it and/or modify
7
6
it under the terms of the GNU General Public License as published by
18
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
18
'''
20
19
import inkex
21
 
import sys, os, tempfile, shutil
22
 
import gettext
 
20
import sys, os, tempfile
23
21
 
24
22
try:
25
23
    from subprocess import Popen, PIPE
30
28
class MyEffect(inkex.Effect):
31
29
    def __init__(self):
32
30
        inkex.Effect.__init__(self)
33
 
        self.OptionParser.add_option("-d", "--guides",
34
 
                                         action="store", type="inkbool",
35
 
                                         dest="saveGuides", default=False,
36
 
                                         help="Save the Guides with the .XCF")
37
 
        self.OptionParser.add_option("-r", "--grid",
38
 
                                         action="store", type="inkbool",
39
 
                                         dest="saveGrid", default=False,
40
 
                                         help="Save the Grid with the .XCF")
41
 
 
 
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")
42
39
    def output(self):
43
40
        pass
44
 
 
45
 
    def clear_tmp(self):
46
 
        shutil.rmtree(self.tmp_dir)
47
 
 
48
41
    def effect(self):
49
42
        svg_file = self.args[-1]
50
 
        ttmp_orig = self.document.getroot()
51
 
        docname = ttmp_orig.get(inkex.addNS('docname',u'sodipodi'))
52
 
        if docname is None: docname = self.args[-1]
53
 
 
54
 
        pageHeight = int(inkex.unittouu(self.xpathSingle('/svg:svg/@height').split('.')[0]))
55
 
        pageWidth = int(inkex.unittouu(self.xpathSingle('/svg:svg/@width').split('.')[0]))
 
43
        docname = self.xpathSingle('/svg:svg/@sodipodi:docname')[:-4]
 
44
        pageHeight = int(self.xpathSingle('/svg:svg/@height').split('.')[0])
 
45
        pageWidth = int(self.xpathSingle('/svg:svg/@width').split('.')[0])
56
46
 
57
47
        #create os temp dir
58
 
        self.tmp_dir = tempfile.mkdtemp()
59
 
 
60
 
        # Guides
61
 
        hGuides = []
62
 
        vGuides = []
63
 
        valid = 0
64
 
        if self.options.saveGuides:
65
 
            guideXpath = "sodipodi:namedview/sodipodi:guide" #grab all guide tags in the namedview tag
66
 
            for guideNode in self.document.xpath(guideXpath, namespaces=inkex.NSS):
67
 
                ori = guideNode.get('orientation')
68
 
                if  ori == '0,1':
69
 
                    #this is a horizontal guide
70
 
                    pos = int(guideNode.get('position').split(',')[1].split('.')[0])
71
 
                    #GIMP doesn't like guides that are outside of the image
72
 
                    if pos > 0 and pos < pageHeight:
73
 
                        #the origin is at the top in GIMP land
74
 
                        hGuides.append(str(pageHeight - pos))
75
 
                elif ori == '1,0':
76
 
                    #this is a vertical guide
77
 
                    pos = int(guideNode.get('position').split(',')[0].split('.')[0])
78
 
                    #GIMP doesn't like guides that are outside of the image
79
 
                    if pos > 0 and pos < pageWidth:
80
 
                        vGuides.append(str(pos))
81
 
 
82
 
        hGList = ' '.join(hGuides)
83
 
        vGList = ' '.join(vGuides)
84
 
 
85
 
        # Grid
86
 
        gridSpacingFunc = ''
87
 
        gridOriginFunc = '' 
88
 
        #GIMP only allows one rectangular grid
89
 
        gridXpath = "sodipodi:namedview/inkscape:grid[@type='xygrid' and (not(@units) or @units='px')]"
90
 
        if (self.options.saveGrid and self.document.xpath(gridXpath, namespaces=inkex.NSS)):
91
 
            gridNode = self.xpathSingle(gridXpath)
92
 
            if gridNode != None:
93
 
                #these attributes could be nonexistant
94
 
                spacingX = gridNode.get('spacingx')
95
 
                if spacingX == None: spacingX = '1  '
96
 
 
97
 
                spacingY = gridNode.get('spacingy')
98
 
                if spacingY == None: spacingY = '1  '
99
 
 
100
 
                originX = gridNode.get('originx')
101
 
                if originX == None: originX = '0  '
102
 
 
103
 
                originY = gridNode.get('originy')
104
 
                if originY == None: originY = '0  '
105
 
 
106
 
                gridSpacingFunc = '(gimp-image-grid-set-spacing img %s %s)' % (spacingX[:-2], spacingY[:-2])
107
 
                gridOriginFunc = '(gimp-image-grid-set-offset img %s %s)'% (originX[:-2], originY[:-2])
108
 
 
109
 
        # Layers
 
48
        tmp_dir = tempfile.mkdtemp()
 
49
 
 
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))
 
69
 
 
70
        hGList = ' '.join(hGuides)
 
71
        vGList = ' '.join(vGuides)
 
72
 
 
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  '
 
82
 
 
83
                        spacingY = gridNode.get('spacingy')
 
84
                        if spacingY == None: spacingY = '1  '
 
85
 
 
86
                        originX = gridNode.get('originx')
 
87
                        if originX == None: originX = '0  '
 
88
 
 
89
                        originY = gridNode.get('originy')
 
90
                        if originY == None: originY = '0  '
 
91
 
 
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])
 
94
 
110
95
        area = '--export-area-page'
111
96
        pngs = []
112
97
        names = []
113
98
        path = "/svg:svg/*[name()='g' or @style][@id]"
114
99
        for node in self.document.xpath(path, namespaces=inkex.NSS):
115
 
            if len(node) > 0: # Get rid of empty layers
116
 
                valid=1
117
 
                id = node.get('id')
118
 
                if node.get("{" + inkex.NSS["inkscape"] + "}label"):
119
 
                    name = node.get("{" + inkex.NSS["inkscape"] + "}label")
120
 
                else:
121
 
                    name = id
122
 
                filename = os.path.join(self.tmp_dir, "%s.png" % id)
123
 
                command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
124
 
                if bsubprocess:
125
 
                    p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
126
 
                    return_code = p.wait()
127
 
                    f = p.stdout
128
 
                    err = p.stderr
129
 
                else:
130
 
                    _,f,err = os.popen3(command,'r')
131
 
                f.read()
132
 
                f.close()
133
 
                err.close()
134
 
                if os.name == 'nt':
135
 
                    filename = filename.replace("\\", "/")
136
 
                pngs.append(filename)
137
 
                names.append(name.encode('utf-8'))
138
 
 
139
 
        if (valid==0):
140
 
            inkex.errormsg(gettext.gettext('This extension requires at least one non empty layer.'))
141
 
            self.clear_tmp()
142
 
            sys.exit(0)
 
100
            id = node.get('id')
 
101
            name = "%s.png" % id
 
102
            filename = os.path.join(tmp_dir, name)
 
103
            command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
 
104
            if bsubprocess:
 
105
                p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
 
106
                return_code = p.wait()
 
107
                f = p.stdout
 
108
                err = p.stderr
 
109
            else:
 
110
                _,f,err = os.popen3(command,'r')
 
111
            f.read()
 
112
            f.close()
 
113
            err.close()
 
114
            pngs.append(filename)
 
115
            names.append(id)
143
116
 
144
117
        filelist = '"%s"' % '" "'.join(pngs)
145
118
        namelist = '"%s"' % '" "'.join(names)
146
 
        xcf = os.path.join(self.tmp_dir, "%s.xcf" % docname)
147
 
        if os.name == 'nt':
148
 
            xcf = xcf.replace("\\", "/")
 
119
        xcf = os.path.join(tmp_dir, "%s.xcf" % docname)
149
120
        script_fu = """
150
121
(tracing 1)
151
122
(define
196
167
(gimp-quit 0)
197
168
        """ % (filelist, namelist, hGList, vGList, gridSpacingFunc, gridOriginFunc, xcf, xcf)
198
169
 
199
 
        junk = os.path.join(self.tmp_dir, 'junk_from_gimp.txt')
 
170
        junk = os.path.join(tmp_dir, 'junk_from_gimp.txt')
200
171
        f = os.popen('gimp -i --batch-interpreter plug-in-script-fu-eval -b - > %s 2>&1' % junk,'w')
201
172
        f.write(script_fu)
202
173
        f.close()
205
176
        #inkex.debug(err.read())
206
177
        #err.close()
207
178
 
208
 
        x = open(xcf, 'rb')
209
 
        if os.name == 'nt':
210
 
            try:
211
 
                import msvcrt
212
 
                msvcrt.setmode(1, os.O_BINARY)
213
 
            except:
214
 
                pass
 
179
        x = open(xcf, 'r')
215
180
        sys.stdout.write(x.read())
216
181
        x.close()
217
 
        self.clear_tmp()
218
 
 
219
182
 
220
183
if __name__ == '__main__':
221
184
    e = MyEffect()