Code

share/extensions/*.py: Use gettext for (many) error messages.
[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
21 import gettext
22 _ = gettext.gettext
24 class Embedder(inkex.Effect):
25     def __init__(self):
26         inkex.Effect.__init__(self)
27         self.OptionParser.add_option("-s", "--selectedonly",
28             action="store", type="inkbool", 
29             dest="selectedonly", default=False,
30             help="embed only selected images")
32     def effect(self):
33         # if slectedonly is enabled and there is a selection only embed selected
34         # images. otherwise embed all images
35         if (self.options.selectedonly):
36             self.embedSelected(self.document, self.selected)
37         else:
38             self.embedAll(self.document)
40     def embedSelected(self, document, selected):
41         self.document=document
42         self.selected=selected
43         if (self.options.ids):
44             for id, node in selected.iteritems():
45                 if node.tag == inkex.addNS('image','svg'):
46                     self.embedImage(node)
48     def embedAll(self, document):
49         self.document=document #not that nice... oh well
50         path = '//svg:image'
51         for node in self.document.getroot().xpath(path, namespaces=inkex.NSS):
52             self.embedImage(node)
54     def embedImage(self, node):
55         xlink = node.get(inkex.addNS('href','xlink'))
56         if (xlink[:4]!='data'):
57             absref=node.get(inkex.addNS('absref','sodipodi'))
58             href=xlink
59             svg=self.document.getroot().xpath('/svg:svg', namespaces=inkex.NSS)[0]
60             docbase=svg.get(inkex.addNS('docbase','sodipodi'))
62             path=''
63             #path selection strategy:
64             # 1. href if absolute
65             # 2. sodipodi:docbase + href
66             # 3. realpath-ified href
67             # 4. absref, only if the above does not point to a file
68             if (href != None):
69                 if (os.path.isabs(href)):
70                     path=os.path.realpath(href)
71                 elif (docbase != None):
72                     path=os.path.join(docbase,href)
73                 else:
74                     path=os.path.realpath(href)
75             if (not os.path.isfile(path)):
76                 if (absref != None):
77                     path=absref
78             if (not os.path.isfile(path)):
79                 inkex.errormsg(_('No xlink:href or sodipodi:absref attributes found, or they do not point to an existing file! Unable to embed image.'))
80             
81             if (os.path.isfile(path)):
82                 file = open(path,"rb").read()
83                 embed=True
84                 if (file[:4]=='\x89PNG'):
85                     type='image/png'
86                 elif (file[:2]=='\xff\xd8'):
87                     type='image/jpeg'
88                 elif (file[:2]=='BM'):
89                     type='image/bmp'
90                 elif (file[:6]=='GIF87a' or file[:6]=='GIF89a'):
91                     type='image/gif'
92                 #ico files lack any magic... therefore we check the filename instead
93                 elif(path.endswith('.ico')):
94                     type='image/x-icon' #official IANA registered MIME is 'image/vnd.microsoft.icon' tho
95                 else:
96                     embed=False
97                 if (embed):
98                     node.set(inkex.addNS('href','xlink'), 'data:%s;base64,%s' % (type, base64.encodestring(file)))
99                     if (absref != None):
100                         del node.attrib[inkex.addNS('absref',u'sodipodi')]
101                 else:
102                     inkex.errormsg(_("%s is not of type image/png, image/jpeg, image/bmp, image/gif or image/x-icon") % path)
103             else:
104                 inkex.errormsg(_("Sorry we could not locate %s") % path)
106 if __name__ == '__main__':
107     e = Embedder()
108     e.affect()
111 # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99