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