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 *
11 * Copyright (C) 2001-2005 authors
12 * Copyright (C) 2001 Ximian, Inc.
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 /** \class SPItem
18 *
19 * SPItem is an abstract base class for all graphic (visible) SVG nodes. It
20 * is a subclass of SPObject, with great deal of specific functionality.
21 */
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
28 #include "sp-item.h"
29 #include "svg/svg.h"
30 #include "print.h"
31 #include "display/nr-arena.h"
32 #include "display/nr-arena-item.h"
33 #include "attributes.h"
34 #include "document.h"
35 #include "uri.h"
37 #include "style.h"
38 #include <glibmm/i18n.h>
39 #include "sp-root.h"
40 #include "sp-clippath.h"
41 #include "sp-mask.h"
42 #include "sp-rect.h"
43 #include "sp-use.h"
44 #include "sp-text.h"
45 #include "sp-item-rm-unsatisfied-cns.h"
46 #include "sp-pattern.h"
47 #include "gradient-chemistry.h"
48 #include "prefs-utils.h"
49 #include "conn-avoid-ref.h"
51 #include "libnr/nr-matrix-div.h"
52 #include "libnr/nr-matrix-fns.h"
53 #include "libnr/nr-matrix-scale-ops.h"
54 #include "libnr/nr-matrix-translate-ops.h"
55 #include "libnr/nr-scale-translate-ops.h"
56 #include "libnr/nr-translate-scale-ops.h"
57 #include "algorithms/find-last-if.h"
58 #include "util/reverse-list.h"
60 #include "xml/repr.h"
62 #define noSP_ITEM_DEBUG_IDLE
64 static void sp_item_class_init(SPItemClass *klass);
65 static void sp_item_init(SPItem *item);
67 static void sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
68 static void sp_item_release(SPObject *object);
69 static void sp_item_set(SPObject *object, unsigned key, gchar const *value);
70 static void sp_item_update(SPObject *object, SPCtx *ctx, guint flags);
71 static Inkscape::XML::Node *sp_item_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
73 static gchar *sp_item_private_description(SPItem *item);
74 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p);
76 static SPItemView *sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem);
77 static SPItemView *sp_item_view_list_remove(SPItemView *list, SPItemView *view);
79 static SPObjectClass *parent_class;
81 static void clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
82 static void mask_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item);
84 /**
85 * Registers SPItem class and returns its type number.
86 */
87 GType
88 sp_item_get_type(void)
89 {
90 static GType type = 0;
91 if (!type) {
92 GTypeInfo info = {
93 sizeof(SPItemClass),
94 NULL, NULL,
95 (GClassInitFunc) sp_item_class_init,
96 NULL, NULL,
97 sizeof(SPItem),
98 16,
99 (GInstanceInitFunc) sp_item_init,
100 NULL, /* value_table */
101 };
102 type = g_type_register_static(SP_TYPE_OBJECT, "SPItem", &info, (GTypeFlags)0);
103 }
104 return type;
105 }
107 /**
108 * SPItem vtable initialization.
109 */
110 static void
111 sp_item_class_init(SPItemClass *klass)
112 {
113 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
115 parent_class = (SPObjectClass *)g_type_class_ref(SP_TYPE_OBJECT);
117 sp_object_class->build = sp_item_build;
118 sp_object_class->release = sp_item_release;
119 sp_object_class->set = sp_item_set;
120 sp_object_class->update = sp_item_update;
121 sp_object_class->write = sp_item_write;
123 klass->description = sp_item_private_description;
124 klass->snappoints = sp_item_private_snappoints;
125 }
127 /**
128 * Callback for SPItem object initialization.
129 */
130 static void
131 sp_item_init(SPItem *item)
132 {
133 SPObject *object = SP_OBJECT(item);
135 item->sensitive = TRUE;
137 item->transform_center_x = 0;
138 item->transform_center_y = 0;
140 item->transform = NR::identity();
142 item->display = NULL;
144 item->clip_ref = new SPClipPathReference(SP_OBJECT(item));
145 {
146 sigc::signal<void, SPObject *, SPObject *> cs1=item->clip_ref->changedSignal();
147 sigc::slot2<void,SPObject*, SPObject *> sl1=sigc::bind(sigc::ptr_fun(clip_ref_changed), item);
148 cs1.connect(sl1);
149 }
151 item->mask_ref = new SPMaskReference(SP_OBJECT(item));
152 sigc::signal<void, SPObject *, SPObject *> cs2=item->mask_ref->changedSignal();
153 sigc::slot2<void,SPObject*, SPObject *> sl2=sigc::bind(sigc::ptr_fun(mask_ref_changed), item);
154 cs2.connect(sl2);
156 if (!object->style) object->style = sp_style_new_from_object(SP_OBJECT(item));
158 item->avoidRef = new SPAvoidRef(item);
160 new (&item->_transformed_signal) sigc::signal<void, NR::Matrix const *, SPItem *>();
161 }
163 bool SPItem::isVisibleAndUnlocked() const {
164 return (!isHidden() && !isLocked());
165 }
167 bool SPItem::isVisibleAndUnlocked(unsigned display_key) const {
168 return (!isHidden(display_key) && !isLocked());
169 }
171 bool SPItem::isLocked() const {
172 for (SPObject *o = SP_OBJECT(this); o != NULL; o = SP_OBJECT_PARENT(o)) {
173 if (SP_IS_ITEM(o) && !(SP_ITEM(o)->sensitive))
174 return true;
175 }
176 return false;
177 }
179 void SPItem::setLocked(bool locked) {
180 SP_OBJECT_REPR(this)->setAttribute("sodipodi:insensitive",
181 ( locked ? "1" : NULL ));
182 updateRepr();
183 }
185 bool SPItem::isHidden() const {
186 return style->display.computed == SP_CSS_DISPLAY_NONE;
187 }
189 void SPItem::setHidden(bool hide) {
190 style->display.set = TRUE;
191 style->display.value = ( hide ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
192 style->display.computed = style->display.value;
193 style->display.inherit = FALSE;
194 updateRepr();
195 }
197 bool SPItem::isHidden(unsigned display_key) const {
198 for ( SPItemView *view(display) ; view ; view = view->next ) {
199 if ( view->key == display_key ) {
200 g_assert(view->arenaitem != NULL);
201 for ( NRArenaItem *arenaitem = view->arenaitem ;
202 arenaitem ; arenaitem = arenaitem->parent )
203 {
204 if (!arenaitem->visible) {
205 return true;
206 }
207 }
208 return false;
209 }
210 }
211 return true;
212 }
214 /**
215 * Returns something suitable for the `Hide' checkbox in the Object Properties dialog box.
216 * Corresponds to setExplicitlyHidden.
217 */
218 bool
219 SPItem::isExplicitlyHidden() const
220 {
221 return (this->style->display.set
222 && this->style->display.value == SP_CSS_DISPLAY_NONE);
223 }
225 /**
226 * Sets the display CSS property to `hidden' if \a val is true,
227 * otherwise makes it unset
228 */
229 void
230 SPItem::setExplicitlyHidden(bool const val) {
231 this->style->display.set = val;
232 this->style->display.value = ( val ? SP_CSS_DISPLAY_NONE : SP_CSS_DISPLAY_INLINE );
233 this->style->display.computed = this->style->display.value;
234 this->updateRepr();
235 }
237 /**
238 * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
239 */
240 void
241 SPItem::setCenter(NR::Point object_centre) {
242 NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
243 if (!bbox.isEmpty()) {
244 transform_center_x = object_centre[NR::X] - bbox.midpoint()[NR::X];
245 if (fabs(transform_center_x) < 1e-5) // rounding error
246 transform_center_x = 0;
247 transform_center_y = object_centre[NR::Y] - bbox.midpoint()[NR::Y];
248 if (fabs(transform_center_y) < 1e-5) // rounding error
249 transform_center_y = 0;
250 }
251 }
253 void
254 SPItem::unsetCenter() {
255 transform_center_x = 0;
256 transform_center_y = 0;
257 }
259 bool SPItem::isCenterSet() {
260 return (transform_center_x != 0 || transform_center_y != 0);
261 }
263 NR::Point SPItem::getCenter() {
264 NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
265 if (!bbox.isEmpty()) {
266 return bbox.midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
267 } else {
268 return NR::Point (0, 0); // something's wrong!
269 }
270 }
273 namespace {
275 bool is_item(SPObject const &object) {
276 return SP_IS_ITEM(&object);
277 }
279 }
281 void SPItem::raiseToTop() {
282 using Inkscape::Algorithms::find_last_if;
284 SPObject *topmost=find_last_if<SPObject::SiblingIterator>(
285 SP_OBJECT_NEXT(this), NULL, &is_item
286 );
287 if (topmost) {
288 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
289 sp_repr_parent(repr)->changeOrder(repr, SP_OBJECT_REPR(topmost));
290 }
291 }
293 void SPItem::raiseOne() {
294 SPObject *next_higher=std::find_if<SPObject::SiblingIterator>(
295 SP_OBJECT_NEXT(this), NULL, &is_item
296 );
297 if (next_higher) {
298 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
299 Inkscape::XML::Node *ref=SP_OBJECT_REPR(next_higher);
300 sp_repr_parent(repr)->changeOrder(repr, ref);
301 }
302 }
304 void SPItem::lowerOne() {
305 using Inkscape::Util::MutableList;
306 using Inkscape::Util::reverse_list;
308 MutableList<SPObject &> next_lower=std::find_if(
309 reverse_list<SPObject::SiblingIterator>(
310 SP_OBJECT_PARENT(this)->firstChild(), this
311 ),
312 MutableList<SPObject &>(),
313 &is_item
314 );
315 if (next_lower) {
316 ++next_lower;
317 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
318 Inkscape::XML::Node *ref=( next_lower ? SP_OBJECT_REPR(&*next_lower) : NULL );
319 sp_repr_parent(repr)->changeOrder(repr, ref);
320 }
321 }
323 void SPItem::lowerToBottom() {
324 using Inkscape::Algorithms::find_last_if;
325 using Inkscape::Util::MutableList;
326 using Inkscape::Util::reverse_list;
328 MutableList<SPObject &> bottom=find_last_if(
329 reverse_list<SPObject::SiblingIterator>(
330 SP_OBJECT_PARENT(this)->firstChild(), this
331 ),
332 MutableList<SPObject &>(),
333 &is_item
334 );
335 if (bottom) {
336 ++bottom;
337 Inkscape::XML::Node *repr=SP_OBJECT_REPR(this);
338 Inkscape::XML::Node *ref=( bottom ? SP_OBJECT_REPR(&*bottom) : NULL );
339 sp_repr_parent(repr)->changeOrder(repr, ref);
340 }
341 }
343 static void
344 sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
345 {
346 sp_object_read_attr(object, "style");
347 sp_object_read_attr(object, "transform");
348 sp_object_read_attr(object, "clip-path");
349 sp_object_read_attr(object, "mask");
350 sp_object_read_attr(object, "sodipodi:insensitive");
351 sp_object_read_attr(object, "sodipodi:nonprintable");
352 sp_object_read_attr(object, "inkscape:transform-center-x");
353 sp_object_read_attr(object, "inkscape:transform-center-y");
354 sp_object_read_attr(object, "inkscape:connector-avoid");
356 if (((SPObjectClass *) (parent_class))->build) {
357 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
358 }
359 }
361 static void
362 sp_item_release(SPObject *object)
363 {
364 SPItem *item = (SPItem *) object;
366 if (item->clip_ref) {
367 item->clip_ref->detach();
368 delete item->clip_ref;
369 item->clip_ref = NULL;
370 }
372 if (item->mask_ref) {
373 item->mask_ref->detach();
374 delete item->mask_ref;
375 item->mask_ref = NULL;
376 }
378 if (item->avoidRef) {
379 delete item->avoidRef;
380 item->avoidRef = NULL;
381 }
383 if (((SPObjectClass *) (parent_class))->release) {
384 ((SPObjectClass *) parent_class)->release(object);
385 }
387 while (item->display) {
388 nr_arena_item_unparent(item->display->arenaitem);
389 item->display = sp_item_view_list_remove(item->display, item->display);
390 }
392 item->_transformed_signal.~signal();
393 }
395 static void
396 sp_item_set(SPObject *object, unsigned key, gchar const *value)
397 {
398 SPItem *item = (SPItem *) object;
400 switch (key) {
401 case SP_ATTR_TRANSFORM: {
402 NR::Matrix t;
403 if (value && sp_svg_transform_read(value, &t)) {
404 sp_item_set_item_transform(item, t);
405 } else {
406 sp_item_set_item_transform(item, NR::identity());
407 }
408 break;
409 }
410 case SP_PROP_CLIP_PATH: {
411 gchar *uri = Inkscape::parse_css_url(value);
412 if (uri) {
413 try {
414 item->clip_ref->attach(Inkscape::URI(uri));
415 } catch (Inkscape::BadURIException &e) {
416 g_warning("%s", e.what());
417 item->clip_ref->detach();
418 }
419 g_free(uri);
420 } else {
421 item->clip_ref->detach();
422 }
424 break;
425 }
426 case SP_PROP_MASK: {
427 gchar *uri=Inkscape::parse_css_url(value);
428 if (uri) {
429 try {
430 item->mask_ref->attach(Inkscape::URI(uri));
431 } catch (Inkscape::BadURIException &e) {
432 g_warning("%s", e.what());
433 item->mask_ref->detach();
434 }
435 g_free(uri);
436 } else {
437 item->mask_ref->detach();
438 }
440 break;
441 }
442 case SP_ATTR_SODIPODI_INSENSITIVE:
443 item->sensitive = !value;
444 for (SPItemView *v = item->display; v != NULL; v = v->next) {
445 nr_arena_item_set_sensitive(v->arenaitem, item->sensitive);
446 }
447 break;
448 case SP_ATTR_STYLE:
449 sp_style_read_from_object(object->style, object);
450 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
451 break;
452 case SP_ATTR_CONNECTOR_AVOID:
453 item->avoidRef->setAvoid(value);
454 break;
455 case SP_ATTR_TRANSFORM_CENTER_X:
456 if (value) {
457 item->transform_center_x = g_strtod(value, NULL);
458 } else {
459 item->transform_center_x = 0;
460 }
461 break;
462 case SP_ATTR_TRANSFORM_CENTER_Y:
463 if (value) {
464 item->transform_center_y = g_strtod(value, NULL);
465 } else {
466 item->transform_center_y = 0;
467 }
468 break;
469 default:
470 if (SP_ATTRIBUTE_IS_CSS(key)) {
471 sp_style_read_from_object(object->style, object);
472 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
473 } else {
474 if (((SPObjectClass *) (parent_class))->set) {
475 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
476 }
477 }
478 break;
479 }
480 }
482 static void
483 clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
484 {
485 if (old_clip) {
486 SPItemView *v;
487 /* Hide clippath */
488 for (v = item->display; v != NULL; v = v->next) {
489 sp_clippath_hide(SP_CLIPPATH(old_clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
490 nr_arena_item_set_clip(v->arenaitem, NULL);
491 }
492 }
493 if (SP_IS_CLIPPATH(clip)) {
494 NRRect bbox;
495 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
496 for (SPItemView *v = item->display; v != NULL; v = v->next) {
497 if (!v->arenaitem->key) {
498 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
499 }
500 NRArenaItem *ai = sp_clippath_show(SP_CLIPPATH(clip),
501 NR_ARENA_ITEM_ARENA(v->arenaitem),
502 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
503 nr_arena_item_set_clip(v->arenaitem, ai);
504 nr_arena_item_unref(ai);
505 sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
506 SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
507 }
508 }
509 }
511 static void
512 mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
513 {
514 if (old_mask) {
515 /* Hide mask */
516 for (SPItemView *v = item->display; v != NULL; v = v->next) {
517 sp_mask_hide(SP_MASK(old_mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
518 nr_arena_item_set_mask(v->arenaitem, NULL);
519 }
520 }
521 if (SP_IS_MASK(mask)) {
522 NRRect bbox;
523 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
524 for (SPItemView *v = item->display; v != NULL; v = v->next) {
525 if (!v->arenaitem->key) {
526 NR_ARENA_ITEM_SET_KEY(v->arenaitem, sp_item_display_key_new(3));
527 }
528 NRArenaItem *ai = sp_mask_show(SP_MASK(mask),
529 NR_ARENA_ITEM_ARENA(v->arenaitem),
530 NR_ARENA_ITEM_GET_KEY(v->arenaitem));
531 nr_arena_item_set_mask(v->arenaitem, ai);
532 nr_arena_item_unref(ai);
533 sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
534 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
535 }
536 }
537 }
539 static void
540 sp_item_update(SPObject *object, SPCtx *ctx, guint flags)
541 {
542 SPItem *item = SP_ITEM(object);
544 if (((SPObjectClass *) (parent_class))->update)
545 (* ((SPObjectClass *) (parent_class))->update)(object, ctx, flags);
547 if (flags & (SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) {
548 if (flags & SP_OBJECT_MODIFIED_FLAG) {
549 for (SPItemView *v = item->display; v != NULL; v = v->next) {
550 nr_arena_item_set_transform(v->arenaitem, item->transform);
551 }
552 }
554 SPClipPath *clip_path = item->clip_ref->getObject();
555 SPMask *mask = item->mask_ref->getObject();
557 if ( clip_path || mask ) {
558 NRRect bbox;
559 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
560 if (clip_path) {
561 for (SPItemView *v = item->display; v != NULL; v = v->next) {
562 sp_clippath_set_bbox(clip_path, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
563 }
564 }
565 if (mask) {
566 for (SPItemView *v = item->display; v != NULL; v = v->next) {
567 sp_mask_set_bbox(mask, NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
568 }
569 }
570 }
572 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
573 for (SPItemView *v = item->display; v != NULL; v = v->next) {
574 nr_arena_item_set_opacity(v->arenaitem, SP_SCALE24_TO_FLOAT(object->style->opacity.value));
575 nr_arena_item_set_visible(v->arenaitem, !item->isHidden());
576 }
577 }
578 }
580 // Update libavoid with item geometry (for connector routing).
581 item->avoidRef->handleSettingChange();
582 }
584 static Inkscape::XML::Node *
585 sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
586 {
587 SPItem *item = SP_ITEM(object);
589 gchar c[256];
590 if (sp_svg_transform_write(c, 256, item->transform)) {
591 repr->setAttribute("transform", c);
592 } else {
593 repr->setAttribute("transform", NULL);
594 }
596 SPObject const *const parent = SP_OBJECT_PARENT(object);
597 /** \todo Can someone please document why this is conditional on having
598 * a parent? The only parentless thing I can think of is the top-level
599 * <svg> element (SPRoot). SPRoot is derived from SPGroup, and can have
600 * style. I haven't looked at callers.
601 */
602 if (parent) {
603 SPStyle const *const obj_style = SP_OBJECT_STYLE(object);
604 if (obj_style) {
605 gchar *s = sp_style_write_string(obj_style, SP_STYLE_FLAG_IFSET);
606 repr->setAttribute("style", ( *s ? s : NULL ));
607 g_free(s);
608 } else {
609 /** \todo I'm not sure what to do in this case. Bug #1165868
610 * suggests that it can arise, but the submitter doesn't know
611 * how to do so reliably. The main two options are either
612 * leave repr's style attribute unchanged, or explicitly clear it.
613 * Must also consider what to do with property attributes for
614 * the element; see below.
615 */
616 char const *style_str = repr->attribute("style");
617 if (!style_str) {
618 style_str = "NULL";
619 }
620 g_warning("Item's style is NULL; repr style attribute is %s", style_str);
621 }
623 /** \note We treat object->style as authoritative. Its effects have
624 * been written to the style attribute above; any properties that are
625 * unset we take to be deliberately unset (e.g. so that clones can
626 * override the property).
627 *
628 * Note that the below has an undesirable consequence of changing the
629 * appearance on renderers that lack CSS support (e.g. SVG tiny);
630 * possibly we should write property attributes instead of a style
631 * attribute.
632 */
633 sp_style_unset_property_attrs (object);
634 }
636 if (flags & SP_OBJECT_WRITE_EXT) {
637 repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
638 if (item->transform_center_x != 0)
639 sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
640 else
641 repr->setAttribute ("inkscape:transform-center-x", NULL);
642 if (item->transform_center_y != 0)
643 sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
644 else
645 repr->setAttribute ("inkscape:transform-center-y", NULL);
646 }
648 if (((SPObjectClass *) (parent_class))->write) {
649 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
650 }
652 return repr;
653 }
655 NR::Rect SPItem::invokeBbox(NR::Matrix const &transform) const
656 {
657 NRRect r;
658 sp_item_invoke_bbox_full(this, &r, transform, 0, TRUE);
659 return NR::Rect(r);
660 }
662 void
663 sp_item_invoke_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const clear)
664 {
665 sp_item_invoke_bbox_full(item, bbox, transform, 0, clear);
666 }
668 /** Calls \a item's subclass' bounding box method; clips it by the bbox of clippath, if any; and
669 * unions the resulting bbox with \a bbox. If \a clear is true, empties \a bbox first. Passes the
670 * transform and the flags to the actual bbox methods. Note that many of subclasses (e.g. groups,
671 * clones), in turn, call this function in their bbox methods. */
672 void
673 sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags, unsigned const clear)
674 {
675 g_assert(item != NULL);
676 g_assert(SP_IS_ITEM(item));
677 g_assert(bbox != NULL);
679 if (clear) {
680 bbox->x0 = bbox->y0 = 1e18;
681 bbox->x1 = bbox->y1 = -1e18;
682 }
684 NRRect this_bbox;
685 this_bbox.x0 = this_bbox.y0 = 1e18;
686 this_bbox.x1 = this_bbox.y1 = -1e18;
688 // call the subclass method
689 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
690 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, &this_bbox, transform, flags);
691 }
693 // crop the bbox by clip path, if any
694 if (item->clip_ref->getObject()) {
695 NRRect b;
696 sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
697 nr_rect_d_intersect (&this_bbox, &this_bbox, &b);
698 }
700 // if non-empty (with some tolerance - ?) union this_bbox with the bbox we've got passed
701 if ( fabs(this_bbox.x1-this_bbox.x0) > -0.00001 && fabs(this_bbox.y1-this_bbox.y0) > -0.00001 ) {
702 nr_rect_d_union (bbox, bbox, &this_bbox);
703 }
704 }
706 unsigned sp_item_pos_in_parent(SPItem *item)
707 {
708 g_assert(item != NULL);
709 g_assert(SP_IS_ITEM(item));
711 SPObject *parent = SP_OBJECT_PARENT(item);
712 g_assert(parent != NULL);
713 g_assert(SP_IS_OBJECT(parent));
715 SPObject *object = SP_OBJECT(item);
717 unsigned pos=0;
718 for ( SPObject *iter = sp_object_first_child(parent) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
719 if ( iter == object ) {
720 return pos;
721 }
722 if (SP_IS_ITEM(iter)) {
723 pos++;
724 }
725 }
727 g_assert_not_reached();
728 return 0;
729 }
731 void
732 sp_item_bbox_desktop(SPItem *item, NRRect *bbox)
733 {
734 g_assert(item != NULL);
735 g_assert(SP_IS_ITEM(item));
736 g_assert(bbox != NULL);
738 sp_item_invoke_bbox(item, bbox, sp_item_i2d_affine(item), TRUE);
739 }
741 NR::Rect sp_item_bbox_desktop(SPItem *item)
742 {
743 NRRect ret;
744 sp_item_bbox_desktop(item, &ret);
745 return NR::Rect(ret);
746 }
748 static void sp_item_private_snappoints(SPItem const *item, SnapPointsIter p)
749 {
750 NR::Rect const bbox = item->invokeBbox(sp_item_i2d_affine(item));
751 /* Just a pair of opposite corners of the bounding box suffices given that we don't yet
752 support angled guide lines. */
754 *p = bbox.min();
755 *p = bbox.max();
756 }
758 void sp_item_snappoints(SPItem const *item, SnapPointsIter p)
759 {
760 g_assert (item != NULL);
761 g_assert (SP_IS_ITEM(item));
763 SPItemClass const &item_class = *(SPItemClass const *) G_OBJECT_GET_CLASS(item);
764 if (item_class.snappoints) {
765 item_class.snappoints(item, p);
766 }
767 }
769 void
770 sp_item_invoke_print(SPItem *item, SPPrintContext *ctx)
771 {
772 if (!item->isHidden()) {
773 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->print) {
774 if (!item->transform.test_identity()
775 || SP_OBJECT_STYLE(item)->opacity.value != SP_SCALE24_MAX)
776 {
777 sp_print_bind(ctx, item->transform, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
778 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
779 sp_print_release(ctx);
780 } else {
781 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->print(item, ctx);
782 }
783 }
784 }
785 }
787 static gchar *
788 sp_item_private_description(SPItem *item)
789 {
790 return g_strdup(_("Object"));
791 }
793 /**
794 * Returns a string suitable for status bar, formatted in pango markup language.
795 *
796 * Must be freed by caller.
797 */
798 gchar *
799 sp_item_description(SPItem *item)
800 {
801 g_assert(item != NULL);
802 g_assert(SP_IS_ITEM(item));
804 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
805 gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
806 if (s && item->clip_ref->getObject()) {
807 gchar *snew = g_strdup_printf ("%s; <i>clipped</i>", s);
808 g_free (s);
809 s = snew;
810 }
811 if (s && item->mask_ref->getObject()) {
812 gchar *snew = g_strdup_printf ("%s; <i>masked</i>", s);
813 g_free (s);
814 s = snew;
815 }
816 return s;
817 }
819 g_assert_not_reached();
820 return NULL;
821 }
823 /**
824 * Allocates unique integer keys.
825 * \param numkeys Number of keys required.
826 * \return First allocated key; hence if the returned key is n
827 * you can use n, n + 1, ..., n + (numkeys - 1)
828 */
829 unsigned
830 sp_item_display_key_new(unsigned numkeys)
831 {
832 static unsigned dkey = 0;
834 dkey += numkeys;
836 return dkey - numkeys;
837 }
839 NRArenaItem *
840 sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
841 {
842 g_assert(item != NULL);
843 g_assert(SP_IS_ITEM(item));
844 g_assert(arena != NULL);
845 g_assert(NR_IS_ARENA(arena));
847 NRArenaItem *ai = NULL;
848 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->show) {
849 ai = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->show(item, arena, key, flags);
850 }
852 if (ai != NULL) {
853 item->display = sp_item_view_new_prepend(item->display, item, flags, key, ai);
854 nr_arena_item_set_transform(ai, item->transform);
855 nr_arena_item_set_opacity(ai, SP_SCALE24_TO_FLOAT(SP_OBJECT_STYLE(item)->opacity.value));
856 nr_arena_item_set_visible(ai, !item->isHidden());
857 nr_arena_item_set_sensitive(ai, item->sensitive);
858 if (item->clip_ref->getObject()) {
859 SPClipPath *cp = item->clip_ref->getObject();
861 if (!item->display->arenaitem->key) {
862 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
863 }
864 int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
866 // Show and set clip
867 NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
868 nr_arena_item_set_clip(ai, ac);
869 nr_arena_item_unref(ac);
871 // Update bbox, in case the clip uses bbox units
872 NRRect bbox;
873 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
874 sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
875 SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
876 }
877 if (item->mask_ref->getObject()) {
878 SPMask *mask = item->mask_ref->getObject();
880 if (!item->display->arenaitem->key) {
881 NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
882 }
883 int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
885 // Show and set mask
886 NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
887 nr_arena_item_set_mask(ai, ac);
888 nr_arena_item_unref(ac);
890 // Update bbox, in case the mask uses bbox units
891 NRRect bbox;
892 sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
893 sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
894 SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
895 }
896 NR_ARENA_ITEM_SET_DATA(ai, item);
897 }
899 return ai;
900 }
902 void
903 sp_item_invoke_hide(SPItem *item, unsigned key)
904 {
905 g_assert(item != NULL);
906 g_assert(SP_IS_ITEM(item));
908 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide) {
909 ((SPItemClass *) G_OBJECT_GET_CLASS(item))->hide(item, key);
910 }
912 SPItemView *ref = NULL;
913 SPItemView *v = item->display;
914 while (v != NULL) {
915 SPItemView *next = v->next;
916 if (v->key == key) {
917 if (item->clip_ref->getObject()) {
918 sp_clippath_hide(item->clip_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
919 nr_arena_item_set_clip(v->arenaitem, NULL);
920 }
921 if (item->mask_ref->getObject()) {
922 sp_mask_hide(item->mask_ref->getObject(), NR_ARENA_ITEM_GET_KEY(v->arenaitem));
923 nr_arena_item_set_mask(v->arenaitem, NULL);
924 }
925 if (!ref) {
926 item->display = v->next;
927 } else {
928 ref->next = v->next;
929 }
930 nr_arena_item_unparent(v->arenaitem);
931 nr_arena_item_unref(v->arenaitem);
932 g_free(v);
933 } else {
934 ref = v;
935 }
936 v = next;
937 }
938 }
940 // Adjusters
942 void
943 sp_item_adjust_pattern (SPItem *item, NR::Matrix const &postmul, bool set)
944 {
945 SPStyle *style = SP_OBJECT_STYLE (item);
947 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
948 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER (item);
949 if (SP_IS_PATTERN (server)) {
950 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "fill");
951 sp_pattern_transform_multiply (pattern, postmul, set);
952 }
953 }
955 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
956 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER (item);
957 if (SP_IS_PATTERN (server)) {
958 SPPattern *pattern = sp_pattern_clone_if_necessary (item, SP_PATTERN (server), "stroke");
959 sp_pattern_transform_multiply (pattern, postmul, set);
960 }
961 }
963 }
965 void
966 sp_item_adjust_gradient (SPItem *item, NR::Matrix const &postmul, bool set)
967 {
968 SPStyle *style = SP_OBJECT_STYLE (item);
970 if (style && (style->fill.type == SP_PAINT_TYPE_PAINTSERVER)) {
971 SPObject *server = SP_OBJECT_STYLE_FILL_SERVER(item);
972 if (SP_IS_GRADIENT (server)) {
974 /**
975 * \note Bbox units for a gradient are generally a bad idea because
976 * with them, you cannot preserve the relative position of the
977 * object and its gradient after rotation or skew. So now we
978 * convert them to userspace units which are easy to keep in sync
979 * just by adding the object's transform to gradientTransform.
980 * \todo FIXME: convert back to bbox units after transforming with
981 * the item, so as to preserve the original units.
982 */
983 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "fill");
985 sp_gradient_transform_multiply (gradient, postmul, set);
986 }
987 }
989 if (style && (style->stroke.type == SP_PAINT_TYPE_PAINTSERVER)) {
990 SPObject *server = SP_OBJECT_STYLE_STROKE_SERVER(item);
991 if (SP_IS_GRADIENT (server)) {
992 SPGradient *gradient = sp_gradient_convert_to_userspace (SP_GRADIENT (server), item, "stroke");
993 sp_gradient_transform_multiply (gradient, postmul, set);
994 }
995 }
996 }
998 void
999 sp_item_adjust_stroke (SPItem *item, gdouble ex)
1000 {
1001 SPStyle *style = SP_OBJECT_STYLE (item);
1003 if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
1005 style->stroke_width.computed *= ex;
1006 style->stroke_width.set = TRUE;
1008 if (style->stroke_dash.n_dash != 0) {
1009 int i;
1010 for (i = 0; i < style->stroke_dash.n_dash; i++) {
1011 style->stroke_dash.dash[i] *= ex;
1012 }
1013 style->stroke_dash.offset *= ex;
1014 }
1016 SP_OBJECT(item)->updateRepr();
1017 }
1018 }
1020 /**
1021 * Find out the inverse of previous transform of an item (from its repr)
1022 */
1023 NR::Matrix
1024 sp_item_transform_repr (SPItem *item)
1025 {
1026 NR::Matrix t_old(NR::identity());
1027 gchar const *t_attr = SP_OBJECT_REPR(item)->attribute("transform");
1028 if (t_attr) {
1029 NR::Matrix t;
1030 if (sp_svg_transform_read(t_attr, &t)) {
1031 t_old = t;
1032 }
1033 }
1035 return t_old;
1036 }
1039 /**
1040 * Recursively scale stroke width in \a item and its children by \a expansion.
1041 */
1042 void
1043 sp_item_adjust_stroke_width_recursive(SPItem *item, double expansion)
1044 {
1045 sp_item_adjust_stroke (item, expansion);
1047 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1048 if (SP_IS_ITEM(o))
1049 sp_item_adjust_stroke_width_recursive(SP_ITEM(o), expansion);
1050 }
1051 }
1053 /**
1054 * Recursively adjust rx and ry of rects.
1055 */
1056 void
1057 sp_item_adjust_rects_recursive(SPItem *item, NR::Matrix advertized_transform)
1058 {
1059 if (SP_IS_RECT (item)) {
1060 sp_rect_compensate_rxry (SP_RECT(item), advertized_transform);
1061 }
1063 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1064 if (SP_IS_ITEM(o))
1065 sp_item_adjust_rects_recursive(SP_ITEM(o), advertized_transform);
1066 }
1067 }
1069 /**
1070 * Recursively compensate pattern or gradient transform.
1071 */
1072 void
1073 sp_item_adjust_paint_recursive (SPItem *item, NR::Matrix advertized_transform, NR::Matrix t_ancestors, bool is_pattern)
1074 {
1075 // A clone must not touch the style (it's that of its parent!) and has no children, so quit now
1076 if (item && SP_IS_USE(item))
1077 return;
1079 // _Before_ full pattern/gradient transform: t_paint * t_item * t_ancestors
1080 // _After_ full pattern/gradient transform: t_paint_new * t_item * t_ancestors * advertised_transform
1081 // By equating these two expressions we get t_paint_new = t_paint * paint_delta, where:
1082 NR::Matrix t_item = sp_item_transform_repr (item);
1083 NR::Matrix paint_delta = t_item * t_ancestors * advertized_transform * t_ancestors.inverse() * t_item.inverse();
1085 if (is_pattern)
1086 sp_item_adjust_pattern (item, paint_delta);
1087 else
1088 sp_item_adjust_gradient (item, paint_delta);
1090 // Within text, we do not fork gradients, and so must not recurse to avoid double compensation
1091 if (item && SP_IS_TEXT(item))
1092 return;
1094 for (SPObject *o = SP_OBJECT(item)->children; o != NULL; o = o->next) {
1095 if (SP_IS_ITEM(o)) {
1096 // At the level of the transformed item, t_ancestors is identity;
1097 // below it, it is the accmmulated chain of transforms from this level to the top level
1098 sp_item_adjust_paint_recursive (SP_ITEM(o), advertized_transform, t_item * t_ancestors, is_pattern);
1099 }
1100 }
1101 }
1103 /**
1104 * A temporary wrapper for the next function accepting the NRMatrix
1105 * instead of NR::Matrix
1106 */
1107 void
1108 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NRMatrix const *transform, NR::Matrix const *adv)
1109 {
1110 if (transform == NULL)
1111 sp_item_write_transform(item, repr, NR::identity(), adv);
1112 else
1113 sp_item_write_transform(item, repr, NR::Matrix (transform), adv);
1114 }
1116 /**
1117 * Set a new transform on an object.
1118 *
1119 * Compensate for stroke scaling and gradient/pattern fill transform, if
1120 * necessary. Call the object's set_transform method if transforms are
1121 * stored optimized. Send _transformed_signal. Invoke _write method so that
1122 * the repr is updated with the new transform.
1123 */
1124 void
1125 sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix const &transform, NR::Matrix const *adv)
1126 {
1127 g_return_if_fail(item != NULL);
1128 g_return_if_fail(SP_IS_ITEM(item));
1129 g_return_if_fail(repr != NULL);
1131 // calculate the relative transform, if not given by the adv attribute
1132 NR::Matrix advertized_transform;
1133 if (adv != NULL) {
1134 advertized_transform = *adv;
1135 } else {
1136 advertized_transform = sp_item_transform_repr (item).inverse() * transform;
1137 }
1139 // recursively compensate for stroke scaling, depending on user preference
1140 if (prefs_get_int_attribute("options.transform", "stroke", 1) == 0) {
1141 double const expansion = 1. / NR::expansion(advertized_transform);
1142 sp_item_adjust_stroke_width_recursive(item, expansion);
1143 }
1145 // recursively compensate rx/ry of a rect if requested
1146 if (prefs_get_int_attribute("options.transform", "rectcorners", 1) == 0) {
1147 sp_item_adjust_rects_recursive(item, advertized_transform);
1148 }
1150 // recursively compensate pattern fill if it's not to be transformed
1151 if (prefs_get_int_attribute("options.transform", "pattern", 1) == 0) {
1152 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), true);
1153 }
1154 /// \todo FIXME: add the same else branch as for gradients below, to convert patterns to userSpaceOnUse as well
1155 /// recursively compensate gradient fill if it's not to be transformed
1156 if (prefs_get_int_attribute("options.transform", "gradient", 1) == 0) {
1157 sp_item_adjust_paint_recursive (item, advertized_transform.inverse(), NR::identity(), false);
1158 } else {
1159 // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
1160 // it here _before_ the new transform is set, so as to use the pre-transform bbox
1161 sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
1162 }
1164 // run the object's set_transform if transforms are stored optimized and there's no clippath or mask
1165 gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
1166 NR::Matrix transform_attr (transform);
1167 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform
1168 && !preserve && !item->clip_ref->getObject() && !item->mask_ref->getObject()) {
1169 transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
1170 }
1171 sp_item_set_item_transform(item, transform_attr);
1173 // Note: updateRepr comes before emitting the transformed signal since
1174 // it causes clone SPUse's copy of the original object to brought up to
1175 // date with the original. Otherwise, sp_use_bbox returns incorrect
1176 // values if called in code handling the transformed signal.
1177 SP_OBJECT(item)->updateRepr();
1179 // send the relative transform with a _transformed_signal
1180 item->_transformed_signal.emit(&advertized_transform, item);
1181 }
1183 gint
1184 sp_item_event(SPItem *item, SPEvent *event)
1185 {
1186 g_return_val_if_fail(item != NULL, FALSE);
1187 g_return_val_if_fail(SP_IS_ITEM(item), FALSE);
1188 g_return_val_if_fail(event != NULL, FALSE);
1190 if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->event)
1191 return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->event(item, event);
1193 return FALSE;
1194 }
1196 /**
1197 * Sets item private transform (not propagated to repr), without compensating stroke widths,
1198 * gradients, patterns as sp_item_write_transform does.
1199 */
1200 void
1201 sp_item_set_item_transform(SPItem *item, NR::Matrix const &transform)
1202 {
1203 g_return_if_fail(item != NULL);
1204 g_return_if_fail(SP_IS_ITEM(item));
1206 if (!matrix_equalp(transform, item->transform, NR_EPSILON)) {
1207 item->transform = transform;
1208 /* The SP_OBJECT_USER_MODIFIED_FLAG_B is used to mark the fact that it's only a
1209 transformation. It's apparently not used anywhere else. */
1210 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_USER_MODIFIED_FLAG_B);
1211 sp_item_rm_unsatisfied_cns(*item);
1212 }
1213 }
1216 /**
1217 * \pre \a ancestor really is an ancestor (\>=) of \a object, or NULL.
1218 * ("Ancestor (\>=)" here includes as far as \a object itself.)
1219 */
1220 NR::Matrix
1221 i2anc_affine(SPObject const *object, SPObject const *const ancestor) {
1222 NR::Matrix ret(NR::identity());
1223 g_return_val_if_fail(object != NULL, ret);
1225 /* stop at first non-renderable ancestor */
1226 while ( object != ancestor && SP_IS_ITEM(object) ) {
1227 if (SP_IS_ROOT(object)) {
1228 ret *= SP_ROOT(object)->c2p;
1229 }
1230 ret *= SP_ITEM(object)->transform;
1231 object = SP_OBJECT_PARENT(object);
1232 }
1233 return ret;
1234 }
1236 NR::Matrix
1237 i2i_affine(SPObject const *src, SPObject const *dest) {
1238 g_return_val_if_fail(src != NULL && dest != NULL, NR::identity());
1239 SPObject const *ancestor = src->nearestCommonAncestor(dest);
1240 return i2anc_affine(src, ancestor) / i2anc_affine(dest, ancestor);
1241 }
1243 NR::Matrix SPItem::getRelativeTransform(SPObject const *dest) const {
1244 return i2i_affine(this, dest);
1245 }
1247 /**
1248 * Returns the accumulated transformation of the item and all its ancestors, including root's viewport.
1249 * \pre (item != NULL) and SP_IS_ITEM(item).
1250 */
1251 NR::Matrix sp_item_i2doc_affine(SPItem const *item)
1252 {
1253 return i2anc_affine(item, NULL);
1254 }
1256 /**
1257 * Returns the accumulated transformation of the item and all its ancestors, but excluding root's viewport.
1258 * Used in path operations mostly.
1259 * \pre (item != NULL) and SP_IS_ITEM(item).
1260 */
1261 NR::Matrix sp_item_i2root_affine(SPItem const *item)
1262 {
1263 g_assert(item != NULL);
1264 g_assert(SP_IS_ITEM(item));
1266 NR::Matrix ret(NR::identity());
1267 g_assert(ret.test_identity());
1268 while ( NULL != SP_OBJECT_PARENT(item) ) {
1269 ret *= item->transform;
1270 item = SP_ITEM(SP_OBJECT_PARENT(item));
1271 }
1272 g_assert(SP_IS_ROOT(item));
1274 ret *= item->transform;
1276 return ret;
1277 }
1279 /* fixme: This is EVIL!!! */
1281 NR::Matrix sp_item_i2d_affine(SPItem const *item)
1282 {
1283 g_assert(item != NULL);
1284 g_assert(SP_IS_ITEM(item));
1286 NR::Matrix const ret( sp_item_i2doc_affine(item)
1287 * NR::scale(1, -1)
1288 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1289 return ret;
1290 }
1292 // same as i2d but with i2root instead of i2doc
1293 NR::Matrix sp_item_i2r_affine(SPItem const *item)
1294 {
1295 g_assert(item != NULL);
1296 g_assert(SP_IS_ITEM(item));
1298 NR::Matrix const ret( sp_item_i2root_affine(item)
1299 * NR::scale(1, -1)
1300 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item))) );
1301 return ret;
1302 }
1304 /**
1305 * Converts a matrix \a m into the desktop coords of the \a item.
1306 * Will become a noop when we eliminate the coordinate flipping.
1307 */
1308 NR::Matrix matrix_to_desktop(NR::Matrix const m, SPItem const *item)
1309 {
1310 NR::Matrix const ret(m
1311 * NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1312 * NR::scale(1, -1));
1313 return ret;
1314 }
1316 /**
1317 * Converts a matrix \a m from the desktop coords of the \a item.
1318 * Will become a noop when we eliminate the coordinate flipping.
1319 */
1320 NR::Matrix matrix_from_desktop(NR::Matrix const m, SPItem const *item)
1321 {
1322 NR::Matrix const ret(NR::scale(1, -1)
1323 * NR::translate(0, sp_document_height(SP_OBJECT_DOCUMENT(item)))
1324 * m);
1325 return ret;
1326 }
1328 void sp_item_set_i2d_affine(SPItem *item, NR::Matrix const &i2dt)
1329 {
1330 g_return_if_fail( item != NULL );
1331 g_return_if_fail( SP_IS_ITEM(item) );
1333 NR::Matrix dt2p; /* desktop to item parent transform */
1334 if (SP_OBJECT_PARENT(item)) {
1335 dt2p = sp_item_i2d_affine((SPItem *) SP_OBJECT_PARENT(item)).inverse();
1336 } else {
1337 dt2p = ( NR::translate(0, -sp_document_height(SP_OBJECT_DOCUMENT(item)))
1338 * NR::scale(1, -1) );
1339 }
1341 NR::Matrix const i2p( i2dt * dt2p );
1342 sp_item_set_item_transform(item, i2p);
1343 }
1346 NR::Matrix
1347 sp_item_dt2i_affine(SPItem const *item)
1348 {
1349 /* fixme: Implement the right way (Lauris) */
1350 return sp_item_i2d_affine(item).inverse();
1351 }
1353 /* Item views */
1355 static SPItemView *
1356 sp_item_view_new_prepend(SPItemView *list, SPItem *item, unsigned flags, unsigned key, NRArenaItem *arenaitem)
1357 {
1358 SPItemView *new_view;
1360 g_assert(item != NULL);
1361 g_assert(SP_IS_ITEM(item));
1362 g_assert(arenaitem != NULL);
1363 g_assert(NR_IS_ARENA_ITEM(arenaitem));
1365 new_view = g_new(SPItemView, 1);
1367 new_view->next = list;
1368 new_view->flags = flags;
1369 new_view->key = key;
1370 new_view->arenaitem = nr_arena_item_ref(arenaitem);
1372 return new_view;
1373 }
1375 static SPItemView *
1376 sp_item_view_list_remove(SPItemView *list, SPItemView *view)
1377 {
1378 if (view == list) {
1379 list = list->next;
1380 } else {
1381 SPItemView *prev;
1382 prev = list;
1383 while (prev->next != view) prev = prev->next;
1384 prev->next = view->next;
1385 }
1387 nr_arena_item_unref(view->arenaitem);
1388 g_free(view);
1390 return list;
1391 }
1393 /**
1394 * Return the arenaitem corresponding to the given item in the display
1395 * with the given key
1396 */
1397 NRArenaItem *
1398 sp_item_get_arenaitem(SPItem *item, unsigned key)
1399 {
1400 for ( SPItemView *iv = item->display ; iv ; iv = iv->next ) {
1401 if ( iv->key == key ) {
1402 return iv->arenaitem;
1403 }
1404 }
1406 return NULL;
1407 }
1409 int
1410 sp_item_repr_compare_position(SPItem *first, SPItem *second)
1411 {
1412 return sp_repr_compare_position(SP_OBJECT_REPR(first),
1413 SP_OBJECT_REPR(second));
1414 }
1416 SPItem *
1417 sp_item_first_item_child (SPObject *obj)
1418 {
1419 for ( SPObject *iter = sp_object_first_child(obj) ; iter ; iter = SP_OBJECT_NEXT(iter)) {
1420 if (SP_IS_ITEM (iter))
1421 return SP_ITEM (iter);
1422 }
1423 return NULL;
1424 }
1427 /*
1428 Local Variables:
1429 mode:c++
1430 c-file-style:"stroustrup"
1431 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1432 indent-tabs-mode:nil
1433 fill-column:99
1434 End:
1435 */
1436 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :