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.
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.
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
20
sys.path.append('/usr/share/inkscape/extensions')
22
sys.path.append('/Applications/Inkscape.app/Contents/Resources/extensions')
24
sys.path.append('C:\Program Files\Inkscape\share\extensions')
26
# We will use the inkex module with the predefined Effect base class.
30
from lxml import etree
31
from copy import deepcopy
33
class JessyInk_Effects(inkex.Effect):
35
# Call the base class constructor.
36
inkex.Effect.__init__(self)
38
self.OptionParser.add_option('--tab', action = 'store', type = 'string', dest = 'what')
40
inkex.NSS[u"jessyink"] = u"https://launchpad.net/jessyink"
44
scriptNodes = self.document.xpath("//svg:script[@jessyink:version='1.5.1']", namespaces=inkex.NSS)
46
if len(scriptNodes) != 1:
47
sys.stderr.write("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 \"Effects\" menu to install or update the JessyInk script.\n\n")
49
baseView = self.document.xpath("//sodipodi:namedview[@id='base']", namespaces=inkex.NSS)
51
if len(baseView) != 1:
52
sys.stderr.write("Could not obtain the selected layer for inclusion of the video element.\n\n")
54
layer = self.document.xpath("//svg:g[@id='" + baseView[0].attrib["{" + inkex.NSS["inkscape"] + "}current-layer"] + "']", namespaces=inkex.NSS)
57
sys.stderr.write("Could not obtain the selected layer for inclusion of the video element.\n\n")
59
# Parse template file.
60
tmplFile = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'jessyInk_video.svg'), 'r')
61
tmplRoot = etree.fromstring(tmplFile.read())
64
elem = deepcopy(tmplRoot.xpath("//svg:g[@jessyink:element='core.video']", namespaces=inkex.NSS)[0])
65
nodeDict = findInternalLinks(elem, tmplRoot)
72
idSubst[key] = getNewId("jessyink.core.video", self.document)
73
deleteIds(nodeDict[key])
74
nodeDict[key].attrib['id'] = idSubst[key]
75
elem.insert(0, nodeDict[key])
77
for ndIter in elem.iter():
78
for attrIter in ndIter.attrib:
79
for entryIter in idSubst:
80
ndIter.attrib[attrIter] = ndIter.attrib[attrIter].replace("#" + entryIter, "#" + idSubst[entryIter])
85
def findInternalLinks(node, docRoot, nodeDict = {}):
86
for entry in re.findall("url\(#.*\)", etree.tostring(node)):
87
linkId = entry[5:len(entry) - 1]
89
if not nodeDict.has_key(linkId):
90
nodeDict[linkId] = deepcopy(docRoot.xpath("//*[@id='" + linkId + "']", namespaces=inkex.NSS)[0])
91
nodeDict = findInternalLinks(nodeDict[linkId], docRoot, nodeDict)
93
for entry in node.iter():
94
if entry.attrib.has_key('{' + inkex.NSS['xlink'] + '}href'):
95
linkId = entry.attrib['{' + inkex.NSS['xlink'] + '}href'][1:len(entry.attrib['{' + inkex.NSS['xlink'] + '}href'])]
97
if not nodeDict.has_key(linkId):
98
nodeDict[linkId] = deepcopy(docRoot.xpath("//*[@id='" + linkId + "']", namespaces=inkex.NSS)[0])
99
nodeDict = findInternalLinks(nodeDict[linkId], docRoot, nodeDict)
103
def getNewId(prefix, docRoot):
106
number = datetime.datetime.now().microsecond
108
while len(docRoot.xpath("//*[@id='" + prefix + str(number) + "']", namespaces=inkex.NSS)) > 0:
111
return prefix + str(number)
114
for entry in node.iter():
115
if entry.attrib.has_key('id'):
116
del entry.attrib['id']
118
# Create effect instance
119
effect = JessyInk_Effects()