Code

working around the additional issue (broken embed/extract/embed cycle) from #1427736...
authoramphi <amphi@users.sourceforge.net>
Tue, 9 Jan 2007 09:24:59 +0000 (09:24 +0000)
committeramphi <amphi@users.sourceforge.net>
Tue, 9 Jan 2007 09:24:59 +0000 (09:24 +0000)
share/extensions/embedimage.py
share/extensions/extractimage.py

index 964d1a5ecfbeea57d4e583120710ee34b9c489a5..81380c165d3f532f7e2354c4f97091cea88eabef 100644 (file)
@@ -45,8 +45,30 @@ class MyEffect(inkex.Effect):
         xlink = node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
         if (xlink.value[:4]!='data'):
             absref=node.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'absref')
-            if (os.path.isfile(absref.value)):
-                file = open(absref.value,"rb").read()
+            href=node.attributes.getNamedItemNS(inkex.NSS[u'xlink'],'href')
+            svg=self.document.getElementsByTagName('svg')[0]
+            docbase=svg.attributes.getNamedItemNS(inkex.NSS[u'sodipodi'],'docbase')
+
+            path=''
+            #path selection strategy:
+            # 1. absref
+            # 2. href if absolute
+            # 3. sodipodi:docbase + href
+            # 4. realpath-ified href
+            if (absref != None):
+                path=absref.value
+            elif (href != None):
+                if (os.path.isabs(href.value)):
+                    path=os.path.realpath(href.value)
+                elif (docbase != None):
+                    path=os.path.join(docbase.value,href.value)
+                else:
+                    path=os.path.realpath(href.value)
+            else:
+                inkex.debug('No xlink:href or sodipodi:absref attributes found! Unable to embed image.')
+            
+            if (os.path.isfile(path)):
+                file = open(path,"rb").read()
                 embed=True
                 if (file[:4]=='\x89PNG'):
                     type='image/png'
@@ -57,7 +79,7 @@ class MyEffect(inkex.Effect):
                 elif (file[:6]=='GIF87a' or file[:6]=='GIF89a'):
                     type='image/gif'
                 #ico files lack any magic... therefore we check the filename instead
-                elif(absref.value.endswith('.ico')):
+                elif(path.endswith('.ico')):
                     type='image/x-icon' #official IANA registered MIME is 'image/vnd.microsoft.icon' tho
                 else:
                     embed=False
@@ -65,8 +87,8 @@ class MyEffect(inkex.Effect):
                     xlink.value = 'data:%s;base64,%s' % (type, base64.encodestring(file))
                     node.removeAttributeNS(inkex.NSS[u'sodipodi'],'absref')
                 else:
-                    inkex.debug("%s is not of type image/png, image/jpeg, image/bmp, image/gif or image/x-icon" % absref.value)
+                    inkex.debug("%s is not of type image/png, image/jpeg, image/bmp, image/gif or image/x-icon" % path)
             else:
-                inkex.debug("Sorry we could not locate %s" % absref.value)
+                inkex.debug("Sorry we could not locate %s" % path)
 e = MyEffect()
 e.affect()
index 5807c9e5b56a0f7947aebda3d824da9345d342fe..f0540ac507f8fc1e35758d0872228f91cde85fd9 100644 (file)
@@ -17,7 +17,7 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 '''
 
-import inkex, base64
+import inkex, base64, os
 
 class MyEffect(inkex.Effect):
     def __init__(self):
@@ -36,7 +36,7 @@ class MyEffect(inkex.Effect):
             'icon':'.ico',
             'gif' :'.gif'
         }
-        ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
+        #ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)
         
         # exbed the first embedded image
         path = self.options.filepath
@@ -60,7 +60,7 @@ class MyEffect(inkex.Effect):
                                 #save
                                 data = base64.decodestring(xlink.value[comma:])
                                 open(path,'wb').write(data)
-                                xlink.value = path
+                                xlink.value = os.path.realpath(path) #absolute for making in-mem cycles work
                             else:
                                 inkex.debug('Difficulty finding the image data.')
                             break