home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / share / extensions / embedimage.py < prev    next >
Text File  |  2011-07-08  |  4KB  |  110 lines

  1. #!/usr/bin/env python 
  2. '''
  3. Copyright (C) 2005,2007 Aaron Spike, aaron@ekips.org
  4.  
  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.
  9.  
  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.
  14.  
  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. '''
  19.  
  20. import inkex, os, base64, urlparse, urllib
  21. import gettext
  22. _ = gettext.gettext
  23.  
  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")
  31.  
  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)
  39.  
  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)
  47.  
  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)
  53.  
  54.     def embedImage(self, node):
  55.         xlink = node.get(inkex.addNS('href','xlink'))
  56.         if xlink is None or xlink[:5] != 'data:':
  57.             absref=node.get(inkex.addNS('absref','sodipodi'))
  58.             url=urlparse.urlparse(xlink)
  59.             href=urllib.url2pathname(url.path)
  60.             
  61.             path=''
  62.             #path selection strategy:
  63.             # 1. href if absolute
  64.             # 2. realpath-ified href
  65.             # 3. absref, only if the above does not point to a file
  66.             if (href != None):
  67.                 path=os.path.realpath(href)
  68.             if (not os.path.isfile(path)):
  69.                 if (absref != None):
  70.                     path=absref
  71.  
  72.             path=unicode(path, "utf-8")
  73.  
  74.             if (not os.path.isfile(path)):
  75.                 inkex.errormsg(_('No xlink:href or sodipodi:absref attributes found, or they do not point to an existing file! Unable to embed image.'))
  76.                 if path:
  77.                     inkex.errormsg(_("Sorry we could not locate %s") % path)
  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.                 elif (file[:4]=='MM\x00\x2a' or file[:4]=='II\x2a\x00'):
  91.                     type='image/tiff'
  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, image/tiff, or image/x-icon") % path)
  103.  
  104. if __name__ == '__main__':
  105.     e = Embedder()
  106.     e.affect()
  107.  
  108.  
  109. # vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 encoding=utf-8 textwidth=99
  110.