From d41eb702951c69bfd362e70bd072a869bf73967a Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 16 Jan 2014 16:05:23 -0500 Subject: Clean up sp-image's read_image, remove duplicate code (bzr r12942) --- src/sp-image.cpp | 57 ++++++++++++++------------------------------------------ 1 file changed, 14 insertions(+), 43 deletions(-) (limited to 'src/sp-image.cpp') diff --git a/src/sp-image.cpp b/src/sp-image.cpp index 8f7a60ca6..9044257f4 100644 --- a/src/sp-image.cpp +++ b/src/sp-image.cpp @@ -649,56 +649,27 @@ Inkscape::DrawingItem* SPImage::show(Inkscape::Drawing &drawing, unsigned int /* Inkscape::Pixbuf *sp_image_repr_read_image(gchar const *href, gchar const *absref, gchar const *base) { Inkscape::Pixbuf *inkpb = 0; - gchar const *filename = href; - + if (filename != NULL) { if (strncmp (filename,"file:",5) == 0) { - gchar *fullname = g_filename_from_uri(filename, NULL, NULL); - if (fullname) { - inkpb = Inkscape::Pixbuf::create_from_file(fullname); - g_free(fullname); - if (inkpb != NULL) { - return inkpb; - } - } + filename = g_filename_from_uri(filename, NULL, NULL); } else if (strncmp (filename,"data:",5) == 0) { /* data URI - embedded image */ filename += 5; - inkpb = Inkscape::Pixbuf::create_from_data_uri(filename); - if (inkpb != NULL) { - return inkpb; - } - } else { - - if (!g_path_is_absolute (filename)) { - /* try to load from relative pos combined with document base*/ - const gchar *docbase = base; - if (!docbase) { - docbase = "."; - } - gchar *fullname = g_build_filename(docbase, filename, NULL); - - // document base can be wrong (on the temporary doc when importing bitmap from a - // different dir) or unset (when doc is not saved yet), so we check for base+href existence first, - // and if it fails, we also try to use bare href regardless of its g_path_is_absolute - if (g_file_test (fullname, G_FILE_TEST_EXISTS) && !g_file_test (fullname, G_FILE_TEST_IS_DIR)) { - inkpb = Inkscape::Pixbuf::create_from_file(fullname); - if (inkpb != NULL) { - g_free (fullname); - return inkpb; - } - } - g_free (fullname); - } + } else if (!g_path_is_absolute (filename)) { + /* try to load from relative pos combined with document base*/ + const gchar *docbase = base; + if (!docbase) docbase = "."; + filename = g_build_filename(docbase, filename, NULL); + } + } - /* try filename as absolute */ - if (g_file_test (filename, G_FILE_TEST_EXISTS) && !g_file_test (filename, G_FILE_TEST_IS_DIR)) { - inkpb = Inkscape::Pixbuf::create_from_file(filename); - if (inkpb != NULL) { - return inkpb; - } - } + if (filename && g_file_test(filename, G_FILE_TEST_EXISTS) ) { + inkpb = Inkscape::Pixbuf::create_from_file(filename); + if (inkpb != NULL) { + // g_free filename? + return inkpb; } } -- cgit v1.2.3 From 47edb3f774f4eca0716960c66cdcdea3954c86b1 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Thu, 16 Jan 2014 22:31:43 -0500 Subject: More cleaning of the image tag handler and seperate filename from load. (bzr r12945) --- src/sp-image.cpp | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) (limited to 'src/sp-image.cpp') diff --git a/src/sp-image.cpp b/src/sp-image.cpp index 9044257f4..f4995ce31 100644 --- a/src/sp-image.cpp +++ b/src/sp-image.cpp @@ -24,6 +24,7 @@ #include <2geom/rect.h> #include <2geom/transforms.h> #include +#include #include "display/drawing-image.h" #include "display/cairo-utils.h" @@ -75,7 +76,7 @@ static void sp_image_set_curve(SPImage *image); -static Inkscape::Pixbuf *sp_image_repr_read_image(gchar const *href, gchar const *absref, gchar const *base ); +gchar const *sp_image_repr_read_filename(gchar const *href, gchar const *absref, gchar const *base ); static void sp_image_update_arenaitem (SPImage *img, Inkscape::DrawingImage *ai); static void sp_image_update_canvas_image (SPImage *image); @@ -333,11 +334,26 @@ void SPImage::update(SPCtx *ctx, unsigned int flags) { if (this->href) { Inkscape::Pixbuf *pixbuf = NULL; - pixbuf = sp_image_repr_read_image ( + + gchar const* filename = sp_image_repr_read_filename ( this->getRepr()->attribute("xlink:href"), this->getRepr()->attribute("sodipodi:absref"), doc->getBase()); - + + if(filename && g_str_has_suffix(filename, ".svg")) { + // TODO: We want to deal with svg images properly. This + // space allows us to do so later. + g_warning("Including svg images tags is not yet supported."); + } else if (filename) { + pixbuf = Inkscape::Pixbuf::create_from_file(filename); + } else { + /* Nope: We do not find any valid pixmap file :-( */ + pixbuf = new Inkscape::Pixbuf( + gdk_pixbuf_new_from_xpm_data((const gchar **) brokenimage_xpm)); + + /* If the xpm doesn't load, our libraries are broken */ + g_assert (pixbuf != NULL); + } if (pixbuf) { // BLIP #if defined(HAVE_LIBLCMS1) || defined(HAVE_LIBLCMS2) @@ -646,9 +662,8 @@ Inkscape::DrawingItem* SPImage::show(Inkscape::Drawing &drawing, unsigned int /* return ai; } -Inkscape::Pixbuf *sp_image_repr_read_image(gchar const *href, gchar const *absref, gchar const *base) +gchar const *sp_image_repr_read_filename(gchar const *href, gchar const *absref, gchar const *base) { - Inkscape::Pixbuf *inkpb = 0; gchar const *filename = href; if (filename != NULL) { @@ -666,37 +681,20 @@ Inkscape::Pixbuf *sp_image_repr_read_image(gchar const *href, gchar const *absre } if (filename && g_file_test(filename, G_FILE_TEST_EXISTS) ) { - inkpb = Inkscape::Pixbuf::create_from_file(filename); - if (inkpb != NULL) { - // g_free filename? - return inkpb; - } + return filename; } /* at last try to load from sp absolute path name */ - filename = absref; - if (filename != NULL) { + if (absref != NULL && g_file_test(absref, G_FILE_TEST_EXISTS)) { // using absref is outside of SVG rules, so we must at least warn the user if ( base != NULL && href != NULL ) { g_warning (" did not resolve to a valid image file (base dir is %s), now trying sodipodi:absref=\"%s\"", href, base, absref); } else { g_warning ("xlink:href did not resolve to a valid image file, now trying sodipodi:absref=\"%s\"", absref); } - - inkpb = Inkscape::Pixbuf::create_from_file(filename); - if (inkpb != NULL) { - return inkpb; - } + return absref; } - /* Nope: We do not find any valid pixmap file :-( */ - GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data((const gchar **) brokenimage_xpm); - inkpb = new Inkscape::Pixbuf(pixbuf); - - /* It should be included xpm, so if it still does not does load, */ - /* our libraries are broken */ - g_assert (inkpb != NULL); - - return inkpb; + return NULL; } /* We assert that realpixbuf is either NULL or identical size to pixbuf */ -- cgit v1.2.3 From 8ce1d4c9f4cad86fb0b1f63c6b25838c8c6d640d Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Fri, 17 Jan 2014 23:51:59 -0500 Subject: Fix missing embeded image condition, kindly caught by suv in bug #1270334 Fixed bugs: - https://launchpad.net/bugs/1270334 (bzr r12954) --- src/sp-image.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/sp-image.cpp') diff --git a/src/sp-image.cpp b/src/sp-image.cpp index f4995ce31..b08d6f9b0 100644 --- a/src/sp-image.cpp +++ b/src/sp-image.cpp @@ -340,13 +340,18 @@ void SPImage::update(SPCtx *ctx, unsigned int flags) { this->getRepr()->attribute("sodipodi:absref"), doc->getBase()); - if(filename && g_str_has_suffix(filename, ".svg")) { + Inkscape::Pixbuf::create_from_data_uri(filename); + if (strncmp (filename,"data:", 5) == 0) { + filename += 5; + pixbuf = Inkscape::Pixbuf::create_from_data_uri(filename); + } else if(filename && g_str_has_suffix(filename, ".svg")) { // TODO: We want to deal with svg images properly. This // space allows us to do so later. g_warning("Including svg images tags is not yet supported."); } else if (filename) { pixbuf = Inkscape::Pixbuf::create_from_file(filename); - } else { + } + if(!pixbuf) { /* Nope: We do not find any valid pixmap file :-( */ pixbuf = new Inkscape::Pixbuf( gdk_pixbuf_new_from_xpm_data((const gchar **) brokenimage_xpm)); @@ -671,7 +676,7 @@ gchar const *sp_image_repr_read_filename(gchar const *href, gchar const *absref, filename = g_filename_from_uri(filename, NULL, NULL); } else if (strncmp (filename,"data:",5) == 0) { /* data URI - embedded image */ - filename += 5; + return filename; } else if (!g_path_is_absolute (filename)) { /* try to load from relative pos combined with document base*/ const gchar *docbase = base; -- cgit v1.2.3