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"
54 #include "libnr/nr-matrix-div.h"
55 #include "libnr/nr-matrix-fns.h"
56 #include "libnr/nr-matrix-scale-ops.h"
57 #include "libnr/nr-matrix-translate-ops.h"
58 #include "libnr/nr-scale-translate-ops.h"
59 #include "libnr/nr-translate-scale-ops.h"
60 #include "algorithms/find-last-if.h"
61 #include "util/reverse-list.h"
63 #include "xml/repr.h"
65 #define noSP_ITEM_DEBUG_IDLE
67 static void sp_item_class_init(SPItemClass *klass);
68 static void sp_item_init(SPItem *item);
70 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
71 static void sp_item_release(SPObject *object);
72 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
73 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
74 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
76 static gchar *sp_item_private_description(SPItem *item);
77 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
79 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
80 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
82 static SPObjectClass *parent_class;
84 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
85 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
87 /**
88 * Registers SPItem class and returns its type number.
89 */
90 GType
91 sp_item_get_type(void)
92 {
93 static GType type = 0;
94 if (!type) {
95 GTypeInfo info = {
96 sizeof(SPItemClass),
97 NULL, NULL,
98 (GClassInitFunc) sp_item_class_init,
99 NULL, NULL,
100 sizeof(SPItem),
101 16,
102 (GInstanceInitFunc) sp_item_init,
103 NULL, /* value_table */
104 };
105 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
106 }
107 return type;
108 }
110 /**
111 * SPItem vtable initialization.
112 */
113 static void
114 sp_item_class_init(SPItemClass *klass)
115 {
116 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
118 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
120 sp_object_class->build = sp_item_build;
121 sp_object_class->release = sp_item_release;
122 sp_object_class->set = sp_item_set;
123 sp_object_class->update = sp_item_update;
124 sp_object_class->write = sp_item_write;
126 klass->description = sp_item_private_description;
127 klass->snappoints = sp_item_private_snappoints;
128 }
130 /**
131 * Callback for SPItem object initialization.
132 */
133 static void
134 sp_item_init(SPItem *item)
135 {
136 item->init();
137 }
139 void SPItem::init() {
140 this->sensitive = TRUE;
142 this->transform_center_x = 0;
143 this->transform_center_y = 0;
145 this->_is_evaluated = true;
146 this->_evaluated_status = StatusUnknown;
148 this->transform = NR::identity();
150 this->display = NULL;
152 this->clip_ref = new SPClipPathReference(this);
153 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
154 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
155 _clip_ref_connection = cs1.connect(sl1);
157 this->mask_ref = new SPMaskReference(this);
158 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
159 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
160 _mask_ref_connection = cs2.connect(sl2);
162 this->avoidRef = new SPAvoidRef(this);
164 new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
165 }
167 bool SPItem::isVisibleAndUnlocked() const {
168 return (!isHidden() && !isLocked());
169 }
171 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
172 return (!isHidden(display_key) && !isLocked());
173 }
175 bool SPItem::isLocked() const {
176 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
177 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
178 return true;
179 }
180 return false;
181 }
183 void SPItem::setLocked(bool locked) {
184 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
185 ( locked ? "1" : NULL ));
186 updateRepr();
187 }
189 bool SPItem::isHidden() const {
190 if (!isEvaluated())
191 return true;
192 return style->display.computed == SP_CSS_DISPLAY_NONE;
193 }
195 void SPItem::setHidden(bool hide) {
196 style->display.set = TRUE;
197 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
198 style->display.computed = style->display.value;
199 style->display.inherit = FALSE;
200 updateRepr();
201 }
203 bool SPItem::isHidden(unsigned display_key) const {
204 if (!isEvaluated())
205 return true;
206 for ( SPItemView *view(display) ; view ; view = view->next ) {
207 if ( view->key == display_key ) {
208 g_assert(view->arenaitem != NULL);
209 for ( NRArenaItem *arenaitem = view->arenaitem ;
210 arenaitem ; arenaitem = arenaitem->parent )
211 {
212 if (!arenaitem->visible) {
213 return true;
214 }
215 }
216 return false;
217 }
218 }
219 return true;
220 }
222 void SPItem::setEvaluated(bool evaluated) {
223 _is_evaluated = evaluated;
224 _evaluated_status = StatusSet;
225 }
227 void SPItem::resetEvaluated() {
228 if ( StatusCalculated == _evaluated_status ) {
229 _evaluated_status = StatusUnknown;
230 bool oldValue = _is_evaluated;
231 if ( oldValue != isEvaluated() ) {
232 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
233 }
234 } if ( StatusSet == _evaluated_status ) {
235 SPObject const *const parent = SP_OBJECT_PARENT(this);
236 if (SP_IS_SWITCH(parent)) {
237 SP_SWITCH(parent)->resetChildEvaluated();
238 }
239 }
240 }
242 bool SPItem::isEvaluated() const {
243 if ( StatusUnknown == _evaluated_status ) {
244 _is_evaluated = sp_item_evaluate(this);
245 _evaluated_status = StatusCalculated;
246 }
247 return _is_evaluated;
248 }
250 /**
251 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
252 * Corresponds to setExplicitlyHidden.
253 */
254 bool
255 SPItem::isExplicitlyHidden() const
256 {
257 return (this->style->display.set
258 && this->style->display.value == SP_CSS_DISPLAY_NONE);
259 }
261 /**
262 * Sets the display CSS property to `hidden' if \a val is true,
263 * otherwise makes it unset
264 */
265 void
266 SPItem::setExplicitlyHidden(bool const val) {
267 this->style->display.set = val;
268 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
269 this->style->display.computed = this->style->display.value;
270 this->updateRepr();
271 }
273 /**
274 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
275 */
276 void
277 SPItem::setCenter(NR::Point object_centre) {
278 NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
279 if (bbox) {
280 transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
281 if (fabs(transform_center_x) < 1e-5) // rounding error
282 transform_center_x = 0;
283 transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
284 if (fabs(transform_center_y) < 1e-5) // rounding error
285 transform_center_y = 0;
286 }
287 }
289 void
290 SPItem::unsetCenter() {
291 transform_center_x = 0;
292 transform_center_y = 0;
293 }
295 bool SPItem::isCenterSet() {
296 return (transform_center_x != 0 || transform_center_y != 0);
297 }
299 NR::Point SPItem::getCenter() const {
300 NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
301 if (bbox) {
302 return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
303 } else {
304 return NR::Point (0, 0); // something's wrong!
305 }
306 }
309 namespace {
311 bool is_item(SPObject const &object) {
312 return SP_IS_ITEM(&object);
313 }
315 }
317 void SPItem::raiseToTop() {
318 using Inkscape::Algorithms::find_last_if;
320 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
321 SP_OBJECT_NEXT(this), NULL, &is_item
322 );
323 if (topmost) {
324 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
325 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
326 }
327 }
329 void SPItem::raiseOne() {
330 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
331 SP_OBJECT_NEXT(this), NULL, &is_item
332 );
333 if (next_higher) {
334 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
335 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
336 sp_repr_parent(repr)->changeOrder(repr, ref);
337 }
338 }
340 void SPItem::lowerOne() {
341 using Inkscape::Util::MutableList;
342 using Inkscape::Util::reverse_list;
344 MutableList<SPObject &> next_lower=std::find_if(
345 reverse_list<SPObject::SiblingIterator>(
346 SP_OBJECT_PARENT(this)->firstChild(), this
347 ),
348 MutableList<SPObject &>(),
349 &is_item
350 );
351 if (next_lower) {
352 ++next_lower;
353 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
354 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
355 sp_repr_parent(repr)->changeOrder(repr, ref);
356 }
357 }
359 void SPItem::lowerToBottom() {
360 using Inkscape::Algorithms::find_last_if;
361 using Inkscape::Util::MutableList;
362 using Inkscape::Util::reverse_list;
364 MutableList<SPObject &> bottom=find_last_if(
365 reverse_list<SPObject::SiblingIterator>(
366 SP_OBJECT_PARENT(this)->firstChild(), this
367 ),
368 MutableList<SPObject &>(),
369 &is_item
370 );
371 if (bottom) {
372 ++bottom;
373 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
374 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
375 sp_repr_parent(repr)->changeOrder(repr, ref);
376 }
377 }
379 static void
380 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
381 {
382 sp_object_read_attr(object, "style");
383 sp_object_read_attr(object, "transform");
384 sp_object_read_attr(object, "clip-path");
385 sp_object_read_attr(object, "mask");
386 sp_object_read_attr(object, "sodipodi:insensitive");
387 sp_object_read_attr(object, "sodipodi:nonprintable");
388 sp_object_read_attr(object, "inkscape:transform-center-x");
389 sp_object_read_attr(object, "inkscape:transform-center-y");
390 sp_object_read_attr(object, "inkscape:connector-avoid");
392 if (((SPObjectClass *) (parent_class))->build) {
393 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
394 }
395 }
397 static void
398 sp_item_release(SPObject *object)
399 {
400 SPItem *item = (SPItem *) object;
402 item->_clip_ref_connection.disconnect();
403 item->_mask_ref_connection.disconnect();
405 if (item->clip_ref) {
406 item->clip_ref->detach();
407 delete item->clip_ref;
408 item->clip_ref = NULL;
409 }
411 if (item->mask_ref) {
412 item->mask_ref->detach();
413 delete item->mask_ref;
414 item->mask_ref = NULL;
415 }
417 if (item->avoidRef) {
418 delete item->avoidRef;
419 item->avoidRef = NULL;
420 }
422 if (((SPObjectClass *) (parent_class))->release) {
423 ((SPObjectClass *) parent_class)->release(object);
424 }
426 while (item->display) {
427 nr_arena_item_unparent(item->display->arenaitem);
428 item->display = sp_item_view_list_remove(item->display, item->display);
429 }
431 item->_transformed_signal.~signal();
432 }
434 static void
435 sp_item_set(SPObject *object, unsigned key, gchar const *value)
436 {
437 SPItem *item = (SPItem *) object;
439 switch (key) {
440 case SP_ATTR_TRANSFORM: {
441 NR::Matrix t;
442 if (value && sp_svg_transform_read(value, &t)) {
443 sp_item_set_item_transform(item, t);
444 } else {
445 sp_item_set_item_transform(item, NR::identity());
446 }
447 break;
448 }
449 case SP_PROP_CLIP_PATH: {
450 gchar *uri = Inkscape::parse_css_url(value);
451 if (uri) {
452 try {
453 item->clip_ref->attach(Inkscape::URI(uri));
454 } catch (Inkscape::BadURIException &e) {
455 g_warning("%s", e.what());
456 item->clip_ref->detach();
457 }
458 g_free(uri);
459 } else {
460 item->clip_ref->detach();
461 }
463 break;
464 }
465 case SP_PROP_MASK: {
466 gchar *uri=Inkscape::parse_css_url(value);
467 if (uri) {
468 try {
469 item->mask_ref->attach(Inkscape::URI(uri));
470 } catch (Inkscape::BadURIException &e) {
471 g_warning("%s", e.what());
472 item->mask_ref->detach();
473 }
474 g_free(uri);
475 } else {
476 item->mask_ref->detach();
477 }
479 break;
480 }
481 case SP_ATTR_SODIPODI_INSENSITIVE:
482 item->sensitive = !value;
483 for (SPItemView *v = item->display; v != NULL; v = v->next) {
484 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
485 }
486 break;
487 case SP_ATTR_CONNECTOR_AVOID:
488 item->avoidRef->setAvoid(value);
489 break;
490 case SP_ATTR_TRANSFORM_CENTER_X:
491 if (value) {
492 item->transform_center_x = g_strtod(value, NULL);
493 } else {
494 item->transform_center_x = 0;
495 }
496 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
497 break;
498 case SP_ATTR_TRANSFORM_CENTER_Y:
499 if (value) {
500 item->transform_center_y = g_strtod(value, NULL);
501 } else {
502 item->transform_center_y = 0;
503 }
504 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
505 break;
506 case SP_PROP_SYSTEM_LANGUAGE:
507 case SP_PROP_REQUIRED_FEATURES:
508 case SP_PROP_REQUIRED_EXTENSIONS:
509 {
510 item->resetEvaluated();
511 // pass to default handler
512 }
513 default:
514 if (SP_ATTRIBUTE_IS_CSS(key)) {
515 sp_style_read_from_object(object->style, object);
516 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
517 } else {
518 if (((SPObjectClass *) (parent_class))->set) {
519 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
520 }
521 }
522 break;
523 }
524 }
526 static void
527 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
528 {
529 if (old_clip) {
530 SPItemView *v;
531 /* Hide clippath */
532 for (v = item->display; v != NULL; v = v->next) {
533 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
534 nr_arena_item_set_clip(v->arenaitem, NULL);
535 }
536 }
537 if (SP_IS_CLIPPATH(clip)) {
538 NRRect bbox;
539 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
540 for (SPItemView *v = item->display; v != NULL; v = v->next) {
541 if (!v->arenaitem->key) {
542 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
543 }
544 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
545 NR_ARENA_ITEM_ARENA(v->arenaitem),
546 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
547 nr_arena_item_set_clip(v->arenaitem, ai);
548 nr_arena_item_unref(ai);
549 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
550 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
551 }
552 }
553 }
555 static void
556 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
557 {
558 if (old_mask) {
559 /* Hide mask */
560 for (SPItemView *v = item->display; v != NULL; v = v->next) {
561 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
562 nr_arena_item_set_mask(v->arenaitem, NULL);
563 }
564 }
565 if (SP_IS_MASK(mask)) {
566 NRRect bbox;
567 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
568 for (SPItemView *v = item->display; v != NULL; v = v->next) {
569 if (!v->arenaitem->key) {
570 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
571 }
572 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
573 NR_ARENA_ITEM_ARENA(v->arenaitem),
574 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
575 nr_arena_item_set_mask(v->arenaitem, ai);
576 nr_arena_item_unref(ai);
577 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
578 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
579 }
580 }
581 }
583 static void
584 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
585 {
586 SPItem *item = SP_ITEM(object);
588 if (((SPObjectClass *) (parent_class))->update)
589 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
591 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
592 if (flags & SP_OBJECT_MODIFIED_FLAG) {
593 for (SPItemView *v = item->display; v != NULL; v = v->next) {
594 nr_arena_item_set_transform(v->arenaitem, item->transform);
595 }
596 }
598 SPClipPath *clip_path = item->clip_ref->getObject();
599 SPMask *mask = item->mask_ref->getObject();
601 if ( clip_path || mask ) {
602 NRRect bbox;
603 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
604 if (clip_path) {
605 for (SPItemView *v = item->display; v != NULL; v = v->next) {
606 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
607 }
608 }
609 if (mask) {
610 for (SPItemView *v = item->display; v != NULL; v = v->next) {
611 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
612 }
613 }
614 }
616 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
617 for (SPItemView *v = item->display; v != NULL; v = v->next) {
618 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
619 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
620 }
621 }
622 }
624 // Update libavoid with item geometry (for connector routing).
625 item->avoidRef->handleSettingChange();
626 }
628 static Inkscape::XML::Node *
629 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
630 {
631 SPItem *item = SP_ITEM(object);
633 gchar *c = sp_svg_transform_write(item->transform);
634 repr->setAttribute("transform", c);
635 g_free(c);
637 if (flags & SP_OBJECT_WRITE_EXT) {
638 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
639 if (item->transform_center_x != 0)
640 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
641 else
642 repr->setAttribute ("inkscape:transform-center-x", NULL);
643 if (item->transform_center_y != 0)
644 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
645 else
646 repr->setAttribute ("inkscape:transform-center-y", NULL);
647 }
649 if (item->clip_ref->getObject()) {
650 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
651 repr->setAttribute ("clip-path", value);
652 g_free ((void *) value);
653 }
654 if (item->mask_ref->getObject()) {
655 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
656 repr->setAttribute ("mask", value);
657 g_free ((void *) value);
658 }
660 if (((SPObjectClass *) (parent_class))->write) {
661 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
662 }
664 return repr;
665 }
667 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
668 SPItem::BBoxType type,
669 unsigned int dkey) const
670 {
671 NRRect r;
672 sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
673 return r;
674 }
676 void
677 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
678 {
679 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
680 }
682 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
683 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
684 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
685 * clones), in turn, call this function in their bbox methods. */
686 void
687 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
688 {
689 g_assert(item != NULL);
690 g_assert(SP_IS_ITEM(item));
691 g_assert(bbox != NULL);
693 if (clear) {
694 bbox->x0 = bbox->y0 = 1e18;
695 bbox->x1 = bbox->y1 = -1e18;
696 }
698 NRRect this_bbox;
699 this_bbox.x0 = this_bbox.y0 = 1e18;
700 this_bbox.x1 = this_bbox.y1 = -1e18;
702 // call the subclass method
703 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
704 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
705 }
707 // unless this is geometric bbox, crop the bbox by clip path, if any
708 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
709 NRRect b;
710 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
711 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
712 }
714 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
715 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
716 nr_rect_d_union (bbox, bbox, &this_bbox);
717 }
718 }
720 unsigned sp_item_pos_in_parent(SPItem *item)
721 {
722 g_assert(item != NULL);
723 g_assert(SP_IS_ITEM(item));
725 SPObject *parent = SP_OBJECT_PARENT(item);
726 g_assert(parent != NULL);
727 g_assert(SP_IS_OBJECT(parent));
729 SPObject *object = SP_OBJECT(item);
731 unsigned pos=0;
732 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
733 if ( iter == object ) {
734 return pos;
735 }
736 if (SP_IS_ITEM(iter)) {
737 pos++;
738 }
739 }
741 g_assert_not_reached();
742 return 0;
743 }
745 void
746 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
747 {
748 g_assert(item != NULL);
749 g_assert(SP_IS_ITEM(item));
750 g_assert(bbox != NULL);
752 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
753 }
755 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
756 {
757 NRRect ret;
758 sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE, type);
759 return ret.upgrade();
760 }
762 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
763 {
764 NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
765 /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
766 support angled guide lines. */
768 if (bbox) {
769 *p = bbox->min();
770 *p = bbox->max();
771 }
772 }
774 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
775 {
776 g_assert (item != NULL);
777 g_assert (SP_IS_ITEM(item));
779 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
780 if (item_class.snappoints) {
781 item_class.snappoints(item, p);
782 }
783 }
785 void
786 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
787 {
788 if (!item->isHidden()) {
789 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
790 if (!item->transform.test_identity()
791 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
792 {
793 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
794 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
795 sp_print_release(ctx);
796 } else {
797 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
798 }
799 }
800 }
801 }
803 static gchar *
804 sp_item_private_description(SPItem *item)
805 {
806 return g_strdup(_("Object"));
807 }
809 /**
810 * Returns a string suitable for status bar, formatted in pango markup language.
811 *
812 * Must be freed by caller.
813 */
814 gchar *
815 sp_item_description(SPItem *item)
816 {
817 g_assert(item != NULL);
818 g_assert(SP_IS_ITEM(item));
820 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
821 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
822 if (s && item->clip_ref->getObject()) {
823 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
824 g_free (s);
825 s = snew;
826 }
827 if (s && item->mask_ref->getObject()) {
828 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
829 g_free (s);
830 s = snew;
831 }
832 return s;
833 }
835 g_assert_not_reached();
836 return NULL;
837 }
839 /**
840 * Allocates unique integer keys.
841 * \param numkeys Number of keys required.
842 * \return First allocated key; hence if the returned key is n
843 * you can use n, n + 1, ..., n + (numkeys - 1)
844 */
845 unsigned
846 sp_item_display_key_new(unsigned numkeys)
847 {
848 static unsigned dkey = 0;
850 dkey += numkeys;
852 return dkey - numkeys;
853 }
855 NRArenaItem *
856 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
857 {
858 g_assert(item != NULL);
859 g_assert(SP_IS_ITEM(item));
860 g_assert(arena != NULL);
861 g_assert(NR_IS_ARENA(arena));
863 NRArenaItem *ai = NULL;
864 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
865 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
866 }
868 if (ai != NULL) {
869 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
870 nr_arena_item_set_transform(ai, item->transform);
871 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
872 nr_arena_item_set_visible(ai, !item->isHidden());
873 nr_arena_item_set_sensitive(ai, item->sensitive);
874 if (item->clip_ref->getObject()) {
875 SPClipPath *cp = item->clip_ref->getObject();
877 if (!item->display->arenaitem->key) {
878 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
879 }
880 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
882 // Show and set clip
883 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
884 nr_arena_item_set_clip(ai, ac);
885 nr_arena_item_unref(ac);
887 // Update bbox, in case the clip uses bbox units
888 NRRect bbox;
889 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
890 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
891 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
892 }
893 if (item->mask_ref->getObject()) {
894 SPMask *mask = item->mask_ref->getObject();
896 if (!item->display->arenaitem->key) {
897 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
898 }
899 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
901 // Show and set mask
902 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
903 nr_arena_item_set_mask(ai, ac);
904 nr_arena_item_unref(ac);
906 // Update bbox, in case the mask uses bbox units
907 NRRect bbox;
908 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
909 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
910 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
911 }
912 NR_ARENA_ITEM_SET_DATA(ai, item);
913 }
915 return ai;
916 }
918 void
919 sp_item_invoke_hide(SPItem *item, unsigned key)
920 {
921 g_assert(item != NULL);
922 g_assert(SP_IS_ITEM(item));
924 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
925 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
926 }
928 SPItemView *ref = NULL;
929 SPItemView *v = item->display;
930 while (v != NULL) {
931 SPItemView *next = v->next;
932 if (v->key == key) {
933 if (item->clip_ref->getObject()) {
934 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
935 nr_arena_item_set_clip(v->arenaitem, NULL);
936 }
937 if (item->mask_ref->getObject()) {
938 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
939 nr_arena_item_set_mask(v->arenaitem, NULL);
940 }
941 if (!ref) {
942 item->display = v->next;
943 } else {
944 ref->next = v->next;
945 }
946 nr_arena_item_unparent(v->arenaitem);
947 nr_arena_item_unref(v->arenaitem);
948 g_free(v);
949 } else {
950 ref = v;
951 }
952 v = next;
953 }
954 }
956 // Adjusters
958 void
959 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
960 {
961 SPStyle *style = SP_OBJECT_STYLE (item);
963 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
964 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
965 if (SP_IS_PATTERN (server)) {
966 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
967 sp_pattern_transform_multiply (pattern, postmul, set);
968 }
969 }
971 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
972 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
973 if (SP_IS_PATTERN (server)) {
974 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
975 sp_pattern_transform_multiply (pattern, postmul, set);
976 }
977 }
979 }
981 void
982 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
983 {
984 SPStyle *style = SP_OBJECT_STYLE (item);
986 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
987 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
988 if (SP_IS_GRADIENT (server)) {
990 /**
991 * \note Bbox units for a gradient are generally a bad idea because
992 * with them, you cannot preserve the relative position of the
993 * object and its gradient after rotation or skew. So now we
994 * convert them to userspace units which are easy to keep in sync
995 * just by adding the object's transform to gradientTransform.
996 * \todo FIXME: convert back to bbox units after transforming with
997 * the item, so as to preserve the original units.
998 */
999 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1001 sp_gradient_transform_multiply (gradient, postmul, set);
1002 }
1003 }
1005 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1006 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1007 if (SP_IS_GRADIENT (server)) {
1008 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1009 sp_gradient_transform_multiply (gradient, postmul, set);
1010 }
1011 }
1012 }
1014 void
1015 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1016 {
1017 SPStyle *style = SP_OBJECT_STYLE (item);
1019 if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1021 style->stroke_width.computed *= ex;
1022 style->stroke_width.set = TRUE;
1024 if (style->stroke_dash.n_dash != 0) {
1025 int i;
1026 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1027 style->stroke_dash.dash[i] *= ex;
1028 }
1029 style->stroke_dash.offset *= ex;
1030 }
1032 SP_OBJECT(item)->updateRepr();
1033 }
1034 }
1036 /**
1037 * Find out the inverse of previous transform of an item (from its repr)
1038 */
1039 NR::Matrix
1040 sp_item_transform_repr (SPItem *item)
1041 {
1042 NR::Matrix t_old(NR::identity());
1043 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1044 if (t_attr) {
1045 NR::Matrix t;
1046 if (sp_svg_transform_read(t_attr, &t)) {
1047 t_old = t;
1048 }
1049 }
1051 return t_old;
1052 }
1055 /**
1056 * Recursively scale stroke width in \a item and its children by \a expansion.
1057 */
1058 void
1059 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1060 {
1061 sp_item_adjust_stroke (item, expansion);
1063 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1064 if (item && SP_IS_USE(item))
1065 return;
1067 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1068 if (SP_IS_ITEM(o))
1069 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1070 }
1071 }
1073 /**
1074 * Recursively adjust rx and ry of rects.
1075 */
1076 void
1077 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1078 {
1079 if (SP_IS_RECT (item)) {
1080 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1081 }
1083 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1084 if (SP_IS_ITEM(o))
1085 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1086 }
1087 }
1089 /**
1090 * Recursively compensate pattern or gradient transform.
1091 */
1092 void
1093 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1094 {
1095 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1096 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1097 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1098 NR::Matrix t_item = sp_item_transform_repr (item);
1099 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1101 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1102 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1103 // we must not touch it
1104 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1105 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1106 if (SP_IS_ITEM(o)) {
1107 // At the level of the transformed item, t_ancestors is identity;
1108 // below it, it is the accmmulated chain of transforms from this level to the top level
1109 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1110 }
1111 }
1112 }
1114 // We recursed into children first, and are now adjusting this object second;
1115 // this is so that adjustments in a tree are done from leaves up to the root,
1116 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1117 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1119 if (is_pattern)
1120 sp_item_adjust_pattern (item, paint_delta);
1121 else
1122 sp_item_adjust_gradient (item, paint_delta);
1124 }
1126 /**
1127 * A temporary wrapper for the next function accepting the NRMatrix
1128 * instead of NR::Matrix
1129 */
1130 void
1131 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1132 {
1133 if (transform == NULL)
1134 sp_item_write_transform(item, repr, NR::identity(), adv);
1135 else
1136 sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1137 }
1139 /**
1140 * Set a new transform on an object.
1141 *
1142 * Compensate for stroke scaling and gradient/pattern fill transform, if
1143 * necessary. Call the object's set_transform method if transforms are
1144 * stored optimized. Send _transformed_signal. Invoke _write method so that
1145 * the repr is updated with the new transform.
1146 */
1147 void
1148 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1149 {
1150 g_return_if_fail(item != NULL);
1151 g_return_if_fail(SP_IS_ITEM(item));
1152 g_return_if_fail(repr != NULL);
1154 // calculate the relative transform, if not given by the adv attribute
1155 NR::Matrix advertized_transform;
1156 if (adv != NULL) {
1157 advertized_transform = *adv;
1158 } else {
1159 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1160 }
1162 if (compensate) {
1164 // recursively compensate for stroke scaling, depending on user preference
1165 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1166 double const expansion = 1. / NR::expansion(advertized_transform);
1167 sp_item_adjust_stroke_width_recursive(item, expansion);
1168 }
1170 // recursively compensate rx/ry of a rect if requested
1171 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1172 sp_item_adjust_rects_recursive(item, advertized_transform);
1173 }
1175 // recursively compensate pattern fill if it's not to be transformed
1176 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1177 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1178 }
1179 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1180 /// recursively compensate gradient fill if it's not to be transformed
1181 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1182 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1183 } else {
1184 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1185 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1186 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1187 }
1189 } // endif(compensate)
1191 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1192 NR::Matrix transform_attr (transform);
1193 if ( // run the object's set_transform (i.e. embed transform) only if:
1194 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1195 !preserve && // user did not chose to preserve all transforms
1196 !item->clip_ref->getObject() && // the object does not have a clippath
1197 !item->mask_ref->getObject() && // the object does not have a mask
1198 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.filter)
1199 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1200 ) {
1201 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1202 }
1203 sp_item_set_item_transform(item, transform_attr);
1205 // Note: updateRepr comes before emitting the transformed signal since
1206 // it causes clone SPUse's copy of the original object to brought up to
1207 // date with the original. Otherwise, sp_use_bbox returns incorrect
1208 // values if called in code handling the transformed signal.
1209 SP_OBJECT(item)->updateRepr();
1211 // send the relative transform with a _transformed_signal
1212 item->_transformed_signal.emit(&advertized_transform, item);
1213 }
1215 gint
1216 sp_item_event(SPItem *item, SPEvent *event)
1217 {
1218 g_return_val_if_fail(item != NULL, FALSE);
1219 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1220 g_return_val_if_fail(event != NULL, FALSE);
1222 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1223 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1225 return FALSE;
1226 }
1228 /**
1229 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1230 * gradients, patterns as sp_item_write_transform does.
1231 */
1232 void
1233 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1234 {
1235 g_return_if_fail(item != NULL);
1236 g_return_if_fail(SP_IS_ITEM(item));
1238 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1239 item->transform = transform;
1240 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1241 transformation. It's apparently not used anywhere else. */
1242 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1243 sp_item_rm_unsatisfied_cns(*item);
1244 }
1245 }
1248 /**
1249 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1250 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1251 */
1252 NR::Matrix
1253 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1254 NR::Matrix ret(NR::identity());
1255 g_return_val_if_fail(object != NULL, ret);
1257 /* stop at first non-renderable ancestor */
1258 while ( object != ancestor && SP_IS_ITEM(object) ) {
1259 if (SP_IS_ROOT(object)) {
1260 ret *= SP_ROOT(object)->c2p;
1261 }
1262 ret *= SP_ITEM(object)->transform;
1263 object = SP_OBJECT_PARENT(object);
1264 }
1265 return ret;
1266 }
1268 NR::Matrix
1269 i2i_affine(SPObject const *src, SPObject const *dest) {
1270 g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1271 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1272 return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1273 }
1275 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1276 return i2i_affine(this, dest);
1277 }
1279 /**
1280 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1281 * \pre (item != NULL) and SP_IS_ITEM(item).
1282 */
1283 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1284 {
1285 return i2anc_affine(item, NULL);
1286 }
1288 /**
1289 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1290 * Used in path operations mostly.
1291 * \pre (item != NULL) and SP_IS_ITEM(item).
1292 */
1293 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1294 {
1295 g_assert(item != NULL);
1296 g_assert(SP_IS_ITEM(item));
1298 NR::Matrix ret(NR::identity());
1299 g_assert(ret.test_identity());
1300 while ( NULL != SP_OBJECT_PARENT(item) ) {
1301 ret *= item->transform;
1302 item = SP_ITEM(SP_OBJECT_PARENT(item));
1303 }
1304 g_assert(SP_IS_ROOT(item));
1306 ret *= item->transform;
1308 return ret;
1309 }
1311 /* fixme: This is EVIL!!! */
1313 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1314 {
1315 g_assert(item != NULL);
1316 g_assert(SP_IS_ITEM(item));
1318 NR::Matrix const ret( sp_item_i2doc_affine(item)
1319 * NR::scale(1, -1)
1320 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1321 return ret;
1322 }
1324 // same as i2d but with i2root instead of i2doc
1325 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1326 {
1327 g_assert(item != NULL);
1328 g_assert(SP_IS_ITEM(item));
1330 NR::Matrix const ret( sp_item_i2root_affine(item)
1331 * NR::scale(1, -1)
1332 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1333 return ret;
1334 }
1336 /**
1337 * Converts a matrix \a m into the desktop coords of the \a item.
1338 * Will become a noop when we eliminate the coordinate flipping.
1339 */
1340 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1341 {
1342 NR::Matrix const ret(m
1343 * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1344 * NR::scale(1, -1));
1345 return ret;
1346 }
1348 /**
1349 * Converts a matrix \a m from the desktop coords of the \a item.
1350 * Will become a noop when we eliminate the coordinate flipping.
1351 */
1352 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1353 {
1354 NR::Matrix const ret(NR::scale(1, -1)
1355 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1356 * m);
1357 return ret;
1358 }
1360 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1361 {
1362 g_return_if_fail( item != NULL );
1363 g_return_if_fail( SP_IS_ITEM(item) );
1365 NR::Matrix dt2p; /* desktop to item parent transform */
1366 if (SP_OBJECT_PARENT(item)) {
1367 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1368 } else {
1369 dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1370 * NR::scale(1, -1) );
1371 }
1373 NR::Matrix const i2p( i2dt * dt2p );
1374 sp_item_set_item_transform(item, i2p);
1375 }
1378 NR::Matrix
1379 sp_item_dt2i_affine(SPItem const *item)
1380 {
1381 /* fixme: Implement the right way (Lauris) */
1382 return sp_item_i2d_affine(item).inverse();
1383 }
1385 /* Item views */
1387 static SPItemView *
1388 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1389 {
1390 SPItemView *new_view;
1392 g_assert(item != NULL);
1393 g_assert(SP_IS_ITEM(item));
1394 g_assert(arenaitem != NULL);
1395 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1397 new_view = g_new(SPItemView, 1);
1399 new_view->next = list;
1400 new_view->flags = flags;
1401 new_view->key = key;
1402 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1404 return new_view;
1405 }
1407 static SPItemView *
1408 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1409 {
1410 if (view == list) {
1411 list = list->next;
1412 } else {
1413 SPItemView *prev;
1414 prev = list;
1415 while (prev->next != view) prev = prev->next;
1416 prev->next = view->next;
1417 }
1419 nr_arena_item_unref(view->arenaitem);
1420 g_free(view);
1422 return list;
1423 }
1425 /**
1426 * Return the arenaitem corresponding to the given item in the display
1427 * with the given key
1428 */
1429 NRArenaItem *
1430 sp_item_get_arenaitem(SPItem *item, unsigned key)
1431 {
1432 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1433 if ( iv->key == key ) {
1434 return iv->arenaitem;
1435 }
1436 }
1438 return NULL;
1439 }
1441 int
1442 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1443 {
1444 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1445 SP_OBJECT_REPR(second));
1446 }
1448 SPItem *
1449 sp_item_first_item_child (SPObject *obj)
1450 {
1451 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1452 if (SP_IS_ITEM (iter))
1453 return SP_ITEM (iter);
1454 }
1455 return NULL;
1456 }
1459 /*
1460 Local Variables:
1461 mode:c++
1462 c-file-style:"stroustrup"
1463 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1464 indent-tabs-mode:nil
1465 fill-column:99
1466 End:
1467 */
1468 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :