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"
38 #include "style.h"
39 #include <glibmm/i18n.h>
40 #include "sp-root.h"
41 #include "sp-clippath.h"
42 #include "sp-mask.h"
43 #include "sp-rect.h"
44 #include "sp-use.h"
45 #include "sp-text.h"
46 #include "sp-item-rm-unsatisfied-cns.h"
47 #include "sp-pattern.h"
48 #include "sp-switch.h"
49 #include "gradient-chemistry.h"
50 #include "prefs-utils.h"
51 #include "conn-avoid-ref.h"
52 #include "conditions.h"
53 #include "sp-filter-reference.h"
54 #include "sp-guide.h"
56 #include "libnr/nr-matrix-div.h"
57 #include "libnr/nr-matrix-fns.h"
58 #include "libnr/nr-matrix-scale-ops.h"
59 #include "libnr/nr-matrix-translate-ops.h"
60 #include "libnr/nr-scale-translate-ops.h"
61 #include "libnr/nr-translate-scale-ops.h"
62 #include "libnr/nr-convert2geom.h"
63 #include "algorithms/find-last-if.h"
64 #include "util/reverse-list.h"
66 #include "xml/repr.h"
67 #include "extract-uri.h"
69 #include "live_effects/lpeobject.h"
70 #include "live_effects/effect.h"
72 #define noSP_ITEM_DEBUG_IDLE
74 static void sp_item_class_init(SPItemClass *klass);
75 static void sp_item_init(SPItem *item);
77 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
78 static void sp_item_release(SPObject *object);
79 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
80 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
81 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
83 static gchar *sp_item_private_description(SPItem *item);
84 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
86 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
87 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
89 static SPObjectClass *parent_class;
91 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
92 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
94 /**
95 * Registers SPItem class and returns its type number.
96 */
97 GType
98 sp_item_get_type(void)
99 {
100 static GType type = 0;
101 if (!type) {
102 GTypeInfo info = {
103 sizeof(SPItemClass),
104 NULL, NULL,
105 (GClassInitFunc) sp_item_class_init,
106 NULL, NULL,
107 sizeof(SPItem),
108 16,
109 (GInstanceInitFunc) sp_item_init,
110 NULL, /* value_table */
111 };
112 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
113 }
114 return type;
115 }
117 /**
118 * SPItem vtable initialization.
119 */
120 static void
121 sp_item_class_init(SPItemClass *klass)
122 {
123 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
125 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
127 sp_object_class->build = sp_item_build;
128 sp_object_class->release = sp_item_release;
129 sp_object_class->set = sp_item_set;
130 sp_object_class->update = sp_item_update;
131 sp_object_class->write = sp_item_write;
133 klass->description = sp_item_private_description;
134 klass->snappoints = sp_item_private_snappoints;
135 }
137 /**
138 * Callback for SPItem object initialization.
139 */
140 static void
141 sp_item_init(SPItem *item)
142 {
143 item->init();
144 }
146 void SPItem::init() {
147 this->sensitive = TRUE;
149 this->transform_center_x = 0;
150 this->transform_center_y = 0;
152 this->_is_evaluated = true;
153 this->_evaluated_status = StatusUnknown;
155 this->transform = NR::identity();
157 this->display = NULL;
159 this->clip_ref = new SPClipPathReference(this);
160 sigc::signal<void, SPObject *, SPObject *> cs1=this->clip_ref->changedSignal();
161 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), this);
162 _clip_ref_connection = cs1.connect(sl1);
164 this->mask_ref = new SPMaskReference(this);
165 sigc::signal<void, SPObject *, SPObject *> cs2=this->mask_ref->changedSignal();
166 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), this);
167 _mask_ref_connection = cs2.connect(sl2);
169 this->avoidRef = new SPAvoidRef(this);
171 new (&this->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
172 }
174 bool SPItem::isVisibleAndUnlocked() const {
175 return (!isHidden() && !isLocked());
176 }
178 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
179 return (!isHidden(display_key) && !isLocked());
180 }
182 bool SPItem::isLocked() const {
183 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
184 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
185 return true;
186 }
187 return false;
188 }
190 void SPItem::setLocked(bool locked) {
191 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
192 ( locked ? "1" : NULL ));
193 updateRepr();
194 }
196 bool SPItem::isHidden() const {
197 if (!isEvaluated())
198 return true;
199 return style->display.computed == SP_CSS_DISPLAY_NONE;
200 }
202 void SPItem::setHidden(bool hide) {
203 style->display.set = TRUE;
204 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
205 style->display.computed = style->display.value;
206 style->display.inherit = FALSE;
207 updateRepr();
208 }
210 bool SPItem::isHidden(unsigned display_key) const {
211 if (!isEvaluated())
212 return true;
213 for ( SPItemView *view(display) ; view ; view = view->next ) {
214 if ( view->key == display_key ) {
215 g_assert(view->arenaitem != NULL);
216 for ( NRArenaItem *arenaitem = view->arenaitem ;
217 arenaitem ; arenaitem = arenaitem->parent )
218 {
219 if (!arenaitem->visible) {
220 return true;
221 }
222 }
223 return false;
224 }
225 }
226 return true;
227 }
229 void SPItem::setEvaluated(bool evaluated) {
230 _is_evaluated = evaluated;
231 _evaluated_status = StatusSet;
232 }
234 void SPItem::resetEvaluated() {
235 if ( StatusCalculated == _evaluated_status ) {
236 _evaluated_status = StatusUnknown;
237 bool oldValue = _is_evaluated;
238 if ( oldValue != isEvaluated() ) {
239 requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
240 }
241 } if ( StatusSet == _evaluated_status ) {
242 SPObject const *const parent = SP_OBJECT_PARENT(this);
243 if (SP_IS_SWITCH(parent)) {
244 SP_SWITCH(parent)->resetChildEvaluated();
245 }
246 }
247 }
249 bool SPItem::isEvaluated() const {
250 if ( StatusUnknown == _evaluated_status ) {
251 _is_evaluated = sp_item_evaluate(this);
252 _evaluated_status = StatusCalculated;
253 }
254 return _is_evaluated;
255 }
257 /**
258 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
259 * Corresponds to setExplicitlyHidden.
260 */
261 bool
262 SPItem::isExplicitlyHidden() const
263 {
264 return (this->style->display.set
265 && this->style->display.value == SP_CSS_DISPLAY_NONE);
266 }
268 /**
269 * Sets the display CSS property to `hidden' if \a val is true,
270 * otherwise makes it unset
271 */
272 void
273 SPItem::setExplicitlyHidden(bool const val) {
274 this->style->display.set = val;
275 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
276 this->style->display.computed = this->style->display.value;
277 this->updateRepr();
278 }
280 /**
281 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
282 */
283 void
284 SPItem::setCenter(NR::Point object_centre) {
285 NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
286 if (bbox) {
287 transform_center_x = object_centre[NR::X] - bbox->midpoint()[NR::X];
288 if (fabs(transform_center_x) < 1e-5) // rounding error
289 transform_center_x = 0;
290 transform_center_y = object_centre[NR::Y] - bbox->midpoint()[NR::Y];
291 if (fabs(transform_center_y) < 1e-5) // rounding error
292 transform_center_y = 0;
293 }
294 }
296 void
297 SPItem::unsetCenter() {
298 transform_center_x = 0;
299 transform_center_y = 0;
300 }
302 bool SPItem::isCenterSet() {
303 return (transform_center_x != 0 || transform_center_y != 0);
304 }
306 NR::Point SPItem::getCenter() const {
307 NR::Maybe<NR::Rect> bbox = getBounds(sp_item_i2d_affine(this));
308 if (bbox) {
309 return bbox->midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
310 } else {
311 return NR::Point (0, 0); // something's wrong!
312 }
313 }
316 namespace {
318 bool is_item(SPObject const &object) {
319 return SP_IS_ITEM(&object);
320 }
322 }
324 void SPItem::raiseToTop() {
325 using Inkscape::Algorithms::find_last_if;
327 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
328 SP_OBJECT_NEXT(this), NULL, &is_item
329 );
330 if (topmost) {
331 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
332 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
333 }
334 }
336 void SPItem::raiseOne() {
337 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
338 SP_OBJECT_NEXT(this), NULL, &is_item
339 );
340 if (next_higher) {
341 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
342 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
343 sp_repr_parent(repr)->changeOrder(repr, ref);
344 }
345 }
347 void SPItem::lowerOne() {
348 using Inkscape::Util::MutableList;
349 using Inkscape::Util::reverse_list;
351 MutableList<SPObject &> next_lower=std::find_if(
352 reverse_list<SPObject::SiblingIterator>(
353 SP_OBJECT_PARENT(this)->firstChild(), this
354 ),
355 MutableList<SPObject &>(),
356 &is_item
357 );
358 if (next_lower) {
359 ++next_lower;
360 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
361 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
362 sp_repr_parent(repr)->changeOrder(repr, ref);
363 }
364 }
366 void SPItem::lowerToBottom() {
367 using Inkscape::Algorithms::find_last_if;
368 using Inkscape::Util::MutableList;
369 using Inkscape::Util::reverse_list;
371 MutableList<SPObject &> bottom=find_last_if(
372 reverse_list<SPObject::SiblingIterator>(
373 SP_OBJECT_PARENT(this)->firstChild(), this
374 ),
375 MutableList<SPObject &>(),
376 &is_item
377 );
378 if (bottom) {
379 ++bottom;
380 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
381 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
382 sp_repr_parent(repr)->changeOrder(repr, ref);
383 }
384 }
386 static void
387 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
388 {
389 sp_object_read_attr(object, "style");
390 sp_object_read_attr(object, "transform");
391 sp_object_read_attr(object, "clip-path");
392 sp_object_read_attr(object, "mask");
393 sp_object_read_attr(object, "sodipodi:insensitive");
394 sp_object_read_attr(object, "sodipodi:nonprintable");
395 sp_object_read_attr(object, "inkscape:transform-center-x");
396 sp_object_read_attr(object, "inkscape:transform-center-y");
397 sp_object_read_attr(object, "inkscape:connector-avoid");
399 if (((SPObjectClass *) (parent_class))->build) {
400 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
401 }
402 }
404 static void
405 sp_item_release(SPObject *object)
406 {
407 SPItem *item = (SPItem *) object;
409 item->_clip_ref_connection.disconnect();
410 item->_mask_ref_connection.disconnect();
412 // Note: do this here before the clip_ref is deleted, since calling
413 // sp_document_ensure_up_to_date for triggered routing may reference
414 // the deleted clip_ref.
415 if (item->avoidRef) {
416 delete item->avoidRef;
417 item->avoidRef = NULL;
418 }
420 if (item->clip_ref) {
421 item->clip_ref->detach();
422 delete item->clip_ref;
423 item->clip_ref = NULL;
424 }
426 if (item->mask_ref) {
427 item->mask_ref->detach();
428 delete item->mask_ref;
429 item->mask_ref = NULL;
430 }
432 if (((SPObjectClass *) (parent_class))->release) {
433 ((SPObjectClass *) parent_class)->release(object);
434 }
436 while (item->display) {
437 nr_arena_item_unparent(item->display->arenaitem);
438 item->display = sp_item_view_list_remove(item->display, item->display);
439 }
441 item->_transformed_signal.~signal();
442 }
444 static void
445 sp_item_set(SPObject *object, unsigned key, gchar const *value)
446 {
447 SPItem *item = (SPItem *) object;
449 switch (key) {
450 case SP_ATTR_TRANSFORM: {
451 NR::Matrix t;
452 if (value && sp_svg_transform_read(value, &t)) {
453 sp_item_set_item_transform(item, t);
454 } else {
455 sp_item_set_item_transform(item, NR::identity());
456 }
457 break;
458 }
459 case SP_PROP_CLIP_PATH: {
460 gchar *uri = extract_uri(value);
461 if (uri) {
462 try {
463 item->clip_ref->attach(Inkscape::URI(uri));
464 } catch (Inkscape::BadURIException &e) {
465 g_warning("%s", e.what());
466 item->clip_ref->detach();
467 }
468 g_free(uri);
469 } else {
470 item->clip_ref->detach();
471 }
473 break;
474 }
475 case SP_PROP_MASK: {
476 gchar *uri = extract_uri(value);
477 if (uri) {
478 try {
479 item->mask_ref->attach(Inkscape::URI(uri));
480 } catch (Inkscape::BadURIException &e) {
481 g_warning("%s", e.what());
482 item->mask_ref->detach();
483 }
484 g_free(uri);
485 } else {
486 item->mask_ref->detach();
487 }
489 break;
490 }
491 case SP_ATTR_SODIPODI_INSENSITIVE:
492 item->sensitive = !value;
493 for (SPItemView *v = item->display; v != NULL; v = v->next) {
494 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
495 }
496 break;
497 case SP_ATTR_CONNECTOR_AVOID:
498 item->avoidRef->setAvoid(value);
499 break;
500 case SP_ATTR_TRANSFORM_CENTER_X:
501 if (value) {
502 item->transform_center_x = g_strtod(value, NULL);
503 } else {
504 item->transform_center_x = 0;
505 }
506 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
507 break;
508 case SP_ATTR_TRANSFORM_CENTER_Y:
509 if (value) {
510 item->transform_center_y = g_strtod(value, NULL);
511 } else {
512 item->transform_center_y = 0;
513 }
514 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
515 break;
516 case SP_PROP_SYSTEM_LANGUAGE:
517 case SP_PROP_REQUIRED_FEATURES:
518 case SP_PROP_REQUIRED_EXTENSIONS:
519 {
520 item->resetEvaluated();
521 // pass to default handler
522 }
523 default:
524 if (SP_ATTRIBUTE_IS_CSS(key)) {
525 sp_style_read_from_object(object->style, object);
526 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
527 } else {
528 if (((SPObjectClass *) (parent_class))->set) {
529 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
530 }
531 }
532 break;
533 }
534 }
536 static void
537 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
538 {
539 if (old_clip) {
540 SPItemView *v;
541 /* Hide clippath */
542 for (v = item->display; v != NULL; v = v->next) {
543 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
544 nr_arena_item_set_clip(v->arenaitem, NULL);
545 }
546 }
547 if (SP_IS_CLIPPATH(clip)) {
548 NRRect bbox;
549 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
550 for (SPItemView *v = item->display; v != NULL; v = v->next) {
551 if (!v->arenaitem->key) {
552 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
553 }
554 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
555 NR_ARENA_ITEM_ARENA(v->arenaitem),
556 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
557 nr_arena_item_set_clip(v->arenaitem, ai);
558 nr_arena_item_unref(ai);
559 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
560 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
561 }
562 }
563 }
565 static void
566 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
567 {
568 if (old_mask) {
569 /* Hide mask */
570 for (SPItemView *v = item->display; v != NULL; v = v->next) {
571 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
572 nr_arena_item_set_mask(v->arenaitem, NULL);
573 }
574 }
575 if (SP_IS_MASK(mask)) {
576 NRRect bbox;
577 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
578 for (SPItemView *v = item->display; v != NULL; v = v->next) {
579 if (!v->arenaitem->key) {
580 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
581 }
582 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
583 NR_ARENA_ITEM_ARENA(v->arenaitem),
584 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
585 nr_arena_item_set_mask(v->arenaitem, ai);
586 nr_arena_item_unref(ai);
587 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
588 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
589 }
590 }
591 }
593 static void
594 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
595 {
596 SPItem *item = SP_ITEM(object);
598 if (((SPObjectClass *) (parent_class))->update)
599 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
601 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
602 if (flags & SP_OBJECT_MODIFIED_FLAG) {
603 for (SPItemView *v = item->display; v != NULL; v = v->next) {
604 nr_arena_item_set_transform(v->arenaitem, item->transform);
605 }
606 }
608 SPClipPath *clip_path = item->clip_ref ? item->clip_ref->getObject() : NULL;
609 SPMask *mask = item->mask_ref ? item->mask_ref->getObject() : NULL;
611 if ( clip_path || mask ) {
612 NRRect bbox;
613 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
614 if (clip_path) {
615 for (SPItemView *v = item->display; v != NULL; v = v->next) {
616 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
617 }
618 }
619 if (mask) {
620 for (SPItemView *v = item->display; v != NULL; v = v->next) {
621 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
622 }
623 }
624 }
626 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
627 for (SPItemView *v = item->display; v != NULL; v = v->next) {
628 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
629 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
630 }
631 }
632 }
634 /* Update bounding box data used by filters */
635 if (item->style->filter.set && item->display) {
636 NRRect item_bbox;
637 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
638 NR::Maybe<NR::Rect> i_bbox = item_bbox;
640 SPItemView *itemview = item->display;
641 do {
642 if (itemview->arenaitem)
643 nr_arena_item_set_item_bbox(itemview->arenaitem, i_bbox);
644 } while ( (itemview = itemview->next) );
645 }
647 // Update libavoid with item geometry (for connector routing).
648 if (item->avoidRef)
649 item->avoidRef->handleSettingChange();
650 }
652 static Inkscape::XML::Node *
653 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
654 {
655 SPItem *item = SP_ITEM(object);
657 gchar *c = sp_svg_transform_write(item->transform);
658 repr->setAttribute("transform", c);
659 g_free(c);
661 if (flags & SP_OBJECT_WRITE_EXT) {
662 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
663 if (item->transform_center_x != 0)
664 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
665 else
666 repr->setAttribute ("inkscape:transform-center-x", NULL);
667 if (item->transform_center_y != 0)
668 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
669 else
670 repr->setAttribute ("inkscape:transform-center-y", NULL);
671 }
673 if (item->clip_ref->getObject()) {
674 const gchar *value = g_strdup_printf ("url(%s)", item->clip_ref->getURI()->toString());
675 repr->setAttribute ("clip-path", value);
676 g_free ((void *) value);
677 }
678 if (item->mask_ref->getObject()) {
679 const gchar *value = g_strdup_printf ("url(%s)", item->mask_ref->getURI()->toString());
680 repr->setAttribute ("mask", value);
681 g_free ((void *) value);
682 }
684 if (((SPObjectClass *) (parent_class))->write) {
685 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
686 }
688 return repr;
689 }
691 NR::Maybe<NR::Rect> SPItem::getBounds(NR::Matrix const &transform,
692 SPItem::BBoxType type,
693 unsigned int /*dkey*/) const
694 {
695 NR::Maybe<NR::Rect> r = NR::Nothing();
696 sp_item_invoke_bbox_full(this, &r, transform, type, TRUE);
697 return r;
698 }
700 void
701 sp_item_invoke_bbox(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
702 {
703 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
704 }
706 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
707 void
708 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear, SPItem::BBoxType type)
709 {
710 sp_item_invoke_bbox_full(item, bbox, transform, type, clear);
711 }
713 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
714 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
715 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
716 * clones), in turn, call this function in their bbox methods. */
717 void
718 sp_item_invoke_bbox_full(SPItem const *item, NR::Maybe<NR::Rect> *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
719 {
720 g_assert(item != NULL);
721 g_assert(SP_IS_ITEM(item));
722 g_assert(bbox != NULL);
724 if (clear) {
725 *bbox = NR::Nothing();
726 }
728 // TODO: replace NRRect by NR::Rect, for all SPItemClasses, and for SP_CLIPPATH
730 NRRect temp_bbox;
731 temp_bbox.x0 = temp_bbox.y0 = NR_HUGE;
732 temp_bbox.x1 = temp_bbox.y1 = -NR_HUGE;
734 // call the subclass method
735 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
736 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &temp_bbox, transform, flags);
737 }
739 // unless this is geometric bbox, crop the bbox by clip path, if any
740 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
741 NRRect b;
742 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
743 nr_rect_d_intersect (&temp_bbox, &temp_bbox, &b);
744 }
746 if (temp_bbox.x0 > temp_bbox.x1 || temp_bbox.y0 > temp_bbox.y1) {
747 // We'll assume here that when x0 > x1 or y0 > y1, the bbox is "nothing"
748 // However it has never been explicitely defined this way for NRRects
749 // (as opposed to NR::Maybe<NR::Rect>)
750 *bbox = NR::Nothing();
751 return;
752 }
754 if (temp_bbox.x0 == temp_bbox.y0 == NR_HUGE && temp_bbox.x1 == temp_bbox.y1 == -NR_HUGE) {
755 // The bbox hasn't been touched by the SPItemClass' bbox method
756 // or it has explicitely been set to be like this (e.g. in sp_shape_bbox)
757 *bbox = NR::Nothing();
758 return;
759 }
761 // Do not use temp_bbox.upgrade() here, because it uses a test that returns NR::Nothing
762 // for any rectangle with zero area. The geometrical bbox of for example a vertical line
763 // would therefore be translated into NR::Nothing (see bug https://bugs.launchpad.net/inkscape/+bug/168684)
764 NR::Maybe<NR::Rect> temp_bbox_new = NR::Rect(NR::Point(temp_bbox.x0, temp_bbox.y0), NR::Point(temp_bbox.x1, temp_bbox.y1));
766 *bbox = NR::union_bounds(*bbox, temp_bbox_new);
767 }
769 // DEPRECATED to phase out the use of NRRect in favor of NR::Maybe<NR::Rect>
770 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
771 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
772 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
773 * clones), in turn, call this function in their bbox methods. */
774 void
775 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
776 {
777 g_assert(item != NULL);
778 g_assert(SP_IS_ITEM(item));
779 g_assert(bbox != NULL);
781 if (clear) {
782 bbox->x0 = bbox->y0 = 1e18;
783 bbox->x1 = bbox->y1 = -1e18;
784 }
786 NRRect this_bbox;
787 this_bbox.x0 = this_bbox.y0 = 1e18;
788 this_bbox.x1 = this_bbox.y1 = -1e18;
790 // call the subclass method
791 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
792 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
793 }
795 // unless this is geometric bbox, crop the bbox by clip path, if any
796 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX && item->clip_ref->getObject()) {
797 NRRect b;
798 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
799 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
800 }
802 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
803 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
804 nr_rect_d_union (bbox, bbox, &this_bbox);
805 }
806 }
808 unsigned sp_item_pos_in_parent(SPItem *item)
809 {
810 g_assert(item != NULL);
811 g_assert(SP_IS_ITEM(item));
813 SPObject *parent = SP_OBJECT_PARENT(item);
814 g_assert(parent != NULL);
815 g_assert(SP_IS_OBJECT(parent));
817 SPObject *object = SP_OBJECT(item);
819 unsigned pos=0;
820 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
821 if ( iter == object ) {
822 return pos;
823 }
824 if (SP_IS_ITEM(iter)) {
825 pos++;
826 }
827 }
829 g_assert_not_reached();
830 return 0;
831 }
833 void
834 sp_item_bbox_desktop(SPItem *item, NRRect *bbox, SPItem::BBoxType type)
835 {
836 g_assert(item != NULL);
837 g_assert(SP_IS_ITEM(item));
838 g_assert(bbox != NULL);
840 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE, type);
841 }
843 NR::Maybe<NR::Rect> sp_item_bbox_desktop(SPItem *item, SPItem::BBoxType type)
844 {
845 NR::Maybe<NR::Rect> rect = NR::Nothing();
846 sp_item_invoke_bbox(item, &rect, sp_item_i2d_affine(item), TRUE, type);
847 return rect;
848 }
850 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
851 {
852 NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2d_affine(item));
853 /* Just the corners of the bounding box suffices given that we don't yet
854 support angled guide lines. */
856 if (bbox) {
857 NR::Point p1, p2;
858 p1 = bbox->min();
859 p2 = bbox->max();
860 *p = p1;
861 *p = NR::Point(p1[NR::X], p2[NR::Y]);
862 *p = p2;
863 *p = NR::Point(p1[NR::Y], p2[NR::X]);
864 }
865 }
867 void sp_item_snappoints(SPItem const *item, bool includeItemCenter, SnapPointsIter p)
868 {
869 g_assert (item != NULL);
870 g_assert (SP_IS_ITEM(item));
872 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
873 if (item_class.snappoints) {
874 item_class.snappoints(item, p);
875 }
877 if (includeItemCenter) {
878 *p = item->getCenter();
879 }
880 }
882 void
883 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
884 {
885 if (!item->isHidden()) {
886 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
887 if (!item->transform.test_identity()
888 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
889 {
890 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
891 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
892 sp_print_release(ctx);
893 } else {
894 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
895 }
896 }
897 }
898 }
900 static gchar *
901 sp_item_private_description(SPItem */*item*/)
902 {
903 return g_strdup(_("Object"));
904 }
906 /**
907 * Returns a string suitable for status bar, formatted in pango markup language.
908 *
909 * Must be freed by caller.
910 */
911 gchar *
912 sp_item_description(SPItem *item)
913 {
914 g_assert(item != NULL);
915 g_assert(SP_IS_ITEM(item));
917 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
918 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
919 if (s && item->clip_ref->getObject()) {
920 gchar *snew = g_strdup_printf (_("%s; <i>clipped</i>"), s);
921 g_free (s);
922 s = snew;
923 }
924 if (s && item->mask_ref->getObject()) {
925 gchar *snew = g_strdup_printf (_("%s; <i>masked</i>"), s);
926 g_free (s);
927 s = snew;
928 }
929 return s;
930 }
932 g_assert_not_reached();
933 return NULL;
934 }
936 /**
937 * Allocates unique integer keys.
938 * \param numkeys Number of keys required.
939 * \return First allocated key; hence if the returned key is n
940 * you can use n, n + 1, ..., n + (numkeys - 1)
941 */
942 unsigned
943 sp_item_display_key_new(unsigned numkeys)
944 {
945 static unsigned dkey = 0;
947 dkey += numkeys;
949 return dkey - numkeys;
950 }
952 NRArenaItem *
953 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
954 {
955 g_assert(item != NULL);
956 g_assert(SP_IS_ITEM(item));
957 g_assert(arena != NULL);
958 g_assert(NR_IS_ARENA(arena));
960 NRArenaItem *ai = NULL;
961 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
962 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
963 }
965 if (ai != NULL) {
966 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
967 nr_arena_item_set_transform(ai, item->transform);
968 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
969 nr_arena_item_set_visible(ai, !item->isHidden());
970 nr_arena_item_set_sensitive(ai, item->sensitive);
971 if (item->clip_ref->getObject()) {
972 SPClipPath *cp = item->clip_ref->getObject();
974 if (!item->display->arenaitem->key) {
975 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
976 }
977 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
979 // Show and set clip
980 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
981 nr_arena_item_set_clip(ai, ac);
982 nr_arena_item_unref(ac);
984 // Update bbox, in case the clip uses bbox units
985 NRRect bbox;
986 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
987 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
988 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
989 }
990 if (item->mask_ref->getObject()) {
991 SPMask *mask = item->mask_ref->getObject();
993 if (!item->display->arenaitem->key) {
994 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
995 }
996 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
998 // Show and set mask
999 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
1000 nr_arena_item_set_mask(ai, ac);
1001 nr_arena_item_unref(ac);
1003 // Update bbox, in case the mask uses bbox units
1004 NRRect bbox;
1005 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
1006 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
1007 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1008 }
1009 NR_ARENA_ITEM_SET_DATA(ai, item);
1010 NRRect item_bbox;
1011 sp_item_invoke_bbox(item, &item_bbox, NR::identity(), TRUE, SPItem::GEOMETRIC_BBOX);
1012 NR::Maybe<NR::Rect> i_bbox = item_bbox;
1013 nr_arena_item_set_item_bbox(ai, i_bbox);
1014 }
1016 return ai;
1017 }
1019 void
1020 sp_item_invoke_hide(SPItem *item, unsigned key)
1021 {
1022 g_assert(item != NULL);
1023 g_assert(SP_IS_ITEM(item));
1025 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
1026 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
1027 }
1029 SPItemView *ref = NULL;
1030 SPItemView *v = item->display;
1031 while (v != NULL) {
1032 SPItemView *next = v->next;
1033 if (v->key == key) {
1034 if (item->clip_ref->getObject()) {
1035 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1036 nr_arena_item_set_clip(v->arenaitem, NULL);
1037 }
1038 if (item->mask_ref->getObject()) {
1039 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
1040 nr_arena_item_set_mask(v->arenaitem, NULL);
1041 }
1042 if (!ref) {
1043 item->display = v->next;
1044 } else {
1045 ref->next = v->next;
1046 }
1047 nr_arena_item_unparent(v->arenaitem);
1048 nr_arena_item_unref(v->arenaitem);
1049 g_free(v);
1050 } else {
1051 ref = v;
1052 }
1053 v = next;
1054 }
1055 }
1057 // Adjusters
1059 void
1060 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
1061 {
1062 SPStyle *style = SP_OBJECT_STYLE (item);
1064 if (style && (style->fill.isPaintserver())) {
1065 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
1066 if (SP_IS_PATTERN (server)) {
1067 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
1068 sp_pattern_transform_multiply (pattern, postmul, set);
1069 }
1070 }
1072 if (style && (style->stroke.isPaintserver())) {
1073 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
1074 if (SP_IS_PATTERN (server)) {
1075 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
1076 sp_pattern_transform_multiply (pattern, postmul, set);
1077 }
1078 }
1080 }
1082 void
1083 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
1084 {
1085 SPStyle *style = SP_OBJECT_STYLE (item);
1087 if (style && (style->fill.isPaintserver())) {
1088 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
1089 if (SP_IS_GRADIENT (server)) {
1091 /**
1092 * \note Bbox units for a gradient are generally a bad idea because
1093 * with them, you cannot preserve the relative position of the
1094 * object and its gradient after rotation or skew. So now we
1095 * convert them to userspace units which are easy to keep in sync
1096 * just by adding the object's transform to gradientTransform.
1097 * \todo FIXME: convert back to bbox units after transforming with
1098 * the item, so as to preserve the original units.
1099 */
1100 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
1102 sp_gradient_transform_multiply (gradient, postmul, set);
1103 }
1104 }
1106 if (style && (style->stroke.isPaintserver())) {
1107 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
1108 if (SP_IS_GRADIENT (server)) {
1109 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
1110 sp_gradient_transform_multiply (gradient, postmul, set);
1111 }
1112 }
1113 }
1115 void
1116 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1117 {
1118 SPStyle *style = SP_OBJECT_STYLE (item);
1120 if (style && !style->stroke.isNone() && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1122 style->stroke_width.computed *= ex;
1123 style->stroke_width.set = TRUE;
1125 if (style->stroke_dash.n_dash != 0) {
1126 int i;
1127 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1128 style->stroke_dash.dash[i] *= ex;
1129 }
1130 style->stroke_dash.offset *= ex;
1131 }
1133 SP_OBJECT(item)->updateRepr();
1134 }
1135 }
1137 /**
1138 * Find out the inverse of previous transform of an item (from its repr)
1139 */
1140 NR::Matrix
1141 sp_item_transform_repr (SPItem *item)
1142 {
1143 NR::Matrix t_old(NR::identity());
1144 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1145 if (t_attr) {
1146 NR::Matrix t;
1147 if (sp_svg_transform_read(t_attr, &t)) {
1148 t_old = t;
1149 }
1150 }
1152 return t_old;
1153 }
1156 /**
1157 * Recursively scale stroke width in \a item and its children by \a expansion.
1158 */
1159 void
1160 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1161 {
1162 sp_item_adjust_stroke (item, expansion);
1164 // A clone's child is the ghost of its original - we must not touch it, skip recursion
1165 if (item && SP_IS_USE(item))
1166 return;
1168 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1169 if (SP_IS_ITEM(o))
1170 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1171 }
1172 }
1174 /**
1175 * Recursively adjust rx and ry of rects.
1176 */
1177 void
1178 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1179 {
1180 if (SP_IS_RECT (item)) {
1181 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1182 }
1184 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1185 if (SP_IS_ITEM(o))
1186 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1187 }
1188 }
1190 /**
1191 * Recursively compensate pattern or gradient transform.
1192 */
1193 void
1194 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1195 {
1196 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1197 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1198 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1199 NR::Matrix t_item = sp_item_transform_repr (item);
1200 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1202 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation;
1203 // also we do not recurse into clones, because a clone's child is the ghost of its original -
1204 // we must not touch it
1205 if (!(item && (SP_IS_TEXT(item) || SP_IS_USE(item)))) {
1206 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1207 if (SP_IS_ITEM(o)) {
1208 // At the level of the transformed item, t_ancestors is identity;
1209 // below it, it is the accmmulated chain of transforms from this level to the top level
1210 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1211 }
1212 }
1213 }
1215 // We recursed into children first, and are now adjusting this object second;
1216 // this is so that adjustments in a tree are done from leaves up to the root,
1217 // and paintservers on leaves inheriting their values from ancestors could adjust themselves properly
1218 // before ancestors themselves are adjusted, probably differently (bug 1286535)
1220 if (is_pattern)
1221 sp_item_adjust_pattern (item, paint_delta);
1222 else
1223 sp_item_adjust_gradient (item, paint_delta);
1225 }
1227 void
1228 sp_item_adjust_livepatheffect (SPItem *item, NR::Matrix const &postmul, bool set)
1229 {
1230 if ( !SP_IS_SHAPE(item) )
1231 return;
1233 SPShape *shape = SP_SHAPE (item);
1234 if ( sp_shape_has_path_effect(shape) ) {
1235 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1236 LivePathEffectObject *new_lpeobj = lpeobj->fork_private_if_necessary();
1237 if (new_lpeobj != lpeobj) {
1238 sp_shape_set_path_effect(shape, new_lpeobj);
1239 }
1241 Inkscape::LivePathEffect::Effect * effect = sp_shape_get_livepatheffect(shape);
1242 if (effect) {
1243 effect->transform_multiply (to_2geom(postmul), set);
1244 }
1245 }
1246 }
1248 /**
1249 * A temporary wrapper for the next function accepting the NRMatrix
1250 * instead of NR::Matrix
1251 */
1252 void
1253 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1254 {
1255 if (transform == NULL)
1256 sp_item_write_transform(item, repr, NR::identity(), adv);
1257 else
1258 sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1259 }
1261 /**
1262 * Set a new transform on an object.
1263 *
1264 * Compensate for stroke scaling and gradient/pattern fill transform, if
1265 * necessary. Call the object's set_transform method if transforms are
1266 * stored optimized. Send _transformed_signal. Invoke _write method so that
1267 * the repr is updated with the new transform.
1268 */
1269 void
1270 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv, bool compensate)
1271 {
1272 g_return_if_fail(item != NULL);
1273 g_return_if_fail(SP_IS_ITEM(item));
1274 g_return_if_fail(repr != NULL);
1276 // calculate the relative transform, if not given by the adv attribute
1277 NR::Matrix advertized_transform;
1278 if (adv != NULL) {
1279 advertized_transform = *adv;
1280 } else {
1281 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1282 }
1284 if (compensate) {
1286 // recursively compensate for stroke scaling, depending on user preference
1287 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1288 double const expansion = 1. / NR::expansion(advertized_transform);
1289 sp_item_adjust_stroke_width_recursive(item, expansion);
1290 }
1292 // recursively compensate rx/ry of a rect if requested
1293 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1294 sp_item_adjust_rects_recursive(item, advertized_transform);
1295 }
1297 // recursively compensate pattern fill if it's not to be transformed
1298 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1299 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1300 }
1301 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1302 /// recursively compensate gradient fill if it's not to be transformed
1303 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1304 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1305 } else {
1306 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1307 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1308 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1309 }
1311 } // endif(compensate)
1313 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1314 NR::Matrix transform_attr (transform);
1315 if ( // run the object's set_transform (i.e. embed transform) only if:
1316 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && // it does have a set_transform method
1317 !preserve && // user did not chose to preserve all transforms
1318 !item->clip_ref->getObject() && // the object does not have a clippath
1319 !item->mask_ref->getObject() && // the object does not have a mask
1320 !(!transform.is_translation() && SP_OBJECT_STYLE(item) && SP_OBJECT_STYLE(item)->getFilter())
1321 // the object does not have a filter, or the transform is translation (which is supposed to not affect filters)
1322 ) {
1323 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1324 }
1325 sp_item_set_item_transform(item, transform_attr);
1327 // Note: updateRepr comes before emitting the transformed signal since
1328 // it causes clone SPUse's copy of the original object to brought up to
1329 // date with the original. Otherwise, sp_use_bbox returns incorrect
1330 // values if called in code handling the transformed signal.
1331 SP_OBJECT(item)->updateRepr();
1333 // send the relative transform with a _transformed_signal
1334 item->_transformed_signal.emit(&advertized_transform, item);
1335 }
1337 gint
1338 sp_item_event(SPItem *item, SPEvent *event)
1339 {
1340 g_return_val_if_fail(item != NULL, FALSE);
1341 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1342 g_return_val_if_fail(event != NULL, FALSE);
1344 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1345 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1347 return FALSE;
1348 }
1350 /**
1351 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1352 * gradients, patterns as sp_item_write_transform does.
1353 */
1354 void
1355 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1356 {
1357 g_return_if_fail(item != NULL);
1358 g_return_if_fail(SP_IS_ITEM(item));
1360 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1361 item->transform = transform;
1362 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1363 transformation. It's apparently not used anywhere else. */
1364 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1365 sp_item_rm_unsatisfied_cns(*item);
1366 }
1367 }
1370 /**
1371 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1372 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1373 */
1374 NR::Matrix
1375 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1376 NR::Matrix ret(NR::identity());
1377 g_return_val_if_fail(object != NULL, ret);
1379 /* stop at first non-renderable ancestor */
1380 while ( object != ancestor && SP_IS_ITEM(object) ) {
1381 if (SP_IS_ROOT(object)) {
1382 ret *= SP_ROOT(object)->c2p;
1383 }
1384 ret *= SP_ITEM(object)->transform;
1385 object = SP_OBJECT_PARENT(object);
1386 }
1387 return ret;
1388 }
1390 NR::Matrix
1391 i2i_affine(SPObject const *src, SPObject const *dest) {
1392 g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1393 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1394 return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1395 }
1397 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1398 return i2i_affine(this, dest);
1399 }
1401 /**
1402 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1403 * \pre (item != NULL) and SP_IS_ITEM(item).
1404 */
1405 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1406 {
1407 return i2anc_affine(item, NULL);
1408 }
1410 /**
1411 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1412 * Used in path operations mostly.
1413 * \pre (item != NULL) and SP_IS_ITEM(item).
1414 */
1415 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1416 {
1417 g_assert(item != NULL);
1418 g_assert(SP_IS_ITEM(item));
1420 NR::Matrix ret(NR::identity());
1421 g_assert(ret.test_identity());
1422 while ( NULL != SP_OBJECT_PARENT(item) ) {
1423 ret *= item->transform;
1424 item = SP_ITEM(SP_OBJECT_PARENT(item));
1425 }
1426 g_assert(SP_IS_ROOT(item));
1428 ret *= item->transform;
1430 return ret;
1431 }
1433 /* fixme: This is EVIL!!! */
1435 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1436 {
1437 g_assert(item != NULL);
1438 g_assert(SP_IS_ITEM(item));
1440 NR::Matrix const ret( sp_item_i2doc_affine(item)
1441 * NR::scale(1, -1)
1442 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1443 return ret;
1444 }
1446 // same as i2d but with i2root instead of i2doc
1447 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1448 {
1449 g_assert(item != NULL);
1450 g_assert(SP_IS_ITEM(item));
1452 NR::Matrix const ret( sp_item_i2root_affine(item)
1453 * NR::scale(1, -1)
1454 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1455 return ret;
1456 }
1458 /**
1459 * Converts a matrix \a m into the desktop coords of the \a item.
1460 * Will become a noop when we eliminate the coordinate flipping.
1461 */
1462 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1463 {
1464 NR::Matrix const ret(m
1465 * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1466 * NR::scale(1, -1));
1467 return ret;
1468 }
1470 /**
1471 * Converts a matrix \a m from the desktop coords of the \a item.
1472 * Will become a noop when we eliminate the coordinate flipping.
1473 */
1474 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1475 {
1476 NR::Matrix const ret(NR::scale(1, -1)
1477 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1478 * m);
1479 return ret;
1480 }
1482 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1483 {
1484 g_return_if_fail( item != NULL );
1485 g_return_if_fail( SP_IS_ITEM(item) );
1487 NR::Matrix dt2p; /* desktop to item parent transform */
1488 if (SP_OBJECT_PARENT(item)) {
1489 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1490 } else {
1491 dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1492 * NR::scale(1, -1) );
1493 }
1495 NR::Matrix const i2p( i2dt * dt2p );
1496 sp_item_set_item_transform(item, i2p);
1497 }
1500 NR::Matrix
1501 sp_item_dt2i_affine(SPItem const *item)
1502 {
1503 /* fixme: Implement the right way (Lauris) */
1504 return sp_item_i2d_affine(item).inverse();
1505 }
1507 /* Item views */
1509 static SPItemView *
1510 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1511 {
1512 SPItemView *new_view;
1514 g_assert(item != NULL);
1515 g_assert(SP_IS_ITEM(item));
1516 g_assert(arenaitem != NULL);
1517 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1519 new_view = g_new(SPItemView, 1);
1521 new_view->next = list;
1522 new_view->flags = flags;
1523 new_view->key = key;
1524 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1526 return new_view;
1527 }
1529 static SPItemView *
1530 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1531 {
1532 if (view == list) {
1533 list = list->next;
1534 } else {
1535 SPItemView *prev;
1536 prev = list;
1537 while (prev->next != view) prev = prev->next;
1538 prev->next = view->next;
1539 }
1541 nr_arena_item_unref(view->arenaitem);
1542 g_free(view);
1544 return list;
1545 }
1547 /**
1548 * Return the arenaitem corresponding to the given item in the display
1549 * with the given key
1550 */
1551 NRArenaItem *
1552 sp_item_get_arenaitem(SPItem *item, unsigned key)
1553 {
1554 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1555 if ( iv->key == key ) {
1556 return iv->arenaitem;
1557 }
1558 }
1560 return NULL;
1561 }
1563 int
1564 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1565 {
1566 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1567 SP_OBJECT_REPR(second));
1568 }
1570 SPItem *
1571 sp_item_first_item_child (SPObject *obj)
1572 {
1573 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1574 if (SP_IS_ITEM (iter))
1575 return SP_ITEM (iter);
1576 }
1577 return NULL;
1578 }
1580 void
1581 sp_item_convert_to_guides(SPItem *item) {
1582 NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(item, SPItem::GEOMETRIC_BBOX);
1583 if (!bbox) {
1584 g_print ("Cannot determine bounding box. Doing nothing.\n");
1585 return;
1586 }
1588 std::list<std::pair<Geom::Point, Geom::Point> > pts;
1590 NR::Point A((*bbox).min());
1591 NR::Point C((*bbox).max());
1592 NR::Point B(A[NR::X], C[NR::Y]);
1593 NR::Point D(C[NR::X], A[NR::Y]);
1595 pts.push_back(std::make_pair(A.to_2geom(), B.to_2geom()));
1596 pts.push_back(std::make_pair(B.to_2geom(), C.to_2geom()));
1597 pts.push_back(std::make_pair(C.to_2geom(), D.to_2geom()));
1598 pts.push_back(std::make_pair(D.to_2geom(), A.to_2geom()));
1600 sp_guide_pt_pairs_to_guides(SP_OBJECT_DOCUMENT(item), pts);
1602 SP_OBJECT(item)->deleteObject(true);
1603 }
1605 /*
1606 Local Variables:
1607 mode:c++
1608 c-file-style:"stroustrup"
1609 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1610 indent-tabs-mode:nil
1611 fill-column:99
1612 End:
1613 */
1614 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :