1 #define __SP_ITEM_C__
3 /** \file
4 * Base class for visual SVG elements
5 */
6 /*
7 * Authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 2001-2005 authors
12 * Copyright (C) 2001 Ximian, Inc.
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 /** \class SPItem
18 *
19 * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
20 * is a subclass of SPObject, with great deal of specific functionality.
21 */
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
28 #include "sp-item.h"
29 #include "svg/svg.h"
30 #include "print.h"
31 #include "display/nr-arena.h"
32 #include "display/nr-arena-item.h"
33 #include "attributes.h"
34 #include "document.h"
35 #include "uri.h"
37 #include "style.h"
38 #include <glibmm/i18n.h>
39 #include "sp-root.h"
40 #include "sp-clippath.h"
41 #include "sp-mask.h"
42 #include "sp-rect.h"
43 #include "sp-use.h"
44 #include "sp-text.h"
45 #include "sp-item-rm-unsatisfied-cns.h"
46 #include "sp-pattern.h"
47 #include "sp-switch.h"
48 #include "gradient-chemistry.h"
49 #include "prefs-utils.h"
50 #include "conn-avoid-ref.h"
51 #include "conditions.h"
53 #include "libnr/nr-matrix-div.h"
54 #include "libnr/nr-matrix-fns.h"
55 #include "libnr/nr-matrix-scale-ops.h"
56 #include "libnr/nr-matrix-translate-ops.h"
57 #include "libnr/nr-scale-translate-ops.h"
58 #include "libnr/nr-translate-scale-ops.h"
59 #include "algorithms/find-last-if.h"
60 #include "util/reverse-list.h"
62 #include "xml/repr.h"
64 #define noSP_ITEM_DEBUG_IDLE
66 static void sp_item_class_init(SPItemClass *klass);
67 static void sp_item_init(SPItem *item);
69 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
70 static void sp_item_release(SPObject *object);
71 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
72 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
73 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
75 static gchar *sp_item_private_description(SPItem *item);
76 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
78 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
79 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
81 static SPObjectClass *parent_class;
83 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
84 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
86 /**
87 * Registers SPItem class and returns its type number.
88 */
89 GType
90 sp_item_get_type(void)
91 {
92 static GType type = 0;
93 if (!type) {
94 GTypeInfo info = {
95 sizeof(SPItemClass),
96 NULL, NULL,
97 (GClassInitFunc) sp_item_class_init,
98 NULL, NULL,
99 sizeof(SPItem),
100 16,
101 (GInstanceInitFunc) sp_item_init,
102 NULL, /* value_table */
103 };
104 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
105 }
106 return type;
107 }
109 /**
110 * SPItem vtable initialization.
111 */
112 static void
113 sp_item_class_init(SPItemClass *klass)
114 {
115 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
117 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
119 sp_object_class->build = sp_item_build;
120 sp_object_class->release = sp_item_release;
121 sp_object_class->set = sp_item_set;
122 sp_object_class->update = sp_item_update;
123 sp_object_class->write = sp_item_write;
125 klass->description = sp_item_private_description;
126 klass->snappoints = sp_item_private_snappoints;
127 }
129 /**
130 * Callback for SPItem object initialization.
131 */
132 static void
133 sp_item_init(SPItem *item)
134 {
135 item->init();
136 }
138 void SPItem::init() {
139 this->sensitive = TRUE;
141 this->transform_center_x = 0;
142 this->transform_center_y = 0;
144 this->_is_evaluated = true;
145 this->_evaluated_status = StatusUnknown;
147 this->transform = NR::identity();
149 this->display = NULL;
151 this->clip_ref = new SPClipPathReference(this);
152 {
153 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
154 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
155 cs1.connect(sl1);
156 }
158 this->mask_ref = new SPMaskReference(this);
159 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
160 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
161 cs2.connect(sl2);
163 if (!this->style) this->style = sp_style_new_from_object(this);
165 this->avoidRef = new SPAvoidRef(this);
167 new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
168 }
170 bool SPItem::isVisibleAndUnlocked() const {
171 return (!isHidden() && !isLocked());
172 }
174 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
175 return (!isHidden(display_key) && !isLocked());
176 }
178 bool SPItem::isLocked() const {
179 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
180 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
181 return true;
182 }
183 return false;
184 }
186 void SPItem::setLocked(bool locked) {
187 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
188 ( locked ? "1" : NULL ));
189 updateRepr();
190 }
192 bool SPItem::isHidden() const {
193 if (!isEvaluated())
194 return true;
195 return style->display.computed == SP_CSS_DISPLAY_NONE;
196 }
198 void SPItem::setHidden(bool hide) {
199 style->display.set = TRUE;
200 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
201 style->display.computed = style->display.value;
202 style->display.inherit = FALSE;
203 updateRepr();
204 }
206 bool SPItem::isHidden(unsigned display_key) const {
207 if (!isEvaluated())
208 return true;
209 for ( SPItemView *view(display) ; view ; view = view->next ) {
210 if ( view->key == display_key ) {
211 g_assert(view->arenaitem != NULL);
212 for ( NRArenaItem *arenaitem = view->arenaitem ;
213 arenaitem ; arenaitem = arenaitem->parent )
214 {
215 if (!arenaitem->visible) {
216 return true;
217 }
218 }
219 return false;
220 }
221 }
222 return true;
223 }
225 void SPItem::setEvaluated(bool evaluated) {
226 _is_evaluated = evaluated;
227 _evaluated_status = StatusSet;
228 }
230 void SPItem::resetEvaluated() {
231 if ( StatusCalculated == _evaluated_status ) {
232 _evaluated_status = StatusUnknown;
233 bool oldValue = _is_evaluated;
234 if ( oldValue != isEvaluated() ) {
235 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
236 }
237 } if ( StatusSet == _evaluated_status ) {
238 SPObject const *const parent = SP_OBJECT_PARENT(this);
239 if (SP_IS_SWITCH(parent)) {
240 SP_SWITCH(parent)->resetChildEvaluated();
241 }
242 }
243 }
245 bool SPItem::isEvaluated() const {
246 if ( StatusUnknown == _evaluated_status ) {
247 _is_evaluated = sp_item_evaluate(this);
248 _evaluated_status = StatusCalculated;
249 }
250 return _is_evaluated;
251 }
253 /**
254 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
255 * Corresponds to setExplicitlyHidden.
256 */
257 bool
258 SPItem::isExplicitlyHidden() const
259 {
260 return (this->style->display.set
261 && this->style->display.value == SP_CSS_DISPLAY_NONE);
262 }
264 /**
265 * Sets the display CSS property to `hidden' if \a val is true,
266 * otherwise makes it unset
267 */
268 void
269 SPItem::setExplicitlyHidden(bool const val) {
270 this->style->display.set = val;
271 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
272 this->style->display.computed = this->style->display.value;
273 this->updateRepr();
274 }
276 /**
277 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
278 */
279 void
280 SPItem::setCenter(NR::Point object_centre) {
281 NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
282 if (!bbox.isEmpty()) {
283 transform_center_x = object_centre[NR::X] - bbox.midpoint()[NR::X];
284 if (fabs(transform_center_x) < 1e-5) // rounding error
285 transform_center_x = 0;
286 transform_center_y = object_centre[NR::Y] - bbox.midpoint()[NR::Y];
287 if (fabs(transform_center_y) < 1e-5) // rounding error
288 transform_center_y = 0;
289 }
290 }
292 void
293 SPItem::unsetCenter() {
294 transform_center_x = 0;
295 transform_center_y = 0;
296 }
298 bool SPItem::isCenterSet() {
299 return (transform_center_x != 0 || transform_center_y != 0);
300 }
302 NR::Point SPItem::getCenter() {
303 NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
304 if (!bbox.isEmpty()) {
305 return bbox.midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
306 } else {
307 return NR::Point (0, 0); // something's wrong!
308 }
309 }
312 namespace {
314 bool is_item(SPObject const &object) {
315 return SP_IS_ITEM(&object);
316 }
318 }
320 void SPItem::raiseToTop() {
321 using Inkscape::Algorithms::find_last_if;
323 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
324 SP_OBJECT_NEXT(this), NULL, &is_item
325 );
326 if (topmost) {
327 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
328 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
329 }
330 }
332 void SPItem::raiseOne() {
333 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
334 SP_OBJECT_NEXT(this), NULL, &is_item
335 );
336 if (next_higher) {
337 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
338 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
339 sp_repr_parent(repr)->changeOrder(repr, ref);
340 }
341 }
343 void SPItem::lowerOne() {
344 using Inkscape::Util::MutableList;
345 using Inkscape::Util::reverse_list;
347 MutableList<SPObject &> next_lower=std::find_if(
348 reverse_list<SPObject::SiblingIterator>(
349 SP_OBJECT_PARENT(this)->firstChild(), this
350 ),
351 MutableList<SPObject &>(),
352 &is_item
353 );
354 if (next_lower) {
355 ++next_lower;
356 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
357 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
358 sp_repr_parent(repr)->changeOrder(repr, ref);
359 }
360 }
362 void SPItem::lowerToBottom() {
363 using Inkscape::Algorithms::find_last_if;
364 using Inkscape::Util::MutableList;
365 using Inkscape::Util::reverse_list;
367 MutableList<SPObject &> bottom=find_last_if(
368 reverse_list<SPObject::SiblingIterator>(
369 SP_OBJECT_PARENT(this)->firstChild(), this
370 ),
371 MutableList<SPObject &>(),
372 &is_item
373 );
374 if (bottom) {
375 ++bottom;
376 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
377 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
378 sp_repr_parent(repr)->changeOrder(repr, ref);
379 }
380 }
382 static void
383 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
384 {
385 sp_object_read_attr(object, "style");
386 sp_object_read_attr(object, "transform");
387 sp_object_read_attr(object, "clip-path");
388 sp_object_read_attr(object, "mask");
389 sp_object_read_attr(object, "sodipodi:insensitive");
390 sp_object_read_attr(object, "sodipodi:nonprintable");
391 sp_object_read_attr(object, "inkscape:transform-center-x");
392 sp_object_read_attr(object, "inkscape:transform-center-y");
393 sp_object_read_attr(object, "inkscape:connector-avoid");
395 if (((SPObjectClass *) (parent_class))->build) {
396 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
397 }
398 }
400 static void
401 sp_item_release(SPObject *object)
402 {
403 SPItem *item = (SPItem *) object;
405 if (item->clip_ref) {
406 item->clip_ref->detach();
407 delete item->clip_ref;
408 item->clip_ref = NULL;
409 }
411 if (item->mask_ref) {
412 item->mask_ref->detach();
413 delete item->mask_ref;
414 item->mask_ref = NULL;
415 }
417 if (item->avoidRef) {
418 delete item->avoidRef;
419 item->avoidRef = NULL;
420 }
422 if (((SPObjectClass *) (parent_class))->release) {
423 ((SPObjectClass *) parent_class)->release(object);
424 }
426 while (item->display) {
427 nr_arena_item_unparent(item->display->arenaitem);
428 item->display = sp_item_view_list_remove(item->display, item->display);
429 }
431 item->_transformed_signal.~signal();
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_STYLE:
488 sp_style_read_from_object(object->style, object);
489 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
490 break;
491 case SP_ATTR_CONNECTOR_AVOID:
492 item->avoidRef->setAvoid(value);
493 break;
494 case SP_ATTR_TRANSFORM_CENTER_X:
495 if (value) {
496 item->transform_center_x = g_strtod(value, NULL);
497 } else {
498 item->transform_center_x = 0;
499 }
500 break;
501 case SP_ATTR_TRANSFORM_CENTER_Y:
502 if (value) {
503 item->transform_center_y = g_strtod(value, NULL);
504 } else {
505 item->transform_center_y = 0;
506 }
507 break;
508 case SP_PROP_SYSTEM_LANGUAGE:
509 case SP_PROP_REQUIRED_FEATURES:
510 case SP_PROP_REQUIRED_EXTENSIONS:
511 {
512 item->resetEvaluated();
513 // pass to default handler
514 }
515 default:
516 if (SP_ATTRIBUTE_IS_CSS(key)) {
517 sp_style_read_from_object(object->style, object);
518 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
519 } else {
520 if (((SPObjectClass *) (parent_class))->set) {
521 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
522 }
523 }
524 break;
525 }
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 libavoid with item geometry (for connector routing).
627 item->avoidRef->handleSettingChange();
628 }
630 static Inkscape::XML::Node *
631 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
632 {
633 SPItem *item = SP_ITEM(object);
635 gchar c[256];
636 if (sp_svg_transform_write(c, 256, item->transform)) {
637 repr->setAttribute("transform", c);
638 } else {
639 repr->setAttribute("transform", NULL);
640 }
642 SPObject const *const parent = SP_OBJECT_PARENT(object);
643 /** \todo Can someone please document why this is conditional on having
644 * a parent? The only parentless thing I can think of is the top-level
645 * <svg> element (SPRoot). SPRoot is derived from SPGroup, and can have
646 * style. I haven't looked at callers.
647 */
648 if (parent) {
649 SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
650 if (obj_style) {
651 gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
652 repr->setAttribute("style", ( *s ? s : NULL ));
653 g_free(s);
654 } else {
655 /** \todo I'm not sure what to do in this case. Bug #1165868
656 * suggests that it can arise, but the submitter doesn't know
657 * how to do so reliably. The main two options are either
658 * leave repr's style attribute unchanged, or explicitly clear it.
659 * Must also consider what to do with property attributes for
660 * the element; see below.
661 */
662 char const *style_str = repr->attribute("style");
663 if (!style_str) {
664 style_str = "NULL";
665 }
666 g_warning("Item's style is NULL; repr style attribute is %s", style_str);
667 }
669 /** \note We treat object->style as authoritative. Its effects have
670 * been written to the style attribute above; any properties that are
671 * unset we take to be deliberately unset (e.g. so that clones can
672 * override the property).
673 *
674 * Note that the below has an undesirable consequence of changing the
675 * appearance on renderers that lack CSS support (e.g. SVG tiny);
676 * possibly we should write property attributes instead of a style
677 * attribute.
678 */
679 sp_style_unset_property_attrs (object);
680 }
682 if (flags & SP_OBJECT_WRITE_EXT) {
683 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
684 if (item->transform_center_x != 0)
685 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
686 else
687 repr->setAttribute ("inkscape:transform-center-x", NULL);
688 if (item->transform_center_y != 0)
689 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
690 else
691 repr->setAttribute ("inkscape:transform-center-y", NULL);
692 }
694 if (((SPObjectClass *) (parent_class))->write) {
695 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
696 }
698 return repr;
699 }
701 NR::Rect SPItem::invokeBbox(NR::Matrix const &transform) const
702 {
703 NRRect r;
704 sp_item_invoke_bbox_full(this, &r, transform, 0, TRUE);
705 return NR::Rect(r);
706 }
708 void
709 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear)
710 {
711 sp_item_invoke_bbox_full(item, bbox, transform, 0, clear);
712 }
714 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
715 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
716 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
717 * clones), in turn, call this function in their bbox methods. */
718 void
719 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
720 {
721 g_assert(item != NULL);
722 g_assert(SP_IS_ITEM(item));
723 g_assert(bbox != NULL);
725 if (clear) {
726 bbox->x0 = bbox->y0 = 1e18;
727 bbox->x1 = bbox->y1 = -1e18;
728 }
730 NRRect this_bbox;
731 this_bbox.x0 = this_bbox.y0 = 1e18;
732 this_bbox.x1 = this_bbox.y1 = -1e18;
734 // call the subclass method
735 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
736 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
737 }
739 // crop the bbox by clip path, if any
740 if (item->clip_ref->getObject()) {
741 NRRect b;
742 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
743 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
744 }
746 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
747 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
748 nr_rect_d_union (bbox, bbox, &this_bbox);
749 }
750 }
752 unsigned sp_item_pos_in_parent(SPItem *item)
753 {
754 g_assert(item != NULL);
755 g_assert(SP_IS_ITEM(item));
757 SPObject *parent = SP_OBJECT_PARENT(item);
758 g_assert(parent != NULL);
759 g_assert(SP_IS_OBJECT(parent));
761 SPObject *object = SP_OBJECT(item);
763 unsigned pos=0;
764 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
765 if ( iter == object ) {
766 return pos;
767 }
768 if (SP_IS_ITEM(iter)) {
769 pos++;
770 }
771 }
773 g_assert_not_reached();
774 return 0;
775 }
777 void
778 sp_item_bbox_desktop(SPItem *item, NRRect *bbox)
779 {
780 g_assert(item != NULL);
781 g_assert(SP_IS_ITEM(item));
782 g_assert(bbox != NULL);
784 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE);
785 }
787 NR::Rect sp_item_bbox_desktop(SPItem *item)
788 {
789 NRRect ret;
790 sp_item_invoke_bbox(item, &ret, sp_item_i2d_affine(item), TRUE);
791 return NR::Rect(ret);
792 }
794 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
795 {
796 NR::Rect const bbox = item->invokeBbox(sp_item_i2d_affine(item));
797 /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
798 support angled guide lines. */
800 *p = bbox.min();
801 *p = bbox.max();
802 }
804 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
805 {
806 g_assert (item != NULL);
807 g_assert (SP_IS_ITEM(item));
809 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
810 if (item_class.snappoints) {
811 item_class.snappoints(item, p);
812 }
813 }
815 void
816 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
817 {
818 if (!item->isHidden()) {
819 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
820 if (!item->transform.test_identity()
821 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
822 {
823 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
824 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
825 sp_print_release(ctx);
826 } else {
827 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
828 }
829 }
830 }
831 }
833 static gchar *
834 sp_item_private_description(SPItem *item)
835 {
836 return g_strdup(_("Object"));
837 }
839 /**
840 * Returns a string suitable for status bar, formatted in pango markup language.
841 *
842 * Must be freed by caller.
843 */
844 gchar *
845 sp_item_description(SPItem *item)
846 {
847 g_assert(item != NULL);
848 g_assert(SP_IS_ITEM(item));
850 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
851 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
852 if (s && item->clip_ref->getObject()) {
853 gchar *snew = g_strdup_printf ("%s; <i>clipped</i>", s);
854 g_free (s);
855 s = snew;
856 }
857 if (s && item->mask_ref->getObject()) {
858 gchar *snew = g_strdup_printf ("%s; <i>masked</i>", s);
859 g_free (s);
860 s = snew;
861 }
862 return s;
863 }
865 g_assert_not_reached();
866 return NULL;
867 }
869 /**
870 * Allocates unique integer keys.
871 * \param numkeys Number of keys required.
872 * \return First allocated key; hence if the returned key is n
873 * you can use n, n + 1, ..., n + (numkeys - 1)
874 */
875 unsigned
876 sp_item_display_key_new(unsigned numkeys)
877 {
878 static unsigned dkey = 0;
880 dkey += numkeys;
882 return dkey - numkeys;
883 }
885 NRArenaItem *
886 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
887 {
888 g_assert(item != NULL);
889 g_assert(SP_IS_ITEM(item));
890 g_assert(arena != NULL);
891 g_assert(NR_IS_ARENA(arena));
893 NRArenaItem *ai = NULL;
894 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
895 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
896 }
898 if (ai != NULL) {
899 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
900 nr_arena_item_set_transform(ai, item->transform);
901 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
902 nr_arena_item_set_visible(ai, !item->isHidden());
903 nr_arena_item_set_sensitive(ai, item->sensitive);
904 if (item->clip_ref->getObject()) {
905 SPClipPath *cp = item->clip_ref->getObject();
907 if (!item->display->arenaitem->key) {
908 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
909 }
910 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
912 // Show and set clip
913 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
914 nr_arena_item_set_clip(ai, ac);
915 nr_arena_item_unref(ac);
917 // Update bbox, in case the clip uses bbox units
918 NRRect bbox;
919 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
920 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
921 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
922 }
923 if (item->mask_ref->getObject()) {
924 SPMask *mask = item->mask_ref->getObject();
926 if (!item->display->arenaitem->key) {
927 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
928 }
929 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
931 // Show and set mask
932 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
933 nr_arena_item_set_mask(ai, ac);
934 nr_arena_item_unref(ac);
936 // Update bbox, in case the mask uses bbox units
937 NRRect bbox;
938 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
939 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
940 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
941 }
942 NR_ARENA_ITEM_SET_DATA(ai, item);
943 }
945 return ai;
946 }
948 void
949 sp_item_invoke_hide(SPItem *item, unsigned key)
950 {
951 g_assert(item != NULL);
952 g_assert(SP_IS_ITEM(item));
954 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
955 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
956 }
958 SPItemView *ref = NULL;
959 SPItemView *v = item->display;
960 while (v != NULL) {
961 SPItemView *next = v->next;
962 if (v->key == key) {
963 if (item->clip_ref->getObject()) {
964 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
965 nr_arena_item_set_clip(v->arenaitem, NULL);
966 }
967 if (item->mask_ref->getObject()) {
968 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
969 nr_arena_item_set_mask(v->arenaitem, NULL);
970 }
971 if (!ref) {
972 item->display = v->next;
973 } else {
974 ref->next = v->next;
975 }
976 nr_arena_item_unparent(v->arenaitem);
977 nr_arena_item_unref(v->arenaitem);
978 g_free(v);
979 } else {
980 ref = v;
981 }
982 v = next;
983 }
984 }
986 // Adjusters
988 void
989 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
990 {
991 SPStyle *style = SP_OBJECT_STYLE (item);
993 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
994 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
995 if (SP_IS_PATTERN (server)) {
996 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
997 sp_pattern_transform_multiply (pattern, postmul, set);
998 }
999 }
1001 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1002 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1003 if (SP_IS_PATTERN (server)) {
1004 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1005 sp_pattern_transform_multiply (pattern, postmul, set);
1006 }
1007 }
1009 }
1011 void
1012 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1013 {
1014 SPStyle *style = SP_OBJECT_STYLE (item);
1016 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
1017 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1018 if (SP_IS_GRADIENT (server)) {
1020 /**
1021 * \note Bbox units for a gradient are generally a bad idea because
1022 * with them, you cannot preserve the relative position of the
1023 * object and its gradient after rotation or skew. So now we
1024 * convert them to userspace units which are easy to keep in sync
1025 * just by adding the object's transform to gradientTransform.
1026 * \todo FIXME: convert back to bbox units after transforming with
1027 * the item, so as to preserve the original units.
1028 */
1029 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1031 sp_gradient_transform_multiply (gradient, postmul, set);
1032 }
1033 }
1035 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
1036 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1037 if (SP_IS_GRADIENT (server)) {
1038 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1039 sp_gradient_transform_multiply (gradient, postmul, set);
1040 }
1041 }
1042 }
1044 void
1045 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1046 {
1047 SPStyle *style = SP_OBJECT_STYLE (item);
1049 if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1051 style->stroke_width.computed *= ex;
1052 style->stroke_width.set = TRUE;
1054 if (style->stroke_dash.n_dash != 0) {
1055 int i;
1056 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1057 style->stroke_dash.dash[i] *= ex;
1058 }
1059 style->stroke_dash.offset *= ex;
1060 }
1062 SP_OBJECT(item)->updateRepr();
1063 }
1064 }
1066 /**
1067 * Find out the inverse of previous transform of an item (from its repr)
1068 */
1069 NR::Matrix
1070 sp_item_transform_repr (SPItem *item)
1071 {
1072 NR::Matrix t_old(NR::identity());
1073 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1074 if (t_attr) {
1075 NR::Matrix t;
1076 if (sp_svg_transform_read(t_attr, &t)) {
1077 t_old = t;
1078 }
1079 }
1081 return t_old;
1082 }
1085 /**
1086 * Recursively scale stroke width in \a item and its children by \a expansion.
1087 */
1088 void
1089 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1090 {
1091 sp_item_adjust_stroke (item, expansion);
1093 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1094 if (SP_IS_ITEM(o))
1095 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1096 }
1097 }
1099 /**
1100 * Recursively adjust rx and ry of rects.
1101 */
1102 void
1103 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1104 {
1105 if (SP_IS_RECT (item)) {
1106 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1107 }
1109 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1110 if (SP_IS_ITEM(o))
1111 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1112 }
1113 }
1115 /**
1116 * Recursively compensate pattern or gradient transform.
1117 */
1118 void
1119 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1120 {
1121 // A clone must not touch the style (it's that of its parent!) and has no children, so quit now
1122 if (item && SP_IS_USE(item))
1123 return;
1125 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1126 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1127 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1128 NR::Matrix t_item = sp_item_transform_repr (item);
1129 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1131 if (is_pattern)
1132 sp_item_adjust_pattern (item, paint_delta);
1133 else
1134 sp_item_adjust_gradient (item, paint_delta);
1136 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation
1137 if (item && SP_IS_TEXT(item))
1138 return;
1140 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1141 if (SP_IS_ITEM(o)) {
1142 // At the level of the transformed item, t_ancestors is identity;
1143 // below it, it is the accmmulated chain of transforms from this level to the top level
1144 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1145 }
1146 }
1147 }
1149 /**
1150 * A temporary wrapper for the next function accepting the NRMatrix
1151 * instead of NR::Matrix
1152 */
1153 void
1154 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1155 {
1156 if (transform == NULL)
1157 sp_item_write_transform(item, repr, NR::identity(), adv);
1158 else
1159 sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1160 }
1162 /**
1163 * Set a new transform on an object.
1164 *
1165 * Compensate for stroke scaling and gradient/pattern fill transform, if
1166 * necessary. Call the object's set_transform method if transforms are
1167 * stored optimized. Send _transformed_signal. Invoke _write method so that
1168 * the repr is updated with the new transform.
1169 */
1170 void
1171 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv)
1172 {
1173 g_return_if_fail(item != NULL);
1174 g_return_if_fail(SP_IS_ITEM(item));
1175 g_return_if_fail(repr != NULL);
1177 // calculate the relative transform, if not given by the adv attribute
1178 NR::Matrix advertized_transform;
1179 if (adv != NULL) {
1180 advertized_transform = *adv;
1181 } else {
1182 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1183 }
1185 // recursively compensate for stroke scaling, depending on user preference
1186 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1187 double const expansion = 1. / NR::expansion(advertized_transform);
1188 sp_item_adjust_stroke_width_recursive(item, expansion);
1189 }
1191 // recursively compensate rx/ry of a rect if requested
1192 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1193 sp_item_adjust_rects_recursive(item, advertized_transform);
1194 }
1196 // recursively compensate pattern fill if it's not to be transformed
1197 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1198 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1199 }
1200 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1201 /// recursively compensate gradient fill if it's not to be transformed
1202 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1203 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1204 } else {
1205 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1206 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1207 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1208 }
1210 // run the object's set_transform if transforms are stored optimized and there's no clippath or mask
1211 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1212 NR::Matrix transform_attr (transform);
1213 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform
1214 && !preserve && !item->clip_ref->getObject() && !item->mask_ref->getObject()) {
1215 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1216 }
1217 sp_item_set_item_transform(item, transform_attr);
1219 // Note: updateRepr comes before emitting the transformed signal since
1220 // it causes clone SPUse's copy of the original object to brought up to
1221 // date with the original. Otherwise, sp_use_bbox returns incorrect
1222 // values if called in code handling the transformed signal.
1223 SP_OBJECT(item)->updateRepr();
1225 // send the relative transform with a _transformed_signal
1226 item->_transformed_signal.emit(&advertized_transform, item);
1227 }
1229 gint
1230 sp_item_event(SPItem *item, SPEvent *event)
1231 {
1232 g_return_val_if_fail(item != NULL, FALSE);
1233 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1234 g_return_val_if_fail(event != NULL, FALSE);
1236 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1237 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1239 return FALSE;
1240 }
1242 /**
1243 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1244 * gradients, patterns as sp_item_write_transform does.
1245 */
1246 void
1247 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1248 {
1249 g_return_if_fail(item != NULL);
1250 g_return_if_fail(SP_IS_ITEM(item));
1252 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1253 item->transform = transform;
1254 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1255 transformation. It's apparently not used anywhere else. */
1256 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1257 sp_item_rm_unsatisfied_cns(*item);
1258 }
1259 }
1262 /**
1263 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1264 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1265 */
1266 NR::Matrix
1267 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1268 NR::Matrix ret(NR::identity());
1269 g_return_val_if_fail(object != NULL, ret);
1271 /* stop at first non-renderable ancestor */
1272 while ( object != ancestor && SP_IS_ITEM(object) ) {
1273 if (SP_IS_ROOT(object)) {
1274 ret *= SP_ROOT(object)->c2p;
1275 }
1276 ret *= SP_ITEM(object)->transform;
1277 object = SP_OBJECT_PARENT(object);
1278 }
1279 return ret;
1280 }
1282 NR::Matrix
1283 i2i_affine(SPObject const *src, SPObject const *dest) {
1284 g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1285 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1286 return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1287 }
1289 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1290 return i2i_affine(this, dest);
1291 }
1293 /**
1294 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1295 * \pre (item != NULL) and SP_IS_ITEM(item).
1296 */
1297 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1298 {
1299 return i2anc_affine(item, NULL);
1300 }
1302 /**
1303 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1304 * Used in path operations mostly.
1305 * \pre (item != NULL) and SP_IS_ITEM(item).
1306 */
1307 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1308 {
1309 g_assert(item != NULL);
1310 g_assert(SP_IS_ITEM(item));
1312 NR::Matrix ret(NR::identity());
1313 g_assert(ret.test_identity());
1314 while ( NULL != SP_OBJECT_PARENT(item) ) {
1315 ret *= item->transform;
1316 item = SP_ITEM(SP_OBJECT_PARENT(item));
1317 }
1318 g_assert(SP_IS_ROOT(item));
1320 ret *= item->transform;
1322 return ret;
1323 }
1325 /* fixme: This is EVIL!!! */
1327 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1328 {
1329 g_assert(item != NULL);
1330 g_assert(SP_IS_ITEM(item));
1332 NR::Matrix const ret( sp_item_i2doc_affine(item)
1333 * NR::scale(1, -1)
1334 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1335 return ret;
1336 }
1338 // same as i2d but with i2root instead of i2doc
1339 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1340 {
1341 g_assert(item != NULL);
1342 g_assert(SP_IS_ITEM(item));
1344 NR::Matrix const ret( sp_item_i2root_affine(item)
1345 * NR::scale(1, -1)
1346 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1347 return ret;
1348 }
1350 /**
1351 * Converts a matrix \a m into the desktop coords of the \a item.
1352 * Will become a noop when we eliminate the coordinate flipping.
1353 */
1354 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1355 {
1356 NR::Matrix const ret(m
1357 * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1358 * NR::scale(1, -1));
1359 return ret;
1360 }
1362 /**
1363 * Converts a matrix \a m from the desktop coords of the \a item.
1364 * Will become a noop when we eliminate the coordinate flipping.
1365 */
1366 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1367 {
1368 NR::Matrix const ret(NR::scale(1, -1)
1369 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1370 * m);
1371 return ret;
1372 }
1374 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1375 {
1376 g_return_if_fail( item != NULL );
1377 g_return_if_fail( SP_IS_ITEM(item) );
1379 NR::Matrix dt2p; /* desktop to item parent transform */
1380 if (SP_OBJECT_PARENT(item)) {
1381 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1382 } else {
1383 dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1384 * NR::scale(1, -1) );
1385 }
1387 NR::Matrix const i2p( i2dt * dt2p );
1388 sp_item_set_item_transform(item, i2p);
1389 }
1392 NR::Matrix
1393 sp_item_dt2i_affine(SPItem const *item)
1394 {
1395 /* fixme: Implement the right way (Lauris) */
1396 return sp_item_i2d_affine(item).inverse();
1397 }
1399 /* Item views */
1401 static SPItemView *
1402 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1403 {
1404 SPItemView *new_view;
1406 g_assert(item != NULL);
1407 g_assert(SP_IS_ITEM(item));
1408 g_assert(arenaitem != NULL);
1409 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1411 new_view = g_new(SPItemView, 1);
1413 new_view->next = list;
1414 new_view->flags = flags;
1415 new_view->key = key;
1416 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1418 return new_view;
1419 }
1421 static SPItemView *
1422 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1423 {
1424 if (view == list) {
1425 list = list->next;
1426 } else {
1427 SPItemView *prev;
1428 prev = list;
1429 while (prev->next != view) prev = prev->next;
1430 prev->next = view->next;
1431 }
1433 nr_arena_item_unref(view->arenaitem);
1434 g_free(view);
1436 return list;
1437 }
1439 /**
1440 * Return the arenaitem corresponding to the given item in the display
1441 * with the given key
1442 */
1443 NRArenaItem *
1444 sp_item_get_arenaitem(SPItem *item, unsigned key)
1445 {
1446 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1447 if ( iv->key == key ) {
1448 return iv->arenaitem;
1449 }
1450 }
1452 return NULL;
1453 }
1455 int
1456 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1457 {
1458 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1459 SP_OBJECT_REPR(second));
1460 }
1462 SPItem *
1463 sp_item_first_item_child (SPObject *obj)
1464 {
1465 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1466 if (SP_IS_ITEM (iter))
1467 return SP_ITEM (iter);
1468 }
1469 return NULL;
1470 }
1473 /*
1474 Local Variables:
1475 mode:c++
1476 c-file-style:"stroustrup"
1477 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1478 indent-tabs-mode:nil
1479 fill-column:99
1480 End:
1481 */
1482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :