Code

Updated the JessyInk extensions to version 1.5.4.
[inkscape.git] / share / extensions / jessyInk_export.py
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
33 import gettext
34 _ = gettext.gettext
36 def propStrToDict(inStr):
37         dictio = {}
39         for prop in inStr.split(";"):
40                 values = prop.split(":")
42                 if (len(values) == 2):
43                         dictio[values[0].strip()] = values[1].strip()
45         return dictio
47 def dictToPropStr(dictio):
48         str = ""
50         for key in dictio.keys():
51                 str += " " + key + ":" + dictio[key] + ";" 
53         return str[1:]
55 def setStyle(node, propKey, propValue):
56         props = {}
58         if node.attrib.has_key("style"):
59                 props = propStrToDict(node.get("style"))
60         
61         props[propKey] = propValue
62         node.set("style", dictToPropStr(props))
64 class MyEffect(inkex.Effect):
65         inkscapeCommand = None
66         zipFile = None
68         def __init__(self):
69                 inkex.Effect.__init__(self)
71                 self.OptionParser.add_option('--tab', action = 'store', type = 'string', dest = 'what')
72                 self.OptionParser.add_option('--type', action = 'store', type = 'string', dest = 'type', default = '')
73                 self.OptionParser.add_option('--resolution', action = 'store', type = 'string', dest = 'resolution', default = '')
75                 # Register jessyink namespace.
76                 inkex.NSS[u"jessyink"] = u"https://launchpad.net/jessyink"
78                 # Set inkscape command.
79                 self.inkscapeCommand = self.findInkscapeCommand()
81                 if (self.inkscapeCommand == None):
82                         inkex.errormsg(_("Could not find Inkscape command.\n"))
83                         sys.exit(1)
85         def output(self):
86                 pass
88         def effect(self):
89                 # Remove any temporary files that might be left from last time.
90                 self.removeJessyInkFilesInTempDir()
92                 # Check whether the JessyInk-script is present (indicating that the presentation has not been properly exported).
93                 scriptNodes = self.document.xpath("//svg:script[@jessyink:version]", namespaces=inkex.NSS)
95                 if len(scriptNodes) != 0:
96                         inkex.errormsg(_("The JessyInk script is not installed in this SVG file or has a different version than the JessyInk extensions. Please select \"install/update...\" from the \"JessyInk\" sub-menu of the \"Extensions\" menu to install or update the JessyInk script.\n\n"))
98                 zipFileDesc, zpFile = tempfile.mkstemp(suffix=".zip", prefix="jessyInk__")
100                 output = zipfile.ZipFile(zpFile, "w", compression=zipfile.ZIP_STORED)
102                 # Find layers.
103                 exportNodes = self.document.xpath("//svg:g[@inkscape:groupmode='layer']", namespaces=inkex.NSS)
105                 if len(exportNodes) < 1:
106                         sys.stderr.write("No layers found.")
108                 for node in exportNodes:
109                         setStyle(node, "display", "none")
111                 for node in exportNodes:
112                         setStyle(node, "display", "inherit")
113                         setStyle(node, "opacity", "1")
114                         self.takeSnapshot(output, node.attrib["{" + inkex.NSS["inkscape"] + "}label"])
115                         setStyle(node, "display", "none")
116                                 
117                 # Write temporary zip file to stdout.
118                 output.close()
119                 out = open(zpFile,'rb')
121                 # Switch stdout to binary on Windows.
122                 if sys.platform == "win32":
123                         import os, msvcrt
124                         msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
126                 # Output the file.
127                 sys.stdout.write(out.read())
128                 sys.stdout.close()
129                 out.close()
131                 # Delete temporary files.
132                 self.removeJessyInkFilesInTempDir()
134         # Function to export the current state of the file using Inkscape.
135         def takeSnapshot(self, output, fileName):
136                 # Write the svg file.
137                 svgFileDesc, svgFile = tempfile.mkstemp(suffix=".svg", prefix="jessyInk__")
138                 self.document.write(os.fdopen(svgFileDesc, "wb"))
139                 
140                 ext = str(self.options.type).lower()
142                 # Prepare output file.
143                 outFileDesc, outFile = tempfile.mkstemp(suffix="." + ext, prefix="jessyInk__")
144                                 
145                 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)
146                 stdout_value, stderr_value = proc.communicate()
147                 
148                 output.write(outFile, fileName + "." + ext)
150         # Function to remove any temporary files created during the export.
151         def removeJessyInkFilesInTempDir(self):
152                 for infile in glob.glob(os.path.join(tempfile.gettempdir(), 'jessyInk__*')):
153                         try:
154                                 os.remove(infile)
155                         except:
156                                 pass
157                                 
158         # Function to try and find the correct command to invoke Inkscape.
159         def findInkscapeCommand(self):
160                 commands = []
161                 commands.append("inkscape")
162                 commands.append("C:\Program Files\Inkscape\inkscape.exe")
163                 commands.append("/Applications/Inkscape.app/Contents/Resources/bin/inkscape")
165                 for command in commands:
166                         proc = subprocess.Popen([command + " --without-gui --version"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
167                         stdout_value, stderr_value = proc.communicate()
169                         if proc.returncode == 0:
170                                 return command
172                 return None
174 e = MyEffect()
175 e.affect()