Code

updated catalan.nsh from launchpad by Xavi Conde
[inkscape.git] / src / sp-item.cpp
1 #define __SP_ITEM_C__
3 /** \file
4  * Base class for visual SVG elements
5  */
6 /*
7  * Authors:
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
11  *
12  * Copyright (C) 2001-2006 authors
13  * Copyright (C) 2001 Ximian, Inc.
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
18 /** \class SPItem
19  *
20  * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
21  * is a subclass of SPObject, with great deal of specific functionality.
22  */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
29 #include "sp-item.h"
30 #include "svg/svg.h"
31 #include "print.h"
32 #include "display/nr-arena.h"
33 #include "display/nr-arena-item.h"
34 #include "attributes.h"
35 #include "document.h"
36 #include "uri.h"
38 #include "style.h"
39 #include <glibmm/i18n.h>
40 #include "sp-root.h"
41 #include "sp-clippath.h"
42 #include "sp-mask.h"
43 #include "sp-rect.h"
44 #include "sp-use.h"
45 #include "sp-text.h"
46 #include "sp-item-rm-unsatisfied-cns.h"
47 #include "sp-pattern.h"
48 #include "sp-switch.h"
49 #include "gradient-chemistry.h"
50 #include "prefs-utils.h"
51 #include "conn-avoid-ref.h"
52 #include "conditions.h"
53 #include "sp-filter-reference.h"
55 #include "libnr/nr-matrix-div.h"
56 #include "libnr/nr-matrix-fns.h"
57 #include "libnr/nr-matrix-scale-ops.h"
58 #include "libnr/nr-matrix-translate-ops.h"
59 #include "libnr/nr-scale-translate-ops.h"
60 #include "libnr/nr-translate-scale-ops.h"
61 #include "libnr/nr-convert2geom.h"
62 #include "algorithms/find-last-if.h"
63 #include "util/reverse-list.h"
65 #include "xml/repr.h"
66 #include "extract-uri.h"
68 #include "live_effects/lpeobject.h"
69 #include "live_effects/effect.h"
71 #define noSP_ITEM_DEBUG_IDLE
73 static void sp_item_class_init(SPItemClass *klass);
74 static void sp_item_init(SPItem *item);
76 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
77 static void sp_item_release(SPObject *object);
78 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
79 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
80 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
82 static gchar *sp_item_private_description(SPItem *item);
83 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
85 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
86 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
88 static SPObjectClass *parent_class;
90 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
91 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
93 /**
94  * Registers SPItem class and returns its type number.
95  */
96 GType
97 sp_item_get_type(void)
98 {
99     static GType type = 0;
100     if (!type) {
101         GTypeInfo info = {
102             sizeof(SPItemClass),
103             NULL, NULL,
104             (GClassInitFunc) sp_item_class_init,
105             NULL, NULL,
106             sizeof(SPItem),
107             16,
108             (GInstanceInitFunc) sp_item_init,
109             NULL,   /* value_table */
110         };
111         type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
112     }
113     return type;
116 /**
117  * SPItem vtable initialization.
118  */
119 static void
120 sp_item_class_init(SPItemClass *klass)
122     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
124     parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
126     sp_object_class->build = sp_item_build;
127     sp_object_class->release = sp_item_release;
128     sp_object_class->set = sp_item_set;
129     sp_object_class->update = sp_item_update;
130     sp_object_class->write = sp_item_write;
132     klass->description = sp_item_private_description;
133     klass->snappoints = sp_item_private_snappoints;
136 /**
137  * Callback for SPItem object initialization.
138  */
139 static void
140 sp_item_init(SPItem *item)
142     item->init();
145 void SPItem::init() {
146     this->sensitive = TRUE;
148     this->transform_center_x = 0;
149     this->transform_center_y = 0;
151     this->_is_evaluated = true;
152     this->_evaluated_status = StatusUnknown;
154     this->transform = NR::identity();
156     this->display = NULL;
158     this->clip_ref = new SPClipPathReference(this);
159                 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
160                 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
161     _clip_ref_connection = cs1.connect(sl1);
163     this->mask_ref = new SPMaskReference(this);
164                 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
165                 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
166     _mask_ref_connection = cs2.connect(sl2);
168     this->avoidRef = new SPAvoidRef(this);
170     new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
173 bool SPItem::isVisibleAndUnlocked() const {
174     return (!isHidden() && !isLocked());
177 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
178     return (!isHidden(display_key) && !isLocked());
181 bool SPItem::isLocked() const {
182     for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
183         if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
184             return true;
185     }
186     return false;
189 void SPItem::setLocked(bool locked) {
190     SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
191                      ( locked ? "1" : NULL ));
192     updateRepr();
195 bool SPItem::isHidden() const {
196     if (!isEvaluated())
197         return true;
198     return style->display.computed == SP_CSS_DISPLAY_NONE;
201 void SPItem::setHidden(bool hide) {
202     style->display.set = TRUE;
203     style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
204     style->display.computed = style->display.value;
205     style->display.inherit = FALSE;
206     updateRepr();
209 bool SPItem::isHidden(unsigned display_key) const {
210     if (!isEvaluated())
211         return true;
212     for ( SPItemView *view(display) ; view ; view = view->next ) {
213         if ( view->key == display_key ) {
214             g_assert(view->arenaitem != NULL);
215             for ( NRArenaItem *arenaitem = view->arenaitem ;
216                   arenaitem ; arenaitem = arenaitem->parent )
217             {
218                 if (!arenaitem->visible) {
219                     return true;
220                 }
221             }
222             return false;
223         }
224     }
225     return true;
228 void SPItem::setEvaluated(bool evaluated) {
229     _is_evaluated = evaluated;
230     _evaluated_status = StatusSet;
233 void SPItem::resetEvaluated() {
234     if ( StatusCalculated == _evaluated_status ) {
235         _evaluated_status = StatusUnknown;
236         bool oldValue = _is_evaluated;
237         if ( oldValue != isEvaluated() ) {
238             requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
239         }
240     } if ( StatusSet == _evaluated_status ) {
241         SPObject const *const parent = SP_OBJECT_PARENT(this);
242         if (SP_IS_SWITCH(parent)) {
243             SP_SWITCH(parent)->resetChildEvaluated();
244         }
245     }
248 bool SPItem::isEvaluated() const {
249     if ( StatusUnknown == _evaluated_status ) {
250         _is_evaluated = sp_item_evaluate(this);
251         _evaluated_status = StatusCalculated;
252     }
253     return _is_evaluated;
256 /**
257  * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
258  *  Corresponds to setExplicitlyHidden.
259  */
260 bool
261 SPItem::isExplicitlyHidden() const
263     return (this->style->display.set
264             && this->style->display.value == SP_CSS_DISPLAY_NONE);
267 /**
268  * Sets the display CSS property to `hidden' if \a val is true,
269  * otherwise makes it unset
270  */
271 void
272 SPItem::setExplicitlyHidden(bool const val) {
273     this->style->display.set = val;
274     this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
275     this->style->display.computed = this->style->display.value;
276     this->updateRepr();
279 /**
280  * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
281  */
282 void
283 SPItem::setCenter(NR::Point object_centre) {
284     NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
285     if (bbox) {
286         transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
287         if (fabs(transform_center_x) < 1e-5) // rounding error
288             transform_center_x = 0;
289         transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
290         if (fabs(transform_center_y) < 1e-5) // rounding error
291             transform_center_y = 0;
292     }
295 void
296 SPItem::unsetCenter() {
297     transform_center_x = 0;
298     transform_center_y = 0;
301 bool SPItem::isCenterSet() {
302     return (transform_center_x != 0 || transform_center_y != 0);
305 NR::Point SPItem::getCenter() const {
306     NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
307     if (bbox) {
308         return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
309     } else {
310         return NR::Point (0, 0); // something's wrong!
311     }
315 namespace {
317 bool is_item(SPObject const &object) {
318     return SP_IS_ITEM(&object);
323 void SPItem::raiseToTop() {
324     using Inkscape::Algorithms::find_last_if;
326     SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
327         SP_OBJECT_NEXT(this), NULL, &is_item
328     );
329     if (topmost) {
330         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
331         sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
332     }
335 void SPItem::raiseOne() {
336     SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
337         SP_OBJECT_NEXT(this), NULL, &is_item
338     );
339     if (next_higher) {
340         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
341         Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
342         sp_repr_parent(repr)->changeOrder(repr, ref);
343     }
346 void SPItem::lowerOne() {
347     using Inkscape::Util::MutableList;
348     using Inkscape::Util::reverse_list;
350     MutableList<SPObject &> next_lower=std::find_if(
351         reverse_list<SPObject::SiblingIterator>(
352             SP_OBJECT_PARENT(this)->firstChild(), this
353         ),
354         MutableList<SPObject &>(),
355         &is_item
356     );
357     if (next_lower) {
358         ++next_lower;
359         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
360         Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
361         sp_repr_parent(repr)->changeOrder(repr, ref);
362     }
365 void SPItem::lowerToBottom() {
366     using Inkscape::Algorithms::find_last_if;
367     using Inkscape::Util::MutableList;
368     using Inkscape::Util::reverse_list;
370     MutableList<SPObject &> bottom=find_last_if(
371         reverse_list<SPObject::SiblingIterator>(
372             SP_OBJECT_PARENT(this)->firstChild(), this
373         ),
374         MutableList<SPObject &>(),
375         &is_item
376     );
377     if (bottom) {
378         ++bottom;
379         Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
380         Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
381         sp_repr_parent(repr)->changeOrder(repr, ref);
382     }
385 static void
386 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
388     sp_object_read_attr(object, "style");
389     sp_object_read_attr(object, "transform");
390     sp_object_read_attr(object, "clip-path");
391     sp_object_read_attr(object, "mask");
392     sp_object_read_attr(object, "sodipodi:insensitive");
393     sp_object_read_attr(object, "sodipodi:nonprintable");
394     sp_object_read_attr(object, "inkscape:transform-center-x");
395     sp_object_read_attr(object, "inkscape:transform-center-y");
396     sp_object_read_attr(object, "inkscape:connector-avoid");
398     if (((SPObjectClass *) (parent_class))->build) {
399         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
400     }
403 static void
404 sp_item_release(SPObject *object)
406     SPItem *item = (SPItem *) object;
408     item->_clip_ref_connection.disconnect();
409     item->_mask_ref_connection.disconnect();
411     // Note: do this here before the clip_ref is deleted, since calling 
412     // sp_document_ensure_up_to_date for triggered routing may reference 
413     // the deleted clip_ref.
414     if (item->avoidRef) {
415         delete item->avoidRef;
416         item->avoidRef = NULL;
417     }
419     if (item->clip_ref) {
420         item->clip_ref->detach();
421         delete item->clip_ref;
422         item->clip_ref = NULL;
423     }
425     if (item->mask_ref) {
426         item->mask_ref->detach();
427         delete item->mask_ref;
428         item->mask_ref = NULL;
429     }
431     if (((SPObjectClass *) (parent_class))->release) {
432         ((SPObjectClass *) parent_class)->release(object);
433     }
435     while (item->display) {
436         nr_arena_item_unparent(item->display->arenaitem);
437         item->display = sp_item_view_list_remove(item->display, item->display);
438     }
440     item->_transformed_signal.~signal();
443 static void
444 sp_item_set(SPObject *object, unsigned key, gchar const *value)
446     SPItem *item = (SPItem *) object;
448     switch (key) {
449         case SP_ATTR_TRANSFORM: {
450             NR::Matrix t;
451             if (value && sp_svg_transform_read(value, &t)) {
452                 sp_item_set_item_transform(item, t);
453             } else {
454                 sp_item_set_item_transform(item, NR::identity());
455             }
456             break;
457         }
458         case SP_PROP_CLIP_PATH: {
459             gchar *uri = extract_uri(value);
460             if (uri) {
461                 try {
462                     item->clip_ref->attach(Inkscape::URI(uri));
463                 } catch (Inkscape::BadURIException &e) {
464                     g_warning("%s", e.what());
465                     item->clip_ref->detach();
466                 }
467                 g_free(uri);
468             } else {
469                 item->clip_ref->detach();
470             }
472             break;
473         }
474         case SP_PROP_MASK: {
475             gchar *uri = extract_uri(value);
476             if (uri) {
477                 try {
478                     item->mask_ref->attach(Inkscape::URI(uri));
479                 } catch (Inkscape::BadURIException &e) {
480                     g_warning("%s", e.what());
481                     item->mask_ref->detach();
482                 }
483                 g_free(uri);
484             } else {
485                 item->mask_ref->detach();
486             }
488             break;
489         }
490         case SP_ATTR_SODIPODI_INSENSITIVE:
491             item->sensitive = !value;
492             for (SPItemView *v = item->display; v != NULL; v = v->next) {
493                 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
494             }
495             break;
496         case SP_ATTR_CONNECTOR_AVOID:
497             item->avoidRef->setAvoid(value);
498             break;
499         case SP_ATTR_TRANSFORM_CENTER_X:
500             if (value) {
501                 item->transform_center_x = g_strtod(value, NULL);
502             } else {
503                 item->transform_center_x = 0;
504             }
505             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
506             break;
507         case SP_ATTR_TRANSFORM_CENTER_Y:
508             if (value) {
509                 item->transform_center_y = g_strtod(value, NULL);
510             } else {
511                 item->transform_center_y = 0;
512             }
513             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
514             break;
515         case SP_PROP_SYSTEM_LANGUAGE:
516         case SP_PROP_REQUIRED_FEATURES:
517         case SP_PROP_REQUIRED_EXTENSIONS:
518             {
519                 item->resetEvaluated();
520                 // pass to default handler
521             }
522         default:
523             if (SP_ATTRIBUTE_IS_CSS(key)) {
524                 sp_style_read_from_object(object->style, object);
525                 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
526             } else {
527                 if (((SPObjectClass *) (parent_class))->set) {
528                     (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
529                 }
530             }
531             break;
532     }
535 static void
536 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
538     if (old_clip) {
539         SPItemView *v;
540         /* Hide clippath */
541         for (v = item->display; v != NULL; v = v->next) {
542             sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
543             nr_arena_item_set_clip(v->arenaitem, NULL);
544         }
545     }
546     if (SP_IS_CLIPPATH(clip)) {
547         NRRect bbox;
548         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
549         for (SPItemView *v = item->display; v != NULL; v = v->next) {
550             if (!v->arenaitem->key) {
551                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
552             }
553             NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
554                                                NR_ARENA_ITEM_ARENA(v->arenaitem),
555                                                NR_ARENA_ITEM_GET_KEY(v->arenaitem));
556             nr_arena_item_set_clip(v->arenaitem, ai);
557             nr_arena_item_unref(ai);
558             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
559             SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
560         }
561     }
564 static void
565 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
567     if (old_mask) {
568         /* Hide mask */
569         for (SPItemView *v = item->display; v != NULL; v = v->next) {
570             sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
571             nr_arena_item_set_mask(v->arenaitem, NULL);
572         }
573     }
574     if (SP_IS_MASK(mask)) {
575         NRRect bbox;
576         sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
577         for (SPItemView *v = item->display; v != NULL; v = v->next) {
578             if (!v->arenaitem->key) {
579                 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
580             }
581             NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
582                                            NR_ARENA_ITEM_ARENA(v->arenaitem),
583                                            NR_ARENA_ITEM_GET_KEY(v->arenaitem));
584             nr_arena_item_set_mask(v->arenaitem, ai);
585             nr_arena_item_unref(ai);
586             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
587             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
588         }
589     }
592 static void
593 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
595     SPItem *item = SP_ITEM(object);
597     if (((SPObjectClass *) (parent_class))->update)
598         (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
600     if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
601         if (flags & SP_OBJECT_MODIFIED_FLAG) {
602             for (SPItemView *v = item->display; v != NULL; v = v->next) {
603                 nr_arena_item_set_transform(v->arenaitem, item->transform);
604             }
605         }
607         SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
608         SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
610         if ( clip_path || mask ) {
611             NRRect bbox;
612             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
613             if (clip_path) {
614                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
615                     sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
616                 }
617             }
618             if (mask) {
619                 for (SPItemView *v = item->display; v != NULL; v = v->next) {
620                     sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
621                 }
622             }
623         }
625         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
626             for (SPItemView *v = item->display; v != NULL; v = v->next) {
627                 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
628                 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
629             }
630         }
631     }
633     /* Update bounding box data used by filters */
634     if (item->style->filter.set && item->display) {
635         NRRect item_bbox;
636         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
637         NR::Maybe<NR::Rect> i_bbox = item_bbox;
639         SPItemView *itemview = item->display;
640         do {
641             if (itemview->arenaitem)
642                 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
643         } while ( (itemview = itemview->next) );
644     }
646     // Update libavoid with item geometry (for connector routing).
647     if (item->avoidRef)
648         item->avoidRef->handleSettingChange();
651 static Inkscape::XML::Node *
652 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
654     SPItem *item = SP_ITEM(object);
656     gchar *c = sp_svg_transform_write(item->transform);
657     repr->setAttribute("transform", c);
658     g_free(c);
660     if (flags & SP_OBJECT_WRITE_EXT) {
661         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
662         if (item->transform_center_x != 0)
663             sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
664         else
665             repr->setAttribute ("inkscape:transform-center-x", NULL);
666         if (item->transform_center_y != 0)
667             sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
668         else
669             repr->setAttribute ("inkscape:transform-center-y", NULL);
670     }
672     if (item->clip_ref->getObject()) {
673         const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
674         repr->setAttribute ("clip-path", value);
675         g_free ((void *) value);
676     }
677     if (item->mask_ref->getObject()) {
678         const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
679         repr->setAttribute ("mask", value);
680         g_free ((void *) value);
681     }
683     if (((SPObjectClass *) (parent_class))->write) {
684         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
685     }
687     return repr;
690 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
691                                       SPItem::BBoxType type,
692                                       unsigned int /*dkey*/) const
694     NR::Maybe<NR::Rect> r = NR::Nothing();
695     sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
696     return r;
699 void
700 sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
702     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
705 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
706 void
707 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
709     sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
712 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
713  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
714  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
715  * clones), in turn, call this function in their bbox methods. */
716 void
717 sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
719     g_assert(item != NULL);
720     g_assert(SP_IS_ITEM(item));
721     g_assert(bbox != NULL);
723     if (clear) {
724         *bbox = NR::Nothing();
725     }
727     // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
729     NRRect temp_bbox;
730     temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
731     temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
733     // call the subclass method
734     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
735         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
736     }
738     // unless this is geometric bbox, crop the bbox by clip path, if any
739     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
740         NRRect b;
741         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
742         nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
743     }
744     
745     if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
746         // We'll assume here that when x0 > x1 or y0 > y1, the bbox is "nothing"
747         // However it has never been explicitely defined this way for NRRects
748         // (as opposed to NR::Maybe<NR::Rect>)
749         *bbox = NR::Nothing();
750         return;
751     } 
752     
753     if (temp_bbox.x0 == temp_bbox.y0 == NR_HUGE && temp_bbox.x1 == temp_bbox.y1 == -NR_HUGE) {
754         // The bbox hasn't been touched by the SPItemClass' bbox method
755         // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
756         *bbox = NR::Nothing();
757         return;
758     }
759     
760     // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
761     // for any rectangle with zero area. The geometrical bbox of for example a vertical line
762     // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684) 
763     NR::Maybe<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
764     
765     *bbox = NR::union_bounds(*bbox, temp_bbox_new);
768 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
769 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
770  * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
771  * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
772  * clones), in turn, call this function in their bbox methods. */
773 void
774 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
776     g_assert(item != NULL);
777     g_assert(SP_IS_ITEM(item));
778     g_assert(bbox != NULL);
780     if (clear) {
781         bbox->x0 = bbox->y0 = 1e18;
782         bbox->x1 = bbox->y1 = -1e18;
783     }
785     NRRect this_bbox;
786     this_bbox.x0 = this_bbox.y0 = 1e18;
787     this_bbox.x1 = this_bbox.y1 = -1e18;
789     // call the subclass method
790     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
791         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
792     }
794     // unless this is geometric bbox, crop the bbox by clip path, if any
795     if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
796         NRRect b;
797         sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
798         nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
799     }
801     // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
802     if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
803         nr_rect_d_union (bbox, bbox, &this_bbox);
804     }
807 unsigned sp_item_pos_in_parent(SPItem *item)
809     g_assert(item != NULL);
810     g_assert(SP_IS_ITEM(item));
812     SPObject *parent = SP_OBJECT_PARENT(item);
813     g_assert(parent != NULL);
814     g_assert(SP_IS_OBJECT(parent));
816     SPObject *object = SP_OBJECT(item);
818     unsigned pos=0;
819     for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
820         if ( iter == object ) {
821             return pos;
822         }
823         if (SP_IS_ITEM(iter)) {
824             pos++;
825         }
826     }
828     g_assert_not_reached();
829     return 0;
832 void
833 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
835     g_assert(item != NULL);
836     g_assert(SP_IS_ITEM(item));
837     g_assert(bbox != NULL);
839     sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
842 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
844     NR::Maybe<NR::Rect> rect = NR::Nothing();
845     sp_item_invoke_bbox(item, &rect, sp_item_i2d_affine(item), TRUE, type);
846     return rect;
849 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
851     NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
852     /* Just the corners of the bounding box suffices given that we don't yet
853        support angled guide lines. */
855     if (bbox) {
856         NR::Point p1, p2;
857         p1 = bbox->min();
858         p2 = bbox->max();
859         *p = p1;
860         *p = NR::Point(p1[NR::X], p2[NR::Y]);
861         *p = p2;
862         *p = NR::Point(p1[NR::Y], p2[NR::X]);
863     }
866 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
868     g_assert (item != NULL);
869     g_assert (SP_IS_ITEM(item));
871     SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
872     if (item_class.snappoints) {
873         item_class.snappoints(item, p);
874     }
876     if (includeItemCenter) {
877         *p = item->getCenter();
878     }
881 void
882 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
884     if (!item->isHidden()) {
885         if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
886             if (!item->transform.test_identity()
887                 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
888             {
889                 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
890                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
891                 sp_print_release(ctx);
892             } else {
893                 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
894             }
895         }
896     }
899 static gchar *
900 sp_item_private_description(SPItem */*item*/)
902     return g_strdup(_("Object"));
905 /**
906  * Returns a string suitable for status bar, formatted in pango markup language.
907  *
908  * Must be freed by caller.
909  */
910 gchar *
911 sp_item_description(SPItem *item)
913     g_assert(item != NULL);
914     g_assert(SP_IS_ITEM(item));
916     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
917         gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
918         if (s && item->clip_ref->getObject()) {
919             gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
920             g_free (s);
921             s = snew;
922         }
923         if (s && item->mask_ref->getObject()) {
924             gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
925             g_free (s);
926             s = snew;
927         }
928         return s;
929     }
931     g_assert_not_reached();
932     return NULL;
935 /**
936  * Allocates unique integer keys.
937  * \param numkeys Number of keys required.
938  * \return First allocated key; hence if the returned key is n
939  * you can use n, n + 1, ..., n + (numkeys - 1)
940  */
941 unsigned
942 sp_item_display_key_new(unsigned numkeys)
944     static unsigned dkey = 0;
946     dkey += numkeys;
948     return dkey - numkeys;
951 NRArenaItem *
952 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
954     g_assert(item != NULL);
955     g_assert(SP_IS_ITEM(item));
956     g_assert(arena != NULL);
957     g_assert(NR_IS_ARENA(arena));
959     NRArenaItem *ai = NULL;
960     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
961         ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
962     }
964     if (ai != NULL) {
965         item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
966         nr_arena_item_set_transform(ai, item->transform);
967         nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
968         nr_arena_item_set_visible(ai, !item->isHidden());
969         nr_arena_item_set_sensitive(ai, item->sensitive);
970         if (item->clip_ref->getObject()) {
971             SPClipPath *cp = item->clip_ref->getObject();
973             if (!item->display->arenaitem->key) {
974                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
975             }
976             int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
978             // Show and set clip
979             NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
980             nr_arena_item_set_clip(ai, ac);
981             nr_arena_item_unref(ac);
983             // Update bbox, in case the clip uses bbox units
984             NRRect bbox;
985             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
986             sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
987             SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
988         }
989         if (item->mask_ref->getObject()) {
990             SPMask *mask = item->mask_ref->getObject();
992             if (!item->display->arenaitem->key) {
993                 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
994             }
995             int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
997             // Show and set mask
998             NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
999             nr_arena_item_set_mask(ai, ac);
1000             nr_arena_item_unref(ac);
1002             // Update bbox, in case the mask uses bbox units
1003             NRRect bbox;
1004             sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1005             sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1006             SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1007         }
1008         NR_ARENA_ITEM_SET_DATA(ai, item);
1009         NRRect item_bbox;
1010         sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1011         NR::Maybe<NR::Rect> i_bbox = item_bbox;
1012         nr_arena_item_set_item_bbox(ai, i_bbox);
1013     }
1015     return ai;
1018 void
1019 sp_item_invoke_hide(SPItem *item, unsigned key)
1021     g_assert(item != NULL);
1022     g_assert(SP_IS_ITEM(item));
1024     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1025         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1026     }
1028     SPItemView *ref = NULL;
1029     SPItemView *v = item->display;
1030     while (v != NULL) {
1031         SPItemView *next = v->next;
1032         if (v->key == key) {
1033             if (item->clip_ref->getObject()) {
1034                 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1035                 nr_arena_item_set_clip(v->arenaitem, NULL);
1036             }
1037             if (item->mask_ref->getObject()) {
1038                 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1039                 nr_arena_item_set_mask(v->arenaitem, NULL);
1040             }
1041             if (!ref) {
1042                 item->display = v->next;
1043             } else {
1044                 ref->next = v->next;
1045             }
1046             nr_arena_item_unparent(v->arenaitem);
1047             nr_arena_item_unref(v->arenaitem);
1048             g_free(v);
1049         } else {
1050             ref = v;
1051         }
1052         v = next;
1053     }
1056 // Adjusters
1058 void
1059 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1061     SPStyle *style = SP_OBJECT_STYLE (item);
1063     if (style && (style->fill.isPaintserver())) {
1064         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1065         if (SP_IS_PATTERN (server)) {
1066             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1067             sp_pattern_transform_multiply (pattern, postmul, set);
1068         }
1069     }
1071     if (style && (style->stroke.isPaintserver())) {
1072         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1073         if (SP_IS_PATTERN (server)) {
1074             SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1075             sp_pattern_transform_multiply (pattern, postmul, set);
1076         }
1077     }
1081 void
1082 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1084     SPStyle *style = SP_OBJECT_STYLE (item);
1086     if (style && (style->fill.isPaintserver())) {
1087         SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1088         if (SP_IS_GRADIENT (server)) {
1090             /**
1091              * \note Bbox units for a gradient are generally a bad idea because
1092              * with them, you cannot preserve the relative position of the
1093              * object and its gradient after rotation or skew. So now we
1094              * convert them to userspace units which are easy to keep in sync
1095              * just by adding the object's transform to gradientTransform.
1096              * \todo FIXME: convert back to bbox units after transforming with
1097              * the item, so as to preserve the original units.
1098              */
1099             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1101             sp_gradient_transform_multiply (gradient, postmul, set);
1102         }
1103     }
1105     if (style && (style->stroke.isPaintserver())) {
1106         SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1107         if (SP_IS_GRADIENT (server)) {
1108             SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1109             sp_gradient_transform_multiply (gradient, postmul, set);
1110         }
1111     }
1114 void
1115 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1117     SPStyle *style = SP_OBJECT_STYLE (item);
1119     if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1121         style->stroke_width.computed *= ex;
1122         style->stroke_width.set = TRUE;
1124         if (style->stroke_dash.n_dash != 0) {
1125             int i;
1126             for (i = 0; i < style->stroke_dash.n_dash; i++) {
1127                 style->stroke_dash.dash[i] *= ex;
1128             }
1129             style->stroke_dash.offset *= ex;
1130         }
1132         SP_OBJECT(item)->updateRepr();
1133     }
1136 /**
1137  * Find out the inverse of previous transform of an item (from its repr)
1138  */
1139 NR::Matrix
1140 sp_item_transform_repr (SPItem *item)
1142     NR::Matrix t_old(NR::identity());
1143     gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1144     if (t_attr) {
1145         NR::Matrix t;
1146         if (sp_svg_transform_read(t_attr, &t)) {
1147             t_old = t;
1148         }
1149     }
1151     return t_old;
1155 /**
1156  * Recursively scale stroke width in \a item and its children by \a expansion.
1157  */
1158 void
1159 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1161     sp_item_adjust_stroke (item, expansion);
1163 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1164     if (item && SP_IS_USE(item))
1165         return;
1167     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1168         if (SP_IS_ITEM(o))
1169             sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1170     }
1173 /**
1174  * Recursively adjust rx and ry of rects.
1175  */
1176 void
1177 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1179     if (SP_IS_RECT (item)) {
1180         sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1181     }
1183     for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1184         if (SP_IS_ITEM(o))
1185             sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1186     }
1189 /**
1190  * Recursively compensate pattern or gradient transform.
1191  */
1192 void
1193 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1195 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1196 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1197 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1198     NR::Matrix t_item = sp_item_transform_repr (item);
1199     NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1201 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1202 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1203 // we must not touch it
1204     if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1205         for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1206             if (SP_IS_ITEM(o)) {
1207 // At the level of the transformed item, t_ancestors is identity;
1208 // below it, it is the accmmulated chain of transforms from this level to the top level
1209                 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1210             }
1211         }
1212     }
1214 // We recursed into children first, and are now adjusting this object second;
1215 // this is so that adjustments in a tree are done from leaves up to the root,
1216 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1217 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1219     if (is_pattern)
1220         sp_item_adjust_pattern (item, paint_delta);
1221     else
1222         sp_item_adjust_gradient (item, paint_delta);
1226 void
1227 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1229     if ( !SP_IS_SHAPE(item) )
1230         return;
1232     SPShape *shape = SP_SHAPE (item);
1233     if ( sp_shape_has_path_effect(shape) ) {
1234         LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1235         LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1236         if (new_lpeobj != lpeobj) {
1237             sp_shape_set_path_effect(shape, new_lpeobj);
1238         }
1240         Inkscape::LivePathEffect::Effect * effect = sp_shape_get_livepatheffect(shape);
1241         if (effect) {
1242             effect->transform_multiply (to_2geom(postmul), set);
1243         }
1244     }
1247 /**
1248  * A temporary wrapper for the next function accepting the NRMatrix
1249  * instead of NR::Matrix
1250  */
1251 void
1252 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1254     if (transform == NULL)
1255         sp_item_write_transform(item, repr, NR::identity(), adv);
1256     else
1257         sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1260 /**
1261  * Set a new transform on an object.
1262  *
1263  * Compensate for stroke scaling and gradient/pattern fill transform, if
1264  * necessary. Call the object's set_transform method if transforms are
1265  * stored optimized. Send _transformed_signal. Invoke _write method so that
1266  * the repr is updated with the new transform.
1267  */
1268 void
1269 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1271     g_return_if_fail(item != NULL);
1272     g_return_if_fail(SP_IS_ITEM(item));
1273     g_return_if_fail(repr != NULL);
1275     // calculate the relative transform, if not given by the adv attribute
1276     NR::Matrix advertized_transform;
1277     if (adv != NULL) {
1278         advertized_transform = *adv;
1279     } else {
1280         advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1281     }
1283     if (compensate) {
1285          // recursively compensate for stroke scaling, depending on user preference
1286         if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1287             double const expansion = 1. / NR::expansion(advertized_transform);
1288             sp_item_adjust_stroke_width_recursive(item, expansion);
1289         }
1291         // recursively compensate rx/ry of a rect if requested
1292         if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1293             sp_item_adjust_rects_recursive(item, advertized_transform);
1294         }
1296         // recursively compensate pattern fill if it's not to be transformed
1297         if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1298             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1299         }
1300         /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1301         /// recursively compensate gradient fill if it's not to be transformed
1302         if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1303             sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1304         } else {
1305             // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1306             // it here _before_ the new transform is set, so as to use the pre-transform bbox
1307             sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1308         }
1310     } // endif(compensate)
1312     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1313     NR::Matrix transform_attr (transform);
1314     if ( // run the object's set_transform (i.e. embed transform) only if:
1315          ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1316              !preserve && // user did not chose to preserve all transforms
1317              !item->clip_ref->getObject() && // the object does not have a clippath
1318              !item->mask_ref->getObject() && // the object does not have a mask
1319          !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1320              // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1321         ) {
1322         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1323     }
1324     sp_item_set_item_transform(item, transform_attr);
1326     // Note: updateRepr comes before emitting the transformed signal since
1327     // it causes clone SPUse's copy of the original object to brought up to
1328     // date with the original.  Otherwise, sp_use_bbox returns incorrect
1329     // values if called in code handling the transformed signal.
1330     SP_OBJECT(item)->updateRepr();
1332     // send the relative transform with a _transformed_signal
1333     item->_transformed_signal.emit(&advertized_transform, item);
1336 gint
1337 sp_item_event(SPItem *item, SPEvent *event)
1339     g_return_val_if_fail(item != NULL, FALSE);
1340     g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1341     g_return_val_if_fail(event != NULL, FALSE);
1343     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1344         return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1346     return FALSE;
1349 /**
1350  * Sets item private transform (not propagated to repr), without compensating stroke widths,
1351  * gradients, patterns as sp_item_write_transform does.
1352  */
1353 void
1354 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1356     g_return_if_fail(item != NULL);
1357     g_return_if_fail(SP_IS_ITEM(item));
1359     if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1360         item->transform = transform;
1361         /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1362            transformation.  It's apparently not used anywhere else. */
1363         item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1364         sp_item_rm_unsatisfied_cns(*item);
1365     }
1369 /**
1370  * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1371  *   ("Ancestor (\>=)" here includes as far as \a object itself.)
1372  */
1373 NR::Matrix
1374 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1375     NR::Matrix ret(NR::identity());
1376     g_return_val_if_fail(object != NULL, ret);
1378     /* stop at first non-renderable ancestor */
1379     while ( object != ancestor && SP_IS_ITEM(object) ) {
1380         if (SP_IS_ROOT(object)) {
1381             ret *= SP_ROOT(object)->c2p;
1382         }
1383         ret *= SP_ITEM(object)->transform;
1384         object = SP_OBJECT_PARENT(object);
1385     }
1386     return ret;
1389 NR::Matrix
1390 i2i_affine(SPObject const *src, SPObject const *dest) {
1391     g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1392     SPObject const *ancestor = src->nearestCommonAncestor(dest);
1393     return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1396 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1397     return i2i_affine(this, dest);
1400 /**
1401  * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1402  * \pre (item != NULL) and SP_IS_ITEM(item).
1403  */
1404 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1406     return i2anc_affine(item, NULL);
1409 /**
1410  * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1411  * Used in path operations mostly.
1412  * \pre (item != NULL) and SP_IS_ITEM(item).
1413  */
1414 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1416     g_assert(item != NULL);
1417     g_assert(SP_IS_ITEM(item));
1419     NR::Matrix ret(NR::identity());
1420     g_assert(ret.test_identity());
1421     while ( NULL != SP_OBJECT_PARENT(item) ) {
1422         ret *= item->transform;
1423         item = SP_ITEM(SP_OBJECT_PARENT(item));
1424     }
1425     g_assert(SP_IS_ROOT(item));
1427     ret *= item->transform;
1429     return ret;
1432 /* fixme: This is EVIL!!! */
1434 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1436     g_assert(item != NULL);
1437     g_assert(SP_IS_ITEM(item));
1439     NR::Matrix const ret( sp_item_i2doc_affine(item)
1440                           * NR::scale(1, -1)
1441                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1442     return ret;
1445 // same as i2d but with i2root instead of i2doc
1446 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1448     g_assert(item != NULL);
1449     g_assert(SP_IS_ITEM(item));
1451     NR::Matrix const ret( sp_item_i2root_affine(item)
1452                           * NR::scale(1, -1)
1453                           * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1454     return ret;
1457 /**
1458  * Converts a matrix \a m into the desktop coords of the \a item.
1459  * Will become a noop when we eliminate the coordinate flipping.
1460  */
1461 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1463     NR::Matrix const ret(m
1464                          * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1465                          * NR::scale(1, -1));
1466     return ret;
1469 /**
1470  * Converts a matrix \a m from the desktop coords of the \a item.
1471  * Will become a noop when we eliminate the coordinate flipping.
1472  */
1473 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1475     NR::Matrix const ret(NR::scale(1, -1)
1476                          * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1477                          * m);
1478     return ret;
1481 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1483     g_return_if_fail( item != NULL );
1484     g_return_if_fail( SP_IS_ITEM(item) );
1486     NR::Matrix dt2p; /* desktop to item parent transform */
1487     if (SP_OBJECT_PARENT(item)) {
1488         dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1489     } else {
1490         dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1491                  * NR::scale(1, -1) );
1492     }
1494     NR::Matrix const i2p( i2dt * dt2p );
1495     sp_item_set_item_transform(item, i2p);
1499 NR::Matrix
1500 sp_item_dt2i_affine(SPItem const *item)
1502     /* fixme: Implement the right way (Lauris) */
1503     return sp_item_i2d_affine(item).inverse();
1506 /* Item views */
1508 static SPItemView *
1509 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1511     SPItemView *new_view;
1513     g_assert(item != NULL);
1514     g_assert(SP_IS_ITEM(item));
1515     g_assert(arenaitem != NULL);
1516     g_assert(NR_IS_ARENA_ITEM(arenaitem));
1518     new_view = g_new(SPItemView, 1);
1520     new_view->next = list;
1521     new_view->flags = flags;
1522     new_view->key = key;
1523     new_view->arenaitem = nr_arena_item_ref(arenaitem);
1525     return new_view;
1528 static SPItemView *
1529 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1531     if (view == list) {
1532         list = list->next;
1533     } else {
1534         SPItemView *prev;
1535         prev = list;
1536         while (prev->next != view) prev = prev->next;
1537         prev->next = view->next;
1538     }
1540     nr_arena_item_unref(view->arenaitem);
1541     g_free(view);
1543     return list;
1546 /**
1547  * Return the arenaitem corresponding to the given item in the display
1548  * with the given key
1549  */
1550 NRArenaItem *
1551 sp_item_get_arenaitem(SPItem *item, unsigned key)
1553     for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1554         if ( iv->key == key ) {
1555             return iv->arenaitem;
1556         }
1557     }
1559     return NULL;
1562 int
1563 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1565     return sp_repr_compare_position(SP_OBJECT_REPR(first),
1566                                     SP_OBJECT_REPR(second));
1569 SPItem *
1570 sp_item_first_item_child (SPObject *obj)
1572     for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1573         if (SP_IS_ITEM (iter))
1574             return SP_ITEM (iter);
1575     }
1576     return NULL;
1580 /*
1581   Local Variables:
1582   mode:c++
1583   c-file-style:"stroustrup"
1584   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1585   indent-tabs-mode:nil
1586   fill-column:99
1587   End:
1588 */
1589 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :