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"
37 #include "inkscape.h"
38 #include "desktop-handles.h"
40 #include "style.h"
41 #include <glibmm/i18n.h>
42 #include "sp-root.h"
43 #include "sp-clippath.h"
44 #include "sp-mask.h"
45 #include "sp-rect.h"
46 #include "sp-use.h"
47 #include "sp-text.h"
48 #include "sp-item-rm-unsatisfied-cns.h"
49 #include "sp-pattern.h"
50 #include "sp-switch.h"
51 #include "gradient-chemistry.h"
52 #include "prefs-utils.h"
53 #include "conn-avoid-ref.h"
54 #include "conditions.h"
55 #include "sp-filter-reference.h"
56 #include "filter-chemistry.h"
57 #include "sp-guide.h"
59 #include "libnr/nr-matrix-fns.h"
60 #include "libnr/nr-matrix-scale-ops.h"
61 #include "libnr/nr-matrix-translate-ops.h"
62 #include "libnr/nr-scale-translate-ops.h"
63 #include "libnr/nr-translate-scale-ops.h"
64 #include "libnr/nr-convert2geom.h"
65 #include "algorithms/find-last-if.h"
66 #include "util/reverse-list.h"
67 #include <2geom/rect.h>
68 #include <2geom/transforms.h>
70 #include "xml/repr.h"
71 #include "extract-uri.h"
73 #include "live_effects/lpeobject.h"
74 #include "live_effects/effect.h"
75 #include "live_effects/lpeobject-reference.h"
77 #define noSP_ITEM_DEBUG_IDLE
79 static void sp_item_class_init(SPItemClass *klass);
80 static void sp_item_init(SPItem *item);
82 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
83 static void sp_item_release(SPObject *object);
84 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
85 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
86 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
88 static gchar *sp_item_private_description(SPItem *item);
89 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
91 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
92 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
94 static SPObjectClass *parent_class;
96 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
97 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
99 /**
100 * Registers SPItem class and returns its type number.
101 */
102 GType
103 sp_item_get_type(void)
104 {
105 static GType type = 0;
106 if (!type) {
107 GTypeInfo info = {
108 sizeof(SPItemClass),
109 NULL, NULL,
110 (GClassInitFunc) sp_item_class_init,
111 NULL, NULL,
112 sizeof(SPItem),
113 16,
114 (GInstanceInitFunc) sp_item_init,
115 NULL, /* value_table */
116 };
117 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
118 }
119 return type;
120 }
122 /**
123 * SPItem vtable initialization.
124 */
125 static void
126 sp_item_class_init(SPItemClass *klass)
127 {
128 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
130 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
132 sp_object_class->build = sp_item_build;
133 sp_object_class->release = sp_item_release;
134 sp_object_class->set = sp_item_set;
135 sp_object_class->update = sp_item_update;
136 sp_object_class->write = sp_item_write;
138 klass->description = sp_item_private_description;
139 klass->snappoints = sp_item_private_snappoints;
140 }
142 /**
143 * Callback for SPItem object initialization.
144 */
145 static void
146 sp_item_init(SPItem *item)
147 {
148 item->init();
149 }
151 void SPItem::init() {
152 this->sensitive = TRUE;
154 this->transform_center_x = 0;
155 this->transform_center_y = 0;
157 this->_is_evaluated = true;
158 this->_evaluated_status = StatusUnknown;
160 this->transform = NR::identity();
162 this->display = NULL;
164 this->clip_ref = new SPClipPathReference(this);
165 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
166 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
167 _clip_ref_connection = cs1.connect(sl1);
169 this->mask_ref = new SPMaskReference(this);
170 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
171 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
172 _mask_ref_connection = cs2.connect(sl2);
174 this->avoidRef = new SPAvoidRef(this);
176 new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
177 }
179 bool SPItem::isVisibleAndUnlocked() const {
180 return (!isHidden() && !isLocked());
181 }
183 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
184 return (!isHidden(display_key) && !isLocked());
185 }
187 bool SPItem::isLocked() const {
188 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
189 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
190 return true;
191 }
192 return false;
193 }
195 void SPItem::setLocked(bool locked) {
196 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
197 ( locked ? "1" : NULL ));
198 updateRepr();
199 }
201 bool SPItem::isHidden() const {
202 if (!isEvaluated())
203 return true;
204 return style->display.computed == SP_CSS_DISPLAY_NONE;
205 }
207 void SPItem::setHidden(bool hide) {
208 style->display.set = TRUE;
209 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
210 style->display.computed = style->display.value;
211 style->display.inherit = FALSE;
212 updateRepr();
213 }
215 bool SPItem::isHidden(unsigned display_key) const {
216 if (!isEvaluated())
217 return true;
218 for ( SPItemView *view(display) ; view ; view = view->next ) {
219 if ( view->key == display_key ) {
220 g_assert(view->arenaitem != NULL);
221 for ( NRArenaItem *arenaitem = view->arenaitem ;
222 arenaitem ; arenaitem = arenaitem->parent )
223 {
224 if (!arenaitem->visible) {
225 return true;
226 }
227 }
228 return false;
229 }
230 }
231 return true;
232 }
234 void SPItem::setEvaluated(bool evaluated) {
235 _is_evaluated = evaluated;
236 _evaluated_status = StatusSet;
237 }
239 void SPItem::resetEvaluated() {
240 if ( StatusCalculated == _evaluated_status ) {
241 _evaluated_status = StatusUnknown;
242 bool oldValue = _is_evaluated;
243 if ( oldValue != isEvaluated() ) {
244 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
245 }
246 } if ( StatusSet == _evaluated_status ) {
247 SPObject const *const parent = SP_OBJECT_PARENT(this);
248 if (SP_IS_SWITCH(parent)) {
249 SP_SWITCH(parent)->resetChildEvaluated();
250 }
251 }
252 }
254 bool SPItem::isEvaluated() const {
255 if ( StatusUnknown == _evaluated_status ) {
256 _is_evaluated = sp_item_evaluate(this);
257 _evaluated_status = StatusCalculated;
258 }
259 return _is_evaluated;
260 }
262 /**
263 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
264 * Corresponds to setExplicitlyHidden.
265 */
266 bool
267 SPItem::isExplicitlyHidden() const
268 {
269 return (this->style->display.set
270 && this->style->display.value == SP_CSS_DISPLAY_NONE);
271 }
273 /**
274 * Sets the display CSS property to `hidden' if \a val is true,
275 * otherwise makes it unset
276 */
277 void
278 SPItem::setExplicitlyHidden(bool const val) {
279 this->style->display.set = val;
280 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
281 this->style->display.computed = this->style->display.value;
282 this->updateRepr();
283 }
285 /**
286 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
287 */
288 void
289 SPItem::setCenter(NR::Point object_centre) {
290 NR::Maybe<NR::Rect> bbox = getBounds(from_2geom(sp_item_i2d_affine(this)));
291 if (bbox) {
292 transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
293 if (fabs(transform_center_x) < 1e-5) // rounding error
294 transform_center_x = 0;
295 transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
296 if (fabs(transform_center_y) < 1e-5) // rounding error
297 transform_center_y = 0;
298 }
299 }
301 void
302 SPItem::unsetCenter() {
303 transform_center_x = 0;
304 transform_center_y = 0;
305 }
307 bool SPItem::isCenterSet() {
308 return (transform_center_x != 0 || transform_center_y != 0);
309 }
311 NR::Point SPItem::getCenter() const {
312 NR::Maybe<NR::Rect> bbox = getBounds(from_2geom(sp_item_i2d_affine(this)));
313 if (bbox) {
314 return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
315 } else {
316 return NR::Point (0, 0); // something's wrong!
317 }
318 }
321 namespace {
323 bool is_item(SPObject const &object) {
324 return SP_IS_ITEM(&object);
325 }
327 }
329 void SPItem::raiseToTop() {
330 using Inkscape::Algorithms::find_last_if;
332 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
333 SP_OBJECT_NEXT(this), NULL, &is_item
334 );
335 if (topmost) {
336 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
337 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
338 }
339 }
341 void SPItem::raiseOne() {
342 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
343 SP_OBJECT_NEXT(this), NULL, &is_item
344 );
345 if (next_higher) {
346 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
347 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
348 sp_repr_parent(repr)->changeOrder(repr, ref);
349 }
350 }
352 void SPItem::lowerOne() {
353 using Inkscape::Util::MutableList;
354 using Inkscape::Util::reverse_list;
356 MutableList<SPObject &> next_lower=std::find_if(
357 reverse_list<SPObject::SiblingIterator>(
358 SP_OBJECT_PARENT(this)->firstChild(), this
359 ),
360 MutableList<SPObject &>(),
361 &is_item
362 );
363 if (next_lower) {
364 ++next_lower;
365 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
366 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
367 sp_repr_parent(repr)->changeOrder(repr, ref);
368 }
369 }
371 void SPItem::lowerToBottom() {
372 using Inkscape::Algorithms::find_last_if;
373 using Inkscape::Util::MutableList;
374 using Inkscape::Util::reverse_list;
376 MutableList<SPObject &> bottom=find_last_if(
377 reverse_list<SPObject::SiblingIterator>(
378 SP_OBJECT_PARENT(this)->firstChild(), this
379 ),
380 MutableList<SPObject &>(),
381 &is_item
382 );
383 if (bottom) {
384 ++bottom;
385 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
386 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
387 sp_repr_parent(repr)->changeOrder(repr, ref);
388 }
389 }
391 static void
392 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
393 {
394 sp_object_read_attr(object, "style");
395 sp_object_read_attr(object, "transform");
396 sp_object_read_attr(object, "clip-path");
397 sp_object_read_attr(object, "mask");
398 sp_object_read_attr(object, "sodipodi:insensitive");
399 sp_object_read_attr(object, "sodipodi:nonprintable");
400 sp_object_read_attr(object, "inkscape:transform-center-x");
401 sp_object_read_attr(object, "inkscape:transform-center-y");
402 sp_object_read_attr(object, "inkscape:connector-avoid");
404 if (((SPObjectClass *) (parent_class))->build) {
405 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
406 }
407 }
409 static void
410 sp_item_release(SPObject *object)
411 {
412 SPItem *item = (SPItem *) object;
414 item->_clip_ref_connection.disconnect();
415 item->_mask_ref_connection.disconnect();
417 // Note: do this here before the clip_ref is deleted, since calling
418 // sp_document_ensure_up_to_date for triggered routing may reference
419 // the deleted clip_ref.
420 if (item->avoidRef) {
421 delete item->avoidRef;
422 item->avoidRef = NULL;
423 }
425 if (item->clip_ref) {
426 item->clip_ref->detach();
427 delete item->clip_ref;
428 item->clip_ref = NULL;
429 }
431 if (item->mask_ref) {
432 item->mask_ref->detach();
433 delete item->mask_ref;
434 item->mask_ref = NULL;
435 }
437 if (((SPObjectClass *) (parent_class))->release) {
438 ((SPObjectClass *) parent_class)->release(object);
439 }
441 while (item->display) {
442 nr_arena_item_unparent(item->display->arenaitem);
443 item->display = sp_item_view_list_remove(item->display, item->display);
444 }
446 item->_transformed_signal.~signal();
447 }
449 static void
450 sp_item_set(SPObject *object, unsigned key, gchar const *value)
451 {
452 SPItem *item = (SPItem *) object;
454 switch (key) {
455 case SP_ATTR_TRANSFORM: {
456 NR::Matrix t;
457 if (value && sp_svg_transform_read(value, &t)) {
458 sp_item_set_item_transform(item, t);
459 } else {
460 sp_item_set_item_transform(item, NR::identity());
461 }
462 break;
463 }
464 case SP_PROP_CLIP_PATH: {
465 gchar *uri = extract_uri(value);
466 if (uri) {
467 try {
468 item->clip_ref->attach(Inkscape::URI(uri));
469 } catch (Inkscape::BadURIException &e) {
470 g_warning("%s", e.what());
471 item->clip_ref->detach();
472 }
473 g_free(uri);
474 } else {
475 item->clip_ref->detach();
476 }
478 break;
479 }
480 case SP_PROP_MASK: {
481 gchar *uri = extract_uri(value);
482 if (uri) {
483 try {
484 item->mask_ref->attach(Inkscape::URI(uri));
485 } catch (Inkscape::BadURIException &e) {
486 g_warning("%s", e.what());
487 item->mask_ref->detach();
488 }
489 g_free(uri);
490 } else {
491 item->mask_ref->detach();
492 }
494 break;
495 }
496 case SP_ATTR_SODIPODI_INSENSITIVE:
497 item->sensitive = !value;
498 for (SPItemView *v = item->display; v != NULL; v = v->next) {
499 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
500 }
501 break;
502 case SP_ATTR_CONNECTOR_AVOID:
503 item->avoidRef->setAvoid(value);
504 break;
505 case SP_ATTR_TRANSFORM_CENTER_X:
506 if (value) {
507 item->transform_center_x = g_strtod(value, NULL);
508 } else {
509 item->transform_center_x = 0;
510 }
511 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
512 break;
513 case SP_ATTR_TRANSFORM_CENTER_Y:
514 if (value) {
515 item->transform_center_y = g_strtod(value, NULL);
516 } else {
517 item->transform_center_y = 0;
518 }
519 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
520 break;
521 case SP_PROP_SYSTEM_LANGUAGE:
522 case SP_PROP_REQUIRED_FEATURES:
523 case SP_PROP_REQUIRED_EXTENSIONS:
524 {
525 item->resetEvaluated();
526 // pass to default handler
527 }
528 default:
529 if (SP_ATTRIBUTE_IS_CSS(key)) {
530 sp_style_read_from_object(object->style, object);
531 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
532 } else {
533 if (((SPObjectClass *) (parent_class))->set) {
534 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
535 }
536 }
537 break;
538 }
539 }
541 static void
542 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
543 {
544 if (old_clip) {
545 SPItemView *v;
546 /* Hide clippath */
547 for (v = item->display; v != NULL; v = v->next) {
548 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
549 nr_arena_item_set_clip(v->arenaitem, NULL);
550 }
551 }
552 if (SP_IS_CLIPPATH(clip)) {
553 NRRect bbox;
554 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
555 for (SPItemView *v = item->display; v != NULL; v = v->next) {
556 if (!v->arenaitem->key) {
557 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
558 }
559 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
560 NR_ARENA_ITEM_ARENA(v->arenaitem),
561 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
562 nr_arena_item_set_clip(v->arenaitem, ai);
563 nr_arena_item_unref(ai);
564 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
565 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
566 }
567 }
568 }
570 static void
571 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
572 {
573 if (old_mask) {
574 /* Hide mask */
575 for (SPItemView *v = item->display; v != NULL; v = v->next) {
576 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
577 nr_arena_item_set_mask(v->arenaitem, NULL);
578 }
579 }
580 if (SP_IS_MASK(mask)) {
581 NRRect bbox;
582 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
583 for (SPItemView *v = item->display; v != NULL; v = v->next) {
584 if (!v->arenaitem->key) {
585 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
586 }
587 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
588 NR_ARENA_ITEM_ARENA(v->arenaitem),
589 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
590 nr_arena_item_set_mask(v->arenaitem, ai);
591 nr_arena_item_unref(ai);
592 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
593 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
594 }
595 }
596 }
598 static void
599 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
600 {
601 SPItem *item = SP_ITEM(object);
603 if (((SPObjectClass *) (parent_class))->update)
604 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
606 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
607 if (flags & SP_OBJECT_MODIFIED_FLAG) {
608 for (SPItemView *v = item->display; v != NULL; v = v->next) {
609 nr_arena_item_set_transform(v->arenaitem, item->transform);
610 }
611 }
613 SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
614 SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
616 if ( clip_path || mask ) {
617 NRRect bbox;
618 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
619 if (clip_path) {
620 for (SPItemView *v = item->display; v != NULL; v = v->next) {
621 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
622 }
623 }
624 if (mask) {
625 for (SPItemView *v = item->display; v != NULL; v = v->next) {
626 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
627 }
628 }
629 }
631 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
632 for (SPItemView *v = item->display; v != NULL; v = v->next) {
633 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
634 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
635 }
636 }
637 }
639 /* Update bounding box data used by filters */
640 if (item->style->filter.set && item->display) {
641 NRRect item_bbox;
642 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
643 NR::Maybe<NR::Rect> i_bbox = item_bbox;
645 SPItemView *itemview = item->display;
646 do {
647 if (itemview->arenaitem)
648 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
649 } while ( (itemview = itemview->next) );
650 }
652 // Update libavoid with item geometry (for connector routing).
653 if (item->avoidRef)
654 item->avoidRef->handleSettingChange();
655 }
657 static Inkscape::XML::Node *
658 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
659 {
660 SPItem *item = SP_ITEM(object);
662 gchar *c = sp_svg_transform_write(item->transform);
663 repr->setAttribute("transform", c);
664 g_free(c);
666 if (flags & SP_OBJECT_WRITE_EXT) {
667 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
668 if (item->transform_center_x != 0)
669 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
670 else
671 repr->setAttribute ("inkscape:transform-center-x", NULL);
672 if (item->transform_center_y != 0)
673 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
674 else
675 repr->setAttribute ("inkscape:transform-center-y", NULL);
676 }
678 if (item->clip_ref->getObject()) {
679 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
680 repr->setAttribute ("clip-path", value);
681 g_free ((void *) value);
682 }
683 if (item->mask_ref->getObject()) {
684 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
685 repr->setAttribute ("mask", value);
686 g_free ((void *) value);
687 }
689 if (((SPObjectClass *) (parent_class))->write) {
690 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
691 }
693 return repr;
694 }
696 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
697 SPItem::BBoxType type,
698 unsigned int /*dkey*/) const
699 {
700 NR::Maybe<NR::Rect> r = NR::Nothing();
701 sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
702 return r;
703 }
705 NR::Maybe<Geom::Rect>
706 SPItem::getBounds(Geom::Matrix const &transform, SPItem::BBoxType type, unsigned int /*dkey*/)
707 const
708 {
709 NR::Maybe<NR::Rect> r = NR::Nothing();
710 sp_item_invoke_bbox_full(this, &r, from_2geom(transform), type, TRUE);
711 return NR::Maybe<Geom::Rect>(to_2geom(*r));
712 }
714 void
715 sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
716 {
717 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
718 }
720 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
721 void
722 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
723 {
724 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
725 }
727 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
728 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
729 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
730 * clones), in turn, call this function in their bbox methods. */
731 void
732 sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
733 {
734 g_assert(item != NULL);
735 g_assert(SP_IS_ITEM(item));
736 g_assert(bbox != NULL);
738 if (clear) {
739 *bbox = NR::Nothing();
740 }
742 // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
744 NRRect temp_bbox;
745 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
746 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
748 // call the subclass method
749 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
750 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
751 }
753 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
754 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
755 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
756 SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
757 if (filter && SP_IS_FILTER(filter)) {
758 // default filer area per the SVG spec:
759 double x = -0.1;
760 double y = -0.1;
761 double w = 1.2;
762 double h = 1.2;
764 // if area is explicitly set, override:
765 if (SP_FILTER(filter)->x._set)
766 x = SP_FILTER(filter)->x.computed;
767 if (SP_FILTER(filter)->y._set)
768 y = SP_FILTER(filter)->y.computed;
769 if (SP_FILTER(filter)->width._set)
770 w = SP_FILTER(filter)->width.computed;
771 if (SP_FILTER(filter)->height._set)
772 h = SP_FILTER(filter)->height.computed;
774 double dx0 = 0;
775 double dx1 = 0;
776 double dy0 = 0;
777 double dy1 = 0;
778 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
779 // if this is a single blur, use 2.4*radius
780 // which may be smaller than the default area;
781 // see set_filter_area for why it's 2.4
782 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
783 dx0 = -2.4 * r;
784 dx1 = 2.4 * r;
785 dy0 = -2.4 * r;
786 dy1 = 2.4 * r;
787 } else {
788 // otherwise, calculate expansion from relative to absolute units:
789 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
790 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
791 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
792 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
793 }
795 // transform the expansions by the item's transform:
796 NR::Matrix i2d = from_2geom(sp_item_i2d_affine (item));
797 dx0 *= NR::expansionX(i2d);
798 dx1 *= NR::expansionX(i2d);
799 dy0 *= NR::expansionY(i2d);
800 dy1 *= NR::expansionY(i2d);
802 // expand the bbox
803 temp_bbox.x0 += dx0;
804 temp_bbox.x1 += dx1;
805 temp_bbox.y0 += dy0;
806 temp_bbox.y1 += dy1;
807 }
808 }
809 if (item->clip_ref->getObject()) {
810 NRRect b;
811 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
812 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
813 }
814 }
816 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
817 // Either the bbox hasn't been touched by the SPItemClass' bbox method
818 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
819 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
821 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
822 // explicitely defined this way for NRRects (as opposed to NR::Maybe<NR::Rect>)
823 // So union bbox with nothing = do nothing, just return
824 return;
825 }
827 // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
828 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
829 // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
830 NR::Maybe<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
832 *bbox = NR::union_bounds(*bbox, temp_bbox_new);
833 }
835 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
836 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
837 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
838 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
839 * clones), in turn, call this function in their bbox methods. */
840 void
841 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
842 {
843 g_assert(item != NULL);
844 g_assert(SP_IS_ITEM(item));
845 g_assert(bbox != NULL);
847 if (clear) {
848 bbox->x0 = bbox->y0 = 1e18;
849 bbox->x1 = bbox->y1 = -1e18;
850 }
852 NRRect this_bbox;
853 this_bbox.x0 = this_bbox.y0 = 1e18;
854 this_bbox.x1 = this_bbox.y1 = -1e18;
856 // call the subclass method
857 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
858 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
859 }
861 // unless this is geometric bbox, crop the bbox by clip path, if any
862 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
863 NRRect b;
864 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
865 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
866 }
868 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
869 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
870 nr_rect_d_union (bbox, bbox, &this_bbox);
871 }
872 }
874 unsigned sp_item_pos_in_parent(SPItem *item)
875 {
876 g_assert(item != NULL);
877 g_assert(SP_IS_ITEM(item));
879 SPObject *parent = SP_OBJECT_PARENT(item);
880 g_assert(parent != NULL);
881 g_assert(SP_IS_OBJECT(parent));
883 SPObject *object = SP_OBJECT(item);
885 unsigned pos=0;
886 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
887 if ( iter == object ) {
888 return pos;
889 }
890 if (SP_IS_ITEM(iter)) {
891 pos++;
892 }
893 }
895 g_assert_not_reached();
896 return 0;
897 }
899 void
900 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
901 {
902 g_assert(item != NULL);
903 g_assert(SP_IS_ITEM(item));
904 g_assert(bbox != NULL);
906 sp_item_invoke_bbox(item, bbox, from_2geom(sp_item_i2d_affine(item)), TRUE, type);
907 }
909 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
910 {
911 NR::Maybe<NR::Rect> rect = NR::Nothing();
912 sp_item_invoke_bbox(item, &rect, from_2geom(sp_item_i2d_affine(item)), TRUE, type);
913 return rect;
914 }
916 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
917 {
918 NR::Maybe<NR::Rect> bbox = item->getBounds(from_2geom(sp_item_i2d_affine(item)));
919 /* Just the corners of the bounding box suffices given that we don't yet
920 support angled guide lines. */
922 if (bbox) {
923 NR::Point p1, p2;
924 p1 = bbox->min();
925 p2 = bbox->max();
926 *p = p1;
927 *p = NR::Point(p1[NR::X], p2[NR::Y]);
928 *p = p2;
929 *p = NR::Point(p1[NR::Y], p2[NR::X]);
930 }
931 }
933 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
934 {
935 g_assert (item != NULL);
936 g_assert (SP_IS_ITEM(item));
938 // Get the snappoints of the item
939 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
940 if (item_class.snappoints) {
941 item_class.snappoints(item, p);
942 }
944 // Get the snappoints at the item's center
945 if (includeItemCenter) {
946 *p = item->getCenter();
947 }
949 // Get the snappoints of clipping paths and mask, if any
950 std::list<SPObject const *> clips_and_masks;
952 clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
953 clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
955 for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
956 if (*o) {
957 // obj is a group object, the children are the actual clippers
958 for (SPObject *child = (*o)->children ; child ; child = child->next) {
959 if (SP_IS_ITEM(child)) {
960 std::vector<NR::Point> p_clip_or_mask;
961 // Please note the recursive call here!
962 sp_item_snappoints(SP_ITEM(child), includeItemCenter, SnapPointsIter(p_clip_or_mask));
963 // Take into account the transformation of the item being clipped or masked
964 for (std::vector<NR::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
965 // All snappoints are in desktop coordinates, but the item's transformation is
966 // in document coordinates. Hence the awkward construction below
967 *p = (*p_orig) * from_2geom(matrix_to_desktop (matrix_from_desktop (to_2geom(item->transform), item), item));
968 }
969 }
970 }
971 }
972 }
973 }
975 void
976 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
977 {
978 if (!item->isHidden()) {
979 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
980 if (!item->transform.test_identity()
981 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
982 {
983 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
984 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
985 sp_print_release(ctx);
986 } else {
987 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
988 }
989 }
990 }
991 }
993 static gchar *
994 sp_item_private_description(SPItem */*item*/)
995 {
996 return g_strdup(_("Object"));
997 }
999 /**
1000 * Returns a string suitable for status bar, formatted in pango markup language.
1001 *
1002 * Must be freed by caller.
1003 */
1004 gchar *
1005 sp_item_description(SPItem *item)
1006 {
1007 g_assert(item != NULL);
1008 g_assert(SP_IS_ITEM(item));
1010 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1011 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1012 if (s && item->clip_ref->getObject()) {
1013 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1014 g_free (s);
1015 s = snew;
1016 }
1017 if (s && item->mask_ref->getObject()) {
1018 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1019 g_free (s);
1020 s = snew;
1021 }
1022 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
1023 gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1024 g_free (s);
1025 s = snew;
1026 }
1027 return s;
1028 }
1030 g_assert_not_reached();
1031 return NULL;
1032 }
1034 /**
1035 * Allocates unique integer keys.
1036 * \param numkeys Number of keys required.
1037 * \return First allocated key; hence if the returned key is n
1038 * you can use n, n + 1, ..., n + (numkeys - 1)
1039 */
1040 unsigned
1041 sp_item_display_key_new(unsigned numkeys)
1042 {
1043 static unsigned dkey = 0;
1045 dkey += numkeys;
1047 return dkey - numkeys;
1048 }
1050 NRArenaItem *
1051 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1052 {
1053 g_assert(item != NULL);
1054 g_assert(SP_IS_ITEM(item));
1055 g_assert(arena != NULL);
1056 g_assert(NR_IS_ARENA(arena));
1058 NRArenaItem *ai = NULL;
1059 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1060 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1061 }
1063 if (ai != NULL) {
1064 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1065 nr_arena_item_set_transform(ai, item->transform);
1066 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1067 nr_arena_item_set_visible(ai, !item->isHidden());
1068 nr_arena_item_set_sensitive(ai, item->sensitive);
1069 if (item->clip_ref->getObject()) {
1070 SPClipPath *cp = item->clip_ref->getObject();
1072 if (!item->display->arenaitem->key) {
1073 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1074 }
1075 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1077 // Show and set clip
1078 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1079 nr_arena_item_set_clip(ai, ac);
1080 nr_arena_item_unref(ac);
1082 // Update bbox, in case the clip uses bbox units
1083 NRRect bbox;
1084 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1085 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1086 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1087 }
1088 if (item->mask_ref->getObject()) {
1089 SPMask *mask = item->mask_ref->getObject();
1091 if (!item->display->arenaitem->key) {
1092 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1093 }
1094 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1096 // Show and set mask
1097 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1098 nr_arena_item_set_mask(ai, ac);
1099 nr_arena_item_unref(ac);
1101 // Update bbox, in case the mask uses bbox units
1102 NRRect bbox;
1103 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1104 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1105 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1106 }
1107 NR_ARENA_ITEM_SET_DATA(ai, item);
1108 NRRect item_bbox;
1109 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1110 NR::Maybe<NR::Rect> i_bbox = item_bbox;
1111 nr_arena_item_set_item_bbox(ai, i_bbox);
1112 }
1114 return ai;
1115 }
1117 void
1118 sp_item_invoke_hide(SPItem *item, unsigned key)
1119 {
1120 g_assert(item != NULL);
1121 g_assert(SP_IS_ITEM(item));
1123 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1124 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1125 }
1127 SPItemView *ref = NULL;
1128 SPItemView *v = item->display;
1129 while (v != NULL) {
1130 SPItemView *next = v->next;
1131 if (v->key == key) {
1132 if (item->clip_ref->getObject()) {
1133 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1134 nr_arena_item_set_clip(v->arenaitem, NULL);
1135 }
1136 if (item->mask_ref->getObject()) {
1137 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1138 nr_arena_item_set_mask(v->arenaitem, NULL);
1139 }
1140 if (!ref) {
1141 item->display = v->next;
1142 } else {
1143 ref->next = v->next;
1144 }
1145 nr_arena_item_unparent(v->arenaitem);
1146 nr_arena_item_unref(v->arenaitem);
1147 g_free(v);
1148 } else {
1149 ref = v;
1150 }
1151 v = next;
1152 }
1153 }
1155 // Adjusters
1157 void
1158 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1159 {
1160 SPStyle *style = SP_OBJECT_STYLE (item);
1162 if (style && (style->fill.isPaintserver())) {
1163 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1164 if (SP_IS_PATTERN (server)) {
1165 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1166 sp_pattern_transform_multiply (pattern, postmul, set);
1167 }
1168 }
1170 if (style && (style->stroke.isPaintserver())) {
1171 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1172 if (SP_IS_PATTERN (server)) {
1173 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1174 sp_pattern_transform_multiply (pattern, postmul, set);
1175 }
1176 }
1178 }
1180 void
1181 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1182 {
1183 SPStyle *style = SP_OBJECT_STYLE (item);
1185 if (style && (style->fill.isPaintserver())) {
1186 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1187 if (SP_IS_GRADIENT (server)) {
1189 /**
1190 * \note Bbox units for a gradient are generally a bad idea because
1191 * with them, you cannot preserve the relative position of the
1192 * object and its gradient after rotation or skew. So now we
1193 * convert them to userspace units which are easy to keep in sync
1194 * just by adding the object's transform to gradientTransform.
1195 * \todo FIXME: convert back to bbox units after transforming with
1196 * the item, so as to preserve the original units.
1197 */
1198 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1200 sp_gradient_transform_multiply (gradient, postmul, set);
1201 }
1202 }
1204 if (style && (style->stroke.isPaintserver())) {
1205 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1206 if (SP_IS_GRADIENT (server)) {
1207 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1208 sp_gradient_transform_multiply (gradient, postmul, set);
1209 }
1210 }
1211 }
1213 void
1214 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1215 {
1216 SPStyle *style = SP_OBJECT_STYLE (item);
1218 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1220 style->stroke_width.computed *= ex;
1221 style->stroke_width.set = TRUE;
1223 if (style->stroke_dash.n_dash != 0) {
1224 int i;
1225 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1226 style->stroke_dash.dash[i] *= ex;
1227 }
1228 style->stroke_dash.offset *= ex;
1229 }
1231 SP_OBJECT(item)->updateRepr();
1232 }
1233 }
1235 /**
1236 * Find out the inverse of previous transform of an item (from its repr)
1237 */
1238 NR::Matrix
1239 sp_item_transform_repr (SPItem *item)
1240 {
1241 NR::Matrix t_old(NR::identity());
1242 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1243 if (t_attr) {
1244 NR::Matrix t;
1245 if (sp_svg_transform_read(t_attr, &t)) {
1246 t_old = t;
1247 }
1248 }
1250 return t_old;
1251 }
1254 /**
1255 * Recursively scale stroke width in \a item and its children by \a expansion.
1256 */
1257 void
1258 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1259 {
1260 sp_item_adjust_stroke (item, expansion);
1262 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1263 if (item && SP_IS_USE(item))
1264 return;
1266 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1267 if (SP_IS_ITEM(o))
1268 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1269 }
1270 }
1272 /**
1273 * Recursively adjust rx and ry of rects.
1274 */
1275 void
1276 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1277 {
1278 if (SP_IS_RECT (item)) {
1279 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1280 }
1282 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1283 if (SP_IS_ITEM(o))
1284 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1285 }
1286 }
1288 /**
1289 * Recursively compensate pattern or gradient transform.
1290 */
1291 void
1292 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1293 {
1294 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1295 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1296 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1297 NR::Matrix t_item = sp_item_transform_repr (item);
1298 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1300 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1301 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1302 // we must not touch it
1303 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1304 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1305 if (SP_IS_ITEM(o)) {
1306 // At the level of the transformed item, t_ancestors is identity;
1307 // below it, it is the accmmulated chain of transforms from this level to the top level
1308 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1309 }
1310 }
1311 }
1313 // We recursed into children first, and are now adjusting this object second;
1314 // this is so that adjustments in a tree are done from leaves up to the root,
1315 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1316 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1318 if (is_pattern)
1319 sp_item_adjust_pattern (item, paint_delta);
1320 else
1321 sp_item_adjust_gradient (item, paint_delta);
1323 }
1325 void
1326 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1327 {
1328 if ( !SP_IS_LPE_ITEM(item) )
1329 return;
1331 SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1332 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1333 PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
1334 for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1335 {
1336 // If the path effect is used by 2 or more items, fork it
1337 // so that each object has its own independent copy of the effect
1338 LivePathEffectObject *lpeobj = (*it)->lpeobject;
1339 if (lpeobj) {
1340 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1341 if (new_lpeobj != lpeobj) {
1342 sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1343 }
1345 if (lpeobj->lpe) {
1346 Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
1347 effect->transform_multiply(to_2geom(postmul), set);
1348 }
1349 }
1350 }
1351 }
1352 }
1354 /**
1355 * A temporary wrapper for the next function accepting the NR::Matrix
1356 * instead of NR::Matrix
1357 */
1358 void
1359 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1360 {
1361 if (transform == NULL)
1362 sp_item_write_transform(item, repr, NR::identity(), adv);
1363 else
1364 sp_item_write_transform(item, repr, *transform, adv);
1365 }
1367 /**
1368 * Set a new transform on an object.
1369 *
1370 * Compensate for stroke scaling and gradient/pattern fill transform, if
1371 * necessary. Call the object's set_transform method if transforms are
1372 * stored optimized. Send _transformed_signal. Invoke _write method so that
1373 * the repr is updated with the new transform.
1374 */
1375 void
1376 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1377 {
1378 g_return_if_fail(item != NULL);
1379 g_return_if_fail(SP_IS_ITEM(item));
1380 g_return_if_fail(repr != NULL);
1382 // calculate the relative transform, if not given by the adv attribute
1383 NR::Matrix advertized_transform;
1384 if (adv != NULL) {
1385 advertized_transform = *adv;
1386 } else {
1387 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1388 }
1390 if (compensate) {
1392 // recursively compensate for stroke scaling, depending on user preference
1393 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1394 double const expansion = 1. / NR::expansion(advertized_transform);
1395 sp_item_adjust_stroke_width_recursive(item, expansion);
1396 }
1398 // recursively compensate rx/ry of a rect if requested
1399 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1400 sp_item_adjust_rects_recursive(item, advertized_transform);
1401 }
1403 // recursively compensate pattern fill if it's not to be transformed
1404 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1405 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1406 }
1407 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1408 /// recursively compensate gradient fill if it's not to be transformed
1409 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1410 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1411 } else {
1412 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1413 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1414 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1415 }
1417 } // endif(compensate)
1419 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1420 NR::Matrix transform_attr (transform);
1421 if ( // run the object's set_transform (i.e. embed transform) only if:
1422 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1423 !preserve && // user did not chose to preserve all transforms
1424 !item->clip_ref->getObject() && // the object does not have a clippath
1425 !item->mask_ref->getObject() && // the object does not have a mask
1426 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1427 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1428 ) {
1429 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1430 }
1431 sp_item_set_item_transform(item, transform_attr);
1433 // Note: updateRepr comes before emitting the transformed signal since
1434 // it causes clone SPUse's copy of the original object to brought up to
1435 // date with the original. Otherwise, sp_use_bbox returns incorrect
1436 // values if called in code handling the transformed signal.
1437 SP_OBJECT(item)->updateRepr();
1439 // send the relative transform with a _transformed_signal
1440 item->_transformed_signal.emit(&advertized_transform, item);
1441 }
1443 gint
1444 sp_item_event(SPItem *item, SPEvent *event)
1445 {
1446 g_return_val_if_fail(item != NULL, FALSE);
1447 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1448 g_return_val_if_fail(event != NULL, FALSE);
1450 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1451 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1453 return FALSE;
1454 }
1456 /**
1457 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1458 * gradients, patterns as sp_item_write_transform does.
1459 */
1460 void
1461 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1462 {
1463 g_return_if_fail(item != NULL);
1464 g_return_if_fail(SP_IS_ITEM(item));
1466 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1467 item->transform = transform;
1468 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1469 transformation. It's apparently not used anywhere else. */
1470 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1471 sp_item_rm_unsatisfied_cns(*item);
1472 }
1473 }
1475 void
1476 sp_item_convert_item_to_guides(SPItem *item) {
1477 g_return_if_fail(item != NULL);
1478 g_return_if_fail(SP_IS_ITEM(item));
1480 /* Use derived method if present ... */
1481 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1482 (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1483 return;
1484 }
1486 /* .. otherwise simply place the guides around the item's bounding box */
1488 sp_item_convert_to_guides(item);
1489 }
1492 /**
1493 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1494 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1495 */
1496 Geom::Matrix
1497 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1498 Geom::Matrix ret(Geom::identity());
1499 g_return_val_if_fail(object != NULL, ret);
1501 /* stop at first non-renderable ancestor */
1502 while ( object != ancestor && SP_IS_ITEM(object) ) {
1503 if (SP_IS_ROOT(object)) {
1504 ret *= to_2geom(SP_ROOT(object)->c2p);
1505 }
1506 ret *= to_2geom(SP_ITEM(object)->transform);
1507 object = SP_OBJECT_PARENT(object);
1508 }
1509 return ret;
1510 }
1512 Geom::Matrix
1513 i2i_affine(SPObject const *src, SPObject const *dest) {
1514 g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1515 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1516 return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1517 }
1519 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1520 return from_2geom(i2i_affine(this, dest));
1521 }
1523 /**
1524 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1525 * \pre (item != NULL) and SP_IS_ITEM(item).
1526 */
1527 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1528 {
1529 return i2anc_affine(item, NULL);
1530 }
1532 /**
1533 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1534 * Used in path operations mostly.
1535 * \pre (item != NULL) and SP_IS_ITEM(item).
1536 */
1537 Geom::Matrix sp_item_i2root_affine(SPItem const *item)
1538 {
1539 g_assert(item != NULL);
1540 g_assert(SP_IS_ITEM(item));
1542 Geom::Matrix ret(Geom::identity());
1543 g_assert(ret.isIdentity());
1544 while ( NULL != SP_OBJECT_PARENT(item) ) {
1545 ret *= to_2geom(item->transform);
1546 item = SP_ITEM(SP_OBJECT_PARENT(item));
1547 }
1548 g_assert(SP_IS_ROOT(item));
1550 ret *= to_2geom(item->transform);
1552 return ret;
1553 }
1555 /* fixme: This is EVIL!!! */
1556 // fix this note: why/what evil? :)
1557 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1558 {
1559 g_assert(item != NULL);
1560 g_assert(SP_IS_ITEM(item));
1562 Geom::Matrix const ret( sp_item_i2doc_affine(item)
1563 * Geom::Scale(1, -1)
1564 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1565 return ret;
1566 }
1568 // same as i2d but with i2root instead of i2doc
1569 Geom::Matrix sp_item_i2r_affine(SPItem const *item)
1570 {
1571 g_assert(item != NULL);
1572 g_assert(SP_IS_ITEM(item));
1574 Geom::Matrix const ret( sp_item_i2root_affine(item)
1575 * Geom::Scale(1, -1)
1576 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1577 return ret;
1578 }
1580 /**
1581 * Converts a matrix \a m into the desktop coords of the \a item.
1582 * Will become a noop when we eliminate the coordinate flipping.
1583 */
1584 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1585 {
1586 Geom::Matrix const ret(m
1587 * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1588 * Geom::Scale(1, -1));
1589 return ret;
1590 }
1592 /**
1593 * Converts a matrix \a m from the desktop coords of the \a item.
1594 * Will become a noop when we eliminate the coordinate flipping.
1595 */
1596 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1597 {
1598 Geom::Matrix const ret(Geom::Scale(1, -1)
1599 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1600 * m);
1601 return ret;
1602 }
1604 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1605 {
1606 g_return_if_fail( item != NULL );
1607 g_return_if_fail( SP_IS_ITEM(item) );
1609 Geom::Matrix dt2p; /* desktop to item parent transform */
1610 if (SP_OBJECT_PARENT(item)) {
1611 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1612 } else {
1613 dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1614 * Geom::Scale(1, -1) );
1615 }
1617 Geom::Matrix const i2p( i2dt * dt2p );
1618 sp_item_set_item_transform(item, from_2geom(i2p));
1619 }
1622 Geom::Matrix
1623 sp_item_dt2i_affine(SPItem const *item)
1624 {
1625 /* fixme: Implement the right way (Lauris) */
1626 return sp_item_i2d_affine(item).inverse();
1627 }
1629 /* Item views */
1631 static SPItemView *
1632 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1633 {
1634 SPItemView *new_view;
1636 g_assert(item != NULL);
1637 g_assert(SP_IS_ITEM(item));
1638 g_assert(arenaitem != NULL);
1639 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1641 new_view = g_new(SPItemView, 1);
1643 new_view->next = list;
1644 new_view->flags = flags;
1645 new_view->key = key;
1646 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1648 return new_view;
1649 }
1651 static SPItemView *
1652 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1653 {
1654 if (view == list) {
1655 list = list->next;
1656 } else {
1657 SPItemView *prev;
1658 prev = list;
1659 while (prev->next != view) prev = prev->next;
1660 prev->next = view->next;
1661 }
1663 nr_arena_item_unref(view->arenaitem);
1664 g_free(view);
1666 return list;
1667 }
1669 /**
1670 * Return the arenaitem corresponding to the given item in the display
1671 * with the given key
1672 */
1673 NRArenaItem *
1674 sp_item_get_arenaitem(SPItem *item, unsigned key)
1675 {
1676 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1677 if ( iv->key == key ) {
1678 return iv->arenaitem;
1679 }
1680 }
1682 return NULL;
1683 }
1685 int
1686 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1687 {
1688 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1689 SP_OBJECT_REPR(second));
1690 }
1692 SPItem *
1693 sp_item_first_item_child (SPObject *obj)
1694 {
1695 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1696 if (SP_IS_ITEM (iter))
1697 return SP_ITEM (iter);
1698 }
1699 return NULL;
1700 }
1702 void
1703 sp_item_convert_to_guides(SPItem *item) {
1704 SPDesktop *dt = inkscape_active_desktop();
1705 SPNamedView *nv = sp_desktop_namedview(dt);
1706 (void)nv;
1708 int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
1709 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1710 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1712 NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1713 if (!bbox) {
1714 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1715 return;
1716 }
1718 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1720 NR::Point A((*bbox).min());
1721 NR::Point C((*bbox).max());
1722 NR::Point B(A[NR::X], C[NR::Y]);
1723 NR::Point D(C[NR::X], A[NR::Y]);
1725 pts.push_back(std::make_pair(A, B));
1726 pts.push_back(std::make_pair(B, C));
1727 pts.push_back(std::make_pair(C, D));
1728 pts.push_back(std::make_pair(D, A));
1730 sp_guide_pt_pairs_to_guides(dt, pts);
1731 }
1733 /*
1734 Local Variables:
1735 mode:c++
1736 c-file-style:"stroustrup"
1737 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1738 indent-tabs-mode:nil
1739 fill-column:99
1740 End:
1741 */
1742 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :