Code

disconnect mask/ref connections when item is destroyed
[inkscape.git] / src / sp-item.cpp
1 #define __SP_ITEM_C__
3 /** \file
4  * Base class for visual SVG elements
5  */
6 /*
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 2001-2005 authors
12  * Copyright (C) 2001 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 /** \class SPItem
18  *
19  * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
20  * is a subclass of SPObject, with great deal of specific functionality.
21  */
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
28 #include "sp-item.h"
29 #include "svg/svg.h"
30 #include "print.h"
31 #include "display/nr-arena.h"
32 #include "display/nr-arena-item.h"
33 #include "attributes.h"
34 #include "document.h"
35 #include "uri.h"
37 #include "style.h"
38 #include <glibmm/i18n.h>
39 #include "sp-root.h"
40 #include "sp-clippath.h"
41 #include "sp-mask.h"
42 #include "sp-rect.h"
43 #include "sp-use.h"
44 #include "sp-text.h"
45 #include "sp-item-rm-unsatisfied-cns.h"
46 #include "sp-pattern.h"
47 #include "sp-switch.h"
48 #include "gradient-chemistry.h"
49 #include "prefs-utils.h"
50 #include "conn-avoid-ref.h"
51 #include "conditions.h"
53 #include "libnr/nr-matrix-div.h"
54 #include "libnr/nr-matrix-fns.h"
55 #include "libnr/nr-matrix-scale-ops.h"
56 #include "libnr/nr-matrix-translate-ops.h"
57 #include "libnr/nr-scale-translate-ops.h"
58 #include "libnr/nr-translate-scale-ops.h"
59 #include "algorithms/find-last-if.h"
60 #include "util/reverse-list.h"
62 #include "xml/repr.h"
64 #define noSP_ITEM_DEBUG_IDLE
66 static void sp_item_class_init(SPItemClass *klass);
67 static void sp_item_init(SPItem *item);
69 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
70 static void sp_item_release(SPObject *object);
71 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
72 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
73 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
75 static gchar *sp_item_private_description(SPItem *item);
76 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
78 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
79 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
81 static SPObjectClass *parent_class;
83 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
84 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
86 /**
87  * Registers SPItem class and returns its type number.
88  */
89 GType
90 sp_item_get_type(void)
91 {
92     static GType type = 0;
93     if (!type) {
94         GTypeInfo info = {
95             sizeof(SPItemClass),
96             NULL, NULL,
97             (GClassInitFunc) sp_item_class_init,
98             NULL, NULL,
99             sizeof(SPItem),
100             16,
101             (GInstanceInitFunc) sp_item_init,
102             NULL,   /* value_table */
103         };
104         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
105     }
106     return type;
109 /**
110  * SPItem vtable initialization.
111  */
112 static void
113 sp_item_class_init(SPItemClass *klass)
115     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
117     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
119     sp_object_class->build = sp_item_build;
120     sp_object_class->release = sp_item_release;
121     sp_object_class->set = sp_item_set;
122     sp_object_class->update = sp_item_update;
123     sp_object_class->write = sp_item_write;
125     klass->description = sp_item_private_description;
126     klass->snappoints = sp_item_private_snappoints;
129 /**
130  * Callback for SPItem object initialization.
131  */
132 static void
133 sp_item_init(SPItem *item)
135     item->init();
138 void SPItem::init() {
139     this->sensitive = TRUE;
141     this->transform_center_x = 0;
142     this->transform_center_y = 0;
144     this->_is_evaluated = true;
145     this->_evaluated_status = StatusUnknown;
147     this->transform = NR::identity();
149     this->display = NULL;
151     this->clip_ref = new SPClipPathReference(this);
152                 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
153                 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
154     _clip_ref_connection = cs1.connect(sl1);
156     this->mask_ref = new SPMaskReference(this);
157                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
158                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
159     _mask_ref_connection = cs2.connect(sl2);
161     if (!this->style) this->style = sp_style_new_from_object(this);
163     this->avoidRef = new SPAvoidRef(this);
165     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
168 bool SPItem::isVisibleAndUnlocked() const {
169     return (!isHidden() && !isLocked());
172 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
173     return (!isHidden(display_key) && !isLocked());
176 bool SPItem::isLocked() const {
177     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
178         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
179             return true;
180     }
181     return false;
184 void SPItem::setLocked(bool locked) {
185     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
186                      ( locked ? "1" : NULL ));
187     updateRepr();
190 bool SPItem::isHidden() const {
191     if (!isEvaluated())
192         return true;
193     return style->display.computed == SP_CSS_DISPLAY_NONE;
196 void SPItem::setHidden(bool hide) {
197     style->display.set = TRUE;
198     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
199     style->display.computed = style->display.value;
200     style->display.inherit = FALSE;
201     updateRepr();
204 bool SPItem::isHidden(unsigned display_key) const {
205     if (!isEvaluated())
206         return true;
207     for ( SPItemView *view(display) ; view ; view = view->next ) {
208         if ( view->key == display_key ) {
209             g_assert(view->arenaitem != NULL);
210             for ( NRArenaItem *arenaitem = view->arenaitem ;
211                   arenaitem ; arenaitem = arenaitem->parent )
212             {
213                 if (!arenaitem->visible) {
214                     return true;
215                 }
216             }
217             return false;
218         }
219     }
220     return true;
223 void SPItem::setEvaluated(bool evaluated) {
224     _is_evaluated = evaluated;
225     _evaluated_status = StatusSet;
228 void SPItem::resetEvaluated() {
229     if ( StatusCalculated == _evaluated_status ) {
230         _evaluated_status = StatusUnknown;
231         bool oldValue = _is_evaluated;
232         if ( oldValue != isEvaluated() ) {
233             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
234         }
235     } if ( StatusSet == _evaluated_status ) {
236         SPObject const *const parent = SP_OBJECT_PARENT(this);
237         if (SP_IS_SWITCH(parent)) {
238             SP_SWITCH(parent)->resetChildEvaluated();
239         }
240     }
243 bool SPItem::isEvaluated() const {
244     if ( StatusUnknown == _evaluated_status ) {
245         _is_evaluated = sp_item_evaluate(this);
246         _evaluated_status = StatusCalculated;
247     }
248     return _is_evaluated;
251 /**
252  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
253  *  Corresponds to setExplicitlyHidden.
254  */
255 bool
256 SPItem::isExplicitlyHidden() const
258     return (this->style->display.set
259             && this->style->display.value == SP_CSS_DISPLAY_NONE);
262 /**
263  * Sets the display CSS property to `hidden' if \a val is true,
264  * otherwise makes it unset
265  */
266 void
267 SPItem::setExplicitlyHidden(bool const val) {
268     this->style->display.set = val;
269     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
270     this->style->display.computed = this->style->display.value;
271     this->updateRepr();
274 /**
275  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
276  */
277 void
278 SPItem::setCenter(NR::Point object_centre) {
279     NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
280     if (!bbox.isEmpty()) {
281         transform_center_x = object_centre[NR::X] - bbox.midpoint()[NR::X];
282         if (fabs(transform_center_x) < 1e-5) // rounding error
283             transform_center_x = 0;
284         transform_center_y = object_centre[NR::Y] - bbox.midpoint()[NR::Y];
285         if (fabs(transform_center_y) < 1e-5) // rounding error
286             transform_center_y = 0;
287     }
290 void
291 SPItem::unsetCenter() {
292     transform_center_x = 0;
293     transform_center_y = 0;
296 bool SPItem::isCenterSet() {
297     return (transform_center_x != 0 || transform_center_y != 0);
300 NR::Point SPItem::getCenter() {
301     NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
302     if (!bbox.isEmpty()) {
303         return bbox.midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
304     } else {
305         return NR::Point (0, 0); // something's wrong!
306     }
310 namespace {
312 bool is_item(SPObject const &object) {
313     return SP_IS_ITEM(&object);
318 void SPItem::raiseToTop() {
319     using Inkscape::Algorithms::find_last_if;
321     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
322         SP_OBJECT_NEXT(this), NULL, &is_item
323     );
324     if (topmost) {
325         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
326         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
327     }
330 void SPItem::raiseOne() {
331     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
332         SP_OBJECT_NEXT(this), NULL, &is_item
333     );
334     if (next_higher) {
335         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
336         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
337         sp_repr_parent(repr)->changeOrder(repr, ref);
338     }
341 void SPItem::lowerOne() {
342     using Inkscape::Util::MutableList;
343     using Inkscape::Util::reverse_list;
345     MutableList<SPObject &> next_lower=std::find_if(
346         reverse_list<SPObject::SiblingIterator>(
347             SP_OBJECT_PARENT(this)->firstChild(), this
348         ),
349         MutableList<SPObject &>(),
350         &is_item
351     );
352     if (next_lower) {
353         ++next_lower;
354         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
355         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
356         sp_repr_parent(repr)->changeOrder(repr, ref);
357     }
360 void SPItem::lowerToBottom() {
361     using Inkscape::Algorithms::find_last_if;
362     using Inkscape::Util::MutableList;
363     using Inkscape::Util::reverse_list;
365     MutableList<SPObject &> bottom=find_last_if(
366         reverse_list<SPObject::SiblingIterator>(
367             SP_OBJECT_PARENT(this)->firstChild(), this
368         ),
369         MutableList<SPObject &>(),
370         &is_item
371     );
372     if (bottom) {
373         ++bottom;
374         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
375         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
376         sp_repr_parent(repr)->changeOrder(repr, ref);
377     }
380 static void
381 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
383     sp_object_read_attr(object, "style");
384     sp_object_read_attr(object, "transform");
385     sp_object_read_attr(object, "clip-path");
386     sp_object_read_attr(object, "mask");
387     sp_object_read_attr(object, "sodipodi:insensitive");
388     sp_object_read_attr(object, "sodipodi:nonprintable");
389     sp_object_read_attr(object, "inkscape:transform-center-x");
390     sp_object_read_attr(object, "inkscape:transform-center-y");
391     sp_object_read_attr(object, "inkscape:connector-avoid");
393     if (((SPObjectClass *) (parent_class))->build) {
394         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
395     }
398 static void
399 sp_item_release(SPObject *object)
401     SPItem *item = (SPItem *) object;
403     item->_clip_ref_connection.disconnect();
404     item->_mask_ref_connection.disconnect();
406     if (item->clip_ref) {
407         item->clip_ref->detach();
408         delete item->clip_ref;
409         item->clip_ref = NULL;
410     }
412     if (item->mask_ref) {
413         item->mask_ref->detach();
414         delete item->mask_ref;
415         item->mask_ref = NULL;
416     }
418     if (item->avoidRef) {
419         delete item->avoidRef;
420         item->avoidRef = NULL;
421     }
423     if (((SPObjectClass *) (parent_class))->release) {
424         ((SPObjectClass *) parent_class)->release(object);
425     }
427     while (item->display) {
428         nr_arena_item_unparent(item->display->arenaitem);
429         item->display = sp_item_view_list_remove(item->display, item->display);
430     }
432     item->_transformed_signal.~signal();
435 static void
436 sp_item_set(SPObject *object, unsigned key, gchar const *value)
438     SPItem *item = (SPItem *) object;
440     switch (key) {
441         case SP_ATTR_TRANSFORM: {
442             NR::Matrix t;
443             if (value && sp_svg_transform_read(value, &t)) {
444                 sp_item_set_item_transform(item, t);
445             } else {
446                 sp_item_set_item_transform(item, NR::identity());
447             }
448             break;
449         }
450         case SP_PROP_CLIP_PATH: {
451             gchar *uri = Inkscape::parse_css_url(value);
452             if (uri) {
453                 try {
454                     item->clip_ref->attach(Inkscape::URI(uri));
455                 } catch (Inkscape::BadURIException &e) {
456                     g_warning("%s", e.what());
457                     item->clip_ref->detach();
458                 }
459                 g_free(uri);
460             } else {
461                 item->clip_ref->detach();
462             }
464             break;
465         }
466         case SP_PROP_MASK: {
467             gchar *uri=Inkscape::parse_css_url(value);
468             if (uri) {
469                 try {
470                     item->mask_ref->attach(Inkscape::URI(uri));
471                 } catch (Inkscape::BadURIException &e) {
472                     g_warning("%s", e.what());
473                     item->mask_ref->detach();
474                 }
475                 g_free(uri);
476             } else {
477                 item->mask_ref->detach();
478             }
480             break;
481         }
482         case SP_ATTR_SODIPODI_INSENSITIVE:
483             item->sensitive = !value;
484             for (SPItemView *v = item->display; v != NULL; v = v->next) {
485                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
486             }
487             break;
488         case SP_ATTR_STYLE:
489             sp_style_read_from_object(object->style, object);
490             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
491             break;
492         case SP_ATTR_CONNECTOR_AVOID:
493             item->avoidRef->setAvoid(value);
494             break;
495         case SP_ATTR_TRANSFORM_CENTER_X:
496             if (value) {
497                 item->transform_center_x = g_strtod(value, NULL);
498             } else {
499                 item->transform_center_x = 0;
500             }
501             break;
502         case SP_ATTR_TRANSFORM_CENTER_Y:
503             if (value) {
504                 item->transform_center_y = g_strtod(value, NULL);
505             } else {
506                 item->transform_center_y = 0;
507             }
508             break;
509         case SP_PROP_SYSTEM_LANGUAGE:
510         case SP_PROP_REQUIRED_FEATURES:
511         case SP_PROP_REQUIRED_EXTENSIONS:
512             {
513                 item->resetEvaluated();
514                 // pass to default handler
515             }
516         default:
517             if (SP_ATTRIBUTE_IS_CSS(key)) {
518                 sp_style_read_from_object(object->style, object);
519                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
520             } else {
521                 if (((SPObjectClass *) (parent_class))->set) {
522                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
523                 }
524             }
525             break;
526     }
529 static void
530 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
532     if (old_clip) {
533         SPItemView *v;
534         /* Hide clippath */
535         for (v = item->display; v != NULL; v = v->next) {
536             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
537             nr_arena_item_set_clip(v->arenaitem, NULL);
538         }
539     }
540     if (SP_IS_CLIPPATH(clip)) {
541         NRRect bbox;
542         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
543         for (SPItemView *v = item->display; v != NULL; v = v->next) {
544             if (!v->arenaitem->key) {
545                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
546             }
547             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
548                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
549                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
550             nr_arena_item_set_clip(v->arenaitem, ai);
551             nr_arena_item_unref(ai);
552             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
553             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
554         }
555     }
558 static void
559 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
561     if (old_mask) {
562         /* Hide mask */
563         for (SPItemView *v = item->display; v != NULL; v = v->next) {
564             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
565             nr_arena_item_set_mask(v->arenaitem, NULL);
566         }
567     }
568     if (SP_IS_MASK(mask)) {
569         NRRect bbox;
570         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
571         for (SPItemView *v = item->display; v != NULL; v = v->next) {
572             if (!v->arenaitem->key) {
573                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
574             }
575             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
576                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
577                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
578             nr_arena_item_set_mask(v->arenaitem, ai);
579             nr_arena_item_unref(ai);
580             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
581             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
582         }
583     }
586 static void
587 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
589     SPItem *item = SP_ITEM(object);
591     if (((SPObjectClass *) (parent_class))->update)
592         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
594     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
595         if (flags & SP_OBJECT_MODIFIED_FLAG) {
596             for (SPItemView *v = item->display; v != NULL; v = v->next) {
597                 nr_arena_item_set_transform(v->arenaitem, item->transform);
598             }
599         }
601         SPClipPath *clip_path = item->clip_ref->getObject();
602         SPMask *mask = item->mask_ref->getObject();
604         if ( clip_path || mask ) {
605             NRRect bbox;
606             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
607             if (clip_path) {
608                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
609                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
610                 }
611             }
612             if (mask) {
613                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
614                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
615                 }
616             }
617         }
619         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
620             for (SPItemView *v = item->display; v != NULL; v = v->next) {
621                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
622                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
623             }
624         }
625     }
627     // Update libavoid with item geometry (for connector routing).
628     item->avoidRef->handleSettingChange();
631 static Inkscape::XML::Node *
632 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
634     SPItem *item = SP_ITEM(object);
636     gchar c[256];
637     if (sp_svg_transform_write(c, 256, item->transform)) {
638         repr->setAttribute("transform", c);
639     } else {
640         repr->setAttribute("transform", NULL);
641     }
643     SPObject const *const parent = SP_OBJECT_PARENT(object);
644     /** \todo Can someone please document why this is conditional on having
645      * a parent? The only parentless thing I can think of is the top-level
646      * <svg> element (SPRoot). SPRoot is derived from SPGroup, and can have
647      * style.  I haven't looked at callers.
648      */
649     if (parent) {
650         SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
651         if (obj_style) {
652             gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
653             repr->setAttribute("style", ( *s ? s : NULL ));
654             g_free(s);
655         } else {
656             /** \todo I'm not sure what to do in this case.  Bug #1165868
657              * suggests that it can arise, but the submitter doesn't know
658              * how to do so reliably.  The main two options are either
659              * leave repr's style attribute unchanged, or explicitly clear it.
660              * Must also consider what to do with property attributes for
661              * the element; see below.
662              */
663             char const *style_str = repr->attribute("style");
664             if (!style_str) {
665                 style_str = "NULL";
666             }
667             g_warning("Item's style is NULL; repr style attribute is %s", style_str);
668         }
670         /** \note We treat object->style as authoritative.  Its effects have
671          * been written to the style attribute above; any properties that are
672          * unset we take to be deliberately unset (e.g. so that clones can
673          * override the property).
674          *
675          * Note that the below has an undesirable consequence of changing the
676          * appearance on renderers that lack CSS support (e.g. SVG tiny);
677          * possibly we should write property attributes instead of a style
678          * attribute.
679          */
680         sp_style_unset_property_attrs (object);
681     }
683     if (flags & SP_OBJECT_WRITE_EXT) {
684         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
685         if (item->transform_center_x != 0)
686             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
687         else
688             repr->setAttribute ("inkscape:transform-center-x", NULL);
689         if (item->transform_center_y != 0)
690             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
691         else
692             repr->setAttribute ("inkscape:transform-center-y", NULL);
693     }
695     if (item->clip_ref->getObject()) {
696         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
697         repr->setAttribute ("clip-path", value);
698         g_free ((void *) value);
699     }
700     if (item->mask_ref->getObject()) {
701         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
702         repr->setAttribute ("mask", value);
703         g_free ((void *) value);
704     }
706     if (((SPObjectClass *) (parent_class))->write) {
707         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
708     }
710     return repr;
713 NR::Rect SPItem::invokeBbox(NR::Matrix const &transform) const
715     NRRect r;
716     sp_item_invoke_bbox_full(this, &r, transform, 0, TRUE);
717     return NR::Rect(r);
720 void
721 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear)
723     sp_item_invoke_bbox_full(item, bbox, transform, 0, clear);
726 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
727  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
728  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
729  * clones), in turn, call this function in their bbox methods. */
730 void
731 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
733     g_assert(item != NULL);
734     g_assert(SP_IS_ITEM(item));
735     g_assert(bbox != NULL);
737     if (clear) {
738         bbox->x0 = bbox->y0 = 1e18;
739         bbox->x1 = bbox->y1 = -1e18;
740     }
742     NRRect this_bbox;
743     this_bbox.x0 = this_bbox.y0 = 1e18;
744     this_bbox.x1 = this_bbox.y1 = -1e18;
746     // call the subclass method
747     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
748         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
749     }
751     // crop the bbox by clip path, if any
752     if (item->clip_ref->getObject()) {
753         NRRect b;
754         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
755         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
756     }
758     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
759     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
760         nr_rect_d_union (bbox, bbox, &this_bbox);
761     }
764 unsigned sp_item_pos_in_parent(SPItem *item)
766     g_assert(item != NULL);
767     g_assert(SP_IS_ITEM(item));
769     SPObject *parent = SP_OBJECT_PARENT(item);
770     g_assert(parent != NULL);
771     g_assert(SP_IS_OBJECT(parent));
773     SPObject *object = SP_OBJECT(item);
775     unsigned pos=0;
776     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
777         if ( iter == object ) {
778             return pos;
779         }
780         if (SP_IS_ITEM(iter)) {
781             pos++;
782         }
783     }
785     g_assert_not_reached();
786     return 0;
789 void
790 sp_item_bbox_desktop(SPItem *item, NRRect *bbox)
792     g_assert(item != NULL);
793     g_assert(SP_IS_ITEM(item));
794     g_assert(bbox != NULL);
796     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE);
799 NR::Rect sp_item_bbox_desktop(SPItem *item)
801     NRRect ret;
802     sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE);
803     return NR::Rect(ret);
806 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
808     NR::Rect const bbox = item->invokeBbox(sp_item_i2d_affine(item));
809     /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
810        support angled guide lines. */
812     *p = bbox.min();
813     *p = bbox.max();
816 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
818     g_assert (item != NULL);
819     g_assert (SP_IS_ITEM(item));
821     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
822     if (item_class.snappoints) {
823         item_class.snappoints(item, p);
824     }
827 void
828 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
830     if (!item->isHidden()) {
831         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
832             if (!item->transform.test_identity()
833                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
834             {
835                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
836                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
837                 sp_print_release(ctx);
838             } else {
839                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
840             }
841         }
842     }
845 static gchar *
846 sp_item_private_description(SPItem *item)
848     return g_strdup(_("Object"));
851 /**
852  * Returns a string suitable for status bar, formatted in pango markup language.
853  *
854  * Must be freed by caller.
855  */
856 gchar *
857 sp_item_description(SPItem *item)
859     g_assert(item != NULL);
860     g_assert(SP_IS_ITEM(item));
862     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
863         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
864         if (s && item->clip_ref->getObject()) {
865             gchar *snew = g_strdup_printf ("%s; <i>clipped</i>", s);
866             g_free (s);
867             s = snew;
868         }
869         if (s && item->mask_ref->getObject()) {
870             gchar *snew = g_strdup_printf ("%s; <i>masked</i>", s);
871             g_free (s);
872             s = snew;
873         }
874         return s;
875     }
877     g_assert_not_reached();
878     return NULL;
881 /**
882  * Allocates unique integer keys.
883  * \param numkeys Number of keys required.
884  * \return First allocated key; hence if the returned key is n
885  * you can use n, n + 1, ..., n + (numkeys - 1)
886  */
887 unsigned
888 sp_item_display_key_new(unsigned numkeys)
890     static unsigned dkey = 0;
892     dkey += numkeys;
894     return dkey - numkeys;
897 NRArenaItem *
898 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
900     g_assert(item != NULL);
901     g_assert(SP_IS_ITEM(item));
902     g_assert(arena != NULL);
903     g_assert(NR_IS_ARENA(arena));
905     NRArenaItem *ai = NULL;
906     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
907         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
908     }
910     if (ai != NULL) {
911         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
912         nr_arena_item_set_transform(ai, item->transform);
913         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
914         nr_arena_item_set_visible(ai, !item->isHidden());
915         nr_arena_item_set_sensitive(ai, item->sensitive);
916         if (item->clip_ref->getObject()) {
917             SPClipPath *cp = item->clip_ref->getObject();
919             if (!item->display->arenaitem->key) {
920                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
921             }
922             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
924             // Show and set clip
925             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
926             nr_arena_item_set_clip(ai, ac);
927             nr_arena_item_unref(ac);
929             // Update bbox, in case the clip uses bbox units
930             NRRect bbox;
931             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
932             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
933             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
934         }
935         if (item->mask_ref->getObject()) {
936             SPMask *mask = item->mask_ref->getObject();
938             if (!item->display->arenaitem->key) {
939                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
940             }
941             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
943             // Show and set mask
944             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
945             nr_arena_item_set_mask(ai, ac);
946             nr_arena_item_unref(ac);
948             // Update bbox, in case the mask uses bbox units
949             NRRect bbox;
950             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
951             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
952             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
953         }
954         NR_ARENA_ITEM_SET_DATA(ai, item);
955     }
957     return ai;
960 void
961 sp_item_invoke_hide(SPItem *item, unsigned key)
963     g_assert(item != NULL);
964     g_assert(SP_IS_ITEM(item));
966     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
967         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
968     }
970     SPItemView *ref = NULL;
971     SPItemView *v = item->display;
972     while (v != NULL) {
973         SPItemView *next = v->next;
974         if (v->key == key) {
975             if (item->clip_ref->getObject()) {
976                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
977                 nr_arena_item_set_clip(v->arenaitem, NULL);
978             }
979             if (item->mask_ref->getObject()) {
980                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
981                 nr_arena_item_set_mask(v->arenaitem, NULL);
982             }
983             if (!ref) {
984                 item->display = v->next;
985             } else {
986                 ref->next = v->next;
987             }
988             nr_arena_item_unparent(v->arenaitem);
989             nr_arena_item_unref(v->arenaitem);
990             g_free(v);
991         } else {
992             ref = v;
993         }
994         v = next;
995     }
998 // Adjusters
1000 void
1001 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1003     SPStyle *style = SP_OBJECT_STYLE (item);
1005     if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1006         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1007         if (SP_IS_PATTERN (server)) {
1008             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1009             sp_pattern_transform_multiply (pattern, postmul, set);
1010         }
1011     }
1013     if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1014         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1015         if (SP_IS_PATTERN (server)) {
1016             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1017             sp_pattern_transform_multiply (pattern, postmul, set);
1018         }
1019     }
1023 void
1024 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1026     SPStyle *style = SP_OBJECT_STYLE (item);
1028     if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1029         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1030         if (SP_IS_GRADIENT (server)) {
1032             /**
1033              * \note Bbox units for a gradient are generally a bad idea because
1034              * with them, you cannot preserve the relative position of the
1035              * object and its gradient after rotation or skew. So now we
1036              * convert them to userspace units which are easy to keep in sync
1037              * just by adding the object's transform to gradientTransform.
1038              * \todo FIXME: convert back to bbox units after transforming with
1039              * the item, so as to preserve the original units.
1040              */
1041             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1043             sp_gradient_transform_multiply (gradient, postmul, set);
1044         }
1045     }
1047     if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1048         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1049         if (SP_IS_GRADIENT (server)) {
1050             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1051             sp_gradient_transform_multiply (gradient, postmul, set);
1052         }
1053     }
1056 void
1057 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1059     SPStyle *style = SP_OBJECT_STYLE (item);
1061     if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1063         style->stroke_width.computed *= ex;
1064         style->stroke_width.set = TRUE;
1066         if (style->stroke_dash.n_dash != 0) {
1067             int i;
1068             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1069                 style->stroke_dash.dash[i] *= ex;
1070             }
1071             style->stroke_dash.offset *= ex;
1072         }
1074         SP_OBJECT(item)->updateRepr();
1075     }
1078 /**
1079  * Find out the inverse of previous transform of an item (from its repr)
1080  */
1081 NR::Matrix
1082 sp_item_transform_repr (SPItem *item)
1084     NR::Matrix t_old(NR::identity());
1085     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1086     if (t_attr) {
1087         NR::Matrix t;
1088         if (sp_svg_transform_read(t_attr, &t)) {
1089             t_old = t;
1090         }
1091     }
1093     return t_old;
1097 /**
1098  * Recursively scale stroke width in \a item and its children by \a expansion.
1099  */
1100 void
1101 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1103     sp_item_adjust_stroke (item, expansion);
1105     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1106         if (SP_IS_ITEM(o))
1107             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1108     }
1111 /**
1112  * Recursively adjust rx and ry of rects.
1113  */
1114 void
1115 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1117     if (SP_IS_RECT (item)) {
1118         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1119     }
1121     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1122         if (SP_IS_ITEM(o))
1123             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1124     }
1127 /**
1128  * Recursively compensate pattern or gradient transform.
1129  */
1130 void
1131 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1133 // A clone must not touch the style (it's that of its parent!) and has no children, so quit now
1134     if (item && SP_IS_USE(item))
1135         return;
1137 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1138 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1139 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1140     NR::Matrix t_item = sp_item_transform_repr (item);
1141     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1143     if (is_pattern)
1144         sp_item_adjust_pattern (item, paint_delta);
1145     else
1146         sp_item_adjust_gradient (item, paint_delta);
1148 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation
1149     if (item && SP_IS_TEXT(item))
1150         return;
1152     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1153         if (SP_IS_ITEM(o)) {
1154 // At the level of the transformed item, t_ancestors is identity;
1155 // below it, it is the accmmulated chain of transforms from this level to the top level
1156             sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1157         }
1158     }
1161 /**
1162  * A temporary wrapper for the next function accepting the NRMatrix
1163  * instead of NR::Matrix
1164  */
1165 void
1166 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1168     if (transform == NULL)
1169         sp_item_write_transform(item, repr, NR::identity(), adv);
1170     else
1171         sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1174 /**
1175  * Set a new transform on an object.
1176  *
1177  * Compensate for stroke scaling and gradient/pattern fill transform, if
1178  * necessary. Call the object's set_transform method if transforms are
1179  * stored optimized. Send _transformed_signal. Invoke _write method so that
1180  * the repr is updated with the new transform.
1181  */
1182 void
1183 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv)
1185     g_return_if_fail(item != NULL);
1186     g_return_if_fail(SP_IS_ITEM(item));
1187     g_return_if_fail(repr != NULL);
1189     // calculate the relative transform, if not given by the adv attribute
1190     NR::Matrix advertized_transform;
1191     if (adv != NULL) {
1192         advertized_transform = *adv;
1193     } else {
1194         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1195     }
1197      // recursively compensate for stroke scaling, depending on user preference
1198     if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1199         double const expansion = 1. / NR::expansion(advertized_transform);
1200         sp_item_adjust_stroke_width_recursive(item, expansion);
1201     }
1203     // recursively compensate rx/ry of a rect if requested
1204     if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1205         sp_item_adjust_rects_recursive(item, advertized_transform);
1206     }
1208     // recursively compensate pattern fill if it's not to be transformed
1209     if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1210         sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1211     }
1212     /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1213     /// recursively compensate gradient fill if it's not to be transformed
1214     if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1215         sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1216     } else {
1217         // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1218         // it here _before_ the new transform is set, so as to use the pre-transform bbox
1219         sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1220     }
1222     // run the object's set_transform if transforms are stored optimized and there's no clippath or mask
1223     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1224     NR::Matrix transform_attr (transform);
1225     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform 
1226                 && !preserve && !item->clip_ref->getObject() && !item->mask_ref->getObject()) {
1227         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1228     }
1229     sp_item_set_item_transform(item, transform_attr);
1231     // Note: updateRepr comes before emitting the transformed signal since
1232     // it causes clone SPUse's copy of the original object to brought up to
1233     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1234     // values if called in code handling the transformed signal.
1235     SP_OBJECT(item)->updateRepr();
1237     // send the relative transform with a _transformed_signal
1238     item->_transformed_signal.emit(&advertized_transform, item);
1241 gint
1242 sp_item_event(SPItem *item, SPEvent *event)
1244     g_return_val_if_fail(item != NULL, FALSE);
1245     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1246     g_return_val_if_fail(event != NULL, FALSE);
1248     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1249         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1251     return FALSE;
1254 /**
1255  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1256  * gradients, patterns as sp_item_write_transform does.
1257  */
1258 void
1259 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1261     g_return_if_fail(item != NULL);
1262     g_return_if_fail(SP_IS_ITEM(item));
1264     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1265         item->transform = transform;
1266         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1267            transformation.  It's apparently not used anywhere else. */
1268         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1269         sp_item_rm_unsatisfied_cns(*item);
1270     }
1274 /**
1275  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1276  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1277  */
1278 NR::Matrix
1279 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1280     NR::Matrix ret(NR::identity());
1281     g_return_val_if_fail(object != NULL, ret);
1283     /* stop at first non-renderable ancestor */
1284     while ( object != ancestor && SP_IS_ITEM(object) ) {
1285         if (SP_IS_ROOT(object)) {
1286             ret *= SP_ROOT(object)->c2p;
1287         }
1288         ret *= SP_ITEM(object)->transform;
1289         object = SP_OBJECT_PARENT(object);
1290     }
1291     return ret;
1294 NR::Matrix
1295 i2i_affine(SPObject const *src, SPObject const *dest) {
1296     g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1297     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1298     return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1301 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1302     return i2i_affine(this, dest);
1305 /**
1306  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1307  * \pre (item != NULL) and SP_IS_ITEM(item).
1308  */
1309 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1311     return i2anc_affine(item, NULL);
1314 /**
1315  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1316  * Used in path operations mostly.
1317  * \pre (item != NULL) and SP_IS_ITEM(item).
1318  */
1319 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1321     g_assert(item != NULL);
1322     g_assert(SP_IS_ITEM(item));
1324     NR::Matrix ret(NR::identity());
1325     g_assert(ret.test_identity());
1326     while ( NULL != SP_OBJECT_PARENT(item) ) {
1327         ret *= item->transform;
1328         item = SP_ITEM(SP_OBJECT_PARENT(item));
1329     }
1330     g_assert(SP_IS_ROOT(item));
1332     ret *= item->transform;
1334     return ret;
1337 /* fixme: This is EVIL!!! */
1339 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1341     g_assert(item != NULL);
1342     g_assert(SP_IS_ITEM(item));
1344     NR::Matrix const ret( sp_item_i2doc_affine(item)
1345                           * NR::scale(1, -1)
1346                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1347     return ret;
1350 // same as i2d but with i2root instead of i2doc
1351 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1353     g_assert(item != NULL);
1354     g_assert(SP_IS_ITEM(item));
1356     NR::Matrix const ret( sp_item_i2root_affine(item)
1357                           * NR::scale(1, -1)
1358                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1359     return ret;
1362 /**
1363  * Converts a matrix \a m into the desktop coords of the \a item.
1364  * Will become a noop when we eliminate the coordinate flipping.
1365  */
1366 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1368     NR::Matrix const ret(m
1369                          * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1370                          * NR::scale(1, -1));
1371     return ret;
1374 /**
1375  * Converts a matrix \a m from the desktop coords of the \a item.
1376  * Will become a noop when we eliminate the coordinate flipping.
1377  */
1378 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1380     NR::Matrix const ret(NR::scale(1, -1)
1381                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1382                          * m);
1383     return ret;
1386 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1388     g_return_if_fail( item != NULL );
1389     g_return_if_fail( SP_IS_ITEM(item) );
1391     NR::Matrix dt2p; /* desktop to item parent transform */
1392     if (SP_OBJECT_PARENT(item)) {
1393         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1394     } else {
1395         dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1396                  * NR::scale(1, -1) );
1397     }
1399     NR::Matrix const i2p( i2dt * dt2p );
1400     sp_item_set_item_transform(item, i2p);
1404 NR::Matrix
1405 sp_item_dt2i_affine(SPItem const *item)
1407     /* fixme: Implement the right way (Lauris) */
1408     return sp_item_i2d_affine(item).inverse();
1411 /* Item views */
1413 static SPItemView *
1414 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1416     SPItemView *new_view;
1418     g_assert(item != NULL);
1419     g_assert(SP_IS_ITEM(item));
1420     g_assert(arenaitem != NULL);
1421     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1423     new_view = g_new(SPItemView, 1);
1425     new_view->next = list;
1426     new_view->flags = flags;
1427     new_view->key = key;
1428     new_view->arenaitem = nr_arena_item_ref(arenaitem);
1430     return new_view;
1433 static SPItemView *
1434 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1436     if (view == list) {
1437         list = list->next;
1438     } else {
1439         SPItemView *prev;
1440         prev = list;
1441         while (prev->next != view) prev = prev->next;
1442         prev->next = view->next;
1443     }
1445     nr_arena_item_unref(view->arenaitem);
1446     g_free(view);
1448     return list;
1451 /**
1452  * Return the arenaitem corresponding to the given item in the display
1453  * with the given key
1454  */
1455 NRArenaItem *
1456 sp_item_get_arenaitem(SPItem *item, unsigned key)
1458     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1459         if ( iv->key == key ) {
1460             return iv->arenaitem;
1461         }
1462     }
1464     return NULL;
1467 int
1468 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1470     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1471                                     SP_OBJECT_REPR(second));
1474 SPItem *
1475 sp_item_first_item_child (SPObject *obj)
1477     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1478         if (SP_IS_ITEM (iter))
1479             return SP_ITEM (iter);
1480     }
1481     return NULL;
1485 /*
1486   Local Variables:
1487   mode:c++
1488   c-file-style:"stroustrup"
1489   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1490   indent-tabs-mode:nil
1491   fill-column:99
1492   End:
1493 */
1494 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :