1 #!/usr/bin/env python
2 # Copyright 2008, 2009 Hannes Hochreiner
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see http://www.gnu.org/licenses/.
16 # These lines are only needed if you don't put the script directly into
17 # the installation directory
18 import sys
19 # Unix
20 sys.path.append('/usr/share/inkscape/extensions')
21 # OS X
22 sys.path.append('/Applications/Inkscape.app/Contents/Resources/extensions')
23 # Windows
24 sys.path.append('C:\Program Files\Inkscape\share\extensions')
26 import inkex, os.path
27 import subprocess
28 import tempfile
29 import os
30 import zipfile
31 import glob
32 import re
34 def propStrToDict(inStr):
35 dictio = {}
37 for prop in inStr.split(";"):
38 values = prop.split(":")
40 if (len(values) == 2):
41 dictio[values[0].strip()] = values[1].strip()
43 return dictio
45 def dictToPropStr(dictio):
46 str = ""
48 for key in dictio.keys():
49 str += " " + key + ":" + dictio[key] + ";"
51 return str[1:]
53 def setStyle(node, propKey, propValue):
54 props = {}
56 if node.attrib.has_key("style"):
57 props = propStrToDict(node.get("style"))
59 props[propKey] = propValue
60 node.set("style", dictToPropStr(props))
62 class MyEffect(inkex.Effect):
63 inkscapeCommand = None
64 zipFile = None
66 def __init__(self):
67 inkex.Effect.__init__(self)
69 self.OptionParser.add_option('--tab', action = 'store', type = 'string', dest = 'what')
70 self.OptionParser.add_option('--type', action = 'store', type = 'string', dest = 'type', default = '')
71 self.OptionParser.add_option('--resolution', action = 'store', type = 'string', dest = 'resolution', default = '')
73 # Register jessyink namespace.
74 inkex.NSS[u"jessyink"] = u"https://launchpad.net/jessyink"
76 # Set inkscape command.
77 self.inkscapeCommand = self.findInkscapeCommand()
79 if (self.inkscapeCommand == None):
80 sys.stderr.write("Could not find Inkscape command.\n")
81 sys.exit(1)
83 def output(self):
84 pass
86 def effect(self):
87 # Remove any temporary files that might be left from last time.
88 self.removeJessyInkFilesInTempDir()
90 # Check whether the JessyInk-script is present (indicating that the presentation has not been properly exported).
91 scriptNodes = self.document.xpath("//svg:script[@jessyink:version]", namespaces=inkex.NSS)
93 if len(scriptNodes) != 0:
94 sys.stderr.write("The JessyInk-script is present in this SVG file. This indicates that the presentation has not been properly exported. Please visit code.goolge.com/p/jessyink to find information on how to export a JessyInk presentation.\n\n")
96 zipFileDesc, zpFile = tempfile.mkstemp(suffix=".zip", prefix="jessyInk__")
98 output = zipfile.ZipFile(zpFile, "w", compression=zipfile.ZIP_STORED)
100 # Find layers.
101 exportNodes = self.document.xpath("//svg:g[@inkscape:groupmode='layer']", namespaces=inkex.NSS)
103 if len(exportNodes) < 1:
104 sys.stderr.write("No layers found.")
106 for node in exportNodes:
107 setStyle(node, "display", "none")
109 for node in exportNodes:
110 setStyle(node, "display", "inherit")
111 setStyle(node, "opacity", "1")
112 self.takeSnapshot(output, node.attrib["{" + inkex.NSS["inkscape"] + "}label"])
113 setStyle(node, "display", "none")
115 # Write temporary zip file to stdout.
116 output.close()
117 out = open(zpFile,'rb')
119 # Switch stdout to binary on Windows.
120 if sys.platform == "win32":
121 import os, msvcrt
122 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
124 # Output the file.
125 sys.stdout.write(out.read())
126 sys.stdout.close()
127 out.close()
129 # Delete temporary files.
130 self.removeJessyInkFilesInTempDir()
132 # Function to export the current state of the file using Inkscape.
133 def takeSnapshot(self, output, fileName):
134 # Write the svg file.
135 svgFileDesc, svgFile = tempfile.mkstemp(suffix=".svg", prefix="jessyInk__")
136 self.document.write(os.fdopen(svgFileDesc, "wb"))
138 ext = str(self.options.type).lower()
140 # Prepare output file.
141 outFileDesc, outFile = tempfile.mkstemp(suffix="." + ext, prefix="jessyInk__")
143 proc = subprocess.Popen([self.inkscapeCommand + " --file=" + svgFile + " --without-gui --export-dpi=" + str(self.options.resolution) + " --export-" + ext + "=" + outFile], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
144 stdout_value, stderr_value = proc.communicate()
146 output.write(outFile, fileName + "." + ext)
148 # Function to remove any temporary files created during the export.
149 def removeJessyInkFilesInTempDir(self):
150 for infile in glob.glob(os.path.join(tempfile.gettempdir(), 'jessyInk__*')):
151 try:
152 os.remove(infile)
153 except:
154 pass
156 # Function to try and find the correct command to invoke Inkscape.
157 def findInkscapeCommand(self):
158 commands = []
159 commands.append("inkscape")
160 commands.append("C:\Program Files\Inkscape\inkscape.exe")
161 commands.append("/Applications/Inkscape.app/Contents/Resources/bin/inkscape")
163 for command in commands:
164 proc = subprocess.Popen([command + " --without-gui --version"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
165 stdout_value, stderr_value = proc.communicate()
167 if proc.returncode == 0:
168 return command
170 return None
172 e = MyEffect()
173 e.affect()