Code

o removed the unused cache as it was just wasting time and space
[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                 {
153                         sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
154                         sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
155                         cs1.connect(sl1);
156                 }
158     this->mask_ref = new SPMaskReference(this);
159                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
160                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
161     cs2.connect(sl2);
163     if (!this->style) this->style = sp_style_new_from_object(this);
165     this->avoidRef = new SPAvoidRef(this);
167     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
170 bool SPItem::isVisibleAndUnlocked() const {
171     return (!isHidden() && !isLocked());
174 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
175     return (!isHidden(display_key) && !isLocked());
178 bool SPItem::isLocked() const {
179     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
180         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
181             return true;
182     }
183     return false;
186 void SPItem::setLocked(bool locked) {
187     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
188                      ( locked ? "1" : NULL ));
189     updateRepr();
192 bool SPItem::isHidden() const {
193     if (!isEvaluated())
194         return true;
195     return style->display.computed == SP_CSS_DISPLAY_NONE;
198 void SPItem::setHidden(bool hide) {
199     style->display.set = TRUE;
200     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
201     style->display.computed = style->display.value;
202     style->display.inherit = FALSE;
203     updateRepr();
206 bool SPItem::isHidden(unsigned display_key) const {
207     if (!isEvaluated())
208         return true;
209     for ( SPItemView *view(display) ; view ; view = view->next ) {
210         if ( view->key == display_key ) {
211             g_assert(view->arenaitem != NULL);
212             for ( NRArenaItem *arenaitem = view->arenaitem ;
213                   arenaitem ; arenaitem = arenaitem->parent )
214             {
215                 if (!arenaitem->visible) {
216                     return true;
217                 }
218             }
219             return false;
220         }
221     }
222     return true;
225 void SPItem::setEvaluated(bool evaluated) {
226     _is_evaluated = evaluated;
227     _evaluated_status = StatusSet;
230 void SPItem::resetEvaluated() {
231     if ( StatusCalculated == _evaluated_status ) {
232         _evaluated_status = StatusUnknown;
233         bool oldValue = _is_evaluated;
234         if ( oldValue != isEvaluated() ) {
235             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
236         }
237     } if ( StatusSet == _evaluated_status ) {
238         SPObject const *const parent = SP_OBJECT_PARENT(this);
239         if (SP_IS_SWITCH(parent)) {
240             SP_SWITCH(parent)->resetChildEvaluated();
241         }
242     }
245 bool SPItem::isEvaluated() const {
246     if ( StatusUnknown == _evaluated_status ) {
247         _is_evaluated = sp_item_evaluate(this);
248         _evaluated_status = StatusCalculated;
249     }
250     return _is_evaluated;
253 /**
254  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
255  *  Corresponds to setExplicitlyHidden.
256  */
257 bool
258 SPItem::isExplicitlyHidden() const
260     return (this->style->display.set
261             && this->style->display.value == SP_CSS_DISPLAY_NONE);
264 /**
265  * Sets the display CSS property to `hidden' if \a val is true,
266  * otherwise makes it unset
267  */
268 void
269 SPItem::setExplicitlyHidden(bool const val) {
270     this->style->display.set = val;
271     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
272     this->style->display.computed = this->style->display.value;
273     this->updateRepr();
276 /**
277  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
278  */
279 void
280 SPItem::setCenter(NR::Point object_centre) {
281     NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
282     if (!bbox.isEmpty()) {
283         transform_center_x = object_centre[NR::X] - bbox.midpoint()[NR::X];
284         if (fabs(transform_center_x) < 1e-5) // rounding error
285             transform_center_x = 0;
286         transform_center_y = object_centre[NR::Y] - bbox.midpoint()[NR::Y];
287         if (fabs(transform_center_y) < 1e-5) // rounding error
288             transform_center_y = 0;
289     }
292 void
293 SPItem::unsetCenter() {
294     transform_center_x = 0;
295     transform_center_y = 0;
298 bool SPItem::isCenterSet() {
299     return (transform_center_x != 0 || transform_center_y != 0);
302 NR::Point SPItem::getCenter() {
303     NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
304     if (!bbox.isEmpty()) {
305         return bbox.midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
306     } else {
307         return NR::Point (0, 0); // something's wrong!
308     }
312 namespace {
314 bool is_item(SPObject const &object) {
315     return SP_IS_ITEM(&object);
320 void SPItem::raiseToTop() {
321     using Inkscape::Algorithms::find_last_if;
323     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
324         SP_OBJECT_NEXT(this), NULL, &is_item
325     );
326     if (topmost) {
327         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
328         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
329     }
332 void SPItem::raiseOne() {
333     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
334         SP_OBJECT_NEXT(this), NULL, &is_item
335     );
336     if (next_higher) {
337         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
338         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
339         sp_repr_parent(repr)->changeOrder(repr, ref);
340     }
343 void SPItem::lowerOne() {
344     using Inkscape::Util::MutableList;
345     using Inkscape::Util::reverse_list;
347     MutableList<SPObject &> next_lower=std::find_if(
348         reverse_list<SPObject::SiblingIterator>(
349             SP_OBJECT_PARENT(this)->firstChild(), this
350         ),
351         MutableList<SPObject &>(),
352         &is_item
353     );
354     if (next_lower) {
355         ++next_lower;
356         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
357         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
358         sp_repr_parent(repr)->changeOrder(repr, ref);
359     }
362 void SPItem::lowerToBottom() {
363     using Inkscape::Algorithms::find_last_if;
364     using Inkscape::Util::MutableList;
365     using Inkscape::Util::reverse_list;
367     MutableList<SPObject &> bottom=find_last_if(
368         reverse_list<SPObject::SiblingIterator>(
369             SP_OBJECT_PARENT(this)->firstChild(), this
370         ),
371         MutableList<SPObject &>(),
372         &is_item
373     );
374     if (bottom) {
375         ++bottom;
376         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
377         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
378         sp_repr_parent(repr)->changeOrder(repr, ref);
379     }
382 static void
383 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
385     sp_object_read_attr(object, "style");
386     sp_object_read_attr(object, "transform");
387     sp_object_read_attr(object, "clip-path");
388     sp_object_read_attr(object, "mask");
389     sp_object_read_attr(object, "sodipodi:insensitive");
390     sp_object_read_attr(object, "sodipodi:nonprintable");
391     sp_object_read_attr(object, "inkscape:transform-center-x");
392     sp_object_read_attr(object, "inkscape:transform-center-y");
393     sp_object_read_attr(object, "inkscape:connector-avoid");
395     if (((SPObjectClass *) (parent_class))->build) {
396         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
397     }
400 static void
401 sp_item_release(SPObject *object)
403     SPItem *item = (SPItem *) object;
405     if (item->clip_ref) {
406         item->clip_ref->detach();
407         delete item->clip_ref;
408         item->clip_ref = NULL;
409     }
411     if (item->mask_ref) {
412         item->mask_ref->detach();
413         delete item->mask_ref;
414         item->mask_ref = NULL;
415     }
417     if (item->avoidRef) {
418         delete item->avoidRef;
419         item->avoidRef = NULL;
420     }
422     if (((SPObjectClass *) (parent_class))->release) {
423         ((SPObjectClass *) parent_class)->release(object);
424     }
426     while (item->display) {
427         nr_arena_item_unparent(item->display->arenaitem);
428         item->display = sp_item_view_list_remove(item->display, item->display);
429     }
431     item->_transformed_signal.~signal();
434 static void
435 sp_item_set(SPObject *object, unsigned key, gchar const *value)
437     SPItem *item = (SPItem *) object;
439     switch (key) {
440         case SP_ATTR_TRANSFORM: {
441             NR::Matrix t;
442             if (value && sp_svg_transform_read(value, &t)) {
443                 sp_item_set_item_transform(item, t);
444             } else {
445                 sp_item_set_item_transform(item, NR::identity());
446             }
447             break;
448         }
449         case SP_PROP_CLIP_PATH: {
450             gchar *uri = Inkscape::parse_css_url(value);
451             if (uri) {
452                 try {
453                     item->clip_ref->attach(Inkscape::URI(uri));
454                 } catch (Inkscape::BadURIException &e) {
455                     g_warning("%s", e.what());
456                     item->clip_ref->detach();
457                 }
458                 g_free(uri);
459             } else {
460                 item->clip_ref->detach();
461             }
463             break;
464         }
465         case SP_PROP_MASK: {
466             gchar *uri=Inkscape::parse_css_url(value);
467             if (uri) {
468                 try {
469                     item->mask_ref->attach(Inkscape::URI(uri));
470                 } catch (Inkscape::BadURIException &e) {
471                     g_warning("%s", e.what());
472                     item->mask_ref->detach();
473                 }
474                 g_free(uri);
475             } else {
476                 item->mask_ref->detach();
477             }
479             break;
480         }
481         case SP_ATTR_SODIPODI_INSENSITIVE:
482             item->sensitive = !value;
483             for (SPItemView *v = item->display; v != NULL; v = v->next) {
484                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
485             }
486             break;
487         case SP_ATTR_STYLE:
488             sp_style_read_from_object(object->style, object);
489             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
490             break;
491         case SP_ATTR_CONNECTOR_AVOID:
492             item->avoidRef->setAvoid(value);
493             break;
494         case SP_ATTR_TRANSFORM_CENTER_X:
495             if (value) {
496                 item->transform_center_x = g_strtod(value, NULL);
497             } else {
498                 item->transform_center_x = 0;
499             }
500             break;
501         case SP_ATTR_TRANSFORM_CENTER_Y:
502             if (value) {
503                 item->transform_center_y = g_strtod(value, NULL);
504             } else {
505                 item->transform_center_y = 0;
506             }
507             break;
508         case SP_PROP_SYSTEM_LANGUAGE:
509         case SP_PROP_REQUIRED_FEATURES:
510         case SP_PROP_REQUIRED_EXTENSIONS:
511             {
512                 item->resetEvaluated();
513                 // pass to default handler
514             }
515         default:
516             if (SP_ATTRIBUTE_IS_CSS(key)) {
517                 sp_style_read_from_object(object->style, object);
518                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
519             } else {
520                 if (((SPObjectClass *) (parent_class))->set) {
521                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
522                 }
523             }
524             break;
525     }
528 static void
529 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
531     if (old_clip) {
532         SPItemView *v;
533         /* Hide clippath */
534         for (v = item->display; v != NULL; v = v->next) {
535             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
536             nr_arena_item_set_clip(v->arenaitem, NULL);
537         }
538     }
539     if (SP_IS_CLIPPATH(clip)) {
540         NRRect bbox;
541         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
542         for (SPItemView *v = item->display; v != NULL; v = v->next) {
543             if (!v->arenaitem->key) {
544                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
545             }
546             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
547                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
548                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
549             nr_arena_item_set_clip(v->arenaitem, ai);
550             nr_arena_item_unref(ai);
551             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
552             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
553         }
554     }
557 static void
558 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
560     if (old_mask) {
561         /* Hide mask */
562         for (SPItemView *v = item->display; v != NULL; v = v->next) {
563             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
564             nr_arena_item_set_mask(v->arenaitem, NULL);
565         }
566     }
567     if (SP_IS_MASK(mask)) {
568         NRRect bbox;
569         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
570         for (SPItemView *v = item->display; v != NULL; v = v->next) {
571             if (!v->arenaitem->key) {
572                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
573             }
574             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
575                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
576                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
577             nr_arena_item_set_mask(v->arenaitem, ai);
578             nr_arena_item_unref(ai);
579             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
580             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
581         }
582     }
585 static void
586 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
588     SPItem *item = SP_ITEM(object);
590     if (((SPObjectClass *) (parent_class))->update)
591         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
593     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
594         if (flags & SP_OBJECT_MODIFIED_FLAG) {
595             for (SPItemView *v = item->display; v != NULL; v = v->next) {
596                 nr_arena_item_set_transform(v->arenaitem, item->transform);
597             }
598         }
600         SPClipPath *clip_path = item->clip_ref->getObject();
601         SPMask *mask = item->mask_ref->getObject();
603         if ( clip_path || mask ) {
604             NRRect bbox;
605             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
606             if (clip_path) {
607                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
608                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
609                 }
610             }
611             if (mask) {
612                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
613                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
614                 }
615             }
616         }
618         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
619             for (SPItemView *v = item->display; v != NULL; v = v->next) {
620                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
621                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
622             }
623         }
624     }
626     // Update libavoid with item geometry (for connector routing).
627     item->avoidRef->handleSettingChange();
630 static Inkscape::XML::Node *
631 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
633     SPItem *item = SP_ITEM(object);
635     gchar c[256];
636     if (sp_svg_transform_write(c, 256, item->transform)) {
637         repr->setAttribute("transform", c);
638     } else {
639         repr->setAttribute("transform", NULL);
640     }
642     SPObject const *const parent = SP_OBJECT_PARENT(object);
643     /** \todo Can someone please document why this is conditional on having
644      * a parent? The only parentless thing I can think of is the top-level
645      * <svg> element (SPRoot). SPRoot is derived from SPGroup, and can have
646      * style.  I haven't looked at callers.
647      */
648     if (parent) {
649         SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
650         if (obj_style) {
651             gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
652             repr->setAttribute("style", ( *s ? s : NULL ));
653             g_free(s);
654         } else {
655             /** \todo I'm not sure what to do in this case.  Bug #1165868
656              * suggests that it can arise, but the submitter doesn't know
657              * how to do so reliably.  The main two options are either
658              * leave repr's style attribute unchanged, or explicitly clear it.
659              * Must also consider what to do with property attributes for
660              * the element; see below.
661              */
662             char const *style_str = repr->attribute("style");
663             if (!style_str) {
664                 style_str = "NULL";
665             }
666             g_warning("Item's style is NULL; repr style attribute is %s", style_str);
667         }
669         /** \note We treat object->style as authoritative.  Its effects have
670          * been written to the style attribute above; any properties that are
671          * unset we take to be deliberately unset (e.g. so that clones can
672          * override the property).
673          *
674          * Note that the below has an undesirable consequence of changing the
675          * appearance on renderers that lack CSS support (e.g. SVG tiny);
676          * possibly we should write property attributes instead of a style
677          * attribute.
678          */
679         sp_style_unset_property_attrs (object);
680     }
682     if (flags & SP_OBJECT_WRITE_EXT) {
683         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
684         if (item->transform_center_x != 0)
685             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
686         else
687             repr->setAttribute ("inkscape:transform-center-x", NULL);
688         if (item->transform_center_y != 0)
689             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
690         else
691             repr->setAttribute ("inkscape:transform-center-y", NULL);
692     }
694     if (item->clip_ref->getObject()) {
695         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
696         repr->setAttribute ("clip-path", value);
697         g_free ((void *) value);
698     }
699     if (item->mask_ref->getObject()) {
700         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
701         repr->setAttribute ("mask", value);
702         g_free ((void *) value);
703     }
705     if (((SPObjectClass *) (parent_class))->write) {
706         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
707     }
709     return repr;
712 NR::Rect SPItem::invokeBbox(NR::Matrix const &transform) const
714     NRRect r;
715     sp_item_invoke_bbox_full(this, &r, transform, 0, TRUE);
716     return NR::Rect(r);
719 void
720 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear)
722     sp_item_invoke_bbox_full(item, bbox, transform, 0, clear);
725 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
726  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
727  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
728  * clones), in turn, call this function in their bbox methods. */
729 void
730 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
732     g_assert(item != NULL);
733     g_assert(SP_IS_ITEM(item));
734     g_assert(bbox != NULL);
736     if (clear) {
737         bbox->x0 = bbox->y0 = 1e18;
738         bbox->x1 = bbox->y1 = -1e18;
739     }
741     NRRect this_bbox;
742     this_bbox.x0 = this_bbox.y0 = 1e18;
743     this_bbox.x1 = this_bbox.y1 = -1e18;
745     // call the subclass method
746     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
747         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
748     }
750     // crop the bbox by clip path, if any
751     if (item->clip_ref->getObject()) {
752         NRRect b;
753         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
754         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
755     }
757     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
758     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
759         nr_rect_d_union (bbox, bbox, &this_bbox);
760     }
763 unsigned sp_item_pos_in_parent(SPItem *item)
765     g_assert(item != NULL);
766     g_assert(SP_IS_ITEM(item));
768     SPObject *parent = SP_OBJECT_PARENT(item);
769     g_assert(parent != NULL);
770     g_assert(SP_IS_OBJECT(parent));
772     SPObject *object = SP_OBJECT(item);
774     unsigned pos=0;
775     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
776         if ( iter == object ) {
777             return pos;
778         }
779         if (SP_IS_ITEM(iter)) {
780             pos++;
781         }
782     }
784     g_assert_not_reached();
785     return 0;
788 void
789 sp_item_bbox_desktop(SPItem *item, NRRect *bbox)
791     g_assert(item != NULL);
792     g_assert(SP_IS_ITEM(item));
793     g_assert(bbox != NULL);
795     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE);
798 NR::Rect sp_item_bbox_desktop(SPItem *item)
800     NRRect ret;
801     sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE);
802     return NR::Rect(ret);
805 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
807     NR::Rect const bbox = item->invokeBbox(sp_item_i2d_affine(item));
808     /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
809        support angled guide lines. */
811     *p = bbox.min();
812     *p = bbox.max();
815 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
817     g_assert (item != NULL);
818     g_assert (SP_IS_ITEM(item));
820     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
821     if (item_class.snappoints) {
822         item_class.snappoints(item, p);
823     }
826 void
827 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
829     if (!item->isHidden()) {
830         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
831             if (!item->transform.test_identity()
832                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
833             {
834                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
835                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
836                 sp_print_release(ctx);
837             } else {
838                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
839             }
840         }
841     }
844 static gchar *
845 sp_item_private_description(SPItem *item)
847     return g_strdup(_("Object"));
850 /**
851  * Returns a string suitable for status bar, formatted in pango markup language.
852  *
853  * Must be freed by caller.
854  */
855 gchar *
856 sp_item_description(SPItem *item)
858     g_assert(item != NULL);
859     g_assert(SP_IS_ITEM(item));
861     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
862         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
863         if (s && item->clip_ref->getObject()) {
864             gchar *snew = g_strdup_printf ("%s; <i>clipped</i>", s);
865             g_free (s);
866             s = snew;
867         }
868         if (s && item->mask_ref->getObject()) {
869             gchar *snew = g_strdup_printf ("%s; <i>masked</i>", s);
870             g_free (s);
871             s = snew;
872         }
873         return s;
874     }
876     g_assert_not_reached();
877     return NULL;
880 /**
881  * Allocates unique integer keys.
882  * \param numkeys Number of keys required.
883  * \return First allocated key; hence if the returned key is n
884  * you can use n, n + 1, ..., n + (numkeys - 1)
885  */
886 unsigned
887 sp_item_display_key_new(unsigned numkeys)
889     static unsigned dkey = 0;
891     dkey += numkeys;
893     return dkey - numkeys;
896 NRArenaItem *
897 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
899     g_assert(item != NULL);
900     g_assert(SP_IS_ITEM(item));
901     g_assert(arena != NULL);
902     g_assert(NR_IS_ARENA(arena));
904     NRArenaItem *ai = NULL;
905     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
906         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
907     }
909     if (ai != NULL) {
910         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
911         nr_arena_item_set_transform(ai, item->transform);
912         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
913         nr_arena_item_set_visible(ai, !item->isHidden());
914         nr_arena_item_set_sensitive(ai, item->sensitive);
915         if (item->clip_ref->getObject()) {
916             SPClipPath *cp = item->clip_ref->getObject();
918             if (!item->display->arenaitem->key) {
919                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
920             }
921             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
923             // Show and set clip
924             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
925             nr_arena_item_set_clip(ai, ac);
926             nr_arena_item_unref(ac);
928             // Update bbox, in case the clip uses bbox units
929             NRRect bbox;
930             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
931             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
932             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
933         }
934         if (item->mask_ref->getObject()) {
935             SPMask *mask = item->mask_ref->getObject();
937             if (!item->display->arenaitem->key) {
938                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
939             }
940             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
942             // Show and set mask
943             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
944             nr_arena_item_set_mask(ai, ac);
945             nr_arena_item_unref(ac);
947             // Update bbox, in case the mask uses bbox units
948             NRRect bbox;
949             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
950             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
951             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
952         }
953         NR_ARENA_ITEM_SET_DATA(ai, item);
954     }
956     return ai;
959 void
960 sp_item_invoke_hide(SPItem *item, unsigned key)
962     g_assert(item != NULL);
963     g_assert(SP_IS_ITEM(item));
965     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
966         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
967     }
969     SPItemView *ref = NULL;
970     SPItemView *v = item->display;
971     while (v != NULL) {
972         SPItemView *next = v->next;
973         if (v->key == key) {
974             if (item->clip_ref->getObject()) {
975                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
976                 nr_arena_item_set_clip(v->arenaitem, NULL);
977             }
978             if (item->mask_ref->getObject()) {
979                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
980                 nr_arena_item_set_mask(v->arenaitem, NULL);
981             }
982             if (!ref) {
983                 item->display = v->next;
984             } else {
985                 ref->next = v->next;
986             }
987             nr_arena_item_unparent(v->arenaitem);
988             nr_arena_item_unref(v->arenaitem);
989             g_free(v);
990         } else {
991             ref = v;
992         }
993         v = next;
994     }
997 // Adjusters
999 void
1000 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1002     SPStyle *style = SP_OBJECT_STYLE (item);
1004     if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1005         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1006         if (SP_IS_PATTERN (server)) {
1007             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1008             sp_pattern_transform_multiply (pattern, postmul, set);
1009         }
1010     }
1012     if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1013         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1014         if (SP_IS_PATTERN (server)) {
1015             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1016             sp_pattern_transform_multiply (pattern, postmul, set);
1017         }
1018     }
1022 void
1023 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1025     SPStyle *style = SP_OBJECT_STYLE (item);
1027     if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1028         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1029         if (SP_IS_GRADIENT (server)) {
1031             /**
1032              * \note Bbox units for a gradient are generally a bad idea because
1033              * with them, you cannot preserve the relative position of the
1034              * object and its gradient after rotation or skew. So now we
1035              * convert them to userspace units which are easy to keep in sync
1036              * just by adding the object's transform to gradientTransform.
1037              * \todo FIXME: convert back to bbox units after transforming with
1038              * the item, so as to preserve the original units.
1039              */
1040             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1042             sp_gradient_transform_multiply (gradient, postmul, set);
1043         }
1044     }
1046     if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1047         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1048         if (SP_IS_GRADIENT (server)) {
1049             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1050             sp_gradient_transform_multiply (gradient, postmul, set);
1051         }
1052     }
1055 void
1056 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1058     SPStyle *style = SP_OBJECT_STYLE (item);
1060     if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1062         style->stroke_width.computed *= ex;
1063         style->stroke_width.set = TRUE;
1065         if (style->stroke_dash.n_dash != 0) {
1066             int i;
1067             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1068                 style->stroke_dash.dash[i] *= ex;
1069             }
1070             style->stroke_dash.offset *= ex;
1071         }
1073         SP_OBJECT(item)->updateRepr();
1074     }
1077 /**
1078  * Find out the inverse of previous transform of an item (from its repr)
1079  */
1080 NR::Matrix
1081 sp_item_transform_repr (SPItem *item)
1083     NR::Matrix t_old(NR::identity());
1084     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1085     if (t_attr) {
1086         NR::Matrix t;
1087         if (sp_svg_transform_read(t_attr, &t)) {
1088             t_old = t;
1089         }
1090     }
1092     return t_old;
1096 /**
1097  * Recursively scale stroke width in \a item and its children by \a expansion.
1098  */
1099 void
1100 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1102     sp_item_adjust_stroke (item, expansion);
1104     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1105         if (SP_IS_ITEM(o))
1106             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1107     }
1110 /**
1111  * Recursively adjust rx and ry of rects.
1112  */
1113 void
1114 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1116     if (SP_IS_RECT (item)) {
1117         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1118     }
1120     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1121         if (SP_IS_ITEM(o))
1122             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1123     }
1126 /**
1127  * Recursively compensate pattern or gradient transform.
1128  */
1129 void
1130 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1132 // A clone must not touch the style (it's that of its parent!) and has no children, so quit now
1133     if (item && SP_IS_USE(item))
1134         return;
1136 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1137 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1138 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1139     NR::Matrix t_item = sp_item_transform_repr (item);
1140     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1142     if (is_pattern)
1143         sp_item_adjust_pattern (item, paint_delta);
1144     else
1145         sp_item_adjust_gradient (item, paint_delta);
1147 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation
1148     if (item && SP_IS_TEXT(item))
1149         return;
1151     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1152         if (SP_IS_ITEM(o)) {
1153 // At the level of the transformed item, t_ancestors is identity;
1154 // below it, it is the accmmulated chain of transforms from this level to the top level
1155             sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1156         }
1157     }
1160 /**
1161  * A temporary wrapper for the next function accepting the NRMatrix
1162  * instead of NR::Matrix
1163  */
1164 void
1165 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1167     if (transform == NULL)
1168         sp_item_write_transform(item, repr, NR::identity(), adv);
1169     else
1170         sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1173 /**
1174  * Set a new transform on an object.
1175  *
1176  * Compensate for stroke scaling and gradient/pattern fill transform, if
1177  * necessary. Call the object's set_transform method if transforms are
1178  * stored optimized. Send _transformed_signal. Invoke _write method so that
1179  * the repr is updated with the new transform.
1180  */
1181 void
1182 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv)
1184     g_return_if_fail(item != NULL);
1185     g_return_if_fail(SP_IS_ITEM(item));
1186     g_return_if_fail(repr != NULL);
1188     // calculate the relative transform, if not given by the adv attribute
1189     NR::Matrix advertized_transform;
1190     if (adv != NULL) {
1191         advertized_transform = *adv;
1192     } else {
1193         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1194     }
1196      // recursively compensate for stroke scaling, depending on user preference
1197     if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1198         double const expansion = 1. / NR::expansion(advertized_transform);
1199         sp_item_adjust_stroke_width_recursive(item, expansion);
1200     }
1202     // recursively compensate rx/ry of a rect if requested
1203     if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1204         sp_item_adjust_rects_recursive(item, advertized_transform);
1205     }
1207     // recursively compensate pattern fill if it's not to be transformed
1208     if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1209         sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1210     }
1211     /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1212     /// recursively compensate gradient fill if it's not to be transformed
1213     if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1214         sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1215     } else {
1216         // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1217         // it here _before_ the new transform is set, so as to use the pre-transform bbox
1218         sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1219     }
1221     // run the object's set_transform if transforms are stored optimized and there's no clippath or mask
1222     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1223     NR::Matrix transform_attr (transform);
1224     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform 
1225                 && !preserve && !item->clip_ref->getObject() && !item->mask_ref->getObject()) {
1226         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1227     }
1228     sp_item_set_item_transform(item, transform_attr);
1230     // Note: updateRepr comes before emitting the transformed signal since
1231     // it causes clone SPUse's copy of the original object to brought up to
1232     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1233     // values if called in code handling the transformed signal.
1234     SP_OBJECT(item)->updateRepr();
1236     // send the relative transform with a _transformed_signal
1237     item->_transformed_signal.emit(&advertized_transform, item);
1240 gint
1241 sp_item_event(SPItem *item, SPEvent *event)
1243     g_return_val_if_fail(item != NULL, FALSE);
1244     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1245     g_return_val_if_fail(event != NULL, FALSE);
1247     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1248         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1250     return FALSE;
1253 /**
1254  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1255  * gradients, patterns as sp_item_write_transform does.
1256  */
1257 void
1258 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1260     g_return_if_fail(item != NULL);
1261     g_return_if_fail(SP_IS_ITEM(item));
1263     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1264         item->transform = transform;
1265         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1266            transformation.  It's apparently not used anywhere else. */
1267         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1268         sp_item_rm_unsatisfied_cns(*item);
1269     }
1273 /**
1274  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1275  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1276  */
1277 NR::Matrix
1278 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1279     NR::Matrix ret(NR::identity());
1280     g_return_val_if_fail(object != NULL, ret);
1282     /* stop at first non-renderable ancestor */
1283     while ( object != ancestor && SP_IS_ITEM(object) ) {
1284         if (SP_IS_ROOT(object)) {
1285             ret *= SP_ROOT(object)->c2p;
1286         }
1287         ret *= SP_ITEM(object)->transform;
1288         object = SP_OBJECT_PARENT(object);
1289     }
1290     return ret;
1293 NR::Matrix
1294 i2i_affine(SPObject const *src, SPObject const *dest) {
1295     g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1296     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1297     return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1300 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1301     return i2i_affine(this, dest);
1304 /**
1305  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1306  * \pre (item != NULL) and SP_IS_ITEM(item).
1307  */
1308 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1310     return i2anc_affine(item, NULL);
1313 /**
1314  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1315  * Used in path operations mostly.
1316  * \pre (item != NULL) and SP_IS_ITEM(item).
1317  */
1318 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1320     g_assert(item != NULL);
1321     g_assert(SP_IS_ITEM(item));
1323     NR::Matrix ret(NR::identity());
1324     g_assert(ret.test_identity());
1325     while ( NULL != SP_OBJECT_PARENT(item) ) {
1326         ret *= item->transform;
1327         item = SP_ITEM(SP_OBJECT_PARENT(item));
1328     }
1329     g_assert(SP_IS_ROOT(item));
1331     ret *= item->transform;
1333     return ret;
1336 /* fixme: This is EVIL!!! */
1338 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1340     g_assert(item != NULL);
1341     g_assert(SP_IS_ITEM(item));
1343     NR::Matrix const ret( sp_item_i2doc_affine(item)
1344                           * NR::scale(1, -1)
1345                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1346     return ret;
1349 // same as i2d but with i2root instead of i2doc
1350 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1352     g_assert(item != NULL);
1353     g_assert(SP_IS_ITEM(item));
1355     NR::Matrix const ret( sp_item_i2root_affine(item)
1356                           * NR::scale(1, -1)
1357                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1358     return ret;
1361 /**
1362  * Converts a matrix \a m into the desktop coords of the \a item.
1363  * Will become a noop when we eliminate the coordinate flipping.
1364  */
1365 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1367     NR::Matrix const ret(m
1368                          * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1369                          * NR::scale(1, -1));
1370     return ret;
1373 /**
1374  * Converts a matrix \a m from the desktop coords of the \a item.
1375  * Will become a noop when we eliminate the coordinate flipping.
1376  */
1377 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1379     NR::Matrix const ret(NR::scale(1, -1)
1380                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1381                          * m);
1382     return ret;
1385 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1387     g_return_if_fail( item != NULL );
1388     g_return_if_fail( SP_IS_ITEM(item) );
1390     NR::Matrix dt2p; /* desktop to item parent transform */
1391     if (SP_OBJECT_PARENT(item)) {
1392         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1393     } else {
1394         dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1395                  * NR::scale(1, -1) );
1396     }
1398     NR::Matrix const i2p( i2dt * dt2p );
1399     sp_item_set_item_transform(item, i2p);
1403 NR::Matrix
1404 sp_item_dt2i_affine(SPItem const *item)
1406     /* fixme: Implement the right way (Lauris) */
1407     return sp_item_i2d_affine(item).inverse();
1410 /* Item views */
1412 static SPItemView *
1413 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1415     SPItemView *new_view;
1417     g_assert(item != NULL);
1418     g_assert(SP_IS_ITEM(item));
1419     g_assert(arenaitem != NULL);
1420     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1422     new_view = g_new(SPItemView, 1);
1424     new_view->next = list;
1425     new_view->flags = flags;
1426     new_view->key = key;
1427     new_view->arenaitem = nr_arena_item_ref(arenaitem);
1429     return new_view;
1432 static SPItemView *
1433 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1435     if (view == list) {
1436         list = list->next;
1437     } else {
1438         SPItemView *prev;
1439         prev = list;
1440         while (prev->next != view) prev = prev->next;
1441         prev->next = view->next;
1442     }
1444     nr_arena_item_unref(view->arenaitem);
1445     g_free(view);
1447     return list;
1450 /**
1451  * Return the arenaitem corresponding to the given item in the display
1452  * with the given key
1453  */
1454 NRArenaItem *
1455 sp_item_get_arenaitem(SPItem *item, unsigned key)
1457     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1458         if ( iv->key == key ) {
1459             return iv->arenaitem;
1460         }
1461     }
1463     return NULL;
1466 int
1467 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1469     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1470                                     SP_OBJECT_REPR(second));
1473 SPItem *
1474 sp_item_first_item_child (SPObject *obj)
1476     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1477         if (SP_IS_ITEM (iter))
1478             return SP_ITEM (iter);
1479     }
1480     return NULL;
1484 /*
1485   Local Variables:
1486   mode:c++
1487   c-file-style:"stroustrup"
1488   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1489   indent-tabs-mode:nil
1490   fill-column:99
1491   End:
1492 */
1493 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :