Code

display filter label, if any, in filtered item description
[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.h"
37 #include "desktop-handles.h"
39 #include "style.h"
40 #include <glibmm/i18n.h>
41 #include "sp-root.h"
42 #include "sp-clippath.h"
43 #include "sp-mask.h"
44 #include "sp-rect.h"
45 #include "sp-use.h"
46 #include "sp-text.h"
47 #include "sp-item-rm-unsatisfied-cns.h"
48 #include "sp-pattern.h"
49 #include "sp-switch.h"
50 #include "gradient-chemistry.h"
51 #include "preferences.h"
52 #include "conn-avoid-ref.h"
53 #include "conditions.h"
54 #include "sp-filter-reference.h"
55 #include "filter-chemistry.h"
56 #include "sp-guide.h"
57 #include "sp-title.h"
58 #include "sp-desc.h"
60 #include "libnr/nr-matrix-fns.h"
61 #include "libnr/nr-matrix-scale-ops.h"
62 #include "libnr/nr-matrix-translate-ops.h"
63 #include "libnr/nr-scale-translate-ops.h"
64 #include "libnr/nr-translate-scale-ops.h"
65 #include "libnr/nr-convert2geom.h"
66 #include "algorithms/find-last-if.h"
67 #include "util/reverse-list.h"
68 #include <2geom/rect.h>
69 #include <2geom/matrix.h>
70 #include <2geom/transforms.h>
72 #include "xml/repr.h"
73 #include "extract-uri.h"
74 #include "helper/geom.h"
76 #include "live_effects/lpeobject.h"
77 #include "live_effects/effect.h"
78 #include "live_effects/lpeobject-reference.h"
80 #define noSP_ITEM_DEBUG_IDLE
82 static void sp_item_class_init(SPItemClass *klass);
83 static void sp_item_init(SPItem *item);
85 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
86 static void sp_item_release(SPObject *object);
87 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
88 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
89 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
91 static gchar *sp_item_private_description(SPItem *item);
92 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
94 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
95 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
97 static SPObjectClass *parent_class;
99 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
100 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
102 /**
103  * Registers SPItem class and returns its type number.
104  */
105 GType
106 sp_item_get_type(void)
108     static GType type = 0;
109     if (!type) {
110         GTypeInfo info = {
111             sizeof(SPItemClass),
112             NULL, NULL,
113             (GClassInitFunc) sp_item_class_init,
114             NULL, NULL,
115             sizeof(SPItem),
116             16,
117             (GInstanceInitFunc) sp_item_init,
118             NULL,   /* value_table */
119         };
120         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
121     }
122     return type;
125 /**
126  * SPItem vtable initialization.
127  */
128 static void
129 sp_item_class_init(SPItemClass *klass)
131     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
133     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
135     sp_object_class->build = sp_item_build;
136     sp_object_class->release = sp_item_release;
137     sp_object_class->set = sp_item_set;
138     sp_object_class->update = sp_item_update;
139     sp_object_class->write = sp_item_write;
141     klass->description = sp_item_private_description;
142     klass->snappoints = sp_item_private_snappoints;
145 /**
146  * Callback for SPItem object initialization.
147  */
148 static void
149 sp_item_init(SPItem *item)
151     item->init();
154 void SPItem::init() {
155     this->sensitive = TRUE;
157     this->transform_center_x = 0;
158     this->transform_center_y = 0;
160     this->_is_evaluated = true;
161     this->_evaluated_status = StatusUnknown;
163     this->transform = Geom::identity();
165     this->display = NULL;
167     this->clip_ref = new SPClipPathReference(this);
168     sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
169     sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
170     _clip_ref_connection = cs1.connect(sl1);
172     this->mask_ref = new SPMaskReference(this);
173     sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
174     sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
175     _mask_ref_connection = cs2.connect(sl2);
177     this->avoidRef = new SPAvoidRef(this);
179     new (&this->_transformed_signal) sigc::signal<void, Geom::Matrix const *, SPItem *>();
182 bool SPItem::isVisibleAndUnlocked() const {
183     return (!isHidden() && !isLocked());
186 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
187     return (!isHidden(display_key) && !isLocked());
190 bool SPItem::isLocked() const {
191     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
192         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
193             return true;
194     }
195     return false;
198 void SPItem::setLocked(bool locked) {
199     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
200                      ( locked ? "1" : NULL ));
201     updateRepr();
204 bool SPItem::isHidden() const {
205     if (!isEvaluated())
206         return true;
207     return style->display.computed == SP_CSS_DISPLAY_NONE;
210 void SPItem::setHidden(bool hide) {
211     style->display.set = TRUE;
212     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
213     style->display.computed = style->display.value;
214     style->display.inherit = FALSE;
215     updateRepr();
218 bool SPItem::isHidden(unsigned display_key) const {
219     if (!isEvaluated())
220         return true;
221     for ( SPItemView *view(display) ; view ; view = view->next ) {
222         if ( view->key == display_key ) {
223             g_assert(view->arenaitem != NULL);
224             for ( NRArenaItem *arenaitem = view->arenaitem ;
225                   arenaitem ; arenaitem = arenaitem->parent )
226             {
227                 if (!arenaitem->visible) {
228                     return true;
229                 }
230             }
231             return false;
232         }
233     }
234     return true;
237 void SPItem::setEvaluated(bool evaluated) {
238     _is_evaluated = evaluated;
239     _evaluated_status = StatusSet;
242 void SPItem::resetEvaluated() {
243     if ( StatusCalculated == _evaluated_status ) {
244         _evaluated_status = StatusUnknown;
245         bool oldValue = _is_evaluated;
246         if ( oldValue != isEvaluated() ) {
247             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
248         }
249     } if ( StatusSet == _evaluated_status ) {
250         SPObject const *const parent = SP_OBJECT_PARENT(this);
251         if (SP_IS_SWITCH(parent)) {
252             SP_SWITCH(parent)->resetChildEvaluated();
253         }
254     }
257 bool SPItem::isEvaluated() const {
258     if ( StatusUnknown == _evaluated_status ) {
259         _is_evaluated = sp_item_evaluate(this);
260         _evaluated_status = StatusCalculated;
261     }
262     return _is_evaluated;
265 /**
266  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
267  *  Corresponds to setExplicitlyHidden.
268  */
269 bool
270 SPItem::isExplicitlyHidden() const
272     return (this->style->display.set
273             && this->style->display.value == SP_CSS_DISPLAY_NONE);
276 /**
277  * Sets the display CSS property to `hidden' if \a val is true,
278  * otherwise makes it unset
279  */
280 void
281 SPItem::setExplicitlyHidden(bool const val) {
282     this->style->display.set = val;
283     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
284     this->style->display.computed = this->style->display.value;
285     this->updateRepr();
288 /**
289  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
290  */
291 void
292 SPItem::setCenter(Geom::Point object_centre) {
293     Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
294     if (bbox) {
295         transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
296         if (fabs(transform_center_x) < 1e-5) // rounding error
297             transform_center_x = 0;
298         transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
299         if (fabs(transform_center_y) < 1e-5) // rounding error
300             transform_center_y = 0;
301     }
304 void
305 SPItem::unsetCenter() {
306     transform_center_x = 0;
307     transform_center_y = 0;
310 bool SPItem::isCenterSet() {
311     return (transform_center_x != 0 || transform_center_y != 0);
314 Geom::Point SPItem::getCenter() const {
315     Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
316     if (bbox) {
317         return to_2geom(bbox->midpoint()) + Geom::Point (this->transform_center_x, this->transform_center_y);
318     } else {
319         return Geom::Point (0, 0); // something's wrong!
320     }
324 namespace {
326 bool is_item(SPObject const &object) {
327     return SP_IS_ITEM(&object);
332 void SPItem::raiseToTop() {
333     using Inkscape::Algorithms::find_last_if;
335     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
336         SP_OBJECT_NEXT(this), NULL, &is_item
337     );
338     if (topmost) {
339         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
340         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
341     }
344 void SPItem::raiseOne() {
345     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
346         SP_OBJECT_NEXT(this), NULL, &is_item
347     );
348     if (next_higher) {
349         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
350         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
351         sp_repr_parent(repr)->changeOrder(repr, ref);
352     }
355 void SPItem::lowerOne() {
356     using Inkscape::Util::MutableList;
357     using Inkscape::Util::reverse_list;
359     MutableList<SPObject &> next_lower=std::find_if(
360         reverse_list<SPObject::SiblingIterator>(
361             SP_OBJECT_PARENT(this)->firstChild(), this
362         ),
363         MutableList<SPObject &>(),
364         &is_item
365     );
366     if (next_lower) {
367         ++next_lower;
368         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
369         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
370         sp_repr_parent(repr)->changeOrder(repr, ref);
371     }
374 void SPItem::lowerToBottom() {
375     using Inkscape::Algorithms::find_last_if;
376     using Inkscape::Util::MutableList;
377     using Inkscape::Util::reverse_list;
379     MutableList<SPObject &> bottom=find_last_if(
380         reverse_list<SPObject::SiblingIterator>(
381             SP_OBJECT_PARENT(this)->firstChild(), this
382         ),
383         MutableList<SPObject &>(),
384         &is_item
385     );
386     if (bottom) {
387         ++bottom;
388         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
389         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
390         sp_repr_parent(repr)->changeOrder(repr, ref);
391     }
394 static void
395 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
397     sp_object_read_attr(object, "style");
398     sp_object_read_attr(object, "transform");
399     sp_object_read_attr(object, "clip-path");
400     sp_object_read_attr(object, "mask");
401     sp_object_read_attr(object, "sodipodi:insensitive");
402     sp_object_read_attr(object, "sodipodi:nonprintable");
403     sp_object_read_attr(object, "inkscape:transform-center-x");
404     sp_object_read_attr(object, "inkscape:transform-center-y");
405     sp_object_read_attr(object, "inkscape:connector-avoid");
407     if (((SPObjectClass *) (parent_class))->build) {
408         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
409     }
412 static void
413 sp_item_release(SPObject *object)
415     SPItem *item = (SPItem *) object;
417     item->_clip_ref_connection.disconnect();
418     item->_mask_ref_connection.disconnect();
420     // Note: do this here before the clip_ref is deleted, since calling
421     // sp_document_ensure_up_to_date for triggered routing may reference
422     // the deleted clip_ref.
423     if (item->avoidRef) {
424         delete item->avoidRef;
425         item->avoidRef = NULL;
426     }
428     if (item->clip_ref) {
429         item->clip_ref->detach();
430         delete item->clip_ref;
431         item->clip_ref = NULL;
432     }
434     if (item->mask_ref) {
435         item->mask_ref->detach();
436         delete item->mask_ref;
437         item->mask_ref = NULL;
438     }
440     if (((SPObjectClass *) (parent_class))->release) {
441         ((SPObjectClass *) parent_class)->release(object);
442     }
444     while (item->display) {
445         nr_arena_item_unparent(item->display->arenaitem);
446         item->display = sp_item_view_list_remove(item->display, item->display);
447     }
449     item->_transformed_signal.~signal();
452 static void
453 sp_item_set(SPObject *object, unsigned key, gchar const *value)
455     SPItem *item = (SPItem *) object;
457     switch (key) {
458         case SP_ATTR_TRANSFORM: {
459             Geom::Matrix t;
460             if (value && sp_svg_transform_read(value, &t)) {
461                 sp_item_set_item_transform(item, t);
462             } else {
463                 sp_item_set_item_transform(item, Geom::identity());
464             }
465             break;
466         }
467         case SP_PROP_CLIP_PATH: {
468             gchar *uri = extract_uri(value);
469             if (uri) {
470                 try {
471                     item->clip_ref->attach(Inkscape::URI(uri));
472                 } catch (Inkscape::BadURIException &e) {
473                     g_warning("%s", e.what());
474                     item->clip_ref->detach();
475                 }
476                 g_free(uri);
477             } else {
478                 item->clip_ref->detach();
479             }
481             break;
482         }
483         case SP_PROP_MASK: {
484             gchar *uri = extract_uri(value);
485             if (uri) {
486                 try {
487                     item->mask_ref->attach(Inkscape::URI(uri));
488                 } catch (Inkscape::BadURIException &e) {
489                     g_warning("%s", e.what());
490                     item->mask_ref->detach();
491                 }
492                 g_free(uri);
493             } else {
494                 item->mask_ref->detach();
495             }
497             break;
498         }
499         case SP_ATTR_SODIPODI_INSENSITIVE:
500             item->sensitive = !value;
501             for (SPItemView *v = item->display; v != NULL; v = v->next) {
502                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
503             }
504             break;
505         case SP_ATTR_CONNECTOR_AVOID:
506             item->avoidRef->setAvoid(value);
507             break;
508         case SP_ATTR_TRANSFORM_CENTER_X:
509             if (value) {
510                 item->transform_center_x = g_strtod(value, NULL);
511             } else {
512                 item->transform_center_x = 0;
513             }
514             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
515             break;
516         case SP_ATTR_TRANSFORM_CENTER_Y:
517             if (value) {
518                 item->transform_center_y = g_strtod(value, NULL);
519             } else {
520                 item->transform_center_y = 0;
521             }
522             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
523             break;
524         case SP_PROP_SYSTEM_LANGUAGE:
525         case SP_PROP_REQUIRED_FEATURES:
526         case SP_PROP_REQUIRED_EXTENSIONS:
527             {
528                 item->resetEvaluated();
529                 // pass to default handler
530             }
531         default:
532             if (SP_ATTRIBUTE_IS_CSS(key)) {
533                 sp_style_read_from_object(object->style, object);
534                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
535             } else {
536                 if (((SPObjectClass *) (parent_class))->set) {
537                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
538                 }
539             }
540             break;
541     }
544 static void
545 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
547     if (old_clip) {
548         SPItemView *v;
549         /* Hide clippath */
550         for (v = item->display; v != NULL; v = v->next) {
551             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
552             nr_arena_item_set_clip(v->arenaitem, NULL);
553         }
554     }
555     if (SP_IS_CLIPPATH(clip)) {
556         NRRect bbox;
557         sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
558         for (SPItemView *v = item->display; v != NULL; v = v->next) {
559             if (!v->arenaitem->key) {
560                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
561             }
562             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
563                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
564                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
565             nr_arena_item_set_clip(v->arenaitem, ai);
566             nr_arena_item_unref(ai);
567             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
568             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
569         }
570     }
573 static void
574 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
576     if (old_mask) {
577         /* Hide mask */
578         for (SPItemView *v = item->display; v != NULL; v = v->next) {
579             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
580             nr_arena_item_set_mask(v->arenaitem, NULL);
581         }
582     }
583     if (SP_IS_MASK(mask)) {
584         NRRect bbox;
585         sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
586         for (SPItemView *v = item->display; v != NULL; v = v->next) {
587             if (!v->arenaitem->key) {
588                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
589             }
590             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
591                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
592                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
593             nr_arena_item_set_mask(v->arenaitem, ai);
594             nr_arena_item_unref(ai);
595             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
596             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
597         }
598     }
601 static void
602 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
604     SPItem *item = SP_ITEM(object);
606     if (((SPObjectClass *) (parent_class))->update)
607         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
609     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
610         if (flags & SP_OBJECT_MODIFIED_FLAG) {
611             for (SPItemView *v = item->display; v != NULL; v = v->next) {
612                 nr_arena_item_set_transform(v->arenaitem, item->transform);
613             }
614         }
616         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
617         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
619         if ( clip_path || mask ) {
620             NRRect bbox;
621             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
622             if (clip_path) {
623                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
624                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
625                 }
626             }
627             if (mask) {
628                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
629                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
630                 }
631             }
632         }
634         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
635             for (SPItemView *v = item->display; v != NULL; v = v->next) {
636                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
637                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
638             }
639         }
640     }
642     /* Update bounding box data used by filters */
643     if (item->style->filter.set && item->display) {
644         Geom::OptRect item_bbox;
645         sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
647         SPItemView *itemview = item->display;
648         do {
649             if (itemview->arenaitem)
650                 nr_arena_item_set_item_bbox(itemview->arenaitem, item_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 /**
723  * \return  There is no guarantee that the return value will contain a rectangle.
724             If this item does not have a boundingbox, it might well be empty.
725  */
726 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
727                                       SPItem::BBoxType type,
728                                       unsigned int /*dkey*/) const
730     Geom::OptRect r;
731     sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
732     return r;
735 void
736 sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
738     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
741 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
742 void
743 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
745     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
748 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
749  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
750  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
751  * clones), in turn, call this function in their bbox methods.
752  * \retval bbox  Note that there is no guarantee that bbox will contain a rectangle when the
753  *               function returns. If this item does not have a boundingbox, this might well be empty.
754  */
755 void
756 sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
758     g_assert(item != NULL);
759     g_assert(SP_IS_ITEM(item));
761     if (clear) {
762         bbox = Geom::OptRect();
763     }
765     // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
767     NRRect temp_bbox;
768     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
769     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
771     // call the subclass method
772     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
773         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
774     }
776     // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
777     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
778         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
779             SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
780             if (filter && SP_IS_FILTER(filter)) {
781                 // default filer area per the SVG spec:
782                 double x = -0.1;
783                 double y = -0.1;
784                 double w = 1.2;
785                 double h = 1.2;
787                 // if area is explicitly set, override:
788                 if (SP_FILTER(filter)->x._set)
789                     x = SP_FILTER(filter)->x.computed;
790                 if (SP_FILTER(filter)->y._set)
791                     y = SP_FILTER(filter)->y.computed;
792                 if (SP_FILTER(filter)->width._set)
793                     w = SP_FILTER(filter)->width.computed;
794                 if (SP_FILTER(filter)->height._set)
795                     h = SP_FILTER(filter)->height.computed;
797                 double dx0 = 0;
798                 double dx1 = 0;
799                 double dy0 = 0;
800                 double dy1 = 0;
801                 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
802                     // if this is a single blur, use 2.4*radius
803                     // which may be smaller than the default area;
804                     // see set_filter_area for why it's 2.4
805                     double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
806                     dx0 = -2.4 * r;
807                     dx1 = 2.4 * r;
808                     dy0 = -2.4 * r;
809                     dy1 = 2.4 * r;
810                 } else {
811                     // otherwise, calculate expansion from relative to absolute units:
812                     dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
813                     dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
814                     dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
815                     dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
816                 }
818                 // transform the expansions by the item's transform:
819                 Geom::Matrix i2d(sp_item_i2d_affine (item));
820                 dx0 *= i2d.expansionX();
821                 dx1 *= i2d.expansionX();
822                 dy0 *= i2d.expansionY();
823                 dy1 *= i2d.expansionY();
825                 // expand the bbox
826                 temp_bbox.x0 += dx0;
827                 temp_bbox.x1 += dx1;
828                 temp_bbox.y0 += dy0;
829                 temp_bbox.y1 += dy1;
830             }
831         }
832         if (item->clip_ref->getObject()) {
833             NRRect b;
834             sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
835             nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
836         }
837     }
839     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
840         // Either the bbox hasn't been touched by the SPItemClass' bbox method
841         // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
842         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
844         // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
845         // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
846         // So union bbox with nothing = do nothing, just return
847         return;
848     }
850     // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
851     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
852     // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
853     Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
855     bbox = Geom::unify(bbox, temp_bbox_new);
858 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
859 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
860  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
861  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
862  * clones), in turn, call this function in their bbox methods. */
863 void
864 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
866     g_assert(item != NULL);
867     g_assert(SP_IS_ITEM(item));
868     g_assert(bbox != NULL);
870     if (clear) {
871         bbox->x0 = bbox->y0 = 1e18;
872         bbox->x1 = bbox->y1 = -1e18;
873     }
875     NRRect this_bbox;
876     this_bbox.x0 = this_bbox.y0 = 1e18;
877     this_bbox.x1 = this_bbox.y1 = -1e18;
879     // call the subclass method
880     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
881         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
882     }
884     // unless this is geometric bbox, crop the bbox by clip path, if any
885     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
886         NRRect b;
887         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
888         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
889     }
891     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
892     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
893         nr_rect_d_union (bbox, bbox, &this_bbox);
894     }
897 unsigned sp_item_pos_in_parent(SPItem *item)
899     g_assert(item != NULL);
900     g_assert(SP_IS_ITEM(item));
902     SPObject *parent = SP_OBJECT_PARENT(item);
903     g_assert(parent != NULL);
904     g_assert(SP_IS_OBJECT(parent));
906     SPObject *object = SP_OBJECT(item);
908     unsigned pos=0;
909     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
910         if ( iter == object ) {
911             return pos;
912         }
913         if (SP_IS_ITEM(iter)) {
914             pos++;
915         }
916     }
918     g_assert_not_reached();
919     return 0;
922 void
923 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
925     g_assert(item != NULL);
926     g_assert(SP_IS_ITEM(item));
927     g_assert(bbox != NULL);
929     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
932 Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
934     Geom::OptRect rect = Geom::OptRect();
935     sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
936     return rect;
939 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const */*snapprefs*/)
941     /* This will only be called if the derived class doesn't override this.
942      * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
943      * We don't know what shape we could be dealing with here, so we'll just
944      * return the corners of the bounding box */
946     Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
948     if (bbox) {
949         Geom::Point p1, p2;
950         p1 = bbox->min();
951         p2 = bbox->max();
952         *p = p1;
953         *p = Geom::Point(p1[Geom::X], p2[Geom::Y]);
954         *p = p2;
955         *p = Geom::Point(p1[Geom::Y], p2[Geom::X]);
956     }
960 void sp_item_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
962     g_assert (item != NULL);
963     g_assert (SP_IS_ITEM(item));
965     // Get the snappoints of the item
966     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
967     if (item_class.snappoints) {
968         item_class.snappoints(item, p, snapprefs);
969     }
971     // Get the snappoints at the item's center
972     if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
973         *p = item->getCenter();
974     }
976     // Get the snappoints of clipping paths and mask, if any
977     std::list<SPObject const *> clips_and_masks;
979     clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
980     clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
982     SPDesktop *desktop = inkscape_active_desktop();
983     for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
984         if (*o) {
985             // obj is a group object, the children are the actual clippers
986             for (SPObject *child = (*o)->children ; child ; child = child->next) {
987                 if (SP_IS_ITEM(child)) {
988                     std::vector<Geom::Point> p_clip_or_mask;
989                     // Please note the recursive call here!
990                     sp_item_snappoints(SP_ITEM(child), SnapPointsIter(p_clip_or_mask), snapprefs);
991                     // Take into account the transformation of the item being clipped or masked
992                     for (std::vector<Geom::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
993                         // All snappoints are in desktop coordinates, but the item's transformation is
994                         // in document coordinates. Hence the awkward construction below
995                         *p = desktop->dt2doc(*p_orig) * sp_item_i2d_affine(item);
996                     }
997                 }
998             }
999         }
1000     }
1003 void
1004 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
1006     if (!item->isHidden()) {
1007         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
1008             if (!item->transform.isIdentity()
1009                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
1010             {
1011                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1012                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1013                 sp_print_release(ctx);
1014             } else {
1015                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1016             }
1017         }
1018     }
1021 static gchar *
1022 sp_item_private_description(SPItem */*item*/)
1024     return g_strdup(_("Object"));
1027 /**
1028  * Returns a string suitable for status bar, formatted in pango markup language.
1029  *
1030  * Must be freed by caller.
1031  */
1032 gchar *
1033 sp_item_description(SPItem *item)
1035     g_assert(item != NULL);
1036     g_assert(SP_IS_ITEM(item));
1038     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1039         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1040         if (s && item->clip_ref->getObject()) {
1041             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1042             g_free (s);
1043             s = snew;
1044         }
1045         if (s && item->mask_ref->getObject()) {
1046             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1047             g_free (s);
1048             s = snew;
1049         }
1050         if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href && SP_OBJECT_STYLE(item)->filter.href->getObject()) {
1051             const gchar *label = SP_OBJECT_STYLE(item)->filter.href->getObject()->label();
1052             gchar *snew;
1053             if (label) {
1054                 snew = g_strdup_printf (_("%s; <i>filtered (%s)</i>"), s, label);
1055             } else {
1056                 snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1057             }
1058             g_free (s);
1059             s = snew;
1060         }
1061         return s;
1062     }
1064     g_assert_not_reached();
1065     return NULL;
1068 /**
1069  * Allocates unique integer keys.
1070  * \param numkeys Number of keys required.
1071  * \return First allocated key; hence if the returned key is n
1072  * you can use n, n + 1, ..., n + (numkeys - 1)
1073  */
1074 unsigned
1075 sp_item_display_key_new(unsigned numkeys)
1077     static unsigned dkey = 0;
1079     dkey += numkeys;
1081     return dkey - numkeys;
1084 NRArenaItem *
1085 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1087     g_assert(item != NULL);
1088     g_assert(SP_IS_ITEM(item));
1089     g_assert(arena != NULL);
1090     g_assert(NR_IS_ARENA(arena));
1092     NRArenaItem *ai = NULL;
1093     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1094         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1095     }
1097     if (ai != NULL) {
1098         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1099         nr_arena_item_set_transform(ai, item->transform);
1100         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1101         nr_arena_item_set_visible(ai, !item->isHidden());
1102         nr_arena_item_set_sensitive(ai, item->sensitive);
1103         if (item->clip_ref->getObject()) {
1104             SPClipPath *cp = item->clip_ref->getObject();
1106             if (!item->display->arenaitem->key) {
1107                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1108             }
1109             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1111             // Show and set clip
1112             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1113             nr_arena_item_set_clip(ai, ac);
1114             nr_arena_item_unref(ac);
1116             // Update bbox, in case the clip uses bbox units
1117             NRRect bbox;
1118             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1119             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1120             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1121         }
1122         if (item->mask_ref->getObject()) {
1123             SPMask *mask = item->mask_ref->getObject();
1125             if (!item->display->arenaitem->key) {
1126                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1127             }
1128             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1130             // Show and set mask
1131             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1132             nr_arena_item_set_mask(ai, ac);
1133             nr_arena_item_unref(ac);
1135             // Update bbox, in case the mask uses bbox units
1136             NRRect bbox;
1137             sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1138             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1139             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1140         }
1141         NR_ARENA_ITEM_SET_DATA(ai, item);
1142         Geom::OptRect item_bbox;
1143         sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1144         nr_arena_item_set_item_bbox(ai, item_bbox);
1145     }
1147     return ai;
1150 void
1151 sp_item_invoke_hide(SPItem *item, unsigned key)
1153     g_assert(item != NULL);
1154     g_assert(SP_IS_ITEM(item));
1156     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1157         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1158     }
1160     SPItemView *ref = NULL;
1161     SPItemView *v = item->display;
1162     while (v != NULL) {
1163         SPItemView *next = v->next;
1164         if (v->key == key) {
1165             if (item->clip_ref->getObject()) {
1166                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1167                 nr_arena_item_set_clip(v->arenaitem, NULL);
1168             }
1169             if (item->mask_ref->getObject()) {
1170                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1171                 nr_arena_item_set_mask(v->arenaitem, NULL);
1172             }
1173             if (!ref) {
1174                 item->display = v->next;
1175             } else {
1176                 ref->next = v->next;
1177             }
1178             nr_arena_item_unparent(v->arenaitem);
1179             nr_arena_item_unref(v->arenaitem);
1180             g_free(v);
1181         } else {
1182             ref = v;
1183         }
1184         v = next;
1185     }
1188 // Adjusters
1190 void
1191 sp_item_adjust_pattern (SPItem *item, Geom::Matrix const &postmul, bool set)
1193     SPStyle *style = SP_OBJECT_STYLE (item);
1195     if (style && (style->fill.isPaintserver())) {
1196         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1197         if (SP_IS_PATTERN (server)) {
1198             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1199             sp_pattern_transform_multiply (pattern, postmul, set);
1200         }
1201     }
1203     if (style && (style->stroke.isPaintserver())) {
1204         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1205         if (SP_IS_PATTERN (server)) {
1206             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1207             sp_pattern_transform_multiply (pattern, postmul, set);
1208         }
1209     }
1213 void
1214 sp_item_adjust_gradient (SPItem *item, Geom::Matrix const &postmul, bool set)
1216     SPStyle *style = SP_OBJECT_STYLE (item);
1218     if (style && (style->fill.isPaintserver())) {
1219         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1220         if (SP_IS_GRADIENT (server)) {
1222             /**
1223              * \note Bbox units for a gradient are generally a bad idea because
1224              * with them, you cannot preserve the relative position of the
1225              * object and its gradient after rotation or skew. So now we
1226              * convert them to userspace units which are easy to keep in sync
1227              * just by adding the object's transform to gradientTransform.
1228              * \todo FIXME: convert back to bbox units after transforming with
1229              * the item, so as to preserve the original units.
1230              */
1231             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1233             sp_gradient_transform_multiply (gradient, postmul, set);
1234         }
1235     }
1237     if (style && (style->stroke.isPaintserver())) {
1238         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1239         if (SP_IS_GRADIENT (server)) {
1240             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1241             sp_gradient_transform_multiply (gradient, postmul, set);
1242         }
1243     }
1246 void
1247 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1249     SPStyle *style = SP_OBJECT_STYLE (item);
1251     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1253         style->stroke_width.computed *= ex;
1254         style->stroke_width.set = TRUE;
1256         if (style->stroke_dash.n_dash != 0) {
1257             int i;
1258             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1259                 style->stroke_dash.dash[i] *= ex;
1260             }
1261             style->stroke_dash.offset *= ex;
1262         }
1264         SP_OBJECT(item)->updateRepr();
1265     }
1268 /**
1269  * Find out the inverse of previous transform of an item (from its repr)
1270  */
1271 Geom::Matrix
1272 sp_item_transform_repr (SPItem *item)
1274     Geom::Matrix t_old(Geom::identity());
1275     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1276     if (t_attr) {
1277         Geom::Matrix t;
1278         if (sp_svg_transform_read(t_attr, &t)) {
1279             t_old = t;
1280         }
1281     }
1283     return t_old;
1287 /**
1288  * Recursively scale stroke width in \a item and its children by \a expansion.
1289  */
1290 void
1291 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1293     sp_item_adjust_stroke (item, expansion);
1295 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1296     if (item && SP_IS_USE(item))
1297         return;
1299     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1300         if (SP_IS_ITEM(o))
1301             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1302     }
1305 /**
1306  * Recursively adjust rx and ry of rects.
1307  */
1308 void
1309 sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
1311     if (SP_IS_RECT (item)) {
1312         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1313     }
1315     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1316         if (SP_IS_ITEM(o))
1317             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1318     }
1321 /**
1322  * Recursively compensate pattern or gradient transform.
1323  */
1324 void
1325 sp_item_adjust_paint_recursive (SPItem *item, Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
1327 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1328 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1329 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1330     Geom::Matrix t_item = sp_item_transform_repr (item);
1331     Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1333 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1334 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1335 // we must not touch it
1336     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1337         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1338             if (SP_IS_ITEM(o)) {
1339 // At the level of the transformed item, t_ancestors is identity;
1340 // below it, it is the accmmulated chain of transforms from this level to the top level
1341                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1342             }
1343         }
1344     }
1346 // We recursed into children first, and are now adjusting this object second;
1347 // this is so that adjustments in a tree are done from leaves up to the root,
1348 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1349 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1351     if (is_pattern)
1352         sp_item_adjust_pattern (item, paint_delta);
1353     else
1354         sp_item_adjust_gradient (item, paint_delta);
1358 void
1359 sp_item_adjust_livepatheffect (SPItem *item, Geom::Matrix const &postmul, bool set)
1361     if ( !SP_IS_LPE_ITEM(item) )
1362         return;
1364     SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1365     if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1366         PathEffectList effect_list =  sp_lpe_item_get_effect_list(lpeitem);
1367         for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1368         {
1369             // If the path effect is used by 2 or more items, fork it
1370             // so that each object has its own independent copy of the effect
1371             LivePathEffectObject *lpeobj = (*it)->lpeobject;
1372             if (lpeobj) {
1373                 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1374                 if (new_lpeobj != lpeobj) {
1375                     sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1376                 }
1378                 if (lpeobj->get_lpe()) {
1379                     Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
1380                     effect->transform_multiply(postmul, set);
1381                 }
1382             }
1383         }
1384     }
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, Geom::Matrix const &transform, Geom::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     Geom::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     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1411     if (compensate) {
1413          // recursively compensate for stroke scaling, depending on user preference
1414         if (!prefs->getBool("/options/transform/stroke", true)) {
1415             double const expansion = 1. / advertized_transform.descrim();
1416             sp_item_adjust_stroke_width_recursive(item, expansion);
1417         }
1419         // recursively compensate rx/ry of a rect if requested
1420         if (!prefs->getBool("/options/transform/rectcorners", true)) {
1421             sp_item_adjust_rects_recursive(item, advertized_transform);
1422         }
1424         // recursively compensate pattern fill if it's not to be transformed
1425         if (!prefs->getBool("/options/transform/pattern", true)) {
1426             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
1427         }
1428         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1429         /// recursively compensate gradient fill if it's not to be transformed
1430         if (!prefs->getBool("/options/transform/gradient", true)) {
1431             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
1432         } else {
1433             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1434             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1435             sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
1436         }
1438     } // endif(compensate)
1440     gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1441     Geom::Matrix transform_attr (transform);
1442     if ( // run the object's set_transform (i.e. embed transform) only if:
1443          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1444              !preserve && // user did not chose to preserve all transforms
1445              !item->clip_ref->getObject() && // the object does not have a clippath
1446              !item->mask_ref->getObject() && // the object does not have a mask
1447          !(!transform.isTranslation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1448              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1449         ) {
1450         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1451     }
1452     sp_item_set_item_transform(item, transform_attr);
1454     // Note: updateRepr comes before emitting the transformed signal since
1455     // it causes clone SPUse's copy of the original object to brought up to
1456     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1457     // values if called in code handling the transformed signal.
1458     SP_OBJECT(item)->updateRepr();
1460     // send the relative transform with a _transformed_signal
1461     item->_transformed_signal.emit(&advertized_transform, item);
1464 gint
1465 sp_item_event(SPItem *item, SPEvent *event)
1467     g_return_val_if_fail(item != NULL, FALSE);
1468     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1469     g_return_val_if_fail(event != NULL, FALSE);
1471     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1472         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1474     return FALSE;
1477 /**
1478  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1479  * gradients, patterns as sp_item_write_transform does.
1480  */
1481 void
1482 sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
1484     g_return_if_fail(item != NULL);
1485     g_return_if_fail(SP_IS_ITEM(item));
1487     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1488         item->transform = transform;
1489         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1490            transformation.  It's apparently not used anywhere else. */
1491         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1492         sp_item_rm_unsatisfied_cns(*item);
1493     }
1496 void
1497 sp_item_convert_item_to_guides(SPItem *item) {
1498     g_return_if_fail(item != NULL);
1499     g_return_if_fail(SP_IS_ITEM(item));
1501     /* Use derived method if present ... */
1502     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1503         (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1504         return;
1505     }
1507     /* .. otherwise simply place the guides around the item's bounding box */
1509     sp_item_convert_to_guides(item);
1513 /**
1514  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1515  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1516  */
1517 Geom::Matrix
1518 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1519     Geom::Matrix ret(Geom::identity());
1520     g_return_val_if_fail(object != NULL, ret);
1522     /* stop at first non-renderable ancestor */
1523     while ( object != ancestor && SP_IS_ITEM(object) ) {
1524         if (SP_IS_ROOT(object)) {
1525             ret *= SP_ROOT(object)->c2p;
1526         } else {
1527             ret *= SP_ITEM(object)->transform;
1528         }
1529         object = SP_OBJECT_PARENT(object);
1530     }
1531     return ret;
1534 Geom::Matrix
1535 i2i_affine(SPObject const *src, SPObject const *dest) {
1536     g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1537     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1538     return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1541 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1542     return i2i_affine(this, dest);
1545 /**
1546  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1547  * \pre (item != NULL) and SP_IS_ITEM(item).
1548  */
1549 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1551     return i2anc_affine(item, NULL);
1554 /**
1555  * Returns the transformation from item to desktop coords
1556  */
1557 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1559     g_assert(item != NULL);
1560     g_assert(SP_IS_ITEM(item));
1562     Geom::Matrix const ret( sp_item_i2doc_affine(item)
1563                           * Geom::Scale(1, -1)
1564                           * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1565     return ret;
1568 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1570     g_return_if_fail( item != NULL );
1571     g_return_if_fail( SP_IS_ITEM(item) );
1573     Geom::Matrix dt2p; /* desktop to item parent transform */
1574     if (SP_OBJECT_PARENT(item)) {
1575         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1576     } else {
1577         dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1578                  * Geom::Scale(1, -1) );
1579     }
1581     Geom::Matrix const i2p( i2dt * dt2p );
1582     sp_item_set_item_transform(item, i2p);
1586 /**
1587  * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
1588  */
1589 Geom::Matrix
1590 sp_item_dt2i_affine(SPItem const *item)
1592     /* fixme: Implement the right way (Lauris) */
1593     return sp_item_i2d_affine(item).inverse();
1596 /* Item views */
1598 static SPItemView *
1599 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1601     SPItemView *new_view;
1603     g_assert(item != NULL);
1604     g_assert(SP_IS_ITEM(item));
1605     g_assert(arenaitem != NULL);
1606     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1608     new_view = g_new(SPItemView, 1);
1610     new_view->next = list;
1611     new_view->flags = flags;
1612     new_view->key = key;
1613     new_view->arenaitem = arenaitem;
1615     return new_view;
1618 static SPItemView *
1619 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1621     if (view == list) {
1622         list = list->next;
1623     } else {
1624         SPItemView *prev;
1625         prev = list;
1626         while (prev->next != view) prev = prev->next;
1627         prev->next = view->next;
1628     }
1630     nr_arena_item_unref(view->arenaitem);
1631     g_free(view);
1633     return list;
1636 /**
1637  * Return the arenaitem corresponding to the given item in the display
1638  * with the given key
1639  */
1640 NRArenaItem *
1641 sp_item_get_arenaitem(SPItem *item, unsigned key)
1643     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1644         if ( iv->key == key ) {
1645             return iv->arenaitem;
1646         }
1647     }
1649     return NULL;
1652 int
1653 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1655     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1656                                     SP_OBJECT_REPR(second));
1659 SPItem *
1660 sp_item_first_item_child (SPObject *obj)
1662     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1663         if (SP_IS_ITEM (iter))
1664             return SP_ITEM (iter);
1665     }
1666     return NULL;
1669 void
1670 sp_item_convert_to_guides(SPItem *item) {
1671     SPDesktop *dt = inkscape_active_desktop();
1672     SPNamedView *nv = sp_desktop_namedview(dt);
1673     (void)nv;
1675     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1676     int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1677     SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1678         SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1680     Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
1681     if (!bbox) {
1682         g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1683         return;
1684     }
1686     std::list<std::pair<Geom::Point, Geom::Point> > pts;
1688     Geom::Point A((*bbox).min());
1689     Geom::Point C((*bbox).max());
1690     Geom::Point B(A[Geom::X], C[Geom::Y]);
1691     Geom::Point D(C[Geom::X], A[Geom::Y]);
1693     pts.push_back(std::make_pair(A, B));
1694     pts.push_back(std::make_pair(B, C));
1695     pts.push_back(std::make_pair(C, D));
1696     pts.push_back(std::make_pair(D, A));
1698     sp_guide_pt_pairs_to_guides(dt, pts);
1701 /*
1702   Local Variables:
1703   mode:c++
1704   c-file-style:"stroustrup"
1705   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1706   indent-tabs-mode:nil
1707   fill-column:99
1708   End:
1709 */
1710 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :