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")
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")
46
shutil.rmtree(self.tmp_dir)
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]
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])
57
47
#create os temp dir
58
self.tmp_dir = tempfile.mkdtemp()
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')
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))
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))
82
hGList = ' '.join(hGuides)
83
vGList = ' '.join(vGuides)
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)
93
#these attributes could be nonexistant
94
spacingX = gridNode.get('spacingx')
95
if spacingX == None: spacingX = '1 '
97
spacingY = gridNode.get('spacingy')
98
if spacingY == None: spacingY = '1 '
100
originX = gridNode.get('originx')
101
if originX == None: originX = '0 '
103
originY = gridNode.get('originy')
104
if originY == None: originY = '0 '
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])
48
tmp_dir = tempfile.mkdtemp()
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')
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))
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)
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')]")
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])
110
95
area = '--export-area-page'
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
118
if node.get("{" + inkex.NSS["inkscape"] + "}label"):
119
name = node.get("{" + inkex.NSS["inkscape"] + "}label")
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)
125
p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
126
return_code = p.wait()
130
_,f,err = os.popen3(command,'r')
135
filename = filename.replace("\\", "/")
136
pngs.append(filename)
137
names.append(name.encode('utf-8'))
140
inkex.errormsg(gettext.gettext('This extension requires at least one non empty layer.'))
102
filename = os.path.join(tmp_dir, name)
103
command = "inkscape -i %s -j %s -e %s %s " % (id, area, filename, svg_file)
105
p = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
106
return_code = p.wait()
110
_,f,err = os.popen3(command,'r')
114
pngs.append(filename)
144
117
filelist = '"%s"' % '" "'.join(pngs)
145
118
namelist = '"%s"' % '" "'.join(names)
146
xcf = os.path.join(self.tmp_dir, "%s.xcf" % docname)
148
xcf = xcf.replace("\\", "/")
119
xcf = os.path.join(tmp_dir, "%s.xcf" % docname)