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