Code

* remove sp_item_i2root_affine and sp_item_i2r_affine and replace all calls by
[inkscape.git] / src / sp-item.cpp
1 /** \file
2  * Base class for visual SVG elements
3  */
4 /*
5  * Authors:
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9  *
10  * Copyright (C) 2001-2006 authors
11  * Copyright (C) 2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 /** \class SPItem
17  *
18  * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
19  * is a subclass of SPObject, with great deal of specific functionality.
20  */
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
27 #include "sp-item.h"
28 #include "svg/svg.h"
29 #include "print.h"
30 #include "display/nr-arena.h"
31 #include "display/nr-arena-item.h"
32 #include "attributes.h"
33 #include "document.h"
34 #include "uri.h"
35 #include "inkscape.h"
36 #include "desktop-handles.h"
38 #include "style.h"
39 #include <glibmm/i18n.h>
40 #include "sp-root.h"
41 #include "sp-clippath.h"
42 #include "sp-mask.h"
43 #include "sp-rect.h"
44 #include "sp-use.h"
45 #include "sp-text.h"
46 #include "sp-item-rm-unsatisfied-cns.h"
47 #include "sp-pattern.h"
48 #include "sp-switch.h"
49 #include "gradient-chemistry.h"
50 #include "preferences.h"
51 #include "conn-avoid-ref.h"
52 #include "conditions.h"
53 #include "sp-filter-reference.h"
54 #include "filter-chemistry.h"
55 #include "sp-guide.h"
56 #include "sp-title.h"
57 #include "sp-desc.h"
59 #include "libnr/nr-matrix-fns.h"
60 #include "libnr/nr-matrix-scale-ops.h"
61 #include "libnr/nr-matrix-translate-ops.h"
62 #include "libnr/nr-scale-translate-ops.h"
63 #include "libnr/nr-translate-scale-ops.h"
64 #include "libnr/nr-convert2geom.h"
65 #include "algorithms/find-last-if.h"
66 #include "util/reverse-list.h"
67 #include <2geom/rect.h>
68 #include <2geom/matrix.h>
69 #include <2geom/transforms.h>
71 #include "xml/repr.h"
72 #include "extract-uri.h"
73 #include "helper/geom.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, Inkscape::SnapPreferences const *snapprefs);
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 = Geom::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, Geom::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(Geom::Point object_centre) {
292     Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
293     if (bbox) {
294         transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
295         if (fabs(transform_center_x) < 1e-5) // rounding error
296             transform_center_x = 0;
297         transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::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     Geom::OptRect 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             Geom::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, Geom::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, Geom::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, Geom::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, Geom::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         Geom::OptRect item_bbox;
644         sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
646         SPItemView *itemview = item->display;
647         do {
648             if (itemview->arenaitem)
649                 nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
650         } while ( (itemview = itemview->next) );
651     }
653     // Update libavoid with item geometry (for connector routing).
654     if (item->avoidRef)
655         item->avoidRef->handleSettingChange();
658 static Inkscape::XML::Node *
659 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
661     SPObject *child;
662     SPItem *item = SP_ITEM(object);
664     // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
665     // so we need to add any children from the underlying object to the new repr
666     if (flags & SP_OBJECT_WRITE_BUILD) {
667         Inkscape::XML::Node *crepr;
668         GSList *l;
669         l = NULL;
670         for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
671             if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
672             crepr = child->updateRepr(xml_doc, NULL, flags);
673             if (crepr) l = g_slist_prepend (l, crepr);
674         }
675         while (l) {
676             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
677             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
678             l = g_slist_remove (l, l->data);
679         }
680     } else {
681         for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
682             if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
683             child->updateRepr(flags);
684         }
685     }
687     gchar *c = sp_svg_transform_write(item->transform);
688     repr->setAttribute("transform", c);
689     g_free(c);
691     if (flags & SP_OBJECT_WRITE_EXT) {
692         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
693         if (item->transform_center_x != 0)
694             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
695         else
696             repr->setAttribute ("inkscape:transform-center-x", NULL);
697         if (item->transform_center_y != 0)
698             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
699         else
700             repr->setAttribute ("inkscape:transform-center-y", NULL);
701     }
703     if (item->clip_ref->getObject()) {
704         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
705         repr->setAttribute ("clip-path", value);
706         g_free ((void *) value);
707     }
708     if (item->mask_ref->getObject()) {
709         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
710         repr->setAttribute ("mask", value);
711         g_free ((void *) value);
712     }
714     if (((SPObjectClass *) (parent_class))->write) {
715         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
716     }
718     return repr;
721 /**
722  * \return  There is no guarantee that the return value will contain a rectangle.
723             If this item does not have a boundingbox, it might well be empty.
724  */
725 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
726                                       SPItem::BBoxType type,
727                                       unsigned int /*dkey*/) const
729     Geom::OptRect r;
730     sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
731     return r;
734 void
735 sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
737     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
740 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
741 void
742 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
744     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
747 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
748  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
749  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
750  * clones), in turn, call this function in their bbox methods.
751  * \retval bbox  Note that there is no guarantee that bbox will contain a rectangle when the
752  *               function returns. If this item does not have a boundingbox, this might well be empty.
753  */
754 void
755 sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
757     g_assert(item != NULL);
758     g_assert(SP_IS_ITEM(item));
760     if (clear) {
761         bbox = Geom::OptRect();
762     }
764     // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
766     NRRect temp_bbox;
767     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
768     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
770     // call the subclass method
771     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
772         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
773     }
775     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
776     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
777         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
778             SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
779             if (filter && SP_IS_FILTER(filter)) {
780                 // default filer area per the SVG spec:
781                 double x = -0.1;
782                 double y = -0.1;
783                 double w = 1.2;
784                 double h = 1.2;
786                 // if area is explicitly set, override:
787                 if (SP_FILTER(filter)->x._set)
788                     x = SP_FILTER(filter)->x.computed;
789                 if (SP_FILTER(filter)->y._set)
790                     y = SP_FILTER(filter)->y.computed;
791                 if (SP_FILTER(filter)->width._set)
792                     w = SP_FILTER(filter)->width.computed;
793                 if (SP_FILTER(filter)->height._set)
794                     h = SP_FILTER(filter)->height.computed;
796                 double dx0 = 0;
797                 double dx1 = 0;
798                 double dy0 = 0;
799                 double dy1 = 0;
800                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
801                     // if this is a single blur, use 2.4*radius
802                     // which may be smaller than the default area;
803                     // see set_filter_area for why it's 2.4
804                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
805                     dx0 = -2.4 * r;
806                     dx1 = 2.4 * r;
807                     dy0 = -2.4 * r;
808                     dy1 = 2.4 * r;
809                 } else {
810                     // otherwise, calculate expansion from relative to absolute units:
811                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
812                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
813                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
814                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
815                 }
817                 // transform the expansions by the item's transform:
818                 Geom::Matrix i2d(sp_item_i2d_affine (item));
819                 dx0 *= i2d.expansionX();
820                 dx1 *= i2d.expansionX();
821                 dy0 *= i2d.expansionY();
822                 dy1 *= i2d.expansionY();
824                 // expand the bbox
825                 temp_bbox.x0 += dx0;
826                 temp_bbox.x1 += dx1;
827                 temp_bbox.y0 += dy0;
828                 temp_bbox.y1 += dy1;
829             }
830         }
831         if (item->clip_ref->getObject()) {
832             NRRect b;
833             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
834             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
835         }
836     }
838     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
839         // Either the bbox hasn't been touched by the SPItemClass' bbox method
840         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
841         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
843         // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
844         // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
845         // So union bbox with nothing = do nothing, just return
846         return;
847     }
849     // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
850     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
851     // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
852     Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
854     bbox = Geom::unify(bbox, temp_bbox_new);
857 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
858 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
859  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
860  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
861  * clones), in turn, call this function in their bbox methods. */
862 void
863 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
865     g_assert(item != NULL);
866     g_assert(SP_IS_ITEM(item));
867     g_assert(bbox != NULL);
869     if (clear) {
870         bbox->x0 = bbox->y0 = 1e18;
871         bbox->x1 = bbox->y1 = -1e18;
872     }
874     NRRect this_bbox;
875     this_bbox.x0 = this_bbox.y0 = 1e18;
876     this_bbox.x1 = this_bbox.y1 = -1e18;
878     // call the subclass method
879     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
880         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
881     }
883     // unless this is geometric bbox, crop the bbox by clip path, if any
884     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
885         NRRect b;
886         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
887         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
888     }
890     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
891     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
892         nr_rect_d_union (bbox, bbox, &this_bbox);
893     }
896 unsigned sp_item_pos_in_parent(SPItem *item)
898     g_assert(item != NULL);
899     g_assert(SP_IS_ITEM(item));
901     SPObject *parent = SP_OBJECT_PARENT(item);
902     g_assert(parent != NULL);
903     g_assert(SP_IS_OBJECT(parent));
905     SPObject *object = SP_OBJECT(item);
907     unsigned pos=0;
908     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
909         if ( iter == object ) {
910             return pos;
911         }
912         if (SP_IS_ITEM(iter)) {
913             pos++;
914         }
915     }
917     g_assert_not_reached();
918     return 0;
921 void
922 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
924     g_assert(item != NULL);
925     g_assert(SP_IS_ITEM(item));
926     g_assert(bbox != NULL);
928     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
931 Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
933     Geom::OptRect rect = Geom::OptRect();
934     sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
935     return rect;
938 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const */*snapprefs*/)
940     /* This will only be called if the derived class doesn't override this.
941      * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
942      * We don't know what shape we could be dealing with here, so we'll just
943      * return the corners of the bounding box */
945     Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
947     if (bbox) {
948         Geom::Point p1, p2;
949         p1 = bbox->min();
950         p2 = bbox->max();
951         *p = p1;
952         *p = Geom::Point(p1[Geom::X], p2[Geom::Y]);
953         *p = p2;
954         *p = Geom::Point(p1[Geom::Y], p2[Geom::X]);
955     }
959 void sp_item_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
961     g_assert (item != NULL);
962     g_assert (SP_IS_ITEM(item));
964     // Get the snappoints of the item
965     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
966     if (item_class.snappoints) {
967         item_class.snappoints(item, p, snapprefs);
968     }
970     // Get the snappoints at the item's center
971     if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
972         *p = item->getCenter();
973     }
975     // Get the snappoints of clipping paths and mask, if any
976     std::list<SPObject const *> clips_and_masks;
978     clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
979     clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
981     for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
982         if (*o) {
983             // obj is a group object, the children are the actual clippers
984             for (SPObject *child = (*o)->children ; child ; child = child->next) {
985                 if (SP_IS_ITEM(child)) {
986                     std::vector<Geom::Point> p_clip_or_mask;
987                     // Please note the recursive call here!
988                     sp_item_snappoints(SP_ITEM(child), SnapPointsIter(p_clip_or_mask), snapprefs);
989                     // Take into account the transformation of the item being clipped or masked
990                     for (std::vector<Geom::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
991                         // All snappoints are in desktop coordinates, but the item's transformation is
992                         // in document coordinates. Hence the awkward construction below
993                         *p = (*p_orig) * matrix_to_desktop (matrix_from_desktop (item->transform, item), item);
994                     }
995                 }
996             }
997         }
998     }
1001 void
1002 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
1004     if (!item->isHidden()) {
1005         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
1006             if (!item->transform.isIdentity()
1007                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
1008             {
1009                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1010                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1011                 sp_print_release(ctx);
1012             } else {
1013                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1014             }
1015         }
1016     }
1019 static gchar *
1020 sp_item_private_description(SPItem */*item*/)
1022     return g_strdup(_("Object"));
1025 /**
1026  * Returns a string suitable for status bar, formatted in pango markup language.
1027  *
1028  * Must be freed by caller.
1029  */
1030 gchar *
1031 sp_item_description(SPItem *item)
1033     g_assert(item != NULL);
1034     g_assert(SP_IS_ITEM(item));
1036     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1037         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1038         if (s && item->clip_ref->getObject()) {
1039             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1040             g_free (s);
1041             s = snew;
1042         }
1043         if (s && item->mask_ref->getObject()) {
1044             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1045             g_free (s);
1046             s = snew;
1047         }
1048         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
1049             gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1050             g_free (s);
1051             s = snew;
1052         }
1053         return s;
1054     }
1056     g_assert_not_reached();
1057     return NULL;
1060 /**
1061  * Allocates unique integer keys.
1062  * \param numkeys Number of keys required.
1063  * \return First allocated key; hence if the returned key is n
1064  * you can use n, n + 1, ..., n + (numkeys - 1)
1065  */
1066 unsigned
1067 sp_item_display_key_new(unsigned numkeys)
1069     static unsigned dkey = 0;
1071     dkey += numkeys;
1073     return dkey - numkeys;
1076 NRArenaItem *
1077 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1079     g_assert(item != NULL);
1080     g_assert(SP_IS_ITEM(item));
1081     g_assert(arena != NULL);
1082     g_assert(NR_IS_ARENA(arena));
1084     NRArenaItem *ai = NULL;
1085     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1086         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1087     }
1089     if (ai != NULL) {
1090         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1091         nr_arena_item_set_transform(ai, item->transform);
1092         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1093         nr_arena_item_set_visible(ai, !item->isHidden());
1094         nr_arena_item_set_sensitive(ai, item->sensitive);
1095         if (item->clip_ref->getObject()) {
1096             SPClipPath *cp = item->clip_ref->getObject();
1098             if (!item->display->arenaitem->key) {
1099                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1100             }
1101             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1103             // Show and set clip
1104             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1105             nr_arena_item_set_clip(ai, ac);
1106             nr_arena_item_unref(ac);
1108             // Update bbox, in case the clip uses bbox units
1109             NRRect bbox;
1110             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1111             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1112             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1113         }
1114         if (item->mask_ref->getObject()) {
1115             SPMask *mask = item->mask_ref->getObject();
1117             if (!item->display->arenaitem->key) {
1118                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1119             }
1120             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1122             // Show and set mask
1123             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1124             nr_arena_item_set_mask(ai, ac);
1125             nr_arena_item_unref(ac);
1127             // Update bbox, in case the mask uses bbox units
1128             NRRect bbox;
1129             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1130             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1131             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1132         }
1133         NR_ARENA_ITEM_SET_DATA(ai, item);
1134         Geom::OptRect item_bbox;
1135         sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1136         nr_arena_item_set_item_bbox(ai, item_bbox);
1137     }
1139     return ai;
1142 void
1143 sp_item_invoke_hide(SPItem *item, unsigned key)
1145     g_assert(item != NULL);
1146     g_assert(SP_IS_ITEM(item));
1148     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1149         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1150     }
1152     SPItemView *ref = NULL;
1153     SPItemView *v = item->display;
1154     while (v != NULL) {
1155         SPItemView *next = v->next;
1156         if (v->key == key) {
1157             if (item->clip_ref->getObject()) {
1158                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1159                 nr_arena_item_set_clip(v->arenaitem, NULL);
1160             }
1161             if (item->mask_ref->getObject()) {
1162                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1163                 nr_arena_item_set_mask(v->arenaitem, NULL);
1164             }
1165             if (!ref) {
1166                 item->display = v->next;
1167             } else {
1168                 ref->next = v->next;
1169             }
1170             nr_arena_item_unparent(v->arenaitem);
1171             nr_arena_item_unref(v->arenaitem);
1172             g_free(v);
1173         } else {
1174             ref = v;
1175         }
1176         v = next;
1177     }
1180 // Adjusters
1182 void
1183 sp_item_adjust_pattern (SPItem *item, Geom::Matrix const &postmul, bool set)
1185     SPStyle *style = SP_OBJECT_STYLE (item);
1187     if (style && (style->fill.isPaintserver())) {
1188         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1189         if (SP_IS_PATTERN (server)) {
1190             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1191             sp_pattern_transform_multiply (pattern, postmul, set);
1192         }
1193     }
1195     if (style && (style->stroke.isPaintserver())) {
1196         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1197         if (SP_IS_PATTERN (server)) {
1198             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1199             sp_pattern_transform_multiply (pattern, postmul, set);
1200         }
1201     }
1205 void
1206 sp_item_adjust_gradient (SPItem *item, Geom::Matrix const &postmul, bool set)
1208     SPStyle *style = SP_OBJECT_STYLE (item);
1210     if (style && (style->fill.isPaintserver())) {
1211         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1212         if (SP_IS_GRADIENT (server)) {
1214             /**
1215              * \note Bbox units for a gradient are generally a bad idea because
1216              * with them, you cannot preserve the relative position of the
1217              * object and its gradient after rotation or skew. So now we
1218              * convert them to userspace units which are easy to keep in sync
1219              * just by adding the object's transform to gradientTransform.
1220              * \todo FIXME: convert back to bbox units after transforming with
1221              * the item, so as to preserve the original units.
1222              */
1223             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1225             sp_gradient_transform_multiply (gradient, postmul, set);
1226         }
1227     }
1229     if (style && (style->stroke.isPaintserver())) {
1230         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1231         if (SP_IS_GRADIENT (server)) {
1232             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1233             sp_gradient_transform_multiply (gradient, postmul, set);
1234         }
1235     }
1238 void
1239 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1241     SPStyle *style = SP_OBJECT_STYLE (item);
1243     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1245         style->stroke_width.computed *= ex;
1246         style->stroke_width.set = TRUE;
1248         if (style->stroke_dash.n_dash != 0) {
1249             int i;
1250             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1251                 style->stroke_dash.dash[i] *= ex;
1252             }
1253             style->stroke_dash.offset *= ex;
1254         }
1256         SP_OBJECT(item)->updateRepr();
1257     }
1260 /**
1261  * Find out the inverse of previous transform of an item (from its repr)
1262  */
1263 Geom::Matrix
1264 sp_item_transform_repr (SPItem *item)
1266     Geom::Matrix t_old(Geom::identity());
1267     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1268     if (t_attr) {
1269         Geom::Matrix t;
1270         if (sp_svg_transform_read(t_attr, &t)) {
1271             t_old = t;
1272         }
1273     }
1275     return t_old;
1279 /**
1280  * Recursively scale stroke width in \a item and its children by \a expansion.
1281  */
1282 void
1283 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1285     sp_item_adjust_stroke (item, expansion);
1287 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1288     if (item && SP_IS_USE(item))
1289         return;
1291     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1292         if (SP_IS_ITEM(o))
1293             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1294     }
1297 /**
1298  * Recursively adjust rx and ry of rects.
1299  */
1300 void
1301 sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
1303     if (SP_IS_RECT (item)) {
1304         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1305     }
1307     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1308         if (SP_IS_ITEM(o))
1309             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1310     }
1313 /**
1314  * Recursively compensate pattern or gradient transform.
1315  */
1316 void
1317 sp_item_adjust_paint_recursive (SPItem *item, Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
1319 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1320 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1321 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1322     Geom::Matrix t_item = sp_item_transform_repr (item);
1323     Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1325 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1326 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1327 // we must not touch it
1328     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1329         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1330             if (SP_IS_ITEM(o)) {
1331 // At the level of the transformed item, t_ancestors is identity;
1332 // below it, it is the accmmulated chain of transforms from this level to the top level
1333                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1334             }
1335         }
1336     }
1338 // We recursed into children first, and are now adjusting this object second;
1339 // this is so that adjustments in a tree are done from leaves up to the root,
1340 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1341 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1343     if (is_pattern)
1344         sp_item_adjust_pattern (item, paint_delta);
1345     else
1346         sp_item_adjust_gradient (item, paint_delta);
1350 void
1351 sp_item_adjust_livepatheffect (SPItem *item, Geom::Matrix const &postmul, bool set)
1353     if ( !SP_IS_LPE_ITEM(item) )
1354         return;
1356     SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1357     if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1358         PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
1359         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1360         {
1361             // If the path effect is used by 2 or more items, fork it
1362             // so that each object has its own independent copy of the effect
1363             LivePathEffectObject *lpeobj = (*it)->lpeobject;
1364             if (lpeobj) {
1365                 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1366                 if (new_lpeobj != lpeobj) {
1367                     sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1368                 }
1370                 if (lpeobj->get_lpe()) {
1371                     Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
1372                     effect->transform_multiply(postmul, set);
1373                 }
1374             }
1375         }
1376     }
1379 /**
1380  * Set a new transform on an object.
1381  *
1382  * Compensate for stroke scaling and gradient/pattern fill transform, if
1383  * necessary. Call the object's set_transform method if transforms are
1384  * stored optimized. Send _transformed_signal. Invoke _write method so that
1385  * the repr is updated with the new transform.
1386  */
1387 void
1388 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
1390     g_return_if_fail(item != NULL);
1391     g_return_if_fail(SP_IS_ITEM(item));
1392     g_return_if_fail(repr != NULL);
1394     // calculate the relative transform, if not given by the adv attribute
1395     Geom::Matrix advertized_transform;
1396     if (adv != NULL) {
1397         advertized_transform = *adv;
1398     } else {
1399         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1400     }
1402     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1403     if (compensate) {
1405          // recursively compensate for stroke scaling, depending on user preference
1406         if (!prefs->getBool("/options/transform/stroke", true)) {
1407             double const expansion = 1. / advertized_transform.descrim();
1408             sp_item_adjust_stroke_width_recursive(item, expansion);
1409         }
1411         // recursively compensate rx/ry of a rect if requested
1412         if (!prefs->getBool("/options/transform/rectcorners", true)) {
1413             sp_item_adjust_rects_recursive(item, advertized_transform);
1414         }
1416         // recursively compensate pattern fill if it's not to be transformed
1417         if (!prefs->getBool("/options/transform/pattern", true)) {
1418             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
1419         }
1420         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1421         /// recursively compensate gradient fill if it's not to be transformed
1422         if (!prefs->getBool("/options/transform/gradient", true)) {
1423             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
1424         } else {
1425             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1426             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1427             sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
1428         }
1430     } // endif(compensate)
1432     gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1433     Geom::Matrix transform_attr (transform);
1434     if ( // run the object's set_transform (i.e. embed transform) only if:
1435          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1436              !preserve && // user did not chose to preserve all transforms
1437              !item->clip_ref->getObject() && // the object does not have a clippath
1438              !item->mask_ref->getObject() && // the object does not have a mask
1439          !(!transform.isTranslation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1440              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1441         ) {
1442         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1443     }
1444     sp_item_set_item_transform(item, transform_attr);
1446     // Note: updateRepr comes before emitting the transformed signal since
1447     // it causes clone SPUse's copy of the original object to brought up to
1448     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1449     // values if called in code handling the transformed signal.
1450     SP_OBJECT(item)->updateRepr();
1452     // send the relative transform with a _transformed_signal
1453     item->_transformed_signal.emit(&advertized_transform, item);
1456 gint
1457 sp_item_event(SPItem *item, SPEvent *event)
1459     g_return_val_if_fail(item != NULL, FALSE);
1460     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1461     g_return_val_if_fail(event != NULL, FALSE);
1463     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1464         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1466     return FALSE;
1469 /**
1470  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1471  * gradients, patterns as sp_item_write_transform does.
1472  */
1473 void
1474 sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
1476     g_return_if_fail(item != NULL);
1477     g_return_if_fail(SP_IS_ITEM(item));
1479     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1480         item->transform = transform;
1481         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1482            transformation.  It's apparently not used anywhere else. */
1483         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1484         sp_item_rm_unsatisfied_cns(*item);
1485     }
1488 void
1489 sp_item_convert_item_to_guides(SPItem *item) {
1490     g_return_if_fail(item != NULL);
1491     g_return_if_fail(SP_IS_ITEM(item));
1493     /* Use derived method if present ... */
1494     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1495         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1496         return;
1497     }
1499     /* .. otherwise simply place the guides around the item's bounding box */
1501     sp_item_convert_to_guides(item);
1505 /**
1506  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1507  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1508  */
1509 Geom::Matrix
1510 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1511     Geom::Matrix ret(Geom::identity());
1512     g_return_val_if_fail(object != NULL, ret);
1514     /* stop at first non-renderable ancestor */
1515     while ( object != ancestor && SP_IS_ITEM(object) ) {
1516         if (SP_IS_ROOT(object)) {
1517             ret *= SP_ROOT(object)->c2p;
1518         } else {
1519             ret *= SP_ITEM(object)->transform;
1520         }
1521         object = SP_OBJECT_PARENT(object);
1522     }
1523     return ret;
1526 Geom::Matrix
1527 i2i_affine(SPObject const *src, SPObject const *dest) {
1528     g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1529     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1530     return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1533 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1534     return i2i_affine(this, dest);
1537 /**
1538  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1539  * \pre (item != NULL) and SP_IS_ITEM(item).
1540  */
1541 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1543     return i2anc_affine(item, NULL);
1546 /**
1547  * Returns the transformation from item to desktop coords
1548  */
1549 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1551     g_assert(item != NULL);
1552     g_assert(SP_IS_ITEM(item));
1554     Geom::Matrix const ret( sp_item_i2doc_affine(item)
1555                           * Geom::Scale(1, -1)
1556                           * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1557     return ret;
1560 /**
1561  * Converts a matrix \a m into the desktop coords of the \a item.
1562  * Will become a noop when we eliminate the coordinate flipping.
1563  */
1564 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1566     Geom::Matrix const ret(m
1567                          * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1568                          * Geom::Scale(1, -1));
1569     return ret;
1572 /**
1573  * Converts a matrix \a m from the desktop coords of the \a item.
1574  * Will become a noop when we eliminate the coordinate flipping.
1575  */
1576 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1578     Geom::Matrix const ret(Geom::Scale(1, -1)
1579                          * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1580                          * m);
1581     return ret;
1584 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1586     g_return_if_fail( item != NULL );
1587     g_return_if_fail( SP_IS_ITEM(item) );
1589     Geom::Matrix dt2p; /* desktop to item parent transform */
1590     if (SP_OBJECT_PARENT(item)) {
1591         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1592     } else {
1593         dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1594                  * Geom::Scale(1, -1) );
1595     }
1597     Geom::Matrix const i2p( i2dt * dt2p );
1598     sp_item_set_item_transform(item, i2p);
1602 /**
1603  * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
1604  */
1605 Geom::Matrix
1606 sp_item_dt2i_affine(SPItem const *item)
1608     /* fixme: Implement the right way (Lauris) */
1609     return sp_item_i2d_affine(item).inverse();
1612 /* Item views */
1614 static SPItemView *
1615 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1617     SPItemView *new_view;
1619     g_assert(item != NULL);
1620     g_assert(SP_IS_ITEM(item));
1621     g_assert(arenaitem != NULL);
1622     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1624     new_view = g_new(SPItemView, 1);
1626     new_view->next = list;
1627     new_view->flags = flags;
1628     new_view->key = key;
1629     new_view->arenaitem = arenaitem;
1631     return new_view;
1634 static SPItemView *
1635 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1637     if (view == list) {
1638         list = list->next;
1639     } else {
1640         SPItemView *prev;
1641         prev = list;
1642         while (prev->next != view) prev = prev->next;
1643         prev->next = view->next;
1644     }
1646     nr_arena_item_unref(view->arenaitem);
1647     g_free(view);
1649     return list;
1652 /**
1653  * Return the arenaitem corresponding to the given item in the display
1654  * with the given key
1655  */
1656 NRArenaItem *
1657 sp_item_get_arenaitem(SPItem *item, unsigned key)
1659     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1660         if ( iv->key == key ) {
1661             return iv->arenaitem;
1662         }
1663     }
1665     return NULL;
1668 int
1669 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1671     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1672                                     SP_OBJECT_REPR(second));
1675 SPItem *
1676 sp_item_first_item_child (SPObject *obj)
1678     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1679         if (SP_IS_ITEM (iter))
1680             return SP_ITEM (iter);
1681     }
1682     return NULL;
1685 void
1686 sp_item_convert_to_guides(SPItem *item) {
1687     SPDesktop *dt = inkscape_active_desktop();
1688     SPNamedView *nv = sp_desktop_namedview(dt);
1689     (void)nv;
1691     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1692     int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1693     SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1694         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1696     Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
1697     if (!bbox) {
1698         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1699         return;
1700     }
1702     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1704     Geom::Point A((*bbox).min());
1705     Geom::Point C((*bbox).max());
1706     Geom::Point B(A[Geom::X], C[Geom::Y]);
1707     Geom::Point D(C[Geom::X], A[Geom::Y]);
1709     pts.push_back(std::make_pair(A, B));
1710     pts.push_back(std::make_pair(B, C));
1711     pts.push_back(std::make_pair(C, D));
1712     pts.push_back(std::make_pair(D, A));
1714     sp_guide_pt_pairs_to_guides(dt, pts);
1717 /*
1718   Local Variables:
1719   mode:c++
1720   c-file-style:"stroustrup"
1721   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1722   indent-tabs-mode:nil
1723   fill-column:99
1724   End:
1725 */
1726 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :