Code

Always embed bitmap copies created with Alt+B.
authorKrzysztof Kosiński <tweenk.pl@gmail.com>
Fri, 5 Mar 2010 00:15:39 +0000 (01:15 +0100)
committerKrzysztof Kosiński <tweenk.pl@gmail.com>
Fri, 5 Mar 2010 00:15:39 +0000 (01:15 +0100)
src/extension/internal/gdkpixbuf-input.cpp
src/selection-chemistry.cpp
src/sp-image.cpp
src/sp-image.h

index 47cde4a5acbffd5cff24b3294fe70d6bee253c7a..4f91825e2904e0b7a08004b971564a091baf1b30 100644 (file)
@@ -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);
index 3bfde6de969c08467cf6c8475f1ec1dd3807d731..7f8fafea5c4b89bfaea6f91dc037682949159ef8 100644 (file)
@@ -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);
index 22b83670359662d3922bd37dd997d5f8adf6907a..8ef7969cd10d877a45242ee8dcee530b64b8b8a8 100644 (file)
@@ -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 ) {
index 7b00e0da360601240f871a23e8d2027b360ed6d0..172cd711885de1a1df04a4413d1c7bda8e33f4b1 100644 (file)
@@ -26,6 +26,7 @@ class SPImageClass;
 /* SPImage */
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
+#include <glibmm/ustring.h>
 #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