From 91967d2ba3e1f30b1e6430196d01718994081f18 Mon Sep 17 00:00:00 2001 From: Jabier Arraiza Date: Tue, 15 May 2018 23:10:21 +0200 Subject: Fis maren improvements to embed/import --- share/extensions/extractimage.inx | 1 + share/extensions/extractimage.py | 88 +++++++++++++++++++++------------- src/extension/internal/svg.cpp | 8 ++-- src/ui/dialog/inkscape-preferences.cpp | 4 +- 4 files changed, 62 insertions(+), 39 deletions(-) diff --git a/share/extensions/extractimage.inx b/share/extensions/extractimage.inx index 984dc9aef..9d1984168 100644 --- a/share/extensions/extractimage.inx +++ b/share/extensions/extractimage.inx @@ -4,6 +4,7 @@ org.ekips.filter.extractimage extractimage.py inkex.py + True none <_param name="desc" type="description" xml:space="preserve">* Don't type the file extension, it is appended automatically. * A relative path (or a filename without path) is relative to the user's home directory. diff --git a/share/extensions/extractimage.py b/share/extensions/extractimage.py index b30a326a7..fe70de433 100755 --- a/share/extensions/extractimage.py +++ b/share/extensions/extractimage.py @@ -21,17 +21,43 @@ import base64 import os # local library import inkex -import imghdr class MyEffect(inkex.Effect): def __init__(self): inkex.Effect.__init__(self) self.OptionParser.add_option("--desc") + self.OptionParser.add_option("-s", "--selectedonly", + action="store", type="inkbool", + dest="selectedonly", default=True, + help="extract only selected images") self.OptionParser.add_option("--filepath", action="store", type="string", dest="filepath", default=None, help="") + def effect(self): + # if slectedonly is enabled and there is a selection only extractselected + # images. otherwise extract all images + if (self.options.selectedonly): + self.extractSelected(self.document, self.selected) + else: + self.extractAll(self.document) + + def extractSelected(self, document, selected): + self.document=document + self.selected=selected + if (self.options.ids): + for id, node in selected.iteritems(): + if node.tag == inkex.addNS('image','svg'): + self.extractImage(node) + + def extractAll(self, document): + self.document=document #not that nice... oh well + path = '//svg:image' + for node in self.document.getroot().xpath(path, namespaces=inkex.NSS): + self.extractImage(node) + + def extractImage(self, node): mimesubext={ 'png' : '.png', 'svg+xml' : '.svg', @@ -45,38 +71,34 @@ class MyEffect(inkex.Effect): # exbed the first embedded image path = self.options.filepath if (path != ''): - if (self.options.ids): - for id, node in self.selected.iteritems(): - if node.tag == inkex.addNS('image','svg'): - xlink = node.get(inkex.addNS('href','xlink')) - if (xlink[:4]=='data'): - comma = xlink.find(',') - if comma>0: - #get extension - fileext='' - semicolon = xlink.find(';') - if semicolon>0: - for sub in mimesubext.keys(): - if sub in xlink[5:semicolon].lower(): - fileext=mimesubext[sub] - path=path+fileext; - if (not os.path.isabs(path)): - if os.name == 'nt': - path = os.path.join(os.environ['USERPROFILE'],path) - else: - path = os.path.join(os.path.expanduser("~"),path) - break - #save - data = base64.decodestring(xlink[comma:]) - open(path,'wb').write(data) - if fileext == '.png' and imghdr.what(path) != 'png': - os.rename(path, path.replace(".png",".svg")) - path.replace(".png",".svg"); - inkex.errormsg(_('Image extracted to: %s') % path) - node.set(inkex.addNS('href','xlink'),os.path.realpath(path)) #absolute for making in-mem cycles work - else: - inkex.errormsg(_('Unable to find image data.')) - break + if node.tag == inkex.addNS('image','svg'): + xlink = node.get(inkex.addNS('href','xlink')) + if (xlink[:4]=='data'): + comma = xlink.find(',') + if comma>0: + #get extension + fileext='' + semicolon = xlink.find(';') + if semicolon>0: + for sub in mimesubext.keys(): + if sub in xlink[5:semicolon].lower(): + fileext=mimesubext[sub] + pathwext=path+fileext + if os.path.isfile(pathwext): + pathwext=path + "_" + node.get("id") +fileext + if (not os.path.isabs(pathwext)): + if os.name == 'nt': + pathwext = os.path.join(os.environ['USERPROFILE'],pathwext) + else: + pathwext = os.path.join(os.path.expanduser("~"),pathwext) + inkex.errormsg(_('Image extracted to: %s') % pathwext) + break + #save + data = base64.decodestring(xlink[comma:]) + open(pathwext,'wb').write(data) + node.set(inkex.addNS('href','xlink'),os.path.realpath(pathwext)) #absolute for making in-mem cycles work + else: + inkex.errormsg(_('Unable to find image data.')) if __name__ == '__main__': e = MyEffect() diff --git a/src/extension/internal/svg.cpp b/src/extension/internal/svg.cpp index 41fca734f..c4e12c174 100644 --- a/src/extension/internal/svg.cpp +++ b/src/extension/internal/svg.cpp @@ -137,10 +137,10 @@ Svg::init(void) "\n" "" N_("SVG Input") "\n" "" SP_MODULE_KEY_INPUT_SVG "\n" - "\n" - "<_option value='include' >" N_("Include") "\n" - "<_option value='embed' >" N_("Embed") "\n" - "<_option value='link' >" N_("Link") "\n" + "\n" + "<_option value='include' >" N_("Include SVG image as editable object(s) in the current file") "\n" + "<_option value='embed' >" N_("Embed the SVG file in a image tag (not editable in this document)") "\n" + "<_option value='link' >" N_("Link the SVG file in a image tag (not editable in this document).") "\n" "\n" "\n" "<_option value='auto' >" N_("None (auto)") "\n" diff --git a/src/ui/dialog/inkscape-preferences.cpp b/src/ui/dialog/inkscape-preferences.cpp index 6b55da0da..ecf6204de 100644 --- a/src/ui/dialog/inkscape-preferences.cpp +++ b/src/ui/dialog/inkscape-preferences.cpp @@ -1559,14 +1559,14 @@ void InkscapePreferences::initPageBitmaps() Glib::ustring labels[] = {_("Embed"), _("Link")}; Glib::ustring values[] = {"embed", "link"}; _bitmap_link.init("/dialogs/import/link", labels, values, G_N_ELEMENTS(values), "link"); - _page_bitmaps.add_line( false, _("Bitmap link:"), _bitmap_link, "", "", false); + _page_bitmaps.add_line( false, _("Bitmap import/open mode:"), _bitmap_link, "", "", false); } { Glib::ustring labels[] = {_("Include"), _("Embed"), _("Link")}; Glib::ustring values[] = {"include", "embed", "link"}; _svg_link.init("/dialogs/import/link_svg", labels, values, G_N_ELEMENTS(values), "include"); - _page_bitmaps.add_line( false, _("SVG link:"), _svg_link, "", "", false); + _page_bitmaps.add_line( false, _("SVG import mode:"), _svg_link, "", "", false); } { -- cgit v1.2.3