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;
114 }
116 /**
117 * SPItem vtable initialization.
118 */
119 static void
120 sp_item_class_init(SPItemClass *klass)
121 {
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;
134 }
136 /**
137 * Callback for SPItem object initialization.
138 */
139 static void
140 sp_item_init(SPItem *item)
141 {
142 item->init();
143 }
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 *>();
171 }
173 bool SPItem::isVisibleAndUnlocked() const {
174 return (!isHidden() && !isLocked());
175 }
177 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
178 return (!isHidden(display_key) && !isLocked());
179 }
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;
187 }
189 void SPItem::setLocked(bool locked) {
190 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
191 ( locked ? "1" : NULL ));
192 updateRepr();
193 }
195 bool SPItem::isHidden() const {
196 if (!isEvaluated())
197 return true;
198 return style->display.computed == SP_CSS_DISPLAY_NONE;
199 }
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();
207 }
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;
226 }
228 void SPItem::setEvaluated(bool evaluated) {
229 _is_evaluated = evaluated;
230 _evaluated_status = StatusSet;
231 }
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 }
246 }
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;
254 }
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
262 {
263 return (this->style->display.set
264 && this->style->display.value == SP_CSS_DISPLAY_NONE);
265 }
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();
277 }
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 }
293 }
295 void
296 SPItem::unsetCenter() {
297 transform_center_x = 0;
298 transform_center_y = 0;
299 }
301 bool SPItem::isCenterSet() {
302 return (transform_center_x != 0 || transform_center_y != 0);
303 }
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 }
312 }
315 namespace {
317 bool is_item(SPObject const &object) {
318 return SP_IS_ITEM(&object);
319 }
321 }
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 }
333 }
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 }
344 }
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 }
363 }
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 }
383 }
385 static void
386 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
387 {
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 }
401 }
403 static void
404 sp_item_release(SPObject *object)
405 {
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();
441 }
443 static void
444 sp_item_set(SPObject *object, unsigned key, gchar const *value)
445 {
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 }
533 }
535 static void
536 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
537 {
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 }
562 }
564 static void
565 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
566 {
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 }
590 }
592 static void
593 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
594 {
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();
649 }
651 static Inkscape::XML::Node *
652 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
653 {
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;
688 }
690 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
691 SPItem::BBoxType type,
692 unsigned int /*dkey*/) const
693 {
694 NRRect r;
695 sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
696 return r;
697 }
699 void
700 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
701 {
702 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
703 }
705 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
706 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
707 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
708 * clones), in turn, call this function in their bbox methods. */
709 void
710 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
711 {
712 g_assert(item != NULL);
713 g_assert(SP_IS_ITEM(item));
714 g_assert(bbox != NULL);
716 if (clear) {
717 bbox->x0 = bbox->y0 = 1e18;
718 bbox->x1 = bbox->y1 = -1e18;
719 }
721 NRRect this_bbox;
722 this_bbox.x0 = this_bbox.y0 = 1e18;
723 this_bbox.x1 = this_bbox.y1 = -1e18;
725 // call the subclass method
726 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
727 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
728 }
730 // unless this is geometric bbox, crop the bbox by clip path, if any
731 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
732 NRRect b;
733 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
734 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
735 }
737 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
738 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
739 nr_rect_d_union (bbox, bbox, &this_bbox);
740 }
741 }
743 unsigned sp_item_pos_in_parent(SPItem *item)
744 {
745 g_assert(item != NULL);
746 g_assert(SP_IS_ITEM(item));
748 SPObject *parent = SP_OBJECT_PARENT(item);
749 g_assert(parent != NULL);
750 g_assert(SP_IS_OBJECT(parent));
752 SPObject *object = SP_OBJECT(item);
754 unsigned pos=0;
755 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
756 if ( iter == object ) {
757 return pos;
758 }
759 if (SP_IS_ITEM(iter)) {
760 pos++;
761 }
762 }
764 g_assert_not_reached();
765 return 0;
766 }
768 void
769 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
770 {
771 g_assert(item != NULL);
772 g_assert(SP_IS_ITEM(item));
773 g_assert(bbox != NULL);
775 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
776 }
778 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
779 {
780 NRRect ret;
781 sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE, type);
782 return ret.upgrade();
783 }
785 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
786 {
787 NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
788 /* Just the corners of the bounding box suffices given that we don't yet
789 support angled guide lines. */
791 if (bbox) {
792 NR::Point p1, p2;
793 p1 = bbox->min();
794 p2 = bbox->max();
795 *p = p1;
796 *p = NR::Point(p1[NR::X], p2[NR::Y]);
797 *p = p2;
798 *p = NR::Point(p1[NR::Y], p2[NR::X]);
799 }
800 }
802 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
803 {
804 g_assert (item != NULL);
805 g_assert (SP_IS_ITEM(item));
807 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
808 if (item_class.snappoints) {
809 item_class.snappoints(item, p);
810 }
812 if (includeItemCenter) {
813 *p = item->getCenter();
814 }
815 }
817 void
818 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
819 {
820 if (!item->isHidden()) {
821 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
822 if (!item->transform.test_identity()
823 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
824 {
825 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
826 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
827 sp_print_release(ctx);
828 } else {
829 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
830 }
831 }
832 }
833 }
835 static gchar *
836 sp_item_private_description(SPItem */*item*/)
837 {
838 return g_strdup(_("Object"));
839 }
841 /**
842 * Returns a string suitable for status bar, formatted in pango markup language.
843 *
844 * Must be freed by caller.
845 */
846 gchar *
847 sp_item_description(SPItem *item)
848 {
849 g_assert(item != NULL);
850 g_assert(SP_IS_ITEM(item));
852 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
853 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
854 if (s && item->clip_ref->getObject()) {
855 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
856 g_free (s);
857 s = snew;
858 }
859 if (s && item->mask_ref->getObject()) {
860 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
861 g_free (s);
862 s = snew;
863 }
864 return s;
865 }
867 g_assert_not_reached();
868 return NULL;
869 }
871 /**
872 * Allocates unique integer keys.
873 * \param numkeys Number of keys required.
874 * \return First allocated key; hence if the returned key is n
875 * you can use n, n + 1, ..., n + (numkeys - 1)
876 */
877 unsigned
878 sp_item_display_key_new(unsigned numkeys)
879 {
880 static unsigned dkey = 0;
882 dkey += numkeys;
884 return dkey - numkeys;
885 }
887 NRArenaItem *
888 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
889 {
890 g_assert(item != NULL);
891 g_assert(SP_IS_ITEM(item));
892 g_assert(arena != NULL);
893 g_assert(NR_IS_ARENA(arena));
895 NRArenaItem *ai = NULL;
896 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
897 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
898 }
900 if (ai != NULL) {
901 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
902 nr_arena_item_set_transform(ai, item->transform);
903 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
904 nr_arena_item_set_visible(ai, !item->isHidden());
905 nr_arena_item_set_sensitive(ai, item->sensitive);
906 if (item->clip_ref->getObject()) {
907 SPClipPath *cp = item->clip_ref->getObject();
909 if (!item->display->arenaitem->key) {
910 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
911 }
912 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
914 // Show and set clip
915 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
916 nr_arena_item_set_clip(ai, ac);
917 nr_arena_item_unref(ac);
919 // Update bbox, in case the clip uses bbox units
920 NRRect bbox;
921 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
922 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
923 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
924 }
925 if (item->mask_ref->getObject()) {
926 SPMask *mask = item->mask_ref->getObject();
928 if (!item->display->arenaitem->key) {
929 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
930 }
931 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
933 // Show and set mask
934 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
935 nr_arena_item_set_mask(ai, ac);
936 nr_arena_item_unref(ac);
938 // Update bbox, in case the mask uses bbox units
939 NRRect bbox;
940 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
941 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
942 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
943 }
944 NR_ARENA_ITEM_SET_DATA(ai, item);
945 NRRect item_bbox;
946 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
947 NR::Maybe<NR::Rect> i_bbox = item_bbox;
948 nr_arena_item_set_item_bbox(ai, i_bbox);
949 }
951 return ai;
952 }
954 void
955 sp_item_invoke_hide(SPItem *item, unsigned key)
956 {
957 g_assert(item != NULL);
958 g_assert(SP_IS_ITEM(item));
960 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
961 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
962 }
964 SPItemView *ref = NULL;
965 SPItemView *v = item->display;
966 while (v != NULL) {
967 SPItemView *next = v->next;
968 if (v->key == key) {
969 if (item->clip_ref->getObject()) {
970 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
971 nr_arena_item_set_clip(v->arenaitem, NULL);
972 }
973 if (item->mask_ref->getObject()) {
974 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
975 nr_arena_item_set_mask(v->arenaitem, NULL);
976 }
977 if (!ref) {
978 item->display = v->next;
979 } else {
980 ref->next = v->next;
981 }
982 nr_arena_item_unparent(v->arenaitem);
983 nr_arena_item_unref(v->arenaitem);
984 g_free(v);
985 } else {
986 ref = v;
987 }
988 v = next;
989 }
990 }
992 // Adjusters
994 void
995 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
996 {
997 SPStyle *style = SP_OBJECT_STYLE (item);
999 if (style && (style->fill.isPaintserver())) {
1000 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1001 if (SP_IS_PATTERN (server)) {
1002 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1003 sp_pattern_transform_multiply (pattern, postmul, set);
1004 }
1005 }
1007 if (style && (style->stroke.isPaintserver())) {
1008 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1009 if (SP_IS_PATTERN (server)) {
1010 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1011 sp_pattern_transform_multiply (pattern, postmul, set);
1012 }
1013 }
1015 }
1017 void
1018 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1019 {
1020 SPStyle *style = SP_OBJECT_STYLE (item);
1022 if (style && (style->fill.isPaintserver())) {
1023 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1024 if (SP_IS_GRADIENT (server)) {
1026 /**
1027 * \note Bbox units for a gradient are generally a bad idea because
1028 * with them, you cannot preserve the relative position of the
1029 * object and its gradient after rotation or skew. So now we
1030 * convert them to userspace units which are easy to keep in sync
1031 * just by adding the object's transform to gradientTransform.
1032 * \todo FIXME: convert back to bbox units after transforming with
1033 * the item, so as to preserve the original units.
1034 */
1035 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1037 sp_gradient_transform_multiply (gradient, postmul, set);
1038 }
1039 }
1041 if (style && (style->stroke.isPaintserver())) {
1042 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1043 if (SP_IS_GRADIENT (server)) {
1044 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1045 sp_gradient_transform_multiply (gradient, postmul, set);
1046 }
1047 }
1048 }
1050 void
1051 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1052 {
1053 SPStyle *style = SP_OBJECT_STYLE (item);
1055 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1057 style->stroke_width.computed *= ex;
1058 style->stroke_width.set = TRUE;
1060 if (style->stroke_dash.n_dash != 0) {
1061 int i;
1062 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1063 style->stroke_dash.dash[i] *= ex;
1064 }
1065 style->stroke_dash.offset *= ex;
1066 }
1068 SP_OBJECT(item)->updateRepr();
1069 }
1070 }
1072 /**
1073 * Find out the inverse of previous transform of an item (from its repr)
1074 */
1075 NR::Matrix
1076 sp_item_transform_repr (SPItem *item)
1077 {
1078 NR::Matrix t_old(NR::identity());
1079 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1080 if (t_attr) {
1081 NR::Matrix t;
1082 if (sp_svg_transform_read(t_attr, &t)) {
1083 t_old = t;
1084 }
1085 }
1087 return t_old;
1088 }
1091 /**
1092 * Recursively scale stroke width in \a item and its children by \a expansion.
1093 */
1094 void
1095 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1096 {
1097 sp_item_adjust_stroke (item, expansion);
1099 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1100 if (item && SP_IS_USE(item))
1101 return;
1103 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1104 if (SP_IS_ITEM(o))
1105 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1106 }
1107 }
1109 /**
1110 * Recursively adjust rx and ry of rects.
1111 */
1112 void
1113 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1114 {
1115 if (SP_IS_RECT (item)) {
1116 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1117 }
1119 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1120 if (SP_IS_ITEM(o))
1121 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1122 }
1123 }
1125 /**
1126 * Recursively compensate pattern or gradient transform.
1127 */
1128 void
1129 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1130 {
1131 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1132 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1133 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1134 NR::Matrix t_item = sp_item_transform_repr (item);
1135 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1137 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1138 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1139 // we must not touch it
1140 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1141 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1142 if (SP_IS_ITEM(o)) {
1143 // At the level of the transformed item, t_ancestors is identity;
1144 // below it, it is the accmmulated chain of transforms from this level to the top level
1145 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1146 }
1147 }
1148 }
1150 // We recursed into children first, and are now adjusting this object second;
1151 // this is so that adjustments in a tree are done from leaves up to the root,
1152 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1153 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1155 if (is_pattern)
1156 sp_item_adjust_pattern (item, paint_delta);
1157 else
1158 sp_item_adjust_gradient (item, paint_delta);
1160 }
1162 void
1163 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1164 {
1165 if ( !SP_IS_SHAPE(item) )
1166 return;
1168 SPShape *shape = SP_SHAPE (item);
1169 if ( sp_shape_has_path_effect(shape) ) {
1170 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1171 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1172 if (new_lpeobj != lpeobj) {
1173 sp_shape_set_path_effect(shape, new_lpeobj);
1174 }
1176 Inkscape::LivePathEffect::Effect * effect = sp_shape_get_livepatheffect(shape);
1177 if (effect) {
1178 effect->transform_multiply (to_2geom(postmul), set);
1179 }
1180 }
1181 }
1183 /**
1184 * A temporary wrapper for the next function accepting the NRMatrix
1185 * instead of NR::Matrix
1186 */
1187 void
1188 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1189 {
1190 if (transform == NULL)
1191 sp_item_write_transform(item, repr, NR::identity(), adv);
1192 else
1193 sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1194 }
1196 /**
1197 * Set a new transform on an object.
1198 *
1199 * Compensate for stroke scaling and gradient/pattern fill transform, if
1200 * necessary. Call the object's set_transform method if transforms are
1201 * stored optimized. Send _transformed_signal. Invoke _write method so that
1202 * the repr is updated with the new transform.
1203 */
1204 void
1205 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1206 {
1207 g_return_if_fail(item != NULL);
1208 g_return_if_fail(SP_IS_ITEM(item));
1209 g_return_if_fail(repr != NULL);
1211 // calculate the relative transform, if not given by the adv attribute
1212 NR::Matrix advertized_transform;
1213 if (adv != NULL) {
1214 advertized_transform = *adv;
1215 } else {
1216 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1217 }
1219 if (compensate) {
1221 // recursively compensate for stroke scaling, depending on user preference
1222 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1223 double const expansion = 1. / NR::expansion(advertized_transform);
1224 sp_item_adjust_stroke_width_recursive(item, expansion);
1225 }
1227 // recursively compensate rx/ry of a rect if requested
1228 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1229 sp_item_adjust_rects_recursive(item, advertized_transform);
1230 }
1232 // recursively compensate pattern fill if it's not to be transformed
1233 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1234 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1235 }
1236 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1237 /// recursively compensate gradient fill if it's not to be transformed
1238 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1239 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1240 } else {
1241 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1242 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1243 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1244 }
1246 } // endif(compensate)
1248 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1249 NR::Matrix transform_attr (transform);
1250 if ( // run the object's set_transform (i.e. embed transform) only if:
1251 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1252 !preserve && // user did not chose to preserve all transforms
1253 !item->clip_ref->getObject() && // the object does not have a clippath
1254 !item->mask_ref->getObject() && // the object does not have a mask
1255 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1256 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1257 ) {
1258 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1259 }
1260 sp_item_set_item_transform(item, transform_attr);
1262 // Note: updateRepr comes before emitting the transformed signal since
1263 // it causes clone SPUse's copy of the original object to brought up to
1264 // date with the original. Otherwise, sp_use_bbox returns incorrect
1265 // values if called in code handling the transformed signal.
1266 SP_OBJECT(item)->updateRepr();
1268 // send the relative transform with a _transformed_signal
1269 item->_transformed_signal.emit(&advertized_transform, item);
1270 }
1272 gint
1273 sp_item_event(SPItem *item, SPEvent *event)
1274 {
1275 g_return_val_if_fail(item != NULL, FALSE);
1276 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1277 g_return_val_if_fail(event != NULL, FALSE);
1279 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1280 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1282 return FALSE;
1283 }
1285 /**
1286 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1287 * gradients, patterns as sp_item_write_transform does.
1288 */
1289 void
1290 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1291 {
1292 g_return_if_fail(item != NULL);
1293 g_return_if_fail(SP_IS_ITEM(item));
1295 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1296 item->transform = transform;
1297 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1298 transformation. It's apparently not used anywhere else. */
1299 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1300 sp_item_rm_unsatisfied_cns(*item);
1301 }
1302 }
1305 /**
1306 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1307 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1308 */
1309 NR::Matrix
1310 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1311 NR::Matrix ret(NR::identity());
1312 g_return_val_if_fail(object != NULL, ret);
1314 /* stop at first non-renderable ancestor */
1315 while ( object != ancestor && SP_IS_ITEM(object) ) {
1316 if (SP_IS_ROOT(object)) {
1317 ret *= SP_ROOT(object)->c2p;
1318 }
1319 ret *= SP_ITEM(object)->transform;
1320 object = SP_OBJECT_PARENT(object);
1321 }
1322 return ret;
1323 }
1325 NR::Matrix
1326 i2i_affine(SPObject const *src, SPObject const *dest) {
1327 g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1328 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1329 return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1330 }
1332 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1333 return i2i_affine(this, dest);
1334 }
1336 /**
1337 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1338 * \pre (item != NULL) and SP_IS_ITEM(item).
1339 */
1340 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1341 {
1342 return i2anc_affine(item, NULL);
1343 }
1345 /**
1346 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1347 * Used in path operations mostly.
1348 * \pre (item != NULL) and SP_IS_ITEM(item).
1349 */
1350 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1351 {
1352 g_assert(item != NULL);
1353 g_assert(SP_IS_ITEM(item));
1355 NR::Matrix ret(NR::identity());
1356 g_assert(ret.test_identity());
1357 while ( NULL != SP_OBJECT_PARENT(item) ) {
1358 ret *= item->transform;
1359 item = SP_ITEM(SP_OBJECT_PARENT(item));
1360 }
1361 g_assert(SP_IS_ROOT(item));
1363 ret *= item->transform;
1365 return ret;
1366 }
1368 /* fixme: This is EVIL!!! */
1370 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1371 {
1372 g_assert(item != NULL);
1373 g_assert(SP_IS_ITEM(item));
1375 NR::Matrix const ret( sp_item_i2doc_affine(item)
1376 * NR::scale(1, -1)
1377 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1378 return ret;
1379 }
1381 // same as i2d but with i2root instead of i2doc
1382 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1383 {
1384 g_assert(item != NULL);
1385 g_assert(SP_IS_ITEM(item));
1387 NR::Matrix const ret( sp_item_i2root_affine(item)
1388 * NR::scale(1, -1)
1389 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1390 return ret;
1391 }
1393 /**
1394 * Converts a matrix \a m into the desktop coords of the \a item.
1395 * Will become a noop when we eliminate the coordinate flipping.
1396 */
1397 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1398 {
1399 NR::Matrix const ret(m
1400 * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1401 * NR::scale(1, -1));
1402 return ret;
1403 }
1405 /**
1406 * Converts a matrix \a m from the desktop coords of the \a item.
1407 * Will become a noop when we eliminate the coordinate flipping.
1408 */
1409 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1410 {
1411 NR::Matrix const ret(NR::scale(1, -1)
1412 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1413 * m);
1414 return ret;
1415 }
1417 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1418 {
1419 g_return_if_fail( item != NULL );
1420 g_return_if_fail( SP_IS_ITEM(item) );
1422 NR::Matrix dt2p; /* desktop to item parent transform */
1423 if (SP_OBJECT_PARENT(item)) {
1424 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1425 } else {
1426 dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1427 * NR::scale(1, -1) );
1428 }
1430 NR::Matrix const i2p( i2dt * dt2p );
1431 sp_item_set_item_transform(item, i2p);
1432 }
1435 NR::Matrix
1436 sp_item_dt2i_affine(SPItem const *item)
1437 {
1438 /* fixme: Implement the right way (Lauris) */
1439 return sp_item_i2d_affine(item).inverse();
1440 }
1442 /* Item views */
1444 static SPItemView *
1445 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1446 {
1447 SPItemView *new_view;
1449 g_assert(item != NULL);
1450 g_assert(SP_IS_ITEM(item));
1451 g_assert(arenaitem != NULL);
1452 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1454 new_view = g_new(SPItemView, 1);
1456 new_view->next = list;
1457 new_view->flags = flags;
1458 new_view->key = key;
1459 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1461 return new_view;
1462 }
1464 static SPItemView *
1465 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1466 {
1467 if (view == list) {
1468 list = list->next;
1469 } else {
1470 SPItemView *prev;
1471 prev = list;
1472 while (prev->next != view) prev = prev->next;
1473 prev->next = view->next;
1474 }
1476 nr_arena_item_unref(view->arenaitem);
1477 g_free(view);
1479 return list;
1480 }
1482 /**
1483 * Return the arenaitem corresponding to the given item in the display
1484 * with the given key
1485 */
1486 NRArenaItem *
1487 sp_item_get_arenaitem(SPItem *item, unsigned key)
1488 {
1489 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1490 if ( iv->key == key ) {
1491 return iv->arenaitem;
1492 }
1493 }
1495 return NULL;
1496 }
1498 int
1499 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1500 {
1501 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1502 SP_OBJECT_REPR(second));
1503 }
1505 SPItem *
1506 sp_item_first_item_child (SPObject *obj)
1507 {
1508 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1509 if (SP_IS_ITEM (iter))
1510 return SP_ITEM (iter);
1511 }
1512 return NULL;
1513 }
1516 /*
1517 Local Variables:
1518 mode:c++
1519 c-file-style:"stroustrup"
1520 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1521 indent-tabs-mode:nil
1522 fill-column:99
1523 End:
1524 */
1525 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :