Code

mention that an item is filtered in its statusbar description
[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"
75 #define noSP_ITEM_DEBUG_IDLE
77 static void sp_item_class_init(SPItemClass *klass);
78 static void sp_item_init(SPItem *item);
80 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
81 static void sp_item_release(SPObject *object);
82 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
83 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
84 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
86 static gchar *sp_item_private_description(SPItem *item);
87 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
89 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
90 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
92 static SPObjectClass *parent_class;
94 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
95 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
97 /**
98  * Registers SPItem class and returns its type number.
99  */
100 GType
101 sp_item_get_type(void)
103     static GType type = 0;
104     if (!type) {
105         GTypeInfo info = {
106             sizeof(SPItemClass),
107             NULL, NULL,
108             (GClassInitFunc) sp_item_class_init,
109             NULL, NULL,
110             sizeof(SPItem),
111             16,
112             (GInstanceInitFunc) sp_item_init,
113             NULL,   /* value_table */
114         };
115         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
116     }
117     return type;
120 /**
121  * SPItem vtable initialization.
122  */
123 static void
124 sp_item_class_init(SPItemClass *klass)
126     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
128     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
130     sp_object_class->build = sp_item_build;
131     sp_object_class->release = sp_item_release;
132     sp_object_class->set = sp_item_set;
133     sp_object_class->update = sp_item_update;
134     sp_object_class->write = sp_item_write;
136     klass->description = sp_item_private_description;
137     klass->snappoints = sp_item_private_snappoints;
140 /**
141  * Callback for SPItem object initialization.
142  */
143 static void
144 sp_item_init(SPItem *item)
146     item->init();
149 void SPItem::init() {
150     this->sensitive = TRUE;
152     this->transform_center_x = 0;
153     this->transform_center_y = 0;
155     this->_is_evaluated = true;
156     this->_evaluated_status = StatusUnknown;
158     this->transform = NR::identity();
160     this->display = NULL;
162     this->clip_ref = new SPClipPathReference(this);
163                 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
164                 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
165     _clip_ref_connection = cs1.connect(sl1);
167     this->mask_ref = new SPMaskReference(this);
168                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
169                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
170     _mask_ref_connection = cs2.connect(sl2);
172     this->avoidRef = new SPAvoidRef(this);
174     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
177 bool SPItem::isVisibleAndUnlocked() const {
178     return (!isHidden() && !isLocked());
181 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
182     return (!isHidden(display_key) && !isLocked());
185 bool SPItem::isLocked() const {
186     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
187         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
188             return true;
189     }
190     return false;
193 void SPItem::setLocked(bool locked) {
194     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
195                      ( locked ? "1" : NULL ));
196     updateRepr();
199 bool SPItem::isHidden() const {
200     if (!isEvaluated())
201         return true;
202     return style->display.computed == SP_CSS_DISPLAY_NONE;
205 void SPItem::setHidden(bool hide) {
206     style->display.set = TRUE;
207     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
208     style->display.computed = style->display.value;
209     style->display.inherit = FALSE;
210     updateRepr();
213 bool SPItem::isHidden(unsigned display_key) const {
214     if (!isEvaluated())
215         return true;
216     for ( SPItemView *view(display) ; view ; view = view->next ) {
217         if ( view->key == display_key ) {
218             g_assert(view->arenaitem != NULL);
219             for ( NRArenaItem *arenaitem = view->arenaitem ;
220                   arenaitem ; arenaitem = arenaitem->parent )
221             {
222                 if (!arenaitem->visible) {
223                     return true;
224                 }
225             }
226             return false;
227         }
228     }
229     return true;
232 void SPItem::setEvaluated(bool evaluated) {
233     _is_evaluated = evaluated;
234     _evaluated_status = StatusSet;
237 void SPItem::resetEvaluated() {
238     if ( StatusCalculated == _evaluated_status ) {
239         _evaluated_status = StatusUnknown;
240         bool oldValue = _is_evaluated;
241         if ( oldValue != isEvaluated() ) {
242             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
243         }
244     } if ( StatusSet == _evaluated_status ) {
245         SPObject const *const parent = SP_OBJECT_PARENT(this);
246         if (SP_IS_SWITCH(parent)) {
247             SP_SWITCH(parent)->resetChildEvaluated();
248         }
249     }
252 bool SPItem::isEvaluated() const {
253     if ( StatusUnknown == _evaluated_status ) {
254         _is_evaluated = sp_item_evaluate(this);
255         _evaluated_status = StatusCalculated;
256     }
257     return _is_evaluated;
260 /**
261  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
262  *  Corresponds to setExplicitlyHidden.
263  */
264 bool
265 SPItem::isExplicitlyHidden() const
267     return (this->style->display.set
268             && this->style->display.value == SP_CSS_DISPLAY_NONE);
271 /**
272  * Sets the display CSS property to `hidden' if \a val is true,
273  * otherwise makes it unset
274  */
275 void
276 SPItem::setExplicitlyHidden(bool const val) {
277     this->style->display.set = val;
278     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
279     this->style->display.computed = this->style->display.value;
280     this->updateRepr();
283 /**
284  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
285  */
286 void
287 SPItem::setCenter(NR::Point object_centre) {
288     NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
289     if (bbox) {
290         transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
291         if (fabs(transform_center_x) < 1e-5) // rounding error
292             transform_center_x = 0;
293         transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
294         if (fabs(transform_center_y) < 1e-5) // rounding error
295             transform_center_y = 0;
296     }
299 void
300 SPItem::unsetCenter() {
301     transform_center_x = 0;
302     transform_center_y = 0;
305 bool SPItem::isCenterSet() {
306     return (transform_center_x != 0 || transform_center_y != 0);
309 NR::Point SPItem::getCenter() const {
310     NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
311     if (bbox) {
312         return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
313     } else {
314         return NR::Point (0, 0); // something's wrong!
315     }
319 namespace {
321 bool is_item(SPObject const &object) {
322     return SP_IS_ITEM(&object);
327 void SPItem::raiseToTop() {
328     using Inkscape::Algorithms::find_last_if;
330     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
331         SP_OBJECT_NEXT(this), NULL, &is_item
332     );
333     if (topmost) {
334         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
335         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
336     }
339 void SPItem::raiseOne() {
340     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
341         SP_OBJECT_NEXT(this), NULL, &is_item
342     );
343     if (next_higher) {
344         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
345         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
346         sp_repr_parent(repr)->changeOrder(repr, ref);
347     }
350 void SPItem::lowerOne() {
351     using Inkscape::Util::MutableList;
352     using Inkscape::Util::reverse_list;
354     MutableList<SPObject &> next_lower=std::find_if(
355         reverse_list<SPObject::SiblingIterator>(
356             SP_OBJECT_PARENT(this)->firstChild(), this
357         ),
358         MutableList<SPObject &>(),
359         &is_item
360     );
361     if (next_lower) {
362         ++next_lower;
363         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
364         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
365         sp_repr_parent(repr)->changeOrder(repr, ref);
366     }
369 void SPItem::lowerToBottom() {
370     using Inkscape::Algorithms::find_last_if;
371     using Inkscape::Util::MutableList;
372     using Inkscape::Util::reverse_list;
374     MutableList<SPObject &> bottom=find_last_if(
375         reverse_list<SPObject::SiblingIterator>(
376             SP_OBJECT_PARENT(this)->firstChild(), this
377         ),
378         MutableList<SPObject &>(),
379         &is_item
380     );
381     if (bottom) {
382         ++bottom;
383         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
384         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
385         sp_repr_parent(repr)->changeOrder(repr, ref);
386     }
389 static void
390 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
392     sp_object_read_attr(object, "style");
393     sp_object_read_attr(object, "transform");
394     sp_object_read_attr(object, "clip-path");
395     sp_object_read_attr(object, "mask");
396     sp_object_read_attr(object, "sodipodi:insensitive");
397     sp_object_read_attr(object, "sodipodi:nonprintable");
398     sp_object_read_attr(object, "inkscape:transform-center-x");
399     sp_object_read_attr(object, "inkscape:transform-center-y");
400     sp_object_read_attr(object, "inkscape:connector-avoid");
402     if (((SPObjectClass *) (parent_class))->build) {
403         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
404     }
407 static void
408 sp_item_release(SPObject *object)
410     SPItem *item = (SPItem *) object;
412     item->_clip_ref_connection.disconnect();
413     item->_mask_ref_connection.disconnect();
415     // Note: do this here before the clip_ref is deleted, since calling
416     // sp_document_ensure_up_to_date for triggered routing may reference
417     // the deleted clip_ref.
418     if (item->avoidRef) {
419         delete item->avoidRef;
420         item->avoidRef = NULL;
421     }
423     if (item->clip_ref) {
424         item->clip_ref->detach();
425         delete item->clip_ref;
426         item->clip_ref = NULL;
427     }
429     if (item->mask_ref) {
430         item->mask_ref->detach();
431         delete item->mask_ref;
432         item->mask_ref = NULL;
433     }
435     if (((SPObjectClass *) (parent_class))->release) {
436         ((SPObjectClass *) parent_class)->release(object);
437     }
439     while (item->display) {
440         nr_arena_item_unparent(item->display->arenaitem);
441         item->display = sp_item_view_list_remove(item->display, item->display);
442     }
444     item->_transformed_signal.~signal();
447 static void
448 sp_item_set(SPObject *object, unsigned key, gchar const *value)
450     SPItem *item = (SPItem *) object;
452     switch (key) {
453         case SP_ATTR_TRANSFORM: {
454             NR::Matrix t;
455             if (value && sp_svg_transform_read(value, &t)) {
456                 sp_item_set_item_transform(item, t);
457             } else {
458                 sp_item_set_item_transform(item, NR::identity());
459             }
460             break;
461         }
462         case SP_PROP_CLIP_PATH: {
463             gchar *uri = extract_uri(value);
464             if (uri) {
465                 try {
466                     item->clip_ref->attach(Inkscape::URI(uri));
467                 } catch (Inkscape::BadURIException &e) {
468                     g_warning("%s", e.what());
469                     item->clip_ref->detach();
470                 }
471                 g_free(uri);
472             } else {
473                 item->clip_ref->detach();
474             }
476             break;
477         }
478         case SP_PROP_MASK: {
479             gchar *uri = extract_uri(value);
480             if (uri) {
481                 try {
482                     item->mask_ref->attach(Inkscape::URI(uri));
483                 } catch (Inkscape::BadURIException &e) {
484                     g_warning("%s", e.what());
485                     item->mask_ref->detach();
486                 }
487                 g_free(uri);
488             } else {
489                 item->mask_ref->detach();
490             }
492             break;
493         }
494         case SP_ATTR_SODIPODI_INSENSITIVE:
495             item->sensitive = !value;
496             for (SPItemView *v = item->display; v != NULL; v = v->next) {
497                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
498             }
499             break;
500         case SP_ATTR_CONNECTOR_AVOID:
501             item->avoidRef->setAvoid(value);
502             break;
503         case SP_ATTR_TRANSFORM_CENTER_X:
504             if (value) {
505                 item->transform_center_x = g_strtod(value, NULL);
506             } else {
507                 item->transform_center_x = 0;
508             }
509             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
510             break;
511         case SP_ATTR_TRANSFORM_CENTER_Y:
512             if (value) {
513                 item->transform_center_y = g_strtod(value, NULL);
514             } else {
515                 item->transform_center_y = 0;
516             }
517             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
518             break;
519         case SP_PROP_SYSTEM_LANGUAGE:
520         case SP_PROP_REQUIRED_FEATURES:
521         case SP_PROP_REQUIRED_EXTENSIONS:
522             {
523                 item->resetEvaluated();
524                 // pass to default handler
525             }
526         default:
527             if (SP_ATTRIBUTE_IS_CSS(key)) {
528                 sp_style_read_from_object(object->style, object);
529                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
530             } else {
531                 if (((SPObjectClass *) (parent_class))->set) {
532                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
533                 }
534             }
535             break;
536     }
539 static void
540 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
542     if (old_clip) {
543         SPItemView *v;
544         /* Hide clippath */
545         for (v = item->display; v != NULL; v = v->next) {
546             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
547             nr_arena_item_set_clip(v->arenaitem, NULL);
548         }
549     }
550     if (SP_IS_CLIPPATH(clip)) {
551         NRRect bbox;
552         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
553         for (SPItemView *v = item->display; v != NULL; v = v->next) {
554             if (!v->arenaitem->key) {
555                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
556             }
557             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
558                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
559                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
560             nr_arena_item_set_clip(v->arenaitem, ai);
561             nr_arena_item_unref(ai);
562             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
563             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
564         }
565     }
568 static void
569 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
571     if (old_mask) {
572         /* Hide mask */
573         for (SPItemView *v = item->display; v != NULL; v = v->next) {
574             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
575             nr_arena_item_set_mask(v->arenaitem, NULL);
576         }
577     }
578     if (SP_IS_MASK(mask)) {
579         NRRect bbox;
580         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
581         for (SPItemView *v = item->display; v != NULL; v = v->next) {
582             if (!v->arenaitem->key) {
583                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
584             }
585             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
586                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
587                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
588             nr_arena_item_set_mask(v->arenaitem, ai);
589             nr_arena_item_unref(ai);
590             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
591             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
592         }
593     }
596 static void
597 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
599     SPItem *item = SP_ITEM(object);
601     if (((SPObjectClass *) (parent_class))->update)
602         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
604     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
605         if (flags & SP_OBJECT_MODIFIED_FLAG) {
606             for (SPItemView *v = item->display; v != NULL; v = v->next) {
607                 nr_arena_item_set_transform(v->arenaitem, item->transform);
608             }
609         }
611         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
612         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
614         if ( clip_path || mask ) {
615             NRRect bbox;
616             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
617             if (clip_path) {
618                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
619                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
620                 }
621             }
622             if (mask) {
623                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
624                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
625                 }
626             }
627         }
629         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
630             for (SPItemView *v = item->display; v != NULL; v = v->next) {
631                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
632                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
633             }
634         }
635     }
637     /* Update bounding box data used by filters */
638     if (item->style->filter.set && item->display) {
639         NRRect item_bbox;
640         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
641         NR::Maybe<NR::Rect> i_bbox = item_bbox;
643         SPItemView *itemview = item->display;
644         do {
645             if (itemview->arenaitem)
646                 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
647         } while ( (itemview = itemview->next) );
648     }
650     // Update libavoid with item geometry (for connector routing).
651     if (item->avoidRef)
652         item->avoidRef->handleSettingChange();
655 static Inkscape::XML::Node *
656 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
658     SPItem *item = SP_ITEM(object);
660     gchar *c = sp_svg_transform_write(item->transform);
661     repr->setAttribute("transform", c);
662     g_free(c);
664     if (flags & SP_OBJECT_WRITE_EXT) {
665         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
666         if (item->transform_center_x != 0)
667             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
668         else
669             repr->setAttribute ("inkscape:transform-center-x", NULL);
670         if (item->transform_center_y != 0)
671             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
672         else
673             repr->setAttribute ("inkscape:transform-center-y", NULL);
674     }
676     if (item->clip_ref->getObject()) {
677         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
678         repr->setAttribute ("clip-path", value);
679         g_free ((void *) value);
680     }
681     if (item->mask_ref->getObject()) {
682         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
683         repr->setAttribute ("mask", value);
684         g_free ((void *) value);
685     }
687     if (((SPObjectClass *) (parent_class))->write) {
688         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
689     }
691     return repr;
694 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
695                                       SPItem::BBoxType type,
696                                       unsigned int /*dkey*/) const
698     NR::Maybe<NR::Rect> r = NR::Nothing();
699     sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
700     return r;
703 void
704 sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
706     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
709 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
710 void
711 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
713     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
716 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
717  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
718  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
719  * clones), in turn, call this function in their bbox methods. */
720 void
721 sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
723     g_assert(item != NULL);
724     g_assert(SP_IS_ITEM(item));
725     g_assert(bbox != NULL);
727     if (clear) {
728         *bbox = NR::Nothing();
729     }
731     // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
733     NRRect temp_bbox;
734     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
735     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
737     // call the subclass method
738     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
739         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
740     }
742     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
743     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
744         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
745             SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
746             if (filter && SP_IS_FILTER(filter)) {
747                 // default filer area per the SVG spec:
748                 double x = -0.1;
749                 double y = -0.1;
750                 double w = 1.2;
751                 double h = 1.2;
753                 // if area is explicitly set, override:
754                 if (SP_FILTER(filter)->x._set)
755                     x = SP_FILTER(filter)->x.computed;
756                 if (SP_FILTER(filter)->y._set)
757                     y = SP_FILTER(filter)->y.computed;
758                 if (SP_FILTER(filter)->width._set)
759                     w = SP_FILTER(filter)->width.computed;
760                 if (SP_FILTER(filter)->height._set) 
761                     h = SP_FILTER(filter)->height.computed;
763                 double dx0 = 0;
764                 double dx1 = 0;
765                 double dy0 = 0;
766                 double dy1 = 0;
767                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
768                     // if this is a single blur, use 2.4*radius 
769                     // which may be smaller than the default area;
770                     // see set_filter_area for why it's 2.4
771                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
772                     dx0 = -2.4 * r;
773                     dx1 = 2.4 * r;
774                     dy0 = -2.4 * r;
775                     dy1 = 2.4 * r;
776                 } else {
777                     // otherwise, calculate expansion from relative to absolute units:
778                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
779                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
780                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
781                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
782                 }
784                 // transform the expansions by the item's transform:
785                 NR::Matrix i2d = sp_item_i2d_affine (item);
786                 dx0 *= NR::expansionX(i2d);
787                 dx1 *= NR::expansionX(i2d);
788                 dy0 *= NR::expansionY(i2d);
789                 dy1 *= NR::expansionY(i2d);
791                 // expand the bbox
792                 temp_bbox.x0 += dx0;
793                 temp_bbox.x1 += dx1;
794                 temp_bbox.y0 += dy0;
795                 temp_bbox.y1 += dy1;
796             }
797         }
798         if (item->clip_ref->getObject()) {
799             NRRect b;
800             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
801             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
802         }
803     }
805     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
806         // We'll assume here that when x0 > x1 or y0 > y1, the bbox is "nothing"
807         // However it has never been explicitely defined this way for NRRects
808         // (as opposed to NR::Maybe<NR::Rect>)
809         *bbox = NR::Nothing();
810         return;
811     }
813     if (temp_bbox.x0 == temp_bbox.y0 == NR_HUGE && temp_bbox.x1 == temp_bbox.y1 == -NR_HUGE) {
814         // The bbox hasn't been touched by the SPItemClass' bbox method
815         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
816         *bbox = NR::Nothing();
817         return;
818     }
820     // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
821     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
822     // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
823     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));
825     *bbox = NR::union_bounds(*bbox, temp_bbox_new);
828 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
829 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
830  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
831  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
832  * clones), in turn, call this function in their bbox methods. */
833 void
834 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
836     g_assert(item != NULL);
837     g_assert(SP_IS_ITEM(item));
838     g_assert(bbox != NULL);
840     if (clear) {
841         bbox->x0 = bbox->y0 = 1e18;
842         bbox->x1 = bbox->y1 = -1e18;
843     }
845     NRRect this_bbox;
846     this_bbox.x0 = this_bbox.y0 = 1e18;
847     this_bbox.x1 = this_bbox.y1 = -1e18;
849     // call the subclass method
850     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
851         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
852     }
854     // unless this is geometric bbox, crop the bbox by clip path, if any
855     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
856         NRRect b;
857         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
858         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
859     }
861     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
862     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
863         nr_rect_d_union (bbox, bbox, &this_bbox);
864     }
867 unsigned sp_item_pos_in_parent(SPItem *item)
869     g_assert(item != NULL);
870     g_assert(SP_IS_ITEM(item));
872     SPObject *parent = SP_OBJECT_PARENT(item);
873     g_assert(parent != NULL);
874     g_assert(SP_IS_OBJECT(parent));
876     SPObject *object = SP_OBJECT(item);
878     unsigned pos=0;
879     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
880         if ( iter == object ) {
881             return pos;
882         }
883         if (SP_IS_ITEM(iter)) {
884             pos++;
885         }
886     }
888     g_assert_not_reached();
889     return 0;
892 void
893 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
895     g_assert(item != NULL);
896     g_assert(SP_IS_ITEM(item));
897     g_assert(bbox != NULL);
899     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
902 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
904     NR::Maybe<NR::Rect> rect = NR::Nothing();
905     sp_item_invoke_bbox(item, &rect, sp_item_i2d_affine(item), TRUE, type);
906     return rect;
909 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
911     NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
912     /* Just the corners of the bounding box suffices given that we don't yet
913        support angled guide lines. */
915     if (bbox) {
916         NR::Point p1, p2;
917         p1 = bbox->min();
918         p2 = bbox->max();
919         *p = p1;
920         *p = NR::Point(p1[NR::X], p2[NR::Y]);
921         *p = p2;
922         *p = NR::Point(p1[NR::Y], p2[NR::X]);
923     }
926 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
928     g_assert (item != NULL);
929     g_assert (SP_IS_ITEM(item));
931     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
932     if (item_class.snappoints) {
933         item_class.snappoints(item, p);
934     }
936     if (includeItemCenter) {
937         *p = item->getCenter();
938     }
941 void
942 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
944     if (!item->isHidden()) {
945         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
946             if (!item->transform.test_identity()
947                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
948             {
949                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
950                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
951                 sp_print_release(ctx);
952             } else {
953                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
954             }
955         }
956     }
959 static gchar *
960 sp_item_private_description(SPItem */*item*/)
962     return g_strdup(_("Object"));
965 /**
966  * Returns a string suitable for status bar, formatted in pango markup language.
967  *
968  * Must be freed by caller.
969  */
970 gchar *
971 sp_item_description(SPItem *item)
973     g_assert(item != NULL);
974     g_assert(SP_IS_ITEM(item));
976     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
977         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
978         if (s && item->clip_ref->getObject()) {
979             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
980             g_free (s);
981             s = snew;
982         }
983         if (s && item->mask_ref->getObject()) {
984             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
985             g_free (s);
986             s = snew;
987         }
988         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
989             gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
990             g_free (s);
991             s = snew;
992         }
993         return s;
994     }
996     g_assert_not_reached();
997     return NULL;
1000 /**
1001  * Allocates unique integer keys.
1002  * \param numkeys Number of keys required.
1003  * \return First allocated key; hence if the returned key is n
1004  * you can use n, n + 1, ..., n + (numkeys - 1)
1005  */
1006 unsigned
1007 sp_item_display_key_new(unsigned numkeys)
1009     static unsigned dkey = 0;
1011     dkey += numkeys;
1013     return dkey - numkeys;
1016 NRArenaItem *
1017 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1019     g_assert(item != NULL);
1020     g_assert(SP_IS_ITEM(item));
1021     g_assert(arena != NULL);
1022     g_assert(NR_IS_ARENA(arena));
1024     NRArenaItem *ai = NULL;
1025     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1026         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1027     }
1029     if (ai != NULL) {
1030         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1031         nr_arena_item_set_transform(ai, item->transform);
1032         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1033         nr_arena_item_set_visible(ai, !item->isHidden());
1034         nr_arena_item_set_sensitive(ai, item->sensitive);
1035         if (item->clip_ref->getObject()) {
1036             SPClipPath *cp = item->clip_ref->getObject();
1038             if (!item->display->arenaitem->key) {
1039                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1040             }
1041             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1043             // Show and set clip
1044             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1045             nr_arena_item_set_clip(ai, ac);
1046             nr_arena_item_unref(ac);
1048             // Update bbox, in case the clip uses bbox units
1049             NRRect bbox;
1050             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1051             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1052             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1053         }
1054         if (item->mask_ref->getObject()) {
1055             SPMask *mask = item->mask_ref->getObject();
1057             if (!item->display->arenaitem->key) {
1058                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1059             }
1060             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1062             // Show and set mask
1063             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1064             nr_arena_item_set_mask(ai, ac);
1065             nr_arena_item_unref(ac);
1067             // Update bbox, in case the mask uses bbox units
1068             NRRect bbox;
1069             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1070             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1071             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1072         }
1073         NR_ARENA_ITEM_SET_DATA(ai, item);
1074         NRRect item_bbox;
1075         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1076         NR::Maybe<NR::Rect> i_bbox = item_bbox;
1077         nr_arena_item_set_item_bbox(ai, i_bbox);
1078     }
1080     return ai;
1083 void
1084 sp_item_invoke_hide(SPItem *item, unsigned key)
1086     g_assert(item != NULL);
1087     g_assert(SP_IS_ITEM(item));
1089     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1090         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1091     }
1093     SPItemView *ref = NULL;
1094     SPItemView *v = item->display;
1095     while (v != NULL) {
1096         SPItemView *next = v->next;
1097         if (v->key == key) {
1098             if (item->clip_ref->getObject()) {
1099                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1100                 nr_arena_item_set_clip(v->arenaitem, NULL);
1101             }
1102             if (item->mask_ref->getObject()) {
1103                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1104                 nr_arena_item_set_mask(v->arenaitem, NULL);
1105             }
1106             if (!ref) {
1107                 item->display = v->next;
1108             } else {
1109                 ref->next = v->next;
1110             }
1111             nr_arena_item_unparent(v->arenaitem);
1112             nr_arena_item_unref(v->arenaitem);
1113             g_free(v);
1114         } else {
1115             ref = v;
1116         }
1117         v = next;
1118     }
1121 // Adjusters
1123 void
1124 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1126     SPStyle *style = SP_OBJECT_STYLE (item);
1128     if (style && (style->fill.isPaintserver())) {
1129         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1130         if (SP_IS_PATTERN (server)) {
1131             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1132             sp_pattern_transform_multiply (pattern, postmul, set);
1133         }
1134     }
1136     if (style && (style->stroke.isPaintserver())) {
1137         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1138         if (SP_IS_PATTERN (server)) {
1139             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1140             sp_pattern_transform_multiply (pattern, postmul, set);
1141         }
1142     }
1146 void
1147 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1149     SPStyle *style = SP_OBJECT_STYLE (item);
1151     if (style && (style->fill.isPaintserver())) {
1152         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1153         if (SP_IS_GRADIENT (server)) {
1155             /**
1156              * \note Bbox units for a gradient are generally a bad idea because
1157              * with them, you cannot preserve the relative position of the
1158              * object and its gradient after rotation or skew. So now we
1159              * convert them to userspace units which are easy to keep in sync
1160              * just by adding the object's transform to gradientTransform.
1161              * \todo FIXME: convert back to bbox units after transforming with
1162              * the item, so as to preserve the original units.
1163              */
1164             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1166             sp_gradient_transform_multiply (gradient, postmul, set);
1167         }
1168     }
1170     if (style && (style->stroke.isPaintserver())) {
1171         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1172         if (SP_IS_GRADIENT (server)) {
1173             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1174             sp_gradient_transform_multiply (gradient, postmul, set);
1175         }
1176     }
1179 void
1180 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1182     SPStyle *style = SP_OBJECT_STYLE (item);
1184     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1186         style->stroke_width.computed *= ex;
1187         style->stroke_width.set = TRUE;
1189         if (style->stroke_dash.n_dash != 0) {
1190             int i;
1191             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1192                 style->stroke_dash.dash[i] *= ex;
1193             }
1194             style->stroke_dash.offset *= ex;
1195         }
1197         SP_OBJECT(item)->updateRepr();
1198     }
1201 /**
1202  * Find out the inverse of previous transform of an item (from its repr)
1203  */
1204 NR::Matrix
1205 sp_item_transform_repr (SPItem *item)
1207     NR::Matrix t_old(NR::identity());
1208     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1209     if (t_attr) {
1210         NR::Matrix t;
1211         if (sp_svg_transform_read(t_attr, &t)) {
1212             t_old = t;
1213         }
1214     }
1216     return t_old;
1220 /**
1221  * Recursively scale stroke width in \a item and its children by \a expansion.
1222  */
1223 void
1224 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1226     sp_item_adjust_stroke (item, expansion);
1228 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1229     if (item && SP_IS_USE(item))
1230         return;
1232     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1233         if (SP_IS_ITEM(o))
1234             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1235     }
1238 /**
1239  * Recursively adjust rx and ry of rects.
1240  */
1241 void
1242 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1244     if (SP_IS_RECT (item)) {
1245         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1246     }
1248     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1249         if (SP_IS_ITEM(o))
1250             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1251     }
1254 /**
1255  * Recursively compensate pattern or gradient transform.
1256  */
1257 void
1258 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1260 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1261 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1262 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1263     NR::Matrix t_item = sp_item_transform_repr (item);
1264     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1266 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1267 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1268 // we must not touch it
1269     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1270         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1271             if (SP_IS_ITEM(o)) {
1272 // At the level of the transformed item, t_ancestors is identity;
1273 // below it, it is the accmmulated chain of transforms from this level to the top level
1274                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1275             }
1276         }
1277     }
1279 // We recursed into children first, and are now adjusting this object second;
1280 // this is so that adjustments in a tree are done from leaves up to the root,
1281 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1282 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1284     if (is_pattern)
1285         sp_item_adjust_pattern (item, paint_delta);
1286     else
1287         sp_item_adjust_gradient (item, paint_delta);
1291 void
1292 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1294     if ( !SP_IS_SHAPE(item) )
1295         return;
1297     SPShape *shape = SP_SHAPE (item);
1298     if ( sp_shape_has_path_effect(shape) ) {
1299         LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1300         LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1301         if (new_lpeobj != lpeobj) {
1302             sp_shape_set_path_effect(shape, new_lpeobj);
1303         }
1305         Inkscape::LivePathEffect::Effect * effect = sp_shape_get_livepatheffect(shape);
1306         if (effect) {
1307             effect->transform_multiply (to_2geom(postmul), set);
1308         }
1309     }
1312 /**
1313  * A temporary wrapper for the next function accepting the NR::Matrix
1314  * instead of NR::Matrix
1315  */
1316 void
1317 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1319     if (transform == NULL)
1320         sp_item_write_transform(item, repr, NR::identity(), adv);
1321     else
1322         sp_item_write_transform(item, repr, *transform, adv);
1325 /**
1326  * Set a new transform on an object.
1327  *
1328  * Compensate for stroke scaling and gradient/pattern fill transform, if
1329  * necessary. Call the object's set_transform method if transforms are
1330  * stored optimized. Send _transformed_signal. Invoke _write method so that
1331  * the repr is updated with the new transform.
1332  */
1333 void
1334 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1336     g_return_if_fail(item != NULL);
1337     g_return_if_fail(SP_IS_ITEM(item));
1338     g_return_if_fail(repr != NULL);
1340     // calculate the relative transform, if not given by the adv attribute
1341     NR::Matrix advertized_transform;
1342     if (adv != NULL) {
1343         advertized_transform = *adv;
1344     } else {
1345         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1346     }
1348     if (compensate) {
1350          // recursively compensate for stroke scaling, depending on user preference
1351         if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1352             double const expansion = 1. / NR::expansion(advertized_transform);
1353             sp_item_adjust_stroke_width_recursive(item, expansion);
1354         }
1356         // recursively compensate rx/ry of a rect if requested
1357         if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1358             sp_item_adjust_rects_recursive(item, advertized_transform);
1359         }
1361         // recursively compensate pattern fill if it's not to be transformed
1362         if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1363             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1364         }
1365         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1366         /// recursively compensate gradient fill if it's not to be transformed
1367         if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1368             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1369         } else {
1370             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1371             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1372             sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1373         }
1375     } // endif(compensate)
1377     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1378     NR::Matrix transform_attr (transform);
1379     if ( // run the object's set_transform (i.e. embed transform) only if:
1380          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1381              !preserve && // user did not chose to preserve all transforms
1382              !item->clip_ref->getObject() && // the object does not have a clippath
1383              !item->mask_ref->getObject() && // the object does not have a mask
1384          !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1385              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1386         ) {
1387         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1388     }
1389     sp_item_set_item_transform(item, transform_attr);
1391     // Note: updateRepr comes before emitting the transformed signal since
1392     // it causes clone SPUse's copy of the original object to brought up to
1393     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1394     // values if called in code handling the transformed signal.
1395     SP_OBJECT(item)->updateRepr();
1397     // send the relative transform with a _transformed_signal
1398     item->_transformed_signal.emit(&advertized_transform, item);
1401 gint
1402 sp_item_event(SPItem *item, SPEvent *event)
1404     g_return_val_if_fail(item != NULL, FALSE);
1405     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1406     g_return_val_if_fail(event != NULL, FALSE);
1408     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1409         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1411     return FALSE;
1414 /**
1415  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1416  * gradients, patterns as sp_item_write_transform does.
1417  */
1418 void
1419 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1421     g_return_if_fail(item != NULL);
1422     g_return_if_fail(SP_IS_ITEM(item));
1424     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1425         item->transform = transform;
1426         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1427            transformation.  It's apparently not used anywhere else. */
1428         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1429         sp_item_rm_unsatisfied_cns(*item);
1430     }
1433 void
1434 sp_item_convert_item_to_guides(SPItem *item) {
1435     g_return_if_fail(item != NULL);
1436     g_return_if_fail(SP_IS_ITEM(item));
1438     /* Use derived method if present ... */
1439     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1440         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1441         return;
1442     }
1444     /* .. otherwise simply place the guides around the item's bounding box */
1446     sp_item_convert_to_guides(item);
1450 /**
1451  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1452  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1453  */
1454 NR::Matrix
1455 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1456     NR::Matrix ret(NR::identity());
1457     g_return_val_if_fail(object != NULL, ret);
1459     /* stop at first non-renderable ancestor */
1460     while ( object != ancestor && SP_IS_ITEM(object) ) {
1461         if (SP_IS_ROOT(object)) {
1462             ret *= SP_ROOT(object)->c2p;
1463         }
1464         ret *= SP_ITEM(object)->transform;
1465         object = SP_OBJECT_PARENT(object);
1466     }
1467     return ret;
1470 NR::Matrix
1471 i2i_affine(SPObject const *src, SPObject const *dest) {
1472     g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1473     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1474     return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1477 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1478     return i2i_affine(this, dest);
1481 /**
1482  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1483  * \pre (item != NULL) and SP_IS_ITEM(item).
1484  */
1485 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1487     return i2anc_affine(item, NULL);
1490 /**
1491  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1492  * Used in path operations mostly.
1493  * \pre (item != NULL) and SP_IS_ITEM(item).
1494  */
1495 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1497     g_assert(item != NULL);
1498     g_assert(SP_IS_ITEM(item));
1500     NR::Matrix ret(NR::identity());
1501     g_assert(ret.test_identity());
1502     while ( NULL != SP_OBJECT_PARENT(item) ) {
1503         ret *= item->transform;
1504         item = SP_ITEM(SP_OBJECT_PARENT(item));
1505     }
1506     g_assert(SP_IS_ROOT(item));
1508     ret *= item->transform;
1510     return ret;
1513 /* fixme: This is EVIL!!! */
1515 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1517     g_assert(item != NULL);
1518     g_assert(SP_IS_ITEM(item));
1520     NR::Matrix const ret( sp_item_i2doc_affine(item)
1521                           * NR::scale(1, -1)
1522                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1523     return ret;
1526 // same as i2d but with i2root instead of i2doc
1527 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1529     g_assert(item != NULL);
1530     g_assert(SP_IS_ITEM(item));
1532     NR::Matrix const ret( sp_item_i2root_affine(item)
1533                           * NR::scale(1, -1)
1534                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1535     return ret;
1538 /**
1539  * Converts a matrix \a m into the desktop coords of the \a item.
1540  * Will become a noop when we eliminate the coordinate flipping.
1541  */
1542 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1544     NR::Matrix const ret(m
1545                          * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1546                          * NR::scale(1, -1));
1547     return ret;
1550 /**
1551  * Converts a matrix \a m from the desktop coords of the \a item.
1552  * Will become a noop when we eliminate the coordinate flipping.
1553  */
1554 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1556     NR::Matrix const ret(NR::scale(1, -1)
1557                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1558                          * m);
1559     return ret;
1562 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1564     g_return_if_fail( item != NULL );
1565     g_return_if_fail( SP_IS_ITEM(item) );
1567     NR::Matrix dt2p; /* desktop to item parent transform */
1568     if (SP_OBJECT_PARENT(item)) {
1569         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1570     } else {
1571         dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1572                  * NR::scale(1, -1) );
1573     }
1575     NR::Matrix const i2p( i2dt * dt2p );
1576     sp_item_set_item_transform(item, i2p);
1580 NR::Matrix
1581 sp_item_dt2i_affine(SPItem const *item)
1583     /* fixme: Implement the right way (Lauris) */
1584     return sp_item_i2d_affine(item).inverse();
1587 /* Item views */
1589 static SPItemView *
1590 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1592     SPItemView *new_view;
1594     g_assert(item != NULL);
1595     g_assert(SP_IS_ITEM(item));
1596     g_assert(arenaitem != NULL);
1597     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1599     new_view = g_new(SPItemView, 1);
1601     new_view->next = list;
1602     new_view->flags = flags;
1603     new_view->key = key;
1604     new_view->arenaitem = nr_arena_item_ref(arenaitem);
1606     return new_view;
1609 static SPItemView *
1610 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1612     if (view == list) {
1613         list = list->next;
1614     } else {
1615         SPItemView *prev;
1616         prev = list;
1617         while (prev->next != view) prev = prev->next;
1618         prev->next = view->next;
1619     }
1621     nr_arena_item_unref(view->arenaitem);
1622     g_free(view);
1624     return list;
1627 /**
1628  * Return the arenaitem corresponding to the given item in the display
1629  * with the given key
1630  */
1631 NRArenaItem *
1632 sp_item_get_arenaitem(SPItem *item, unsigned key)
1634     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1635         if ( iv->key == key ) {
1636             return iv->arenaitem;
1637         }
1638     }
1640     return NULL;
1643 int
1644 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1646     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1647                                     SP_OBJECT_REPR(second));
1650 SPItem *
1651 sp_item_first_item_child (SPObject *obj)
1653     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1654         if (SP_IS_ITEM (iter))
1655             return SP_ITEM (iter);
1656     }
1657     return NULL;
1660 void
1661 sp_item_convert_to_guides(SPItem *item) {
1662     SPDesktop *dt = inkscape_active_desktop();
1663     SPNamedView *nv = sp_desktop_namedview(dt);
1664     (void)nv;
1666     gchar const *prefs_bbox = prefs_get_string_attribute("tools", "bounding_box");
1667     SPItem::BBoxType bbox_type = (prefs_bbox != NULL && strcmp(prefs_bbox, "geometric")==0)? SPItem::GEOMETRIC_BBOX : SPItem::RENDERING_BBOX;
1669     NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1670     if (!bbox) {
1671         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1672         return;
1673     }
1675     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1677     NR::Point A((*bbox).min());
1678     NR::Point C((*bbox).max());
1679     NR::Point B(A[NR::X], C[NR::Y]);
1680     NR::Point D(C[NR::X], A[NR::Y]);
1682     pts.push_back(std::make_pair(A.to_2geom(), B.to_2geom()));
1683     pts.push_back(std::make_pair(B.to_2geom(), C.to_2geom()));
1684     pts.push_back(std::make_pair(C.to_2geom(), D.to_2geom()));
1685     pts.push_back(std::make_pair(D.to_2geom(), A.to_2geom()));
1687     sp_guide_pt_pairs_to_guides(SP_OBJECT_DOCUMENT(item), pts);
1690 /*
1691   Local Variables:
1692   mode:c++
1693   c-file-style:"stroustrup"
1694   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1695   indent-tabs-mode:nil
1696   fill-column:99
1697   End:
1698 */
1699 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :