Code

fix bbox calculation for groups that contain groups with nothing in them (zero bbox...
[inkscape.git] / src / sp-item.cpp
1 #define __SP_ITEM_C__
3 /** \file
4  * Base class for visual SVG elements
5  */
6 /*
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
11  *
12  * Copyright (C) 2001-2006 authors
13  * Copyright (C) 2001 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 /** \class SPItem
19  *
20  * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
21  * is a subclass of SPObject, with great deal of specific functionality.
22  */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
29 #include "sp-item.h"
30 #include "svg/svg.h"
31 #include "print.h"
32 #include "display/nr-arena.h"
33 #include "display/nr-arena-item.h"
34 #include "attributes.h"
35 #include "document.h"
36 #include "uri.h"
37 #include "inkscape.h"
38 #include "desktop-handles.h"
40 #include "style.h"
41 #include <glibmm/i18n.h>
42 #include "sp-root.h"
43 #include "sp-clippath.h"
44 #include "sp-mask.h"
45 #include "sp-rect.h"
46 #include "sp-use.h"
47 #include "sp-text.h"
48 #include "sp-item-rm-unsatisfied-cns.h"
49 #include "sp-pattern.h"
50 #include "sp-switch.h"
51 #include "gradient-chemistry.h"
52 #include "prefs-utils.h"
53 #include "conn-avoid-ref.h"
54 #include "conditions.h"
55 #include "sp-filter-reference.h"
56 #include "filter-chemistry.h"
57 #include "sp-guide.h"
59 #include "libnr/nr-matrix-div.h"
60 #include "libnr/nr-matrix-fns.h"
61 #include "libnr/nr-matrix-scale-ops.h"
62 #include "libnr/nr-matrix-translate-ops.h"
63 #include "libnr/nr-scale-translate-ops.h"
64 #include "libnr/nr-translate-scale-ops.h"
65 #include "libnr/nr-convert2geom.h"
66 #include "algorithms/find-last-if.h"
67 #include "util/reverse-list.h"
68 #include <2geom/rect.h>
69 #include <2geom/transforms.h>
71 #include "xml/repr.h"
72 #include "extract-uri.h"
74 #include "live_effects/lpeobject.h"
75 #include "live_effects/effect.h"
76 #include "live_effects/lpeobject-reference.h"
78 #define noSP_ITEM_DEBUG_IDLE
80 static void sp_item_class_init(SPItemClass *klass);
81 static void sp_item_init(SPItem *item);
83 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
84 static void sp_item_release(SPObject *object);
85 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
86 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
87 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
89 static gchar *sp_item_private_description(SPItem *item);
90 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
92 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
93 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
95 static SPObjectClass *parent_class;
97 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
98 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
100 /**
101  * Registers SPItem class and returns its type number.
102  */
103 GType
104 sp_item_get_type(void)
106     static GType type = 0;
107     if (!type) {
108         GTypeInfo info = {
109             sizeof(SPItemClass),
110             NULL, NULL,
111             (GClassInitFunc) sp_item_class_init,
112             NULL, NULL,
113             sizeof(SPItem),
114             16,
115             (GInstanceInitFunc) sp_item_init,
116             NULL,   /* value_table */
117         };
118         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
119     }
120     return type;
123 /**
124  * SPItem vtable initialization.
125  */
126 static void
127 sp_item_class_init(SPItemClass *klass)
129     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
131     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
133     sp_object_class->build = sp_item_build;
134     sp_object_class->release = sp_item_release;
135     sp_object_class->set = sp_item_set;
136     sp_object_class->update = sp_item_update;
137     sp_object_class->write = sp_item_write;
139     klass->description = sp_item_private_description;
140     klass->snappoints = sp_item_private_snappoints;
143 /**
144  * Callback for SPItem object initialization.
145  */
146 static void
147 sp_item_init(SPItem *item)
149     item->init();
152 void SPItem::init() {
153     this->sensitive = TRUE;
155     this->transform_center_x = 0;
156     this->transform_center_y = 0;
158     this->_is_evaluated = true;
159     this->_evaluated_status = StatusUnknown;
161     this->transform = NR::identity();
163     this->display = NULL;
165     this->clip_ref = new SPClipPathReference(this);
166                 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
167                 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
168     _clip_ref_connection = cs1.connect(sl1);
170     this->mask_ref = new SPMaskReference(this);
171                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
172                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
173     _mask_ref_connection = cs2.connect(sl2);
175     this->avoidRef = new SPAvoidRef(this);
177     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
180 bool SPItem::isVisibleAndUnlocked() const {
181     return (!isHidden() && !isLocked());
184 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
185     return (!isHidden(display_key) && !isLocked());
188 bool SPItem::isLocked() const {
189     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
190         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
191             return true;
192     }
193     return false;
196 void SPItem::setLocked(bool locked) {
197     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
198                      ( locked ? "1" : NULL ));
199     updateRepr();
202 bool SPItem::isHidden() const {
203     if (!isEvaluated())
204         return true;
205     return style->display.computed == SP_CSS_DISPLAY_NONE;
208 void SPItem::setHidden(bool hide) {
209     style->display.set = TRUE;
210     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
211     style->display.computed = style->display.value;
212     style->display.inherit = FALSE;
213     updateRepr();
216 bool SPItem::isHidden(unsigned display_key) const {
217     if (!isEvaluated())
218         return true;
219     for ( SPItemView *view(display) ; view ; view = view->next ) {
220         if ( view->key == display_key ) {
221             g_assert(view->arenaitem != NULL);
222             for ( NRArenaItem *arenaitem = view->arenaitem ;
223                   arenaitem ; arenaitem = arenaitem->parent )
224             {
225                 if (!arenaitem->visible) {
226                     return true;
227                 }
228             }
229             return false;
230         }
231     }
232     return true;
235 void SPItem::setEvaluated(bool evaluated) {
236     _is_evaluated = evaluated;
237     _evaluated_status = StatusSet;
240 void SPItem::resetEvaluated() {
241     if ( StatusCalculated == _evaluated_status ) {
242         _evaluated_status = StatusUnknown;
243         bool oldValue = _is_evaluated;
244         if ( oldValue != isEvaluated() ) {
245             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
246         }
247     } if ( StatusSet == _evaluated_status ) {
248         SPObject const *const parent = SP_OBJECT_PARENT(this);
249         if (SP_IS_SWITCH(parent)) {
250             SP_SWITCH(parent)->resetChildEvaluated();
251         }
252     }
255 bool SPItem::isEvaluated() const {
256     if ( StatusUnknown == _evaluated_status ) {
257         _is_evaluated = sp_item_evaluate(this);
258         _evaluated_status = StatusCalculated;
259     }
260     return _is_evaluated;
263 /**
264  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
265  *  Corresponds to setExplicitlyHidden.
266  */
267 bool
268 SPItem::isExplicitlyHidden() const
270     return (this->style->display.set
271             && this->style->display.value == SP_CSS_DISPLAY_NONE);
274 /**
275  * Sets the display CSS property to `hidden' if \a val is true,
276  * otherwise makes it unset
277  */
278 void
279 SPItem::setExplicitlyHidden(bool const val) {
280     this->style->display.set = val;
281     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
282     this->style->display.computed = this->style->display.value;
283     this->updateRepr();
286 /**
287  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
288  */
289 void
290 SPItem::setCenter(NR::Point object_centre) {
291     NR::Maybe<NR::Rect> bbox = getBounds(from_2geom(sp_item_i2d_affine(this)));
292     if (bbox) {
293         transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
294         if (fabs(transform_center_x) < 1e-5) // rounding error
295             transform_center_x = 0;
296         transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
297         if (fabs(transform_center_y) < 1e-5) // rounding error
298             transform_center_y = 0;
299     }
302 void
303 SPItem::unsetCenter() {
304     transform_center_x = 0;
305     transform_center_y = 0;
308 bool SPItem::isCenterSet() {
309     return (transform_center_x != 0 || transform_center_y != 0);
312 NR::Point SPItem::getCenter() const {
313     NR::Maybe<NR::Rect> bbox = getBounds(from_2geom(sp_item_i2d_affine(this)));
314     if (bbox) {
315         return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
316     } else {
317         return NR::Point (0, 0); // something's wrong!
318     }
322 namespace {
324 bool is_item(SPObject const &object) {
325     return SP_IS_ITEM(&object);
330 void SPItem::raiseToTop() {
331     using Inkscape::Algorithms::find_last_if;
333     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
334         SP_OBJECT_NEXT(this), NULL, &is_item
335     );
336     if (topmost) {
337         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
338         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
339     }
342 void SPItem::raiseOne() {
343     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
344         SP_OBJECT_NEXT(this), NULL, &is_item
345     );
346     if (next_higher) {
347         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
348         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
349         sp_repr_parent(repr)->changeOrder(repr, ref);
350     }
353 void SPItem::lowerOne() {
354     using Inkscape::Util::MutableList;
355     using Inkscape::Util::reverse_list;
357     MutableList<SPObject &> next_lower=std::find_if(
358         reverse_list<SPObject::SiblingIterator>(
359             SP_OBJECT_PARENT(this)->firstChild(), this
360         ),
361         MutableList<SPObject &>(),
362         &is_item
363     );
364     if (next_lower) {
365         ++next_lower;
366         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
367         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
368         sp_repr_parent(repr)->changeOrder(repr, ref);
369     }
372 void SPItem::lowerToBottom() {
373     using Inkscape::Algorithms::find_last_if;
374     using Inkscape::Util::MutableList;
375     using Inkscape::Util::reverse_list;
377     MutableList<SPObject &> bottom=find_last_if(
378         reverse_list<SPObject::SiblingIterator>(
379             SP_OBJECT_PARENT(this)->firstChild(), this
380         ),
381         MutableList<SPObject &>(),
382         &is_item
383     );
384     if (bottom) {
385         ++bottom;
386         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
387         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
388         sp_repr_parent(repr)->changeOrder(repr, ref);
389     }
392 static void
393 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
395     sp_object_read_attr(object, "style");
396     sp_object_read_attr(object, "transform");
397     sp_object_read_attr(object, "clip-path");
398     sp_object_read_attr(object, "mask");
399     sp_object_read_attr(object, "sodipodi:insensitive");
400     sp_object_read_attr(object, "sodipodi:nonprintable");
401     sp_object_read_attr(object, "inkscape:transform-center-x");
402     sp_object_read_attr(object, "inkscape:transform-center-y");
403     sp_object_read_attr(object, "inkscape:connector-avoid");
405     if (((SPObjectClass *) (parent_class))->build) {
406         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
407     }
410 static void
411 sp_item_release(SPObject *object)
413     SPItem *item = (SPItem *) object;
415     item->_clip_ref_connection.disconnect();
416     item->_mask_ref_connection.disconnect();
418     // Note: do this here before the clip_ref is deleted, since calling
419     // sp_document_ensure_up_to_date for triggered routing may reference
420     // the deleted clip_ref.
421     if (item->avoidRef) {
422         delete item->avoidRef;
423         item->avoidRef = NULL;
424     }
426     if (item->clip_ref) {
427         item->clip_ref->detach();
428         delete item->clip_ref;
429         item->clip_ref = NULL;
430     }
432     if (item->mask_ref) {
433         item->mask_ref->detach();
434         delete item->mask_ref;
435         item->mask_ref = NULL;
436     }
438     if (((SPObjectClass *) (parent_class))->release) {
439         ((SPObjectClass *) parent_class)->release(object);
440     }
442     while (item->display) {
443         nr_arena_item_unparent(item->display->arenaitem);
444         item->display = sp_item_view_list_remove(item->display, item->display);
445     }
447     item->_transformed_signal.~signal();
450 static void
451 sp_item_set(SPObject *object, unsigned key, gchar const *value)
453     SPItem *item = (SPItem *) object;
455     switch (key) {
456         case SP_ATTR_TRANSFORM: {
457             NR::Matrix t;
458             if (value && sp_svg_transform_read(value, &t)) {
459                 sp_item_set_item_transform(item, t);
460             } else {
461                 sp_item_set_item_transform(item, NR::identity());
462             }
463             break;
464         }
465         case SP_PROP_CLIP_PATH: {
466             gchar *uri = extract_uri(value);
467             if (uri) {
468                 try {
469                     item->clip_ref->attach(Inkscape::URI(uri));
470                 } catch (Inkscape::BadURIException &e) {
471                     g_warning("%s", e.what());
472                     item->clip_ref->detach();
473                 }
474                 g_free(uri);
475             } else {
476                 item->clip_ref->detach();
477             }
479             break;
480         }
481         case SP_PROP_MASK: {
482             gchar *uri = extract_uri(value);
483             if (uri) {
484                 try {
485                     item->mask_ref->attach(Inkscape::URI(uri));
486                 } catch (Inkscape::BadURIException &e) {
487                     g_warning("%s", e.what());
488                     item->mask_ref->detach();
489                 }
490                 g_free(uri);
491             } else {
492                 item->mask_ref->detach();
493             }
495             break;
496         }
497         case SP_ATTR_SODIPODI_INSENSITIVE:
498             item->sensitive = !value;
499             for (SPItemView *v = item->display; v != NULL; v = v->next) {
500                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
501             }
502             break;
503         case SP_ATTR_CONNECTOR_AVOID:
504             item->avoidRef->setAvoid(value);
505             break;
506         case SP_ATTR_TRANSFORM_CENTER_X:
507             if (value) {
508                 item->transform_center_x = g_strtod(value, NULL);
509             } else {
510                 item->transform_center_x = 0;
511             }
512             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
513             break;
514         case SP_ATTR_TRANSFORM_CENTER_Y:
515             if (value) {
516                 item->transform_center_y = g_strtod(value, NULL);
517             } else {
518                 item->transform_center_y = 0;
519             }
520             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
521             break;
522         case SP_PROP_SYSTEM_LANGUAGE:
523         case SP_PROP_REQUIRED_FEATURES:
524         case SP_PROP_REQUIRED_EXTENSIONS:
525             {
526                 item->resetEvaluated();
527                 // pass to default handler
528             }
529         default:
530             if (SP_ATTRIBUTE_IS_CSS(key)) {
531                 sp_style_read_from_object(object->style, object);
532                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
533             } else {
534                 if (((SPObjectClass *) (parent_class))->set) {
535                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
536                 }
537             }
538             break;
539     }
542 static void
543 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
545     if (old_clip) {
546         SPItemView *v;
547         /* Hide clippath */
548         for (v = item->display; v != NULL; v = v->next) {
549             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
550             nr_arena_item_set_clip(v->arenaitem, NULL);
551         }
552     }
553     if (SP_IS_CLIPPATH(clip)) {
554         NRRect bbox;
555         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
556         for (SPItemView *v = item->display; v != NULL; v = v->next) {
557             if (!v->arenaitem->key) {
558                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
559             }
560             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
561                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
562                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
563             nr_arena_item_set_clip(v->arenaitem, ai);
564             nr_arena_item_unref(ai);
565             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
566             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
567         }
568     }
571 static void
572 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
574     if (old_mask) {
575         /* Hide mask */
576         for (SPItemView *v = item->display; v != NULL; v = v->next) {
577             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
578             nr_arena_item_set_mask(v->arenaitem, NULL);
579         }
580     }
581     if (SP_IS_MASK(mask)) {
582         NRRect bbox;
583         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
584         for (SPItemView *v = item->display; v != NULL; v = v->next) {
585             if (!v->arenaitem->key) {
586                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
587             }
588             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
589                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
590                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
591             nr_arena_item_set_mask(v->arenaitem, ai);
592             nr_arena_item_unref(ai);
593             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
594             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
595         }
596     }
599 static void
600 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
602     SPItem *item = SP_ITEM(object);
604     if (((SPObjectClass *) (parent_class))->update)
605         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
607     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
608         if (flags & SP_OBJECT_MODIFIED_FLAG) {
609             for (SPItemView *v = item->display; v != NULL; v = v->next) {
610                 nr_arena_item_set_transform(v->arenaitem, item->transform);
611             }
612         }
614         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
615         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
617         if ( clip_path || mask ) {
618             NRRect bbox;
619             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
620             if (clip_path) {
621                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
622                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
623                 }
624             }
625             if (mask) {
626                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
627                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
628                 }
629             }
630         }
632         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
633             for (SPItemView *v = item->display; v != NULL; v = v->next) {
634                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
635                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
636             }
637         }
638     }
640     /* Update bounding box data used by filters */
641     if (item->style->filter.set && item->display) {
642         NRRect item_bbox;
643         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
644         NR::Maybe<NR::Rect> i_bbox = item_bbox;
646         SPItemView *itemview = item->display;
647         do {
648             if (itemview->arenaitem)
649                 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
650         } while ( (itemview = itemview->next) );
651     }
653     // Update libavoid with item geometry (for connector routing).
654     if (item->avoidRef)
655         item->avoidRef->handleSettingChange();
658 static Inkscape::XML::Node *
659 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
661     SPItem *item = SP_ITEM(object);
663     gchar *c = sp_svg_transform_write(item->transform);
664     repr->setAttribute("transform", c);
665     g_free(c);
667     if (flags & SP_OBJECT_WRITE_EXT) {
668         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
669         if (item->transform_center_x != 0)
670             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
671         else
672             repr->setAttribute ("inkscape:transform-center-x", NULL);
673         if (item->transform_center_y != 0)
674             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
675         else
676             repr->setAttribute ("inkscape:transform-center-y", NULL);
677     }
679     if (item->clip_ref->getObject()) {
680         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
681         repr->setAttribute ("clip-path", value);
682         g_free ((void *) value);
683     }
684     if (item->mask_ref->getObject()) {
685         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
686         repr->setAttribute ("mask", value);
687         g_free ((void *) value);
688     }
690     if (((SPObjectClass *) (parent_class))->write) {
691         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
692     }
694     return repr;
697 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
698                                       SPItem::BBoxType type,
699                                       unsigned int /*dkey*/) const
701     NR::Maybe<NR::Rect> r = NR::Nothing();
702     sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
703     return r;
706 NR::Maybe<Geom::Rect>
707 SPItem::getBounds(Geom::Matrix const &transform, SPItem::BBoxType type, unsigned int /*dkey*/)
708 const
710     NR::Maybe<NR::Rect> r = NR::Nothing();
711     sp_item_invoke_bbox_full(this, &r, from_2geom(transform), type, TRUE);
712     return NR::Maybe<Geom::Rect>(to_2geom(*r));
715 void
716 sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
718     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
721 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
722 void
723 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
725     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
728 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
729  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
730  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
731  * clones), in turn, call this function in their bbox methods. */
732 void
733 sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
735     g_assert(item != NULL);
736     g_assert(SP_IS_ITEM(item));
737     g_assert(bbox != NULL);
739     if (clear) {
740         *bbox = NR::Nothing();
741     }
743     // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
745     NRRect temp_bbox;
746     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
747     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
749     // call the subclass method
750     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
751         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
752     }
754     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
755     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
756         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
757             SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
758             if (filter && SP_IS_FILTER(filter)) {
759                 // default filer area per the SVG spec:
760                 double x = -0.1;
761                 double y = -0.1;
762                 double w = 1.2;
763                 double h = 1.2;
765                 // if area is explicitly set, override:
766                 if (SP_FILTER(filter)->x._set)
767                     x = SP_FILTER(filter)->x.computed;
768                 if (SP_FILTER(filter)->y._set)
769                     y = SP_FILTER(filter)->y.computed;
770                 if (SP_FILTER(filter)->width._set)
771                     w = SP_FILTER(filter)->width.computed;
772                 if (SP_FILTER(filter)->height._set) 
773                     h = SP_FILTER(filter)->height.computed;
775                 double dx0 = 0;
776                 double dx1 = 0;
777                 double dy0 = 0;
778                 double dy1 = 0;
779                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
780                     // if this is a single blur, use 2.4*radius 
781                     // which may be smaller than the default area;
782                     // see set_filter_area for why it's 2.4
783                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
784                     dx0 = -2.4 * r;
785                     dx1 = 2.4 * r;
786                     dy0 = -2.4 * r;
787                     dy1 = 2.4 * r;
788                 } else {
789                     // otherwise, calculate expansion from relative to absolute units:
790                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
791                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
792                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
793                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
794                 }
796                 // transform the expansions by the item's transform:
797                 NR::Matrix i2d = from_2geom(sp_item_i2d_affine (item));
798                 dx0 *= NR::expansionX(i2d);
799                 dx1 *= NR::expansionX(i2d);
800                 dy0 *= NR::expansionY(i2d);
801                 dy1 *= NR::expansionY(i2d);
803                 // expand the bbox
804                 temp_bbox.x0 += dx0;
805                 temp_bbox.x1 += dx1;
806                 temp_bbox.y0 += dy0;
807                 temp_bbox.y1 += dy1;
808             }
809         }
810         if (item->clip_ref->getObject()) {
811             NRRect b;
812             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
813             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
814         }
815     }
817     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
818         // Either the bbox hasn't been touched by the SPItemClass' bbox method 
819         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
820         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
821         
822         // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been 
823         // explicitely defined this way for NRRects (as opposed to NR::Maybe<NR::Rect>)
824         // So union bbox with nothing = do nothing, just return
825         return;
826     }
828     // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
829     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
830     // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
831     NR::Maybe<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
833     *bbox = NR::union_bounds(*bbox, temp_bbox_new);
836 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
837 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
838  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
839  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
840  * clones), in turn, call this function in their bbox methods. */
841 void
842 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
844     g_assert(item != NULL);
845     g_assert(SP_IS_ITEM(item));
846     g_assert(bbox != NULL);
848     if (clear) {
849         bbox->x0 = bbox->y0 = 1e18;
850         bbox->x1 = bbox->y1 = -1e18;
851     }
853     NRRect this_bbox;
854     this_bbox.x0 = this_bbox.y0 = 1e18;
855     this_bbox.x1 = this_bbox.y1 = -1e18;
857     // call the subclass method
858     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
859         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
860     }
862     // unless this is geometric bbox, crop the bbox by clip path, if any
863     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
864         NRRect b;
865         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
866         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
867     }
869     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
870     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
871         nr_rect_d_union (bbox, bbox, &this_bbox);
872     }
875 unsigned sp_item_pos_in_parent(SPItem *item)
877     g_assert(item != NULL);
878     g_assert(SP_IS_ITEM(item));
880     SPObject *parent = SP_OBJECT_PARENT(item);
881     g_assert(parent != NULL);
882     g_assert(SP_IS_OBJECT(parent));
884     SPObject *object = SP_OBJECT(item);
886     unsigned pos=0;
887     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
888         if ( iter == object ) {
889             return pos;
890         }
891         if (SP_IS_ITEM(iter)) {
892             pos++;
893         }
894     }
896     g_assert_not_reached();
897     return 0;
900 void
901 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
903     g_assert(item != NULL);
904     g_assert(SP_IS_ITEM(item));
905     g_assert(bbox != NULL);
907     sp_item_invoke_bbox(item, bbox, from_2geom(sp_item_i2d_affine(item)), TRUE, type);
910 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
912     NR::Maybe<NR::Rect> rect = NR::Nothing();
913     sp_item_invoke_bbox(item, &rect, from_2geom(sp_item_i2d_affine(item)), TRUE, type);
914     return rect;
917 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
919     NR::Maybe<NR::Rect> bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
920     /* Just the corners of the bounding box suffices given that we don't yet
921        support angled guide lines. */
923     if (bbox) {
924         NR::Point p1, p2;
925         p1 = bbox->min();
926         p2 = bbox->max();
927         *p = p1;
928         *p = NR::Point(p1[NR::X], p2[NR::Y]);
929         *p = p2;
930         *p = NR::Point(p1[NR::Y], p2[NR::X]);
931     }
934 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
936     g_assert (item != NULL);
937     g_assert (SP_IS_ITEM(item));
939     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
940     if (item_class.snappoints) {
941         item_class.snappoints(item, p);
942     }
944     if (includeItemCenter) {
945         *p = item->getCenter();
946     }
949 void
950 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
952     if (!item->isHidden()) {
953         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
954             if (!item->transform.test_identity()
955                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
956             {
957                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
958                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
959                 sp_print_release(ctx);
960             } else {
961                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
962             }
963         }
964     }
967 static gchar *
968 sp_item_private_description(SPItem */*item*/)
970     return g_strdup(_("Object"));
973 /**
974  * Returns a string suitable for status bar, formatted in pango markup language.
975  *
976  * Must be freed by caller.
977  */
978 gchar *
979 sp_item_description(SPItem *item)
981     g_assert(item != NULL);
982     g_assert(SP_IS_ITEM(item));
984     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
985         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
986         if (s && item->clip_ref->getObject()) {
987             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
988             g_free (s);
989             s = snew;
990         }
991         if (s && item->mask_ref->getObject()) {
992             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
993             g_free (s);
994             s = snew;
995         }
996         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
997             gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
998             g_free (s);
999             s = snew;
1000         }
1001         return s;
1002     }
1004     g_assert_not_reached();
1005     return NULL;
1008 /**
1009  * Allocates unique integer keys.
1010  * \param numkeys Number of keys required.
1011  * \return First allocated key; hence if the returned key is n
1012  * you can use n, n + 1, ..., n + (numkeys - 1)
1013  */
1014 unsigned
1015 sp_item_display_key_new(unsigned numkeys)
1017     static unsigned dkey = 0;
1019     dkey += numkeys;
1021     return dkey - numkeys;
1024 NRArenaItem *
1025 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1027     g_assert(item != NULL);
1028     g_assert(SP_IS_ITEM(item));
1029     g_assert(arena != NULL);
1030     g_assert(NR_IS_ARENA(arena));
1032     NRArenaItem *ai = NULL;
1033     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1034         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1035     }
1037     if (ai != NULL) {
1038         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1039         nr_arena_item_set_transform(ai, item->transform);
1040         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1041         nr_arena_item_set_visible(ai, !item->isHidden());
1042         nr_arena_item_set_sensitive(ai, item->sensitive);
1043         if (item->clip_ref->getObject()) {
1044             SPClipPath *cp = item->clip_ref->getObject();
1046             if (!item->display->arenaitem->key) {
1047                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1048             }
1049             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1051             // Show and set clip
1052             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1053             nr_arena_item_set_clip(ai, ac);
1054             nr_arena_item_unref(ac);
1056             // Update bbox, in case the clip uses bbox units
1057             NRRect bbox;
1058             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1059             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1060             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1061         }
1062         if (item->mask_ref->getObject()) {
1063             SPMask *mask = item->mask_ref->getObject();
1065             if (!item->display->arenaitem->key) {
1066                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1067             }
1068             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1070             // Show and set mask
1071             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1072             nr_arena_item_set_mask(ai, ac);
1073             nr_arena_item_unref(ac);
1075             // Update bbox, in case the mask uses bbox units
1076             NRRect bbox;
1077             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1078             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1079             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1080         }
1081         NR_ARENA_ITEM_SET_DATA(ai, item);
1082         NRRect item_bbox;
1083         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1084         NR::Maybe<NR::Rect> i_bbox = item_bbox;
1085         nr_arena_item_set_item_bbox(ai, i_bbox);
1086     }
1088     return ai;
1091 void
1092 sp_item_invoke_hide(SPItem *item, unsigned key)
1094     g_assert(item != NULL);
1095     g_assert(SP_IS_ITEM(item));
1097     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1098         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1099     }
1101     SPItemView *ref = NULL;
1102     SPItemView *v = item->display;
1103     while (v != NULL) {
1104         SPItemView *next = v->next;
1105         if (v->key == key) {
1106             if (item->clip_ref->getObject()) {
1107                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1108                 nr_arena_item_set_clip(v->arenaitem, NULL);
1109             }
1110             if (item->mask_ref->getObject()) {
1111                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1112                 nr_arena_item_set_mask(v->arenaitem, NULL);
1113             }
1114             if (!ref) {
1115                 item->display = v->next;
1116             } else {
1117                 ref->next = v->next;
1118             }
1119             nr_arena_item_unparent(v->arenaitem);
1120             nr_arena_item_unref(v->arenaitem);
1121             g_free(v);
1122         } else {
1123             ref = v;
1124         }
1125         v = next;
1126     }
1129 // Adjusters
1131 void
1132 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1134     SPStyle *style = SP_OBJECT_STYLE (item);
1136     if (style && (style->fill.isPaintserver())) {
1137         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1138         if (SP_IS_PATTERN (server)) {
1139             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1140             sp_pattern_transform_multiply (pattern, postmul, set);
1141         }
1142     }
1144     if (style && (style->stroke.isPaintserver())) {
1145         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1146         if (SP_IS_PATTERN (server)) {
1147             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1148             sp_pattern_transform_multiply (pattern, postmul, set);
1149         }
1150     }
1154 void
1155 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1157     SPStyle *style = SP_OBJECT_STYLE (item);
1159     if (style && (style->fill.isPaintserver())) {
1160         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1161         if (SP_IS_GRADIENT (server)) {
1163             /**
1164              * \note Bbox units for a gradient are generally a bad idea because
1165              * with them, you cannot preserve the relative position of the
1166              * object and its gradient after rotation or skew. So now we
1167              * convert them to userspace units which are easy to keep in sync
1168              * just by adding the object's transform to gradientTransform.
1169              * \todo FIXME: convert back to bbox units after transforming with
1170              * the item, so as to preserve the original units.
1171              */
1172             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1174             sp_gradient_transform_multiply (gradient, postmul, set);
1175         }
1176     }
1178     if (style && (style->stroke.isPaintserver())) {
1179         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1180         if (SP_IS_GRADIENT (server)) {
1181             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1182             sp_gradient_transform_multiply (gradient, postmul, set);
1183         }
1184     }
1187 void
1188 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1190     SPStyle *style = SP_OBJECT_STYLE (item);
1192     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1194         style->stroke_width.computed *= ex;
1195         style->stroke_width.set = TRUE;
1197         if (style->stroke_dash.n_dash != 0) {
1198             int i;
1199             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1200                 style->stroke_dash.dash[i] *= ex;
1201             }
1202             style->stroke_dash.offset *= ex;
1203         }
1205         SP_OBJECT(item)->updateRepr();
1206     }
1209 /**
1210  * Find out the inverse of previous transform of an item (from its repr)
1211  */
1212 NR::Matrix
1213 sp_item_transform_repr (SPItem *item)
1215     NR::Matrix t_old(NR::identity());
1216     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1217     if (t_attr) {
1218         NR::Matrix t;
1219         if (sp_svg_transform_read(t_attr, &t)) {
1220             t_old = t;
1221         }
1222     }
1224     return t_old;
1228 /**
1229  * Recursively scale stroke width in \a item and its children by \a expansion.
1230  */
1231 void
1232 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1234     sp_item_adjust_stroke (item, expansion);
1236 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1237     if (item && SP_IS_USE(item))
1238         return;
1240     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1241         if (SP_IS_ITEM(o))
1242             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1243     }
1246 /**
1247  * Recursively adjust rx and ry of rects.
1248  */
1249 void
1250 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1252     if (SP_IS_RECT (item)) {
1253         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1254     }
1256     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1257         if (SP_IS_ITEM(o))
1258             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1259     }
1262 /**
1263  * Recursively compensate pattern or gradient transform.
1264  */
1265 void
1266 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1268 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1269 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1270 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1271     NR::Matrix t_item = sp_item_transform_repr (item);
1272     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1274 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1275 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1276 // we must not touch it
1277     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1278         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1279             if (SP_IS_ITEM(o)) {
1280 // At the level of the transformed item, t_ancestors is identity;
1281 // below it, it is the accmmulated chain of transforms from this level to the top level
1282                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1283             }
1284         }
1285     }
1287 // We recursed into children first, and are now adjusting this object second;
1288 // this is so that adjustments in a tree are done from leaves up to the root,
1289 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1290 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1292     if (is_pattern)
1293         sp_item_adjust_pattern (item, paint_delta);
1294     else
1295         sp_item_adjust_gradient (item, paint_delta);
1299 void
1300 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1302     if ( !SP_IS_LPE_ITEM(item) )
1303         return;
1305     SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1306     if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1307         PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
1308         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1309         {
1310             // If the path effect is used by 2 or more items, fork it
1311             // so that each object has its own independent copy of the effect
1312             LivePathEffectObject *lpeobj = (*it)->lpeobject;
1313             if (lpeobj) {
1314                 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1315                 if (new_lpeobj != lpeobj) {
1316                     sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1317                 }
1318         
1319                 if (lpeobj->lpe) {
1320                     Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
1321                     effect->transform_multiply(to_2geom(postmul), set);
1322                 }
1323             }
1324         }
1325     }
1328 /**
1329  * A temporary wrapper for the next function accepting the NR::Matrix
1330  * instead of NR::Matrix
1331  */
1332 void
1333 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1335     if (transform == NULL)
1336         sp_item_write_transform(item, repr, NR::identity(), adv);
1337     else
1338         sp_item_write_transform(item, repr, *transform, adv);
1341 /**
1342  * Set a new transform on an object.
1343  *
1344  * Compensate for stroke scaling and gradient/pattern fill transform, if
1345  * necessary. Call the object's set_transform method if transforms are
1346  * stored optimized. Send _transformed_signal. Invoke _write method so that
1347  * the repr is updated with the new transform.
1348  */
1349 void
1350 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1352     g_return_if_fail(item != NULL);
1353     g_return_if_fail(SP_IS_ITEM(item));
1354     g_return_if_fail(repr != NULL);
1356     // calculate the relative transform, if not given by the adv attribute
1357     NR::Matrix advertized_transform;
1358     if (adv != NULL) {
1359         advertized_transform = *adv;
1360     } else {
1361         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1362     }
1364     if (compensate) {
1366          // recursively compensate for stroke scaling, depending on user preference
1367         if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1368             double const expansion = 1. / NR::expansion(advertized_transform);
1369             sp_item_adjust_stroke_width_recursive(item, expansion);
1370         }
1372         // recursively compensate rx/ry of a rect if requested
1373         if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1374             sp_item_adjust_rects_recursive(item, advertized_transform);
1375         }
1377         // recursively compensate pattern fill if it's not to be transformed
1378         if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1379             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1380         }
1381         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1382         /// recursively compensate gradient fill if it's not to be transformed
1383         if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1384             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1385         } else {
1386             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1387             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1388             sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1389         }
1391     } // endif(compensate)
1393     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1394     NR::Matrix transform_attr (transform);
1395     if ( // run the object's set_transform (i.e. embed transform) only if:
1396          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1397              !preserve && // user did not chose to preserve all transforms
1398              !item->clip_ref->getObject() && // the object does not have a clippath
1399              !item->mask_ref->getObject() && // the object does not have a mask
1400          !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1401              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1402         ) {
1403         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1404     }
1405     sp_item_set_item_transform(item, transform_attr);
1407     // Note: updateRepr comes before emitting the transformed signal since
1408     // it causes clone SPUse's copy of the original object to brought up to
1409     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1410     // values if called in code handling the transformed signal.
1411     SP_OBJECT(item)->updateRepr();
1413     // send the relative transform with a _transformed_signal
1414     item->_transformed_signal.emit(&advertized_transform, item);
1417 gint
1418 sp_item_event(SPItem *item, SPEvent *event)
1420     g_return_val_if_fail(item != NULL, FALSE);
1421     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1422     g_return_val_if_fail(event != NULL, FALSE);
1424     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1425         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1427     return FALSE;
1430 /**
1431  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1432  * gradients, patterns as sp_item_write_transform does.
1433  */
1434 void
1435 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1437     g_return_if_fail(item != NULL);
1438     g_return_if_fail(SP_IS_ITEM(item));
1440     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1441         item->transform = transform;
1442         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1443            transformation.  It's apparently not used anywhere else. */
1444         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1445         sp_item_rm_unsatisfied_cns(*item);
1446     }
1449 void
1450 sp_item_convert_item_to_guides(SPItem *item) {
1451     g_return_if_fail(item != NULL);
1452     g_return_if_fail(SP_IS_ITEM(item));
1454     /* Use derived method if present ... */
1455     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1456         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1457         return;
1458     }
1460     /* .. otherwise simply place the guides around the item's bounding box */
1462     sp_item_convert_to_guides(item);
1466 /**
1467  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1468  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1469  */
1470 Geom::Matrix
1471 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1472     Geom::Matrix ret(Geom::identity());
1473     g_return_val_if_fail(object != NULL, ret);
1475     /* stop at first non-renderable ancestor */
1476     while ( object != ancestor && SP_IS_ITEM(object) ) {
1477         if (SP_IS_ROOT(object)) {
1478             ret *= to_2geom(SP_ROOT(object)->c2p);
1479         }
1480         ret *= to_2geom(SP_ITEM(object)->transform);
1481         object = SP_OBJECT_PARENT(object);
1482     }
1483     return ret;
1486 Geom::Matrix
1487 i2i_affine(SPObject const *src, SPObject const *dest) {
1488     g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1489     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1490     return to_2geom( from_2geom(i2anc_affine(src, ancestor)) / from_2geom(i2anc_affine(dest, ancestor)) );
1493 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1494     return from_2geom(i2i_affine(this, dest));
1497 /**
1498  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1499  * \pre (item != NULL) and SP_IS_ITEM(item).
1500  */
1501 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1503     return i2anc_affine(item, NULL);
1506 /**
1507  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1508  * Used in path operations mostly.
1509  * \pre (item != NULL) and SP_IS_ITEM(item).
1510  */
1511 Geom::Matrix sp_item_i2root_affine(SPItem const *item)
1513     g_assert(item != NULL);
1514     g_assert(SP_IS_ITEM(item));
1516     Geom::Matrix ret(Geom::identity());
1517     g_assert(ret.isIdentity());
1518     while ( NULL != SP_OBJECT_PARENT(item) ) {
1519         ret *= to_2geom(item->transform);
1520         item = SP_ITEM(SP_OBJECT_PARENT(item));
1521     }
1522     g_assert(SP_IS_ROOT(item));
1524     ret *= to_2geom(item->transform);
1526     return ret;
1529 /* fixme: This is EVIL!!! */
1530 // fix this note: why/what evil? :)
1531 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1533     g_assert(item != NULL);
1534     g_assert(SP_IS_ITEM(item));
1536     Geom::Matrix const ret( sp_item_i2doc_affine(item)
1537                           * Geom::Scale(1, -1)
1538                           * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1539     return ret;
1542 // same as i2d but with i2root instead of i2doc
1543 Geom::Matrix sp_item_i2r_affine(SPItem const *item)
1545     g_assert(item != NULL);
1546     g_assert(SP_IS_ITEM(item));
1548     Geom::Matrix const ret( sp_item_i2root_affine(item)
1549                           * Geom::Scale(1, -1)
1550                           * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1551     return ret;
1554 /**
1555  * Converts a matrix \a m into the desktop coords of the \a item.
1556  * Will become a noop when we eliminate the coordinate flipping.
1557  */
1558 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1560     Geom::Matrix const ret(m
1561                          * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1562                          * Geom::Scale(1, -1));
1563     return ret;
1566 /**
1567  * Converts a matrix \a m from the desktop coords of the \a item.
1568  * Will become a noop when we eliminate the coordinate flipping.
1569  */
1570 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1572     Geom::Matrix const ret(Geom::Scale(1, -1)
1573                          * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1574                          * m);
1575     return ret;
1578 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1580     g_return_if_fail( item != NULL );
1581     g_return_if_fail( SP_IS_ITEM(item) );
1583     Geom::Matrix dt2p; /* desktop to item parent transform */
1584     if (SP_OBJECT_PARENT(item)) {
1585         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1586     } else {
1587         dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1588                  * Geom::Scale(1, -1) );
1589     }
1591     Geom::Matrix const i2p( i2dt * dt2p );
1592     sp_item_set_item_transform(item, from_2geom(i2p));
1596 Geom::Matrix
1597 sp_item_dt2i_affine(SPItem const *item)
1599     /* fixme: Implement the right way (Lauris) */
1600     return sp_item_i2d_affine(item).inverse();
1603 /* Item views */
1605 static SPItemView *
1606 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1608     SPItemView *new_view;
1610     g_assert(item != NULL);
1611     g_assert(SP_IS_ITEM(item));
1612     g_assert(arenaitem != NULL);
1613     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1615     new_view = g_new(SPItemView, 1);
1617     new_view->next = list;
1618     new_view->flags = flags;
1619     new_view->key = key;
1620     new_view->arenaitem = nr_arena_item_ref(arenaitem);
1622     return new_view;
1625 static SPItemView *
1626 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1628     if (view == list) {
1629         list = list->next;
1630     } else {
1631         SPItemView *prev;
1632         prev = list;
1633         while (prev->next != view) prev = prev->next;
1634         prev->next = view->next;
1635     }
1637     nr_arena_item_unref(view->arenaitem);
1638     g_free(view);
1640     return list;
1643 /**
1644  * Return the arenaitem corresponding to the given item in the display
1645  * with the given key
1646  */
1647 NRArenaItem *
1648 sp_item_get_arenaitem(SPItem *item, unsigned key)
1650     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1651         if ( iv->key == key ) {
1652             return iv->arenaitem;
1653         }
1654     }
1656     return NULL;
1659 int
1660 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1662     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1663                                     SP_OBJECT_REPR(second));
1666 SPItem *
1667 sp_item_first_item_child (SPObject *obj)
1669     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1670         if (SP_IS_ITEM (iter))
1671             return SP_ITEM (iter);
1672     }
1673     return NULL;
1676 void
1677 sp_item_convert_to_guides(SPItem *item) {
1678     SPDesktop *dt = inkscape_active_desktop();
1679     SPNamedView *nv = sp_desktop_namedview(dt);
1680     (void)nv;
1682     int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
1683     SPItem::BBoxType bbox_type = (prefs_bbox ==0)? 
1684         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1686     NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1687     if (!bbox) {
1688         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1689         return;
1690     }
1692     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1694     NR::Point A((*bbox).min());
1695     NR::Point C((*bbox).max());
1696     NR::Point B(A[NR::X], C[NR::Y]);
1697     NR::Point D(C[NR::X], A[NR::Y]);
1699     pts.push_back(std::make_pair(A.to_2geom(), B.to_2geom()));
1700     pts.push_back(std::make_pair(B.to_2geom(), C.to_2geom()));
1701     pts.push_back(std::make_pair(C.to_2geom(), D.to_2geom()));
1702     pts.push_back(std::make_pair(D.to_2geom(), A.to_2geom()));
1704     sp_guide_pt_pairs_to_guides(SP_OBJECT_DOCUMENT(item), pts);
1707 /*
1708   Local Variables:
1709   mode:c++
1710   c-file-style:"stroustrup"
1711   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1712   indent-tabs-mode:nil
1713   fill-column:99
1714   End:
1715 */
1716 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :