f88d7b9e04b728f2047e9f031c752c98078f7275
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 boost::optional<NR::Rect> bbox = getBounds(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 boost::optional<NR::Rect> bbox = getBounds(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 boost::optional<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 boost::optional<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
697 SPItem::BBoxType type,
698 unsigned int /*dkey*/) const
699 {
700 boost::optional<NR::Rect> r;
701 sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
702 return r;
703 }
705 void
706 sp_item_invoke_bbox(SPItem const *item, boost::optional<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
707 {
708 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
709 }
711 // DEPRECATED to phase out the use of NRRect in favor of boost::optional<NR::Rect>
712 void
713 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
714 {
715 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
716 }
718 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
719 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
720 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
721 * clones), in turn, call this function in their bbox methods. */
722 void
723 sp_item_invoke_bbox_full(SPItem const *item, boost::optional<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
724 {
725 g_assert(item != NULL);
726 g_assert(SP_IS_ITEM(item));
727 g_assert(bbox != NULL);
729 if (clear) {
730 *bbox = boost::optional<NR::Rect>();
731 }
733 // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
735 NRRect temp_bbox;
736 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
737 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
739 // call the subclass method
740 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
741 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
742 }
744 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
745 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
746 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
747 SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
748 if (filter && SP_IS_FILTER(filter)) {
749 // default filer area per the SVG spec:
750 double x = -0.1;
751 double y = -0.1;
752 double w = 1.2;
753 double h = 1.2;
755 // if area is explicitly set, override:
756 if (SP_FILTER(filter)->x._set)
757 x = SP_FILTER(filter)->x.computed;
758 if (SP_FILTER(filter)->y._set)
759 y = SP_FILTER(filter)->y.computed;
760 if (SP_FILTER(filter)->width._set)
761 w = SP_FILTER(filter)->width.computed;
762 if (SP_FILTER(filter)->height._set)
763 h = SP_FILTER(filter)->height.computed;
765 double dx0 = 0;
766 double dx1 = 0;
767 double dy0 = 0;
768 double dy1 = 0;
769 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
770 // if this is a single blur, use 2.4*radius
771 // which may be smaller than the default area;
772 // see set_filter_area for why it's 2.4
773 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
774 dx0 = -2.4 * r;
775 dx1 = 2.4 * r;
776 dy0 = -2.4 * r;
777 dy1 = 2.4 * r;
778 } else {
779 // otherwise, calculate expansion from relative to absolute units:
780 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
781 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
782 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
783 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
784 }
786 // transform the expansions by the item's transform:
787 NR::Matrix i2d(sp_item_i2d_affine (item));
788 dx0 *= NR::expansionX(i2d);
789 dx1 *= NR::expansionX(i2d);
790 dy0 *= NR::expansionY(i2d);
791 dy1 *= NR::expansionY(i2d);
793 // expand the bbox
794 temp_bbox.x0 += dx0;
795 temp_bbox.x1 += dx1;
796 temp_bbox.y0 += dy0;
797 temp_bbox.y1 += dy1;
798 }
799 }
800 if (item->clip_ref->getObject()) {
801 NRRect b;
802 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
803 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
804 }
805 }
807 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
808 // Either the bbox hasn't been touched by the SPItemClass' bbox method
809 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
810 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
812 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
813 // explicitely defined this way for NRRects (as opposed to boost::optional<NR::Rect>)
814 // So union bbox with nothing = do nothing, just return
815 return;
816 }
818 // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty boost::optional<NR::Rect>()
819 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
820 // would therefore be translated into empty boost::optional<NR::Rect>() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
821 boost::optional<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
823 *bbox = NR::union_bounds(*bbox, temp_bbox_new);
824 }
826 // DEPRECATED to phase out the use of NRRect in favor of boost::optional<NR::Rect>
827 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
828 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
829 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
830 * clones), in turn, call this function in their bbox methods. */
831 void
832 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
833 {
834 g_assert(item != NULL);
835 g_assert(SP_IS_ITEM(item));
836 g_assert(bbox != NULL);
838 if (clear) {
839 bbox->x0 = bbox->y0 = 1e18;
840 bbox->x1 = bbox->y1 = -1e18;
841 }
843 NRRect this_bbox;
844 this_bbox.x0 = this_bbox.y0 = 1e18;
845 this_bbox.x1 = this_bbox.y1 = -1e18;
847 // call the subclass method
848 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
849 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
850 }
852 // unless this is geometric bbox, crop the bbox by clip path, if any
853 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
854 NRRect b;
855 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
856 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
857 }
859 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
860 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
861 nr_rect_d_union (bbox, bbox, &this_bbox);
862 }
863 }
865 unsigned sp_item_pos_in_parent(SPItem *item)
866 {
867 g_assert(item != NULL);
868 g_assert(SP_IS_ITEM(item));
870 SPObject *parent = SP_OBJECT_PARENT(item);
871 g_assert(parent != NULL);
872 g_assert(SP_IS_OBJECT(parent));
874 SPObject *object = SP_OBJECT(item);
876 unsigned pos=0;
877 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
878 if ( iter == object ) {
879 return pos;
880 }
881 if (SP_IS_ITEM(iter)) {
882 pos++;
883 }
884 }
886 g_assert_not_reached();
887 return 0;
888 }
890 void
891 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
892 {
893 g_assert(item != NULL);
894 g_assert(SP_IS_ITEM(item));
895 g_assert(bbox != NULL);
897 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
898 }
900 boost::optional<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
901 {
902 boost::optional<NR::Rect> rect = boost::optional<NR::Rect>();
903 sp_item_invoke_bbox(item, &rect, sp_item_i2d_affine(item), TRUE, type);
904 return rect;
905 }
907 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
908 {
909 boost::optional<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
910 /* Just the corners of the bounding box suffices given that we don't yet
911 support angled guide lines. */
913 if (bbox) {
914 NR::Point p1, p2;
915 p1 = bbox->min();
916 p2 = bbox->max();
917 *p = p1;
918 *p = NR::Point(p1[NR::X], p2[NR::Y]);
919 *p = p2;
920 *p = NR::Point(p1[NR::Y], p2[NR::X]);
921 }
922 }
924 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
925 {
926 g_assert (item != NULL);
927 g_assert (SP_IS_ITEM(item));
929 // Get the snappoints of the item
930 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
931 if (item_class.snappoints) {
932 item_class.snappoints(item, p);
933 }
935 // Get the snappoints at the item's center
936 if (includeItemCenter) {
937 *p = item->getCenter();
938 }
940 // Get the snappoints of clipping paths and mask, if any
941 std::list<SPObject const *> clips_and_masks;
943 clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
944 clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
946 for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
947 if (*o) {
948 // obj is a group object, the children are the actual clippers
949 for (SPObject *child = (*o)->children ; child ; child = child->next) {
950 if (SP_IS_ITEM(child)) {
951 std::vector<NR::Point> p_clip_or_mask;
952 // Please note the recursive call here!
953 sp_item_snappoints(SP_ITEM(child), includeItemCenter, SnapPointsIter(p_clip_or_mask));
954 // Take into account the transformation of the item being clipped or masked
955 for (std::vector<NR::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
956 // All snappoints are in desktop coordinates, but the item's transformation is
957 // in document coordinates. Hence the awkward construction below
958 *p = (*p_orig) * from_2geom(matrix_to_desktop (matrix_from_desktop (item->transform, item), item));
959 }
960 }
961 }
962 }
963 }
964 }
966 void
967 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
968 {
969 if (!item->isHidden()) {
970 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
971 if (!item->transform.test_identity()
972 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
973 {
974 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
975 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
976 sp_print_release(ctx);
977 } else {
978 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
979 }
980 }
981 }
982 }
984 static gchar *
985 sp_item_private_description(SPItem */*item*/)
986 {
987 return g_strdup(_("Object"));
988 }
990 /**
991 * Returns a string suitable for status bar, formatted in pango markup language.
992 *
993 * Must be freed by caller.
994 */
995 gchar *
996 sp_item_description(SPItem *item)
997 {
998 g_assert(item != NULL);
999 g_assert(SP_IS_ITEM(item));
1001 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1002 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1003 if (s && item->clip_ref->getObject()) {
1004 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1005 g_free (s);
1006 s = snew;
1007 }
1008 if (s && item->mask_ref->getObject()) {
1009 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1010 g_free (s);
1011 s = snew;
1012 }
1013 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
1014 gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1015 g_free (s);
1016 s = snew;
1017 }
1018 return s;
1019 }
1021 g_assert_not_reached();
1022 return NULL;
1023 }
1025 /**
1026 * Allocates unique integer keys.
1027 * \param numkeys Number of keys required.
1028 * \return First allocated key; hence if the returned key is n
1029 * you can use n, n + 1, ..., n + (numkeys - 1)
1030 */
1031 unsigned
1032 sp_item_display_key_new(unsigned numkeys)
1033 {
1034 static unsigned dkey = 0;
1036 dkey += numkeys;
1038 return dkey - numkeys;
1039 }
1041 NRArenaItem *
1042 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1043 {
1044 g_assert(item != NULL);
1045 g_assert(SP_IS_ITEM(item));
1046 g_assert(arena != NULL);
1047 g_assert(NR_IS_ARENA(arena));
1049 NRArenaItem *ai = NULL;
1050 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1051 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1052 }
1054 if (ai != NULL) {
1055 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1056 nr_arena_item_set_transform(ai, item->transform);
1057 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1058 nr_arena_item_set_visible(ai, !item->isHidden());
1059 nr_arena_item_set_sensitive(ai, item->sensitive);
1060 if (item->clip_ref->getObject()) {
1061 SPClipPath *cp = item->clip_ref->getObject();
1063 if (!item->display->arenaitem->key) {
1064 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1065 }
1066 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1068 // Show and set clip
1069 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1070 nr_arena_item_set_clip(ai, ac);
1071 nr_arena_item_unref(ac);
1073 // Update bbox, in case the clip uses bbox units
1074 NRRect bbox;
1075 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1076 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1077 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1078 }
1079 if (item->mask_ref->getObject()) {
1080 SPMask *mask = item->mask_ref->getObject();
1082 if (!item->display->arenaitem->key) {
1083 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1084 }
1085 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1087 // Show and set mask
1088 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1089 nr_arena_item_set_mask(ai, ac);
1090 nr_arena_item_unref(ac);
1092 // Update bbox, in case the mask uses bbox units
1093 NRRect bbox;
1094 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1095 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1096 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1097 }
1098 NR_ARENA_ITEM_SET_DATA(ai, item);
1099 NRRect item_bbox;
1100 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1101 boost::optional<NR::Rect> i_bbox = item_bbox;
1102 nr_arena_item_set_item_bbox(ai, i_bbox);
1103 }
1105 return ai;
1106 }
1108 void
1109 sp_item_invoke_hide(SPItem *item, unsigned key)
1110 {
1111 g_assert(item != NULL);
1112 g_assert(SP_IS_ITEM(item));
1114 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1115 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1116 }
1118 SPItemView *ref = NULL;
1119 SPItemView *v = item->display;
1120 while (v != NULL) {
1121 SPItemView *next = v->next;
1122 if (v->key == key) {
1123 if (item->clip_ref->getObject()) {
1124 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1125 nr_arena_item_set_clip(v->arenaitem, NULL);
1126 }
1127 if (item->mask_ref->getObject()) {
1128 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1129 nr_arena_item_set_mask(v->arenaitem, NULL);
1130 }
1131 if (!ref) {
1132 item->display = v->next;
1133 } else {
1134 ref->next = v->next;
1135 }
1136 nr_arena_item_unparent(v->arenaitem);
1137 nr_arena_item_unref(v->arenaitem);
1138 g_free(v);
1139 } else {
1140 ref = v;
1141 }
1142 v = next;
1143 }
1144 }
1146 // Adjusters
1148 void
1149 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1150 {
1151 SPStyle *style = SP_OBJECT_STYLE (item);
1153 if (style && (style->fill.isPaintserver())) {
1154 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1155 if (SP_IS_PATTERN (server)) {
1156 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1157 sp_pattern_transform_multiply (pattern, postmul, set);
1158 }
1159 }
1161 if (style && (style->stroke.isPaintserver())) {
1162 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1163 if (SP_IS_PATTERN (server)) {
1164 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1165 sp_pattern_transform_multiply (pattern, postmul, set);
1166 }
1167 }
1169 }
1171 void
1172 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1173 {
1174 SPStyle *style = SP_OBJECT_STYLE (item);
1176 if (style && (style->fill.isPaintserver())) {
1177 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1178 if (SP_IS_GRADIENT (server)) {
1180 /**
1181 * \note Bbox units for a gradient are generally a bad idea because
1182 * with them, you cannot preserve the relative position of the
1183 * object and its gradient after rotation or skew. So now we
1184 * convert them to userspace units which are easy to keep in sync
1185 * just by adding the object's transform to gradientTransform.
1186 * \todo FIXME: convert back to bbox units after transforming with
1187 * the item, so as to preserve the original units.
1188 */
1189 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1191 sp_gradient_transform_multiply (gradient, postmul, set);
1192 }
1193 }
1195 if (style && (style->stroke.isPaintserver())) {
1196 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1197 if (SP_IS_GRADIENT (server)) {
1198 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1199 sp_gradient_transform_multiply (gradient, postmul, set);
1200 }
1201 }
1202 }
1204 void
1205 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1206 {
1207 SPStyle *style = SP_OBJECT_STYLE (item);
1209 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1211 style->stroke_width.computed *= ex;
1212 style->stroke_width.set = TRUE;
1214 if (style->stroke_dash.n_dash != 0) {
1215 int i;
1216 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1217 style->stroke_dash.dash[i] *= ex;
1218 }
1219 style->stroke_dash.offset *= ex;
1220 }
1222 SP_OBJECT(item)->updateRepr();
1223 }
1224 }
1226 /**
1227 * Find out the inverse of previous transform of an item (from its repr)
1228 */
1229 NR::Matrix
1230 sp_item_transform_repr (SPItem *item)
1231 {
1232 NR::Matrix t_old(NR::identity());
1233 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1234 if (t_attr) {
1235 NR::Matrix t;
1236 if (sp_svg_transform_read(t_attr, &t)) {
1237 t_old = t;
1238 }
1239 }
1241 return t_old;
1242 }
1245 /**
1246 * Recursively scale stroke width in \a item and its children by \a expansion.
1247 */
1248 void
1249 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1250 {
1251 sp_item_adjust_stroke (item, expansion);
1253 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1254 if (item && SP_IS_USE(item))
1255 return;
1257 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1258 if (SP_IS_ITEM(o))
1259 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1260 }
1261 }
1263 /**
1264 * Recursively adjust rx and ry of rects.
1265 */
1266 void
1267 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1268 {
1269 if (SP_IS_RECT (item)) {
1270 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1271 }
1273 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1274 if (SP_IS_ITEM(o))
1275 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1276 }
1277 }
1279 /**
1280 * Recursively compensate pattern or gradient transform.
1281 */
1282 void
1283 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1284 {
1285 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1286 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1287 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1288 NR::Matrix t_item = sp_item_transform_repr (item);
1289 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1291 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1292 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1293 // we must not touch it
1294 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1295 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1296 if (SP_IS_ITEM(o)) {
1297 // At the level of the transformed item, t_ancestors is identity;
1298 // below it, it is the accmmulated chain of transforms from this level to the top level
1299 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1300 }
1301 }
1302 }
1304 // We recursed into children first, and are now adjusting this object second;
1305 // this is so that adjustments in a tree are done from leaves up to the root,
1306 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1307 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1309 if (is_pattern)
1310 sp_item_adjust_pattern (item, paint_delta);
1311 else
1312 sp_item_adjust_gradient (item, paint_delta);
1314 }
1316 void
1317 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1318 {
1319 if ( !SP_IS_LPE_ITEM(item) )
1320 return;
1322 SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1323 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1324 PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
1325 for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1326 {
1327 // If the path effect is used by 2 or more items, fork it
1328 // so that each object has its own independent copy of the effect
1329 LivePathEffectObject *lpeobj = (*it)->lpeobject;
1330 if (lpeobj) {
1331 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1332 if (new_lpeobj != lpeobj) {
1333 sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1334 }
1336 if (lpeobj->lpe) {
1337 Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
1338 effect->transform_multiply(postmul, set);
1339 }
1340 }
1341 }
1342 }
1343 }
1345 /**
1346 * A temporary wrapper for the next function accepting the NR::Matrix
1347 * instead of NR::Matrix
1348 */
1349 void
1350 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1351 {
1352 if (transform == NULL)
1353 sp_item_write_transform(item, repr, NR::identity(), adv);
1354 else
1355 sp_item_write_transform(item, repr, *transform, adv);
1356 }
1358 /**
1359 * Set a new transform on an object.
1360 *
1361 * Compensate for stroke scaling and gradient/pattern fill transform, if
1362 * necessary. Call the object's set_transform method if transforms are
1363 * stored optimized. Send _transformed_signal. Invoke _write method so that
1364 * the repr is updated with the new transform.
1365 */
1366 void
1367 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1368 {
1369 g_return_if_fail(item != NULL);
1370 g_return_if_fail(SP_IS_ITEM(item));
1371 g_return_if_fail(repr != NULL);
1373 // calculate the relative transform, if not given by the adv attribute
1374 NR::Matrix advertized_transform;
1375 if (adv != NULL) {
1376 advertized_transform = *adv;
1377 } else {
1378 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1379 }
1381 if (compensate) {
1383 // recursively compensate for stroke scaling, depending on user preference
1384 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1385 double const expansion = 1. / NR::expansion(advertized_transform);
1386 sp_item_adjust_stroke_width_recursive(item, expansion);
1387 }
1389 // recursively compensate rx/ry of a rect if requested
1390 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1391 sp_item_adjust_rects_recursive(item, advertized_transform);
1392 }
1394 // recursively compensate pattern fill if it's not to be transformed
1395 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1396 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1397 }
1398 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1399 /// recursively compensate gradient fill if it's not to be transformed
1400 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1401 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1402 } else {
1403 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1404 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1405 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1406 }
1408 } // endif(compensate)
1410 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1411 NR::Matrix transform_attr (transform);
1412 if ( // run the object's set_transform (i.e. embed transform) only if:
1413 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1414 !preserve && // user did not chose to preserve all transforms
1415 !item->clip_ref->getObject() && // the object does not have a clippath
1416 !item->mask_ref->getObject() && // the object does not have a mask
1417 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1418 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1419 ) {
1420 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1421 }
1422 sp_item_set_item_transform(item, transform_attr);
1424 // Note: updateRepr comes before emitting the transformed signal since
1425 // it causes clone SPUse's copy of the original object to brought up to
1426 // date with the original. Otherwise, sp_use_bbox returns incorrect
1427 // values if called in code handling the transformed signal.
1428 SP_OBJECT(item)->updateRepr();
1430 // send the relative transform with a _transformed_signal
1431 item->_transformed_signal.emit(&advertized_transform, item);
1432 }
1434 gint
1435 sp_item_event(SPItem *item, SPEvent *event)
1436 {
1437 g_return_val_if_fail(item != NULL, FALSE);
1438 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1439 g_return_val_if_fail(event != NULL, FALSE);
1441 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1442 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1444 return FALSE;
1445 }
1447 /**
1448 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1449 * gradients, patterns as sp_item_write_transform does.
1450 */
1451 void
1452 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1453 {
1454 g_return_if_fail(item != NULL);
1455 g_return_if_fail(SP_IS_ITEM(item));
1457 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1458 item->transform = transform;
1459 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1460 transformation. It's apparently not used anywhere else. */
1461 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1462 sp_item_rm_unsatisfied_cns(*item);
1463 }
1464 }
1466 void
1467 sp_item_convert_item_to_guides(SPItem *item) {
1468 g_return_if_fail(item != NULL);
1469 g_return_if_fail(SP_IS_ITEM(item));
1471 /* Use derived method if present ... */
1472 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1473 (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1474 return;
1475 }
1477 /* .. otherwise simply place the guides around the item's bounding box */
1479 sp_item_convert_to_guides(item);
1480 }
1483 /**
1484 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1485 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1486 */
1487 Geom::Matrix
1488 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1489 Geom::Matrix ret(Geom::identity());
1490 g_return_val_if_fail(object != NULL, ret);
1492 /* stop at first non-renderable ancestor */
1493 while ( object != ancestor && SP_IS_ITEM(object) ) {
1494 if (SP_IS_ROOT(object)) {
1495 ret *= SP_ROOT(object)->c2p;
1496 }
1497 ret *= SP_ITEM(object)->transform;
1498 object = SP_OBJECT_PARENT(object);
1499 }
1500 return ret;
1501 }
1503 Geom::Matrix
1504 i2i_affine(SPObject const *src, SPObject const *dest) {
1505 g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1506 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1507 return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1508 }
1510 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1511 return i2i_affine(this, dest);
1512 }
1514 /**
1515 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1516 * \pre (item != NULL) and SP_IS_ITEM(item).
1517 */
1518 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1519 {
1520 return i2anc_affine(item, NULL);
1521 }
1523 /**
1524 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1525 * Used in path operations mostly.
1526 * \pre (item != NULL) and SP_IS_ITEM(item).
1527 */
1528 Geom::Matrix sp_item_i2root_affine(SPItem const *item)
1529 {
1530 g_assert(item != NULL);
1531 g_assert(SP_IS_ITEM(item));
1533 Geom::Matrix ret(Geom::identity());
1534 g_assert(ret.isIdentity());
1535 while ( NULL != SP_OBJECT_PARENT(item) ) {
1536 ret *= item->transform;
1537 item = SP_ITEM(SP_OBJECT_PARENT(item));
1538 }
1539 g_assert(SP_IS_ROOT(item));
1541 ret *= item->transform;
1543 return ret;
1544 }
1546 /* fixme: This is EVIL!!! */
1547 // fix this note: why/what evil? :)
1548 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1549 {
1550 g_assert(item != NULL);
1551 g_assert(SP_IS_ITEM(item));
1553 Geom::Matrix const ret( sp_item_i2doc_affine(item)
1554 * Geom::Scale(1, -1)
1555 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1556 return ret;
1557 }
1559 // same as i2d but with i2root instead of i2doc
1560 Geom::Matrix sp_item_i2r_affine(SPItem const *item)
1561 {
1562 g_assert(item != NULL);
1563 g_assert(SP_IS_ITEM(item));
1565 Geom::Matrix const ret( sp_item_i2root_affine(item)
1566 * Geom::Scale(1, -1)
1567 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1568 return ret;
1569 }
1571 /**
1572 * Converts a matrix \a m into the desktop coords of the \a item.
1573 * Will become a noop when we eliminate the coordinate flipping.
1574 */
1575 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1576 {
1577 Geom::Matrix const ret(m
1578 * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1579 * Geom::Scale(1, -1));
1580 return ret;
1581 }
1583 /**
1584 * Converts a matrix \a m from the desktop coords of the \a item.
1585 * Will become a noop when we eliminate the coordinate flipping.
1586 */
1587 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1588 {
1589 Geom::Matrix const ret(Geom::Scale(1, -1)
1590 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1591 * m);
1592 return ret;
1593 }
1595 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1596 {
1597 g_return_if_fail( item != NULL );
1598 g_return_if_fail( SP_IS_ITEM(item) );
1600 Geom::Matrix dt2p; /* desktop to item parent transform */
1601 if (SP_OBJECT_PARENT(item)) {
1602 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1603 } else {
1604 dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1605 * Geom::Scale(1, -1) );
1606 }
1608 Geom::Matrix const i2p( i2dt * dt2p );
1609 sp_item_set_item_transform(item, i2p);
1610 }
1613 Geom::Matrix
1614 sp_item_dt2i_affine(SPItem const *item)
1615 {
1616 /* fixme: Implement the right way (Lauris) */
1617 return sp_item_i2d_affine(item).inverse();
1618 }
1620 /* Item views */
1622 static SPItemView *
1623 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1624 {
1625 SPItemView *new_view;
1627 g_assert(item != NULL);
1628 g_assert(SP_IS_ITEM(item));
1629 g_assert(arenaitem != NULL);
1630 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1632 new_view = g_new(SPItemView, 1);
1634 new_view->next = list;
1635 new_view->flags = flags;
1636 new_view->key = key;
1637 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1639 return new_view;
1640 }
1642 static SPItemView *
1643 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1644 {
1645 if (view == list) {
1646 list = list->next;
1647 } else {
1648 SPItemView *prev;
1649 prev = list;
1650 while (prev->next != view) prev = prev->next;
1651 prev->next = view->next;
1652 }
1654 nr_arena_item_unref(view->arenaitem);
1655 g_free(view);
1657 return list;
1658 }
1660 /**
1661 * Return the arenaitem corresponding to the given item in the display
1662 * with the given key
1663 */
1664 NRArenaItem *
1665 sp_item_get_arenaitem(SPItem *item, unsigned key)
1666 {
1667 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1668 if ( iv->key == key ) {
1669 return iv->arenaitem;
1670 }
1671 }
1673 return NULL;
1674 }
1676 int
1677 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1678 {
1679 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1680 SP_OBJECT_REPR(second));
1681 }
1683 SPItem *
1684 sp_item_first_item_child (SPObject *obj)
1685 {
1686 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1687 if (SP_IS_ITEM (iter))
1688 return SP_ITEM (iter);
1689 }
1690 return NULL;
1691 }
1693 void
1694 sp_item_convert_to_guides(SPItem *item) {
1695 SPDesktop *dt = inkscape_active_desktop();
1696 SPNamedView *nv = sp_desktop_namedview(dt);
1697 (void)nv;
1699 int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
1700 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1701 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1703 boost::optional<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1704 if (!bbox) {
1705 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1706 return;
1707 }
1709 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1711 NR::Point A((*bbox).min());
1712 NR::Point C((*bbox).max());
1713 NR::Point B(A[NR::X], C[NR::Y]);
1714 NR::Point D(C[NR::X], A[NR::Y]);
1716 pts.push_back(std::make_pair(A, B));
1717 pts.push_back(std::make_pair(B, C));
1718 pts.push_back(std::make_pair(C, D));
1719 pts.push_back(std::make_pair(D, A));
1721 sp_guide_pt_pairs_to_guides(dt, pts);
1722 }
1724 /*
1725 Local Variables:
1726 mode:c++
1727 c-file-style:"stroustrup"
1728 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1729 indent-tabs-mode:nil
1730 fill-column:99
1731 End:
1732 */
1733 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :