Code

fix crash when ungrouping lpe paths
[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"
69 #include "xml/repr.h"
70 #include "extract-uri.h"
72 #include "live_effects/lpeobject.h"
73 #include "live_effects/effect.h"
74 #include "live_effects/lpeobject-reference.h"
76 #define noSP_ITEM_DEBUG_IDLE
78 static void sp_item_class_init(SPItemClass *klass);
79 static void sp_item_init(SPItem *item);
81 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
82 static void sp_item_release(SPObject *object);
83 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
84 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
85 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
87 static gchar *sp_item_private_description(SPItem *item);
88 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
90 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
91 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
93 static SPObjectClass *parent_class;
95 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
96 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
98 /**
99  * Registers SPItem class and returns its type number.
100  */
101 GType
102 sp_item_get_type(void)
104     static GType type = 0;
105     if (!type) {
106         GTypeInfo info = {
107             sizeof(SPItemClass),
108             NULL, NULL,
109             (GClassInitFunc) sp_item_class_init,
110             NULL, NULL,
111             sizeof(SPItem),
112             16,
113             (GInstanceInitFunc) sp_item_init,
114             NULL,   /* value_table */
115         };
116         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
117     }
118     return type;
121 /**
122  * SPItem vtable initialization.
123  */
124 static void
125 sp_item_class_init(SPItemClass *klass)
127     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
129     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
131     sp_object_class->build = sp_item_build;
132     sp_object_class->release = sp_item_release;
133     sp_object_class->set = sp_item_set;
134     sp_object_class->update = sp_item_update;
135     sp_object_class->write = sp_item_write;
137     klass->description = sp_item_private_description;
138     klass->snappoints = sp_item_private_snappoints;
141 /**
142  * Callback for SPItem object initialization.
143  */
144 static void
145 sp_item_init(SPItem *item)
147     item->init();
150 void SPItem::init() {
151     this->sensitive = TRUE;
153     this->transform_center_x = 0;
154     this->transform_center_y = 0;
156     this->_is_evaluated = true;
157     this->_evaluated_status = StatusUnknown;
159     this->transform = NR::identity();
161     this->display = NULL;
163     this->clip_ref = new SPClipPathReference(this);
164                 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
165                 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
166     _clip_ref_connection = cs1.connect(sl1);
168     this->mask_ref = new SPMaskReference(this);
169                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
170                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
171     _mask_ref_connection = cs2.connect(sl2);
173     this->avoidRef = new SPAvoidRef(this);
175     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
178 bool SPItem::isVisibleAndUnlocked() const {
179     return (!isHidden() && !isLocked());
182 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
183     return (!isHidden(display_key) && !isLocked());
186 bool SPItem::isLocked() const {
187     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
188         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
189             return true;
190     }
191     return false;
194 void SPItem::setLocked(bool locked) {
195     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
196                      ( locked ? "1" : NULL ));
197     updateRepr();
200 bool SPItem::isHidden() const {
201     if (!isEvaluated())
202         return true;
203     return style->display.computed == SP_CSS_DISPLAY_NONE;
206 void SPItem::setHidden(bool hide) {
207     style->display.set = TRUE;
208     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
209     style->display.computed = style->display.value;
210     style->display.inherit = FALSE;
211     updateRepr();
214 bool SPItem::isHidden(unsigned display_key) const {
215     if (!isEvaluated())
216         return true;
217     for ( SPItemView *view(display) ; view ; view = view->next ) {
218         if ( view->key == display_key ) {
219             g_assert(view->arenaitem != NULL);
220             for ( NRArenaItem *arenaitem = view->arenaitem ;
221                   arenaitem ; arenaitem = arenaitem->parent )
222             {
223                 if (!arenaitem->visible) {
224                     return true;
225                 }
226             }
227             return false;
228         }
229     }
230     return true;
233 void SPItem::setEvaluated(bool evaluated) {
234     _is_evaluated = evaluated;
235     _evaluated_status = StatusSet;
238 void SPItem::resetEvaluated() {
239     if ( StatusCalculated == _evaluated_status ) {
240         _evaluated_status = StatusUnknown;
241         bool oldValue = _is_evaluated;
242         if ( oldValue != isEvaluated() ) {
243             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
244         }
245     } if ( StatusSet == _evaluated_status ) {
246         SPObject const *const parent = SP_OBJECT_PARENT(this);
247         if (SP_IS_SWITCH(parent)) {
248             SP_SWITCH(parent)->resetChildEvaluated();
249         }
250     }
253 bool SPItem::isEvaluated() const {
254     if ( StatusUnknown == _evaluated_status ) {
255         _is_evaluated = sp_item_evaluate(this);
256         _evaluated_status = StatusCalculated;
257     }
258     return _is_evaluated;
261 /**
262  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
263  *  Corresponds to setExplicitlyHidden.
264  */
265 bool
266 SPItem::isExplicitlyHidden() const
268     return (this->style->display.set
269             && this->style->display.value == SP_CSS_DISPLAY_NONE);
272 /**
273  * Sets the display CSS property to `hidden' if \a val is true,
274  * otherwise makes it unset
275  */
276 void
277 SPItem::setExplicitlyHidden(bool const val) {
278     this->style->display.set = val;
279     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
280     this->style->display.computed = this->style->display.value;
281     this->updateRepr();
284 /**
285  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
286  */
287 void
288 SPItem::setCenter(NR::Point object_centre) {
289     NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
290     if (bbox) {
291         transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
292         if (fabs(transform_center_x) < 1e-5) // rounding error
293             transform_center_x = 0;
294         transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
295         if (fabs(transform_center_y) < 1e-5) // rounding error
296             transform_center_y = 0;
297     }
300 void
301 SPItem::unsetCenter() {
302     transform_center_x = 0;
303     transform_center_y = 0;
306 bool SPItem::isCenterSet() {
307     return (transform_center_x != 0 || transform_center_y != 0);
310 NR::Point SPItem::getCenter() const {
311     NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
312     if (bbox) {
313         return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
314     } else {
315         return NR::Point (0, 0); // something's wrong!
316     }
320 namespace {
322 bool is_item(SPObject const &object) {
323     return SP_IS_ITEM(&object);
328 void SPItem::raiseToTop() {
329     using Inkscape::Algorithms::find_last_if;
331     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
332         SP_OBJECT_NEXT(this), NULL, &is_item
333     );
334     if (topmost) {
335         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
336         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
337     }
340 void SPItem::raiseOne() {
341     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
342         SP_OBJECT_NEXT(this), NULL, &is_item
343     );
344     if (next_higher) {
345         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
346         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
347         sp_repr_parent(repr)->changeOrder(repr, ref);
348     }
351 void SPItem::lowerOne() {
352     using Inkscape::Util::MutableList;
353     using Inkscape::Util::reverse_list;
355     MutableList<SPObject &> next_lower=std::find_if(
356         reverse_list<SPObject::SiblingIterator>(
357             SP_OBJECT_PARENT(this)->firstChild(), this
358         ),
359         MutableList<SPObject &>(),
360         &is_item
361     );
362     if (next_lower) {
363         ++next_lower;
364         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
365         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
366         sp_repr_parent(repr)->changeOrder(repr, ref);
367     }
370 void SPItem::lowerToBottom() {
371     using Inkscape::Algorithms::find_last_if;
372     using Inkscape::Util::MutableList;
373     using Inkscape::Util::reverse_list;
375     MutableList<SPObject &> bottom=find_last_if(
376         reverse_list<SPObject::SiblingIterator>(
377             SP_OBJECT_PARENT(this)->firstChild(), this
378         ),
379         MutableList<SPObject &>(),
380         &is_item
381     );
382     if (bottom) {
383         ++bottom;
384         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
385         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
386         sp_repr_parent(repr)->changeOrder(repr, ref);
387     }
390 static void
391 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
393     sp_object_read_attr(object, "style");
394     sp_object_read_attr(object, "transform");
395     sp_object_read_attr(object, "clip-path");
396     sp_object_read_attr(object, "mask");
397     sp_object_read_attr(object, "sodipodi:insensitive");
398     sp_object_read_attr(object, "sodipodi:nonprintable");
399     sp_object_read_attr(object, "inkscape:transform-center-x");
400     sp_object_read_attr(object, "inkscape:transform-center-y");
401     sp_object_read_attr(object, "inkscape:connector-avoid");
403     if (((SPObjectClass *) (parent_class))->build) {
404         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
405     }
408 static void
409 sp_item_release(SPObject *object)
411     SPItem *item = (SPItem *) object;
413     item->_clip_ref_connection.disconnect();
414     item->_mask_ref_connection.disconnect();
416     // Note: do this here before the clip_ref is deleted, since calling
417     // sp_document_ensure_up_to_date for triggered routing may reference
418     // the deleted clip_ref.
419     if (item->avoidRef) {
420         delete item->avoidRef;
421         item->avoidRef = NULL;
422     }
424     if (item->clip_ref) {
425         item->clip_ref->detach();
426         delete item->clip_ref;
427         item->clip_ref = NULL;
428     }
430     if (item->mask_ref) {
431         item->mask_ref->detach();
432         delete item->mask_ref;
433         item->mask_ref = NULL;
434     }
436     if (((SPObjectClass *) (parent_class))->release) {
437         ((SPObjectClass *) parent_class)->release(object);
438     }
440     while (item->display) {
441         nr_arena_item_unparent(item->display->arenaitem);
442         item->display = sp_item_view_list_remove(item->display, item->display);
443     }
445     item->_transformed_signal.~signal();
448 static void
449 sp_item_set(SPObject *object, unsigned key, gchar const *value)
451     SPItem *item = (SPItem *) object;
453     switch (key) {
454         case SP_ATTR_TRANSFORM: {
455             NR::Matrix t;
456             if (value && sp_svg_transform_read(value, &t)) {
457                 sp_item_set_item_transform(item, t);
458             } else {
459                 sp_item_set_item_transform(item, NR::identity());
460             }
461             break;
462         }
463         case SP_PROP_CLIP_PATH: {
464             gchar *uri = extract_uri(value);
465             if (uri) {
466                 try {
467                     item->clip_ref->attach(Inkscape::URI(uri));
468                 } catch (Inkscape::BadURIException &e) {
469                     g_warning("%s", e.what());
470                     item->clip_ref->detach();
471                 }
472                 g_free(uri);
473             } else {
474                 item->clip_ref->detach();
475             }
477             break;
478         }
479         case SP_PROP_MASK: {
480             gchar *uri = extract_uri(value);
481             if (uri) {
482                 try {
483                     item->mask_ref->attach(Inkscape::URI(uri));
484                 } catch (Inkscape::BadURIException &e) {
485                     g_warning("%s", e.what());
486                     item->mask_ref->detach();
487                 }
488                 g_free(uri);
489             } else {
490                 item->mask_ref->detach();
491             }
493             break;
494         }
495         case SP_ATTR_SODIPODI_INSENSITIVE:
496             item->sensitive = !value;
497             for (SPItemView *v = item->display; v != NULL; v = v->next) {
498                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
499             }
500             break;
501         case SP_ATTR_CONNECTOR_AVOID:
502             item->avoidRef->setAvoid(value);
503             break;
504         case SP_ATTR_TRANSFORM_CENTER_X:
505             if (value) {
506                 item->transform_center_x = g_strtod(value, NULL);
507             } else {
508                 item->transform_center_x = 0;
509             }
510             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
511             break;
512         case SP_ATTR_TRANSFORM_CENTER_Y:
513             if (value) {
514                 item->transform_center_y = g_strtod(value, NULL);
515             } else {
516                 item->transform_center_y = 0;
517             }
518             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
519             break;
520         case SP_PROP_SYSTEM_LANGUAGE:
521         case SP_PROP_REQUIRED_FEATURES:
522         case SP_PROP_REQUIRED_EXTENSIONS:
523             {
524                 item->resetEvaluated();
525                 // pass to default handler
526             }
527         default:
528             if (SP_ATTRIBUTE_IS_CSS(key)) {
529                 sp_style_read_from_object(object->style, object);
530                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
531             } else {
532                 if (((SPObjectClass *) (parent_class))->set) {
533                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
534                 }
535             }
536             break;
537     }
540 static void
541 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
543     if (old_clip) {
544         SPItemView *v;
545         /* Hide clippath */
546         for (v = item->display; v != NULL; v = v->next) {
547             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
548             nr_arena_item_set_clip(v->arenaitem, NULL);
549         }
550     }
551     if (SP_IS_CLIPPATH(clip)) {
552         NRRect bbox;
553         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
554         for (SPItemView *v = item->display; v != NULL; v = v->next) {
555             if (!v->arenaitem->key) {
556                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
557             }
558             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
559                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
560                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
561             nr_arena_item_set_clip(v->arenaitem, ai);
562             nr_arena_item_unref(ai);
563             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
564             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
565         }
566     }
569 static void
570 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
572     if (old_mask) {
573         /* Hide mask */
574         for (SPItemView *v = item->display; v != NULL; v = v->next) {
575             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
576             nr_arena_item_set_mask(v->arenaitem, NULL);
577         }
578     }
579     if (SP_IS_MASK(mask)) {
580         NRRect bbox;
581         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
582         for (SPItemView *v = item->display; v != NULL; v = v->next) {
583             if (!v->arenaitem->key) {
584                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
585             }
586             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
587                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
588                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
589             nr_arena_item_set_mask(v->arenaitem, ai);
590             nr_arena_item_unref(ai);
591             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
592             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
593         }
594     }
597 static void
598 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
600     SPItem *item = SP_ITEM(object);
602     if (((SPObjectClass *) (parent_class))->update)
603         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
605     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
606         if (flags & SP_OBJECT_MODIFIED_FLAG) {
607             for (SPItemView *v = item->display; v != NULL; v = v->next) {
608                 nr_arena_item_set_transform(v->arenaitem, item->transform);
609             }
610         }
612         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
613         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
615         if ( clip_path || mask ) {
616             NRRect bbox;
617             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
618             if (clip_path) {
619                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
620                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
621                 }
622             }
623             if (mask) {
624                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
625                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
626                 }
627             }
628         }
630         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
631             for (SPItemView *v = item->display; v != NULL; v = v->next) {
632                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
633                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
634             }
635         }
636     }
638     /* Update bounding box data used by filters */
639     if (item->style->filter.set && item->display) {
640         NRRect item_bbox;
641         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
642         NR::Maybe<NR::Rect> i_bbox = item_bbox;
644         SPItemView *itemview = item->display;
645         do {
646             if (itemview->arenaitem)
647                 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
648         } while ( (itemview = itemview->next) );
649     }
651     // Update libavoid with item geometry (for connector routing).
652     if (item->avoidRef)
653         item->avoidRef->handleSettingChange();
656 static Inkscape::XML::Node *
657 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
659     SPItem *item = SP_ITEM(object);
661     gchar *c = sp_svg_transform_write(item->transform);
662     repr->setAttribute("transform", c);
663     g_free(c);
665     if (flags & SP_OBJECT_WRITE_EXT) {
666         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
667         if (item->transform_center_x != 0)
668             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
669         else
670             repr->setAttribute ("inkscape:transform-center-x", NULL);
671         if (item->transform_center_y != 0)
672             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
673         else
674             repr->setAttribute ("inkscape:transform-center-y", NULL);
675     }
677     if (item->clip_ref->getObject()) {
678         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
679         repr->setAttribute ("clip-path", value);
680         g_free ((void *) value);
681     }
682     if (item->mask_ref->getObject()) {
683         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
684         repr->setAttribute ("mask", value);
685         g_free ((void *) value);
686     }
688     if (((SPObjectClass *) (parent_class))->write) {
689         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
690     }
692     return repr;
695 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
696                                       SPItem::BBoxType type,
697                                       unsigned int /*dkey*/) const
699     NR::Maybe<NR::Rect> r = NR::Nothing();
700     sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
701     return r;
704 void
705 sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
707     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
710 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
711 void
712 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
714     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
717 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
718  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
719  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
720  * clones), in turn, call this function in their bbox methods. */
721 void
722 sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
724     g_assert(item != NULL);
725     g_assert(SP_IS_ITEM(item));
726     g_assert(bbox != NULL);
728     if (clear) {
729         *bbox = NR::Nothing();
730     }
732     // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
734     NRRect temp_bbox;
735     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
736     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
738     // call the subclass method
739     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
740         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
741     }
743     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
744     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
745         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
746             SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
747             if (filter && SP_IS_FILTER(filter)) {
748                 // default filer area per the SVG spec:
749                 double x = -0.1;
750                 double y = -0.1;
751                 double w = 1.2;
752                 double h = 1.2;
754                 // if area is explicitly set, override:
755                 if (SP_FILTER(filter)->x._set)
756                     x = SP_FILTER(filter)->x.computed;
757                 if (SP_FILTER(filter)->y._set)
758                     y = SP_FILTER(filter)->y.computed;
759                 if (SP_FILTER(filter)->width._set)
760                     w = SP_FILTER(filter)->width.computed;
761                 if (SP_FILTER(filter)->height._set) 
762                     h = SP_FILTER(filter)->height.computed;
764                 double dx0 = 0;
765                 double dx1 = 0;
766                 double dy0 = 0;
767                 double dy1 = 0;
768                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
769                     // if this is a single blur, use 2.4*radius 
770                     // which may be smaller than the default area;
771                     // see set_filter_area for why it's 2.4
772                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
773                     dx0 = -2.4 * r;
774                     dx1 = 2.4 * r;
775                     dy0 = -2.4 * r;
776                     dy1 = 2.4 * r;
777                 } else {
778                     // otherwise, calculate expansion from relative to absolute units:
779                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
780                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
781                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
782                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
783                 }
785                 // transform the expansions by the item's transform:
786                 NR::Matrix i2d = sp_item_i2d_affine (item);
787                 dx0 *= NR::expansionX(i2d);
788                 dx1 *= NR::expansionX(i2d);
789                 dy0 *= NR::expansionY(i2d);
790                 dy1 *= NR::expansionY(i2d);
792                 // expand the bbox
793                 temp_bbox.x0 += dx0;
794                 temp_bbox.x1 += dx1;
795                 temp_bbox.y0 += dy0;
796                 temp_bbox.y1 += dy1;
797             }
798         }
799         if (item->clip_ref->getObject()) {
800             NRRect b;
801             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
802             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
803         }
804     }
806     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
807         // Either the bbox hasn't been touched by the SPItemClass' bbox method 
808         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
809         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
810         
811         // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been 
812         // explicitely defined this way for NRRects (as opposed to NR::Maybe<NR::Rect>)
813         *bbox = NR::Nothing();
814         return;
815     }
817     // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
818     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
819     // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
820     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));
822     *bbox = NR::union_bounds(*bbox, temp_bbox_new);
825 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
826 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
827  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
828  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
829  * clones), in turn, call this function in their bbox methods. */
830 void
831 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
833     g_assert(item != NULL);
834     g_assert(SP_IS_ITEM(item));
835     g_assert(bbox != NULL);
837     if (clear) {
838         bbox->x0 = bbox->y0 = 1e18;
839         bbox->x1 = bbox->y1 = -1e18;
840     }
842     NRRect this_bbox;
843     this_bbox.x0 = this_bbox.y0 = 1e18;
844     this_bbox.x1 = this_bbox.y1 = -1e18;
846     // call the subclass method
847     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
848         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
849     }
851     // unless this is geometric bbox, crop the bbox by clip path, if any
852     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
853         NRRect b;
854         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
855         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
856     }
858     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
859     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
860         nr_rect_d_union (bbox, bbox, &this_bbox);
861     }
864 unsigned sp_item_pos_in_parent(SPItem *item)
866     g_assert(item != NULL);
867     g_assert(SP_IS_ITEM(item));
869     SPObject *parent = SP_OBJECT_PARENT(item);
870     g_assert(parent != NULL);
871     g_assert(SP_IS_OBJECT(parent));
873     SPObject *object = SP_OBJECT(item);
875     unsigned pos=0;
876     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
877         if ( iter == object ) {
878             return pos;
879         }
880         if (SP_IS_ITEM(iter)) {
881             pos++;
882         }
883     }
885     g_assert_not_reached();
886     return 0;
889 void
890 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
892     g_assert(item != NULL);
893     g_assert(SP_IS_ITEM(item));
894     g_assert(bbox != NULL);
896     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
899 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
901     NR::Maybe<NR::Rect> rect = NR::Nothing();
902     sp_item_invoke_bbox(item, &rect, sp_item_i2d_affine(item), TRUE, type);
903     return rect;
906 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
908     NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
909     /* Just the corners of the bounding box suffices given that we don't yet
910        support angled guide lines. */
912     if (bbox) {
913         NR::Point p1, p2;
914         p1 = bbox->min();
915         p2 = bbox->max();
916         *p = p1;
917         *p = NR::Point(p1[NR::X], p2[NR::Y]);
918         *p = p2;
919         *p = NR::Point(p1[NR::Y], p2[NR::X]);
920     }
923 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
925     g_assert (item != NULL);
926     g_assert (SP_IS_ITEM(item));
928     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
929     if (item_class.snappoints) {
930         item_class.snappoints(item, p);
931     }
933     if (includeItemCenter) {
934         *p = item->getCenter();
935     }
938 void
939 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
941     if (!item->isHidden()) {
942         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
943             if (!item->transform.test_identity()
944                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
945             {
946                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
947                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
948                 sp_print_release(ctx);
949             } else {
950                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
951             }
952         }
953     }
956 static gchar *
957 sp_item_private_description(SPItem */*item*/)
959     return g_strdup(_("Object"));
962 /**
963  * Returns a string suitable for status bar, formatted in pango markup language.
964  *
965  * Must be freed by caller.
966  */
967 gchar *
968 sp_item_description(SPItem *item)
970     g_assert(item != NULL);
971     g_assert(SP_IS_ITEM(item));
973     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
974         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
975         if (s && item->clip_ref->getObject()) {
976             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
977             g_free (s);
978             s = snew;
979         }
980         if (s && item->mask_ref->getObject()) {
981             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
982             g_free (s);
983             s = snew;
984         }
985         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
986             gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
987             g_free (s);
988             s = snew;
989         }
990         return s;
991     }
993     g_assert_not_reached();
994     return NULL;
997 /**
998  * Allocates unique integer keys.
999  * \param numkeys Number of keys required.
1000  * \return First allocated key; hence if the returned key is n
1001  * you can use n, n + 1, ..., n + (numkeys - 1)
1002  */
1003 unsigned
1004 sp_item_display_key_new(unsigned numkeys)
1006     static unsigned dkey = 0;
1008     dkey += numkeys;
1010     return dkey - numkeys;
1013 NRArenaItem *
1014 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1016     g_assert(item != NULL);
1017     g_assert(SP_IS_ITEM(item));
1018     g_assert(arena != NULL);
1019     g_assert(NR_IS_ARENA(arena));
1021     NRArenaItem *ai = NULL;
1022     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1023         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1024     }
1026     if (ai != NULL) {
1027         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1028         nr_arena_item_set_transform(ai, item->transform);
1029         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1030         nr_arena_item_set_visible(ai, !item->isHidden());
1031         nr_arena_item_set_sensitive(ai, item->sensitive);
1032         if (item->clip_ref->getObject()) {
1033             SPClipPath *cp = item->clip_ref->getObject();
1035             if (!item->display->arenaitem->key) {
1036                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1037             }
1038             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1040             // Show and set clip
1041             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1042             nr_arena_item_set_clip(ai, ac);
1043             nr_arena_item_unref(ac);
1045             // Update bbox, in case the clip uses bbox units
1046             NRRect bbox;
1047             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1048             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1049             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1050         }
1051         if (item->mask_ref->getObject()) {
1052             SPMask *mask = item->mask_ref->getObject();
1054             if (!item->display->arenaitem->key) {
1055                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1056             }
1057             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1059             // Show and set mask
1060             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1061             nr_arena_item_set_mask(ai, ac);
1062             nr_arena_item_unref(ac);
1064             // Update bbox, in case the mask uses bbox units
1065             NRRect bbox;
1066             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1067             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1068             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1069         }
1070         NR_ARENA_ITEM_SET_DATA(ai, item);
1071         NRRect item_bbox;
1072         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1073         NR::Maybe<NR::Rect> i_bbox = item_bbox;
1074         nr_arena_item_set_item_bbox(ai, i_bbox);
1075     }
1077     return ai;
1080 void
1081 sp_item_invoke_hide(SPItem *item, unsigned key)
1083     g_assert(item != NULL);
1084     g_assert(SP_IS_ITEM(item));
1086     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1087         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1088     }
1090     SPItemView *ref = NULL;
1091     SPItemView *v = item->display;
1092     while (v != NULL) {
1093         SPItemView *next = v->next;
1094         if (v->key == key) {
1095             if (item->clip_ref->getObject()) {
1096                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1097                 nr_arena_item_set_clip(v->arenaitem, NULL);
1098             }
1099             if (item->mask_ref->getObject()) {
1100                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1101                 nr_arena_item_set_mask(v->arenaitem, NULL);
1102             }
1103             if (!ref) {
1104                 item->display = v->next;
1105             } else {
1106                 ref->next = v->next;
1107             }
1108             nr_arena_item_unparent(v->arenaitem);
1109             nr_arena_item_unref(v->arenaitem);
1110             g_free(v);
1111         } else {
1112             ref = v;
1113         }
1114         v = next;
1115     }
1118 // Adjusters
1120 void
1121 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1123     SPStyle *style = SP_OBJECT_STYLE (item);
1125     if (style && (style->fill.isPaintserver())) {
1126         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1127         if (SP_IS_PATTERN (server)) {
1128             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1129             sp_pattern_transform_multiply (pattern, postmul, set);
1130         }
1131     }
1133     if (style && (style->stroke.isPaintserver())) {
1134         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1135         if (SP_IS_PATTERN (server)) {
1136             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1137             sp_pattern_transform_multiply (pattern, postmul, set);
1138         }
1139     }
1143 void
1144 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1146     SPStyle *style = SP_OBJECT_STYLE (item);
1148     if (style && (style->fill.isPaintserver())) {
1149         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1150         if (SP_IS_GRADIENT (server)) {
1152             /**
1153              * \note Bbox units for a gradient are generally a bad idea because
1154              * with them, you cannot preserve the relative position of the
1155              * object and its gradient after rotation or skew. So now we
1156              * convert them to userspace units which are easy to keep in sync
1157              * just by adding the object's transform to gradientTransform.
1158              * \todo FIXME: convert back to bbox units after transforming with
1159              * the item, so as to preserve the original units.
1160              */
1161             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1163             sp_gradient_transform_multiply (gradient, postmul, set);
1164         }
1165     }
1167     if (style && (style->stroke.isPaintserver())) {
1168         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1169         if (SP_IS_GRADIENT (server)) {
1170             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1171             sp_gradient_transform_multiply (gradient, postmul, set);
1172         }
1173     }
1176 void
1177 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1179     SPStyle *style = SP_OBJECT_STYLE (item);
1181     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1183         style->stroke_width.computed *= ex;
1184         style->stroke_width.set = TRUE;
1186         if (style->stroke_dash.n_dash != 0) {
1187             int i;
1188             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1189                 style->stroke_dash.dash[i] *= ex;
1190             }
1191             style->stroke_dash.offset *= ex;
1192         }
1194         SP_OBJECT(item)->updateRepr();
1195     }
1198 /**
1199  * Find out the inverse of previous transform of an item (from its repr)
1200  */
1201 NR::Matrix
1202 sp_item_transform_repr (SPItem *item)
1204     NR::Matrix t_old(NR::identity());
1205     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1206     if (t_attr) {
1207         NR::Matrix t;
1208         if (sp_svg_transform_read(t_attr, &t)) {
1209             t_old = t;
1210         }
1211     }
1213     return t_old;
1217 /**
1218  * Recursively scale stroke width in \a item and its children by \a expansion.
1219  */
1220 void
1221 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1223     sp_item_adjust_stroke (item, expansion);
1225 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1226     if (item && SP_IS_USE(item))
1227         return;
1229     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1230         if (SP_IS_ITEM(o))
1231             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1232     }
1235 /**
1236  * Recursively adjust rx and ry of rects.
1237  */
1238 void
1239 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1241     if (SP_IS_RECT (item)) {
1242         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1243     }
1245     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1246         if (SP_IS_ITEM(o))
1247             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1248     }
1251 /**
1252  * Recursively compensate pattern or gradient transform.
1253  */
1254 void
1255 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1257 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1258 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1259 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1260     NR::Matrix t_item = sp_item_transform_repr (item);
1261     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1263 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1264 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1265 // we must not touch it
1266     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1267         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1268             if (SP_IS_ITEM(o)) {
1269 // At the level of the transformed item, t_ancestors is identity;
1270 // below it, it is the accmmulated chain of transforms from this level to the top level
1271                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1272             }
1273         }
1274     }
1276 // We recursed into children first, and are now adjusting this object second;
1277 // this is so that adjustments in a tree are done from leaves up to the root,
1278 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1279 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1281     if (is_pattern)
1282         sp_item_adjust_pattern (item, paint_delta);
1283     else
1284         sp_item_adjust_gradient (item, paint_delta);
1288 void
1289 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1291     if ( !SP_IS_LPE_ITEM(item) )
1292         return;
1294     SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1295     if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1296         PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
1297         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1298         {
1299             // If the path effect is used by 2 or more items, fork it
1300             // so that each object has its own independent copy of the effect
1301             LivePathEffectObject *lpeobj = (*it)->lpeobject;
1302             if (lpeobj) {
1303                 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1304                 if (new_lpeobj != lpeobj) {
1305                     sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1306                 }
1307         
1308                 if (lpeobj->lpe) {
1309                     Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
1310                     effect->transform_multiply(to_2geom(postmul), set);
1311                 }
1312             }
1313         }
1314     }
1317 /**
1318  * A temporary wrapper for the next function accepting the NR::Matrix
1319  * instead of NR::Matrix
1320  */
1321 void
1322 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1324     if (transform == NULL)
1325         sp_item_write_transform(item, repr, NR::identity(), adv);
1326     else
1327         sp_item_write_transform(item, repr, *transform, adv);
1330 /**
1331  * Set a new transform on an object.
1332  *
1333  * Compensate for stroke scaling and gradient/pattern fill transform, if
1334  * necessary. Call the object's set_transform method if transforms are
1335  * stored optimized. Send _transformed_signal. Invoke _write method so that
1336  * the repr is updated with the new transform.
1337  */
1338 void
1339 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1341     g_return_if_fail(item != NULL);
1342     g_return_if_fail(SP_IS_ITEM(item));
1343     g_return_if_fail(repr != NULL);
1345     // calculate the relative transform, if not given by the adv attribute
1346     NR::Matrix advertized_transform;
1347     if (adv != NULL) {
1348         advertized_transform = *adv;
1349     } else {
1350         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1351     }
1353     if (compensate) {
1355          // recursively compensate for stroke scaling, depending on user preference
1356         if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1357             double const expansion = 1. / NR::expansion(advertized_transform);
1358             sp_item_adjust_stroke_width_recursive(item, expansion);
1359         }
1361         // recursively compensate rx/ry of a rect if requested
1362         if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1363             sp_item_adjust_rects_recursive(item, advertized_transform);
1364         }
1366         // recursively compensate pattern fill if it's not to be transformed
1367         if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1368             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1369         }
1370         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1371         /// recursively compensate gradient fill if it's not to be transformed
1372         if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1373             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1374         } else {
1375             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1376             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1377             sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1378         }
1380     } // endif(compensate)
1382     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1383     NR::Matrix transform_attr (transform);
1384     if ( // run the object's set_transform (i.e. embed transform) only if:
1385          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1386              !preserve && // user did not chose to preserve all transforms
1387              !item->clip_ref->getObject() && // the object does not have a clippath
1388              !item->mask_ref->getObject() && // the object does not have a mask
1389          !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1390              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1391         ) {
1392         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1393     }
1394     sp_item_set_item_transform(item, transform_attr);
1396     // Note: updateRepr comes before emitting the transformed signal since
1397     // it causes clone SPUse's copy of the original object to brought up to
1398     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1399     // values if called in code handling the transformed signal.
1400     SP_OBJECT(item)->updateRepr();
1402     // send the relative transform with a _transformed_signal
1403     item->_transformed_signal.emit(&advertized_transform, item);
1406 gint
1407 sp_item_event(SPItem *item, SPEvent *event)
1409     g_return_val_if_fail(item != NULL, FALSE);
1410     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1411     g_return_val_if_fail(event != NULL, FALSE);
1413     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1414         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1416     return FALSE;
1419 /**
1420  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1421  * gradients, patterns as sp_item_write_transform does.
1422  */
1423 void
1424 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1426     g_return_if_fail(item != NULL);
1427     g_return_if_fail(SP_IS_ITEM(item));
1429     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1430         item->transform = transform;
1431         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1432            transformation.  It's apparently not used anywhere else. */
1433         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1434         sp_item_rm_unsatisfied_cns(*item);
1435     }
1438 void
1439 sp_item_convert_item_to_guides(SPItem *item) {
1440     g_return_if_fail(item != NULL);
1441     g_return_if_fail(SP_IS_ITEM(item));
1443     /* Use derived method if present ... */
1444     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1445         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1446         return;
1447     }
1449     /* .. otherwise simply place the guides around the item's bounding box */
1451     sp_item_convert_to_guides(item);
1455 /**
1456  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1457  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1458  */
1459 NR::Matrix
1460 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1461     NR::Matrix ret(NR::identity());
1462     g_return_val_if_fail(object != NULL, ret);
1464     /* stop at first non-renderable ancestor */
1465     while ( object != ancestor && SP_IS_ITEM(object) ) {
1466         if (SP_IS_ROOT(object)) {
1467             ret *= SP_ROOT(object)->c2p;
1468         }
1469         ret *= SP_ITEM(object)->transform;
1470         object = SP_OBJECT_PARENT(object);
1471     }
1472     return ret;
1475 NR::Matrix
1476 i2i_affine(SPObject const *src, SPObject const *dest) {
1477     g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1478     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1479     return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1482 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1483     return i2i_affine(this, dest);
1486 /**
1487  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1488  * \pre (item != NULL) and SP_IS_ITEM(item).
1489  */
1490 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1492     return i2anc_affine(item, NULL);
1495 /**
1496  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1497  * Used in path operations mostly.
1498  * \pre (item != NULL) and SP_IS_ITEM(item).
1499  */
1500 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1502     g_assert(item != NULL);
1503     g_assert(SP_IS_ITEM(item));
1505     NR::Matrix ret(NR::identity());
1506     g_assert(ret.test_identity());
1507     while ( NULL != SP_OBJECT_PARENT(item) ) {
1508         ret *= item->transform;
1509         item = SP_ITEM(SP_OBJECT_PARENT(item));
1510     }
1511     g_assert(SP_IS_ROOT(item));
1513     ret *= item->transform;
1515     return ret;
1518 /* fixme: This is EVIL!!! */
1520 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1522     g_assert(item != NULL);
1523     g_assert(SP_IS_ITEM(item));
1525     NR::Matrix const ret( sp_item_i2doc_affine(item)
1526                           * NR::scale(1, -1)
1527                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1528     return ret;
1531 // same as i2d but with i2root instead of i2doc
1532 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1534     g_assert(item != NULL);
1535     g_assert(SP_IS_ITEM(item));
1537     NR::Matrix const ret( sp_item_i2root_affine(item)
1538                           * NR::scale(1, -1)
1539                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1540     return ret;
1543 /**
1544  * Converts a matrix \a m into the desktop coords of the \a item.
1545  * Will become a noop when we eliminate the coordinate flipping.
1546  */
1547 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1549     NR::Matrix const ret(m
1550                          * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1551                          * NR::scale(1, -1));
1552     return ret;
1555 /**
1556  * Converts a matrix \a m from the desktop coords of the \a item.
1557  * Will become a noop when we eliminate the coordinate flipping.
1558  */
1559 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1561     NR::Matrix const ret(NR::scale(1, -1)
1562                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1563                          * m);
1564     return ret;
1567 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1569     g_return_if_fail( item != NULL );
1570     g_return_if_fail( SP_IS_ITEM(item) );
1572     NR::Matrix dt2p; /* desktop to item parent transform */
1573     if (SP_OBJECT_PARENT(item)) {
1574         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1575     } else {
1576         dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1577                  * NR::scale(1, -1) );
1578     }
1580     NR::Matrix const i2p( i2dt * dt2p );
1581     sp_item_set_item_transform(item, i2p);
1585 NR::Matrix
1586 sp_item_dt2i_affine(SPItem const *item)
1588     /* fixme: Implement the right way (Lauris) */
1589     return sp_item_i2d_affine(item).inverse();
1592 /* Item views */
1594 static SPItemView *
1595 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1597     SPItemView *new_view;
1599     g_assert(item != NULL);
1600     g_assert(SP_IS_ITEM(item));
1601     g_assert(arenaitem != NULL);
1602     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1604     new_view = g_new(SPItemView, 1);
1606     new_view->next = list;
1607     new_view->flags = flags;
1608     new_view->key = key;
1609     new_view->arenaitem = nr_arena_item_ref(arenaitem);
1611     return new_view;
1614 static SPItemView *
1615 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1617     if (view == list) {
1618         list = list->next;
1619     } else {
1620         SPItemView *prev;
1621         prev = list;
1622         while (prev->next != view) prev = prev->next;
1623         prev->next = view->next;
1624     }
1626     nr_arena_item_unref(view->arenaitem);
1627     g_free(view);
1629     return list;
1632 /**
1633  * Return the arenaitem corresponding to the given item in the display
1634  * with the given key
1635  */
1636 NRArenaItem *
1637 sp_item_get_arenaitem(SPItem *item, unsigned key)
1639     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1640         if ( iv->key == key ) {
1641             return iv->arenaitem;
1642         }
1643     }
1645     return NULL;
1648 int
1649 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1651     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1652                                     SP_OBJECT_REPR(second));
1655 SPItem *
1656 sp_item_first_item_child (SPObject *obj)
1658     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1659         if (SP_IS_ITEM (iter))
1660             return SP_ITEM (iter);
1661     }
1662     return NULL;
1665 void
1666 sp_item_convert_to_guides(SPItem *item) {
1667     SPDesktop *dt = inkscape_active_desktop();
1668     SPNamedView *nv = sp_desktop_namedview(dt);
1669     (void)nv;
1671     int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
1672     SPItem::BBoxType bbox_type = (prefs_bbox ==0)? 
1673         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1675     NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1676     if (!bbox) {
1677         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1678         return;
1679     }
1681     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1683     NR::Point A((*bbox).min());
1684     NR::Point C((*bbox).max());
1685     NR::Point B(A[NR::X], C[NR::Y]);
1686     NR::Point D(C[NR::X], A[NR::Y]);
1688     pts.push_back(std::make_pair(A.to_2geom(), B.to_2geom()));
1689     pts.push_back(std::make_pair(B.to_2geom(), C.to_2geom()));
1690     pts.push_back(std::make_pair(C.to_2geom(), D.to_2geom()));
1691     pts.push_back(std::make_pair(D.to_2geom(), A.to_2geom()));
1693     sp_guide_pt_pairs_to_guides(SP_OBJECT_DOCUMENT(item), pts);
1696 /*
1697   Local Variables:
1698   mode:c++
1699   c-file-style:"stroustrup"
1700   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1701   indent-tabs-mode:nil
1702   fill-column:99
1703   End:
1704 */
1705 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :