706469d3cad42440c3d846f5630d338e4befb96e
1 #define __SP_ITEM_C__
3 /** \file
4 * Base class for visual SVG elements
5 */
6 /*
7 * Authors:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * bulia byak <buliabyak@users.sf.net>
10 * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
11 *
12 * Copyright (C) 2001-2006 authors
13 * Copyright (C) 2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 /** \class SPItem
19 *
20 * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
21 * is a subclass of SPObject, with great deal of specific functionality.
22 */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
29 #include "sp-item.h"
30 #include "svg/svg.h"
31 #include "print.h"
32 #include "display/nr-arena.h"
33 #include "display/nr-arena-item.h"
34 #include "attributes.h"
35 #include "document.h"
36 #include "uri.h"
37 #include "inkscape.h"
38 #include "desktop-handles.h"
40 #include "style.h"
41 #include <glibmm/i18n.h>
42 #include "sp-root.h"
43 #include "sp-clippath.h"
44 #include "sp-mask.h"
45 #include "sp-rect.h"
46 #include "sp-use.h"
47 #include "sp-text.h"
48 #include "sp-item-rm-unsatisfied-cns.h"
49 #include "sp-pattern.h"
50 #include "sp-switch.h"
51 #include "gradient-chemistry.h"
52 #include "prefs-utils.h"
53 #include "conn-avoid-ref.h"
54 #include "conditions.h"
55 #include "sp-filter-reference.h"
56 #include "filter-chemistry.h"
57 #include "sp-guide.h"
58 #include "sp-title.h"
59 #include "sp-desc.h"
61 #include "libnr/nr-matrix-fns.h"
62 #include "libnr/nr-matrix-scale-ops.h"
63 #include "libnr/nr-matrix-translate-ops.h"
64 #include "libnr/nr-scale-translate-ops.h"
65 #include "libnr/nr-translate-scale-ops.h"
66 #include "libnr/nr-convert2geom.h"
67 #include "algorithms/find-last-if.h"
68 #include "util/reverse-list.h"
69 #include <2geom/rect.h>
70 #include <2geom/transforms.h>
72 #include "xml/repr.h"
73 #include "extract-uri.h"
75 #include "live_effects/lpeobject.h"
76 #include "live_effects/effect.h"
77 #include "live_effects/lpeobject-reference.h"
79 #define noSP_ITEM_DEBUG_IDLE
81 static void sp_item_class_init(SPItemClass *klass);
82 static void sp_item_init(SPItem *item);
84 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
85 static void sp_item_release(SPObject *object);
86 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
87 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
88 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
90 static gchar *sp_item_private_description(SPItem *item);
91 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
93 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
94 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
96 static SPObjectClass *parent_class;
98 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
99 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
101 /**
102 * Registers SPItem class and returns its type number.
103 */
104 GType
105 sp_item_get_type(void)
106 {
107 static GType type = 0;
108 if (!type) {
109 GTypeInfo info = {
110 sizeof(SPItemClass),
111 NULL, NULL,
112 (GClassInitFunc) sp_item_class_init,
113 NULL, NULL,
114 sizeof(SPItem),
115 16,
116 (GInstanceInitFunc) sp_item_init,
117 NULL, /* value_table */
118 };
119 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
120 }
121 return type;
122 }
124 /**
125 * SPItem vtable initialization.
126 */
127 static void
128 sp_item_class_init(SPItemClass *klass)
129 {
130 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
132 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
134 sp_object_class->build = sp_item_build;
135 sp_object_class->release = sp_item_release;
136 sp_object_class->set = sp_item_set;
137 sp_object_class->update = sp_item_update;
138 sp_object_class->write = sp_item_write;
140 klass->description = sp_item_private_description;
141 klass->snappoints = sp_item_private_snappoints;
142 }
144 /**
145 * Callback for SPItem object initialization.
146 */
147 static void
148 sp_item_init(SPItem *item)
149 {
150 item->init();
151 }
153 void SPItem::init() {
154 this->sensitive = TRUE;
156 this->transform_center_x = 0;
157 this->transform_center_y = 0;
159 this->_is_evaluated = true;
160 this->_evaluated_status = StatusUnknown;
162 this->transform = NR::identity();
164 this->display = NULL;
166 this->clip_ref = new SPClipPathReference(this);
167 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
168 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
169 _clip_ref_connection = cs1.connect(sl1);
171 this->mask_ref = new SPMaskReference(this);
172 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
173 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
174 _mask_ref_connection = cs2.connect(sl2);
176 this->avoidRef = new SPAvoidRef(this);
178 new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
179 }
181 bool SPItem::isVisibleAndUnlocked() const {
182 return (!isHidden() && !isLocked());
183 }
185 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
186 return (!isHidden(display_key) && !isLocked());
187 }
189 bool SPItem::isLocked() const {
190 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
191 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
192 return true;
193 }
194 return false;
195 }
197 void SPItem::setLocked(bool locked) {
198 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
199 ( locked ? "1" : NULL ));
200 updateRepr();
201 }
203 bool SPItem::isHidden() const {
204 if (!isEvaluated())
205 return true;
206 return style->display.computed == SP_CSS_DISPLAY_NONE;
207 }
209 void SPItem::setHidden(bool hide) {
210 style->display.set = TRUE;
211 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
212 style->display.computed = style->display.value;
213 style->display.inherit = FALSE;
214 updateRepr();
215 }
217 bool SPItem::isHidden(unsigned display_key) const {
218 if (!isEvaluated())
219 return true;
220 for ( SPItemView *view(display) ; view ; view = view->next ) {
221 if ( view->key == display_key ) {
222 g_assert(view->arenaitem != NULL);
223 for ( NRArenaItem *arenaitem = view->arenaitem ;
224 arenaitem ; arenaitem = arenaitem->parent )
225 {
226 if (!arenaitem->visible) {
227 return true;
228 }
229 }
230 return false;
231 }
232 }
233 return true;
234 }
236 void SPItem::setEvaluated(bool evaluated) {
237 _is_evaluated = evaluated;
238 _evaluated_status = StatusSet;
239 }
241 void SPItem::resetEvaluated() {
242 if ( StatusCalculated == _evaluated_status ) {
243 _evaluated_status = StatusUnknown;
244 bool oldValue = _is_evaluated;
245 if ( oldValue != isEvaluated() ) {
246 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
247 }
248 } if ( StatusSet == _evaluated_status ) {
249 SPObject const *const parent = SP_OBJECT_PARENT(this);
250 if (SP_IS_SWITCH(parent)) {
251 SP_SWITCH(parent)->resetChildEvaluated();
252 }
253 }
254 }
256 bool SPItem::isEvaluated() const {
257 if ( StatusUnknown == _evaluated_status ) {
258 _is_evaluated = sp_item_evaluate(this);
259 _evaluated_status = StatusCalculated;
260 }
261 return _is_evaluated;
262 }
264 /**
265 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
266 * Corresponds to setExplicitlyHidden.
267 */
268 bool
269 SPItem::isExplicitlyHidden() const
270 {
271 return (this->style->display.set
272 && this->style->display.value == SP_CSS_DISPLAY_NONE);
273 }
275 /**
276 * Sets the display CSS property to `hidden' if \a val is true,
277 * otherwise makes it unset
278 */
279 void
280 SPItem::setExplicitlyHidden(bool const val) {
281 this->style->display.set = val;
282 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
283 this->style->display.computed = this->style->display.value;
284 this->updateRepr();
285 }
287 /**
288 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
289 */
290 void
291 SPItem::setCenter(NR::Point object_centre) {
292 boost::optional<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
293 if (bbox) {
294 transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
295 if (fabs(transform_center_x) < 1e-5) // rounding error
296 transform_center_x = 0;
297 transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
298 if (fabs(transform_center_y) < 1e-5) // rounding error
299 transform_center_y = 0;
300 }
301 }
303 void
304 SPItem::unsetCenter() {
305 transform_center_x = 0;
306 transform_center_y = 0;
307 }
309 bool SPItem::isCenterSet() {
310 return (transform_center_x != 0 || transform_center_y != 0);
311 }
313 NR::Point SPItem::getCenter() const {
314 boost::optional<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
315 if (bbox) {
316 return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
317 } else {
318 return NR::Point (0, 0); // something's wrong!
319 }
320 }
323 namespace {
325 bool is_item(SPObject const &object) {
326 return SP_IS_ITEM(&object);
327 }
329 }
331 void SPItem::raiseToTop() {
332 using Inkscape::Algorithms::find_last_if;
334 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
335 SP_OBJECT_NEXT(this), NULL, &is_item
336 );
337 if (topmost) {
338 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
339 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
340 }
341 }
343 void SPItem::raiseOne() {
344 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
345 SP_OBJECT_NEXT(this), NULL, &is_item
346 );
347 if (next_higher) {
348 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
349 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
350 sp_repr_parent(repr)->changeOrder(repr, ref);
351 }
352 }
354 void SPItem::lowerOne() {
355 using Inkscape::Util::MutableList;
356 using Inkscape::Util::reverse_list;
358 MutableList<SPObject &> next_lower=std::find_if(
359 reverse_list<SPObject::SiblingIterator>(
360 SP_OBJECT_PARENT(this)->firstChild(), this
361 ),
362 MutableList<SPObject &>(),
363 &is_item
364 );
365 if (next_lower) {
366 ++next_lower;
367 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
368 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
369 sp_repr_parent(repr)->changeOrder(repr, ref);
370 }
371 }
373 void SPItem::lowerToBottom() {
374 using Inkscape::Algorithms::find_last_if;
375 using Inkscape::Util::MutableList;
376 using Inkscape::Util::reverse_list;
378 MutableList<SPObject &> bottom=find_last_if(
379 reverse_list<SPObject::SiblingIterator>(
380 SP_OBJECT_PARENT(this)->firstChild(), this
381 ),
382 MutableList<SPObject &>(),
383 &is_item
384 );
385 if (bottom) {
386 ++bottom;
387 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
388 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
389 sp_repr_parent(repr)->changeOrder(repr, ref);
390 }
391 }
393 static void
394 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
395 {
396 sp_object_read_attr(object, "style");
397 sp_object_read_attr(object, "transform");
398 sp_object_read_attr(object, "clip-path");
399 sp_object_read_attr(object, "mask");
400 sp_object_read_attr(object, "sodipodi:insensitive");
401 sp_object_read_attr(object, "sodipodi:nonprintable");
402 sp_object_read_attr(object, "inkscape:transform-center-x");
403 sp_object_read_attr(object, "inkscape:transform-center-y");
404 sp_object_read_attr(object, "inkscape:connector-avoid");
406 if (((SPObjectClass *) (parent_class))->build) {
407 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
408 }
409 }
411 static void
412 sp_item_release(SPObject *object)
413 {
414 SPItem *item = (SPItem *) object;
416 item->_clip_ref_connection.disconnect();
417 item->_mask_ref_connection.disconnect();
419 // Note: do this here before the clip_ref is deleted, since calling
420 // sp_document_ensure_up_to_date for triggered routing may reference
421 // the deleted clip_ref.
422 if (item->avoidRef) {
423 delete item->avoidRef;
424 item->avoidRef = NULL;
425 }
427 if (item->clip_ref) {
428 item->clip_ref->detach();
429 delete item->clip_ref;
430 item->clip_ref = NULL;
431 }
433 if (item->mask_ref) {
434 item->mask_ref->detach();
435 delete item->mask_ref;
436 item->mask_ref = NULL;
437 }
439 if (((SPObjectClass *) (parent_class))->release) {
440 ((SPObjectClass *) parent_class)->release(object);
441 }
443 while (item->display) {
444 nr_arena_item_unparent(item->display->arenaitem);
445 item->display = sp_item_view_list_remove(item->display, item->display);
446 }
448 item->_transformed_signal.~signal();
449 }
451 static void
452 sp_item_set(SPObject *object, unsigned key, gchar const *value)
453 {
454 SPItem *item = (SPItem *) object;
456 switch (key) {
457 case SP_ATTR_TRANSFORM: {
458 NR::Matrix t;
459 if (value && sp_svg_transform_read(value, &t)) {
460 sp_item_set_item_transform(item, t);
461 } else {
462 sp_item_set_item_transform(item, NR::identity());
463 }
464 break;
465 }
466 case SP_PROP_CLIP_PATH: {
467 gchar *uri = extract_uri(value);
468 if (uri) {
469 try {
470 item->clip_ref->attach(Inkscape::URI(uri));
471 } catch (Inkscape::BadURIException &e) {
472 g_warning("%s", e.what());
473 item->clip_ref->detach();
474 }
475 g_free(uri);
476 } else {
477 item->clip_ref->detach();
478 }
480 break;
481 }
482 case SP_PROP_MASK: {
483 gchar *uri = extract_uri(value);
484 if (uri) {
485 try {
486 item->mask_ref->attach(Inkscape::URI(uri));
487 } catch (Inkscape::BadURIException &e) {
488 g_warning("%s", e.what());
489 item->mask_ref->detach();
490 }
491 g_free(uri);
492 } else {
493 item->mask_ref->detach();
494 }
496 break;
497 }
498 case SP_ATTR_SODIPODI_INSENSITIVE:
499 item->sensitive = !value;
500 for (SPItemView *v = item->display; v != NULL; v = v->next) {
501 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
502 }
503 break;
504 case SP_ATTR_CONNECTOR_AVOID:
505 item->avoidRef->setAvoid(value);
506 break;
507 case SP_ATTR_TRANSFORM_CENTER_X:
508 if (value) {
509 item->transform_center_x = g_strtod(value, NULL);
510 } else {
511 item->transform_center_x = 0;
512 }
513 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
514 break;
515 case SP_ATTR_TRANSFORM_CENTER_Y:
516 if (value) {
517 item->transform_center_y = g_strtod(value, NULL);
518 } else {
519 item->transform_center_y = 0;
520 }
521 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
522 break;
523 case SP_PROP_SYSTEM_LANGUAGE:
524 case SP_PROP_REQUIRED_FEATURES:
525 case SP_PROP_REQUIRED_EXTENSIONS:
526 {
527 item->resetEvaluated();
528 // pass to default handler
529 }
530 default:
531 if (SP_ATTRIBUTE_IS_CSS(key)) {
532 sp_style_read_from_object(object->style, object);
533 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
534 } else {
535 if (((SPObjectClass *) (parent_class))->set) {
536 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
537 }
538 }
539 break;
540 }
541 }
543 static void
544 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
545 {
546 if (old_clip) {
547 SPItemView *v;
548 /* Hide clippath */
549 for (v = item->display; v != NULL; v = v->next) {
550 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
551 nr_arena_item_set_clip(v->arenaitem, NULL);
552 }
553 }
554 if (SP_IS_CLIPPATH(clip)) {
555 NRRect bbox;
556 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
557 for (SPItemView *v = item->display; v != NULL; v = v->next) {
558 if (!v->arenaitem->key) {
559 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
560 }
561 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
562 NR_ARENA_ITEM_ARENA(v->arenaitem),
563 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
564 nr_arena_item_set_clip(v->arenaitem, ai);
565 nr_arena_item_unref(ai);
566 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
567 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
568 }
569 }
570 }
572 static void
573 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
574 {
575 if (old_mask) {
576 /* Hide mask */
577 for (SPItemView *v = item->display; v != NULL; v = v->next) {
578 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
579 nr_arena_item_set_mask(v->arenaitem, NULL);
580 }
581 }
582 if (SP_IS_MASK(mask)) {
583 NRRect bbox;
584 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
585 for (SPItemView *v = item->display; v != NULL; v = v->next) {
586 if (!v->arenaitem->key) {
587 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
588 }
589 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
590 NR_ARENA_ITEM_ARENA(v->arenaitem),
591 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
592 nr_arena_item_set_mask(v->arenaitem, ai);
593 nr_arena_item_unref(ai);
594 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
595 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
596 }
597 }
598 }
600 static void
601 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
602 {
603 SPItem *item = SP_ITEM(object);
605 if (((SPObjectClass *) (parent_class))->update)
606 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
608 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
609 if (flags & SP_OBJECT_MODIFIED_FLAG) {
610 for (SPItemView *v = item->display; v != NULL; v = v->next) {
611 nr_arena_item_set_transform(v->arenaitem, item->transform);
612 }
613 }
615 SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
616 SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
618 if ( clip_path || mask ) {
619 NRRect bbox;
620 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
621 if (clip_path) {
622 for (SPItemView *v = item->display; v != NULL; v = v->next) {
623 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
624 }
625 }
626 if (mask) {
627 for (SPItemView *v = item->display; v != NULL; v = v->next) {
628 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
629 }
630 }
631 }
633 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
634 for (SPItemView *v = item->display; v != NULL; v = v->next) {
635 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
636 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
637 }
638 }
639 }
641 /* Update bounding box data used by filters */
642 if (item->style->filter.set && item->display) {
643 NRRect item_bbox;
644 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
645 boost::optional<NR::Rect> i_bbox = item_bbox;
647 SPItemView *itemview = item->display;
648 do {
649 if (itemview->arenaitem)
650 nr_arena_item_set_item_bbox(itemview->arenaitem, i_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 boost::optional<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
723 SPItem::BBoxType type,
724 unsigned int /*dkey*/) const
725 {
726 boost::optional<NR::Rect> r;
727 sp_item_invoke_bbox_full(this, r, transform, type, TRUE);
728 return r;
729 }
731 void
732 sp_item_invoke_bbox(SPItem const *item, boost::optional<NR::Rect> &bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
733 {
734 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
735 }
737 // DEPRECATED to phase out the use of NRRect in favor of boost::optional<NR::Rect>
738 void
739 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
740 {
741 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
742 }
744 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
745 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
746 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
747 * clones), in turn, call this function in their bbox methods. */
748 void
749 sp_item_invoke_bbox_full(SPItem const *item, boost::optional<NR::Rect> &bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
750 {
751 g_assert(item != NULL);
752 g_assert(SP_IS_ITEM(item));
754 if (clear) {
755 bbox = boost::optional<NR::Rect>();
756 }
758 // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
760 NRRect temp_bbox;
761 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
762 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
764 // call the subclass method
765 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
766 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
767 }
769 // unless this is geometric bbox, extend by filter area and crop the bbox by clip path, if any
770 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
771 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
772 SPObject *filter = SP_OBJECT_STYLE(item)->getFilter();
773 if (filter && SP_IS_FILTER(filter)) {
774 // default filer area per the SVG spec:
775 double x = -0.1;
776 double y = -0.1;
777 double w = 1.2;
778 double h = 1.2;
780 // if area is explicitly set, override:
781 if (SP_FILTER(filter)->x._set)
782 x = SP_FILTER(filter)->x.computed;
783 if (SP_FILTER(filter)->y._set)
784 y = SP_FILTER(filter)->y.computed;
785 if (SP_FILTER(filter)->width._set)
786 w = SP_FILTER(filter)->width.computed;
787 if (SP_FILTER(filter)->height._set)
788 h = SP_FILTER(filter)->height.computed;
790 double dx0 = 0;
791 double dx1 = 0;
792 double dy0 = 0;
793 double dy1 = 0;
794 if (filter_is_single_gaussian_blur(SP_FILTER(filter))) {
795 // if this is a single blur, use 2.4*radius
796 // which may be smaller than the default area;
797 // see set_filter_area for why it's 2.4
798 double r = get_single_gaussian_blur_radius (SP_FILTER(filter));
799 dx0 = -2.4 * r;
800 dx1 = 2.4 * r;
801 dy0 = -2.4 * r;
802 dy1 = 2.4 * r;
803 } else {
804 // otherwise, calculate expansion from relative to absolute units:
805 dx0 = x * (temp_bbox.x1 - temp_bbox.x0);
806 dx1 = (w + x - 1) * (temp_bbox.x1 - temp_bbox.x0);
807 dy0 = y * (temp_bbox.y1 - temp_bbox.y0);
808 dy1 = (h + y - 1) * (temp_bbox.y1 - temp_bbox.y0);
809 }
811 // transform the expansions by the item's transform:
812 NR::Matrix i2d(sp_item_i2d_affine (item));
813 dx0 *= NR::expansionX(i2d);
814 dx1 *= NR::expansionX(i2d);
815 dy0 *= NR::expansionY(i2d);
816 dy1 *= NR::expansionY(i2d);
818 // expand the bbox
819 temp_bbox.x0 += dx0;
820 temp_bbox.x1 += dx1;
821 temp_bbox.y0 += dy0;
822 temp_bbox.y1 += dy1;
823 }
824 }
825 if (item->clip_ref->getObject()) {
826 NRRect b;
827 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
828 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
829 }
830 }
832 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
833 // Either the bbox hasn't been touched by the SPItemClass' bbox method
834 // (it still has its initial values, see above: x0 = y0 = NR_HUGE and x1 = y1 = -NR_HUGE)
835 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
837 // When x0 > x1 or y0 > y1, the bbox is considered to be "nothing", although it has not been
838 // explicitely defined this way for NRRects (as opposed to boost::optional<NR::Rect>)
839 // So union bbox with nothing = do nothing, just return
840 return;
841 }
843 // Do not use temp_bbox.upgrade() here, because it uses a test that returns an empty boost::optional<NR::Rect>()
844 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
845 // would therefore be translated into empty boost::optional<NR::Rect>() (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
846 boost::optional<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
848 bbox = NR::union_bounds(bbox, temp_bbox_new);
849 }
851 // DEPRECATED to phase out the use of NRRect in favor of boost::optional<NR::Rect>
852 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
853 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
854 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
855 * clones), in turn, call this function in their bbox methods. */
856 void
857 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
858 {
859 g_assert(item != NULL);
860 g_assert(SP_IS_ITEM(item));
861 g_assert(bbox != NULL);
863 if (clear) {
864 bbox->x0 = bbox->y0 = 1e18;
865 bbox->x1 = bbox->y1 = -1e18;
866 }
868 NRRect this_bbox;
869 this_bbox.x0 = this_bbox.y0 = 1e18;
870 this_bbox.x1 = this_bbox.y1 = -1e18;
872 // call the subclass method
873 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
874 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
875 }
877 // unless this is geometric bbox, crop the bbox by clip path, if any
878 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
879 NRRect b;
880 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
881 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
882 }
884 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
885 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
886 nr_rect_d_union (bbox, bbox, &this_bbox);
887 }
888 }
890 unsigned sp_item_pos_in_parent(SPItem *item)
891 {
892 g_assert(item != NULL);
893 g_assert(SP_IS_ITEM(item));
895 SPObject *parent = SP_OBJECT_PARENT(item);
896 g_assert(parent != NULL);
897 g_assert(SP_IS_OBJECT(parent));
899 SPObject *object = SP_OBJECT(item);
901 unsigned pos=0;
902 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
903 if ( iter == object ) {
904 return pos;
905 }
906 if (SP_IS_ITEM(iter)) {
907 pos++;
908 }
909 }
911 g_assert_not_reached();
912 return 0;
913 }
915 void
916 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
917 {
918 g_assert(item != NULL);
919 g_assert(SP_IS_ITEM(item));
920 g_assert(bbox != NULL);
922 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
923 }
925 boost::optional<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
926 {
927 boost::optional<NR::Rect> rect = boost::optional<NR::Rect>();
928 sp_item_invoke_bbox(item, rect, sp_item_i2d_affine(item), TRUE, type);
929 return rect;
930 }
932 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
933 {
934 boost::optional<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
935 /* Just the corners of the bounding box suffices given that we don't yet
936 support angled guide lines. */
938 if (bbox) {
939 NR::Point p1, p2;
940 p1 = bbox->min();
941 p2 = bbox->max();
942 *p = p1;
943 *p = NR::Point(p1[NR::X], p2[NR::Y]);
944 *p = p2;
945 *p = NR::Point(p1[NR::Y], p2[NR::X]);
946 }
947 }
949 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
950 {
951 g_assert (item != NULL);
952 g_assert (SP_IS_ITEM(item));
954 // Get the snappoints of the item
955 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
956 if (item_class.snappoints) {
957 item_class.snappoints(item, p);
958 }
960 // Get the snappoints at the item's center
961 if (includeItemCenter) {
962 *p = item->getCenter();
963 }
965 // Get the snappoints of clipping paths and mask, if any
966 std::list<SPObject const *> clips_and_masks;
968 clips_and_masks.push_back(SP_OBJECT(item->clip_ref->getObject()));
969 clips_and_masks.push_back(SP_OBJECT(item->mask_ref->getObject()));
971 for (std::list<SPObject const *>::const_iterator o = clips_and_masks.begin(); o != clips_and_masks.end(); o++) {
972 if (*o) {
973 // obj is a group object, the children are the actual clippers
974 for (SPObject *child = (*o)->children ; child ; child = child->next) {
975 if (SP_IS_ITEM(child)) {
976 std::vector<NR::Point> p_clip_or_mask;
977 // Please note the recursive call here!
978 sp_item_snappoints(SP_ITEM(child), includeItemCenter, SnapPointsIter(p_clip_or_mask));
979 // Take into account the transformation of the item being clipped or masked
980 for (std::vector<NR::Point>::const_iterator p_orig = p_clip_or_mask.begin(); p_orig != p_clip_or_mask.end(); p_orig++) {
981 // All snappoints are in desktop coordinates, but the item's transformation is
982 // in document coordinates. Hence the awkward construction below
983 *p = (*p_orig) * from_2geom(matrix_to_desktop (matrix_from_desktop (item->transform, item), item));
984 }
985 }
986 }
987 }
988 }
989 }
991 void
992 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
993 {
994 if (!item->isHidden()) {
995 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
996 if (!item->transform.test_identity()
997 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
998 {
999 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1000 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1001 sp_print_release(ctx);
1002 } else {
1003 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
1004 }
1005 }
1006 }
1007 }
1009 static gchar *
1010 sp_item_private_description(SPItem */*item*/)
1011 {
1012 return g_strdup(_("Object"));
1013 }
1015 /**
1016 * Returns a string suitable for status bar, formatted in pango markup language.
1017 *
1018 * Must be freed by caller.
1019 */
1020 gchar *
1021 sp_item_description(SPItem *item)
1022 {
1023 g_assert(item != NULL);
1024 g_assert(SP_IS_ITEM(item));
1026 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
1027 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
1028 if (s && item->clip_ref->getObject()) {
1029 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
1030 g_free (s);
1031 s = snew;
1032 }
1033 if (s && item->mask_ref->getObject()) {
1034 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
1035 g_free (s);
1036 s = snew;
1037 }
1038 if (SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->filter.href) {
1039 gchar *snew = g_strdup_printf (_("%s; <i>filtered</i>"), s);
1040 g_free (s);
1041 s = snew;
1042 }
1043 return s;
1044 }
1046 g_assert_not_reached();
1047 return NULL;
1048 }
1050 /**
1051 * Allocates unique integer keys.
1052 * \param numkeys Number of keys required.
1053 * \return First allocated key; hence if the returned key is n
1054 * you can use n, n + 1, ..., n + (numkeys - 1)
1055 */
1056 unsigned
1057 sp_item_display_key_new(unsigned numkeys)
1058 {
1059 static unsigned dkey = 0;
1061 dkey += numkeys;
1063 return dkey - numkeys;
1064 }
1066 NRArenaItem *
1067 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
1068 {
1069 g_assert(item != NULL);
1070 g_assert(SP_IS_ITEM(item));
1071 g_assert(arena != NULL);
1072 g_assert(NR_IS_ARENA(arena));
1074 NRArenaItem *ai = NULL;
1075 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
1076 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
1077 }
1079 if (ai != NULL) {
1080 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
1081 nr_arena_item_set_transform(ai, item->transform);
1082 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
1083 nr_arena_item_set_visible(ai, !item->isHidden());
1084 nr_arena_item_set_sensitive(ai, item->sensitive);
1085 if (item->clip_ref->getObject()) {
1086 SPClipPath *cp = item->clip_ref->getObject();
1088 if (!item->display->arenaitem->key) {
1089 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1090 }
1091 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1093 // Show and set clip
1094 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
1095 nr_arena_item_set_clip(ai, ac);
1096 nr_arena_item_unref(ac);
1098 // Update bbox, in case the clip uses bbox units
1099 NRRect bbox;
1100 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1101 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
1102 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1103 }
1104 if (item->mask_ref->getObject()) {
1105 SPMask *mask = item->mask_ref->getObject();
1107 if (!item->display->arenaitem->key) {
1108 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
1109 }
1110 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
1112 // Show and set mask
1113 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1114 nr_arena_item_set_mask(ai, ac);
1115 nr_arena_item_unref(ac);
1117 // Update bbox, in case the mask uses bbox units
1118 NRRect bbox;
1119 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1120 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1121 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1122 }
1123 NR_ARENA_ITEM_SET_DATA(ai, item);
1124 NRRect item_bbox;
1125 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1126 boost::optional<NR::Rect> i_bbox = item_bbox;
1127 nr_arena_item_set_item_bbox(ai, i_bbox);
1128 }
1130 return ai;
1131 }
1133 void
1134 sp_item_invoke_hide(SPItem *item, unsigned key)
1135 {
1136 g_assert(item != NULL);
1137 g_assert(SP_IS_ITEM(item));
1139 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1140 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1141 }
1143 SPItemView *ref = NULL;
1144 SPItemView *v = item->display;
1145 while (v != NULL) {
1146 SPItemView *next = v->next;
1147 if (v->key == key) {
1148 if (item->clip_ref->getObject()) {
1149 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1150 nr_arena_item_set_clip(v->arenaitem, NULL);
1151 }
1152 if (item->mask_ref->getObject()) {
1153 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1154 nr_arena_item_set_mask(v->arenaitem, NULL);
1155 }
1156 if (!ref) {
1157 item->display = v->next;
1158 } else {
1159 ref->next = v->next;
1160 }
1161 nr_arena_item_unparent(v->arenaitem);
1162 nr_arena_item_unref(v->arenaitem);
1163 g_free(v);
1164 } else {
1165 ref = v;
1166 }
1167 v = next;
1168 }
1169 }
1171 // Adjusters
1173 void
1174 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1175 {
1176 SPStyle *style = SP_OBJECT_STYLE (item);
1178 if (style && (style->fill.isPaintserver())) {
1179 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1180 if (SP_IS_PATTERN (server)) {
1181 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1182 sp_pattern_transform_multiply (pattern, postmul, set);
1183 }
1184 }
1186 if (style && (style->stroke.isPaintserver())) {
1187 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1188 if (SP_IS_PATTERN (server)) {
1189 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1190 sp_pattern_transform_multiply (pattern, postmul, set);
1191 }
1192 }
1194 }
1196 void
1197 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1198 {
1199 SPStyle *style = SP_OBJECT_STYLE (item);
1201 if (style && (style->fill.isPaintserver())) {
1202 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1203 if (SP_IS_GRADIENT (server)) {
1205 /**
1206 * \note Bbox units for a gradient are generally a bad idea because
1207 * with them, you cannot preserve the relative position of the
1208 * object and its gradient after rotation or skew. So now we
1209 * convert them to userspace units which are easy to keep in sync
1210 * just by adding the object's transform to gradientTransform.
1211 * \todo FIXME: convert back to bbox units after transforming with
1212 * the item, so as to preserve the original units.
1213 */
1214 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1216 sp_gradient_transform_multiply (gradient, postmul, set);
1217 }
1218 }
1220 if (style && (style->stroke.isPaintserver())) {
1221 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1222 if (SP_IS_GRADIENT (server)) {
1223 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1224 sp_gradient_transform_multiply (gradient, postmul, set);
1225 }
1226 }
1227 }
1229 void
1230 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1231 {
1232 SPStyle *style = SP_OBJECT_STYLE (item);
1234 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1236 style->stroke_width.computed *= ex;
1237 style->stroke_width.set = TRUE;
1239 if (style->stroke_dash.n_dash != 0) {
1240 int i;
1241 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1242 style->stroke_dash.dash[i] *= ex;
1243 }
1244 style->stroke_dash.offset *= ex;
1245 }
1247 SP_OBJECT(item)->updateRepr();
1248 }
1249 }
1251 /**
1252 * Find out the inverse of previous transform of an item (from its repr)
1253 */
1254 NR::Matrix
1255 sp_item_transform_repr (SPItem *item)
1256 {
1257 NR::Matrix t_old(NR::identity());
1258 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1259 if (t_attr) {
1260 NR::Matrix t;
1261 if (sp_svg_transform_read(t_attr, &t)) {
1262 t_old = t;
1263 }
1264 }
1266 return t_old;
1267 }
1270 /**
1271 * Recursively scale stroke width in \a item and its children by \a expansion.
1272 */
1273 void
1274 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1275 {
1276 sp_item_adjust_stroke (item, expansion);
1278 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1279 if (item && SP_IS_USE(item))
1280 return;
1282 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1283 if (SP_IS_ITEM(o))
1284 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1285 }
1286 }
1288 /**
1289 * Recursively adjust rx and ry of rects.
1290 */
1291 void
1292 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1293 {
1294 if (SP_IS_RECT (item)) {
1295 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1296 }
1298 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1299 if (SP_IS_ITEM(o))
1300 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1301 }
1302 }
1304 /**
1305 * Recursively compensate pattern or gradient transform.
1306 */
1307 void
1308 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1309 {
1310 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1311 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1312 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1313 NR::Matrix t_item = sp_item_transform_repr (item);
1314 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1316 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1317 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1318 // we must not touch it
1319 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1320 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1321 if (SP_IS_ITEM(o)) {
1322 // At the level of the transformed item, t_ancestors is identity;
1323 // below it, it is the accmmulated chain of transforms from this level to the top level
1324 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1325 }
1326 }
1327 }
1329 // We recursed into children first, and are now adjusting this object second;
1330 // this is so that adjustments in a tree are done from leaves up to the root,
1331 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1332 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1334 if (is_pattern)
1335 sp_item_adjust_pattern (item, paint_delta);
1336 else
1337 sp_item_adjust_gradient (item, paint_delta);
1339 }
1341 void
1342 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1343 {
1344 if ( !SP_IS_LPE_ITEM(item) )
1345 return;
1347 SPLPEItem *lpeitem = SP_LPE_ITEM (item);
1348 if ( sp_lpe_item_has_path_effect(lpeitem) ) {
1349 PathEffectList effect_list = sp_lpe_item_get_effect_list(lpeitem);
1350 for (PathEffectList::iterator it = effect_list.begin(); it != effect_list.end(); it++)
1351 {
1352 // If the path effect is used by 2 or more items, fork it
1353 // so that each object has its own independent copy of the effect
1354 LivePathEffectObject *lpeobj = (*it)->lpeobject;
1355 if (lpeobj) {
1356 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1357 if (new_lpeobj != lpeobj) {
1358 sp_lpe_item_replace_path_effect(lpeitem, lpeobj, new_lpeobj);
1359 }
1361 if (lpeobj->lpe) {
1362 Inkscape::LivePathEffect::Effect * effect = lpeobj->lpe;
1363 effect->transform_multiply(postmul, set);
1364 }
1365 }
1366 }
1367 }
1368 }
1370 /**
1371 * A temporary wrapper for the next function accepting the NR::Matrix
1372 * instead of NR::Matrix
1373 */
1374 void
1375 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const *transform, NR::Matrix const *adv)
1376 {
1377 if (transform == NULL)
1378 sp_item_write_transform(item, repr, NR::identity(), adv);
1379 else
1380 sp_item_write_transform(item, repr, *transform, adv);
1381 }
1383 /**
1384 * Set a new transform on an object.
1385 *
1386 * Compensate for stroke scaling and gradient/pattern fill transform, if
1387 * necessary. Call the object's set_transform method if transforms are
1388 * stored optimized. Send _transformed_signal. Invoke _write method so that
1389 * the repr is updated with the new transform.
1390 */
1391 void
1392 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1393 {
1394 g_return_if_fail(item != NULL);
1395 g_return_if_fail(SP_IS_ITEM(item));
1396 g_return_if_fail(repr != NULL);
1398 // calculate the relative transform, if not given by the adv attribute
1399 NR::Matrix advertized_transform;
1400 if (adv != NULL) {
1401 advertized_transform = *adv;
1402 } else {
1403 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1404 }
1406 if (compensate) {
1408 // recursively compensate for stroke scaling, depending on user preference
1409 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1410 double const expansion = 1. / NR::expansion(advertized_transform);
1411 sp_item_adjust_stroke_width_recursive(item, expansion);
1412 }
1414 // recursively compensate rx/ry of a rect if requested
1415 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1416 sp_item_adjust_rects_recursive(item, advertized_transform);
1417 }
1419 // recursively compensate pattern fill if it's not to be transformed
1420 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1421 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1422 }
1423 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1424 /// recursively compensate gradient fill if it's not to be transformed
1425 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1426 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1427 } else {
1428 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1429 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1430 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1431 }
1433 } // endif(compensate)
1435 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1436 NR::Matrix transform_attr (transform);
1437 if ( // run the object's set_transform (i.e. embed transform) only if:
1438 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1439 !preserve && // user did not chose to preserve all transforms
1440 !item->clip_ref->getObject() && // the object does not have a clippath
1441 !item->mask_ref->getObject() && // the object does not have a mask
1442 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1443 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1444 ) {
1445 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1446 }
1447 sp_item_set_item_transform(item, transform_attr);
1449 // Note: updateRepr comes before emitting the transformed signal since
1450 // it causes clone SPUse's copy of the original object to brought up to
1451 // date with the original. Otherwise, sp_use_bbox returns incorrect
1452 // values if called in code handling the transformed signal.
1453 SP_OBJECT(item)->updateRepr();
1455 // send the relative transform with a _transformed_signal
1456 item->_transformed_signal.emit(&advertized_transform, item);
1457 }
1459 gint
1460 sp_item_event(SPItem *item, SPEvent *event)
1461 {
1462 g_return_val_if_fail(item != NULL, FALSE);
1463 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1464 g_return_val_if_fail(event != NULL, FALSE);
1466 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1467 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1469 return FALSE;
1470 }
1472 /**
1473 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1474 * gradients, patterns as sp_item_write_transform does.
1475 */
1476 void
1477 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1478 {
1479 g_return_if_fail(item != NULL);
1480 g_return_if_fail(SP_IS_ITEM(item));
1482 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1483 item->transform = transform;
1484 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1485 transformation. It's apparently not used anywhere else. */
1486 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1487 sp_item_rm_unsatisfied_cns(*item);
1488 }
1489 }
1491 void
1492 sp_item_convert_item_to_guides(SPItem *item) {
1493 g_return_if_fail(item != NULL);
1494 g_return_if_fail(SP_IS_ITEM(item));
1496 /* Use derived method if present ... */
1497 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides) {
1498 (*((SPItemClass *) G_OBJECT_GET_CLASS(item))->convert_to_guides)(item);
1499 return;
1500 }
1502 /* .. otherwise simply place the guides around the item's bounding box */
1504 sp_item_convert_to_guides(item);
1505 }
1508 /**
1509 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1510 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1511 */
1512 Geom::Matrix
1513 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1514 Geom::Matrix ret(Geom::identity());
1515 g_return_val_if_fail(object != NULL, ret);
1517 /* stop at first non-renderable ancestor */
1518 while ( object != ancestor && SP_IS_ITEM(object) ) {
1519 if (SP_IS_ROOT(object)) {
1520 ret *= SP_ROOT(object)->c2p;
1521 }
1522 ret *= SP_ITEM(object)->transform;
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 NR::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 accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1550 * Used in path operations mostly.
1551 * \pre (item != NULL) and SP_IS_ITEM(item).
1552 */
1553 Geom::Matrix sp_item_i2root_affine(SPItem const *item)
1554 {
1555 g_assert(item != NULL);
1556 g_assert(SP_IS_ITEM(item));
1558 Geom::Matrix ret(Geom::identity());
1559 g_assert(ret.isIdentity());
1560 while ( NULL != SP_OBJECT_PARENT(item) ) {
1561 ret *= item->transform;
1562 item = SP_ITEM(SP_OBJECT_PARENT(item));
1563 }
1564 g_assert(SP_IS_ROOT(item));
1566 ret *= item->transform;
1568 return ret;
1569 }
1571 /* fixme: This is EVIL!!! */
1572 // fix this note: why/what evil? :)
1573 Geom::Matrix sp_item_i2d_affine(SPItem const *item)
1574 {
1575 g_assert(item != NULL);
1576 g_assert(SP_IS_ITEM(item));
1578 Geom::Matrix const ret( sp_item_i2doc_affine(item)
1579 * Geom::Scale(1, -1)
1580 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1581 return ret;
1582 }
1584 // same as i2d but with i2root instead of i2doc
1585 Geom::Matrix sp_item_i2r_affine(SPItem const *item)
1586 {
1587 g_assert(item != NULL);
1588 g_assert(SP_IS_ITEM(item));
1590 Geom::Matrix const ret( sp_item_i2root_affine(item)
1591 * Geom::Scale(1, -1)
1592 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1593 return ret;
1594 }
1596 /**
1597 * Converts a matrix \a m into the desktop coords of the \a item.
1598 * Will become a noop when we eliminate the coordinate flipping.
1599 */
1600 Geom::Matrix matrix_to_desktop(Geom::Matrix const m, SPItem const *item)
1601 {
1602 Geom::Matrix const ret(m
1603 * Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1604 * Geom::Scale(1, -1));
1605 return ret;
1606 }
1608 /**
1609 * Converts a matrix \a m from the desktop coords of the \a item.
1610 * Will become a noop when we eliminate the coordinate flipping.
1611 */
1612 Geom::Matrix matrix_from_desktop(Geom::Matrix const m, SPItem const *item)
1613 {
1614 Geom::Matrix const ret(Geom::Scale(1, -1)
1615 * Geom::Translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1616 * m);
1617 return ret;
1618 }
1620 void sp_item_set_i2d_affine(SPItem *item, Geom::Matrix const &i2dt)
1621 {
1622 g_return_if_fail( item != NULL );
1623 g_return_if_fail( SP_IS_ITEM(item) );
1625 Geom::Matrix dt2p; /* desktop to item parent transform */
1626 if (SP_OBJECT_PARENT(item)) {
1627 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1628 } else {
1629 dt2p = ( Geom::Translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1630 * Geom::Scale(1, -1) );
1631 }
1633 Geom::Matrix const i2p( i2dt * dt2p );
1634 sp_item_set_item_transform(item, i2p);
1635 }
1638 Geom::Matrix
1639 sp_item_dt2i_affine(SPItem const *item)
1640 {
1641 /* fixme: Implement the right way (Lauris) */
1642 return sp_item_i2d_affine(item).inverse();
1643 }
1645 /* Item views */
1647 static SPItemView *
1648 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1649 {
1650 SPItemView *new_view;
1652 g_assert(item != NULL);
1653 g_assert(SP_IS_ITEM(item));
1654 g_assert(arenaitem != NULL);
1655 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1657 new_view = g_new(SPItemView, 1);
1659 new_view->next = list;
1660 new_view->flags = flags;
1661 new_view->key = key;
1662 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1664 return new_view;
1665 }
1667 static SPItemView *
1668 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1669 {
1670 if (view == list) {
1671 list = list->next;
1672 } else {
1673 SPItemView *prev;
1674 prev = list;
1675 while (prev->next != view) prev = prev->next;
1676 prev->next = view->next;
1677 }
1679 nr_arena_item_unref(view->arenaitem);
1680 g_free(view);
1682 return list;
1683 }
1685 /**
1686 * Return the arenaitem corresponding to the given item in the display
1687 * with the given key
1688 */
1689 NRArenaItem *
1690 sp_item_get_arenaitem(SPItem *item, unsigned key)
1691 {
1692 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1693 if ( iv->key == key ) {
1694 return iv->arenaitem;
1695 }
1696 }
1698 return NULL;
1699 }
1701 int
1702 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1703 {
1704 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1705 SP_OBJECT_REPR(second));
1706 }
1708 SPItem *
1709 sp_item_first_item_child (SPObject *obj)
1710 {
1711 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1712 if (SP_IS_ITEM (iter))
1713 return SP_ITEM (iter);
1714 }
1715 return NULL;
1716 }
1718 void
1719 sp_item_convert_to_guides(SPItem *item) {
1720 SPDesktop *dt = inkscape_active_desktop();
1721 SPNamedView *nv = sp_desktop_namedview(dt);
1722 (void)nv;
1724 int prefs_bbox = prefs_get_int_attribute("tools", "bounding_box", 0);
1725 SPItem::BBoxType bbox_type = (prefs_bbox ==0)?
1726 SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
1728 boost::optional<NR::Rect> bbox = sp_item_bbox_desktop(item, bbox_type);
1729 if (!bbox) {
1730 g_warning ("Cannot determine item's bounding box during conversion to guides.\n");
1731 return;
1732 }
1734 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1736 NR::Point A((*bbox).min());
1737 NR::Point C((*bbox).max());
1738 NR::Point B(A[NR::X], C[NR::Y]);
1739 NR::Point D(C[NR::X], A[NR::Y]);
1741 pts.push_back(std::make_pair(A, B));
1742 pts.push_back(std::make_pair(B, C));
1743 pts.push_back(std::make_pair(C, D));
1744 pts.push_back(std::make_pair(D, A));
1746 sp_guide_pt_pairs_to_guides(dt, pts);
1747 }
1749 /*
1750 Local Variables:
1751 mode:c++
1752 c-file-style:"stroustrup"
1753 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1754 indent-tabs-mode:nil
1755 fill-column:99
1756 End:
1757 */
1758 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :