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 * Abhishek Sharma
10 * Jon A. Cruz <jon@joncruz.org>
11 *
12 * Copyright (C) 2001-2006 authors
13 * Copyright (C) 2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 /** \class SPItem
19 *
20 * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
21 * is a subclass of SPObject, with great deal of specific functionality.
22 */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
29 #include "sp-item.h"
30 #include "svg/svg.h"
31 #include "print.h"
32 #include "display/nr-arena.h"
33 #include "display/nr-arena-item.h"
34 #include "attributes.h"
35 #include "document.h"
36 #include "uri.h"
37 #include "inkscape.h"
38 #include "desktop.h"
39 #include "desktop-handles.h"
41 #include "style.h"
42 #include <glibmm/i18n.h>
43 #include "sp-root.h"
44 #include "sp-clippath.h"
45 #include "sp-mask.h"
46 #include "sp-rect.h"
47 #include "sp-use.h"
48 #include "sp-text.h"
49 #include "sp-item-rm-unsatisfied-cns.h"
50 #include "sp-pattern.h"
51 #include "sp-paint-server.h"
52 #include "sp-switch.h"
53 #include "sp-guide-constraint.h"
54 #include "gradient-chemistry.h"
55 #include "preferences.h"
56 #include "conn-avoid-ref.h"
57 #include "conditions.h"
58 #include "sp-filter-reference.h"
59 #include "filter-chemistry.h"
60 #include "sp-guide.h"
61 #include "sp-title.h"
62 #include "sp-desc.h"
64 #include "libnr/nr-matrix-fns.h"
65 #include "libnr/nr-matrix-scale-ops.h"
66 #include "libnr/nr-matrix-translate-ops.h"
67 #include "libnr/nr-scale-translate-ops.h"
68 #include "libnr/nr-translate-scale-ops.h"
69 #include "libnr/nr-convert2geom.h"
70 #include "util/find-last-if.h"
71 #include "util/reverse-list.h"
72 #include <2geom/rect.h>
73 #include <2geom/matrix.h>
74 #include <2geom/transforms.h>
76 #include "xml/repr.h"
77 #include "extract-uri.h"
78 #include "helper/geom.h"
80 #include "live_effects/lpeobject.h"
81 #include "live_effects/effect.h"
82 #include "live_effects/lpeobject-reference.h"
84 #define noSP_ITEM_DEBUG_IDLE
86 SPObjectClass * SPItemClass::static_parent_class=0;
88 /**
89 * Registers SPItem class and returns its type number.
90 */
91 GType
92 SPItem::getType(void)
93 {
94 static GType type = 0;
95 if (!type) {
96 GTypeInfo info = {
97 sizeof(SPItemClass),
98 NULL, NULL,
99 (GClassInitFunc) SPItemClass::sp_item_class_init,
100 NULL, NULL,
101 sizeof(SPItem),
102 16,
103 (GInstanceInitFunc) sp_item_init,
104 NULL, /* value_table */
105 };
106 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
107 }
108 return type;
109 }
111 /**
112 * SPItem vtable initialization.
113 */
114 void
115 SPItemClass::sp_item_class_init(SPItemClass *klass)
116 {
117 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
119 static_parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
121 sp_object_class->build = SPItem::sp_item_build;
122 sp_object_class->release = SPItem::sp_item_release;
123 sp_object_class->set = SPItem::sp_item_set;
124 sp_object_class->update = SPItem::sp_item_update;
125 sp_object_class->write = SPItem::sp_item_write;
127 klass->description = SPItem::sp_item_private_description;
128 klass->snappoints = SPItem::sp_item_private_snappoints;
129 }
131 /**
132 * Callback for SPItem object initialization.
133 */
134 void SPItem::sp_item_init(SPItem *item)
135 {
136 item->init();
137 }
139 void SPItem::init() {
140 sensitive = TRUE;
142 transform_center_x = 0;
143 transform_center_y = 0;
145 _is_evaluated = true;
146 _evaluated_status = StatusUnknown;
148 transform = Geom::identity();
150 display = NULL;
152 clip_ref = new SPClipPathReference(this);
153 sigc::signal<void, SPObject *, SPObject *> cs1 = clip_ref->changedSignal();
154 sigc::slot2<void,SPObject*, SPObject *> sl1 = sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
155 _clip_ref_connection = cs1.connect(sl1);
157 mask_ref = new SPMaskReference(this);
158 sigc::signal<void, SPObject *, SPObject *> cs2 = mask_ref->changedSignal();
159 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
160 _mask_ref_connection = cs2.connect(sl2);
162 avoidRef = new SPAvoidRef(this);
164 new (&constraints) std::vector<SPGuideConstraint>();
166 new (&_transformed_signal) sigc::signal<void, Geom::Matrix const *, SPItem *>();
167 }
169 bool SPItem::isVisibleAndUnlocked() const {
170 return (!isHidden() && !isLocked());
171 }
173 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
174 return (!isHidden(display_key) && !isLocked());
175 }
177 bool SPItem::isLocked() const {
178 for (SPObject const *o = this; o != NULL; o = o->parent) {
179 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive)) {
180 return true;
181 }
182 }
183 return false;
184 }
186 void SPItem::setLocked(bool locked) {
187 setAttribute("sodipodi:insensitive",
188 ( locked ? "1" : NULL ));
189 updateRepr();
190 }
192 bool SPItem::isHidden() const {
193 if (!isEvaluated())
194 return true;
195 return style->display.computed == SP_CSS_DISPLAY_NONE;
196 }
198 void SPItem::setHidden(bool hide) {
199 style->display.set = TRUE;
200 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
201 style->display.computed = style->display.value;
202 style->display.inherit = FALSE;
203 updateRepr();
204 }
206 bool SPItem::isHidden(unsigned display_key) const {
207 if (!isEvaluated())
208 return true;
209 for ( SPItemView *view(display) ; view ; view = view->next ) {
210 if ( view->key == display_key ) {
211 g_assert(view->arenaitem != NULL);
212 for ( NRArenaItem *arenaitem = view->arenaitem ;
213 arenaitem ; arenaitem = arenaitem->parent )
214 {
215 if (!arenaitem->visible) {
216 return true;
217 }
218 }
219 return false;
220 }
221 }
222 return true;
223 }
225 void SPItem::setEvaluated(bool evaluated) {
226 _is_evaluated = evaluated;
227 _evaluated_status = StatusSet;
228 }
230 void SPItem::resetEvaluated() {
231 if ( StatusCalculated == _evaluated_status ) {
232 _evaluated_status = StatusUnknown;
233 bool oldValue = _is_evaluated;
234 if ( oldValue != isEvaluated() ) {
235 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
236 }
237 } if ( StatusSet == _evaluated_status ) {
238 if (SP_IS_SWITCH(parent)) {
239 SP_SWITCH(parent)->resetChildEvaluated();
240 }
241 }
242 }
244 bool SPItem::isEvaluated() const {
245 if ( StatusUnknown == _evaluated_status ) {
246 _is_evaluated = sp_item_evaluate(this);
247 _evaluated_status = StatusCalculated;
248 }
249 return _is_evaluated;
250 }
252 /**
253 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
254 * Corresponds to setExplicitlyHidden.
255 */
256 bool SPItem::isExplicitlyHidden() const
257 {
258 return (style->display.set
259 && style->display.value == SP_CSS_DISPLAY_NONE);
260 }
262 /**
263 * Sets the display CSS property to `hidden' if \a val is true,
264 * otherwise makes it unset
265 */
266 void SPItem::setExplicitlyHidden(bool const val) {
267 style->display.set = val;
268 style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
269 style->display.computed = style->display.value;
270 updateRepr();
271 }
273 /**
274 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
275 */
276 void SPItem::setCenter(Geom::Point object_centre) {
277 // for getBounds() to work
278 document->ensureUpToDate();
280 Geom::OptRect bbox = getBounds(i2d_affine());
281 if (bbox) {
282 transform_center_x = object_centre[Geom::X] - bbox->midpoint()[Geom::X];
283 if (fabs(transform_center_x) < 1e-5) // rounding error
284 transform_center_x = 0;
285 transform_center_y = object_centre[Geom::Y] - bbox->midpoint()[Geom::Y];
286 if (fabs(transform_center_y) < 1e-5) // rounding error
287 transform_center_y = 0;
288 }
289 }
291 void
292 SPItem::unsetCenter() {
293 transform_center_x = 0;
294 transform_center_y = 0;
295 }
297 bool SPItem::isCenterSet() {
298 return (transform_center_x != 0 || transform_center_y != 0);
299 }
301 Geom::Point SPItem::getCenter() const {
302 // for getBounds() to work
303 document->ensureUpToDate();
305 Geom::OptRect bbox = getBounds(i2d_affine());
306 if (bbox) {
307 return to_2geom(bbox->midpoint()) + Geom::Point (transform_center_x, transform_center_y);
308 } else {
309 return Geom::Point(0, 0); // something's wrong!
310 }
311 }
314 namespace {
316 bool is_item(SPObject const &object) {
317 return SP_IS_ITEM(&object);
318 }
320 }
322 void SPItem::raiseToTop() {
323 using Inkscape::Algorithms::find_last_if;
325 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
326 next, NULL, &is_item
327 );
328 if (topmost) {
329 getRepr()->parent()->changeOrder( getRepr(), topmost->getRepr() );
330 }
331 }
333 void SPItem::raiseOne() {
334 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
335 next, NULL, &is_item
336 );
337 if (next_higher) {
338 Inkscape::XML::Node *ref = next_higher->getRepr();
339 getRepr()->parent()->changeOrder(getRepr(), ref);
340 }
341 }
343 void SPItem::lowerOne() {
344 using Inkscape::Util::MutableList;
345 using Inkscape::Util::reverse_list;
347 MutableList<SPObject &> next_lower=std::find_if(
348 reverse_list<SPObject::SiblingIterator>(
349 parent->firstChild(), this
350 ),
351 MutableList<SPObject &>(),
352 &is_item
353 );
354 if (next_lower) {
355 ++next_lower;
356 Inkscape::XML::Node *ref = ( next_lower ? next_lower->getRepr() : NULL );
357 getRepr()->parent()->changeOrder(getRepr(), ref);
358 }
359 }
361 void SPItem::lowerToBottom() {
362 using Inkscape::Algorithms::find_last_if;
363 using Inkscape::Util::MutableList;
364 using Inkscape::Util::reverse_list;
366 MutableList<SPObject &> bottom=find_last_if(
367 reverse_list<SPObject::SiblingIterator>(
368 parent->firstChild(), this
369 ),
370 MutableList<SPObject &>(),
371 &is_item
372 );
373 if (bottom) {
374 ++bottom;
375 Inkscape::XML::Node *ref = ( bottom ? bottom->getRepr() : NULL );
376 getRepr()->parent()->changeOrder(getRepr(), ref);
377 }
378 }
380 void SPItem::sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
381 {
382 object->readAttr( "style" );
383 object->readAttr( "transform" );
384 object->readAttr( "clip-path" );
385 object->readAttr( "mask" );
386 object->readAttr( "sodipodi:insensitive" );
387 object->readAttr( "sodipodi:nonprintable" );
388 object->readAttr( "inkscape:transform-center-x" );
389 object->readAttr( "inkscape:transform-center-y" );
390 object->readAttr( "inkscape:connector-avoid" );
391 object->readAttr( "inkscape:connection-points" );
393 if (((SPObjectClass *) (SPItemClass::static_parent_class))->build) {
394 (* ((SPObjectClass *) (SPItemClass::static_parent_class))->build)(object, document, repr);
395 }
396 }
398 void SPItem::sp_item_release(SPObject *object)
399 {
400 SPItem *item = (SPItem *) object;
402 item->_clip_ref_connection.disconnect();
403 item->_mask_ref_connection.disconnect();
405 // Note: do this here before the clip_ref is deleted, since calling
406 // ensureUpToDate() for triggered routing may reference
407 // the deleted clip_ref.
408 if (item->avoidRef) {
409 delete item->avoidRef;
410 item->avoidRef = NULL;
411 }
413 if (item->clip_ref) {
414 item->clip_ref->detach();
415 delete item->clip_ref;
416 item->clip_ref = NULL;
417 }
419 if (item->mask_ref) {
420 item->mask_ref->detach();
421 delete item->mask_ref;
422 item->mask_ref = NULL;
423 }
425 if (((SPObjectClass *) (SPItemClass::static_parent_class))->release) {
426 ((SPObjectClass *) SPItemClass::static_parent_class)->release(object);
427 }
429 while (item->display) {
430 nr_arena_item_unparent(item->display->arenaitem);
431 item->display = sp_item_view_list_remove(item->display, item->display);
432 }
434 item->_transformed_signal.~signal();
435 }
437 void SPItem::sp_item_set(SPObject *object, unsigned key, gchar const *value)
438 {
439 SPItem *item = (SPItem *) object;
441 switch (key) {
442 case SP_ATTR_TRANSFORM: {
443 Geom::Matrix t;
444 if (value && sp_svg_transform_read(value, &t)) {
445 item->set_item_transform(t);
446 } else {
447 item->set_item_transform(Geom::identity());
448 }
449 break;
450 }
451 case SP_PROP_CLIP_PATH: {
452 gchar *uri = extract_uri(value);
453 if (uri) {
454 try {
455 item->clip_ref->attach(Inkscape::URI(uri));
456 } catch (Inkscape::BadURIException &e) {
457 g_warning("%s", e.what());
458 item->clip_ref->detach();
459 }
460 g_free(uri);
461 } else {
462 item->clip_ref->detach();
463 }
465 break;
466 }
467 case SP_PROP_MASK: {
468 gchar *uri = extract_uri(value);
469 if (uri) {
470 try {
471 item->mask_ref->attach(Inkscape::URI(uri));
472 } catch (Inkscape::BadURIException &e) {
473 g_warning("%s", e.what());
474 item->mask_ref->detach();
475 }
476 g_free(uri);
477 } else {
478 item->mask_ref->detach();
479 }
481 break;
482 }
483 case SP_ATTR_SODIPODI_INSENSITIVE:
484 item->sensitive = !value;
485 for (SPItemView *v = item->display; v != NULL; v = v->next) {
486 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
487 }
488 break;
489 case SP_ATTR_CONNECTOR_AVOID:
490 item->avoidRef->setAvoid(value);
491 break;
492 case SP_ATTR_CONNECTION_POINTS:
493 item->avoidRef->setConnectionPoints(value);
494 break;
495 case SP_ATTR_TRANSFORM_CENTER_X:
496 if (value) {
497 item->transform_center_x = g_strtod(value, NULL);
498 } else {
499 item->transform_center_x = 0;
500 }
501 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
502 break;
503 case SP_ATTR_TRANSFORM_CENTER_Y:
504 if (value) {
505 item->transform_center_y = g_strtod(value, NULL);
506 } else {
507 item->transform_center_y = 0;
508 }
509 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
510 break;
511 case SP_PROP_SYSTEM_LANGUAGE:
512 case SP_PROP_REQUIRED_FEATURES:
513 case SP_PROP_REQUIRED_EXTENSIONS:
514 {
515 item->resetEvaluated();
516 // pass to default handler
517 }
518 default:
519 if (SP_ATTRIBUTE_IS_CSS(key)) {
520 sp_style_read_from_object(object->style, object);
521 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
522 } else {
523 if (((SPObjectClass *) (SPItemClass::static_parent_class))->set) {
524 (* ((SPObjectClass *) (SPItemClass::static_parent_class))->set)(object, key, value);
525 }
526 }
527 break;
528 }
529 }
531 void SPItem::clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
532 {
533 if (old_clip) {
534 SPItemView *v;
535 /* Hide clippath */
536 for (v = item->display; v != NULL; v = v->next) {
537 SP_CLIPPATH(old_clip)->hide(NR_ARENA_ITEM_GET_KEY(v->arenaitem));
538 nr_arena_item_set_clip(v->arenaitem, NULL);
539 }
540 }
541 if (SP_IS_CLIPPATH(clip)) {
542 NRRect bbox;
543 item->invoke_bbox( &bbox, Geom::identity(), TRUE);
544 for (SPItemView *v = item->display; v != NULL; v = v->next) {
545 if (!v->arenaitem->key) {
546 NR_ARENA_ITEM_SET_KEY(v->arenaitem, SPItem::display_key_new(3));
547 }
548 NRArenaItem *ai = SP_CLIPPATH(clip)->show(
549 NR_ARENA_ITEM_ARENA(v->arenaitem),
550 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
551 nr_arena_item_set_clip(v->arenaitem, ai);
552 nr_arena_item_unref(ai);
553 SP_CLIPPATH(clip)->setBBox(NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
554 clip->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
555 }
556 }
557 }
559 void SPItem::mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
560 {
561 if (old_mask) {
562 /* Hide mask */
563 for (SPItemView *v = item->display; v != NULL; v = v->next) {
564 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
565 nr_arena_item_set_mask(v->arenaitem, NULL);
566 }
567 }
568 if (SP_IS_MASK(mask)) {
569 NRRect bbox;
570 item->invoke_bbox( &bbox, Geom::identity(), TRUE);
571 for (SPItemView *v = item->display; v != NULL; v = v->next) {
572 if (!v->arenaitem->key) {
573 NR_ARENA_ITEM_SET_KEY(v->arenaitem, SPItem::display_key_new(3));
574 }
575 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
576 NR_ARENA_ITEM_ARENA(v->arenaitem),
577 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
578 nr_arena_item_set_mask(v->arenaitem, ai);
579 nr_arena_item_unref(ai);
580 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
581 mask->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
582 }
583 }
584 }
586 void SPItem::sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
587 {
588 SPItem *item = SP_ITEM(object);
590 if (((SPObjectClass *) (SPItemClass::static_parent_class))->update) {
591 (* ((SPObjectClass *) (SPItemClass::static_parent_class))->update)(object, ctx, flags);
592 }
594 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
595 if (flags & SP_OBJECT_MODIFIED_FLAG) {
596 for (SPItemView *v = item->display; v != NULL; v = v->next) {
597 nr_arena_item_set_transform(v->arenaitem, item->transform);
598 }
599 }
601 SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
602 SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
604 if ( clip_path || mask ) {
605 NRRect bbox;
606 item->invoke_bbox( &bbox, Geom::identity(), TRUE);
607 if (clip_path) {
608 for (SPItemView *v = item->display; v != NULL; v = v->next) {
609 clip_path->setBBox(NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
610 }
611 }
612 if (mask) {
613 for (SPItemView *v = item->display; v != NULL; v = v->next) {
614 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
615 }
616 }
617 }
619 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
620 for (SPItemView *v = item->display; v != NULL; v = v->next) {
621 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
622 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
623 }
624 }
625 }
627 /* Update bounding box data used by filters */
628 if (item->style->filter.set && item->display) {
629 Geom::OptRect item_bbox;
630 item->invoke_bbox( item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
632 SPItemView *itemview = item->display;
633 do {
634 if (itemview->arenaitem)
635 nr_arena_item_set_item_bbox(itemview->arenaitem, item_bbox);
636 } while ( (itemview = itemview->next) );
637 }
639 // Update libavoid with item geometry (for connector routing).
640 if (item->avoidRef)
641 item->avoidRef->handleSettingChange();
642 }
644 Inkscape::XML::Node *SPItem::sp_item_write(SPObject *const object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
645 {
646 SPObject *child;
647 SPItem *item = SP_ITEM(object);
649 // in the case of SP_OBJECT_WRITE_BUILD, the item should always be newly created,
650 // so we need to add any children from the underlying object to the new repr
651 if (flags & SP_OBJECT_WRITE_BUILD) {
652 Inkscape::XML::Node *crepr;
653 GSList *l;
654 l = NULL;
655 for (child = object->firstChild(); child != NULL; child = child->next ) {
656 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
657 crepr = child->updateRepr(xml_doc, NULL, flags);
658 if (crepr) l = g_slist_prepend (l, crepr);
659 }
660 while (l) {
661 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
662 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
663 l = g_slist_remove (l, l->data);
664 }
665 } else {
666 for (child = object->firstChild() ; child != NULL; child = child->next ) {
667 if (!SP_IS_TITLE(child) && !SP_IS_DESC(child)) continue;
668 child->updateRepr(flags);
669 }
670 }
672 gchar *c = sp_svg_transform_write(item->transform);
673 repr->setAttribute("transform", c);
674 g_free(c);
676 if (flags & SP_OBJECT_WRITE_EXT) {
677 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
678 if (item->transform_center_x != 0)
679 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
680 else
681 repr->setAttribute ("inkscape:transform-center-x", NULL);
682 if (item->transform_center_y != 0)
683 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
684 else
685 repr->setAttribute ("inkscape:transform-center-y", NULL);
686 }
688 if (item->clip_ref->getObject()) {
689 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
690 repr->setAttribute ("clip-path", value);
691 g_free ((void *) value);
692 }
693 if (item->mask_ref->getObject()) {
694 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
695 repr->setAttribute ("mask", value);
696 g_free ((void *) value);
697 }
699 if (((SPObjectClass *) (SPItemClass::static_parent_class))->write) {
700 ((SPObjectClass *) (SPItemClass::static_parent_class))->write(object, xml_doc, repr, flags);
701 }
703 return repr;
704 }
706 /**
707 * \return There is no guarantee that the return value will contain a rectangle.
708 If this item does not have a boundingbox, it might well be empty.
709 */
710 Geom::OptRect SPItem::getBounds(Geom::Matrix const &transform,
711 SPItem::BBoxType type,
712 unsigned int /*dkey*/) const
713 {
714 Geom::OptRect r;
715 invoke_bbox_full( r, transform, type, TRUE);
716 return r;
717 }
719 void SPItem::invoke_bbox( Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
720 {
721 invoke_bbox_full( bbox, transform, type, clear);
722 }
724 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
725 void SPItem::invoke_bbox( NRRect *bbox, Geom::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
726 {
727 invoke_bbox_full( bbox, transform, type, clear);
728 }
730 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
731 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
732 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
733 * clones), in turn, call this function in their bbox methods.
734 * \retval bbox Note that there is no guarantee that bbox will contain a rectangle when the
735 * function returns. If this item does not have a boundingbox, this might well be empty.
736 */
737 void SPItem::invoke_bbox_full( Geom::OptRect &bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear) const
738 {
739 if (clear) {
740 bbox = Geom::OptRect();
741 }
743 // TODO: replace NRRect by Geom::Rect, for all SPItemClasses, and for SP_CLIPPATH
745 NRRect temp_bbox;
746 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
747 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
749 // call the subclass method
750 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox) {
751 ((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox(this, &temp_bbox, transform, flags);
752 }
754 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
755 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
756 if ( style && style->filter.href) {
757 SPObject *filter = style->getFilter();
758 if (filter && SP_IS_FILTER(filter)) {
759 // default filer area per the SVG spec:
760 double x = -0.1;
761 double y = -0.1;
762 double w = 1.2;
763 double h = 1.2;
765 // if area is explicitly set, override:
766 if (SP_FILTER(filter)->x._set)
767 x = SP_FILTER(filter)->x.computed;
768 if (SP_FILTER(filter)->y._set)
769 y = SP_FILTER(filter)->y.computed;
770 if (SP_FILTER(filter)->width._set)
771 w = SP_FILTER(filter)->width.computed;
772 if (SP_FILTER(filter)->height._set)
773 h = SP_FILTER(filter)->height.computed;
775 double dx0 = 0;
776 double dx1 = 0;
777 double dy0 = 0;
778 double dy1 = 0;
779 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
780 // if this is a single blur, use 2.4*radius
781 // which may be smaller than the default area;
782 // see set_filter_area for why it's 2.4
783 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
784 dx0 = -2.4 * r;
785 dx1 = 2.4 * r;
786 dy0 = -2.4 * r;
787 dy1 = 2.4 * r;
788 } else {
789 // otherwise, calculate expansion from relative to absolute units:
790 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
791 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
792 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
793 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
794 }
796 // transform the expansions by the item's transform:
797 Geom::Matrix i2d(i2d_affine ());
798 dx0 *= i2d.expansionX();
799 dx1 *= i2d.expansionX();
800 dy0 *= i2d.expansionY();
801 dy1 *= i2d.expansionY();
803 // expand the bbox
804 temp_bbox.x0 += dx0;
805 temp_bbox.x1 += dx1;
806 temp_bbox.y0 += dy0;
807 temp_bbox.y1 += dy1;
808 }
809 }
810 if (clip_ref->getObject()) {
811 NRRect b;
812 SP_CLIPPATH(clip_ref->getObject())->getBBox(&b, transform, flags);
813 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
814 }
815 }
817 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
818 // Either the bbox hasn't been touched by the SPItemClass' bbox method
819 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
820 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
822 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
823 // explicitely defined this way for NRRects (as opposed to Geom::OptRect)
824 // So union bbox with nothing = do nothing, just return
825 return;
826 }
828 // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty Geom::OptRect()
829 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
830 // would therefore be translated into empty Geom::OptRect() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
831 Geom::OptRect temp_bbox_new = Geom::Rect(Geom::Point(temp_bbox.x0, temp_bbox.y0), Geom::Point(temp_bbox.x1, temp_bbox.y1));
833 bbox = Geom::unify(bbox, temp_bbox_new);
834 }
836 // DEPRECATED to phase out the use of NRRect in favor of Geom::OptRect
837 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
838 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
839 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
840 * clones), in turn, call this function in their bbox methods. */
841 void SPItem::invoke_bbox_full( NRRect *bbox, Geom::Matrix const &transform, unsigned const flags, unsigned const clear)
842 {
843 g_assert(bbox != NULL);
845 if (clear) {
846 bbox->x0 = bbox->y0 = 1e18;
847 bbox->x1 = bbox->y1 = -1e18;
848 }
850 NRRect this_bbox;
851 this_bbox.x0 = this_bbox.y0 = 1e18;
852 this_bbox.x1 = this_bbox.y1 = -1e18;
854 // call the subclass method
855 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox) {
856 ((SPItemClass *) G_OBJECT_GET_CLASS(this))->bbox(this, &this_bbox, transform, flags);
857 }
859 // unless this is geometric bbox, crop the bbox by clip path, if any
860 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && clip_ref->getObject()) {
861 NRRect b;
862 SP_CLIPPATH(clip_ref->getObject())->getBBox(&b, transform, flags);
863 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
864 }
866 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
867 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
868 nr_rect_d_union (bbox, bbox, &this_bbox);
869 }
870 }
872 unsigned SPItem::pos_in_parent()
873 {
874 g_assert(parent != NULL);
875 g_assert(SP_IS_OBJECT(parent));
877 SPObject *object = this;
879 unsigned pos=0;
880 for ( SPObject *iter = parent->firstChild() ; iter ; iter = iter->next) {
881 if ( iter == object ) {
882 return pos;
883 }
884 if (SP_IS_ITEM(iter)) {
885 pos++;
886 }
887 }
889 g_assert_not_reached();
890 return 0;
891 }
893 void SPItem::getBboxDesktop(NRRect *bbox, SPItem::BBoxType type)
894 {
895 g_assert(bbox != NULL);
897 invoke_bbox( bbox, i2d_affine(), TRUE, type);
898 }
900 Geom::OptRect SPItem::getBboxDesktop(SPItem::BBoxType type)
901 {
902 Geom::OptRect rect = Geom::OptRect();
903 invoke_bbox( rect, i2d_affine(), TRUE, type);
904 return rect;
905 }
907 void SPItem::sp_item_private_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const */*snapprefs*/)
908 {
909 /* This will only be called if the derived class doesn't override this.
910 * see for example sp_genericellipse_snappoints in sp-ellipse.cpp
911 * We don't know what shape we could be dealing with here, so we'll just
912 * return the corners of the bounding box */
914 Geom::OptRect bbox = item->getBounds(item->i2d_affine());
916 if (bbox) {
917 Geom::Point p1, p2;
918 p1 = bbox->min();
919 p2 = bbox->max();
920 p.push_back(Inkscape::SnapCandidatePoint(p1, Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
921 p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(p1[Geom::X], p2[Geom::Y]), Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
922 p.push_back(Inkscape::SnapCandidatePoint(p2, Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
923 p.push_back(Inkscape::SnapCandidatePoint(Geom::Point(p2[Geom::X], p1[Geom::Y]), Inkscape::SNAPSOURCE_BBOX_CORNER, Inkscape::SNAPTARGET_BBOX_CORNER));
924 }
926 }
928 void SPItem::getSnappoints(std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs) const
929 {
930 // Get the snappoints of the item
931 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(this);
932 if (item_class.snappoints) {
933 item_class.snappoints(this, p, snapprefs);
934 }
936 // Get the snappoints at the item's center
937 if (snapprefs != NULL && snapprefs->getIncludeItemCenter()) {
938 p.push_back(Inkscape::SnapCandidatePoint(getCenter(), Inkscape::SNAPSOURCE_ROTATION_CENTER, Inkscape::SNAPTARGET_ROTATION_CENTER));
939 }
941 // Get the snappoints of clipping paths and mask, if any
942 std::list<SPObject const *> clips_and_masks;
944 clips_and_masks.push_back(clip_ref->getObject());
945 clips_and_masks.push_back(mask_ref->getObject());
947 SPDesktop *desktop = inkscape_active_desktop();
948 for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
949 if (*o) {
950 // obj is a group object, the children are the actual clippers
951 for (SPObject *child = (*o)->children ; child ; child = child->next) {
952 if (SP_IS_ITEM(child)) {
953 std::vector<Inkscape::SnapCandidatePoint> p_clip_or_mask;
954 // Please note the recursive call here!
955 SP_ITEM(child)->getSnappoints(p_clip_or_mask, snapprefs);
956 // Take into account the transformation of the item being clipped or masked
957 for (std::vector<Inkscape::SnapCandidatePoint>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
958 // All snappoints are in desktop coordinates, but the item's transformation is
959 // in document coordinates. Hence the awkward construction below
960 Geom::Point pt = desktop->dt2doc((*p_orig).getPoint()) * i2d_affine();
961 p.push_back(Inkscape::SnapCandidatePoint(pt, (*p_orig).getSourceType(), (*p_orig).getTargetType()));
962 }
963 }
964 }
965 }
966 }
967 }
969 void SPItem::invoke_print(SPPrintContext *ctx)
970 {
971 if ( !isHidden() ) {
972 if ( reinterpret_cast<SPItemClass *>(G_OBJECT_GET_CLASS(this))->print ) {
973 if (!transform.isIdentity()
974 || style->opacity.value != SP_SCALE24_MAX)
975 {
976 sp_print_bind(ctx, transform, SP_SCALE24_TO_FLOAT(style->opacity.value));
977 reinterpret_cast<SPItemClass *>(G_OBJECT_GET_CLASS(this))->print(this, ctx);
978 sp_print_release(ctx);
979 } else {
980 reinterpret_cast<SPItemClass *>(G_OBJECT_GET_CLASS(this))->print(this, ctx);
981 }
982 }
983 }
984 }
986 gchar *SPItem::sp_item_private_description(SPItem */*item*/)
987 {
988 return g_strdup(_("Object"));
989 }
991 /**
992 * Returns a string suitable for status bar, formatted in pango markup language.
993 *
994 * Must be freed by caller.
995 */
996 gchar *SPItem::description()
997 {
998 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->description) {
999 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->description(this);
1000 if (s && clip_ref->getObject()) {
1001 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1002 g_free (s);
1003 s = snew;
1004 }
1005 if (s && mask_ref->getObject()) {
1006 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1007 g_free (s);
1008 s = snew;
1009 }
1010 if ( style && style->filter.href && style->filter.href->getObject() ) {
1011 const gchar *label = style->filter.href->getObject()->label();
1012 gchar *snew = 0;
1013 if (label) {
1014 snew = g_strdup_printf (_("%s; <i>filtered (%s)</i>"), s, _(label));
1015 } else {
1016 snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1017 }
1018 g_free (s);
1019 s = snew;
1020 }
1021 return s;
1022 }
1024 g_assert_not_reached();
1025 return NULL;
1026 }
1028 /**
1029 * Allocates unique integer keys.
1030 * \param numkeys Number of keys required.
1031 * \return First allocated key; hence if the returned key is n
1032 * you can use n, n + 1, ..., n + (numkeys - 1)
1033 */
1034 unsigned SPItem::display_key_new(unsigned numkeys)
1035 {
1036 static unsigned dkey = 0;
1038 dkey += numkeys;
1040 return dkey - numkeys;
1041 }
1043 NRArenaItem *SPItem::invoke_show(NRArena *arena, unsigned key, unsigned flags)
1044 {
1045 g_assert(arena != NULL);
1046 g_assert(NR_IS_ARENA(arena));
1048 NRArenaItem *ai = NULL;
1049 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->show) {
1050 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->show(this, arena, key, flags);
1051 }
1053 if (ai != NULL) {
1054 display = sp_item_view_new_prepend(display, this, flags, key, ai);
1055 nr_arena_item_set_transform(ai, transform);
1056 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(style->opacity.value));
1057 nr_arena_item_set_visible(ai, !isHidden());
1058 nr_arena_item_set_sensitive(ai, sensitive);
1059 if (clip_ref->getObject()) {
1060 SPClipPath *cp = clip_ref->getObject();
1062 if (!display->arenaitem->key) {
1063 NR_ARENA_ITEM_SET_KEY(display->arenaitem, display_key_new(3));
1064 }
1065 int clip_key = NR_ARENA_ITEM_GET_KEY(display->arenaitem);
1067 // Show and set clip
1068 NRArenaItem *ac = cp->show(arena, clip_key);
1069 nr_arena_item_set_clip(ai, ac);
1070 nr_arena_item_unref(ac);
1072 // Update bbox, in case the clip uses bbox units
1073 NRRect bbox;
1074 invoke_bbox( &bbox, Geom::identity(), TRUE);
1075 SP_CLIPPATH(cp)->setBBox(clip_key, &bbox);
1076 cp->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1077 }
1078 if (mask_ref->getObject()) {
1079 SPMask *mask = mask_ref->getObject();
1081 if (!display->arenaitem->key) {
1082 NR_ARENA_ITEM_SET_KEY(display->arenaitem, display_key_new(3));
1083 }
1084 int mask_key = NR_ARENA_ITEM_GET_KEY(display->arenaitem);
1086 // Show and set mask
1087 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1088 nr_arena_item_set_mask(ai, ac);
1089 nr_arena_item_unref(ac);
1091 // Update bbox, in case the mask uses bbox units
1092 NRRect bbox;
1093 invoke_bbox( &bbox, Geom::identity(), TRUE);
1094 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1095 mask->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1096 }
1097 NR_ARENA_ITEM_SET_DATA(ai, this);
1098 Geom::OptRect item_bbox;
1099 invoke_bbox( item_bbox, Geom::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1100 nr_arena_item_set_item_bbox(ai, item_bbox);
1101 }
1103 return ai;
1104 }
1106 void SPItem::invoke_hide(unsigned key)
1107 {
1108 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->hide) {
1109 ((SPItemClass *) G_OBJECT_GET_CLASS(this))->hide(this, key);
1110 }
1112 SPItemView *ref = NULL;
1113 SPItemView *v = display;
1114 while (v != NULL) {
1115 SPItemView *next = v->next;
1116 if (v->key == key) {
1117 if (clip_ref->getObject()) {
1118 (clip_ref->getObject())->hide(NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1119 nr_arena_item_set_clip(v->arenaitem, NULL);
1120 }
1121 if (mask_ref->getObject()) {
1122 sp_mask_hide(mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1123 nr_arena_item_set_mask(v->arenaitem, NULL);
1124 }
1125 if (!ref) {
1126 display = v->next;
1127 } else {
1128 ref->next = v->next;
1129 }
1130 nr_arena_item_unparent(v->arenaitem);
1131 nr_arena_item_unref(v->arenaitem);
1132 g_free(v);
1133 } else {
1134 ref = v;
1135 }
1136 v = next;
1137 }
1138 }
1140 // Adjusters
1142 void SPItem::adjust_pattern (Geom::Matrix const &postmul, bool set)
1143 {
1144 if (style && (style->fill.isPaintserver())) {
1145 SPObject *server = style->getFillPaintServer();
1146 if ( SP_IS_PATTERN(server) ) {
1147 SPPattern *pattern = sp_pattern_clone_if_necessary(this, SP_PATTERN(server), "fill");
1148 sp_pattern_transform_multiply(pattern, postmul, set);
1149 }
1150 }
1152 if (style && (style->stroke.isPaintserver())) {
1153 SPObject *server = style->getStrokePaintServer();
1154 if ( SP_IS_PATTERN(server) ) {
1155 SPPattern *pattern = sp_pattern_clone_if_necessary(this, SP_PATTERN(server), "stroke");
1156 sp_pattern_transform_multiply(pattern, postmul, set);
1157 }
1158 }
1159 }
1161 void SPItem::adjust_gradient( Geom::Matrix const &postmul, bool set )
1162 {
1163 if ( style && style->fill.isPaintserver() ) {
1164 SPPaintServer *server = style->getFillPaintServer();
1165 if ( SP_IS_GRADIENT(server) ) {
1167 /**
1168 * \note Bbox units for a gradient are generally a bad idea because
1169 * with them, you cannot preserve the relative position of the
1170 * object and its gradient after rotation or skew. So now we
1171 * convert them to userspace units which are easy to keep in sync
1172 * just by adding the object's transform to gradientTransform.
1173 * \todo FIXME: convert back to bbox units after transforming with
1174 * the item, so as to preserve the original units.
1175 */
1176 SPGradient *gradient = sp_gradient_convert_to_userspace( SP_GRADIENT(server), this, "fill" );
1178 sp_gradient_transform_multiply( gradient, postmul, set );
1179 }
1180 }
1182 if ( style && style->stroke.isPaintserver() ) {
1183 SPPaintServer *server = style->getStrokePaintServer();
1184 if ( SP_IS_GRADIENT(server) ) {
1185 SPGradient *gradient = sp_gradient_convert_to_userspace( SP_GRADIENT(server), this, "stroke");
1186 sp_gradient_transform_multiply( gradient, postmul, set );
1187 }
1188 }
1189 }
1191 void SPItem::adjust_stroke( gdouble ex )
1192 {
1193 if ( style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE(ex, 1.0, NR_EPSILON) ) {
1194 style->stroke_width.computed *= ex;
1195 style->stroke_width.set = TRUE;
1197 if ( style->stroke_dash.n_dash != 0 ) {
1198 for (int i = 0; i < style->stroke_dash.n_dash; i++) {
1199 style->stroke_dash.dash[i] *= ex;
1200 }
1201 style->stroke_dash.offset *= ex;
1202 }
1204 updateRepr();
1205 }
1206 }
1208 /**
1209 * Find out the inverse of previous transform of an item (from its repr)
1210 */
1211 Geom::Matrix sp_item_transform_repr (SPItem *item)
1212 {
1213 Geom::Matrix t_old(Geom::identity());
1214 gchar const *t_attr = item->getRepr()->attribute("transform");
1215 if (t_attr) {
1216 Geom::Matrix t;
1217 if (sp_svg_transform_read(t_attr, &t)) {
1218 t_old = t;
1219 }
1220 }
1222 return t_old;
1223 }
1226 /**
1227 * Recursively scale stroke width in \a item and its children by \a expansion.
1228 */
1229 void SPItem::adjust_stroke_width_recursive(double expansion)
1230 {
1231 adjust_stroke (expansion);
1233 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1234 if ( !SP_IS_USE(this) ) {
1235 for ( SPObject *o = children; o; o = o->getNext() ) {
1236 if (SP_IS_ITEM(o)) {
1237 SP_ITEM(o)->adjust_stroke_width_recursive(expansion);
1238 }
1239 }
1240 }
1241 }
1243 /**
1244 * Recursively adjust rx and ry of rects.
1245 */
1246 void
1247 sp_item_adjust_rects_recursive(SPItem *item, Geom::Matrix advertized_transform)
1248 {
1249 if (SP_IS_RECT (item)) {
1250 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1251 }
1253 for (SPObject *o = item->children; o != NULL; o = o->next) {
1254 if (SP_IS_ITEM(o))
1255 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1256 }
1257 }
1259 /**
1260 * Recursively compensate pattern or gradient transform.
1261 */
1262 void SPItem::adjust_paint_recursive (Geom::Matrix advertized_transform, Geom::Matrix t_ancestors, bool is_pattern)
1263 {
1264 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1265 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1266 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1267 Geom::Matrix t_item = sp_item_transform_repr (this);
1268 Geom::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1270 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1271 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1272 // we must not touch it
1273 if (!(this && (SP_IS_TEXT(this) || SP_IS_USE(this)))) {
1274 for (SPObject *o = children; o != NULL; o = o->next) {
1275 if (SP_IS_ITEM(o)) {
1276 // At the level of the transformed item, t_ancestors is identity;
1277 // below it, it is the accmmulated chain of transforms from this level to the top level
1278 SP_ITEM(o)->adjust_paint_recursive (advertized_transform, t_item * t_ancestors, is_pattern);
1279 }
1280 }
1281 }
1283 // We recursed into children first, and are now adjusting this object second;
1284 // this is so that adjustments in a tree are done from leaves up to the root,
1285 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1286 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1288 if (is_pattern) {
1289 adjust_pattern(paint_delta);
1290 } else {
1291 adjust_gradient(paint_delta);
1292 }
1293 }
1295 void SPItem::adjust_livepatheffect (Geom::Matrix const &postmul, bool set)
1296 {
1297 if ( SP_IS_LPE_ITEM(this) ) {
1298 SPLPEItem *lpeitem = SP_LPE_ITEM (this);
1299 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1300 sp_lpe_item_fork_path_effects_if_necessary(lpeitem);
1302 // now that all LPEs are forked_if_necessary, we can apply the transform
1303 PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
1304 for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1305 {
1306 LivePathEffectObject *lpeobj = (*it)->lpeobject;
1307 if (lpeobj && lpeobj->get_lpe()) {
1308 Inkscape::LivePathEffect::Effect * effect = lpeobj->get_lpe();
1309 effect->transform_multiply(postmul, set);
1310 }
1311 }
1312 }
1313 }
1314 }
1316 /**
1317 * Set a new transform on an object.
1318 *
1319 * Compensate for stroke scaling and gradient/pattern fill transform, if
1320 * necessary. Call the object's set_transform method if transforms are
1321 * stored optimized. Send _transformed_signal. Invoke _write method so that
1322 * the repr is updated with the new transform.
1323 */
1324 void SPItem::doWriteTransform(Inkscape::XML::Node *repr, Geom::Matrix const &transform, Geom::Matrix const *adv, bool compensate)
1325 {
1326 g_return_if_fail(repr != NULL);
1328 // calculate the relative transform, if not given by the adv attribute
1329 Geom::Matrix advertized_transform;
1330 if (adv != NULL) {
1331 advertized_transform = *adv;
1332 } else {
1333 advertized_transform = sp_item_transform_repr (this).inverse() * transform;
1334 }
1336 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1337 if (compensate) {
1339 // recursively compensate for stroke scaling, depending on user preference
1340 if (!prefs->getBool("/options/transform/stroke", true)) {
1341 double const expansion = 1. / advertized_transform.descrim();
1342 adjust_stroke_width_recursive(expansion);
1343 }
1345 // recursively compensate rx/ry of a rect if requested
1346 if (!prefs->getBool("/options/transform/rectcorners", true)) {
1347 sp_item_adjust_rects_recursive(this, advertized_transform);
1348 }
1350 // recursively compensate pattern fill if it's not to be transformed
1351 if (!prefs->getBool("/options/transform/pattern", true)) {
1352 adjust_paint_recursive (advertized_transform.inverse(), Geom::identity(), true);
1353 }
1354 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1355 /// recursively compensate gradient fill if it's not to be transformed
1356 if (!prefs->getBool("/options/transform/gradient", true)) {
1357 adjust_paint_recursive (advertized_transform.inverse(), Geom::identity(), false);
1358 } else {
1359 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1360 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1361 adjust_paint_recursive (Geom::identity(), Geom::identity(), false);
1362 }
1364 } // endif(compensate)
1366 gint preserve = prefs->getBool("/options/preservetransform/value", 0);
1367 Geom::Matrix transform_attr (transform);
1368 if ( // run the object's set_transform (i.e. embed transform) only if:
1369 ((SPItemClass *) G_OBJECT_GET_CLASS(this))->set_transform && // it does have a set_transform method
1370 !preserve && // user did not chose to preserve all transforms
1371 !clip_ref->getObject() && // the object does not have a clippath
1372 !mask_ref->getObject() && // the object does not have a mask
1373 !(!transform.isTranslation() && style && style->getFilter())
1374 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1375 ) {
1376 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(this))->set_transform(this, transform);
1377 }
1378 set_item_transform(transform_attr);
1380 // Note: updateRepr comes before emitting the transformed signal since
1381 // it causes clone SPUse's copy of the original object to brought up to
1382 // date with the original. Otherwise, sp_use_bbox returns incorrect
1383 // values if called in code handling the transformed signal.
1384 updateRepr();
1386 // send the relative transform with a _transformed_signal
1387 _transformed_signal.emit(&advertized_transform, this);
1388 }
1390 gint SPItem::emitEvent(SPEvent &event)
1391 {
1392 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->event) {
1393 return ((SPItemClass *) G_OBJECT_GET_CLASS(this))->event(this, &event);
1394 }
1396 return FALSE;
1397 }
1399 /**
1400 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1401 * gradients, patterns as sp_item_write_transform does.
1402 */
1403 void SPItem::set_item_transform(Geom::Matrix const &transform_matrix)
1404 {
1405 if (!matrix_equalp(transform_matrix, transform, NR_EPSILON)) {
1406 transform = transform_matrix;
1407 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1408 transformation. It's apparently not used anywhere else. */
1409 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1410 sp_item_rm_unsatisfied_cns(*this);
1411 }
1412 }
1414 void SPItem::convert_item_to_guides() {
1415 // Use derived method if present ...
1416 if (((SPItemClass *) G_OBJECT_GET_CLASS(this))->convert_to_guides) {
1417 (*((SPItemClass *) G_OBJECT_GET_CLASS(this))->convert_to_guides)(this);
1418 } else {
1419 // .. otherwise simply place the guides around the item's bounding box
1421 convert_to_guides();
1422 }
1423 }
1426 /**
1427 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1428 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1429 */
1430 Geom::Matrix
1431 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1432 Geom::Matrix ret(Geom::identity());
1433 g_return_val_if_fail(object != NULL, ret);
1435 /* stop at first non-renderable ancestor */
1436 while ( object != ancestor && SP_IS_ITEM(object) ) {
1437 if (SP_IS_ROOT(object)) {
1438 ret *= SP_ROOT(object)->c2p;
1439 } else {
1440 ret *= SP_ITEM(object)->transform;
1441 }
1442 object = object->parent;
1443 }
1444 return ret;
1445 }
1447 Geom::Matrix
1448 i2i_affine(SPObject const *src, SPObject const *dest) {
1449 g_return_val_if_fail(src != NULL && dest != NULL, Geom::identity());
1450 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1451 return i2anc_affine(src, ancestor) * i2anc_affine(dest, ancestor).inverse();
1452 }
1454 Geom::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1455 return i2i_affine(this, dest);
1456 }
1458 /**
1459 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1460 * \pre (item != NULL) and SP_IS_ITEM(item).
1461 */
1462 Geom::Matrix SPItem::i2doc_affine() const
1463 {
1464 return i2anc_affine(this, NULL);
1465 }
1467 /**
1468 * Returns the transformation from item to desktop coords
1469 */
1470 Geom::Matrix SPItem::i2d_affine() const
1471 {
1472 Geom::Matrix const ret( i2doc_affine()
1473 * Geom::Scale(1, -1)
1474 * Geom::Translate(0, document->getHeight()) );
1475 return ret;
1476 }
1478 void SPItem::set_i2d_affine(Geom::Matrix const &i2dt)
1479 {
1480 Geom::Matrix dt2p; /* desktop to item parent transform */
1481 if (parent) {
1482 dt2p = static_cast<SPItem *>(parent)->i2d_affine().inverse();
1483 } else {
1484 dt2p = ( Geom::Translate(0, -document->getHeight())
1485 * Geom::Scale(1, -1) );
1486 }
1488 Geom::Matrix const i2p( i2dt * dt2p );
1489 set_item_transform(i2p);
1490 }
1493 /**
1494 * should rather be named "sp_item_d2i_affine" to match "sp_item_i2d_affine" (or vice versa)
1495 */
1496 Geom::Matrix SPItem::dt2i_affine() const
1497 {
1498 /* fixme: Implement the right way (Lauris) */
1499 return i2d_affine().inverse();
1500 }
1502 /* Item views */
1504 SPItemView *SPItem::sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1505 {
1506 g_assert(item != NULL);
1507 g_assert(SP_IS_ITEM(item));
1508 g_assert(arenaitem != NULL);
1509 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1511 SPItemView *new_view = g_new(SPItemView, 1);
1513 new_view->next = list;
1514 new_view->flags = flags;
1515 new_view->key = key;
1516 new_view->arenaitem = arenaitem;
1518 return new_view;
1519 }
1521 SPItemView *SPItem::sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1522 {
1523 if (view == list) {
1524 list = list->next;
1525 } else {
1526 SPItemView *prev;
1527 prev = list;
1528 while (prev->next != view) prev = prev->next;
1529 prev->next = view->next;
1530 }
1532 nr_arena_item_unref(view->arenaitem);
1533 g_free(view);
1535 return list;
1536 }
1538 /**
1539 * Return the arenaitem corresponding to the given item in the display
1540 * with the given key
1541 */
1542 NRArenaItem *SPItem::get_arenaitem(unsigned key)
1543 {
1544 for ( SPItemView *iv = display ; iv ; iv = iv->next ) {
1545 if ( iv->key == key ) {
1546 return iv->arenaitem;
1547 }
1548 }
1550 return NULL;
1551 }
1553 int sp_item_repr_compare_position(SPItem *first, SPItem *second)
1554 {
1555 return sp_repr_compare_position(first->getRepr(),
1556 second->getRepr());
1557 }
1559 SPItem *sp_item_first_item_child(SPObject *obj)
1560 {
1561 SPItem *child = 0;
1562 for ( SPObject *iter = obj->firstChild() ; iter ; iter = iter->next ) {
1563 if ( SP_IS_ITEM(iter) ) {
1564 child = SP_ITEM(iter);
1565 break;
1566 }
1567 }
1568 return child;
1569 }
1571 void SPItem::convert_to_guides() {
1572 SPDesktop *dt = inkscape_active_desktop();
1573 sp_desktop_namedview(dt);
1575 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1576 int prefs_bbox = prefs->getInt("/tools/bounding_box", 0);
1577 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1578 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1580 Geom::OptRect bbox = getBboxDesktop(bbox_type);
1581 if (!bbox) {
1582 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1583 return;
1584 }
1586 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1588 Geom::Point A((*bbox).min());
1589 Geom::Point C((*bbox).max());
1590 Geom::Point B(A[Geom::X], C[Geom::Y]);
1591 Geom::Point D(C[Geom::X], A[Geom::Y]);
1593 pts.push_back(std::make_pair(A, B));
1594 pts.push_back(std::make_pair(B, C));
1595 pts.push_back(std::make_pair(C, D));
1596 pts.push_back(std::make_pair(D, A));
1598 sp_guide_pt_pairs_to_guides(dt, pts);
1599 }
1601 /*
1602 Local Variables:
1603 mode:c++
1604 c-file-style:"stroustrup"
1605 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1606 indent-tabs-mode:nil
1607 fill-column:99
1608 End:
1609 */
1610 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :