~ubuntu-branches/ubuntu/natty/inkscape/natty

« back to all changes in this revision

Viewing changes to share/extensions/jessyInk_video.py

  • Committer: Bazaar Package Importer
  • Author(s): Alex Valavanis
  • Date: 2010-09-12 19:44:58 UTC
  • mfrom: (1.1.12 upstream) (45.1.3 maverick)
  • Revision ID: james.westby@ubuntu.com-20100912194458-4sjwmbl7dlsrk5dc
Tags: 0.48.0-1ubuntu1
* Merge with Debian unstable (LP: #628048, LP: #401567, LP: #456248, 
  LP: #463602, LP: #591986)
* debian/control: 
  - Ubuntu maintainers
  - Promote python-lxml, python-numpy, python-uniconvertor to Recommends.
  - Demote pstoedit to Suggests (universe package).
  - Suggests ttf-dejavu instead of ttf-bitstream-vera (LP: #513319)
* debian/rules:
  - Run intltool-update on build (Ubuntu-specific).
  - Add translation domain to .desktop files (Ubuntu-specific).
* debian/dirs:
  - Add usr/share/pixmaps.  Allow inkscape.xpm installation
* drop 50-poppler-API.dpatch (now upstream)
* drop 51-paste-in-unwritable-directory.dpatch (now upstream) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/.
 
15
 
 
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')
 
25
 
 
26
# We will use the inkex module with the predefined Effect base class.
 
27
import inkex
 
28
import os
 
29
import re
 
30
from lxml import etree
 
31
from copy import deepcopy
 
32
 
 
33
class JessyInk_Effects(inkex.Effect):
 
34
        def __init__(self):
 
35
                # Call the base class constructor.
 
36
                inkex.Effect.__init__(self)
 
37
 
 
38
                self.OptionParser.add_option('--tab', action = 'store', type = 'string', dest = 'what')
 
39
 
 
40
                inkex.NSS[u"jessyink"] = u"https://launchpad.net/jessyink"
 
41
 
 
42
        def effect(self):
 
43
                # Check version.
 
44
                scriptNodes = self.document.xpath("//svg:script[@jessyink:version='1.5.1']", namespaces=inkex.NSS)
 
45
 
 
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")
 
48
 
 
49
                baseView = self.document.xpath("//sodipodi:namedview[@id='base']", namespaces=inkex.NSS)
 
50
 
 
51
                if len(baseView) != 1:
 
52
                        sys.stderr.write("Could not obtain the selected layer for inclusion of the video element.\n\n")
 
53
 
 
54
                layer = self.document.xpath("//svg:g[@id='" + baseView[0].attrib["{" + inkex.NSS["inkscape"] + "}current-layer"] + "']", namespaces=inkex.NSS)
 
55
 
 
56
                if (len(layer) != 1):
 
57
                        sys.stderr.write("Could not obtain the selected layer for inclusion of the video element.\n\n")
 
58
 
 
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())
 
62
                tmplFile.close()
 
63
 
 
64
                elem = deepcopy(tmplRoot.xpath("//svg:g[@jessyink:element='core.video']", namespaces=inkex.NSS)[0])
 
65
                nodeDict = findInternalLinks(elem, tmplRoot)
 
66
 
 
67
                deleteIds(elem)
 
68
 
 
69
                idSubst = {}
 
70
 
 
71
                for key in nodeDict:
 
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])
 
76
 
 
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])
 
81
 
 
82
                # Append element.
 
83
                layer[0].append(elem)
 
84
 
 
85
def findInternalLinks(node, docRoot, nodeDict = {}):
 
86
        for entry in re.findall("url\(#.*\)", etree.tostring(node)):
 
87
                linkId = entry[5:len(entry) - 1]
 
88
 
 
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)
 
92
 
 
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'])]
 
96
        
 
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)
 
100
 
 
101
        return nodeDict
 
102
 
 
103
def getNewId(prefix, docRoot):
 
104
        import datetime
 
105
 
 
106
        number = datetime.datetime.now().microsecond
 
107
 
 
108
        while len(docRoot.xpath("//*[@id='" + prefix + str(number) + "']", namespaces=inkex.NSS)) > 0:
 
109
                number += 1
 
110
 
 
111
        return prefix + str(number)
 
112
 
 
113
def deleteIds(node):
 
114
        for entry in node.iter():
 
115
                if entry.attrib.has_key('id'):
 
116
                        del entry.attrib['id']
 
117
 
 
118
# Create effect instance
 
119
effect = JessyInk_Effects()
 
120
effect.affect()
 
121