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 Geom::OptRect 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 Geom::OptRect 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 Geom::OptRect item_bbox;
646 sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
648 SPItemView *itemview = item->display;
649 do {
650 if (itemview->arenaitem)
651 nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
652 } while ( (itemview = itemview->next) );
653 }
655 // Update libavoid with item geometry (for connector routing).
656 if (item->avoidRef)
657 item->avoidRef->handleSettingChange();
658 }
660 static Inkscape::XML::Node *
661 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
662 {
663 SPObject *child;
664 SPItem *item = SP_ITEM(object);
666 // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
667 // so we need to add any children from the underlying object to the new repr
668 if (flags & SP_OBJECT_WRITE_BUILD) {
669 Inkscape::XML::Node *crepr;
670 GSList *l;
671 l = NULL;
672 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
673 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
674 crepr = child->updateRepr(xml_doc, NULL, flags);
675 if (crepr) l = g_slist_prepend (l, crepr);
676 }
677 while (l) {
678 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
679 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
680 l = g_slist_remove (l, l->data);
681 }
682 } else {
683 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
684 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
685 child->updateRepr(flags);
686 }
687 }
689 gchar *c = sp_svg_transform_write(item->transform);
690 repr->setAttribute("transform", c);
691 g_free(c);
693 if (flags & SP_OBJECT_WRITE_EXT) {
694 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
695 if (item->transform_center_x != 0)
696 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
697 else
698 repr->setAttribute ("inkscape:transform-center-x", NULL);
699 if (item->transform_center_y != 0)
700 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
701 else
702 repr->setAttribute ("inkscape:transform-center-y", NULL);
703 }
705 if (item->clip_ref->getObject()) {
706 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
707 repr->setAttribute ("clip-path", value);
708 g_free ((void *) value);
709 }
710 if (item->mask_ref->getObject()) {
711 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
712 repr->setAttribute ("mask", value);
713 g_free ((void *) value);
714 }
716 if (((SPObjectClass *) (parent_class))->write) {
717 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
718 }
720 return repr;
721 }
723 /**
724 * \return There is no guarantee that the return value will contain a rectangle.
725 If this item does not have a boundingbox, it might well be empty.
726 */
727 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
728 SPItem::BBoxType type,
729 unsigned int /*dkey*/) const
730 {
731 Geom::OptRect r;
732 sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
733 return r;
734 }
736 void
737 sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
738 {
739 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
740 }
742 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
743 void
744 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
745 {
746 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
747 }
749 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
750 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
751 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
752 * clones), in turn, call this function in their bbox methods.
753 * \retval bbox Note that there is no guarantee that bbox will contain a rectangle when the
754 * function returns. If this item does not have a boundingbox, this might well be empty.
755 */
756 void
757 sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
758 {
759 g_assert(item != NULL);
760 g_assert(SP_IS_ITEM(item));
762 if (clear) {
763 bbox = Geom::OptRect();
764 }
766 // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
768 NRRect temp_bbox;
769 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
770 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
772 // call the subclass method
773 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
774 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
775 }
777 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
778 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
779 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
780 SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
781 if (filter && SP_IS_FILTER(filter)) {
782 // default filer area per the SVG spec:
783 double x = -0.1;
784 double y = -0.1;
785 double w = 1.2;
786 double h = 1.2;
788 // if area is explicitly set, override:
789 if (SP_FILTER(filter)->x._set)
790 x = SP_FILTER(filter)->x.computed;
791 if (SP_FILTER(filter)->y._set)
792 y = SP_FILTER(filter)->y.computed;
793 if (SP_FILTER(filter)->width._set)
794 w = SP_FILTER(filter)->width.computed;
795 if (SP_FILTER(filter)->height._set)
796 h = SP_FILTER(filter)->height.computed;
798 double dx0 = 0;
799 double dx1 = 0;
800 double dy0 = 0;
801 double dy1 = 0;
802 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
803 // if this is a single blur, use 2.4*radius
804 // which may be smaller than the default area;
805 // see set_filter_area for why it's 2.4
806 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
807 dx0 = -2.4 * r;
808 dx1 = 2.4 * r;
809 dy0 = -2.4 * r;
810 dy1 = 2.4 * r;
811 } else {
812 // otherwise, calculate expansion from relative to absolute units:
813 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
814 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
815 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
816 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
817 }
819 // transform the expansions by the item's transform:
820 Geom::Matrix i2d(sp_item_i2d_affine (item));
821 dx0 *= i2d.expansionX();
822 dx1 *= i2d.expansionX();
823 dy0 *= i2d.expansionY();
824 dy1 *= i2d.expansionY();
826 // expand the bbox
827 temp_bbox.x0 += dx0;
828 temp_bbox.x1 += dx1;
829 temp_bbox.y0 += dy0;
830 temp_bbox.y1 += dy1;
831 }
832 }
833 if (item->clip_ref->getObject()) {
834 NRRect b;
835 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
836 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
837 }
838 }
840 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
841 // Either the bbox hasn't been touched by the SPItemClass' bbox method
842 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
843 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
845 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
846 // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
847 // So union bbox with nothing = do nothing, just return
848 return;
849 }
851 // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
852 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
853 // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
854 Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
856 bbox = Geom::unify(bbox, temp_bbox_new);
857 }
859 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
860 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
861 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
862 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
863 * clones), in turn, call this function in their bbox methods. */
864 void
865 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
866 {
867 g_assert(item != NULL);
868 g_assert(SP_IS_ITEM(item));
869 g_assert(bbox != NULL);
871 if (clear) {
872 bbox->x0 = bbox->y0 = 1e18;
873 bbox->x1 = bbox->y1 = -1e18;
874 }
876 NRRect this_bbox;
877 this_bbox.x0 = this_bbox.y0 = 1e18;
878 this_bbox.x1 = this_bbox.y1 = -1e18;
880 // call the subclass method
881 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
882 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
883 }
885 // unless this is geometric bbox, crop the bbox by clip path, if any
886 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
887 NRRect b;
888 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
889 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
890 }
892 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
893 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
894 nr_rect_d_union (bbox, bbox, &this_bbox);
895 }
896 }
898 unsigned sp_item_pos_in_parent(SPItem *item)
899 {
900 g_assert(item != NULL);
901 g_assert(SP_IS_ITEM(item));
903 SPObject *parent = SP_OBJECT_PARENT(item);
904 g_assert(parent != NULL);
905 g_assert(SP_IS_OBJECT(parent));
907 SPObject *object = SP_OBJECT(item);
909 unsigned pos=0;
910 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
911 if ( iter == object ) {
912 return pos;
913 }
914 if (SP_IS_ITEM(iter)) {
915 pos++;
916 }
917 }
919 g_assert_not_reached();
920 return 0;
921 }
923 void
924 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
925 {
926 g_assert(item != NULL);
927 g_assert(SP_IS_ITEM(item));
928 g_assert(bbox != NULL);
930 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
931 }
933 Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
934 {
935 Geom::OptRect rect = Geom::OptRect();
936 sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
937 return rect;
938 }
940 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
941 {
942 /* This will only be called if the derived class doesn't override this.
943 * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
944 * We don't know what shape we could be dealing with here, so we'll just
945 * return the corners of the bounding box */
947 Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
949 if (bbox) {
950 Geom::Point p1, p2;
951 p1 = bbox->min();
952 p2 = bbox->max();
953 *p = p1;
954 *p = Geom::Point(p1[Geom::X], p2[Geom::Y]);
955 *p = p2;
956 *p = Geom::Point(p1[Geom::Y], p2[Geom::X]);
957 }
959 }
961 void sp_item_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
962 {
963 g_assert (item != NULL);
964 g_assert (SP_IS_ITEM(item));
966 // Get the snappoints of the item
967 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
968 if (item_class.snappoints) {
969 item_class.snappoints(item, p, snapprefs);
970 }
972 // Get the snappoints at the item's center
973 if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
974 *p = item->getCenter();
975 }
977 // Get the snappoints of clipping paths and mask, if any
978 std::list<SPObject const *> clips_and_masks;
980 clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
981 clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
983 for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
984 if (*o) {
985 // obj is a group object, the children are the actual clippers
986 for (SPObject *child = (*o)->children ; child ; child = child->next) {
987 if (SP_IS_ITEM(child)) {
988 std::vector<Geom::Point> p_clip_or_mask;
989 // Please note the recursive call here!
990 sp_item_snappoints(SP_ITEM(child), SnapPointsIter(p_clip_or_mask), snapprefs);
991 // Take into account the transformation of the item being clipped or masked
992 for (std::vector<Geom::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
993 // All snappoints are in desktop coordinates, but the item's transformation is
994 // in document coordinates. Hence the awkward construction below
995 *p = (*p_orig) * matrix_to_desktop (matrix_from_desktop (item->transform, item), item);
996 }
997 }
998 }
999 }
1000 }
1001 }
1003 void
1004 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
1005 {
1006 if (!item->isHidden()) {
1007 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
1008 if (!item->transform.isIdentity()
1009 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
1010 {
1011 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1012 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1013 sp_print_release(ctx);
1014 } else {
1015 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1016 }
1017 }
1018 }
1019 }
1021 static gchar *
1022 sp_item_private_description(SPItem */*item*/)
1023 {
1024 return g_strdup(_("Object"));
1025 }
1027 /**
1028 * Returns a string suitable for status bar, formatted in pango markup language.
1029 *
1030 * Must be freed by caller.
1031 */
1032 gchar *
1033 sp_item_description(SPItem *item)
1034 {
1035 g_assert(item != NULL);
1036 g_assert(SP_IS_ITEM(item));
1038 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1039 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1040 if (s && item->clip_ref->getObject()) {
1041 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1042 g_free (s);
1043 s = snew;
1044 }
1045 if (s && item->mask_ref->getObject()) {
1046 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1047 g_free (s);
1048 s = snew;
1049 }
1050 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
1051 gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1052 g_free (s);
1053 s = snew;
1054 }
1055 return s;
1056 }
1058 g_assert_not_reached();
1059 return NULL;
1060 }
1062 /**
1063 * Allocates unique integer keys.
1064 * \param numkeys Number of keys required.
1065 * \return First allocated key; hence if the returned key is n
1066 * you can use n, n + 1, ..., n + (numkeys - 1)
1067 */
1068 unsigned
1069 sp_item_display_key_new(unsigned numkeys)
1070 {
1071 static unsigned dkey = 0;
1073 dkey += numkeys;
1075 return dkey - numkeys;
1076 }
1078 NRArenaItem *
1079 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1080 {
1081 g_assert(item != NULL);
1082 g_assert(SP_IS_ITEM(item));
1083 g_assert(arena != NULL);
1084 g_assert(NR_IS_ARENA(arena));
1086 NRArenaItem *ai = NULL;
1087 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1088 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1089 }
1091 if (ai != NULL) {
1092 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1093 nr_arena_item_set_transform(ai, item->transform);
1094 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1095 nr_arena_item_set_visible(ai, !item->isHidden());
1096 nr_arena_item_set_sensitive(ai, item->sensitive);
1097 if (item->clip_ref->getObject()) {
1098 SPClipPath *cp = item->clip_ref->getObject();
1100 if (!item->display->arenaitem->key) {
1101 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1102 }
1103 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1105 // Show and set clip
1106 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1107 nr_arena_item_set_clip(ai, ac);
1108 nr_arena_item_unref(ac);
1110 // Update bbox, in case the clip uses bbox units
1111 NRRect bbox;
1112 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1113 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1114 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1115 }
1116 if (item->mask_ref->getObject()) {
1117 SPMask *mask = item->mask_ref->getObject();
1119 if (!item->display->arenaitem->key) {
1120 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1121 }
1122 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1124 // Show and set mask
1125 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1126 nr_arena_item_set_mask(ai, ac);
1127 nr_arena_item_unref(ac);
1129 // Update bbox, in case the mask uses bbox units
1130 NRRect bbox;
1131 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1132 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1133 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1134 }
1135 NR_ARENA_ITEM_SET_DATA(ai, item);
1136 Geom::OptRect item_bbox;
1137 sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1138 nr_arena_item_set_item_bbox(ai, item_bbox);
1139 }
1141 return ai;
1142 }
1144 void
1145 sp_item_invoke_hide(SPItem *item, unsigned key)
1146 {
1147 g_assert(item != NULL);
1148 g_assert(SP_IS_ITEM(item));
1150 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1151 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1152 }
1154 SPItemView *ref = NULL;
1155 SPItemView *v = item->display;
1156 while (v != NULL) {
1157 SPItemView *next = v->next;
1158 if (v->key == key) {
1159 if (item->clip_ref->getObject()) {
1160 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1161 nr_arena_item_set_clip(v->arenaitem, NULL);
1162 }
1163 if (item->mask_ref->getObject()) {
1164 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1165 nr_arena_item_set_mask(v->arenaitem, NULL);
1166 }
1167 if (!ref) {
1168 item->display = v->next;
1169 } else {
1170 ref->next = v->next;
1171 }
1172 nr_arena_item_unparent(v->arenaitem);
1173 nr_arena_item_unref(v->arenaitem);
1174 g_free(v);
1175 } else {
1176 ref = v;
1177 }
1178 v = next;
1179 }
1180 }
1182 // Adjusters
1184 void
1185 sp_item_adjust_pattern (SPItem *item, Geom::Matrix const &postmul, bool set)
1186 {
1187 SPStyle *style = SP_OBJECT_STYLE (item);
1189 if (style && (style->fill.isPaintserver())) {
1190 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1191 if (SP_IS_PATTERN (server)) {
1192 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1193 sp_pattern_transform_multiply (pattern, postmul, set);
1194 }
1195 }
1197 if (style && (style->stroke.isPaintserver())) {
1198 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1199 if (SP_IS_PATTERN (server)) {
1200 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1201 sp_pattern_transform_multiply (pattern, postmul, set);
1202 }
1203 }
1205 }
1207 void
1208 sp_item_adjust_gradient (SPItem *item, Geom::Matrix const &postmul, bool set)
1209 {
1210 SPStyle *style = SP_OBJECT_STYLE (item);
1212 if (style && (style->fill.isPaintserver())) {
1213 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1214 if (SP_IS_GRADIENT (server)) {
1216 /**
1217 * \note Bbox units for a gradient are generally a bad idea because
1218 * with them, you cannot preserve the relative position of the
1219 * object and its gradient after rotation or skew. So now we
1220 * convert them to userspace units which are easy to keep in sync
1221 * just by adding the object's transform to gradientTransform.
1222 * \todo FIXME: convert back to bbox units after transforming with
1223 * the item, so as to preserve the original units.
1224 */
1225 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1227 sp_gradient_transform_multiply (gradient, postmul, set);
1228 }
1229 }
1231 if (style && (style->stroke.isPaintserver())) {
1232 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1233 if (SP_IS_GRADIENT (server)) {
1234 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1235 sp_gradient_transform_multiply (gradient, postmul, set);
1236 }
1237 }
1238 }
1240 void
1241 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1242 {
1243 SPStyle *style = SP_OBJECT_STYLE (item);
1245 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1247 style->stroke_width.computed *= ex;
1248 style->stroke_width.set = TRUE;
1250 if (style->stroke_dash.n_dash != 0) {
1251 int i;
1252 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1253 style->stroke_dash.dash[i] *= ex;
1254 }
1255 style->stroke_dash.offset *= ex;
1256 }
1258 SP_OBJECT(item)->updateRepr();
1259 }
1260 }
1262 /**
1263 * Find out the inverse of previous transform of an item (from its repr)
1264 */
1265 Geom::Matrix
1266 sp_item_transform_repr (SPItem *item)
1267 {
1268 Geom::Matrix t_old(Geom::identity());
1269 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1270 if (t_attr) {
1271 Geom::Matrix t;
1272 if (sp_svg_transform_read(t_attr, &t)) {
1273 t_old = t;
1274 }
1275 }
1277 return t_old;
1278 }
1281 /**
1282 * Recursively scale stroke width in \a item and its children by \a expansion.
1283 */
1284 void
1285 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1286 {
1287 sp_item_adjust_stroke (item, expansion);
1289 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1290 if (item && SP_IS_USE(item))
1291 return;
1293 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1294 if (SP_IS_ITEM(o))
1295 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1296 }
1297 }
1299 /**
1300 * Recursively adjust rx and ry of rects.
1301 */
1302 void
1303 sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
1304 {
1305 if (SP_IS_RECT (item)) {
1306 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1307 }
1309 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1310 if (SP_IS_ITEM(o))
1311 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1312 }
1313 }
1315 /**
1316 * Recursively compensate pattern or gradient transform.
1317 */
1318 void
1319 sp_item_adjust_paint_recursive (SPItem *item, Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
1320 {
1321 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1322 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1323 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1324 Geom::Matrix t_item = sp_item_transform_repr (item);
1325 Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1327 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1328 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1329 // we must not touch it
1330 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1331 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1332 if (SP_IS_ITEM(o)) {
1333 // At the level of the transformed item, t_ancestors is identity;
1334 // below it, it is the accmmulated chain of transforms from this level to the top level
1335 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1336 }
1337 }
1338 }
1340 // We recursed into children first, and are now adjusting this object second;
1341 // this is so that adjustments in a tree are done from leaves up to the root,
1342 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1343 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1345 if (is_pattern)
1346 sp_item_adjust_pattern (item, paint_delta);
1347 else
1348 sp_item_adjust_gradient (item, paint_delta);
1350 }
1352 void
1353 sp_item_adjust_livepatheffect (SPItem *item, Geom::Matrix const &postmul, bool set)
1354 {
1355 if ( !SP_IS_LPE_ITEM(item) )
1356 return;
1358 SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1359 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1360 PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
1361 for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1362 {
1363 // If the path effect is used by 2 or more items, fork it
1364 // so that each object has its own independent copy of the effect
1365 LivePathEffectObject *lpeobj = (*it)->lpeobject;
1366 if (lpeobj) {
1367 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1368 if (new_lpeobj != lpeobj) {
1369 sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1370 }
1372 if (lpeobj->get_lpe()) {
1373 Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
1374 effect->transform_multiply(postmul, set);
1375 }
1376 }
1377 }
1378 }
1379 }
1381 /**
1382 * A temporary wrapper for the next function accepting the Geom::Matrix
1383 * instead of Geom::Matrix
1384 */
1385 void
1386 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const *transform, Geom::Matrix const *adv)
1387 {
1388 if (transform == NULL)
1389 sp_item_write_transform(item, repr, Geom::identity(), adv);
1390 else
1391 sp_item_write_transform(item, repr, *transform, adv);
1392 }
1394 /**
1395 * Set a new transform on an object.
1396 *
1397 * Compensate for stroke scaling and gradient/pattern fill transform, if
1398 * necessary. Call the object's set_transform method if transforms are
1399 * stored optimized. Send _transformed_signal. Invoke _write method so that
1400 * the repr is updated with the new transform.
1401 */
1402 void
1403 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
1404 {
1405 g_return_if_fail(item != NULL);
1406 g_return_if_fail(SP_IS_ITEM(item));
1407 g_return_if_fail(repr != NULL);
1409 // calculate the relative transform, if not given by the adv attribute
1410 Geom::Matrix advertized_transform;
1411 if (adv != NULL) {
1412 advertized_transform = *adv;
1413 } else {
1414 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1415 }
1417 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1418 if (compensate) {
1420 // recursively compensate for stroke scaling, depending on user preference
1421 if (!prefs->getBool("/options/transform/stroke", true)) {
1422 double const expansion = 1. / advertized_transform.descrim();
1423 sp_item_adjust_stroke_width_recursive(item, expansion);
1424 }
1426 // recursively compensate rx/ry of a rect if requested
1427 if (!prefs->getBool("/options/transform/rectcorners", true)) {
1428 sp_item_adjust_rects_recursive(item, advertized_transform);
1429 }
1431 // recursively compensate pattern fill if it's not to be transformed
1432 if (!prefs->getBool("/options/transform/pattern", true)) {
1433 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
1434 }
1435 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1436 /// recursively compensate gradient fill if it's not to be transformed
1437 if (!prefs->getBool("/options/transform/gradient", true)) {
1438 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
1439 } else {
1440 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1441 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1442 sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
1443 }
1445 } // endif(compensate)
1447 gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1448 Geom::Matrix transform_attr (transform);
1449 if ( // run the object's set_transform (i.e. embed transform) only if:
1450 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1451 !preserve && // user did not chose to preserve all transforms
1452 !item->clip_ref->getObject() && // the object does not have a clippath
1453 !item->mask_ref->getObject() && // the object does not have a mask
1454 !(!transform.isTranslation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1455 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1456 ) {
1457 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1458 }
1459 sp_item_set_item_transform(item, transform_attr);
1461 // Note: updateRepr comes before emitting the transformed signal since
1462 // it causes clone SPUse's copy of the original object to brought up to
1463 // date with the original. Otherwise, sp_use_bbox returns incorrect
1464 // values if called in code handling the transformed signal.
1465 SP_OBJECT(item)->updateRepr();
1467 // send the relative transform with a _transformed_signal
1468 item->_transformed_signal.emit(&advertized_transform, item);
1469 }
1471 gint
1472 sp_item_event(SPItem *item, SPEvent *event)
1473 {
1474 g_return_val_if_fail(item != NULL, FALSE);
1475 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1476 g_return_val_if_fail(event != NULL, FALSE);
1478 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1479 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1481 return FALSE;
1482 }
1484 /**
1485 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1486 * gradients, patterns as sp_item_write_transform does.
1487 */
1488 void
1489 sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
1490 {
1491 g_return_if_fail(item != NULL);
1492 g_return_if_fail(SP_IS_ITEM(item));
1494 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1495 item->transform = transform;
1496 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1497 transformation. It's apparently not used anywhere else. */
1498 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1499 sp_item_rm_unsatisfied_cns(*item);
1500 }
1501 }
1503 void
1504 sp_item_convert_item_to_guides(SPItem *item) {
1505 g_return_if_fail(item != NULL);
1506 g_return_if_fail(SP_IS_ITEM(item));
1508 /* Use derived method if present ... */
1509 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1510 (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1511 return;
1512 }
1514 /* .. otherwise simply place the guides around the item's bounding box */
1516 sp_item_convert_to_guides(item);
1517 }
1520 /**
1521 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1522 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1523 */
1524 Geom::Matrix
1525 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1526 Geom::Matrix ret(Geom::identity());
1527 g_return_val_if_fail(object != NULL, ret);
1529 /* stop at first non-renderable ancestor */
1530 while ( object != ancestor && SP_IS_ITEM(object) ) {
1531 if (SP_IS_ROOT(object)) {
1532 ret *= SP_ROOT(object)->c2p;
1533 }
1534 ret *= SP_ITEM(object)->transform;
1535 object = SP_OBJECT_PARENT(object);
1536 }
1537 return ret;
1538 }
1540 Geom::Matrix
1541 i2i_affine(SPObject const *src, SPObject const *dest) {
1542 g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1543 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1544 return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1545 }
1547 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1548 return i2i_affine(this, dest);
1549 }
1551 /**
1552 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1553 * \pre (item != NULL) and SP_IS_ITEM(item).
1554 */
1555 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1556 {
1557 return i2anc_affine(item, NULL);
1558 }
1560 /**
1561 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1562 * Used in path operations mostly.
1563 * \pre (item != NULL) and SP_IS_ITEM(item).
1564 */
1565 Geom::Matrix sp_item_i2root_affine(SPItem const *item)
1566 {
1567 g_assert(item != NULL);
1568 g_assert(SP_IS_ITEM(item));
1570 Geom::Matrix ret(Geom::identity());
1571 g_assert(ret.isIdentity());
1572 while ( NULL != SP_OBJECT_PARENT(item) ) {
1573 ret *= item->transform;
1574 item = SP_ITEM(SP_OBJECT_PARENT(item));
1575 }
1576 g_assert(SP_IS_ROOT(item));
1578 ret *= item->transform;
1580 return ret;
1581 }
1583 /* fixme: This is EVIL!!! */
1584 // fix this note: why/what evil? :)
1585 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1586 {
1587 g_assert(item != NULL);
1588 g_assert(SP_IS_ITEM(item));
1590 Geom::Matrix const ret( sp_item_i2doc_affine(item)
1591 * Geom::Scale(1, -1)
1592 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1593 return ret;
1594 }
1596 // same as i2d but with i2root instead of i2doc
1597 Geom::Matrix sp_item_i2r_affine(SPItem const *item)
1598 {
1599 g_assert(item != NULL);
1600 g_assert(SP_IS_ITEM(item));
1602 Geom::Matrix const ret( sp_item_i2root_affine(item)
1603 * Geom::Scale(1, -1)
1604 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1605 return ret;
1606 }
1608 /**
1609 * Converts a matrix \a m into the desktop coords of the \a item.
1610 * Will become a noop when we eliminate the coordinate flipping.
1611 */
1612 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1613 {
1614 Geom::Matrix const ret(m
1615 * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1616 * Geom::Scale(1, -1));
1617 return ret;
1618 }
1620 /**
1621 * Converts a matrix \a m from the desktop coords of the \a item.
1622 * Will become a noop when we eliminate the coordinate flipping.
1623 */
1624 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1625 {
1626 Geom::Matrix const ret(Geom::Scale(1, -1)
1627 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1628 * m);
1629 return ret;
1630 }
1632 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1633 {
1634 g_return_if_fail( item != NULL );
1635 g_return_if_fail( SP_IS_ITEM(item) );
1637 Geom::Matrix dt2p; /* desktop to item parent transform */
1638 if (SP_OBJECT_PARENT(item)) {
1639 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1640 } else {
1641 dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1642 * Geom::Scale(1, -1) );
1643 }
1645 Geom::Matrix const i2p( i2dt * dt2p );
1646 sp_item_set_item_transform(item, i2p);
1647 }
1650 Geom::Matrix
1651 sp_item_dt2i_affine(SPItem const *item)
1652 {
1653 /* fixme: Implement the right way (Lauris) */
1654 return sp_item_i2d_affine(item).inverse();
1655 }
1657 /* Item views */
1659 static SPItemView *
1660 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1661 {
1662 SPItemView *new_view;
1664 g_assert(item != NULL);
1665 g_assert(SP_IS_ITEM(item));
1666 g_assert(arenaitem != NULL);
1667 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1669 new_view = g_new(SPItemView, 1);
1671 new_view->next = list;
1672 new_view->flags = flags;
1673 new_view->key = key;
1674 new_view->arenaitem = arenaitem;
1676 return new_view;
1677 }
1679 static SPItemView *
1680 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1681 {
1682 if (view == list) {
1683 list = list->next;
1684 } else {
1685 SPItemView *prev;
1686 prev = list;
1687 while (prev->next != view) prev = prev->next;
1688 prev->next = view->next;
1689 }
1691 nr_arena_item_unref(view->arenaitem);
1692 g_free(view);
1694 return list;
1695 }
1697 /**
1698 * Return the arenaitem corresponding to the given item in the display
1699 * with the given key
1700 */
1701 NRArenaItem *
1702 sp_item_get_arenaitem(SPItem *item, unsigned key)
1703 {
1704 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1705 if ( iv->key == key ) {
1706 return iv->arenaitem;
1707 }
1708 }
1710 return NULL;
1711 }
1713 int
1714 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1715 {
1716 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1717 SP_OBJECT_REPR(second));
1718 }
1720 SPItem *
1721 sp_item_first_item_child (SPObject *obj)
1722 {
1723 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1724 if (SP_IS_ITEM (iter))
1725 return SP_ITEM (iter);
1726 }
1727 return NULL;
1728 }
1730 void
1731 sp_item_convert_to_guides(SPItem *item) {
1732 SPDesktop *dt = inkscape_active_desktop();
1733 SPNamedView *nv = sp_desktop_namedview(dt);
1734 (void)nv;
1736 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1737 int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1738 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1739 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1741 Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
1742 if (!bbox) {
1743 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1744 return;
1745 }
1747 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1749 Geom::Point A((*bbox).min());
1750 Geom::Point C((*bbox).max());
1751 Geom::Point B(A[Geom::X], C[Geom::Y]);
1752 Geom::Point D(C[Geom::X], A[Geom::Y]);
1754 pts.push_back(std::make_pair(A, B));
1755 pts.push_back(std::make_pair(B, C));
1756 pts.push_back(std::make_pair(C, D));
1757 pts.push_back(std::make_pair(D, A));
1759 sp_guide_pt_pairs_to_guides(dt, pts);
1760 }
1762 /*
1763 Local Variables:
1764 mode:c++
1765 c-file-style:"stroustrup"
1766 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1767 indent-tabs-mode:nil
1768 fill-column:99
1769 End:
1770 */
1771 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :