Code

fix shift+middle button zoom area when there are other modifiers in the mask; safe...
[inkscape.git] / src / path-chemistry.cpp
index aac75756e305ffc7098b146b00f97a15cd9be940..99ee78ade932963fbb01cabfca7e7f0411965935 100644 (file)
 #include "sp-path.h"
 #include "sp-text.h"
 #include "sp-flowtext.h"
-#include "libnr/nr-path.h"
 #include "text-editing.h"
 #include "style.h"
-#include "inkscape.h"
 #include "desktop.h"
 #include "document.h"
 #include "message-stack.h"
 #include "selection.h"
 #include "desktop-handles.h"
 #include "box3d.h"
-
+#include <2geom/pathvector.h>
+#include "selection-chemistry.h"
 #include "path-chemistry.h"
 
-/* Helper functions for sp_selected_path_to_curves */
-static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
-static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
-
-enum {
-    /* Not used yet. This is the placeholder of Lauris's idea. */
-    SP_TOCURVE_INTERACTIVE       = 1 << 0,
-    SP_TOCURVE_GROUPING_BY_WORD  = 1 << 1,
-    SP_TOCURVE_GROUPING_BY_LINE  = 1 << 2,
-    SP_TOCURVE_GROUPING_BY_WHOLE = 1 << 3
-};
-
 void
-sp_selected_path_combine(void)
+sp_selected_path_combine(SPDesktop *desktop)
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
     SPDocument *doc = sp_desktop_document(desktop);
     
-    if (g_slist_length((GSList *) selection->itemList()) < 2) {
-        sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
+    if (g_slist_length((GSList *) selection->itemList()) < 1) {
+        sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to combine."));
         return;
     }
 
@@ -69,10 +55,13 @@ sp_selected_path_combine(void)
     desktop->setWaitingCursor();
 
     GSList *items = g_slist_copy((GSList *) selection->itemList());
+
+    items = sp_degroup_list (items); // descend into any groups in selection
+
     GSList *to_paths = NULL;
     for (GSList *i = items; i != NULL; i = i->next) {
         SPItem *item = (SPItem *) i->data;
-        if (!SP_IS_PATH(item))
+        if (!SP_IS_PATH(item) && !SP_IS_GROUP(item))
             to_paths = g_slist_prepend(to_paths, item);
     }
     GSList *converted = NULL;
@@ -81,6 +70,8 @@ sp_selected_path_combine(void)
     for (GSList *i = converted; i != NULL; i = i->next)
         items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
 
+    items = sp_degroup_list (items); // converting to path may have added more groups, descend again
+
     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
     items = g_slist_reverse(items);
 
@@ -148,17 +139,18 @@ sp_selected_path_combine(void)
         repr->setAttribute("style", style);
         g_free(style);
 
+        repr->setAttribute("inkscape:path-effect", path_effect);
+        g_free(path_effect);
+
         // set path data corresponding to new curve
         gchar *dstring = sp_svg_write_path(curve->get_pathvector());
         curve->unref();
-        repr->setAttribute("d", dstring);
         if (path_effect)
             repr->setAttribute("inkscape:original-d", dstring);
+        else
+            repr->setAttribute("d", dstring);
         g_free(dstring);
 
-        repr->setAttribute("inkscape:path-effect", path_effect);
-        g_free(path_effect);
-
         // add the new group to the parent of the topmost
         parent->appendChild(repr);
 
@@ -180,10 +172,8 @@ sp_selected_path_combine(void)
 }
 
 void
-sp_selected_path_break_apart(void)
+sp_selected_path_break_apart(SPDesktop *desktop)
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
-
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     if (selection->isEmpty()) {
@@ -221,14 +211,14 @@ sp_selected_path_break_apart(void)
         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
         gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
 
-        NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
+        Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
 
         curve->unref();
 
         // it's going to resurrect as one of the pieces, so we delete without advertisement
         SP_OBJECT(item)->deleteObject(false);
 
-        curve = SPCurve::new_from_bpath(abp);
+        curve = new SPCurve(apv);
         g_assert(curve != NULL);
 
         GSList *list = curve->split();
@@ -242,14 +232,15 @@ sp_selected_path_break_apart(void)
             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
             repr->setAttribute("style", style);
 
+            repr->setAttribute("inkscape:path-effect", path_effect);
+
             gchar *str = sp_svg_write_path(curve->get_pathvector());
-            repr->setAttribute("d", str);
             if (path_effect)
                 repr->setAttribute("inkscape:original-d", str);
+            else
+                repr->setAttribute("d", str);
             g_free(str);
 
-            repr->setAttribute("inkscape:path-effect", path_effect);
-
             // add the new repr to the parent
             parent->appendChild(repr);
 
@@ -270,7 +261,7 @@ sp_selected_path_break_apart(void)
         g_slist_free(reprs);
         g_slist_free(list);
         g_free(style);
-
+        g_free(path_effect);
     }
 
     desktop->clearWaitingCursor();
@@ -285,20 +276,8 @@ sp_selected_path_break_apart(void)
 
 /* This function is an entry point from GUI */
 void
-sp_selected_path_to_curves(bool interactive)
+sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
 {
-    if (interactive) {
-        sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
-    } else {
-        sp_selected_path_to_curves0(false, 0);
-    }
-}
-
-static void
-sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
-{
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
-
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     if (selection->isEmpty()) {
@@ -339,7 +318,7 @@ sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy
     }
 }
 
-static bool
+bool
 sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select)
 {
     bool did = false;
@@ -349,6 +328,7 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
          items = items->next) {
 
         SPItem *item = SP_ITEM(items->data);
+       SPDocument *document = item->document;
 
         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
             continue; // already a path, and no path effect
@@ -397,6 +377,10 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
         // remember id
         char const *id = SP_OBJECT_REPR(item)->attribute("id");
+        // remember title
+        gchar *title = item->title();
+        // remember description
+        gchar *desc = item->desc();
 
         // It's going to resurrect, so we delete without notifying listeners.
         SP_OBJECT(item)->deleteObject(false);
@@ -405,6 +389,16 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
         repr->setAttribute("id", id);
         // add the new repr to the parent
         parent->appendChild(repr);
+        SPObject* newObj = document->getObjectByRepr(repr);
+        if (title && newObj) {
+               newObj->setTitle(title);
+               g_free(title);
+        }
+        if (desc && newObj) {
+               newObj->setDesc(desc);
+               g_free(desc);
+        }
+
         // move to the saved position
         repr->setPosition(pos > 0 ? pos : 0);
 
@@ -423,25 +417,98 @@ sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
     if (!item)
         return NULL;
 
+    Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
+
+    if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
+        // Special treatment for text: convert each glyph to separate path, then group the paths
+        Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
+        g_repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
+        /* Mask */
+        gchar *mask_str = (gchar *) SP_OBJECT_REPR(item)->attribute("mask");
+        if ( mask_str )
+            g_repr->setAttribute("mask", mask_str);
+        /* Clip path */
+        gchar *clip_path_str = (gchar *) SP_OBJECT_REPR(item)->attribute("clip-path");
+        if ( clip_path_str )
+            g_repr->setAttribute("clip-path", clip_path_str);
+        /* Rotation center */
+        g_repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
+        g_repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
+        /* Whole text's style */
+        gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(item),
+                                             SP_OBJECT_STYLE(SP_OBJECT_PARENT(item)));
+        g_repr->setAttribute("style", style_str);
+        g_free(style_str);
+        Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); 
+        do {
+            Inkscape::Text::Layout::iterator iter_next = iter;
+            iter_next.nextGlyph(); // iter_next is one glyph ahead from iter
+            if (iter == iter_next)
+                break;
+
+            /* This glyph's style */
+            SPObject const *pos_obj = 0;
+            void *rawptr = 0;
+            te_get_layout(item)->getSourceOfCharacter(iter, &rawptr);
+            if (!rawptr || !SP_IS_OBJECT(rawptr)) // no source for glyph, abort
+                break;
+            pos_obj = SP_OBJECT(rawptr);
+            while (SP_IS_STRING(pos_obj) && SP_OBJECT_PARENT(pos_obj)) {
+               pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
+            }
+            gchar *style_str = sp_style_write_difference(SP_OBJECT_STYLE(pos_obj),
+                                                 SP_OBJECT_STYLE(SP_OBJECT_PARENT(pos_obj)));
+
+            // get path from iter to iter_next:
+            SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next);
+            iter = iter_next; // shift to next glyph
+            if (!curve) { // error converting this glyph
+                g_free (style_str);
+                continue;
+            }
+            if (curve->is_empty()) { // whitespace glyph?
+                curve->unref();
+                g_free (style_str);
+                continue;
+            }
+
+            Inkscape::XML::Node *p_repr = xml_doc->createElement("svg:path");
+
+            gchar *def_str = sp_svg_write_path(curve->get_pathvector());
+            p_repr->setAttribute("d", def_str);
+            g_free(def_str);
+            curve->unref();
+
+            p_repr->setAttribute("style", style_str);
+            g_free(style_str);
+
+            g_repr->appendChild(p_repr);
+            Inkscape::GC::release(p_repr);
+
+            if (iter == te_get_layout(item)->end())
+                break;
+
+        } while (true);
+
+        return g_repr;
+    }
+
     SPCurve *curve = NULL;
     if (SP_IS_SHAPE(item)) {
         curve = sp_shape_get_curve(SP_SHAPE(item));
-    } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
-        curve = te_get_layout(item)->convertToCurves();
-    }
+    } 
 
     if (!curve)
         return NULL;
 
     // Prevent empty paths from being added to the document
     // otherwise we end up with zomby markup in the SVG file
-    if(curve->get_length() <= 0)
+    if(curve->is_empty())
     {
         curve->unref();
         return NULL;
     }
 
-    Inkscape::XML::Document *xml_doc = SP_OBJECT_REPR(item)->document();
     Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
     /* Transformation */
     repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
@@ -474,12 +541,9 @@ sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
 }
 
 
-// FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
 void
-sp_selected_path_reverse()
+sp_selected_path_reverse(SPDesktop *desktop)
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
-
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
     GSList *items = (GSList *) selection->itemList();
 
@@ -514,6 +578,13 @@ sp_selected_path_reverse()
         g_free(str);
 
         rcurve->unref();
+
+        // reverse nodetypes order (Bug #179866)
+        gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
+        if ( nodetypes ) {
+            SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
+            g_free(nodetypes);
+        }
     }
 
     desktop->clearWaitingCursor();