1 /** \file
2 * Base class for visual SVG elements
3 */
4 /*
5 * Authors:
6 * Lauris Kaplinski <lauris@kaplinski.com>
7 * bulia byak <buliabyak@users.sf.net>
8 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
9 *
10 * Copyright (C) 2001-2006 authors
11 * Copyright (C) 2001 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 /** \class SPItem
17 *
18 * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
19 * is a subclass of SPObject, with great deal of specific functionality.
20 */
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
27 #include "sp-item.h"
28 #include "svg/svg.h"
29 #include "print.h"
30 #include "display/nr-arena.h"
31 #include "display/nr-arena-item.h"
32 #include "attributes.h"
33 #include "document.h"
34 #include "uri.h"
35 #include "inkscape.h"
36 #include "desktop.h"
37 #include "desktop-handles.h"
39 #include "style.h"
40 #include <glibmm/i18n.h>
41 #include "sp-root.h"
42 #include "sp-clippath.h"
43 #include "sp-mask.h"
44 #include "sp-rect.h"
45 #include "sp-use.h"
46 #include "sp-text.h"
47 #include "sp-item-rm-unsatisfied-cns.h"
48 #include "sp-pattern.h"
49 #include "sp-switch.h"
50 #include "sp-guide-constraint.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->constraints) std::vector<SPGuideConstraint>();
182 new (&this->_transformed_signal) sigc::signal<void, Geom::Matrix const *, SPItem *>();
183 }
185 bool SPItem::isVisibleAndUnlocked() const {
186 return (!isHidden() && !isLocked());
187 }
189 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
190 return (!isHidden(display_key) && !isLocked());
191 }
193 bool SPItem::isLocked() const {
194 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
195 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
196 return true;
197 }
198 return false;
199 }
201 void SPItem::setLocked(bool locked) {
202 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
203 ( locked ? "1" : NULL ));
204 updateRepr();
205 }
207 bool SPItem::isHidden() const {
208 if (!isEvaluated())
209 return true;
210 return style->display.computed == SP_CSS_DISPLAY_NONE;
211 }
213 void SPItem::setHidden(bool hide) {
214 style->display.set = TRUE;
215 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
216 style->display.computed = style->display.value;
217 style->display.inherit = FALSE;
218 updateRepr();
219 }
221 bool SPItem::isHidden(unsigned display_key) const {
222 if (!isEvaluated())
223 return true;
224 for ( SPItemView *view(display) ; view ; view = view->next ) {
225 if ( view->key == display_key ) {
226 g_assert(view->arenaitem != NULL);
227 for ( NRArenaItem *arenaitem = view->arenaitem ;
228 arenaitem ; arenaitem = arenaitem->parent )
229 {
230 if (!arenaitem->visible) {
231 return true;
232 }
233 }
234 return false;
235 }
236 }
237 return true;
238 }
240 void SPItem::setEvaluated(bool evaluated) {
241 _is_evaluated = evaluated;
242 _evaluated_status = StatusSet;
243 }
245 void SPItem::resetEvaluated() {
246 if ( StatusCalculated == _evaluated_status ) {
247 _evaluated_status = StatusUnknown;
248 bool oldValue = _is_evaluated;
249 if ( oldValue != isEvaluated() ) {
250 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
251 }
252 } if ( StatusSet == _evaluated_status ) {
253 SPObject const *const parent = SP_OBJECT_PARENT(this);
254 if (SP_IS_SWITCH(parent)) {
255 SP_SWITCH(parent)->resetChildEvaluated();
256 }
257 }
258 }
260 bool SPItem::isEvaluated() const {
261 if ( StatusUnknown == _evaluated_status ) {
262 _is_evaluated = sp_item_evaluate(this);
263 _evaluated_status = StatusCalculated;
264 }
265 return _is_evaluated;
266 }
268 /**
269 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
270 * Corresponds to setExplicitlyHidden.
271 */
272 bool
273 SPItem::isExplicitlyHidden() const
274 {
275 return (this->style->display.set
276 && this->style->display.value == SP_CSS_DISPLAY_NONE);
277 }
279 /**
280 * Sets the display CSS property to `hidden' if \a val is true,
281 * otherwise makes it unset
282 */
283 void
284 SPItem::setExplicitlyHidden(bool const val) {
285 this->style->display.set = val;
286 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
287 this->style->display.computed = this->style->display.value;
288 this->updateRepr();
289 }
291 /**
292 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
293 */
294 void
295 SPItem::setCenter(Geom::Point object_centre) {
296 Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
297 if (bbox) {
298 transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
299 if (fabs(transform_center_x) < 1e-5) // rounding error
300 transform_center_x = 0;
301 transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
302 if (fabs(transform_center_y) < 1e-5) // rounding error
303 transform_center_y = 0;
304 }
305 }
307 void
308 SPItem::unsetCenter() {
309 transform_center_x = 0;
310 transform_center_y = 0;
311 }
313 bool SPItem::isCenterSet() {
314 return (transform_center_x != 0 || transform_center_y != 0);
315 }
317 Geom::Point SPItem::getCenter() const {
318 Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
319 if (bbox) {
320 return to_2geom(bbox->midpoint()) + Geom::Point (this->transform_center_x, this->transform_center_y);
321 } else {
322 return Geom::Point (0, 0); // something's wrong!
323 }
324 }
327 namespace {
329 bool is_item(SPObject const &object) {
330 return SP_IS_ITEM(&object);
331 }
333 }
335 void SPItem::raiseToTop() {
336 using Inkscape::Algorithms::find_last_if;
338 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
339 SP_OBJECT_NEXT(this), NULL, &is_item
340 );
341 if (topmost) {
342 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
343 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
344 }
345 }
347 void SPItem::raiseOne() {
348 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
349 SP_OBJECT_NEXT(this), NULL, &is_item
350 );
351 if (next_higher) {
352 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
353 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
354 sp_repr_parent(repr)->changeOrder(repr, ref);
355 }
356 }
358 void SPItem::lowerOne() {
359 using Inkscape::Util::MutableList;
360 using Inkscape::Util::reverse_list;
362 MutableList<SPObject &> next_lower=std::find_if(
363 reverse_list<SPObject::SiblingIterator>(
364 SP_OBJECT_PARENT(this)->firstChild(), this
365 ),
366 MutableList<SPObject &>(),
367 &is_item
368 );
369 if (next_lower) {
370 ++next_lower;
371 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
372 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
373 sp_repr_parent(repr)->changeOrder(repr, ref);
374 }
375 }
377 void SPItem::lowerToBottom() {
378 using Inkscape::Algorithms::find_last_if;
379 using Inkscape::Util::MutableList;
380 using Inkscape::Util::reverse_list;
382 MutableList<SPObject &> bottom=find_last_if(
383 reverse_list<SPObject::SiblingIterator>(
384 SP_OBJECT_PARENT(this)->firstChild(), this
385 ),
386 MutableList<SPObject &>(),
387 &is_item
388 );
389 if (bottom) {
390 ++bottom;
391 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
392 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
393 sp_repr_parent(repr)->changeOrder(repr, ref);
394 }
395 }
397 static void
398 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
399 {
400 sp_object_read_attr(object, "style");
401 sp_object_read_attr(object, "transform");
402 sp_object_read_attr(object, "clip-path");
403 sp_object_read_attr(object, "mask");
404 sp_object_read_attr(object, "sodipodi:insensitive");
405 sp_object_read_attr(object, "sodipodi:nonprintable");
406 sp_object_read_attr(object, "inkscape:transform-center-x");
407 sp_object_read_attr(object, "inkscape:transform-center-y");
408 sp_object_read_attr(object, "inkscape:connector-avoid");
410 if (((SPObjectClass *) (parent_class))->build) {
411 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
412 }
413 }
415 static void
416 sp_item_release(SPObject *object)
417 {
418 SPItem *item = (SPItem *) object;
420 item->_clip_ref_connection.disconnect();
421 item->_mask_ref_connection.disconnect();
423 // Note: do this here before the clip_ref is deleted, since calling
424 // sp_document_ensure_up_to_date for triggered routing may reference
425 // the deleted clip_ref.
426 if (item->avoidRef) {
427 delete item->avoidRef;
428 item->avoidRef = NULL;
429 }
431 if (item->clip_ref) {
432 item->clip_ref->detach();
433 delete item->clip_ref;
434 item->clip_ref = NULL;
435 }
437 if (item->mask_ref) {
438 item->mask_ref->detach();
439 delete item->mask_ref;
440 item->mask_ref = NULL;
441 }
443 if (((SPObjectClass *) (parent_class))->release) {
444 ((SPObjectClass *) parent_class)->release(object);
445 }
447 while (item->display) {
448 nr_arena_item_unparent(item->display->arenaitem);
449 item->display = sp_item_view_list_remove(item->display, item->display);
450 }
452 item->_transformed_signal.~signal();
453 }
455 static void
456 sp_item_set(SPObject *object, unsigned key, gchar const *value)
457 {
458 SPItem *item = (SPItem *) object;
460 switch (key) {
461 case SP_ATTR_TRANSFORM: {
462 Geom::Matrix t;
463 if (value && sp_svg_transform_read(value, &t)) {
464 sp_item_set_item_transform(item, t);
465 } else {
466 sp_item_set_item_transform(item, Geom::identity());
467 }
468 break;
469 }
470 case SP_PROP_CLIP_PATH: {
471 gchar *uri = extract_uri(value);
472 if (uri) {
473 try {
474 item->clip_ref->attach(Inkscape::URI(uri));
475 } catch (Inkscape::BadURIException &e) {
476 g_warning("%s", e.what());
477 item->clip_ref->detach();
478 }
479 g_free(uri);
480 } else {
481 item->clip_ref->detach();
482 }
484 break;
485 }
486 case SP_PROP_MASK: {
487 gchar *uri = extract_uri(value);
488 if (uri) {
489 try {
490 item->mask_ref->attach(Inkscape::URI(uri));
491 } catch (Inkscape::BadURIException &e) {
492 g_warning("%s", e.what());
493 item->mask_ref->detach();
494 }
495 g_free(uri);
496 } else {
497 item->mask_ref->detach();
498 }
500 break;
501 }
502 case SP_ATTR_SODIPODI_INSENSITIVE:
503 item->sensitive = !value;
504 for (SPItemView *v = item->display; v != NULL; v = v->next) {
505 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
506 }
507 break;
508 case SP_ATTR_CONNECTOR_AVOID:
509 item->avoidRef->setAvoid(value);
510 break;
511 case SP_ATTR_TRANSFORM_CENTER_X:
512 if (value) {
513 item->transform_center_x = g_strtod(value, NULL);
514 } else {
515 item->transform_center_x = 0;
516 }
517 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
518 break;
519 case SP_ATTR_TRANSFORM_CENTER_Y:
520 if (value) {
521 item->transform_center_y = g_strtod(value, NULL);
522 } else {
523 item->transform_center_y = 0;
524 }
525 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
526 break;
527 case SP_PROP_SYSTEM_LANGUAGE:
528 case SP_PROP_REQUIRED_FEATURES:
529 case SP_PROP_REQUIRED_EXTENSIONS:
530 {
531 item->resetEvaluated();
532 // pass to default handler
533 }
534 default:
535 if (SP_ATTRIBUTE_IS_CSS(key)) {
536 sp_style_read_from_object(object->style, object);
537 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
538 } else {
539 if (((SPObjectClass *) (parent_class))->set) {
540 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
541 }
542 }
543 break;
544 }
545 }
547 static void
548 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
549 {
550 if (old_clip) {
551 SPItemView *v;
552 /* Hide clippath */
553 for (v = item->display; v != NULL; v = v->next) {
554 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
555 nr_arena_item_set_clip(v->arenaitem, NULL);
556 }
557 }
558 if (SP_IS_CLIPPATH(clip)) {
559 NRRect bbox;
560 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
561 for (SPItemView *v = item->display; v != NULL; v = v->next) {
562 if (!v->arenaitem->key) {
563 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
564 }
565 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
566 NR_ARENA_ITEM_ARENA(v->arenaitem),
567 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
568 nr_arena_item_set_clip(v->arenaitem, ai);
569 nr_arena_item_unref(ai);
570 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
571 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
572 }
573 }
574 }
576 static void
577 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
578 {
579 if (old_mask) {
580 /* Hide mask */
581 for (SPItemView *v = item->display; v != NULL; v = v->next) {
582 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
583 nr_arena_item_set_mask(v->arenaitem, NULL);
584 }
585 }
586 if (SP_IS_MASK(mask)) {
587 NRRect bbox;
588 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
589 for (SPItemView *v = item->display; v != NULL; v = v->next) {
590 if (!v->arenaitem->key) {
591 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
592 }
593 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
594 NR_ARENA_ITEM_ARENA(v->arenaitem),
595 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
596 nr_arena_item_set_mask(v->arenaitem, ai);
597 nr_arena_item_unref(ai);
598 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
599 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
600 }
601 }
602 }
604 static void
605 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
606 {
607 SPItem *item = SP_ITEM(object);
609 if (((SPObjectClass *) (parent_class))->update)
610 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
612 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
613 if (flags & SP_OBJECT_MODIFIED_FLAG) {
614 for (SPItemView *v = item->display; v != NULL; v = v->next) {
615 nr_arena_item_set_transform(v->arenaitem, item->transform);
616 }
617 }
619 SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
620 SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
622 if ( clip_path || mask ) {
623 NRRect bbox;
624 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
625 if (clip_path) {
626 for (SPItemView *v = item->display; v != NULL; v = v->next) {
627 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
628 }
629 }
630 if (mask) {
631 for (SPItemView *v = item->display; v != NULL; v = v->next) {
632 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
633 }
634 }
635 }
637 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
638 for (SPItemView *v = item->display; v != NULL; v = v->next) {
639 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
640 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
641 }
642 }
643 }
645 /* Update bounding box data used by filters */
646 if (item->style->filter.set && item->display) {
647 Geom::OptRect item_bbox;
648 sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
650 SPItemView *itemview = item->display;
651 do {
652 if (itemview->arenaitem)
653 nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
654 } while ( (itemview = itemview->next) );
655 }
657 // Update libavoid with item geometry (for connector routing).
658 if (item->avoidRef)
659 item->avoidRef->handleSettingChange();
660 }
662 static Inkscape::XML::Node *
663 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
664 {
665 SPObject *child;
666 SPItem *item = SP_ITEM(object);
668 // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
669 // so we need to add any children from the underlying object to the new repr
670 if (flags & SP_OBJECT_WRITE_BUILD) {
671 Inkscape::XML::Node *crepr;
672 GSList *l;
673 l = NULL;
674 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
675 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
676 crepr = child->updateRepr(xml_doc, NULL, flags);
677 if (crepr) l = g_slist_prepend (l, crepr);
678 }
679 while (l) {
680 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
681 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
682 l = g_slist_remove (l, l->data);
683 }
684 } else {
685 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
686 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
687 child->updateRepr(flags);
688 }
689 }
691 gchar *c = sp_svg_transform_write(item->transform);
692 repr->setAttribute("transform", c);
693 g_free(c);
695 if (flags & SP_OBJECT_WRITE_EXT) {
696 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
697 if (item->transform_center_x != 0)
698 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
699 else
700 repr->setAttribute ("inkscape:transform-center-x", NULL);
701 if (item->transform_center_y != 0)
702 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
703 else
704 repr->setAttribute ("inkscape:transform-center-y", NULL);
705 }
707 if (item->clip_ref->getObject()) {
708 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
709 repr->setAttribute ("clip-path", value);
710 g_free ((void *) value);
711 }
712 if (item->mask_ref->getObject()) {
713 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
714 repr->setAttribute ("mask", value);
715 g_free ((void *) value);
716 }
718 if (((SPObjectClass *) (parent_class))->write) {
719 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
720 }
722 return repr;
723 }
725 /**
726 * \return There is no guarantee that the return value will contain a rectangle.
727 If this item does not have a boundingbox, it might well be empty.
728 */
729 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
730 SPItem::BBoxType type,
731 unsigned int /*dkey*/) const
732 {
733 Geom::OptRect r;
734 sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
735 return r;
736 }
738 void
739 sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
740 {
741 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
742 }
744 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
745 void
746 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
747 {
748 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
749 }
751 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
752 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
753 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
754 * clones), in turn, call this function in their bbox methods.
755 * \retval bbox Note that there is no guarantee that bbox will contain a rectangle when the
756 * function returns. If this item does not have a boundingbox, this might well be empty.
757 */
758 void
759 sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
760 {
761 g_assert(item != NULL);
762 g_assert(SP_IS_ITEM(item));
764 if (clear) {
765 bbox = Geom::OptRect();
766 }
768 // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
770 NRRect temp_bbox;
771 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
772 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
774 // call the subclass method
775 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
776 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
777 }
779 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
780 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
781 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
782 SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
783 if (filter && SP_IS_FILTER(filter)) {
784 // default filer area per the SVG spec:
785 double x = -0.1;
786 double y = -0.1;
787 double w = 1.2;
788 double h = 1.2;
790 // if area is explicitly set, override:
791 if (SP_FILTER(filter)->x._set)
792 x = SP_FILTER(filter)->x.computed;
793 if (SP_FILTER(filter)->y._set)
794 y = SP_FILTER(filter)->y.computed;
795 if (SP_FILTER(filter)->width._set)
796 w = SP_FILTER(filter)->width.computed;
797 if (SP_FILTER(filter)->height._set)
798 h = SP_FILTER(filter)->height.computed;
800 double dx0 = 0;
801 double dx1 = 0;
802 double dy0 = 0;
803 double dy1 = 0;
804 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
805 // if this is a single blur, use 2.4*radius
806 // which may be smaller than the default area;
807 // see set_filter_area for why it's 2.4
808 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
809 dx0 = -2.4 * r;
810 dx1 = 2.4 * r;
811 dy0 = -2.4 * r;
812 dy1 = 2.4 * r;
813 } else {
814 // otherwise, calculate expansion from relative to absolute units:
815 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
816 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
817 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
818 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
819 }
821 // transform the expansions by the item's transform:
822 Geom::Matrix i2d(sp_item_i2d_affine (item));
823 dx0 *= i2d.expansionX();
824 dx1 *= i2d.expansionX();
825 dy0 *= i2d.expansionY();
826 dy1 *= i2d.expansionY();
828 // expand the bbox
829 temp_bbox.x0 += dx0;
830 temp_bbox.x1 += dx1;
831 temp_bbox.y0 += dy0;
832 temp_bbox.y1 += dy1;
833 }
834 }
835 if (item->clip_ref->getObject()) {
836 NRRect b;
837 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
838 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
839 }
840 }
842 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
843 // Either the bbox hasn't been touched by the SPItemClass' bbox method
844 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
845 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
847 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
848 // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
849 // So union bbox with nothing = do nothing, just return
850 return;
851 }
853 // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
854 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
855 // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
856 Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
858 bbox = Geom::unify(bbox, temp_bbox_new);
859 }
861 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
862 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
863 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
864 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
865 * clones), in turn, call this function in their bbox methods. */
866 void
867 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
868 {
869 g_assert(item != NULL);
870 g_assert(SP_IS_ITEM(item));
871 g_assert(bbox != NULL);
873 if (clear) {
874 bbox->x0 = bbox->y0 = 1e18;
875 bbox->x1 = bbox->y1 = -1e18;
876 }
878 NRRect this_bbox;
879 this_bbox.x0 = this_bbox.y0 = 1e18;
880 this_bbox.x1 = this_bbox.y1 = -1e18;
882 // call the subclass method
883 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
884 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
885 }
887 // unless this is geometric bbox, crop the bbox by clip path, if any
888 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
889 NRRect b;
890 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
891 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
892 }
894 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
895 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
896 nr_rect_d_union (bbox, bbox, &this_bbox);
897 }
898 }
900 unsigned sp_item_pos_in_parent(SPItem *item)
901 {
902 g_assert(item != NULL);
903 g_assert(SP_IS_ITEM(item));
905 SPObject *parent = SP_OBJECT_PARENT(item);
906 g_assert(parent != NULL);
907 g_assert(SP_IS_OBJECT(parent));
909 SPObject *object = SP_OBJECT(item);
911 unsigned pos=0;
912 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
913 if ( iter == object ) {
914 return pos;
915 }
916 if (SP_IS_ITEM(iter)) {
917 pos++;
918 }
919 }
921 g_assert_not_reached();
922 return 0;
923 }
925 void
926 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
927 {
928 g_assert(item != NULL);
929 g_assert(SP_IS_ITEM(item));
930 g_assert(bbox != NULL);
932 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
933 }
935 Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
936 {
937 Geom::OptRect rect = Geom::OptRect();
938 sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
939 return rect;
940 }
942 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const */*snapprefs*/)
943 {
944 /* This will only be called if the derived class doesn't override this.
945 * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
946 * We don't know what shape we could be dealing with here, so we'll just
947 * return the corners of the bounding box */
949 Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
951 if (bbox) {
952 Geom::Point p1, p2;
953 p1 = bbox->min();
954 p2 = bbox->max();
955 *p = p1;
956 *p = Geom::Point(p1[Geom::X], p2[Geom::Y]);
957 *p = p2;
958 *p = Geom::Point(p1[Geom::Y], p2[Geom::X]);
959 }
961 }
963 void sp_item_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
964 {
965 g_assert (item != NULL);
966 g_assert (SP_IS_ITEM(item));
968 // Get the snappoints of the item
969 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
970 if (item_class.snappoints) {
971 item_class.snappoints(item, p, snapprefs);
972 }
974 // Get the snappoints at the item's center
975 if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
976 *p = item->getCenter();
977 }
979 // Get the snappoints of clipping paths and mask, if any
980 std::list<SPObject const *> clips_and_masks;
982 clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
983 clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
985 SPDesktop *desktop = inkscape_active_desktop();
986 for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
987 if (*o) {
988 // obj is a group object, the children are the actual clippers
989 for (SPObject *child = (*o)->children ; child ; child = child->next) {
990 if (SP_IS_ITEM(child)) {
991 std::vector<Geom::Point> p_clip_or_mask;
992 // Please note the recursive call here!
993 sp_item_snappoints(SP_ITEM(child), SnapPointsIter(p_clip_or_mask), snapprefs);
994 // Take into account the transformation of the item being clipped or masked
995 for (std::vector<Geom::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
996 // All snappoints are in desktop coordinates, but the item's transformation is
997 // in document coordinates. Hence the awkward construction below
998 *p = desktop->dt2doc(*p_orig) * sp_item_i2d_affine(item);
999 }
1000 }
1001 }
1002 }
1003 }
1004 }
1006 void
1007 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
1008 {
1009 if (!item->isHidden()) {
1010 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
1011 if (!item->transform.isIdentity()
1012 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
1013 {
1014 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1015 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1016 sp_print_release(ctx);
1017 } else {
1018 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1019 }
1020 }
1021 }
1022 }
1024 static gchar *
1025 sp_item_private_description(SPItem */*item*/)
1026 {
1027 return g_strdup(_("Object"));
1028 }
1030 /**
1031 * Returns a string suitable for status bar, formatted in pango markup language.
1032 *
1033 * Must be freed by caller.
1034 */
1035 gchar *
1036 sp_item_description(SPItem *item)
1037 {
1038 g_assert(item != NULL);
1039 g_assert(SP_IS_ITEM(item));
1041 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1042 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1043 if (s && item->clip_ref->getObject()) {
1044 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1045 g_free (s);
1046 s = snew;
1047 }
1048 if (s && item->mask_ref->getObject()) {
1049 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1050 g_free (s);
1051 s = snew;
1052 }
1053 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href && SP_OBJECT_STYLE(item)->filter.href->getObject()) {
1054 const gchar *label = SP_OBJECT_STYLE(item)->filter.href->getObject()->label();
1055 gchar *snew;
1056 if (label) {
1057 snew = g_strdup_printf (_("%s; <i>filtered (%s)</i>"), s, label);
1058 } else {
1059 snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1060 }
1061 g_free (s);
1062 s = snew;
1063 }
1064 return s;
1065 }
1067 g_assert_not_reached();
1068 return NULL;
1069 }
1071 /**
1072 * Allocates unique integer keys.
1073 * \param numkeys Number of keys required.
1074 * \return First allocated key; hence if the returned key is n
1075 * you can use n, n + 1, ..., n + (numkeys - 1)
1076 */
1077 unsigned
1078 sp_item_display_key_new(unsigned numkeys)
1079 {
1080 static unsigned dkey = 0;
1082 dkey += numkeys;
1084 return dkey - numkeys;
1085 }
1087 NRArenaItem *
1088 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1089 {
1090 g_assert(item != NULL);
1091 g_assert(SP_IS_ITEM(item));
1092 g_assert(arena != NULL);
1093 g_assert(NR_IS_ARENA(arena));
1095 NRArenaItem *ai = NULL;
1096 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1097 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1098 }
1100 if (ai != NULL) {
1101 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1102 nr_arena_item_set_transform(ai, item->transform);
1103 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1104 nr_arena_item_set_visible(ai, !item->isHidden());
1105 nr_arena_item_set_sensitive(ai, item->sensitive);
1106 if (item->clip_ref->getObject()) {
1107 SPClipPath *cp = item->clip_ref->getObject();
1109 if (!item->display->arenaitem->key) {
1110 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1111 }
1112 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1114 // Show and set clip
1115 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1116 nr_arena_item_set_clip(ai, ac);
1117 nr_arena_item_unref(ac);
1119 // Update bbox, in case the clip uses bbox units
1120 NRRect bbox;
1121 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1122 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1123 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1124 }
1125 if (item->mask_ref->getObject()) {
1126 SPMask *mask = item->mask_ref->getObject();
1128 if (!item->display->arenaitem->key) {
1129 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1130 }
1131 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1133 // Show and set mask
1134 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1135 nr_arena_item_set_mask(ai, ac);
1136 nr_arena_item_unref(ac);
1138 // Update bbox, in case the mask uses bbox units
1139 NRRect bbox;
1140 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
1141 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1142 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1143 }
1144 NR_ARENA_ITEM_SET_DATA(ai, item);
1145 Geom::OptRect item_bbox;
1146 sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1147 nr_arena_item_set_item_bbox(ai, item_bbox);
1148 }
1150 return ai;
1151 }
1153 void
1154 sp_item_invoke_hide(SPItem *item, unsigned key)
1155 {
1156 g_assert(item != NULL);
1157 g_assert(SP_IS_ITEM(item));
1159 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1160 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1161 }
1163 SPItemView *ref = NULL;
1164 SPItemView *v = item->display;
1165 while (v != NULL) {
1166 SPItemView *next = v->next;
1167 if (v->key == key) {
1168 if (item->clip_ref->getObject()) {
1169 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1170 nr_arena_item_set_clip(v->arenaitem, NULL);
1171 }
1172 if (item->mask_ref->getObject()) {
1173 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1174 nr_arena_item_set_mask(v->arenaitem, NULL);
1175 }
1176 if (!ref) {
1177 item->display = v->next;
1178 } else {
1179 ref->next = v->next;
1180 }
1181 nr_arena_item_unparent(v->arenaitem);
1182 nr_arena_item_unref(v->arenaitem);
1183 g_free(v);
1184 } else {
1185 ref = v;
1186 }
1187 v = next;
1188 }
1189 }
1191 // Adjusters
1193 void
1194 sp_item_adjust_pattern (SPItem *item, Geom::Matrix const &postmul, bool set)
1195 {
1196 SPStyle *style = SP_OBJECT_STYLE (item);
1198 if (style && (style->fill.isPaintserver())) {
1199 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1200 if (SP_IS_PATTERN (server)) {
1201 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1202 sp_pattern_transform_multiply (pattern, postmul, set);
1203 }
1204 }
1206 if (style && (style->stroke.isPaintserver())) {
1207 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1208 if (SP_IS_PATTERN (server)) {
1209 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1210 sp_pattern_transform_multiply (pattern, postmul, set);
1211 }
1212 }
1214 }
1216 void
1217 sp_item_adjust_gradient (SPItem *item, Geom::Matrix const &postmul, bool set)
1218 {
1219 SPStyle *style = SP_OBJECT_STYLE (item);
1221 if (style && (style->fill.isPaintserver())) {
1222 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1223 if (SP_IS_GRADIENT (server)) {
1225 /**
1226 * \note Bbox units for a gradient are generally a bad idea because
1227 * with them, you cannot preserve the relative position of the
1228 * object and its gradient after rotation or skew. So now we
1229 * convert them to userspace units which are easy to keep in sync
1230 * just by adding the object's transform to gradientTransform.
1231 * \todo FIXME: convert back to bbox units after transforming with
1232 * the item, so as to preserve the original units.
1233 */
1234 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1236 sp_gradient_transform_multiply (gradient, postmul, set);
1237 }
1238 }
1240 if (style && (style->stroke.isPaintserver())) {
1241 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1242 if (SP_IS_GRADIENT (server)) {
1243 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1244 sp_gradient_transform_multiply (gradient, postmul, set);
1245 }
1246 }
1247 }
1249 void
1250 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1251 {
1252 SPStyle *style = SP_OBJECT_STYLE (item);
1254 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1256 style->stroke_width.computed *= ex;
1257 style->stroke_width.set = TRUE;
1259 if (style->stroke_dash.n_dash != 0) {
1260 int i;
1261 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1262 style->stroke_dash.dash[i] *= ex;
1263 }
1264 style->stroke_dash.offset *= ex;
1265 }
1267 SP_OBJECT(item)->updateRepr();
1268 }
1269 }
1271 /**
1272 * Find out the inverse of previous transform of an item (from its repr)
1273 */
1274 Geom::Matrix
1275 sp_item_transform_repr (SPItem *item)
1276 {
1277 Geom::Matrix t_old(Geom::identity());
1278 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1279 if (t_attr) {
1280 Geom::Matrix t;
1281 if (sp_svg_transform_read(t_attr, &t)) {
1282 t_old = t;
1283 }
1284 }
1286 return t_old;
1287 }
1290 /**
1291 * Recursively scale stroke width in \a item and its children by \a expansion.
1292 */
1293 void
1294 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1295 {
1296 sp_item_adjust_stroke (item, expansion);
1298 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1299 if (item && SP_IS_USE(item))
1300 return;
1302 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1303 if (SP_IS_ITEM(o))
1304 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1305 }
1306 }
1308 /**
1309 * Recursively adjust rx and ry of rects.
1310 */
1311 void
1312 sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
1313 {
1314 if (SP_IS_RECT (item)) {
1315 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1316 }
1318 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1319 if (SP_IS_ITEM(o))
1320 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1321 }
1322 }
1324 /**
1325 * Recursively compensate pattern or gradient transform.
1326 */
1327 void
1328 sp_item_adjust_paint_recursive (SPItem *item, Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
1329 {
1330 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1331 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1332 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1333 Geom::Matrix t_item = sp_item_transform_repr (item);
1334 Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1336 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1337 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1338 // we must not touch it
1339 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1340 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1341 if (SP_IS_ITEM(o)) {
1342 // At the level of the transformed item, t_ancestors is identity;
1343 // below it, it is the accmmulated chain of transforms from this level to the top level
1344 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1345 }
1346 }
1347 }
1349 // We recursed into children first, and are now adjusting this object second;
1350 // this is so that adjustments in a tree are done from leaves up to the root,
1351 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1352 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1354 if (is_pattern)
1355 sp_item_adjust_pattern (item, paint_delta);
1356 else
1357 sp_item_adjust_gradient (item, paint_delta);
1359 }
1361 void
1362 sp_item_adjust_livepatheffect (SPItem *item, Geom::Matrix const &postmul, bool set)
1363 {
1364 if ( !SP_IS_LPE_ITEM(item) )
1365 return;
1367 SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1368 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1369 PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
1370 for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1371 {
1372 // If the path effect is used by 2 or more items, fork it
1373 // so that each object has its own independent copy of the effect
1374 LivePathEffectObject *lpeobj = (*it)->lpeobject;
1375 if (lpeobj) {
1376 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1377 if (new_lpeobj != lpeobj) {
1378 sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1379 }
1381 if (lpeobj->get_lpe()) {
1382 Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
1383 effect->transform_multiply(postmul, set);
1384 }
1385 }
1386 }
1387 }
1388 }
1390 /**
1391 * Set a new transform on an object.
1392 *
1393 * Compensate for stroke scaling and gradient/pattern fill transform, if
1394 * necessary. Call the object's set_transform method if transforms are
1395 * stored optimized. Send _transformed_signal. Invoke _write method so that
1396 * the repr is updated with the new transform.
1397 */
1398 void
1399 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
1400 {
1401 g_return_if_fail(item != NULL);
1402 g_return_if_fail(SP_IS_ITEM(item));
1403 g_return_if_fail(repr != NULL);
1405 // calculate the relative transform, if not given by the adv attribute
1406 Geom::Matrix advertized_transform;
1407 if (adv != NULL) {
1408 advertized_transform = *adv;
1409 } else {
1410 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1411 }
1413 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1414 if (compensate) {
1416 // recursively compensate for stroke scaling, depending on user preference
1417 if (!prefs->getBool("/options/transform/stroke", true)) {
1418 double const expansion = 1. / advertized_transform.descrim();
1419 sp_item_adjust_stroke_width_recursive(item, expansion);
1420 }
1422 // recursively compensate rx/ry of a rect if requested
1423 if (!prefs->getBool("/options/transform/rectcorners", true)) {
1424 sp_item_adjust_rects_recursive(item, advertized_transform);
1425 }
1427 // recursively compensate pattern fill if it's not to be transformed
1428 if (!prefs->getBool("/options/transform/pattern", true)) {
1429 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
1430 }
1431 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1432 /// recursively compensate gradient fill if it's not to be transformed
1433 if (!prefs->getBool("/options/transform/gradient", true)) {
1434 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
1435 } else {
1436 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1437 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1438 sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
1439 }
1441 } // endif(compensate)
1443 gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1444 Geom::Matrix transform_attr (transform);
1445 if ( // run the object's set_transform (i.e. embed transform) only if:
1446 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1447 !preserve && // user did not chose to preserve all transforms
1448 !item->clip_ref->getObject() && // the object does not have a clippath
1449 !item->mask_ref->getObject() && // the object does not have a mask
1450 !(!transform.isTranslation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1451 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1452 ) {
1453 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1454 }
1455 sp_item_set_item_transform(item, transform_attr);
1457 // Note: updateRepr comes before emitting the transformed signal since
1458 // it causes clone SPUse's copy of the original object to brought up to
1459 // date with the original. Otherwise, sp_use_bbox returns incorrect
1460 // values if called in code handling the transformed signal.
1461 SP_OBJECT(item)->updateRepr();
1463 // send the relative transform with a _transformed_signal
1464 item->_transformed_signal.emit(&advertized_transform, item);
1465 }
1467 gint
1468 sp_item_event(SPItem *item, SPEvent *event)
1469 {
1470 g_return_val_if_fail(item != NULL, FALSE);
1471 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1472 g_return_val_if_fail(event != NULL, FALSE);
1474 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1475 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1477 return FALSE;
1478 }
1480 /**
1481 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1482 * gradients, patterns as sp_item_write_transform does.
1483 */
1484 void
1485 sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
1486 {
1487 g_return_if_fail(item != NULL);
1488 g_return_if_fail(SP_IS_ITEM(item));
1490 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1491 item->transform = transform;
1492 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1493 transformation. It's apparently not used anywhere else. */
1494 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1495 sp_item_rm_unsatisfied_cns(*item);
1496 }
1497 }
1499 void
1500 sp_item_convert_item_to_guides(SPItem *item) {
1501 g_return_if_fail(item != NULL);
1502 g_return_if_fail(SP_IS_ITEM(item));
1504 /* Use derived method if present ... */
1505 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1506 (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1507 return;
1508 }
1510 /* .. otherwise simply place the guides around the item's bounding box */
1512 sp_item_convert_to_guides(item);
1513 }
1516 /**
1517 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1518 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1519 */
1520 Geom::Matrix
1521 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1522 Geom::Matrix ret(Geom::identity());
1523 g_return_val_if_fail(object != NULL, ret);
1525 /* stop at first non-renderable ancestor */
1526 while ( object != ancestor && SP_IS_ITEM(object) ) {
1527 if (SP_IS_ROOT(object)) {
1528 ret *= SP_ROOT(object)->c2p;
1529 } else {
1530 ret *= SP_ITEM(object)->transform;
1531 }
1532 object = SP_OBJECT_PARENT(object);
1533 }
1534 return ret;
1535 }
1537 Geom::Matrix
1538 i2i_affine(SPObject const *src, SPObject const *dest) {
1539 g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1540 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1541 return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1542 }
1544 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1545 return i2i_affine(this, dest);
1546 }
1548 /**
1549 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1550 * \pre (item != NULL) and SP_IS_ITEM(item).
1551 */
1552 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1553 {
1554 return i2anc_affine(item, NULL);
1555 }
1557 /**
1558 * Returns the transformation from item to desktop coords
1559 */
1560 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1561 {
1562 g_assert(item != NULL);
1563 g_assert(SP_IS_ITEM(item));
1565 Geom::Matrix const ret( sp_item_i2doc_affine(item)
1566 * Geom::Scale(1, -1)
1567 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1568 return ret;
1569 }
1571 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1572 {
1573 g_return_if_fail( item != NULL );
1574 g_return_if_fail( SP_IS_ITEM(item) );
1576 Geom::Matrix dt2p; /* desktop to item parent transform */
1577 if (SP_OBJECT_PARENT(item)) {
1578 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1579 } else {
1580 dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1581 * Geom::Scale(1, -1) );
1582 }
1584 Geom::Matrix const i2p( i2dt * dt2p );
1585 sp_item_set_item_transform(item, i2p);
1586 }
1589 /**
1590 * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
1591 */
1592 Geom::Matrix
1593 sp_item_dt2i_affine(SPItem const *item)
1594 {
1595 /* fixme: Implement the right way (Lauris) */
1596 return sp_item_i2d_affine(item).inverse();
1597 }
1599 /* Item views */
1601 static SPItemView *
1602 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1603 {
1604 SPItemView *new_view;
1606 g_assert(item != NULL);
1607 g_assert(SP_IS_ITEM(item));
1608 g_assert(arenaitem != NULL);
1609 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1611 new_view = g_new(SPItemView, 1);
1613 new_view->next = list;
1614 new_view->flags = flags;
1615 new_view->key = key;
1616 new_view->arenaitem = arenaitem;
1618 return new_view;
1619 }
1621 static SPItemView *
1622 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1623 {
1624 if (view == list) {
1625 list = list->next;
1626 } else {
1627 SPItemView *prev;
1628 prev = list;
1629 while (prev->next != view) prev = prev->next;
1630 prev->next = view->next;
1631 }
1633 nr_arena_item_unref(view->arenaitem);
1634 g_free(view);
1636 return list;
1637 }
1639 /**
1640 * Return the arenaitem corresponding to the given item in the display
1641 * with the given key
1642 */
1643 NRArenaItem *
1644 sp_item_get_arenaitem(SPItem *item, unsigned key)
1645 {
1646 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1647 if ( iv->key == key ) {
1648 return iv->arenaitem;
1649 }
1650 }
1652 return NULL;
1653 }
1655 int
1656 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1657 {
1658 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1659 SP_OBJECT_REPR(second));
1660 }
1662 SPItem *
1663 sp_item_first_item_child (SPObject *obj)
1664 {
1665 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1666 if (SP_IS_ITEM (iter))
1667 return SP_ITEM (iter);
1668 }
1669 return NULL;
1670 }
1672 void
1673 sp_item_convert_to_guides(SPItem *item) {
1674 SPDesktop *dt = inkscape_active_desktop();
1675 SPNamedView *nv = sp_desktop_namedview(dt);
1676 (void)nv;
1678 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1679 int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1680 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1681 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1683 Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
1684 if (!bbox) {
1685 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1686 return;
1687 }
1689 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1691 Geom::Point A((*bbox).min());
1692 Geom::Point C((*bbox).max());
1693 Geom::Point B(A[Geom::X], C[Geom::Y]);
1694 Geom::Point D(C[Geom::X], A[Geom::Y]);
1696 pts.push_back(std::make_pair(A, B));
1697 pts.push_back(std::make_pair(B, C));
1698 pts.push_back(std::make_pair(C, D));
1699 pts.push_back(std::make_pair(D, A));
1701 sp_guide_pt_pairs_to_guides(dt, pts);
1702 }
1704 /*
1705 Local Variables:
1706 mode:c++
1707 c-file-style:"stroustrup"
1708 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1709 indent-tabs-mode:nil
1710 fill-column:99
1711 End:
1712 */
1713 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :