Code

factor out retrieving export hints from selection and document; use that for create_b...
[inkscape.git] / src / selection-chemistry.cpp
index c26efb71da7860775fa26bb728de28d82b5e1bc8..58decfad76c443bfc87bf74e1c37903ffd5390bd 100644 (file)
 #include "sp-clippath.h"
 #include "sp-mask.h"
 #include "file.h"
+#include "helper/png-write.h"
 #include "layer-fns.h"
 #include "context-fns.h"
 #include <map>
 #include "helper/units.h"
 #include "sp-item.h"
+#include "unit-constants.h"
 using NR::X;
 using NR::Y;
 
@@ -933,6 +935,13 @@ void sp_copy_stuff_used_by_item (GSList **defs_clip, SPItem *item, const GSList
         }
     }
 
+    if (style->filter.filter) {
+        SPObject *filter = style->filter.filter;
+        if (SP_IS_FILTER(filter)) {
+            sp_copy_single (defs_clip, filter);
+        }
+    }
+
     // recurse
     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
         if (SP_IS_ITEM(o))
@@ -1699,10 +1708,10 @@ sp_selection_move(gdouble dx, gdouble dy)
 
     if (dx == 0) {
         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:vertical", SP_VERB_CONTEXT_SELECT, 
-                               _("Nudge vertically"));
+                               _("Move vertically"));
     } else if (dy == 0) {
         sp_document_maybe_done(sp_desktop_document(desktop), "selector:move:horizontal", SP_VERB_CONTEXT_SELECT, 
-                               _("Nudge horizontally"));
+                               _("Move horizontally"));
     } else {
         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_SELECT, 
                          _("Move"));
@@ -2138,6 +2147,8 @@ sp_selection_tile(bool apply)
     // bottommost object, after sorting
     SPObject *parent = SP_OBJECT_PARENT (items->data);
 
+    NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
+
     // remember the position of the first item
     gint pos = SP_OBJECT_REPR (items->data)->position();
 
@@ -2165,7 +2176,7 @@ sp_selection_tile(bool apply)
     prefs_set_int_attribute("options.clonecompensation", "value", SP_CLONE_COMPENSATION_UNMOVED);
 
     const gchar *pat_id = pattern_tile (repr_copies, bounds, document,
-                                        NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))), move);
+                                        NR::Matrix(NR::translate(desktop->dt2doc(NR::Point(r.min()[NR::X], r.max()[NR::Y])))) * parent_transform.inverse(), parent_transform * move);
 
     // restore compensation setting
     prefs_set_int_attribute("options.clonecompensation", "value", saved_compensation);
@@ -2173,10 +2184,14 @@ sp_selection_tile(bool apply)
     if (apply) {
         Inkscape::XML::Node *rect = sp_repr_new ("svg:rect");
         rect->setAttribute("style", g_strdup_printf("stroke:none;fill:url(#%s)", pat_id));
-        sp_repr_set_svg_double(rect, "width", bounds.extent(NR::X));
-        sp_repr_set_svg_double(rect, "height", bounds.extent(NR::Y));
-        sp_repr_set_svg_double(rect, "x", bounds.min()[NR::X]);
-        sp_repr_set_svg_double(rect, "y", bounds.min()[NR::Y]);
+
+        NR::Point min = bounds.min() * parent_transform.inverse();
+        NR::Point max = bounds.max() * parent_transform.inverse();
+
+        sp_repr_set_svg_double(rect, "width", max[NR::X] - min[NR::X]);
+        sp_repr_set_svg_double(rect, "height", max[NR::Y] - min[NR::Y]);
+        sp_repr_set_svg_double(rect, "x", min[NR::X]);
+        sp_repr_set_svg_double(rect, "y", min[NR::Y]);
 
         // restore parent and position
         SP_OBJECT_REPR (parent)->appendChild(rect);
@@ -2269,6 +2284,73 @@ sp_selection_untile()
     }
 }
 
+void
+sp_selection_get_export_hints (Inkscape::Selection *selection, const char **filename, float *xdpi, float *ydpi) 
+{
+    if (selection->isEmpty()) {
+        return;
+    }
+
+    const GSList * reprlst = selection->reprList();
+    bool filename_search = TRUE;
+    bool xdpi_search = TRUE;
+    bool ydpi_search = TRUE;
+
+    for(; reprlst != NULL &&
+            filename_search &&
+            xdpi_search &&
+            ydpi_search;
+        reprlst = reprlst->next) {
+        const gchar * dpi_string;
+        Inkscape::XML::Node * repr = (Inkscape::XML::Node *)reprlst->data;
+
+        if (filename_search) {
+            *filename = repr->attribute("inkscape:export-filename");
+            if (*filename != NULL)
+                filename_search = FALSE;
+        }
+
+        if (xdpi_search) {
+            dpi_string = NULL;
+            dpi_string = repr->attribute("inkscape:export-xdpi");
+            if (dpi_string != NULL) {
+                *xdpi = atof(dpi_string);
+                xdpi_search = FALSE;
+            }
+        }
+
+        if (ydpi_search) {
+            dpi_string = NULL;
+            dpi_string = repr->attribute("inkscape:export-ydpi");
+            if (dpi_string != NULL) {
+                *ydpi = atof(dpi_string);
+                ydpi_search = FALSE;
+            }
+        }
+    }
+}
+
+void
+sp_document_get_export_hints (SPDocument * doc, const char **filename, float *xdpi, float *ydpi) 
+{
+    Inkscape::XML::Node * repr = sp_document_repr_root(doc);
+    const gchar * dpi_string;
+
+    *filename = repr->attribute("inkscape:export-filename");
+
+    dpi_string = NULL;
+    dpi_string = repr->attribute("inkscape:export-xdpi");
+    if (dpi_string != NULL) {
+        *xdpi = atof(dpi_string);
+    }
+
+    dpi_string = NULL;
+    dpi_string = repr->attribute("inkscape:export-ydpi");
+    if (dpi_string != NULL) {
+        *ydpi = atof(dpi_string);
+    }
+}
+
 void
 sp_selection_create_bitmap_copy ()
 {
@@ -2324,18 +2406,35 @@ sp_selection_create_bitmap_copy ()
     // Calculate resolution
     double res;
     int const prefs_res = prefs_get_int_attribute ("options.createbitmap", "resolution", 0);
+    int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 0);
     if (0 < prefs_res) {
         // If it's given explicitly in prefs, take it
         res = prefs_res;
+    } else if (0 < prefs_min) {
+        // If minsize is given, look up minimum bitmap size (default 250 pixels) and calculate resolution from it
+        res = PX_PER_IN * prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
     } else {
-        // Otherwise look up minimum bitmap size (default 250 pixels) and calculate resolution from it
-        int const prefs_min = prefs_get_int_attribute ("options.createbitmap", "minsize", 250);
-        res = prefs_min / MIN ((bbox.x1 - bbox.x0), (bbox.y1 - bbox.y0));
+        float hint_xdpi = 0, hint_ydpi = 0;
+        const char *hint_filename;
+        // take resolution hint from the selected objects
+        sp_selection_get_export_hints (selection, &hint_filename, &hint_xdpi, &hint_ydpi);
+        if (hint_xdpi != 0) {
+            res = hint_xdpi;
+        } else {
+            // take resolution hint from the document
+            sp_document_get_export_hints (document, &hint_filename, &hint_xdpi, &hint_ydpi);
+            if (hint_xdpi != 0) {
+                res = hint_xdpi;
+            } else {
+                // if all else fails, take the default 90 dpi
+                res = PX_PER_IN;
+            }
+        }
     }
 
     // The width and height of the bitmap in pixels
-    unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res);
-    unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res);
+    unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
+    unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
 
     // Find out if we have to run a filter
     const gchar *run = NULL;
@@ -2363,7 +2462,12 @@ sp_selection_create_bitmap_copy ()
 
     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
     NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
-    NR::Matrix t = NR::scale (1/res, -1/res) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
+    NR::Matrix t;
+    if (res == PX_PER_IN) { // for default 90 dpi, snap it to pixel grid
+        t = NR::scale (1, -1) * NR::translate ((unsigned) (bbox.x0 + 0.5), (unsigned) (bbox.y1 + 0.5)) * eek.inverse();
+    } else {
+        t = NR::scale (1, -1) * NR::translate (bbox.x0, bbox.y1) * eek.inverse();
+    }
 
     // Do the export
     sp_export_png_file(document, filepath,
@@ -2389,8 +2493,13 @@ sp_selection_create_bitmap_copy ()
         Inkscape::XML::Node * repr = sp_repr_new ("svg:image");
         repr->setAttribute("xlink:href", filename);
         repr->setAttribute("sodipodi:absref", filepath);
-        sp_repr_set_svg_double(repr, "width", gdk_pixbuf_get_width(pb));
-        sp_repr_set_svg_double(repr, "height", gdk_pixbuf_get_height(pb));
+        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);
+        } else {
+            sp_repr_set_svg_double(repr, "width", (bbox.x1 - bbox.x0));
+            sp_repr_set_svg_double(repr, "height", (bbox.y1 - bbox.y0));
+        }
 
         // Write transform
         gchar c[256];
@@ -2462,6 +2571,7 @@ sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
     // create a list of duplicates
     GSList *mask_items = NULL;
     GSList *apply_to_items = NULL;
+    GSList *items_to_delete = NULL;
     bool topmost = prefs_get_int_attribute ("options.maskobject", "topmost", 1);
     bool remove_original = prefs_get_int_attribute ("options.maskobject", "remove", 1);
     
@@ -2475,7 +2585,7 @@ sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
 
             if (remove_original) {
                 SPObject *item = SP_OBJECT (i->data);
-                item->deleteObject (false);
+                items_to_delete = g_slist_prepend (items_to_delete, item);
             }
         }
     } else if (!topmost) {
@@ -2486,7 +2596,7 @@ sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
 
         if (remove_original) {
             SPObject *item = SP_OBJECT (i->data);
-            item->deleteObject (false);
+            items_to_delete = g_slist_prepend (items_to_delete, item);
         }
         
         for (i = i->next; i != NULL; i = i->next) {
@@ -2503,7 +2613,7 @@ sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
 
         if (remove_original) {
             SPObject *item = SP_OBJECT (i->data);
-            item->deleteObject (false);
+            items_to_delete = g_slist_prepend (items_to_delete, item);
         }
     }
     
@@ -2539,6 +2649,12 @@ sp_selection_set_mask(bool apply_clip_path, bool apply_to_layer)
     g_slist_free (mask_items);
     g_slist_free (apply_to_items);
 
+    for (GSList *i = items_to_delete; NULL != i; i = i->next) {
+        SPObject *item = SP_OBJECT (i->data);
+        item->deleteObject (false);
+    }
+    g_slist_free (items_to_delete);
+
     if (apply_clip_path) 
         sp_document_done (document, SP_VERB_OBJECT_SET_CLIPPATH, _("Set clipping path"));
     else