Code

made it callable by other extensions. the workaround is sorta ugly, but it should...
[inkscape.git] / share / extensions / embedimage.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 '''
20 import inkex, os, base64
22 class Embedder(inkex.Effect):
23     def __init__(self):
24         inkex.Effect.__init__(self)
25         self.OptionParser.add_option("-s", "--selectedonly",
26             action="store", type="inkbool", 
27             dest="selectedonly", default=False,
28             help="embed only selected images")
30     def effect(self):
31         # if slectedonly is enabled and there is a selection only embed selected
32         # images. otherwise embed all images
33         if (self.options.selectedonly):
34             self.embedSelected(self.document, self.selected)
35         else:
36             self.embedAll(self.document)
38     def embedSelected(self, document, selected):
39         self.document=document
40         self.selected=selected
41         if (self.options.ids):
42             for id, node in selected.iteritems():
43                 if node.tagName == 'image':
44                     self.embedImage(node)
46     def embedAll(self, document):
47         self.document=document #not that nice... oh well
48         ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
49         path = '//image'
50         for node in inkex.xml.xpath.Evaluate(path, self.document, context=ctx):
51             self.embedImage(node)
53     def embedImage(self, node):
54         xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
55         if (xlink.value[:4]!='data'):
56             absref=node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'absref')
57             href=node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
58             svg=self.document.getElementsByTagName('svg')[0]
59             docbase=svg.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docbase')
61             path=''
62             #path selection strategy:
63             # 1. href if absolute
64             # 2. sodipodi:docbase + href
65             # 3. realpath-ified href
66             # 4. absref, only if the above does not point to a file
67             if (href != None):
68                 if (os.path.isabs(href.value)):
69                     path=os.path.realpath(href.value)
70                 elif (docbase != None):
71                     path=os.path.join(docbase.value,href.value)
72                 else:
73                     path=os.path.realpath(href.value)
74             if (not os.path.isfile(path)):
75                 if (absref != None):
76                     path=absref.value
77             if (not os.path.isfile(path)):
78                 inkex.debug('No xlink:href or sodipodi:absref attributes found, or they do not point to an existing file! Unable to embed image.')
79             
80             if (os.path.isfile(path)):
81                 file = open(path,"rb").read()
82                 embed=True
83                 if (file[:4]=='\x89PNG'):
84                     type='image/png'
85                 elif (file[:2]=='\xff\xd8'):
86                     type='image/jpeg'
87                 elif (file[:2]=='BM'):
88                     type='image/bmp'
89                 elif (file[:6]=='GIF87a' or file[:6]=='GIF89a'):
90                     type='image/gif'
91                 #ico files lack any magic... therefore we check the filename instead
92                 elif(path.endswith('.ico')):
93                     type='image/x-icon' #official IANA registered MIME is 'image/vnd.microsoft.icon' tho
94                 else:
95                     embed=False
96                 if (embed):
97                     xlink.value = 'data:%s;base64,%s' % (type, base64.encodestring(file))
98                     node.removeAttributeNS(inkex.NSS[u'sodipodi'],'absref')
99                 else:
100                     inkex.debug("%s is not of type image/png, image/jpeg, image/bmp, image/gif or image/x-icon" % path)
101             else:
102                 inkex.debug("Sorry we could not locate %s" % path)
104 if __name__ == '__main__':
105     e = Embedder()
106     e.affect()