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