From c9b918d330b74b82f4e61cfa537674bcd53ade5f Mon Sep 17 00:00:00 2001 From: Thomas Holder Date: Sat, 3 Nov 2018 22:34:23 +0100 Subject: Image HTTP support Uses Gio::File::load_contents (via URI::getContents) to load images from non-file/non-data URIs. Depends on GVfs. --- src/display/cairo-utils.cpp | 29 +++++++++++++++++++++++++---- src/display/cairo-utils.h | 3 +++ src/object/sp-image.cpp | 8 ++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/display/cairo-utils.cpp b/src/display/cairo-utils.cpp index e4cef786d..516d52678 100644 --- a/src/display/cairo-utils.cpp +++ b/src/display/cairo-utils.cpp @@ -363,6 +363,31 @@ Pixbuf *Pixbuf::create_from_file(std::string const &fn, double svgdpi) std::cerr << " (" << fn << ")" << std::endl; return nullptr; } + + pb = Pixbuf::create_from_buffer(std::move(data), len, svgdpi, fn); + + if (pb) { + pb->_mod_time = stdir.st_mtime; + } + } else { + std::cerr << "Pixbuf::create_from_file: failed to get contents: " << fn << std::endl; + return nullptr; + } + + return pb; +} + +Pixbuf *Pixbuf::create_from_buffer(std::string const &buffer, double svgdpi, std::string const &fn) +{ + auto datacopy = (gchar *)g_memdup(buffer.data(), buffer.size()); + return Pixbuf::create_from_buffer(std::move(datacopy), buffer.size(), svgdpi, fn); +} + +Pixbuf *Pixbuf::create_from_buffer(gchar *&&data, gsize len, double svgdpi, std::string const &fn) +{ + Pixbuf *pb = nullptr; + GError *error = nullptr; + { GdkPixbuf *buf = nullptr; GdkPixbufLoader *loader = nullptr; std::string::size_type idx; @@ -432,7 +457,6 @@ Pixbuf *Pixbuf::create_from_file(std::string const &fn, double svgdpi) if (buf) { g_object_ref(buf); pb = new Pixbuf(buf); - pb->_mod_time = stdir.st_mtime; pb->_path = fn; if (!is_svg) { GdkPixbufFormat *fmt = gdk_pixbuf_loader_get_format(loader); @@ -450,9 +474,6 @@ Pixbuf *Pixbuf::create_from_file(std::string const &fn, double svgdpi) // TODO: we could also read DPI, ICC profile, gamma correction, and other information // from the file. This can be done by using format-specific libraries e.g. libpng. - } else { - std::cerr << "Pixbuf::create_from_file: failed to get contents: " << fn << std::endl; - return nullptr; } return pb; diff --git a/src/display/cairo-utils.h b/src/display/cairo-utils.h index ada78ce51..9c4c9b68b 100644 --- a/src/display/cairo-utils.h +++ b/src/display/cairo-utils.h @@ -119,8 +119,11 @@ public: static Pixbuf *create_from_data_uri(gchar const *uri, double svgdpi = 0); static Pixbuf *create_from_file(std::string const &fn, double svgddpi = 0); + static Pixbuf *create_from_buffer(std::string const &, double svgddpi = 0, std::string const &fn = ""); private: + static Pixbuf *create_from_buffer(gchar *&&, gsize, double svgddpi = 0, std::string const &fn = ""); + void _ensurePixelsARGB32(); void _ensurePixelsPixbuf(); void _forceAlpha(); diff --git a/src/object/sp-image.cpp b/src/object/sp-image.cpp index a66bb3625..ee0efc720 100644 --- a/src/object/sp-image.cpp +++ b/src/object/sp-image.cpp @@ -25,6 +25,7 @@ #include <2geom/rect.h> #include <2geom/transforms.h> #include +#include #include "display/drawing-image.h" #include "display/cairo-utils.h" @@ -590,6 +591,13 @@ Inkscape::Pixbuf *sp_image_repr_read_image(gchar const *href, gchar const *absre if (url.hasScheme("file")) { auto native = url.toNativeFilename(); inkpb = Inkscape::Pixbuf::create_from_file(native.c_str(), svgdpi); + } else { + try { + auto contents = url.getContents(); + inkpb = Inkscape::Pixbuf::create_from_buffer(contents, svgdpi); + } catch (const Gio::Error &e) { + g_warning("URI::getContents failed for '%.100s'", href); + } } } -- cgit v1.2.3