Code

Snap to intersections of line segments
[inkscape.git] / src / splivarot.cpp
index ecafd2f04e9eeee17d22d58608f8943bf2d77e9d..fff70eb25a34b8c8a13a7a7bd8daf69a6220d0d5 100644 (file)
 #include "svg/svg.h"
 #include "sp-path.h"
 #include "sp-shape.h"
+#include "sp-image.h"
 #include "marker.h"
 #include "enums.h"
 #include "sp-text.h"
+#include "sp-flowtext.h"
+#include "text-editing.h"
 #include "sp-item-group.h"
 #include "style.h"
 #include "inkscape.h"
@@ -66,6 +69,12 @@ sp_selected_path_union()
     sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
 }
 
+void
+sp_selected_path_union_skip_undo()
+{
+    sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union"));
+}
+
 void
 sp_selected_path_intersect()
 {
@@ -178,7 +187,7 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     for (GSList *l = il; l != NULL; l = l->next)
     {
         SPItem *item = SP_ITEM(l->data);
-        if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item))
+        if (!SP_IS_SHAPE(item) && !SP_IS_TEXT(item) && !SP_IS_FLOWTEXT(item))
         {
             desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("One of the objects is <b>not a path</b>, cannot perform boolean operation."));
             g_slist_free(il);
@@ -442,10 +451,10 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     // adjust style properties that depend on a possible transform in the source object in order
     // to get a correct style attribute for the new path
     SPItem* item_source = SP_ITEM(source);
-    NR::Matrix i2d = sp_item_i2d_affine(item_source);
-    sp_item_adjust_stroke(item_source, i2d.expansion());
-    sp_item_adjust_pattern(item_source, i2d);
-    sp_item_adjust_gradient(item_source, i2d);
+    NR::Matrix i2root = sp_item_i2root_affine(item_source);
+    sp_item_adjust_stroke(item_source, i2root.expansion());
+    sp_item_adjust_pattern(item_source, i2root);
+    sp_item_adjust_gradient(item_source, i2root);
 
     Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
 
@@ -474,11 +483,7 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     // premultiply by the inverse of parent's repr
     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
     NR::Matrix local = sp_item_i2doc_affine(parent_item);
-    gchar affinestr[80];
-    gchar *transform = NULL;
-    if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
-        transform = affinestr;
-    }
+    gchar *transform = sp_svg_transform_write(local.inverse());
 
     // now that we have the result, add it on the canvas
     if ( bop == bool_op_cut || bop == bool_op_slice ) {
@@ -575,7 +580,11 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         Inkscape::GC::release(repr);
     }
 
-    sp_document_done(sp_desktop_document(desktop), verb, description);
+    g_free(transform);
+
+    if (verb != SP_VERB_NONE) {
+        sp_document_done(sp_desktop_document(desktop), verb, description);
+    }
 
     delete res;
 }
@@ -825,7 +834,7 @@ sp_selected_path_outline()
                             tr = marker_item->transform * marker->c2p * tr * transform;
 
                             if (SP_OBJECT_REPR(marker_item)) {
-                                Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
+                                Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
                                 g_repr->appendChild(m_repr);
                                 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
                                 sp_item_write_transform(marker_item, m_repr, tr);
@@ -1475,6 +1484,8 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
     // remember id
     char const *id = SP_OBJECT_REPR(item)->attribute("id");
+    // remember path effect
+    char const *patheffect = SP_OBJECT_REPR(item)->attribute("inkscape:path-effect");
 
     //If a group was selected, to not change the selection list
     if (modifySelection)
@@ -1508,7 +1519,10 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
 
     // path
     gchar *str = orig->svg_dump_path();
-    repr->setAttribute("d", str);
+    if (patheffect)
+        repr->setAttribute("inkscape:original-d", str);
+    else 
+        repr->setAttribute("d", str);
     g_free(str);
 
     // restore id
@@ -1525,6 +1539,9 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
     // reapply the transform
     sp_item_write_transform(newitem, repr, transform);
 
+    // restore path effect
+    repr->setAttribute("inkscape:path-effect", patheffect);
+
     //If we are not in a selected group
     if (modifySelection)
         selection->add(repr);
@@ -1550,22 +1567,26 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
   
   gchar *simplificationType;
   if (simplifyIndividualPaths) {
-    simplificationType = "individual paths";
+      simplificationType = _("Simplifying paths (separately):");
   } else {
-    simplificationType = "as a group";
+      simplificationType = _("Simplifying paths:");
   }
 
   bool didSomething = false;
 
-  NR::Rect selectionBbox = selection->bounds();
-  gdouble selectionSize  = L2(selectionBbox.dimensions());
+  NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
+  if (!selectionBbox) {
+    return false;
+  }
+  gdouble selectionSize  = L2(selectionBbox->dimensions());
 
   gdouble simplifySize  = selectionSize;
   
   int pathsSimplified = 0;
   int totalPathCount  = g_slist_length(items);
   
-  desktop->disableInteraction();
+  // set "busy" cursor
+  desktop->setWaitingCursor();
   
   for (; items != NULL; items = items->next) {
       SPItem *item = (SPItem *) items->data;
@@ -1574,27 +1595,29 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
           continue;
 
       if (simplifyIndividualPaths) {
-          NR::Rect itemBbox = item->getBounds(sp_item_i2d_affine(item));        
-          simplifySize      = L2(itemBbox.dimensions());
+          NR::Maybe<NR::Rect> itemBbox = item->getBounds(sp_item_i2d_affine(item));        
+          if (itemBbox) {
+              simplifySize      = L2(itemBbox->dimensions());
+          } else {
+              simplifySize      = 0;
+          }
       }
 
-
       pathsSimplified++;
 
       if (pathsSimplified % 20 == 0) {
-        gchar *message = g_strdup_printf(_("Simplifying %s - <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
-        desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, message);
-        desktop->updateCanvasNow();
+        gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
+        desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
       }
 
       didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
                           threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
   }
 
-  desktop->enableInteraction();
-  
+  desktop->clearWaitingCursor();
+
   if (pathsSimplified > 20) {
-    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("Done - <b>%d</b> paths simplified."), pathsSimplified));
+    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
   }
   
   return didSomething;
@@ -1688,26 +1711,31 @@ Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who)
 Path *
 Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
 {
-    SPCurve *curve;
+    SPCurve *curve = NULL;
 
     if (!item)
         return NULL;
 
     if (SP_IS_SHAPE(item))
     {
-        curve = sp_shape_get_curve(SP_SHAPE(item));
+        if (SP_IS_PATH(item)) {
+            curve = sp_path_get_curve_for_edit(SP_PATH(item));
+        } else {
+            curve = sp_shape_get_curve(SP_SHAPE(item));
+        }
     }
-    else if (SP_IS_TEXT(item))
+    else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
     {
-        curve = SP_TEXT(item)->getNormalizedBpath();
+        curve = te_get_layout(item)->convertToCurves();
     }
-    else
+    else if (SP_IS_IMAGE(item))
     {
-        curve = NULL;
+        curve = sp_image_get_curve(SP_IMAGE(item));
     }
 
     if (!curve)
         return NULL;
+
     NArtBpath *bpath = SP_CURVE_BPATH(curve);
     if (bpath == NULL)
         return NULL;
@@ -1723,6 +1751,17 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
         bpath=SP_CURVE_BPATH(curve);
     }
 
+    Path *dest = bpath_to_Path(bpath);
+
+    if ( doTransformation ) {
+        if ( bpath ) g_free(bpath);
+    } else {
+        sp_curve_unref(curve);
+    }
+    return dest;
+}
+
+Path *bpath_to_Path(NArtBpath const *bpath) {
     Path *dest = new Path;
     dest->SetBackData(false);
     {
@@ -1776,12 +1815,6 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
         if (closed)
             dest->Close();
     }
-
-    if ( doTransformation ) {
-        if ( bpath ) g_free(bpath);
-    } else {
-        sp_curve_unref(curve);
-    }
     return dest;
 }