From 2f340821a78e0b407cd8a6a62726aa8914240221 Mon Sep 17 00:00:00 2001 From: amphi Date: Tue, 9 Jan 2007 09:24:59 +0000 Subject: [PATCH] working around the additional issue (broken embed/extract/embed cycle) from #1427736 2006-02-08 17:57 (by bactisme) --- share/extensions/embedimage.py | 32 +++++++++++++++++++++++++++----- share/extensions/extractimage.py | 6 +++--- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/share/extensions/embedimage.py b/share/extensions/embedimage.py index 964d1a5ec..81380c165 100644 --- a/share/extensions/embedimage.py +++ b/share/extensions/embedimage.py @@ -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() diff --git a/share/extensions/extractimage.py b/share/extensions/extractimage.py index 5807c9e5b..f0540ac50 100644 --- a/share/extensions/extractimage.py +++ b/share/extensions/extractimage.py @@ -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 -- 2.30.2