Code

More NR ==> Geom conversion (points and some matrices/transforms)
[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"
58 #include "sp-title.h"
59 #include "sp-desc.h"
61 #include "libnr/nr-matrix-fns.h"
62 #include "libnr/nr-matrix-scale-ops.h"
63 #include "libnr/nr-matrix-translate-ops.h"
64 #include "libnr/nr-scale-translate-ops.h"
65 #include "libnr/nr-translate-scale-ops.h"
66 #include "libnr/nr-convert2geom.h"
67 #include "algorithms/find-last-if.h"
68 #include "util/reverse-list.h"
69 #include <2geom/rect.h>
70 #include <2geom/transforms.h>
72 #include "xml/repr.h"
73 #include "extract-uri.h"
75 #include "live_effects/lpeobject.h"
76 #include "live_effects/effect.h"
77 #include "live_effects/lpeobject-reference.h"
79 #define noSP_ITEM_DEBUG_IDLE
81 static void sp_item_class_init(SPItemClass *klass);
82 static void sp_item_init(SPItem *item);
84 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
85 static void sp_item_release(SPObject *object);
86 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
87 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
88 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
90 static gchar *sp_item_private_description(SPItem *item);
91 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
93 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
94 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
96 static SPObjectClass *parent_class;
98 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
99 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
101 /**
102  * Registers SPItem class and returns its type number.
103  */
104 GType
105 sp_item_get_type(void)
107     static GType type = 0;
108     if (!type) {
109         GTypeInfo info = {
110             sizeof(SPItemClass),
111             NULL, NULL,
112             (GClassInitFunc) sp_item_class_init,
113             NULL, NULL,
114             sizeof(SPItem),
115             16,
116             (GInstanceInitFunc) sp_item_init,
117             NULL,   /* value_table */
118         };
119         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
120     }
121     return type;
124 /**
125  * SPItem vtable initialization.
126  */
127 static void
128 sp_item_class_init(SPItemClass *klass)
130     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
132     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
134     sp_object_class->build = sp_item_build;
135     sp_object_class->release = sp_item_release;
136     sp_object_class->set = sp_item_set;
137     sp_object_class->update = sp_item_update;
138     sp_object_class->write = sp_item_write;
140     klass->description = sp_item_private_description;
141     klass->snappoints = sp_item_private_snappoints;
144 /**
145  * Callback for SPItem object initialization.
146  */
147 static void
148 sp_item_init(SPItem *item)
150     item->init();
153 void SPItem::init() {
154     this->sensitive = TRUE;
156     this->transform_center_x = 0;
157     this->transform_center_y = 0;
159     this->_is_evaluated = true;
160     this->_evaluated_status = StatusUnknown;
162     this->transform = NR::identity();
164     this->display = NULL;
166     this->clip_ref = new SPClipPathReference(this);
167                 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
168                 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
169     _clip_ref_connection = cs1.connect(sl1);
171     this->mask_ref = new SPMaskReference(this);
172                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
173                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
174     _mask_ref_connection = cs2.connect(sl2);
176     this->avoidRef = new SPAvoidRef(this);
178     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
181 bool SPItem::isVisibleAndUnlocked() const {
182     return (!isHidden() && !isLocked());
185 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
186     return (!isHidden(display_key) && !isLocked());
189 bool SPItem::isLocked() const {
190     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
191         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
192             return true;
193     }
194     return false;
197 void SPItem::setLocked(bool locked) {
198     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
199                      ( locked ? "1" : NULL ));
200     updateRepr();
203 bool SPItem::isHidden() const {
204     if (!isEvaluated())
205         return true;
206     return style->display.computed == SP_CSS_DISPLAY_NONE;
209 void SPItem::setHidden(bool hide) {
210     style->display.set = TRUE;
211     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
212     style->display.computed = style->display.value;
213     style->display.inherit = FALSE;
214     updateRepr();
217 bool SPItem::isHidden(unsigned display_key) const {
218     if (!isEvaluated())
219         return true;
220     for ( SPItemView *view(display) ; view ; view = view->next ) {
221         if ( view->key == display_key ) {
222             g_assert(view->arenaitem != NULL);
223             for ( NRArenaItem *arenaitem = view->arenaitem ;
224                   arenaitem ; arenaitem = arenaitem->parent )
225             {
226                 if (!arenaitem->visible) {
227                     return true;
228                 }
229             }
230             return false;
231         }
232     }
233     return true;
236 void SPItem::setEvaluated(bool evaluated) {
237     _is_evaluated = evaluated;
238     _evaluated_status = StatusSet;
241 void SPItem::resetEvaluated() {
242     if ( StatusCalculated == _evaluated_status ) {
243         _evaluated_status = StatusUnknown;
244         bool oldValue = _is_evaluated;
245         if ( oldValue != isEvaluated() ) {
246             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
247         }
248     } if ( StatusSet == _evaluated_status ) {
249         SPObject const *const parent = SP_OBJECT_PARENT(this);
250         if (SP_IS_SWITCH(parent)) {
251             SP_SWITCH(parent)->resetChildEvaluated();
252         }
253     }
256 bool SPItem::isEvaluated() const {
257     if ( StatusUnknown == _evaluated_status ) {
258         _is_evaluated = sp_item_evaluate(this);
259         _evaluated_status = StatusCalculated;
260     }
261     return _is_evaluated;
264 /**
265  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
266  *  Corresponds to setExplicitlyHidden.
267  */
268 bool
269 SPItem::isExplicitlyHidden() const
271     return (this->style->display.set
272             && this->style->display.value == SP_CSS_DISPLAY_NONE);
275 /**
276  * Sets the display CSS property to `hidden' if \a val is true,
277  * otherwise makes it unset
278  */
279 void
280 SPItem::setExplicitlyHidden(bool const val) {
281     this->style->display.set = val;
282     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
283     this->style->display.computed = this->style->display.value;
284     this->updateRepr();
287 /**
288  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
289  */
290 void
291 SPItem::setCenter(NR::Point object_centre) {
292     boost::optional<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
293     if (bbox) {
294         transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
295         if (fabs(transform_center_x) < 1e-5) // rounding error
296             transform_center_x = 0;
297         transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
298         if (fabs(transform_center_y) < 1e-5) // rounding error
299             transform_center_y = 0;
300     }
303 void
304 SPItem::unsetCenter() {
305     transform_center_x = 0;
306     transform_center_y = 0;
309 bool SPItem::isCenterSet() {
310     return (transform_center_x != 0 || transform_center_y != 0);
313 Geom::Point SPItem::getCenter() const {
314     boost::optional<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
315     if (bbox) {
316         return to_2geom(bbox->midpoint()) + Geom::Point (this->transform_center_x, this->transform_center_y);
317     } else {
318         return Geom::Point (0, 0); // something's wrong!
319     }
323 namespace {
325 bool is_item(SPObject const &object) {
326     return SP_IS_ITEM(&object);
331 void SPItem::raiseToTop() {
332     using Inkscape::Algorithms::find_last_if;
334     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
335         SP_OBJECT_NEXT(this), NULL, &is_item
336     );
337     if (topmost) {
338         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
339         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
340     }
343 void SPItem::raiseOne() {
344     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
345         SP_OBJECT_NEXT(this), NULL, &is_item
346     );
347     if (next_higher) {
348         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
349         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
350         sp_repr_parent(repr)->changeOrder(repr, ref);
351     }
354 void SPItem::lowerOne() {
355     using Inkscape::Util::MutableList;
356     using Inkscape::Util::reverse_list;
358     MutableList<SPObject &> next_lower=std::find_if(
359         reverse_list<SPObject::SiblingIterator>(
360             SP_OBJECT_PARENT(this)->firstChild(), this
361         ),
362         MutableList<SPObject &>(),
363         &is_item
364     );
365     if (next_lower) {
366         ++next_lower;
367         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
368         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
369         sp_repr_parent(repr)->changeOrder(repr, ref);
370     }
373 void SPItem::lowerToBottom() {
374     using Inkscape::Algorithms::find_last_if;
375     using Inkscape::Util::MutableList;
376     using Inkscape::Util::reverse_list;
378     MutableList<SPObject &> bottom=find_last_if(
379         reverse_list<SPObject::SiblingIterator>(
380             SP_OBJECT_PARENT(this)->firstChild(), this
381         ),
382         MutableList<SPObject &>(),
383         &is_item
384     );
385     if (bottom) {
386         ++bottom;
387         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
388         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
389         sp_repr_parent(repr)->changeOrder(repr, ref);
390     }
393 static void
394 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
396     sp_object_read_attr(object, "style");
397     sp_object_read_attr(object, "transform");
398     sp_object_read_attr(object, "clip-path");
399     sp_object_read_attr(object, "mask");
400     sp_object_read_attr(object, "sodipodi:insensitive");
401     sp_object_read_attr(object, "sodipodi:nonprintable");
402     sp_object_read_attr(object, "inkscape:transform-center-x");
403     sp_object_read_attr(object, "inkscape:transform-center-y");
404     sp_object_read_attr(object, "inkscape:connector-avoid");
406     if (((SPObjectClass *) (parent_class))->build) {
407         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
408     }
411 static void
412 sp_item_release(SPObject *object)
414     SPItem *item = (SPItem *) object;
416     item->_clip_ref_connection.disconnect();
417     item->_mask_ref_connection.disconnect();
419     // Note: do this here before the clip_ref is deleted, since calling
420     // sp_document_ensure_up_to_date for triggered routing may reference
421     // the deleted clip_ref.
422     if (item->avoidRef) {
423         delete item->avoidRef;
424         item->avoidRef = NULL;
425     }
427     if (item->clip_ref) {
428         item->clip_ref->detach();
429         delete item->clip_ref;
430         item->clip_ref = NULL;
431     }
433     if (item->mask_ref) {
434         item->mask_ref->detach();
435         delete item->mask_ref;
436         item->mask_ref = NULL;
437     }
439     if (((SPObjectClass *) (parent_class))->release) {
440         ((SPObjectClass *) parent_class)->release(object);
441     }
443     while (item->display) {
444         nr_arena_item_unparent(item->display->arenaitem);
445         item->display = sp_item_view_list_remove(item->display, item->display);
446     }
448     item->_transformed_signal.~signal();
451 static void
452 sp_item_set(SPObject *object, unsigned key, gchar const *value)
454     SPItem *item = (SPItem *) object;
456     switch (key) {
457         case SP_ATTR_TRANSFORM: {
458             NR::Matrix t;
459             if (value && sp_svg_transform_read(value, &t)) {
460                 sp_item_set_item_transform(item, t);
461             } else {
462                 sp_item_set_item_transform(item, NR::identity());
463             }
464             break;
465         }
466         case SP_PROP_CLIP_PATH: {
467             gchar *uri = extract_uri(value);
468             if (uri) {
469                 try {
470                     item->clip_ref->attach(Inkscape::URI(uri));
471                 } catch (Inkscape::BadURIException &e) {
472                     g_warning("%s", e.what());
473                     item->clip_ref->detach();
474                 }
475                 g_free(uri);
476             } else {
477                 item->clip_ref->detach();
478             }
480             break;
481         }
482         case SP_PROP_MASK: {
483             gchar *uri = extract_uri(value);
484             if (uri) {
485                 try {
486                     item->mask_ref->attach(Inkscape::URI(uri));
487                 } catch (Inkscape::BadURIException &e) {
488                     g_warning("%s", e.what());
489                     item->mask_ref->detach();
490                 }
491                 g_free(uri);
492             } else {
493                 item->mask_ref->detach();
494             }
496             break;
497         }
498         case SP_ATTR_SODIPODI_INSENSITIVE:
499             item->sensitive = !value;
500             for (SPItemView *v = item->display; v != NULL; v = v->next) {
501                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
502             }
503             break;
504         case SP_ATTR_CONNECTOR_AVOID:
505             item->avoidRef->setAvoid(value);
506             break;
507         case SP_ATTR_TRANSFORM_CENTER_X:
508             if (value) {
509                 item->transform_center_x = g_strtod(value, NULL);
510             } else {
511                 item->transform_center_x = 0;
512             }
513             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
514             break;
515         case SP_ATTR_TRANSFORM_CENTER_Y:
516             if (value) {
517                 item->transform_center_y = g_strtod(value, NULL);
518             } else {
519                 item->transform_center_y = 0;
520             }
521             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
522             break;
523         case SP_PROP_SYSTEM_LANGUAGE:
524         case SP_PROP_REQUIRED_FEATURES:
525         case SP_PROP_REQUIRED_EXTENSIONS:
526             {
527                 item->resetEvaluated();
528                 // pass to default handler
529             }
530         default:
531             if (SP_ATTRIBUTE_IS_CSS(key)) {
532                 sp_style_read_from_object(object->style, object);
533                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
534             } else {
535                 if (((SPObjectClass *) (parent_class))->set) {
536                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
537                 }
538             }
539             break;
540     }
543 static void
544 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
546     if (old_clip) {
547         SPItemView *v;
548         /* Hide clippath */
549         for (v = item->display; v != NULL; v = v->next) {
550             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
551             nr_arena_item_set_clip(v->arenaitem, NULL);
552         }
553     }
554     if (SP_IS_CLIPPATH(clip)) {
555         NRRect bbox;
556         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
557         for (SPItemView *v = item->display; v != NULL; v = v->next) {
558             if (!v->arenaitem->key) {
559                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
560             }
561             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
562                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
563                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
564             nr_arena_item_set_clip(v->arenaitem, ai);
565             nr_arena_item_unref(ai);
566             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
567             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
568         }
569     }
572 static void
573 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
575     if (old_mask) {
576         /* Hide mask */
577         for (SPItemView *v = item->display; v != NULL; v = v->next) {
578             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
579             nr_arena_item_set_mask(v->arenaitem, NULL);
580         }
581     }
582     if (SP_IS_MASK(mask)) {
583         NRRect bbox;
584         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
585         for (SPItemView *v = item->display; v != NULL; v = v->next) {
586             if (!v->arenaitem->key) {
587                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
588             }
589             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
590                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
591                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
592             nr_arena_item_set_mask(v->arenaitem, ai);
593             nr_arena_item_unref(ai);
594             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
595             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
596         }
597     }
600 static void
601 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
603     SPItem *item = SP_ITEM(object);
605     if (((SPObjectClass *) (parent_class))->update)
606         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
608     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
609         if (flags & SP_OBJECT_MODIFIED_FLAG) {
610             for (SPItemView *v = item->display; v != NULL; v = v->next) {
611                 nr_arena_item_set_transform(v->arenaitem, item->transform);
612             }
613         }
615         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
616         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
618         if ( clip_path || mask ) {
619             NRRect bbox;
620             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
621             if (clip_path) {
622                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
623                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
624                 }
625             }
626             if (mask) {
627                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
628                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
629                 }
630             }
631         }
633         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
634             for (SPItemView *v = item->display; v != NULL; v = v->next) {
635                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
636                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
637             }
638         }
639     }
641     /* Update bounding box data used by filters */
642     if (item->style->filter.set && item->display) {
643         NRRect item_bbox;
644         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
645         boost::optional<NR::Rect> i_bbox = item_bbox;
647         SPItemView *itemview = item->display;
648         do {
649             if (itemview->arenaitem)
650                 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
651         } while ( (itemview = itemview->next) );
652     }
654     // Update libavoid with item geometry (for connector routing).
655     if (item->avoidRef)
656         item->avoidRef->handleSettingChange();
659 static Inkscape::XML::Node *
660 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
662     SPObject *child;
663     SPItem *item = SP_ITEM(object);
665     // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created, 
666     // so we need to add any children from the underlying object to the new repr
667     if (flags & SP_OBJECT_WRITE_BUILD) {
668         Inkscape::XML::Node *crepr;
669         GSList *l;
670         l = NULL;
671         for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
672             if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
673             crepr = child->updateRepr(xml_doc, NULL, flags);
674             if (crepr) l = g_slist_prepend (l, crepr);
675         }
676         while (l) {
677             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
678             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
679             l = g_slist_remove (l, l->data);
680         }
681     } else {
682         for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
683             if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
684             child->updateRepr(flags);
685         }
686     }
688     gchar *c = sp_svg_transform_write(item->transform);
689     repr->setAttribute("transform", c);
690     g_free(c);
692     if (flags & SP_OBJECT_WRITE_EXT) {
693         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
694         if (item->transform_center_x != 0)
695             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
696         else
697             repr->setAttribute ("inkscape:transform-center-x", NULL);
698         if (item->transform_center_y != 0)
699             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
700         else
701             repr->setAttribute ("inkscape:transform-center-y", NULL);
702     }
704     if (item->clip_ref->getObject()) {
705         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
706         repr->setAttribute ("clip-path", value);
707         g_free ((void *) value);
708     }
709     if (item->mask_ref->getObject()) {
710         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
711         repr->setAttribute ("mask", value);
712         g_free ((void *) value);
713     }
715     if (((SPObjectClass *) (parent_class))->write) {
716         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
717     }
719     return repr;
722 boost::optional<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
723                                       SPItem::BBoxType type,
724                                       unsigned int /*dkey*/) const
726     boost::optional<NR::Rect> r;
727     sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
728     return r;
731 void
732 sp_item_invoke_bbox(SPItem const *item, boost::optional<NR::Rect> &bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
734     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
737 // DEPRECATED to phase out the use of NRRect in favor of boost::optional<NR::Rect>
738 void
739 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
741     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
744 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
745  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
746  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
747  * clones), in turn, call this function in their bbox methods. */
748 void
749 sp_item_invoke_bbox_full(SPItem const *item, boost::optional<NR::Rect> &bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
751     g_assert(item != NULL);
752     g_assert(SP_IS_ITEM(item));
754     if (clear) {
755         bbox = boost::optional<NR::Rect>();
756     }
758     // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
760     NRRect temp_bbox;
761     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
762     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
764     // call the subclass method
765     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
766         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
767     }
769     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
770     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
771         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
772             SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
773             if (filter && SP_IS_FILTER(filter)) {
774                 // default filer area per the SVG spec:
775                 double x = -0.1;
776                 double y = -0.1;
777                 double w = 1.2;
778                 double h = 1.2;
780                 // if area is explicitly set, override:
781                 if (SP_FILTER(filter)->x._set)
782                     x = SP_FILTER(filter)->x.computed;
783                 if (SP_FILTER(filter)->y._set)
784                     y = SP_FILTER(filter)->y.computed;
785                 if (SP_FILTER(filter)->width._set)
786                     w = SP_FILTER(filter)->width.computed;
787                 if (SP_FILTER(filter)->height._set) 
788                     h = SP_FILTER(filter)->height.computed;
790                 double dx0 = 0;
791                 double dx1 = 0;
792                 double dy0 = 0;
793                 double dy1 = 0;
794                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
795                     // if this is a single blur, use 2.4*radius 
796                     // which may be smaller than the default area;
797                     // see set_filter_area for why it's 2.4
798                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
799                     dx0 = -2.4 * r;
800                     dx1 = 2.4 * r;
801                     dy0 = -2.4 * r;
802                     dy1 = 2.4 * r;
803                 } else {
804                     // otherwise, calculate expansion from relative to absolute units:
805                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
806                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
807                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
808                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
809                 }
811                 // transform the expansions by the item's transform:
812                 NR::Matrix i2d(sp_item_i2d_affine (item));
813                 dx0 *= NR::expansionX(i2d);
814                 dx1 *= NR::expansionX(i2d);
815                 dy0 *= NR::expansionY(i2d);
816                 dy1 *= NR::expansionY(i2d);
818                 // expand the bbox
819                 temp_bbox.x0 += dx0;
820                 temp_bbox.x1 += dx1;
821                 temp_bbox.y0 += dy0;
822                 temp_bbox.y1 += dy1;
823             }
824         }
825         if (item->clip_ref->getObject()) {
826             NRRect b;
827             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
828             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
829         }
830     }
832     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
833         // Either the bbox hasn't been touched by the SPItemClass' bbox method 
834         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
835         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
836         
837         // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been 
838         // explicitely defined this way for NRRects (as opposed to boost::optional<NR::Rect>)
839         // So union bbox with nothing = do nothing, just return
840         return;
841     }
843     // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty boost::optional<NR::Rect>()
844     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
845     // would therefore be translated into empty boost::optional<NR::Rect>() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
846     boost::optional<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
848     bbox = NR::union_bounds(bbox, temp_bbox_new);
851 // DEPRECATED to phase out the use of NRRect in favor of boost::optional<NR::Rect>
852 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
853  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
854  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
855  * clones), in turn, call this function in their bbox methods. */
856 void
857 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
859     g_assert(item != NULL);
860     g_assert(SP_IS_ITEM(item));
861     g_assert(bbox != NULL);
863     if (clear) {
864         bbox->x0 = bbox->y0 = 1e18;
865         bbox->x1 = bbox->y1 = -1e18;
866     }
868     NRRect this_bbox;
869     this_bbox.x0 = this_bbox.y0 = 1e18;
870     this_bbox.x1 = this_bbox.y1 = -1e18;
872     // call the subclass method
873     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
874         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
875     }
877     // unless this is geometric bbox, crop the bbox by clip path, if any
878     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
879         NRRect b;
880         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
881         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
882     }
884     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
885     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
886         nr_rect_d_union (bbox, bbox, &this_bbox);
887     }
890 unsigned sp_item_pos_in_parent(SPItem *item)
892     g_assert(item != NULL);
893     g_assert(SP_IS_ITEM(item));
895     SPObject *parent = SP_OBJECT_PARENT(item);
896     g_assert(parent != NULL);
897     g_assert(SP_IS_OBJECT(parent));
899     SPObject *object = SP_OBJECT(item);
901     unsigned pos=0;
902     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
903         if ( iter == object ) {
904             return pos;
905         }
906         if (SP_IS_ITEM(iter)) {
907             pos++;
908         }
909     }
911     g_assert_not_reached();
912     return 0;
915 void
916 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
918     g_assert(item != NULL);
919     g_assert(SP_IS_ITEM(item));
920     g_assert(bbox != NULL);
922     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
925 boost::optional<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
927     boost::optional<NR::Rect> rect = boost::optional<NR::Rect>();
928     sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
929     return rect;
932 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
934         /* This will only be called if the derived class doesn't override this.
935          * see for example sp_genericellipse_snappoints in sp-ellipse.cpp        
936          * We don't know what shape we could be dealing with here, so we'll just
937          * return the corners of the bounding box */
939         boost::optional<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
940     
941     if (bbox) {
942         NR::Point p1, p2;
943         p1 = bbox->min();
944         p2 = bbox->max();
945         *p = p1;
946         *p = NR::Point(p1[NR::X], p2[NR::Y]);
947         *p = p2;
948         *p = NR::Point(p1[NR::Y], p2[NR::X]);
949     }
950       
953 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
955         g_assert (item != NULL);
956     g_assert (SP_IS_ITEM(item));
958     // Get the snappoints of the item
959     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
960     if (item_class.snappoints) {
961         item_class.snappoints(item, p);
962     }
964     // Get the snappoints at the item's center
965     if (includeItemCenter) {
966         *p = item->getCenter();
967     }    
968     
969     // Get the snappoints of clipping paths and mask, if any
970     std::list<SPObject const *> clips_and_masks;
971     
972     clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
973     clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
974     
975     for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
976         if (*o) {
977                 // obj is a group object, the children are the actual clippers
978                 for (SPObject *child = (*o)->children ; child ; child = child->next) {
979                     if (SP_IS_ITEM(child)) {
980                         std::vector<NR::Point> p_clip_or_mask;                      
981                         // Please note the recursive call here!
982                         sp_item_snappoints(SP_ITEM(child), includeItemCenter, SnapPointsIter(p_clip_or_mask));
983                         // Take into account the transformation of the item being clipped or masked
984                         for (std::vector<NR::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
985                         // All snappoints are in desktop coordinates, but the item's transformation is
986                                 // in document coordinates. Hence the awkward construction below
987                                 *p = (*p_orig) * from_2geom(matrix_to_desktop (matrix_from_desktop (item->transform, item), item));
988                     }
989                     }
990                 }
991         }
992     }
995 void
996 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
998     if (!item->isHidden()) {
999         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
1000             if (!item->transform.test_identity()
1001                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
1002             {
1003                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1004                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1005                 sp_print_release(ctx);
1006             } else {
1007                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1008             }
1009         }
1010     }
1013 static gchar *
1014 sp_item_private_description(SPItem */*item*/)
1016     return g_strdup(_("Object"));
1019 /**
1020  * Returns a string suitable for status bar, formatted in pango markup language.
1021  *
1022  * Must be freed by caller.
1023  */
1024 gchar *
1025 sp_item_description(SPItem *item)
1027     g_assert(item != NULL);
1028     g_assert(SP_IS_ITEM(item));
1030     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1031         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1032         if (s && item->clip_ref->getObject()) {
1033             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1034             g_free (s);
1035             s = snew;
1036         }
1037         if (s && item->mask_ref->getObject()) {
1038             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1039             g_free (s);
1040             s = snew;
1041         }
1042         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
1043             gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1044             g_free (s);
1045             s = snew;
1046         }
1047         return s;
1048     }
1050     g_assert_not_reached();
1051     return NULL;
1054 /**
1055  * Allocates unique integer keys.
1056  * \param numkeys Number of keys required.
1057  * \return First allocated key; hence if the returned key is n
1058  * you can use n, n + 1, ..., n + (numkeys - 1)
1059  */
1060 unsigned
1061 sp_item_display_key_new(unsigned numkeys)
1063     static unsigned dkey = 0;
1065     dkey += numkeys;
1067     return dkey - numkeys;
1070 NRArenaItem *
1071 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1073     g_assert(item != NULL);
1074     g_assert(SP_IS_ITEM(item));
1075     g_assert(arena != NULL);
1076     g_assert(NR_IS_ARENA(arena));
1078     NRArenaItem *ai = NULL;
1079     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1080         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1081     }
1083     if (ai != NULL) {
1084         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1085         nr_arena_item_set_transform(ai, item->transform);
1086         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1087         nr_arena_item_set_visible(ai, !item->isHidden());
1088         nr_arena_item_set_sensitive(ai, item->sensitive);
1089         if (item->clip_ref->getObject()) {
1090             SPClipPath *cp = item->clip_ref->getObject();
1092             if (!item->display->arenaitem->key) {
1093                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1094             }
1095             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1097             // Show and set clip
1098             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1099             nr_arena_item_set_clip(ai, ac);
1100             nr_arena_item_unref(ac);
1102             // Update bbox, in case the clip uses bbox units
1103             NRRect bbox;
1104             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1105             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1106             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1107         }
1108         if (item->mask_ref->getObject()) {
1109             SPMask *mask = item->mask_ref->getObject();
1111             if (!item->display->arenaitem->key) {
1112                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1113             }
1114             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1116             // Show and set mask
1117             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1118             nr_arena_item_set_mask(ai, ac);
1119             nr_arena_item_unref(ac);
1121             // Update bbox, in case the mask uses bbox units
1122             NRRect bbox;
1123             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1124             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1125             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1126         }
1127         NR_ARENA_ITEM_SET_DATA(ai, item);
1128         NRRect item_bbox;
1129         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1130         boost::optional<NR::Rect> i_bbox = item_bbox;
1131         nr_arena_item_set_item_bbox(ai, i_bbox);
1132     }
1134     return ai;
1137 void
1138 sp_item_invoke_hide(SPItem *item, unsigned key)
1140     g_assert(item != NULL);
1141     g_assert(SP_IS_ITEM(item));
1143     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1144         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1145     }
1147     SPItemView *ref = NULL;
1148     SPItemView *v = item->display;
1149     while (v != NULL) {
1150         SPItemView *next = v->next;
1151         if (v->key == key) {
1152             if (item->clip_ref->getObject()) {
1153                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1154                 nr_arena_item_set_clip(v->arenaitem, NULL);
1155             }
1156             if (item->mask_ref->getObject()) {
1157                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1158                 nr_arena_item_set_mask(v->arenaitem, NULL);
1159             }
1160             if (!ref) {
1161                 item->display = v->next;
1162             } else {
1163                 ref->next = v->next;
1164             }
1165             nr_arena_item_unparent(v->arenaitem);
1166             nr_arena_item_unref(v->arenaitem);
1167             g_free(v);
1168         } else {
1169             ref = v;
1170         }
1171         v = next;
1172     }
1175 // Adjusters
1177 void
1178 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1180     SPStyle *style = SP_OBJECT_STYLE (item);
1182     if (style && (style->fill.isPaintserver())) {
1183         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1184         if (SP_IS_PATTERN (server)) {
1185             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1186             sp_pattern_transform_multiply (pattern, postmul, set);
1187         }
1188     }
1190     if (style && (style->stroke.isPaintserver())) {
1191         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1192         if (SP_IS_PATTERN (server)) {
1193             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1194             sp_pattern_transform_multiply (pattern, postmul, set);
1195         }
1196     }
1200 void
1201 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1203     SPStyle *style = SP_OBJECT_STYLE (item);
1205     if (style && (style->fill.isPaintserver())) {
1206         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1207         if (SP_IS_GRADIENT (server)) {
1209             /**
1210              * \note Bbox units for a gradient are generally a bad idea because
1211              * with them, you cannot preserve the relative position of the
1212              * object and its gradient after rotation or skew. So now we
1213              * convert them to userspace units which are easy to keep in sync
1214              * just by adding the object's transform to gradientTransform.
1215              * \todo FIXME: convert back to bbox units after transforming with
1216              * the item, so as to preserve the original units.
1217              */
1218             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1220             sp_gradient_transform_multiply (gradient, postmul, set);
1221         }
1222     }
1224     if (style && (style->stroke.isPaintserver())) {
1225         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1226         if (SP_IS_GRADIENT (server)) {
1227             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1228             sp_gradient_transform_multiply (gradient, postmul, set);
1229         }
1230     }
1233 void
1234 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1236     SPStyle *style = SP_OBJECT_STYLE (item);
1238     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1240         style->stroke_width.computed *= ex;
1241         style->stroke_width.set = TRUE;
1243         if (style->stroke_dash.n_dash != 0) {
1244             int i;
1245             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1246                 style->stroke_dash.dash[i] *= ex;
1247             }
1248             style->stroke_dash.offset *= ex;
1249         }
1251         SP_OBJECT(item)->updateRepr();
1252     }
1255 /**
1256  * Find out the inverse of previous transform of an item (from its repr)
1257  */
1258 NR::Matrix
1259 sp_item_transform_repr (SPItem *item)
1261     NR::Matrix t_old(NR::identity());
1262     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1263     if (t_attr) {
1264         NR::Matrix t;
1265         if (sp_svg_transform_read(t_attr, &t)) {
1266             t_old = t;
1267         }
1268     }
1270     return t_old;
1274 /**
1275  * Recursively scale stroke width in \a item and its children by \a expansion.
1276  */
1277 void
1278 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1280     sp_item_adjust_stroke (item, expansion);
1282 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1283     if (item && SP_IS_USE(item))
1284         return;
1286     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1287         if (SP_IS_ITEM(o))
1288             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1289     }
1292 /**
1293  * Recursively adjust rx and ry of rects.
1294  */
1295 void
1296 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1298     if (SP_IS_RECT (item)) {
1299         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1300     }
1302     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1303         if (SP_IS_ITEM(o))
1304             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1305     }
1308 /**
1309  * Recursively compensate pattern or gradient transform.
1310  */
1311 void
1312 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1314 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1315 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1316 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1317     NR::Matrix t_item = sp_item_transform_repr (item);
1318     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1320 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1321 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1322 // we must not touch it
1323     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1324         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1325             if (SP_IS_ITEM(o)) {
1326 // At the level of the transformed item, t_ancestors is identity;
1327 // below it, it is the accmmulated chain of transforms from this level to the top level
1328                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1329             }
1330         }
1331     }
1333 // We recursed into children first, and are now adjusting this object second;
1334 // this is so that adjustments in a tree are done from leaves up to the root,
1335 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1336 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1338     if (is_pattern)
1339         sp_item_adjust_pattern (item, paint_delta);
1340     else
1341         sp_item_adjust_gradient (item, paint_delta);
1345 void
1346 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1348     if ( !SP_IS_LPE_ITEM(item) )
1349         return;
1351     SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1352     if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1353         PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
1354         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1355         {
1356             // If the path effect is used by 2 or more items, fork it
1357             // so that each object has its own independent copy of the effect
1358             LivePathEffectObject *lpeobj = (*it)->lpeobject;
1359             if (lpeobj) {
1360                 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1361                 if (new_lpeobj != lpeobj) {
1362                     sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1363                 }
1364         
1365                 if (lpeobj->lpe) {
1366                     Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
1367                     effect->transform_multiply(postmul, set);
1368                 }
1369             }
1370         }
1371     }
1374 /**
1375  * A temporary wrapper for the next function accepting the NR::Matrix
1376  * instead of NR::Matrix
1377  */
1378 void
1379 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1381     if (transform == NULL)
1382         sp_item_write_transform(item, repr, NR::identity(), adv);
1383     else
1384         sp_item_write_transform(item, repr, *transform, adv);
1387 /**
1388  * Set a new transform on an object.
1389  *
1390  * Compensate for stroke scaling and gradient/pattern fill transform, if
1391  * necessary. Call the object's set_transform method if transforms are
1392  * stored optimized. Send _transformed_signal. Invoke _write method so that
1393  * the repr is updated with the new transform.
1394  */
1395 void
1396 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1398     g_return_if_fail(item != NULL);
1399     g_return_if_fail(SP_IS_ITEM(item));
1400     g_return_if_fail(repr != NULL);
1402     // calculate the relative transform, if not given by the adv attribute
1403     NR::Matrix advertized_transform;
1404     if (adv != NULL) {
1405         advertized_transform = *adv;
1406     } else {
1407         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1408     }
1410     if (compensate) {
1412          // recursively compensate for stroke scaling, depending on user preference
1413         if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1414             double const expansion = 1. / NR::expansion(advertized_transform);
1415             sp_item_adjust_stroke_width_recursive(item, expansion);
1416         }
1418         // recursively compensate rx/ry of a rect if requested
1419         if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1420             sp_item_adjust_rects_recursive(item, advertized_transform);
1421         }
1423         // recursively compensate pattern fill if it's not to be transformed
1424         if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1425             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1426         }
1427         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1428         /// recursively compensate gradient fill if it's not to be transformed
1429         if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1430             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1431         } else {
1432             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1433             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1434             sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1435         }
1437     } // endif(compensate)
1439     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1440     NR::Matrix transform_attr (transform);
1441     if ( // run the object's set_transform (i.e. embed transform) only if:
1442          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1443              !preserve && // user did not chose to preserve all transforms
1444              !item->clip_ref->getObject() && // the object does not have a clippath
1445              !item->mask_ref->getObject() && // the object does not have a mask
1446          !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1447              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1448         ) {
1449         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1450     }
1451     sp_item_set_item_transform(item, transform_attr);
1453     // Note: updateRepr comes before emitting the transformed signal since
1454     // it causes clone SPUse's copy of the original object to brought up to
1455     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1456     // values if called in code handling the transformed signal.
1457     SP_OBJECT(item)->updateRepr();
1459     // send the relative transform with a _transformed_signal
1460     item->_transformed_signal.emit(&advertized_transform, item);
1463 gint
1464 sp_item_event(SPItem *item, SPEvent *event)
1466     g_return_val_if_fail(item != NULL, FALSE);
1467     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1468     g_return_val_if_fail(event != NULL, FALSE);
1470     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1471         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1473     return FALSE;
1476 /**
1477  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1478  * gradients, patterns as sp_item_write_transform does.
1479  */
1480 void
1481 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1483     g_return_if_fail(item != NULL);
1484     g_return_if_fail(SP_IS_ITEM(item));
1486     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1487         item->transform = transform;
1488         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1489            transformation.  It's apparently not used anywhere else. */
1490         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1491         sp_item_rm_unsatisfied_cns(*item);
1492     }
1495 void
1496 sp_item_convert_item_to_guides(SPItem *item) {
1497     g_return_if_fail(item != NULL);
1498     g_return_if_fail(SP_IS_ITEM(item));
1500     /* Use derived method if present ... */
1501     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1502         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1503         return;
1504     }
1506     /* .. otherwise simply place the guides around the item's bounding box */
1508     sp_item_convert_to_guides(item);
1512 /**
1513  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1514  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1515  */
1516 Geom::Matrix
1517 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1518     Geom::Matrix ret(Geom::identity());
1519     g_return_val_if_fail(object != NULL, ret);
1521     /* stop at first non-renderable ancestor */
1522     while ( object != ancestor && SP_IS_ITEM(object) ) {
1523         if (SP_IS_ROOT(object)) {
1524             ret *= SP_ROOT(object)->c2p;
1525         }
1526         ret *= SP_ITEM(object)->transform;
1527         object = SP_OBJECT_PARENT(object);
1528     }
1529     return ret;
1532 Geom::Matrix
1533 i2i_affine(SPObject const *src, SPObject const *dest) {
1534     g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1535     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1536     return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1539 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1540     return i2i_affine(this, dest);
1543 /**
1544  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1545  * \pre (item != NULL) and SP_IS_ITEM(item).
1546  */
1547 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1549     return i2anc_affine(item, NULL);
1552 /**
1553  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1554  * Used in path operations mostly.
1555  * \pre (item != NULL) and SP_IS_ITEM(item).
1556  */
1557 Geom::Matrix sp_item_i2root_affine(SPItem const *item)
1559     g_assert(item != NULL);
1560     g_assert(SP_IS_ITEM(item));
1562     Geom::Matrix ret(Geom::identity());
1563     g_assert(ret.isIdentity());
1564     while ( NULL != SP_OBJECT_PARENT(item) ) {
1565         ret *= item->transform;
1566         item = SP_ITEM(SP_OBJECT_PARENT(item));
1567     }
1568     g_assert(SP_IS_ROOT(item));
1570     ret *= item->transform;
1572     return ret;
1575 /* fixme: This is EVIL!!! */
1576 // fix this note: why/what evil? :)
1577 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1579     g_assert(item != NULL);
1580     g_assert(SP_IS_ITEM(item));
1582     Geom::Matrix const ret( sp_item_i2doc_affine(item)
1583                           * Geom::Scale(1, -1)
1584                           * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1585     return ret;
1588 // same as i2d but with i2root instead of i2doc
1589 Geom::Matrix sp_item_i2r_affine(SPItem const *item)
1591     g_assert(item != NULL);
1592     g_assert(SP_IS_ITEM(item));
1594     Geom::Matrix const ret( sp_item_i2root_affine(item)
1595                           * Geom::Scale(1, -1)
1596                           * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1597     return ret;
1600 /**
1601  * Converts a matrix \a m into the desktop coords of the \a item.
1602  * Will become a noop when we eliminate the coordinate flipping.
1603  */
1604 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1606     Geom::Matrix const ret(m
1607                          * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1608                          * Geom::Scale(1, -1));
1609     return ret;
1612 /**
1613  * Converts a matrix \a m from the desktop coords of the \a item.
1614  * Will become a noop when we eliminate the coordinate flipping.
1615  */
1616 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1618     Geom::Matrix const ret(Geom::Scale(1, -1)
1619                          * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1620                          * m);
1621     return ret;
1624 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1626     g_return_if_fail( item != NULL );
1627     g_return_if_fail( SP_IS_ITEM(item) );
1629     Geom::Matrix dt2p; /* desktop to item parent transform */
1630     if (SP_OBJECT_PARENT(item)) {
1631         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1632     } else {
1633         dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1634                  * Geom::Scale(1, -1) );
1635     }
1637     Geom::Matrix const i2p( i2dt * dt2p );
1638     sp_item_set_item_transform(item, i2p);
1642 Geom::Matrix
1643 sp_item_dt2i_affine(SPItem const *item)
1645     /* fixme: Implement the right way (Lauris) */
1646     return sp_item_i2d_affine(item).inverse();
1649 /* Item views */
1651 static SPItemView *
1652 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1654     SPItemView *new_view;
1656     g_assert(item != NULL);
1657     g_assert(SP_IS_ITEM(item));
1658     g_assert(arenaitem != NULL);
1659     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1661     new_view = g_new(SPItemView, 1);
1663     new_view->next = list;
1664     new_view->flags = flags;
1665     new_view->key = key;
1666     new_view->arenaitem = arenaitem;
1668     return new_view;
1671 static SPItemView *
1672 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1674     if (view == list) {
1675         list = list->next;
1676     } else {
1677         SPItemView *prev;
1678         prev = list;
1679         while (prev->next != view) prev = prev->next;
1680         prev->next = view->next;
1681     }
1683     nr_arena_item_unref(view->arenaitem);
1684     g_free(view);
1686     return list;
1689 /**
1690  * Return the arenaitem corresponding to the given item in the display
1691  * with the given key
1692  */
1693 NRArenaItem *
1694 sp_item_get_arenaitem(SPItem *item, unsigned key)
1696     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1697         if ( iv->key == key ) {
1698             return iv->arenaitem;
1699         }
1700     }
1702     return NULL;
1705 int
1706 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1708     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1709                                     SP_OBJECT_REPR(second));
1712 SPItem *
1713 sp_item_first_item_child (SPObject *obj)
1715     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1716         if (SP_IS_ITEM (iter))
1717             return SP_ITEM (iter);
1718     }
1719     return NULL;
1722 void
1723 sp_item_convert_to_guides(SPItem *item) {
1724     SPDesktop *dt = inkscape_active_desktop();
1725     SPNamedView *nv = sp_desktop_namedview(dt);
1726     (void)nv;
1728     int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
1729     SPItem::BBoxType bbox_type = (prefs_bbox ==0)? 
1730         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1732     boost::optional<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1733     if (!bbox) {
1734         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1735         return;
1736     }
1738     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1740     NR::Point A((*bbox).min());
1741     NR::Point C((*bbox).max());
1742     NR::Point B(A[NR::X], C[NR::Y]);
1743     NR::Point D(C[NR::X], A[NR::Y]);
1745     pts.push_back(std::make_pair(A, B));
1746     pts.push_back(std::make_pair(B, C));
1747     pts.push_back(std::make_pair(C, D));
1748     pts.push_back(std::make_pair(D, A));
1750     sp_guide_pt_pairs_to_guides(dt, pts);
1753 /*
1754   Local Variables:
1755   mode:c++
1756   c-file-style:"stroustrup"
1757   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1758   indent-tabs-mode:nil
1759   fill-column:99
1760   End:
1761 */
1762 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :