Code

Fix ef spam when adjusting pattern on text - patch from Adonis Papaderos
[inkscape.git] / src / sp-item-group.cpp
index d0338f369ecc8bba93bd5d6632f308ddd062b97f..5884277521c9a24fcfac82d519363029c005d18d 100644 (file)
@@ -34,6 +34,7 @@
 #include "sp-item-transform.h"
 #include "sp-root.h"
 #include "sp-use.h"
+#include "sp-offset.h"
 #include "sp-clippath.h"
 #include "sp-mask.h"
 #include "sp-path.h"
@@ -42,6 +43,7 @@
 #include "inkscape.h"
 #include "desktop-handles.h"
 #include "selection.h"
+#include "live_effects/effect.h"
 #include "live_effects/lpeobject.h"
 #include "live_effects/lpeobject-reference.h"
 #include "sp-title.h"
@@ -65,13 +67,12 @@ static void sp_group_set(SPObject *object, unsigned key, char const *value);
 static void sp_group_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
 static gchar * sp_group_description (SPItem * item);
-static Geom::Matrix sp_group_set_transform(SPItem *item, Geom::Matrix const &xform);
 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
 static void sp_group_hide (SPItem * item, unsigned int key);
-static void sp_group_snappoints (SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
+static void sp_group_snappoints (SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
 
 static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
-static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup);
+static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write);
 
 static SPLPEItemClass * parent_class;
 
@@ -127,7 +128,6 @@ sp_group_class_init (SPGroupClass *klass)
     item_class->bbox = sp_group_bbox;
     item_class->print = sp_group_print;
     item_class->description = sp_group_description;
-        item_class->set_transform = sp_group_set_transform;
     item_class->show = sp_group_show;
     item_class->hide = sp_group_hide;
     item_class->snappoints = sp_group_snappoints;
@@ -258,6 +258,8 @@ sp_group_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XM
         const char *value;
         if ( group->_layer_mode == SPGroup::LAYER ) {
             value = "layer";
+        } else if ( group->_layer_mode == SPGroup::MASK_HELPER ) {
+            value = "maskhelper";
         } else if ( flags & SP_OBJECT_WRITE_ALL ) {
             value = "group";
         } else {
@@ -289,24 +291,6 @@ static gchar * sp_group_description (SPItem * item)
     return SP_GROUP(item)->group->getDescription();
 }
 
-static Geom::Matrix
-sp_group_set_transform(SPItem *item, Geom::Matrix const &xform)
-{
-    Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
-    persp3d_split_perspectives_according_to_selection(selection);
-
-    Geom::Matrix last_trans;
-    sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
-    Geom::Matrix inc_trans = last_trans.inverse()*xform;
-
-    std::list<Persp3D *> plist = selection->perspList();
-    for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
-        persp3d_apply_affine_transformation(*i, inc_trans);
-    }
-
-    return xform;
-}
-
 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
     SPGroup *group = SP_GROUP(object);
 
@@ -314,6 +298,8 @@ static void sp_group_set(SPObject *object, unsigned key, char const *value) {
         case SP_ATTR_INKSCAPE_GROUPMODE:
             if ( value && !strcmp(value, "layer") ) {
                 group->setLayerMode(SPGroup::LAYER);
+            } else if ( value && !strcmp(value, "maskhelper") ) {
+                group->setLayerMode(SPGroup::MASK_HELPER);
             } else {
                 group->setLayerMode(SPGroup::GROUP);
             }
@@ -338,7 +324,7 @@ sp_group_hide (SPItem *item, unsigned int key)
     SP_GROUP(item)->group->hide(key);
 }
 
-static void sp_group_snappoints (SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
+static void sp_group_snappoints (SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs)
 {
     for (SPObject const *o = sp_object_first_child(SP_OBJECT(item));
          o != NULL;
@@ -366,18 +352,18 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
 
     g_return_if_fail (!strcmp (grepr->name(), "svg:g") || !strcmp (grepr->name(), "svg:a") || !strcmp (grepr->name(), "svg:switch"));
 
-      // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
-      sp_item_adjust_paint_recursive (gitem, NR::identity(), NR::identity(), false);
+    // this converts the gradient/pattern fill/stroke on the group, if any, to userSpaceOnUse
+    sp_item_adjust_paint_recursive (gitem, Geom::identity(), Geom::identity(), false);
 
     SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
     Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
 
-        if (SP_IS_BOX3D(gitem)) {
-            group = box3d_convert_to_group(SP_BOX3D(gitem));
-            gitem = SP_ITEM(group);
-        }
+       if (SP_IS_BOX3D(gitem)) {
+               group = box3d_convert_to_group(SP_BOX3D(gitem));
+               gitem = SP_ITEM(group);
+       }
 
-        sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
+       sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
 
     /* Step 1 - generate lists of children objects */
     GSList *items = NULL;
@@ -391,7 +377,7 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
             /* Merging of style */
             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
             // it here _before_ the new transform is set, so as to use the pre-transform bbox
-            sp_item_adjust_paint_recursive (citem, NR::identity(), NR::identity(), false);
+            sp_item_adjust_paint_recursive (citem, Geom::identity(), Geom::identity(), false);
 
             sp_style_merge_from_dying_parent(SP_OBJECT_STYLE(child), SP_OBJECT_STYLE(gitem));
             /*
@@ -428,7 +414,24 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
                 // make sure a clone's effective transform is the same as was under group
                 ctrans = g.inverse() * citem->transform * g;
             } else {
-                ctrans = citem->transform * g;
+               // We should not apply the group's transformation to both a linked offset AND to its source
+               if (SP_IS_OFFSET(citem)) { // Do we have an offset at hand (whether it's dynamic or linked)?
+                       SPItem *source = sp_offset_get_source(SP_OFFSET(citem));
+                       // When dealing with a chain of linked offsets, the transformation of an offset will be
+                       // tied to the transformation of the top-most source, not to any of the intermediate
+                       // offsets. So let's find the top-most source
+                       while (source != NULL && SP_IS_OFFSET(source)) {
+                               source = sp_offset_get_source(SP_OFFSET(source));
+                       }
+                       if (source != NULL && // If true then we must be dealing with a linked offset ...
+                                       SP_OBJECT(group)->isAncestorOf(SP_OBJECT(source)) == false) { // ... of which the source is not in the same group
+                               ctrans = citem->transform * g; // then we should apply the transformation of the group to the offset
+                       } else {
+                               ctrans = citem->transform;
+                       }
+                } else {
+                       ctrans = citem->transform * g;
+                }
             }
 
             // FIXME: constructing a transform that would fully preserve the appearance of a
@@ -443,7 +446,7 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
             // (i.e. optimized into the object if the corresponding preference is set)
             gchar *affinestr=sp_svg_transform_write(ctrans);
             nrepr->setAttribute("transform", affinestr);
-                        g_free(affinestr);
+            g_free(affinestr);
 
             items = g_slist_prepend (items, nrepr);
 
@@ -464,11 +467,11 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
     if (objects) {
         Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
         while (objects) {
-                Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
-                if (!sp_repr_is_meta_element(repr))
-                    SP_OBJECT_REPR(defs)->addChild(repr, last_def);
-                Inkscape::GC::release(repr);
-        objects = g_slist_remove (objects, objects->data);
+                       Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
+                       if (!sp_repr_is_meta_element(repr))
+                               SP_OBJECT_REPR(defs)->addChild(repr, last_def);
+                       Inkscape::GC::release(repr);
+                       objects = g_slist_remove (objects, objects->data);
         }
     }
 
@@ -476,7 +479,7 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
     while (items) {
         Inkscape::XML::Node *repr = (Inkscape::XML::Node *) items->data;
         // add item
-                prepr->appendChild(repr);
+        prepr->appendChild(repr);
         // restore position; since the items list was prepended (i.e. reverse), we now add
         // all children at the same pos, which inverts the order once again
         repr->setPosition(pos > 0 ? pos : 0);
@@ -504,15 +507,12 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
 GSList *
 sp_item_group_item_list (SPGroup * group)
 {
-        GSList *s;
-    SPObject *o;
-
     g_return_val_if_fail (group != NULL, NULL);
     g_return_val_if_fail (SP_IS_GROUP (group), NULL);
 
-    s = NULL;
+    GSList *s = NULL;
 
-    for ( o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
+    for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
         if (SP_IS_ITEM (o)) {
             s = g_slist_prepend (s, o);
         }
@@ -536,7 +536,7 @@ void SPGroup::setLayerMode(LayerMode mode) {
     if ( _layer_mode != mode ) {
         if ( mode == LAYER ) {
             sp_document_add_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
-        } else {
+        } else if ( _layer_mode == LAYER ) {
             sp_document_remove_resource(SP_OBJECT_DOCUMENT(this), "layer", this);
         }
         _layer_mode = mode;
@@ -701,7 +701,7 @@ void CGroup::onModified(guint flags) {
 
 void CGroup::calculateBBox(NRRect *bbox, Geom::Matrix const &transform, unsigned const flags) {
 
-    boost::optional<Geom::Rect> dummy_bbox;
+    Geom::OptRect dummy_bbox;
 
     GSList *l = _group->childList(false, SPObject::ActionBBox);
     while (l) {
@@ -836,35 +836,44 @@ sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
         for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
         {
             LivePathEffectObject *lpeobj = (*it)->lpeobject;
-            if (lpeobj->get_lpe()) {
+            if (lpeobj && lpeobj->get_lpe()) {
                 lpeobj->get_lpe()->doBeforeEffect(lpeitem);
             }
         }
 
-        sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem));
+        sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem), write);
     }
 }
 
 static void
-sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup)
+sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup, bool write)
 {
     GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
     for ( GSList const *iter = item_list; iter; iter = iter->next ) {
         SPObject *subitem = static_cast<SPObject *>(iter->data);
         if (SP_IS_GROUP(subitem)) {
-            sp_group_perform_patheffect(SP_GROUP(subitem), topgroup);
+            sp_group_perform_patheffect(SP_GROUP(subitem), topgroup, write);
         } else if (SP_IS_SHAPE(subitem)) {
-            SPCurve * c = sp_shape_get_curve(SP_SHAPE(subitem));
+            SPCurve * c = NULL;
+            if (SP_IS_PATH(subitem)) {
+                c = sp_path_get_original_curve(SP_PATH(subitem));
+            } else {
+                c = sp_shape_get_curve(SP_SHAPE(subitem));
+            }
             // only run LPEs when the shape has a curve defined
             if (c) {
                 sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
                 sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
 
-                Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
-
-                gchar *str = sp_svg_write_path(c->get_pathvector());
-                repr->setAttribute("d", str);
-                g_free(str);
+                if (write) {
+                    Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
+                    gchar *str = sp_svg_write_path(c->get_pathvector());
+                    repr->setAttribute("d", str);
+#ifdef GROUP_VERBOSE
+g_message("sp_group_perform_patheffect writes 'd' attribute");
+#endif
+                    g_free(str);
+                }
 
                 c->unref();
             }