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 "gradient-chemistry.h"
51 #include "preferences.h"
52 #include "conn-avoid-ref.h"
53 #include "conditions.h"
54 #include "sp-filter-reference.h"
55 #include "filter-chemistry.h"
56 #include "sp-guide.h"
57 #include "sp-title.h"
58 #include "sp-desc.h"
60 #include "libnr/nr-matrix-fns.h"
61 #include "libnr/nr-matrix-scale-ops.h"
62 #include "libnr/nr-matrix-translate-ops.h"
63 #include "libnr/nr-scale-translate-ops.h"
64 #include "libnr/nr-translate-scale-ops.h"
65 #include "libnr/nr-convert2geom.h"
66 #include "algorithms/find-last-if.h"
67 #include "util/reverse-list.h"
68 #include <2geom/rect.h>
69 #include <2geom/matrix.h>
70 #include <2geom/transforms.h>
72 #include "xml/repr.h"
73 #include "extract-uri.h"
74 #include "helper/geom.h"
76 #include "live_effects/lpeobject.h"
77 #include "live_effects/effect.h"
78 #include "live_effects/lpeobject-reference.h"
80 #define noSP_ITEM_DEBUG_IDLE
82 static void sp_item_class_init(SPItemClass *klass);
83 static void sp_item_init(SPItem *item);
85 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
86 static void sp_item_release(SPObject *object);
87 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
88 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
89 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
91 static gchar *sp_item_private_description(SPItem *item);
92 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
94 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
95 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
97 static SPObjectClass *parent_class;
99 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
100 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
102 /**
103 * Registers SPItem class and returns its type number.
104 */
105 GType
106 sp_item_get_type(void)
107 {
108 static GType type = 0;
109 if (!type) {
110 GTypeInfo info = {
111 sizeof(SPItemClass),
112 NULL, NULL,
113 (GClassInitFunc) sp_item_class_init,
114 NULL, NULL,
115 sizeof(SPItem),
116 16,
117 (GInstanceInitFunc) sp_item_init,
118 NULL, /* value_table */
119 };
120 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
121 }
122 return type;
123 }
125 /**
126 * SPItem vtable initialization.
127 */
128 static void
129 sp_item_class_init(SPItemClass *klass)
130 {
131 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
133 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
135 sp_object_class->build = sp_item_build;
136 sp_object_class->release = sp_item_release;
137 sp_object_class->set = sp_item_set;
138 sp_object_class->update = sp_item_update;
139 sp_object_class->write = sp_item_write;
141 klass->description = sp_item_private_description;
142 klass->snappoints = sp_item_private_snappoints;
143 }
145 /**
146 * Callback for SPItem object initialization.
147 */
148 static void
149 sp_item_init(SPItem *item)
150 {
151 item->init();
152 }
154 void SPItem::init() {
155 this->sensitive = TRUE;
157 this->transform_center_x = 0;
158 this->transform_center_y = 0;
160 this->_is_evaluated = true;
161 this->_evaluated_status = StatusUnknown;
163 this->transform = Geom::identity();
165 this->display = NULL;
167 this->clip_ref = new SPClipPathReference(this);
168 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
169 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
170 _clip_ref_connection = cs1.connect(sl1);
172 this->mask_ref = new SPMaskReference(this);
173 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
174 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
175 _mask_ref_connection = cs2.connect(sl2);
177 this->avoidRef = new SPAvoidRef(this);
179 new (&this->_transformed_signal) sigc::signal<void, Geom::Matrix const *, SPItem *>();
180 }
182 bool SPItem::isVisibleAndUnlocked() const {
183 return (!isHidden() && !isLocked());
184 }
186 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
187 return (!isHidden(display_key) && !isLocked());
188 }
190 bool SPItem::isLocked() const {
191 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
192 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
193 return true;
194 }
195 return false;
196 }
198 void SPItem::setLocked(bool locked) {
199 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
200 ( locked ? "1" : NULL ));
201 updateRepr();
202 }
204 bool SPItem::isHidden() const {
205 if (!isEvaluated())
206 return true;
207 return style->display.computed == SP_CSS_DISPLAY_NONE;
208 }
210 void SPItem::setHidden(bool hide) {
211 style->display.set = TRUE;
212 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
213 style->display.computed = style->display.value;
214 style->display.inherit = FALSE;
215 updateRepr();
216 }
218 bool SPItem::isHidden(unsigned display_key) const {
219 if (!isEvaluated())
220 return true;
221 for ( SPItemView *view(display) ; view ; view = view->next ) {
222 if ( view->key == display_key ) {
223 g_assert(view->arenaitem != NULL);
224 for ( NRArenaItem *arenaitem = view->arenaitem ;
225 arenaitem ; arenaitem = arenaitem->parent )
226 {
227 if (!arenaitem->visible) {
228 return true;
229 }
230 }
231 return false;
232 }
233 }
234 return true;
235 }
237 void SPItem::setEvaluated(bool evaluated) {
238 _is_evaluated = evaluated;
239 _evaluated_status = StatusSet;
240 }
242 void SPItem::resetEvaluated() {
243 if ( StatusCalculated == _evaluated_status ) {
244 _evaluated_status = StatusUnknown;
245 bool oldValue = _is_evaluated;
246 if ( oldValue != isEvaluated() ) {
247 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
248 }
249 } if ( StatusSet == _evaluated_status ) {
250 SPObject const *const parent = SP_OBJECT_PARENT(this);
251 if (SP_IS_SWITCH(parent)) {
252 SP_SWITCH(parent)->resetChildEvaluated();
253 }
254 }
255 }
257 bool SPItem::isEvaluated() const {
258 if ( StatusUnknown == _evaluated_status ) {
259 _is_evaluated = sp_item_evaluate(this);
260 _evaluated_status = StatusCalculated;
261 }
262 return _is_evaluated;
263 }
265 /**
266 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
267 * Corresponds to setExplicitlyHidden.
268 */
269 bool
270 SPItem::isExplicitlyHidden() const
271 {
272 return (this->style->display.set
273 && this->style->display.value == SP_CSS_DISPLAY_NONE);
274 }
276 /**
277 * Sets the display CSS property to `hidden' if \a val is true,
278 * otherwise makes it unset
279 */
280 void
281 SPItem::setExplicitlyHidden(bool const val) {
282 this->style->display.set = val;
283 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
284 this->style->display.computed = this->style->display.value;
285 this->updateRepr();
286 }
288 /**
289 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
290 */
291 void
292 SPItem::setCenter(Geom::Point object_centre) {
293 Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
294 if (bbox) {
295 transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
296 if (fabs(transform_center_x) < 1e-5) // rounding error
297 transform_center_x = 0;
298 transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
299 if (fabs(transform_center_y) < 1e-5) // rounding error
300 transform_center_y = 0;
301 }
302 }
304 void
305 SPItem::unsetCenter() {
306 transform_center_x = 0;
307 transform_center_y = 0;
308 }
310 bool SPItem::isCenterSet() {
311 return (transform_center_x != 0 || transform_center_y != 0);
312 }
314 Geom::Point SPItem::getCenter() const {
315 Geom::OptRect bbox = getBounds(sp_item_i2d_affine(this));
316 if (bbox) {
317 return to_2geom(bbox->midpoint()) + Geom::Point (this->transform_center_x, this->transform_center_y);
318 } else {
319 return Geom::Point (0, 0); // something's wrong!
320 }
321 }
324 namespace {
326 bool is_item(SPObject const &object) {
327 return SP_IS_ITEM(&object);
328 }
330 }
332 void SPItem::raiseToTop() {
333 using Inkscape::Algorithms::find_last_if;
335 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
336 SP_OBJECT_NEXT(this), NULL, &is_item
337 );
338 if (topmost) {
339 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
340 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
341 }
342 }
344 void SPItem::raiseOne() {
345 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
346 SP_OBJECT_NEXT(this), NULL, &is_item
347 );
348 if (next_higher) {
349 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
350 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
351 sp_repr_parent(repr)->changeOrder(repr, ref);
352 }
353 }
355 void SPItem::lowerOne() {
356 using Inkscape::Util::MutableList;
357 using Inkscape::Util::reverse_list;
359 MutableList<SPObject &> next_lower=std::find_if(
360 reverse_list<SPObject::SiblingIterator>(
361 SP_OBJECT_PARENT(this)->firstChild(), this
362 ),
363 MutableList<SPObject &>(),
364 &is_item
365 );
366 if (next_lower) {
367 ++next_lower;
368 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
369 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
370 sp_repr_parent(repr)->changeOrder(repr, ref);
371 }
372 }
374 void SPItem::lowerToBottom() {
375 using Inkscape::Algorithms::find_last_if;
376 using Inkscape::Util::MutableList;
377 using Inkscape::Util::reverse_list;
379 MutableList<SPObject &> bottom=find_last_if(
380 reverse_list<SPObject::SiblingIterator>(
381 SP_OBJECT_PARENT(this)->firstChild(), this
382 ),
383 MutableList<SPObject &>(),
384 &is_item
385 );
386 if (bottom) {
387 ++bottom;
388 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
389 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
390 sp_repr_parent(repr)->changeOrder(repr, ref);
391 }
392 }
394 static void
395 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
396 {
397 sp_object_read_attr(object, "style");
398 sp_object_read_attr(object, "transform");
399 sp_object_read_attr(object, "clip-path");
400 sp_object_read_attr(object, "mask");
401 sp_object_read_attr(object, "sodipodi:insensitive");
402 sp_object_read_attr(object, "sodipodi:nonprintable");
403 sp_object_read_attr(object, "inkscape:transform-center-x");
404 sp_object_read_attr(object, "inkscape:transform-center-y");
405 sp_object_read_attr(object, "inkscape:connector-avoid");
407 if (((SPObjectClass *) (parent_class))->build) {
408 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
409 }
410 }
412 static void
413 sp_item_release(SPObject *object)
414 {
415 SPItem *item = (SPItem *) object;
417 item->_clip_ref_connection.disconnect();
418 item->_mask_ref_connection.disconnect();
420 // Note: do this here before the clip_ref is deleted, since calling
421 // sp_document_ensure_up_to_date for triggered routing may reference
422 // the deleted clip_ref.
423 if (item->avoidRef) {
424 delete item->avoidRef;
425 item->avoidRef = NULL;
426 }
428 if (item->clip_ref) {
429 item->clip_ref->detach();
430 delete item->clip_ref;
431 item->clip_ref = NULL;
432 }
434 if (item->mask_ref) {
435 item->mask_ref->detach();
436 delete item->mask_ref;
437 item->mask_ref = NULL;
438 }
440 if (((SPObjectClass *) (parent_class))->release) {
441 ((SPObjectClass *) parent_class)->release(object);
442 }
444 while (item->display) {
445 nr_arena_item_unparent(item->display->arenaitem);
446 item->display = sp_item_view_list_remove(item->display, item->display);
447 }
449 item->_transformed_signal.~signal();
450 }
452 static void
453 sp_item_set(SPObject *object, unsigned key, gchar const *value)
454 {
455 SPItem *item = (SPItem *) object;
457 switch (key) {
458 case SP_ATTR_TRANSFORM: {
459 Geom::Matrix t;
460 if (value && sp_svg_transform_read(value, &t)) {
461 sp_item_set_item_transform(item, t);
462 } else {
463 sp_item_set_item_transform(item, Geom::identity());
464 }
465 break;
466 }
467 case SP_PROP_CLIP_PATH: {
468 gchar *uri = extract_uri(value);
469 if (uri) {
470 try {
471 item->clip_ref->attach(Inkscape::URI(uri));
472 } catch (Inkscape::BadURIException &e) {
473 g_warning("%s", e.what());
474 item->clip_ref->detach();
475 }
476 g_free(uri);
477 } else {
478 item->clip_ref->detach();
479 }
481 break;
482 }
483 case SP_PROP_MASK: {
484 gchar *uri = extract_uri(value);
485 if (uri) {
486 try {
487 item->mask_ref->attach(Inkscape::URI(uri));
488 } catch (Inkscape::BadURIException &e) {
489 g_warning("%s", e.what());
490 item->mask_ref->detach();
491 }
492 g_free(uri);
493 } else {
494 item->mask_ref->detach();
495 }
497 break;
498 }
499 case SP_ATTR_SODIPODI_INSENSITIVE:
500 item->sensitive = !value;
501 for (SPItemView *v = item->display; v != NULL; v = v->next) {
502 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
503 }
504 break;
505 case SP_ATTR_CONNECTOR_AVOID:
506 item->avoidRef->setAvoid(value);
507 break;
508 case SP_ATTR_TRANSFORM_CENTER_X:
509 if (value) {
510 item->transform_center_x = g_strtod(value, NULL);
511 } else {
512 item->transform_center_x = 0;
513 }
514 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
515 break;
516 case SP_ATTR_TRANSFORM_CENTER_Y:
517 if (value) {
518 item->transform_center_y = g_strtod(value, NULL);
519 } else {
520 item->transform_center_y = 0;
521 }
522 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
523 break;
524 case SP_PROP_SYSTEM_LANGUAGE:
525 case SP_PROP_REQUIRED_FEATURES:
526 case SP_PROP_REQUIRED_EXTENSIONS:
527 {
528 item->resetEvaluated();
529 // pass to default handler
530 }
531 default:
532 if (SP_ATTRIBUTE_IS_CSS(key)) {
533 sp_style_read_from_object(object->style, object);
534 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
535 } else {
536 if (((SPObjectClass *) (parent_class))->set) {
537 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
538 }
539 }
540 break;
541 }
542 }
544 static void
545 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
546 {
547 if (old_clip) {
548 SPItemView *v;
549 /* Hide clippath */
550 for (v = item->display; v != NULL; v = v->next) {
551 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
552 nr_arena_item_set_clip(v->arenaitem, NULL);
553 }
554 }
555 if (SP_IS_CLIPPATH(clip)) {
556 NRRect bbox;
557 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
558 for (SPItemView *v = item->display; v != NULL; v = v->next) {
559 if (!v->arenaitem->key) {
560 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
561 }
562 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
563 NR_ARENA_ITEM_ARENA(v->arenaitem),
564 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
565 nr_arena_item_set_clip(v->arenaitem, ai);
566 nr_arena_item_unref(ai);
567 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
568 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
569 }
570 }
571 }
573 static void
574 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
575 {
576 if (old_mask) {
577 /* Hide mask */
578 for (SPItemView *v = item->display; v != NULL; v = v->next) {
579 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
580 nr_arena_item_set_mask(v->arenaitem, NULL);
581 }
582 }
583 if (SP_IS_MASK(mask)) {
584 NRRect bbox;
585 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
586 for (SPItemView *v = item->display; v != NULL; v = v->next) {
587 if (!v->arenaitem->key) {
588 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
589 }
590 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
591 NR_ARENA_ITEM_ARENA(v->arenaitem),
592 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
593 nr_arena_item_set_mask(v->arenaitem, ai);
594 nr_arena_item_unref(ai);
595 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
596 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
597 }
598 }
599 }
601 static void
602 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
603 {
604 SPItem *item = SP_ITEM(object);
606 if (((SPObjectClass *) (parent_class))->update)
607 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
609 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
610 if (flags & SP_OBJECT_MODIFIED_FLAG) {
611 for (SPItemView *v = item->display; v != NULL; v = v->next) {
612 nr_arena_item_set_transform(v->arenaitem, item->transform);
613 }
614 }
616 SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
617 SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
619 if ( clip_path || mask ) {
620 NRRect bbox;
621 sp_item_invoke_bbox(item, &bbox, Geom::identity(), TRUE);
622 if (clip_path) {
623 for (SPItemView *v = item->display; v != NULL; v = v->next) {
624 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
625 }
626 }
627 if (mask) {
628 for (SPItemView *v = item->display; v != NULL; v = v->next) {
629 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
630 }
631 }
632 }
634 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
635 for (SPItemView *v = item->display; v != NULL; v = v->next) {
636 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
637 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
638 }
639 }
640 }
642 /* Update bounding box data used by filters */
643 if (item->style->filter.set && item->display) {
644 Geom::OptRect item_bbox;
645 sp_item_invoke_bbox(item, item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
647 SPItemView *itemview = item->display;
648 do {
649 if (itemview->arenaitem)
650 nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
651 } while ( (itemview = itemview->next) );
652 }
654 // Update libavoid with item geometry (for connector routing).
655 if (item->avoidRef)
656 item->avoidRef->handleSettingChange();
657 }
659 static Inkscape::XML::Node *
660 sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
661 {
662 SPObject *child;
663 SPItem *item = SP_ITEM(object);
665 // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
666 // so we need to add any children from the underlying object to the new repr
667 if (flags & SP_OBJECT_WRITE_BUILD) {
668 Inkscape::XML::Node *crepr;
669 GSList *l;
670 l = NULL;
671 for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
672 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
673 crepr = child->updateRepr(xml_doc, NULL, flags);
674 if (crepr) l = g_slist_prepend (l, crepr);
675 }
676 while (l) {
677 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
678 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
679 l = g_slist_remove (l, l->data);
680 }
681 } else {
682 for (child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
683 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
684 child->updateRepr(flags);
685 }
686 }
688 gchar *c = sp_svg_transform_write(item->transform);
689 repr->setAttribute("transform", c);
690 g_free(c);
692 if (flags & SP_OBJECT_WRITE_EXT) {
693 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
694 if (item->transform_center_x != 0)
695 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
696 else
697 repr->setAttribute ("inkscape:transform-center-x", NULL);
698 if (item->transform_center_y != 0)
699 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
700 else
701 repr->setAttribute ("inkscape:transform-center-y", NULL);
702 }
704 if (item->clip_ref->getObject()) {
705 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
706 repr->setAttribute ("clip-path", value);
707 g_free ((void *) value);
708 }
709 if (item->mask_ref->getObject()) {
710 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
711 repr->setAttribute ("mask", value);
712 g_free ((void *) value);
713 }
715 if (((SPObjectClass *) (parent_class))->write) {
716 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
717 }
719 return repr;
720 }
722 /**
723 * \return There is no guarantee that the return value will contain a rectangle.
724 If this item does not have a boundingbox, it might well be empty.
725 */
726 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
727 SPItem::BBoxType type,
728 unsigned int /*dkey*/) const
729 {
730 Geom::OptRect r;
731 sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
732 return r;
733 }
735 void
736 sp_item_invoke_bbox(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
737 {
738 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
739 }
741 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
742 void
743 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
744 {
745 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
746 }
748 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
749 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
750 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
751 * clones), in turn, call this function in their bbox methods.
752 * \retval bbox Note that there is no guarantee that bbox will contain a rectangle when the
753 * function returns. If this item does not have a boundingbox, this might well be empty.
754 */
755 void
756 sp_item_invoke_bbox_full(SPItem const *item, Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
757 {
758 g_assert(item != NULL);
759 g_assert(SP_IS_ITEM(item));
761 if (clear) {
762 bbox = Geom::OptRect();
763 }
765 // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
767 NRRect temp_bbox;
768 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
769 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
771 // call the subclass method
772 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
773 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
774 }
776 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
777 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
778 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
779 SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
780 if (filter && SP_IS_FILTER(filter)) {
781 // default filer area per the SVG spec:
782 double x = -0.1;
783 double y = -0.1;
784 double w = 1.2;
785 double h = 1.2;
787 // if area is explicitly set, override:
788 if (SP_FILTER(filter)->x._set)
789 x = SP_FILTER(filter)->x.computed;
790 if (SP_FILTER(filter)->y._set)
791 y = SP_FILTER(filter)->y.computed;
792 if (SP_FILTER(filter)->width._set)
793 w = SP_FILTER(filter)->width.computed;
794 if (SP_FILTER(filter)->height._set)
795 h = SP_FILTER(filter)->height.computed;
797 double dx0 = 0;
798 double dx1 = 0;
799 double dy0 = 0;
800 double dy1 = 0;
801 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
802 // if this is a single blur, use 2.4*radius
803 // which may be smaller than the default area;
804 // see set_filter_area for why it's 2.4
805 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
806 dx0 = -2.4 * r;
807 dx1 = 2.4 * r;
808 dy0 = -2.4 * r;
809 dy1 = 2.4 * r;
810 } else {
811 // otherwise, calculate expansion from relative to absolute units:
812 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
813 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
814 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
815 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
816 }
818 // transform the expansions by the item's transform:
819 Geom::Matrix i2d(sp_item_i2d_affine (item));
820 dx0 *= i2d.expansionX();
821 dx1 *= i2d.expansionX();
822 dy0 *= i2d.expansionY();
823 dy1 *= i2d.expansionY();
825 // expand the bbox
826 temp_bbox.x0 += dx0;
827 temp_bbox.x1 += dx1;
828 temp_bbox.y0 += dy0;
829 temp_bbox.y1 += dy1;
830 }
831 }
832 if (item->clip_ref->getObject()) {
833 NRRect b;
834 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
835 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
836 }
837 }
839 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
840 // Either the bbox hasn't been touched by the SPItemClass' bbox method
841 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
842 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
844 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
845 // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
846 // So union bbox with nothing = do nothing, just return
847 return;
848 }
850 // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
851 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
852 // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
853 Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
855 bbox = Geom::unify(bbox, temp_bbox_new);
856 }
858 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
859 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
860 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
861 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
862 * clones), in turn, call this function in their bbox methods. */
863 void
864 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
865 {
866 g_assert(item != NULL);
867 g_assert(SP_IS_ITEM(item));
868 g_assert(bbox != NULL);
870 if (clear) {
871 bbox->x0 = bbox->y0 = 1e18;
872 bbox->x1 = bbox->y1 = -1e18;
873 }
875 NRRect this_bbox;
876 this_bbox.x0 = this_bbox.y0 = 1e18;
877 this_bbox.x1 = this_bbox.y1 = -1e18;
879 // call the subclass method
880 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
881 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
882 }
884 // unless this is geometric bbox, crop the bbox by clip path, if any
885 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
886 NRRect b;
887 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
888 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
889 }
891 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
892 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
893 nr_rect_d_union (bbox, bbox, &this_bbox);
894 }
895 }
897 unsigned sp_item_pos_in_parent(SPItem *item)
898 {
899 g_assert(item != NULL);
900 g_assert(SP_IS_ITEM(item));
902 SPObject *parent = SP_OBJECT_PARENT(item);
903 g_assert(parent != NULL);
904 g_assert(SP_IS_OBJECT(parent));
906 SPObject *object = SP_OBJECT(item);
908 unsigned pos=0;
909 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
910 if ( iter == object ) {
911 return pos;
912 }
913 if (SP_IS_ITEM(iter)) {
914 pos++;
915 }
916 }
918 g_assert_not_reached();
919 return 0;
920 }
922 void
923 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
924 {
925 g_assert(item != NULL);
926 g_assert(SP_IS_ITEM(item));
927 g_assert(bbox != NULL);
929 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
930 }
932 Geom::OptRect sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
933 {
934 Geom::OptRect rect = Geom::OptRect();
935 sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
936 return rect;
937 }
939 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const */*snapprefs*/)
940 {
941 /* This will only be called if the derived class doesn't override this.
942 * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
943 * We don't know what shape we could be dealing with here, so we'll just
944 * return the corners of the bounding box */
946 Geom::OptRect bbox = item->getBounds(sp_item_i2d_affine(item));
948 if (bbox) {
949 Geom::Point p1, p2;
950 p1 = bbox->min();
951 p2 = bbox->max();
952 *p = p1;
953 *p = Geom::Point(p1[Geom::X], p2[Geom::Y]);
954 *p = p2;
955 *p = Geom::Point(p1[Geom::Y], p2[Geom::X]);
956 }
958 }
960 void sp_item_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
961 {
962 g_assert (item != NULL);
963 g_assert (SP_IS_ITEM(item));
965 // Get the snappoints of the item
966 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
967 if (item_class.snappoints) {
968 item_class.snappoints(item, p, snapprefs);
969 }
971 // Get the snappoints at the item's center
972 if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
973 *p = item->getCenter();
974 }
976 // Get the snappoints of clipping paths and mask, if any
977 std::list<SPObject const *> clips_and_masks;
979 clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
980 clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
982 SPDesktop *desktop = inkscape_active_desktop();
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 = desktop->dt2doc(*p_orig) * sp_item_i2d_affine(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 * Set a new transform on an object.
1383 *
1384 * Compensate for stroke scaling and gradient/pattern fill transform, if
1385 * necessary. Call the object's set_transform method if transforms are
1386 * stored optimized. Send _transformed_signal. Invoke _write method so that
1387 * the repr is updated with the new transform.
1388 */
1389 void
1390 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
1391 {
1392 g_return_if_fail(item != NULL);
1393 g_return_if_fail(SP_IS_ITEM(item));
1394 g_return_if_fail(repr != NULL);
1396 // calculate the relative transform, if not given by the adv attribute
1397 Geom::Matrix advertized_transform;
1398 if (adv != NULL) {
1399 advertized_transform = *adv;
1400 } else {
1401 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1402 }
1404 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1405 if (compensate) {
1407 // recursively compensate for stroke scaling, depending on user preference
1408 if (!prefs->getBool("/options/transform/stroke", true)) {
1409 double const expansion = 1. / advertized_transform.descrim();
1410 sp_item_adjust_stroke_width_recursive(item, expansion);
1411 }
1413 // recursively compensate rx/ry of a rect if requested
1414 if (!prefs->getBool("/options/transform/rectcorners", true)) {
1415 sp_item_adjust_rects_recursive(item, advertized_transform);
1416 }
1418 // recursively compensate pattern fill if it's not to be transformed
1419 if (!prefs->getBool("/options/transform/pattern", true)) {
1420 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), true);
1421 }
1422 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1423 /// recursively compensate gradient fill if it's not to be transformed
1424 if (!prefs->getBool("/options/transform/gradient", true)) {
1425 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), Geom::identity(), false);
1426 } else {
1427 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1428 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1429 sp_item_adjust_paint_recursive (item, Geom::identity(), Geom::identity(), false);
1430 }
1432 } // endif(compensate)
1434 gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1435 Geom::Matrix transform_attr (transform);
1436 if ( // run the object's set_transform (i.e. embed transform) only if:
1437 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1438 !preserve && // user did not chose to preserve all transforms
1439 !item->clip_ref->getObject() && // the object does not have a clippath
1440 !item->mask_ref->getObject() && // the object does not have a mask
1441 !(!transform.isTranslation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1442 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1443 ) {
1444 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1445 }
1446 sp_item_set_item_transform(item, transform_attr);
1448 // Note: updateRepr comes before emitting the transformed signal since
1449 // it causes clone SPUse's copy of the original object to brought up to
1450 // date with the original. Otherwise, sp_use_bbox returns incorrect
1451 // values if called in code handling the transformed signal.
1452 SP_OBJECT(item)->updateRepr();
1454 // send the relative transform with a _transformed_signal
1455 item->_transformed_signal.emit(&advertized_transform, item);
1456 }
1458 gint
1459 sp_item_event(SPItem *item, SPEvent *event)
1460 {
1461 g_return_val_if_fail(item != NULL, FALSE);
1462 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1463 g_return_val_if_fail(event != NULL, FALSE);
1465 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1466 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1468 return FALSE;
1469 }
1471 /**
1472 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1473 * gradients, patterns as sp_item_write_transform does.
1474 */
1475 void
1476 sp_item_set_item_transform(SPItem *item, Geom::Matrix const &transform)
1477 {
1478 g_return_if_fail(item != NULL);
1479 g_return_if_fail(SP_IS_ITEM(item));
1481 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1482 item->transform = transform;
1483 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1484 transformation. It's apparently not used anywhere else. */
1485 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1486 sp_item_rm_unsatisfied_cns(*item);
1487 }
1488 }
1490 void
1491 sp_item_convert_item_to_guides(SPItem *item) {
1492 g_return_if_fail(item != NULL);
1493 g_return_if_fail(SP_IS_ITEM(item));
1495 /* Use derived method if present ... */
1496 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1497 (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1498 return;
1499 }
1501 /* .. otherwise simply place the guides around the item's bounding box */
1503 sp_item_convert_to_guides(item);
1504 }
1507 /**
1508 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1509 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1510 */
1511 Geom::Matrix
1512 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1513 Geom::Matrix ret(Geom::identity());
1514 g_return_val_if_fail(object != NULL, ret);
1516 /* stop at first non-renderable ancestor */
1517 while ( object != ancestor && SP_IS_ITEM(object) ) {
1518 if (SP_IS_ROOT(object)) {
1519 ret *= SP_ROOT(object)->c2p;
1520 } else {
1521 ret *= SP_ITEM(object)->transform;
1522 }
1523 object = SP_OBJECT_PARENT(object);
1524 }
1525 return ret;
1526 }
1528 Geom::Matrix
1529 i2i_affine(SPObject const *src, SPObject const *dest) {
1530 g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1531 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1532 return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1533 }
1535 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1536 return i2i_affine(this, dest);
1537 }
1539 /**
1540 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1541 * \pre (item != NULL) and SP_IS_ITEM(item).
1542 */
1543 Geom::Matrix sp_item_i2doc_affine(SPItem const *item)
1544 {
1545 return i2anc_affine(item, NULL);
1546 }
1548 /**
1549 * Returns the transformation from item to desktop coords
1550 */
1551 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1552 {
1553 g_assert(item != NULL);
1554 g_assert(SP_IS_ITEM(item));
1556 SPDesktop *desktop = inkscape_active_desktop();
1557 return sp_item_i2doc_affine(item) * desktop->doc2dt();
1558 }
1560 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1561 {
1562 g_return_if_fail( item != NULL );
1563 g_return_if_fail( SP_IS_ITEM(item) );
1565 Geom::Matrix dt2p; /* desktop to item parent transform */
1566 if (SP_OBJECT_PARENT(item)) {
1567 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1568 } else {
1569 dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1570 * Geom::Scale(1, -1) );
1571 }
1573 Geom::Matrix const i2p( i2dt * dt2p );
1574 sp_item_set_item_transform(item, i2p);
1575 }
1578 /**
1579 * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
1580 */
1581 Geom::Matrix
1582 sp_item_dt2i_affine(SPItem const *item)
1583 {
1584 /* fixme: Implement the right way (Lauris) */
1585 return sp_item_i2d_affine(item).inverse();
1586 }
1588 /* Item views */
1590 static SPItemView *
1591 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1592 {
1593 SPItemView *new_view;
1595 g_assert(item != NULL);
1596 g_assert(SP_IS_ITEM(item));
1597 g_assert(arenaitem != NULL);
1598 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1600 new_view = g_new(SPItemView, 1);
1602 new_view->next = list;
1603 new_view->flags = flags;
1604 new_view->key = key;
1605 new_view->arenaitem = arenaitem;
1607 return new_view;
1608 }
1610 static SPItemView *
1611 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1612 {
1613 if (view == list) {
1614 list = list->next;
1615 } else {
1616 SPItemView *prev;
1617 prev = list;
1618 while (prev->next != view) prev = prev->next;
1619 prev->next = view->next;
1620 }
1622 nr_arena_item_unref(view->arenaitem);
1623 g_free(view);
1625 return list;
1626 }
1628 /**
1629 * Return the arenaitem corresponding to the given item in the display
1630 * with the given key
1631 */
1632 NRArenaItem *
1633 sp_item_get_arenaitem(SPItem *item, unsigned key)
1634 {
1635 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1636 if ( iv->key == key ) {
1637 return iv->arenaitem;
1638 }
1639 }
1641 return NULL;
1642 }
1644 int
1645 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1646 {
1647 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1648 SP_OBJECT_REPR(second));
1649 }
1651 SPItem *
1652 sp_item_first_item_child (SPObject *obj)
1653 {
1654 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1655 if (SP_IS_ITEM (iter))
1656 return SP_ITEM (iter);
1657 }
1658 return NULL;
1659 }
1661 void
1662 sp_item_convert_to_guides(SPItem *item) {
1663 SPDesktop *dt = inkscape_active_desktop();
1664 SPNamedView *nv = sp_desktop_namedview(dt);
1665 (void)nv;
1667 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1668 int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1669 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1670 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1672 Geom::OptRect bbox = sp_item_bbox_desktop(item, bbox_type);
1673 if (!bbox) {
1674 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1675 return;
1676 }
1678 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1680 Geom::Point A((*bbox).min());
1681 Geom::Point C((*bbox).max());
1682 Geom::Point B(A[Geom::X], C[Geom::Y]);
1683 Geom::Point D(C[Geom::X], A[Geom::Y]);
1685 pts.push_back(std::make_pair(A, B));
1686 pts.push_back(std::make_pair(B, C));
1687 pts.push_back(std::make_pair(C, D));
1688 pts.push_back(std::make_pair(D, A));
1690 sp_guide_pt_pairs_to_guides(dt, pts);
1691 }
1693 /*
1694 Local Variables:
1695 mode:c++
1696 c-file-style:"stroustrup"
1697 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1698 indent-tabs-mode:nil
1699 fill-column:99
1700 End:
1701 */
1702 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :