Code

01cefacb00cb350956c20c8f20a55a203b30564d
[inkscape.git] / share / extensions / embedimage.py
1 #!/usr/bin/env python 
2 '''
3 Copyright (C) 2005,2007 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.tag == inkex.addNS('image','svg'):
44                     self.embedImage(node)
46     def embedAll(self, document):
47         self.document=document #not that nice... oh well
48         path = '//svg:image'
49         for node in self.document.getroot().xpath(path, namespaces=inkex.NSS):
50             self.embedImage(node)
52     def embedImage(self, node):
53         xlink = node.get(inkex.addNS('href','xlink'))
54         if (xlink[:4]!='data'):
55             absref=node.get(inkex.addNS('absref','sodipodi'))
56             href=xlink
57             svg=self.document.getroot().xpath('/svg:svg', namespaces=inkex.NSS)[0]
58             docbase=svg.get(inkex.addNS('docbase','sodipodi'))
60             path=''
61             #path selection strategy:
62             # 1. href if absolute
63             # 2. sodipodi:docbase + href
64             # 3. realpath-ified href
65             # 4. absref, only if the above does not point to a file
66             if (href != None):
67                 if (os.path.isabs(href)):
68                     path=os.path.realpath(href)
69                 elif (docbase != None):
70                     path=os.path.join(docbase,href)
71                 else:
72                     path=os.path.realpath(href)
73             if (not os.path.isfile(path)):
74                 if (absref != None):
75                     path=absref
76             if (not os.path.isfile(path)):
77                 inkex.debug('No xlink:href or sodipodi:absref attributes found, or they do not point to an existing file! Unable to embed image.')
78             
79             if (os.path.isfile(path)):
80                 file = open(path,"rb").read()
81                 embed=True
82                 if (file[:4]=='\x89PNG'):
83                     type='image/png'
84                 elif (file[:2]=='\xff\xd8'):
85                     type='image/jpeg'
86                 elif (file[:2]=='BM'):
87                     type='image/bmp'
88                 elif (file[:6]=='GIF87a' or file[:6]=='GIF89a'):
89                     type='image/gif'
90                 #ico files lack any magic... therefore we check the filename instead
91                 elif(path.endswith('.ico')):
92                     type='image/x-icon' #official IANA registered MIME is 'image/vnd.microsoft.icon' tho
93                 else:
94                     embed=False
95                 if (embed):
96                     node.set(inkex.addNS('href','xlink'), 'data:%s;base64,%s' % (type, base64.encodestring(file)))
97                     if (absref != None):
98                         del node.attrib[inkex.addNS('absref',u'sodipodi')]
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()
109 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99