From f49803d679cb83129d976d09112fd372eba71e68 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Krzysztof=20Kosi=C5=84ski?= Date: Fri, 5 Mar 2010 01:15:39 +0100 Subject: [PATCH] Always embed bitmap copies created with Alt+B. --- src/extension/internal/gdkpixbuf-input.cpp | 23 ++------------- src/selection-chemistry.cpp | 8 ++--- src/sp-image.cpp | 34 ++++++++++++++++++++++ src/sp-image.h | 3 +- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/extension/internal/gdkpixbuf-input.cpp b/src/extension/internal/gdkpixbuf-input.cpp index 47cde4a5a..4f91825e2 100644 --- a/src/extension/internal/gdkpixbuf-input.cpp +++ b/src/extension/internal/gdkpixbuf-input.cpp @@ -8,6 +8,7 @@ #include "extension/system.h" #include "gdkpixbuf-input.h" #include "selection-chemistry.h" +#include "sp-image.h" namespace Inkscape { @@ -82,27 +83,7 @@ GdkpixbufInput::open(Inkscape::Extension::Input *mod, char const *uri) sp_repr_set_svg_double(image_node, "height", height); if (embed) { - // Save pixbuf as JPEG or PNG for embedding - gchar *data; - gsize length; - gdk_pixbuf_save_to_buffer(pb, &data, &length, is_lossy ? "jpeg" : "png", NULL, NULL); - - // Save base64 encoded data in image node - // this formula taken from Glib docs - guint needed_size = length * 4 / 3 + length * 4 / (3 * 72) + 7; - needed_size += 5 + 8 + mime_type.size(); // 5 bytes for data:, 8 for ;base64, - - gchar *buffer = (gchar *) g_malloc(needed_size), *buf_work = buffer; - buf_work += g_sprintf(buffer, "data:%s;base64,", mime_type.data()); - - gint state = 0, save = 0; - gsize written = 0; - written += g_base64_encode_step((guchar*) data, length, TRUE, buf_work, &state, &save); - written += g_base64_encode_close(TRUE, buf_work + written, &state, &save); - buf_work[written] = 0; // null terminate - - image_node->setAttribute("xlink:href", buffer); - g_free(buffer); + sp_embed_image(image_node, pb, mime_type); } else { // convert filename to uri gchar* _uri = g_filename_to_uri(uri, NULL, NULL); diff --git a/src/selection-chemistry.cpp b/src/selection-chemistry.cpp index 3bfde6de9..7f8fafea5 100644 --- a/src/selection-chemistry.cpp +++ b/src/selection-chemistry.cpp @@ -43,6 +43,7 @@ SPCycleType SP_CYCLING = SP_CYCLE_FOCUS; #include "sp-tref.h" #include "sp-flowtext.h" #include "sp-flowregion.h" +#include "sp-image.h" #include "text-editing.h" #include "text-context.h" #include "connector-context.h" @@ -2770,12 +2771,7 @@ sp_selection_create_bitmap_copy(SPDesktop *desktop) if (pb) { // Create the repr for the image Inkscape::XML::Node * repr = xml_doc->createElement("svg:image"); - { - repr->setAttribute("sodipodi:absref", filepath); - gchar *abs_base = Inkscape::XML::calc_abs_doc_base(document->base); - repr->setAttribute("xlink:href", sp_relative_path_from_path(filepath, abs_base)); - g_free(abs_base); - } + sp_embed_image(repr, pb, "image/png"); if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid sp_repr_set_svg_double(repr, "width", width); sp_repr_set_svg_double(repr, "height", height); diff --git a/src/sp-image.cpp b/src/sp-image.cpp index 22b836703..8ef7969cd 100644 --- a/src/sp-image.cpp +++ b/src/sp-image.cpp @@ -1590,6 +1590,40 @@ sp_image_get_curve (SPImage *image) return result; } +void +sp_embed_image(Inkscape::XML::Node *image_node, GdkPixbuf *pb, Glib::ustring const &mime_in) +{ + Glib::ustring format, mime; + if (mime_in == "image/jpeg") { + mime = mime_in; + format = "jpeg"; + } else { + mime = "image/png"; + format = "png"; + } + + gchar *data; + gsize length; + gdk_pixbuf_save_to_buffer(pb, &data, &length, format.data(), NULL, NULL); + + // Save base64 encoded data in image node + // this formula taken from Glib docs + guint needed_size = length * 4 / 3 + length * 4 / (3 * 72) + 7; + needed_size += 5 + 8 + mime.size(); // 5 bytes for data:, 8 for ;base64, + + gchar *buffer = (gchar *) g_malloc(needed_size), *buf_work = buffer; + buf_work += g_sprintf(buffer, "data:%s;base64,", mime.data()); + + gint state = 0, save = 0; + gsize written = 0; + written += g_base64_encode_step((guchar*) data, length, TRUE, buf_work, &state, &save); + written += g_base64_encode_close(TRUE, buf_work + written, &state, &save); + buf_work[written] = 0; // null terminate + + image_node->setAttribute("xlink:href", buffer); + g_free(buffer); +} + void sp_image_refresh_if_outdated( SPImage* image ) { if ( image->href && image->lastMod ) { diff --git a/src/sp-image.h b/src/sp-image.h index 7b00e0da3..172cd7118 100644 --- a/src/sp-image.h +++ b/src/sp-image.h @@ -26,6 +26,7 @@ class SPImageClass; /* SPImage */ #include +#include #include "svg/svg-length.h" #include "sp-item.h" #include "display/display-forward.h" @@ -65,7 +66,7 @@ GType sp_image_get_type (void); /* Return duplicate of curve or NULL */ SPCurve *sp_image_get_curve (SPImage *image); - +void sp_embed_image(Inkscape::XML::Node *imgnode, GdkPixbuf *pb, Glib::ustring const &mime); void sp_image_refresh_if_outdated( SPImage* image ); #endif -- 2.30.2