Code

specialize MaybeStorage for Rect, and start using reference maybes to
[inkscape.git] / src / sp-item.cpp
index ab241a0cde9f32b7c0f4fe997992fadd74918548..0f6ea38397b15621eca6b8e75c6a696e43a3e2fb 100644 (file)
@@ -7,8 +7,9 @@
  * Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   bulia byak <buliabyak@users.sf.net>
+ *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
  *
- * Copyright (C) 2001-2005 authors
+ * Copyright (C) 2001-2006 authors
  * Copyright (C) 2001 Ximian, Inc.
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
@@ -25,7 +26,7 @@
 #endif
 
 
-
+#include "sp-item.h"
 #include "svg/svg.h"
 #include "print.h"
 #include "display/nr-arena.h"
 #include "sp-text.h"
 #include "sp-item-rm-unsatisfied-cns.h"
 #include "sp-pattern.h"
+#include "sp-switch.h"
 #include "gradient-chemistry.h"
 #include "prefs-utils.h"
 #include "conn-avoid-ref.h"
+#include "conditions.h"
 
 #include "libnr/nr-matrix-div.h"
 #include "libnr/nr-matrix-fns.h"
@@ -130,34 +133,37 @@ sp_item_class_init(SPItemClass *klass)
 static void
 sp_item_init(SPItem *item)
 {
-    SPObject *object = SP_OBJECT(item);
+    item->init();
+}
+
+void SPItem::init() {
+    this->sensitive = TRUE;
 
-    item->sensitive = TRUE;
+    this->transform_center_x = 0;
+    this->transform_center_y = 0;
 
-    item->transform_center_x = 0;
-    item->transform_center_y = 0;
+    this->_is_evaluated = true;
+    this->_evaluated_status = StatusUnknown;
 
-    item->transform = NR::identity();
+    this->transform = NR::identity();
 
-    item->display = NULL;
+    this->display = NULL;
 
-    item->clip_ref = new SPClipPathReference(SP_OBJECT(item));
-               {
-                       sigc::signal<void, SPObject *, SPObject *> cs1=item->clip_ref->changedSignal();
-                       sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), item);
-                       cs1.connect(sl1);
-               }
+    this->clip_ref = new SPClipPathReference(this);
+               sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
+               sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
+    _clip_ref_connection = cs1.connect(sl1);
 
-    item->mask_ref = new SPMaskReference(SP_OBJECT(item));
-               sigc::signal<void, SPObject *, SPObject *> cs2=item->mask_ref->changedSignal();
-               sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), item);
-    cs2.connect(sl2);
+    this->mask_ref = new SPMaskReference(this);
+               sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
+               sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
+    _mask_ref_connection = cs2.connect(sl2);
 
-    if (!object->style) object->style = sp_style_new_from_object(SP_OBJECT(item));
+    if (!this->style) this->style = sp_style_new_from_object(this);
 
-    item->avoidRef = new SPAvoidRef(item);
+    this->avoidRef = new SPAvoidRef(this);
 
-    new (&item->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
+    new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
 }
 
 bool SPItem::isVisibleAndUnlocked() const {
@@ -183,6 +189,8 @@ void SPItem::setLocked(bool locked) {
 }
 
 bool SPItem::isHidden() const {
+    if (!isEvaluated())
+        return true;
     return style->display.computed == SP_CSS_DISPLAY_NONE;
 }
 
@@ -195,6 +203,8 @@ void SPItem::setHidden(bool hide) {
 }
 
 bool SPItem::isHidden(unsigned display_key) const {
+    if (!isEvaluated())
+        return true;
     for ( SPItemView *view(display) ; view ; view = view->next ) {
         if ( view->key == display_key ) {
             g_assert(view->arenaitem != NULL);
@@ -211,6 +221,34 @@ bool SPItem::isHidden(unsigned display_key) const {
     return true;
 }
 
+void SPItem::setEvaluated(bool evaluated) {
+    _is_evaluated = evaluated;
+    _evaluated_status = StatusSet;
+}
+
+void SPItem::resetEvaluated() {
+    if ( StatusCalculated == _evaluated_status ) {
+        _evaluated_status = StatusUnknown;
+        bool oldValue = _is_evaluated;
+        if ( oldValue != isEvaluated() ) {
+            requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
+        }
+    } if ( StatusSet == _evaluated_status ) {
+        SPObject const *const parent = SP_OBJECT_PARENT(this);
+        if (SP_IS_SWITCH(parent)) {
+            SP_SWITCH(parent)->resetChildEvaluated();
+        }
+    }
+}
+
+bool SPItem::isEvaluated() const {
+    if ( StatusUnknown == _evaluated_status ) {
+        _is_evaluated = sp_item_evaluate(this);
+        _evaluated_status = StatusCalculated;
+    }
+    return _is_evaluated;
+}
+
 /**
  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
  *  Corresponds to setExplicitlyHidden.
@@ -239,12 +277,12 @@ SPItem::setExplicitlyHidden(bool const val) {
  */
 void
 SPItem::setCenter(NR::Point object_centre) {
-    NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
-    if (!bbox.isEmpty()) {
-        transform_center_x = object_centre[NR::X] - bbox.midpoint()[NR::X];
+    NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
+    if (bbox) {
+        transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
         if (fabs(transform_center_x) < 1e-5) // rounding error
             transform_center_x = 0;
-        transform_center_y = object_centre[NR::Y] - bbox.midpoint()[NR::Y];
+        transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
         if (fabs(transform_center_y) < 1e-5) // rounding error
             transform_center_y = 0;
     }
@@ -261,9 +299,9 @@ bool SPItem::isCenterSet() {
 }
 
 NR::Point SPItem::getCenter() {
-    NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
-    if (!bbox.isEmpty()) {
-        return bbox.midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
+    NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
+    if (bbox) {
+        return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
     } else {
         return NR::Point (0, 0); // something's wrong!
     }
@@ -363,6 +401,9 @@ sp_item_release(SPObject *object)
 {
     SPItem *item = (SPItem *) object;
 
+    item->_clip_ref_connection.disconnect();
+    item->_mask_ref_connection.disconnect();
+
     if (item->clip_ref) {
         item->clip_ref->detach();
         delete item->clip_ref;
@@ -458,6 +499,7 @@ sp_item_set(SPObject *object, unsigned key, gchar const *value)
             } else {
                 item->transform_center_x = 0;
             }
+            object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_TRANSFORM_CENTER_Y:
             if (value) {
@@ -465,7 +507,15 @@ sp_item_set(SPObject *object, unsigned key, gchar const *value)
             } else {
                 item->transform_center_y = 0;
             }
+            object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
             break;
+        case SP_PROP_SYSTEM_LANGUAGE:
+        case SP_PROP_REQUIRED_FEATURES:
+        case SP_PROP_REQUIRED_EXTENSIONS:
+            {
+                item->resetEvaluated();
+                // pass to default handler
+            }
         default:
             if (SP_ATTRIBUTE_IS_CSS(key)) {
                 sp_style_read_from_object(object->style, object);
@@ -531,6 +581,7 @@ mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
             nr_arena_item_set_mask(v->arenaitem, ai);
             nr_arena_item_unref(ai);
             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
+            SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
     }
 }
@@ -585,12 +636,9 @@ sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
 {
     SPItem *item = SP_ITEM(object);
 
-    gchar c[256];
-    if (sp_svg_transform_write(c, 256, item->transform)) {
-        repr->setAttribute("transform", c);
-    } else {
-        repr->setAttribute("transform", NULL);
-    }
+    gchar *c = sp_svg_transform_write(item->transform);
+    repr->setAttribute("transform", c);
+    g_free(c);
 
     SPObject const *const parent = SP_OBJECT_PARENT(object);
     /** \todo Can someone please document why this is conditional on having
@@ -636,12 +684,23 @@ sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
         if (item->transform_center_x != 0)
             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
-        else 
+        else
             repr->setAttribute ("inkscape:transform-center-x", NULL);
         if (item->transform_center_y != 0)
             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
-        else 
-            repr->setAttribute ("inkscape:transform-center-y", NULL);    
+        else
+            repr->setAttribute ("inkscape:transform-center-y", NULL);
+    }
+
+    if (item->clip_ref->getObject()) {
+        const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
+        repr->setAttribute ("clip-path", value);
+        g_free ((void *) value);
+    }
+    if (item->mask_ref->getObject()) {
+        const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
+        repr->setAttribute ("mask", value);
+        g_free ((void *) value);
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
@@ -651,11 +710,13 @@ sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
     return repr;
 }
 
-NR::Rect SPItem::invokeBbox(NR::Matrix const &transform) const
+NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
+                                      SPItem::BBoxType type,
+                                      unsigned int dkey) const
 {
     NRRect r;
-    sp_item_invoke_bbox_full(this, &r, transform, 0, TRUE);
-    return NR::Rect(r);
+    sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
+    return r;
 }
 
 void
@@ -664,6 +725,10 @@ sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transfor
     sp_item_invoke_bbox_full(item, bbox, transform, 0, clear);
 }
 
+/** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
+ * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
+ * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
+ * clones), in turn, call this function in their bbox methods. */
 void
 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
 {
@@ -676,15 +741,25 @@ sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &tra
         bbox->x1 = bbox->y1 = -1e18;
     }
 
+    NRRect this_bbox;
+    this_bbox.x0 = this_bbox.y0 = 1e18;
+    this_bbox.x1 = this_bbox.y1 = -1e18;
+
+    // call the subclass method
     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
-        ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, bbox, transform, flags);
+        ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
     }
 
     // crop the bbox by clip path, if any
     if (item->clip_ref->getObject()) {
         NRRect b;
         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
-        nr_rect_d_intersect (bbox, bbox, &b);
+        nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
+    }
+
+    // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
+    if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
+        nr_rect_d_union (bbox, bbox, &this_bbox);
     }
 }
 
@@ -726,18 +801,26 @@ sp_item_bbox_desktop(SPItem *item, NRRect *bbox)
 NR::Rect sp_item_bbox_desktop(SPItem *item)
 {
     NRRect ret;
-    sp_item_bbox_desktop(item, &ret);
-    return NR::Rect(ret);
+    sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE);
+    NR::Maybe<NR::Rect> result = ret.upgrade();
+    if (result) {
+        return *result;
+    } else {
+        // FIXME
+        return NR::Rect(NR::Point(0, 0), NR::Point(0, 0));
+    }
 }
 
 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
 {
-    NR::Rect const bbox = item->invokeBbox(sp_item_i2d_affine(item));
+    NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
     /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
        support angled guide lines. */
 
-    *p = bbox.min();
-    *p = bbox.max();
+    if (bbox) {
+        *p = bbox->min();
+        *p = bbox->max();
+    }
 }
 
 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
@@ -787,7 +870,18 @@ sp_item_description(SPItem *item)
     g_assert(SP_IS_ITEM(item));
 
     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
-        return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
+        gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
+        if (s && item->clip_ref->getObject()) {
+            gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
+            g_free (s);
+            s = snew;
+        }
+        if (s && item->mask_ref->getObject()) {
+            gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
+            g_free (s);
+            s = snew;
+        }
+        return s;
     }
 
     g_assert_not_reached();
@@ -830,18 +924,42 @@ sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
         nr_arena_item_set_visible(ai, !item->isHidden());
         nr_arena_item_set_sensitive(ai, item->sensitive);
         if (item->clip_ref->getObject()) {
-            NRArenaItem *ac;
-            if (!item->display->arenaitem->key) NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
-            ac = sp_clippath_show(item->clip_ref->getObject(), arena, NR_ARENA_ITEM_GET_KEY(item->display->arenaitem));
+            SPClipPath *cp = item->clip_ref->getObject();
+
+            if (!item->display->arenaitem->key) {
+                NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
+            }
+            int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
+
+            // Show and set clip
+            NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
             nr_arena_item_set_clip(ai, ac);
             nr_arena_item_unref(ac);
+
+            // Update bbox, in case the clip uses bbox units
+            NRRect bbox;
+            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
+            SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
         if (item->mask_ref->getObject()) {
-            NRArenaItem *ac;
-            if (!item->display->arenaitem->key) NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
-            ac = sp_mask_show(item->mask_ref->getObject(), arena, NR_ARENA_ITEM_GET_KEY(item->display->arenaitem));
+            SPMask *mask = item->mask_ref->getObject();
+
+            if (!item->display->arenaitem->key) {
+                NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
+            }
+            int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
+
+            // Show and set mask
+            NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
             nr_arena_item_set_mask(ai, ac);
             nr_arena_item_unref(ac);
+
+            // Update bbox, in case the mask uses bbox units
+            NRRect bbox;
+            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
+            SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
         NR_ARENA_ITEM_SET_DATA(ai, item);
     }
@@ -1072,7 +1190,7 @@ sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const
  * the repr is updated with the new transform.
  */
 void
-sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv)
+sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
 {
     g_return_if_fail(item != NULL);
     g_return_if_fail(SP_IS_ITEM(item));
@@ -1085,36 +1203,46 @@ sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix cons
     } else {
         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
     }
+    
+    if (compensate) {
+        
+         // recursively compensate for stroke scaling, depending on user preference
+        if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
+            double const expansion = 1. / NR::expansion(advertized_transform);
+            sp_item_adjust_stroke_width_recursive(item, expansion);
+        }
+    
+        // recursively compensate rx/ry of a rect if requested
+        if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
+            sp_item_adjust_rects_recursive(item, advertized_transform);
+        }
+    
+        // recursively compensate pattern fill if it's not to be transformed
+        if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
+            sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
+        }
+        /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
+        /// recursively compensate gradient fill if it's not to be transformed
+        if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
+            sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
+        } else {
+            // 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 (item, NR::identity(), NR::identity(), false);
+        }          
+        
+    } // endif(compensate)
 
-     // recursively compensate for stroke scaling, depending on user preference
-    if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
-        double const expansion = 1. / NR::expansion(advertized_transform);
-        sp_item_adjust_stroke_width_recursive(item, expansion);
-    }
-
-    // recursively compensate rx/ry of a rect if requested
-    if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
-        sp_item_adjust_rects_recursive(item, advertized_transform);
-    }
-
-    // recursively compensate pattern fill if it's not to be transformed
-    if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
-        sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
-    }
-    /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
-    /// recursively compensate gradient fill if it's not to be transformed
-    if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
-        sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
-    } else {
-        // 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 (item, NR::identity(), NR::identity(), false);
-    }
-
-    // run the object's set_transform if transforms are stored optimized
     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
     NR::Matrix transform_attr (transform);
-    if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && !preserve) {
+    if ( // run the object's set_transform (i.e. embed transform) only if:
+         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
+             !preserve && // user did not chose to preserve all transforms
+             !item->clip_ref->getObject() && // the object does not have a clippath
+             !item->mask_ref->getObject() && // the object does not have a mask
+             !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.filter) 
+             // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
+        ) {
         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
     }
     sp_item_set_item_transform(item, transform_attr);